@@ -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
0 commit comments