Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/codestream/ojph_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,20 @@ namespace ojph {
if (buf4_d_min >= buf4_d_max)
OJPH_ERROR(0x00050146, "In an NLT marker segment, Dmin must smaller "
"than Dmax; here we have %d and %d", buf4_d_min, buf4_d_max);
// Dmin and Dmax are mapped to floats in [0, 1] when decoding, and
// adjacent 32-bit values (such as 0xFFFFFFFE and 0xFFFFFFFF) collapse
// to the same float. prepare_for_decoding would then divide by a
// zero float range, making inv_delta infinite and every LUT index
// NaN, which indexes the decode table out of bounds. Reject the
// segment while the field is still being parsed.
{
double d = 1.0 / (double)((1ull << 32) - 1);
if ((float)((double)buf4_d_min * d) ==
(float)((double)buf4_d_max * d))
OJPH_ERROR(0x00050149, "In an NLT marker segment, Dmin and Dmax "
"are too close to be represented as distinct values; here we "
"have %u and %u", buf4_d_min, buf4_d_max);
}
if (buf_pt_val == 0 || buf_pt_val > 32)
OJPH_ERROR(0x00050147, "In an NLT marker segment, the value of "
"pt_val must be larger than 0 and smaller or equal to 32; "
Expand Down
51 changes: 49 additions & 2 deletions tests/test_nlt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,10 @@ namespace {
// write that combination, so a codestream that carries it has to be put
// together here, the way another encoder may have produced it.
///////////////////////////////////////////////////////////////////////////
bool insert_lut_nlt_marker(const std::string& filename, ui8 nlt_type)
bool insert_lut_nlt_marker(const std::string& filename, ui8 nlt_type,
ui32 d_min = 0, ui32 d_max = 0xFFFFFFFFu)
{
const ui16 num_points = 17;
const ui32 d_min = 0, d_max = 0xFFFFFFFFu;
const ui32 length = 17u + 4u * (ui32)num_points; // as the library does

std::vector<ui8> marker;
Expand Down Expand Up @@ -1653,6 +1653,53 @@ TEST(NltTest, LutStyleWithReversibleWaveletIsReportedWhenRead)
cs.close();
}

///////////////////////////////////////////////////////////////////////////////
// Dmin and Dmax are legal as distinct 32-bit integers but are mapped to floats
// in [0, 1] when the table is prepared for decoding, and two adjacent values
// such as 0xFFFFFFFE and 0xFFFFFFFF round to the same float. The float range
// of the table is then zero, inv_delta is infinite and every table index is
// NaN, which indexes the decode table out of bounds in the irreversible type 2
// and type 4 decode paths. A codestream that carries such a segment, which
// another encoder may have produced, has to be rejected when its headers are
// read rather than reach the decoder.
///////////////////////////////////////////////////////////////////////////////
TEST(NltTest, LutStyleWithDegenerateRangeIsRejected)
{
test_image img;
make_synthetic_image(img, 64, 64);

const ui8 types[2] = { param_nlt::OJPH_NLT_LUT_STYLE_NLT,
param_nlt::OJPH_NLT_BINARY_COMPLEMENT_PLUS_LUT };
for (size_t i = 0; i < 2; ++i)
{
// an irreversible codestream without a nonlinearity, into which an NLT
// marker segment for a LUT style nonlinearity with a collapsing range is
// inserted
nlt_setting setting;
setting.type = param_nlt::OJPH_NLT_NO_NLT;
setting.use_pfm_lut = true;
setting.use_approx_inverse = true;
setting.reversible = false;
setting.qstep = default_qstep();

const std::string filename = OUT_FILE_DIR "nlt_degenerate.j2c";
encode_image(filename, img, setting);
ASSERT_TRUE(insert_lut_nlt_marker(filename, types[i],
0xFFFFFFFEu, 0xFFFFFFFFu)) << "nonlinearity " << (int)types[i];

message_capture capture;
codestream cs;
j2c_infile file;
file.open(filename.c_str());
EXPECT_THROW(cs.read_headers(&file), std::runtime_error)
<< "nonlinearity " << (int)types[i];
EXPECT_GE(capture.errors.num_messages, 1);
EXPECT_EQ(capture.errors.last_code, 0x00050149u)
<< std::hex << "error code " << capture.errors.last_code;
cs.close();
}
}

///////////////////////////////////////////////////////////////////////////////
// A component can be given an NLT record of type 0, no nonlinearity, of its
// own, to keep it out of the nonlinearity the other components use. Such a
Expand Down
Loading