Skip to content

Commit 6386daf

Browse files
committed
Fixed the SPqcd issue.
1 parent e96d6da commit 6386daf

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/core/codestream/ojph_params.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,10 @@ namespace ojph {
786786
ui32 bit_depth = 32;
787787
if (reversible) {
788788
bit_depth = siz->get_bit_depth(comp_num);
789-
bit_depth += employing_color_transform + get_num_decompositions();
789+
bit_depth += comp_num < 3 ? employing_color_transform : 0;
790+
// 3 or 4 is how many extra bits are needed for the HH band at the
791+
// bottom most level of decomposition.
792+
bit_depth += get_num_decompositions() > 5 ? 4 : 3;
790793
}
791794

792795
return bit_depth;
@@ -945,23 +948,46 @@ namespace ojph {
945948
void param_qcd::set_rev_quant(ui32 num_decomps, ui32 bit_depth,
946949
bool is_employing_color_transform)
947950
{
948-
int guard_bits = 1;
949-
Sqcd = (ui8)(guard_bits << 5); //one guard bit, and no quantization
950951
ui32 B = bit_depth;
951952
B += is_employing_color_transform ? 1 : 0; //1 bit for RCT
952953
int s = 0;
953954
double bibo_l = bibo_gains::get_bibo_gain_l(num_decomps, true);
954955
ui32 X = (ui32) ceil(log(bibo_l * bibo_l) / M_LN2);
955-
u8_SPqcd[s++] = encode_SPqcd((ui8)(B + X));
956+
u8_SPqcd[s++] = (ui8)(B + X);
957+
ui32 max_B_plus_X = (ui32)(B + X);
956958
for (ui32 d = num_decomps; d > 0; --d)
957959
{
958960
double bibo_l = bibo_gains::get_bibo_gain_l(d, true);
959961
double bibo_h = bibo_gains::get_bibo_gain_h(d - 1, true);
960962
X = (ui32) ceil(log(bibo_h * bibo_l) / M_LN2);
961-
u8_SPqcd[s++] = encode_SPqcd((ui8)(B + X));
962-
u8_SPqcd[s++] = encode_SPqcd((ui8)(B + X));
963+
u8_SPqcd[s++] = (ui8)(B + X);
964+
max_B_plus_X = ojph_max(max_B_plus_X, B + X);
965+
u8_SPqcd[s++] = (ui8)(B + X);
966+
max_B_plus_X = ojph_max(max_B_plus_X, B + X);
963967
X = (ui32) ceil(log(bibo_h * bibo_h) / M_LN2);
964-
u8_SPqcd[s++] = encode_SPqcd((ui8)(B + X));
968+
u8_SPqcd[s++] = (ui8)(B + X);
969+
max_B_plus_X = ojph_max(max_B_plus_X, B + X);
970+
}
971+
972+
if (max_B_plus_X > 38)
973+
OJPH_ERROR(0x00050151, "The specified combination of bit_depth, "
974+
"colour transform, and type of wavelet transform requires more than "
975+
"38 bits; it requires %d bits. This is beyond what is allowed in "
976+
"the JPEG2000 image coding format.", max_B_plus_X);
977+
978+
int guard_bits = ojph_max(1, (si32)max_B_plus_X - 31);
979+
Sqcd = (ui8)(guard_bits << 5);
980+
s = 0;
981+
u8_SPqcd[s] = encode_SPqcd((ui8)(u8_SPqcd[s] - guard_bits));
982+
s++;
983+
for (ui32 d = num_decomps; d > 0; --d)
984+
{
985+
u8_SPqcd[s] = encode_SPqcd((ui8)(u8_SPqcd[s] - guard_bits));
986+
s++;
987+
u8_SPqcd[s] = encode_SPqcd((ui8)(u8_SPqcd[s] - guard_bits));
988+
s++;
989+
u8_SPqcd[s] = encode_SPqcd((ui8)(u8_SPqcd[s] - guard_bits));
990+
s++;
965991
}
966992
}
967993

src/core/codestream/ojph_params_local.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ namespace ojph {
596596
{
597597
friend ::ojph::param_qcd;
598598
public:
599-
param_qcd() : reversible_SPqcd_shift(3), old_SPqcd(false)
599+
param_qcd()
600600
{
601601
Lqcd = 0;
602602
Sqcd = 0;
@@ -650,23 +650,12 @@ namespace ojph {
650650
void set_irrev_quant(ui32 num_decomps);
651651

652652
ui8 decode_SPqcd(ui8 v) const
653-
{
654-
if (old_SPqcd) return (ui8)(v >> reversible_SPqcd_shift); // old
655-
else {
656-
v = v & 0b11111011;
657-
return (ui8)((v << 5) | (v >> 3)); // new
658-
}
659-
}
653+
{ return (ui8)(v >> 3); }
660654
ui8 encode_SPqcd(ui8 v) const
661-
{
662-
if (old_SPqcd) return (ui8)(v << reversible_SPqcd_shift); // old
663-
else return (ui8)((v >> 5) | (v << 3)); // new
664-
}
655+
{ return (ui8)(v << 3); }
665656
protected:
666657
ui16 Lqcd;
667658
ui8 Sqcd;
668-
const ui8 reversible_SPqcd_shift;
669-
const bool old_SPqcd;
670659
union
671660
{
672661
ui8 u8_SPqcd[97];

0 commit comments

Comments
 (0)