38#define _USE_MATH_DEFINES
94 ui32 bit_depth,
bool is_signed)
170 if (num_decompositions > 32)
172 "maximum number of decompositions cannot exceed 32");
181 if (width == 0 || width != (1u << log_width)
182 || height == 0 || height != (1u << log_height)
183 || log_width < 2 || log_height < 2
184 || log_width + log_height > 12)
185 OJPH_ERROR(0x00050011,
"incorrect code block dimensions");
193 if (num_levels == 0 || precinct_size == NULL)
200 size t = precinct_size[i < num_levels ? i : num_levels - 1];
204 if (t.
w == 0 || t.
h == 0)
205 OJPH_ERROR(0x00050021,
"precinct width or height cannot be 0");
206 if (t.
w != (1u<<PPx) || t.
h != (1u<<PPy))
208 "precinct width and height should be a power of 2");
209 if (PPx > 15 || PPy > 15)
210 OJPH_ERROR(0x00050023,
"precinct size is too large");
211 if (i > 0 && (PPx == 0 || PPy == 0))
212 OJPH_ERROR(0x00050024,
"precinct size is too small");
222 size_t len = strlen(name);
236 OJPH_ERROR(0x00050031,
"unknown progression order");
239 OJPH_ERROR(0x00050032,
"improper progression order");
373 size_t t = strlen(str);
376 "COM marker string length cannot be larger than 65531");
387 "COM marker string length cannot be larger than 65531");
407 return (
ui16)((t << 8) | (t >> 8));
447 1.4021e+00f, 2.0304e+00f, 2.9012e+00f, 4.1153e+00f, 5.8245e+00f,
448 8.2388e+00f, 1.1652e+01f, 1.6479e+01f, 2.3304e+01f, 3.2957e+01f,
449 4.6609e+01f, 6.5915e+01f, 9.3217e+01f, 1.3183e+02f, 1.8643e+02f,
450 2.6366e+02f, 3.7287e+02f, 5.2732e+02f, 7.4574e+02f, 1.0546e+03f,
451 1.4915e+03f, 2.1093e+03f, 2.9830e+03f, 4.2185e+03f, 5.9659e+03f,
452 8.4371e+03f, 1.1932e+04f, 1.6874e+04f, 2.3864e+04f, 3.3748e+04f,
453 4.7727e+04f, 6.7496e+04f, 9.5454e+04f };
455 1.9669e+00f, 2.8839e+00f, 4.1475e+00f, 5.8946e+00f, 8.3472e+00f,
456 1.1809e+01f, 1.6701e+01f, 2.3620e+01f, 3.3403e+01f, 4.7240e+01f,
457 6.6807e+01f, 9.4479e+01f, 1.3361e+02f, 1.8896e+02f, 2.6723e+02f,
458 3.7792e+02f, 5.3446e+02f, 7.5583e+02f, 1.0689e+03f, 1.5117e+03f,
459 2.1378e+03f, 3.0233e+03f, 4.2756e+03f, 6.0467e+03f, 8.5513e+03f,
460 1.2093e+04f, 1.7103e+04f, 2.4187e+04f, 3.4205e+04f, 4.8373e+04f,
461 6.8410e+04f, 9.6747e+04f, 1.3682e+05f };
463 1.2247e+00f, 1.3229e+00f, 1.5411e+00f, 1.7139e+00f, 1.9605e+00f,
464 2.2044e+00f, 2.5047e+00f, 2.8277e+00f, 3.2049e+00f, 3.6238e+00f,
465 4.1033e+00f, 4.6423e+00f, 5.2548e+00f, 5.9462e+00f, 6.7299e+00f,
466 7.6159e+00f, 8.6193e+00f, 9.7544e+00f, 1.1039e+01f, 1.2493e+01f,
467 1.4139e+01f, 1.6001e+01f, 1.8108e+01f, 2.0493e+01f, 2.3192e+01f,
468 2.6246e+01f, 2.9702e+01f, 3.3614e+01f, 3.8041e+01f, 4.3051e+01f,
469 4.8721e+01f, 5.5138e+01f, 6.2399e+01f };
471 1.3975e+00f, 1.4389e+00f, 1.7287e+00f, 1.8880e+00f, 2.1841e+00f,
472 2.4392e+00f, 2.7830e+00f, 3.1341e+00f, 3.5576e+00f, 4.0188e+00f,
473 4.5532e+00f, 5.1494e+00f, 5.8301e+00f, 6.5963e+00f, 7.4663e+00f,
474 8.4489e+00f, 9.5623e+00f, 1.0821e+01f, 1.2247e+01f, 1.3860e+01f,
475 1.5685e+01f, 1.7751e+01f, 2.0089e+01f, 2.2735e+01f, 2.5729e+01f,
476 2.9117e+01f, 3.2952e+01f, 3.7292e+01f, 4.2203e+01f, 4.7761e+01f,
477 5.4051e+01f, 6.1170e+01f, 6.9226e+01f };
498 1.3328e+00f, 1.3067e+00f, 1.3028e+00f, 1.3001e+00f, 1.2993e+00f,
499 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
500 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
501 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
502 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
503 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
504 1.2992e+00f, 1.2992e+00f };
506 1.2757e+00f, 1.2352e+00f, 1.2312e+00f, 1.2285e+00f, 1.2280e+00f,
507 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
508 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
509 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
510 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
511 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
512 1.2278e+00f, 1.2278e+00f };
514 1.6250e+00f, 1.6875e+00f, 1.6963e+00f, 1.7067e+00f, 1.7116e+00f,
515 1.7129e+00f, 1.7141e+00f, 1.7145e+00f, 1.7151e+00f, 1.7152e+00f,
516 1.7155e+00f, 1.7155e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
517 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
518 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
519 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
520 1.7156e+00f, 1.7156e+00f };
522 2.7500e+00f, 2.8047e+00f, 2.8198e+00f, 2.8410e+00f, 2.8558e+00f,
523 2.8601e+00f, 2.8628e+00f, 2.8656e+00f, 2.8662e+00f, 2.8667e+00f,
524 2.8669e+00f, 2.8670e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
525 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
526 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
527 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
528 2.8671e+00f, 2.8671e+00f };
550 result &= file->
write(&buf, 2) == 2;
552 result &= file->
write(&buf, 2) == 2;
554 result &= file->
write(&buf, 2) == 2;
556 result &= file->
write(&buf, 4) == 4;
558 result &= file->
write(&buf, 4) == 4;
560 result &= file->
write(&buf, 4) == 4;
562 result &= file->
write(&buf, 4) == 4;
564 result &= file->
write(&buf, 4) == 4;
566 result &= file->
write(&buf, 4) == 4;
568 result &= file->
write(&buf, 4) == 4;
570 result &= file->
write(&buf, 4) == 4;
572 result &= file->
write(&buf, 2) == 2;
573 for (
int c = 0; c <
Csiz; ++c)
578 result &= file->
write(&buf, 3) == 3;
588 OJPH_ERROR(0x00050041,
"error reading SIZ marker");
590 int num_comps = (
Lsiz - 38) / 3;
591 if (
Lsiz != 38 + 3 * num_comps)
592 OJPH_ERROR(0x00050042,
"error in SIZ marker length");
594 OJPH_ERROR(0x00050043,
"error reading SIZ marker");
596 if ((
Rsiz & 0x4000) == 0)
597 OJPH_ERROR(0x00050044,
"Rsiz bit 14 not set (this is not a JPH file)");
599 OJPH_WARN(0x00050001,
"Rsiz in SIZ has unimplemented fields");
601 OJPH_ERROR(0x00050045,
"error reading SIZ marker");
604 OJPH_ERROR(0x00050046,
"error reading SIZ marker");
607 OJPH_ERROR(0x00050047,
"error reading SIZ marker");
610 OJPH_ERROR(0x00050048,
"error reading SIZ marker");
613 OJPH_ERROR(0x00050049,
"error reading SIZ marker");
616 OJPH_ERROR(0x0005004A,
"error reading SIZ marker");
619 OJPH_ERROR(0x0005004B,
"error reading SIZ marker");
622 OJPH_ERROR(0x0005004C,
"error reading SIZ marker");
625 OJPH_ERROR(0x0005004D,
"error reading SIZ marker");
627 if (
Csiz != num_comps)
628 OJPH_ERROR(0x0005004E,
"Csiz does not match the SIZ marker size");
636 for (
int c = 0; c <
Csiz; ++c)
639 OJPH_ERROR(0x00050051,
"error reading SIZ marker");
641 OJPH_ERROR(0x00050052,
"error reading SIZ marker");
643 OJPH_ERROR(0x00050053,
"error reading SIZ marker");
666 result &= file->
write(&buf, 2) == 2;
668 result &= file->
write(&buf, 2) == 2;
670 result &= file->
write(&buf, 4) == 4;
673 result &= file->
write(&buf, 2) == 2;
682 OJPH_ERROR(0x00050061,
"error reading CAP marker");
685 OJPH_ERROR(0x00050062,
"error reading CAP marker");
688 if (
Pcap & 0xFFFDFFFF)
690 "error Pcap in CAP has options that are not supported");
691 if ((
Pcap & 0x00020000) == 0)
693 "error Pcap should have its 15th MSB set, Pcap^15. "
694 " This is not a JPH file");
695 for (
ui32 i = 0; i < count; ++i)
697 OJPH_ERROR(0x00050065,
"error reading CAP marker");
698 if (
Lcap != 6 + 2 * count)
699 OJPH_ERROR(0x00050066,
"error in CAP marker length");
722 result &= file->
write(&buf, 2) == 2;
724 result &= file->
write(&buf, 2) == 2;
726 result &= file->
write(&buf, 1) == 1;
728 result &= file->
write(&buf, 1) == 1;
730 result &= file->
write(&buf, 2) == 2;
732 result &= file->
write(&buf, 1) == 1;
737 result &= file->
write(&buf, 4) == 4;
739 result &= file->
write(&buf, 1) == 1;
744 result &= file->
write(&buf, 1) == 1;
754 OJPH_ERROR(0x00050071,
"error reading COD marker");
757 OJPH_ERROR(0x00050072,
"error reading COD marker");
759 OJPH_ERROR(0x00050073,
"error reading COD marker");
761 {
OJPH_ERROR(0x00050074,
"error reading COD marker"); }
765 OJPH_ERROR(0x00050075,
"error reading COD marker");
767 OJPH_ERROR(0x00050076,
"error reading COD marker");
769 OJPH_ERROR(0x00050077,
"error reading COD marker");
771 OJPH_ERROR(0x00050078,
"error reading COD marker");
773 OJPH_ERROR(0x00050079,
"error reading COD marker");
775 OJPH_ERROR(0x0005007A,
"error reading COD marker");
779 OJPH_ERROR(0x0005007B,
"error reading COD marker");
781 OJPH_ERROR(0x0005007C,
"error in COD marker length");
794 bool is_employing_color_transform)
799 B += is_employing_color_transform ? 1 : 0;
803 ui32 X = (
ui32) ceil(log(bibo_l * bibo_l * 1.1f) / M_LN2);
809 X = (
ui32) ceil(log(bibo_h * bibo_l * 1.1f) / M_LN2);
812 X = (
ui32) ceil(log(bibo_h * bibo_h * 1.1f) / M_LN2);
821 Sqcd = (
ui8)((guard_bits<<5)|0x2);
824 float delta_b =
base_delta / (gain_l * gain_l);
825 int exp = 0, mantissa;
826 while (delta_b < 1.0f)
827 { exp++; delta_b *= 2.0f; }
830 mantissa = (int)round(delta_b * (
float)(1<<11)) - (1<<11);
831 mantissa = mantissa < (1<<11) ? mantissa : 0x7FF;
840 int exp = 0, mantissa;
841 while (delta_b < 1.0f)
842 { exp++; delta_b *= 2.0f; }
843 mantissa = (int)round(delta_b * (
float)(1<<11)) - (1<<11);
844 mantissa = mantissa < (1<<11) ? mantissa : 0x7FF;
852 { exp++; delta_b *= 2.0f; }
853 mantissa = (int)round(delta_b * (
float)(1<<11)) - (1<<11);
854 mantissa = mantissa < (1<<11) ? mantissa : 0x7FF;
863 int irrev =
Sqcd & 0x1F;
884 assert((
Sqcd & 0x1F) == 2);
885 float arr[] = { 1.0f, 2.0f, 2.0f, 4.0f };
891 mantissa /= (float)(1 << 11);
892 mantissa /= (float)(1u << eps);
909 int irrev =
Sqcd & 0x1F;
913 num_bits = num_bits == 0 ? 0 : num_bits - 1;
928 int irrev =
Sqcd & 0x1F;
945 result &= file->
write(&buf, 2) == 2;
947 result &= file->
write(&buf, 2) == 2;
949 result &= file->
write(&buf, 1) == 1;
952 for (
ui32 i = 0; i < num_subbands; ++i)
955 result &= file->
write(&buf, 1) == 1;
958 for (
ui32 i = 0; i < num_subbands; ++i)
961 result &= file->
write(&buf, 2) == 2;
973 OJPH_ERROR(0x00050081,
"error reading QCD marker");
976 OJPH_ERROR(0x00050082,
"error reading QCD marker");
977 if ((
Sqcd & 0x1F) == 0)
981 OJPH_ERROR(0x00050083,
"wrong Lqcd value in QCD marker");
984 OJPH_ERROR(0x00050084,
"error reading QCD marker");
986 else if ((
Sqcd & 0x1F) == 1)
990 "Scalar derived quantization is not supported yet in QCD marker");
992 OJPH_ERROR(0x00050085,
"wrong Lqcd value in QCD marker");
994 else if ((
Sqcd & 0x1F) == 2)
998 OJPH_ERROR(0x00050086,
"wrong Lqcd value in QCD marker");
1002 OJPH_ERROR(0x00050087,
"error reading QCD marker");
1007 OJPH_ERROR(0x00050088,
"wrong Sqcd value in QCD marker");
1022 OJPH_ERROR(0x000500A1,
"error reading QCC marker");
1024 if (num_comps < 257)
1027 if (file->
read(&v, 1) != 1)
1028 OJPH_ERROR(0x000500A2,
"error reading QCC marker");
1034 OJPH_ERROR(0x000500A3,
"error reading QCC marker");
1038 OJPH_ERROR(0x000500A4,
"error reading QCC marker");
1039 if ((
Sqcd & 0x1F) == 0)
1041 ui32 offset = num_comps < 257 ? 5 : 6;
1044 OJPH_ERROR(0x000500A5,
"wrong Lqcd value in QCC marker");
1047 OJPH_ERROR(0x000500A6,
"error reading QCC marker");
1049 else if ((
Sqcd & 0x1F) == 1)
1051 ui32 offset = num_comps < 257 ? 6 : 7;
1054 "Scalar derived quantization is not supported yet in QCC marker");
1056 OJPH_ERROR(0x000500A7,
"wrong Lqcc value in QCC marker");
1058 else if ((
Sqcd & 0x1F) == 2)
1060 ui32 offset = num_comps < 257 ? 6 : 7;
1063 OJPH_ERROR(0x000500A8,
"wrong Lqcc value in QCC marker");
1067 OJPH_ERROR(0x000500A9,
"error reading QCC marker");
1072 OJPH_ERROR(0x000500AA,
"wrong Sqcc value in QCC marker");
1089 this->
Psot = payload_len + 14;
1093 result &= file->
write(&buf, 2) == 2;
1095 result &= file->
write(&buf, 2) == 2;
1097 result &= file->
write(&buf, 2) == 2;
1099 result &= file->
write(&buf, 4) == 4;
1101 result &= file->
write(&buf, 1) == 1;
1103 result &= file->
write(&buf, 1) == 1;
1117 result &= file->
write(&buf, 2) == 2;
1119 result &= file->
write(&buf, 2) == 2;
1121 result &= file->
write(&buf, 2) == 2;
1123 result &= file->
write(&buf, 4) == 4;
1125 result &= file->
write(&buf, 1) == 1;
1127 result &= file->
write(&buf, 1) == 1;
1139 OJPH_INFO(0x00050091,
"error reading SOT marker");
1146 OJPH_INFO(0x00050092,
"error in SOT length");
1152 OJPH_INFO(0x00050093,
"error reading tile index");
1159 OJPH_INFO(0x00050094,
"tile index in SOT marker cannot be 0xFFFF");
1165 OJPH_INFO(0x00050095,
"error reading SOT marker");
1172 OJPH_INFO(0x00050096,
"error reading SOT marker");
1178 OJPH_INFO(0x00050097,
"error reading SOT marker");
1186 OJPH_ERROR(0x00050091,
"error reading SOT marker");
1189 OJPH_ERROR(0x00050092,
"error in SOT length");
1191 OJPH_ERROR(0x00050093,
"error reading SOT tile index");
1194 OJPH_ERROR(0x00050094,
"tile index in SOT marker cannot be 0xFFFF");
1196 OJPH_ERROR(0x00050095,
"error reading SOT marker");
1199 OJPH_ERROR(0x00050096,
"error reading SOT marker");
1201 OJPH_ERROR(0x00050097,
"error reading SOT marker");
1218 OJPH_ERROR(0x000500B1,
"Trying to allocate more than 65535 bytes for "
1219 "a TLM marker; this can be resolved by having more than "
1220 "one TLM marker, but the code does not support this. "
1221 "In any case, this limit means that we have 10922 "
1222 "tileparts or more, which is a huge number.");
1248 result &= file->
write(&buf, 2) == 2;
1250 result &= file->
write(&buf, 2) == 2;
1256 result &= file->
write(&buf, 2) == 2;
1258 result &= file->
write(&buf, 4) == 4;
virtual size_t read(void *ptr, size_t size)=0
static const float gain_5x3_l[34]
static float get_bibo_gain_l(ui32 num_decomp, bool reversible)
static const float gain_5x3_h[34]
static float get_bibo_gain_h(ui32 num_decomp, bool reversible)
static const float gain_9x7_h[34]
static const float gain_9x7_l[34]
static const float gain_5x3_l[34]
static const float gain_5x3_h[34]
static float get_gain_l(ui32 num_decomp, bool reversible)
static const float gain_9x7_l[34]
static float get_gain_h(ui32 num_decomp, bool reversible)
static const float gain_9x7_h[34]
virtual size_t write(const void *ptr, size_t size)=0
OJPH_EXPORT size get_block_dims() const
OJPH_EXPORT int get_progression_order() const
OJPH_EXPORT bool is_using_color_transform() const
OJPH_EXPORT void set_num_decomposition(ui32 num_decompositions)
OJPH_EXPORT ui32 get_num_decompositions() const
OJPH_EXPORT size get_log_block_dims() const
OJPH_EXPORT bool packets_may_use_sop() const
OJPH_EXPORT size get_precinct_size(ui32 level_num) const
OJPH_EXPORT const char * get_progression_order_as_string() const
OJPH_EXPORT void set_precinct_size(int num_levels, size *precinct_size)
OJPH_EXPORT bool packets_use_eph() const
OJPH_EXPORT bool is_reversible() const
OJPH_EXPORT void set_progression_order(const char *name)
OJPH_EXPORT bool get_block_vertical_causality() const
OJPH_EXPORT void set_block_dims(ui32 width, ui32 height)
OJPH_EXPORT size get_log_precinct_size(ui32 level_num) const
OJPH_EXPORT int get_num_layers() const
OJPH_EXPORT void set_color_transform(bool color_transform)
OJPH_EXPORT void set_reversible(bool reversible)
OJPH_EXPORT void set_irrev_quant(float delta)
OJPH_EXPORT void set_tile_size(size s)
OJPH_EXPORT point get_image_extent() const
OJPH_EXPORT void set_component(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
OJPH_EXPORT void set_num_components(ui32 num_comps)
OJPH_EXPORT ui32 get_bit_depth(ui32 comp_num) const
OJPH_EXPORT void set_tile_offset(point offset)
OJPH_EXPORT point get_image_offset() const
OJPH_EXPORT void set_image_offset(point offset)
OJPH_EXPORT size get_tile_size() const
OJPH_EXPORT ui32 get_recon_height(ui32 comp_num) const
OJPH_EXPORT point get_downsampling(ui32 comp_num) const
OJPH_EXPORT void set_image_extent(point extent)
OJPH_EXPORT point get_tile_offset() const
OJPH_EXPORT ui32 get_recon_width(ui32 comp_num) const
OJPH_EXPORT bool is_signed(ui32 comp_num) const
OJPH_EXPORT ui32 get_num_components() const
static ui16 swap_byte(ui16 t)
const char OJPH_PO_STRING_PCRL[]
static ui32 population_count(ui32 val)
const char OJPH_PO_STRING_RLCP[]
const char OJPH_PO_STRING_RPCL[]
const char OJPH_PO_STRING_CPRL[]
static ui32 count_leading_zeros(ui32 val)
const char OJPH_PO_STRING_LRCP[]
#define OJPH_ERROR(t,...)
void read(infile_base *file)
bool write(outfile_base *file)
bool write(outfile_base *file)
size get_block_dims() const
size get_log_block_dims() const
void set_reversible(bool reversible)
bool is_employing_color_transform() const
void employ_color_transform(ui8 val)
void read(infile_base *file)
size get_log_precinct_size(ui32 res_num) const
bool packets_use_eph() const
ui8 get_num_decompositions() const
bool packets_may_use_sop() const
bool is_reversible() const
size get_precinct_size(ui32 res_num) const
void read(infile_base *file, ui32 num_comps)
ui32 get_Kmax(ui32 resolution, ui32 subband) const
ui32 get_num_guard_bits() const
void set_delta(float delta)
bool write(outfile_base *file)
void read(infile_base *file)
void set_rev_quant(ui32 bit_depth, bool is_employing_color_transform)
float irrev_get_delta(ui32 resolution, ui32 subband) const
ui32 get_bit_depth(ui32 comp_num) const
ui32 get_recon_height(ui32 comp_num) const
bool is_signed(ui32 comp_num) const
bool write(outfile_base *file)
void set_comp_info(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
point get_downsampling(ui32 comp_num) const
void read(infile_base *file)
void set_num_components(ui32 num_comps)
ui32 get_recon_width(ui32 comp_num) const
bool read(infile_base *file, bool resilient)
bool write(outfile_base *file, ui32 payload_len)
void set_next_pair(ui16 Ttlm, ui32 Ptlm)
bool write(outfile_base *file)
void init(ui32 num_pairs, Ttlm_Ptlm_pair *store)