Skip to content

Commit af15dd0

Browse files
committed
Modified NLT, NLT Validity needs work
1 parent 579d7dd commit af15dd0

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

src/core/codestream/ojph_params.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,17 @@ namespace ojph {
372372
//
373373
////////////////////////////////////////////////////////////////////////////
374374

375-
//////////////////////////////////////////////////////////////////////////
376-
void param_nlt::set_type3_transformation(ui32 comp_num, bool enable)
375+
////////////////////////////////////////////////////////////////////////////
376+
void param_nlt::set_nonlinearity(ui32 comp_num, nonlinearity type)
377377
{
378-
state->set_type3_transformation(comp_num, enable);
378+
state->set_nonlinearity(comp_num, type);
379379
}
380380

381-
//////////////////////////////////////////////////////////////////////////
382-
bool param_nlt::get_type3_transformation(ui32 comp_num, ui8& bit_depth,
383-
bool& is_signed)
381+
////////////////////////////////////////////////////////////////////////////
382+
bool param_nlt::get_nonlinearity(ui32 comp_num, ui8& bit_depth,
383+
bool& is_signed, nonlinearity& type) const
384384
{
385-
return state->get_type3_transformation(comp_num, bit_depth, is_signed);
385+
return state->get_nonlinearity(comp_num, bit_depth, is_signed, type);
386386
}
387387

388388
////////////////////////////////////////////////////////////////////////////
@@ -1333,7 +1333,7 @@ namespace ojph {
13331333

13341334
// first stage; find out if all components captured by the default
13351335
// entry (ALL_COMPS) has the same bit_depth/signedness,
1336-
// while doing this, set the BDnlt for components not captured but the
1336+
// while doing this, set the BDnlt for components not captured by the
13371337
// default entry (ALL_COMPS)
13381338
ui32 bit_depth = 0; // unknown yet
13391339
bool is_signed = false; // unknown yet
@@ -1405,23 +1405,29 @@ namespace ojph {
14051405

14061406
trim_non_existing_components(num_comps);
14071407

1408-
if (is_any_enabled() == false)
1409-
return;
1410-
siz.set_Rsiz_flag(param_siz::RSIZ_EXT_FLAG | param_siz::RSIZ_NLT_FLAG);
1408+
if (is_any_enabled() == true)
1409+
siz.set_Rsiz_flag(param_siz::RSIZ_EXT_FLAG | param_siz::RSIZ_NLT_FLAG);
14111410
}
14121411

14131412
//////////////////////////////////////////////////////////////////////////
1414-
void param_nlt::set_type3_transformation(ui32 comp_num, bool enable)
1413+
void param_nlt::set_nonlinearity(ui32 comp_num, nonlinearity type)
14151414
{
1415+
if (type != ojph::param_nlt::OJPH_NLT_NO_NLT &&
1416+
type != ojph::param_nlt::OJPH_NLT_BINARY_COMPLEMENT_NLT)
1417+
OJPH_ERROR(0x00050171, "Nonliearity other than types 0 "
1418+
"(No Nonlinearity) and 3 (Binary Binary Complement to Sign Magnitude "
1419+
"Conversion) is not supported yet");
14161420
param_nlt* p = get_comp_object(comp_num);
14171421
if (p == NULL)
14181422
p = add_object(comp_num);
1419-
p->enabled = enable;
1423+
p->Tnlt = type;
1424+
p->enabled = true;
14201425
}
14211426

14221427
//////////////////////////////////////////////////////////////////////////
1423-
bool param_nlt::get_type3_transformation(ui32 comp_num, ui8& bit_depth,
1424-
bool& is_signed) const
1428+
bool
1429+
param_nlt::get_nonlinearity(ui32 comp_num, ui8& bit_depth, bool& is_signed,
1430+
nonlinearity& type) const
14251431
{
14261432
const param_nlt* p = get_comp_object(comp_num);
14271433
p = p ? p : this;
@@ -1430,8 +1436,10 @@ namespace ojph {
14301436
bit_depth = (ui8)((p->BDnlt & 0x7F) + 1);
14311437
bit_depth = bit_depth <= 38 ? bit_depth : 38;
14321438
is_signed = (p->BDnlt & 0x80) == 0x80;
1439+
type = (nonlinearity)p->Tnlt;
1440+
return true;
14331441
}
1434-
return p->enabled;
1442+
return false;
14351443
}
14361444

14371445
//////////////////////////////////////////////////////////////////////////
@@ -1499,14 +1507,10 @@ namespace ojph {
14991507
//////////////////////////////////////////////////////////////////////////
15001508
const param_nlt* param_nlt::get_comp_object(ui32 comp_num) const
15011509
{
1502-
if (Cnlt == comp_num)
1503-
return this;
1504-
else {
1505-
param_nlt* p = next;
1506-
while (p && p->Cnlt != comp_num)
1507-
p = p->next;
1508-
return p;
1509-
}
1510+
const param_nlt* p = this;
1511+
while (p && p->Cnlt != comp_num)
1512+
p = p->next;
1513+
return p;
15101514
}
15111515

15121516
//////////////////////////////////////////////////////////////////////////
@@ -1540,8 +1544,11 @@ namespace ojph {
15401544
{
15411545
param_nlt* p = this->next;
15421546
while (p) {
1543-
if (p->enabled == true && p->Cnlt >= num_comps)
1547+
if (p->enabled == true && p->Cnlt >= num_comps) {
15441548
p->enabled = false;
1549+
OJPH_INFO(0x00050161, "We are removing the NLT marker segment "
1550+
"for the non-existing component %d", p->Cnlt);
1551+
}
15451552
p = p->next;
15461553
}
15471554
}

src/core/codestream/ojph_params_local.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,13 @@ namespace ojph {
673673
struct param_nlt
674674
{
675675
using special_comp_num = ojph::param_nlt::special_comp_num;
676+
using nonlinearity = ojph::param_nlt::nonlinearity;
676677
public:
677678
param_nlt() {
678679
Lnlt = 6;
679680
Cnlt = special_comp_num::ALL_COMPS; // default
680681
BDnlt = 0;
681-
Tnlt = 3;
682+
Tnlt = nonlinearity::OJPH_NLT_NO_NLT;
682683
enabled = false; next = NULL; alloced_next = false;
683684
}
684685

@@ -691,9 +692,9 @@ namespace ojph {
691692
}
692693

693694
void check_validity(param_siz& siz);
694-
void set_type3_transformation(ui32 comp_num, bool enable);
695-
bool get_type3_transformation(ui32 comp_num, ui8& bit_depth,
696-
bool& is_signed) const;
695+
void set_nonlinearity(ui32 comp_num, nonlinearity type);
696+
bool get_nonlinearity(ui32 comp_num, ui8& bit_depth,
697+
bool& is_signed, nonlinearity& type) const;
697698
bool write(outfile_base* file) const;
698699
void read(infile_base* file);
699700

src/core/common/ojph_params.h

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,66 @@ namespace ojph {
141141
{
142142
public:
143143
enum special_comp_num : ui16 { ALL_COMPS = 65535 };
144+
enum nonlinearity : ui8 {
145+
OJPH_NLT_NO_NLT = 0, // supported
146+
OJPH_NLT_GAMMA_STYLE_NLT = 1, // not supported
147+
OJPH_NLT_LUT_STYLE_NLT = 2, // not supported
148+
OJPH_NLT_BINARY_COMPLEMENT_NLT = 3 // supported
149+
};
144150
public:
145151
param_nlt(local::param_nlt* p) : state(p) {}
146152

147153
/**
148154
* @brief enables or disables type 3 nonlinearity for a component
149155
* or the default setting
150156
*
151-
* If you think that you need type 3 nonlinearity for all components,
152-
* call this function with comp_num set to 65535 and enable to true.
157+
* When creating a codestream for writing, call this function before
158+
* you call codestream::write_headers.
159+
*
160+
* There are a few things to know here.
161+
* The NLT marker segment contains the nonlinearity type and the
162+
* bit depth and signedness of the component to which it applies.
163+
* There is the default component ALL_COMPS which applies to all
164+
* components unless it is overridden by another NLT segment marker.
165+
* The library checks that the settings make sense, and also make
166+
* sure that bit depth and signedness are correct, creating any missing
167+
* NLT marker segments.
168+
* If all components have the same bit depth and signedness, and need
169+
* nonlinearity type 3 (Binary Complement to Sign Magnitude Conversion),
170+
* then the best option is to set ALL_COMPS to type 3.
171+
* Otherwise, the best option is to set type 3 only to components that
172+
* need it, leaving out the default ALL_COMPS nonlinearity not set.
173+
* Another option is for the end-user can set the ALL_COMPS to type 3,
174+
* and then put exception for the components that does not need type 3,
175+
* by setting them to type 0.
176+
*
177+
* It also worth noting that type 3 nonlinearity has no effect on
178+
* positive image samples. It is also not recommended for integer-valued
179+
* types. It is only recommended for floating-points image samples for
180+
* which some of the samples are negative, where type 3 nonlinearity
181+
* should be beneficial. This is because the encoding engine expects
182+
* two-complement representation for negative values while floating point
183+
* numbers have a sign bit followed by an exponent, which has a biased
184+
* integer representation. The core idea is to make floating-point
185+
* representation more compatible with integer representation.
153186
*
154187
* @param comp_num: component number, or 65535 for the default setting
155-
* @param enable: true to enable nlt type 3 for this component or the
156-
default setting, false to disable nlt type 3.
188+
* @param type: desired non-linearity from enum nonlinearity
157189
*/
158-
void set_type3_transformation(ui32 comp_num, bool enable);
190+
void set_nonlinearity(ui32 comp_num, nonlinearity type);
159191

160192
/**
161-
* @brief get the state (enabled or disabled) of type 3 nonlinearity
162-
* for a component or the default setting
193+
* @brief get the nonlinearity type associated with comp_num, which
194+
* should be one from enum nonlinearity
163195
*
164196
* @param comp_num: component number, or 65535 for the default setting
165197
* @param bit_depth: returns the bit depth of the component/default
166198
* @param is_signed: returns true if the component/default is signed
167-
* @return true if enabled or false if not.
199+
* @param type: nonlinearity type
200+
* @return true if the nonlinearity for comp_num is set
168201
*/
169-
bool get_type3_transformation(ui32 comp_num, ui8& bit_depth,
170-
bool& is_signed);
202+
bool get_nonlinearity(ui32 comp_num, ui8& bit_depth,
203+
bool& is_signed, nonlinearity& type) const;
171204

172205
private:
173206
local::param_nlt* state;

0 commit comments

Comments
 (0)