JPEG2000Loader.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Enumerate.h>
  8. #include <AK/MemoryStream.h>
  9. #include <LibGfx/ImageFormats/ISOBMFF/JPEG2000Boxes.h>
  10. #include <LibGfx/ImageFormats/ISOBMFF/Reader.h>
  11. #include <LibGfx/ImageFormats/JPEG2000Loader.h>
  12. // Core coding system spec (.jp2 format): T-REC-T.800-201511-S!!PDF-E.pdf available here:
  13. // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.800-201511-S!!PDF-E&type=items
  14. // Extensions (.jpx format): T-REC-T.801-202106-S!!PDF-E.pdf available here:
  15. // https://handle.itu.int/11.1002/1000/14666-en?locatt=format:pdf&auth
  16. // rfc3745 lists the MIME type. It only mentions the jp2_id_string as magic number.
  17. namespace Gfx {
  18. // A JPEG2000 image can be stored in a codestream with markers, similar to a JPEG image,
  19. // or in a JP2 file, which is a container format based on boxes similar to ISOBMFF.
  20. // This is the marker for the codestream version. We don't support this yet.
  21. // If we add support, add a second `"image/jp2"` line to MimeData.cpp for this magic number.
  22. // T.800 Annex A, Codestream syntax, A.2 Information in the marker segments and A.3 Construction of the codestream
  23. [[maybe_unused]] static constexpr u8 marker_id_string[] = { 0xFF, 0x4F, 0xFF, 0x51 };
  24. // This is the marker for the box version.
  25. // T.800 Annex I, JP2 file format syntax, I.5.1 JPEG 2000 Signature box
  26. static constexpr u8 jp2_id_string[] = { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
  27. // Table A.2 – List of markers and marker segments
  28. // "Delimiting markers and marker segments"
  29. #define J2K_SOC 0xFF4F // "Start of codestream"
  30. #define J2K_SOT 0xFF90 // "Start of tile-part"
  31. #define J2K_SOD 0xFF93 // "Start of data"
  32. #define J2K_EOC 0xFFD9 // "End of codestream"
  33. // "Fixed information marker segments"
  34. #define J2K_SIZ 0xFF51 // "Image and tile size"
  35. // "Functional marker segments"
  36. #define J2K_COD 0xFF52 // "Coding style default"
  37. #define J2K_COC 0xFF53 // "Coding style component"
  38. #define J2K_RGN 0xFF5E // "Region-of-interest"
  39. #define J2K_QCD 0xFF5C // "Quantization default"
  40. #define J2K_QCC 0xFF5D // "Quantization component"
  41. #define J2K_POC 0xFF5F // "Progression order change"
  42. // "Pointer marker segments"
  43. #define J2K_TLM 0xFF55 // "Tile-part lengths"
  44. #define J2K_PLM 0xFF57 // "Packet length, main header"
  45. #define J2K_PLT 0xFF58 // "Packet length, tile-part header"
  46. #define J2K_PPM 0xFF60 // "Packed packet headers, main header"
  47. #define J2K_PPT 0xFF61 // "Packed packet headers, tile-part header"
  48. // "In-bit-stream markers and marker segments"
  49. #define J2K_SOP 0xFF91 // "Start of packet"
  50. #define J2K_EPH 0xFF92 // "End of packet header"
  51. // "Informational marker segments"
  52. #define J2K_CRG 0xFF63 // "Component registration"
  53. #define J2K_COM 0xFF64 // "Comment"
  54. // A.4.2 Start of tile-part (SOT)
  55. struct StartOfTilePart {
  56. // "Tile index. This number refers to the tiles in raster order starting at the number 0."
  57. u16 tile_index { 0 }; // "Isot" in spec.
  58. // "Length, in bytes, from the beginning of the first byte of this SOT marker segment of the tile-part to
  59. // the end of the data of that tile-part. Figure A.16 shows this alignment. Only the last tile-part in the
  60. // codestream may contain a 0 for Psot. If the Psot is 0, this tile-part is assumed to contain all data until
  61. // the EOC marker."
  62. u32 tile_part_length { 0 }; // "Psot" in spec.
  63. // "Tile-part index. There is a specific order required for decoding tile-parts; this index denotes the order
  64. // from 0. If there is only one tile-part for a tile, then this value is zero. The tile-parts of this tile shall
  65. // appear in the codestream in this order, although not necessarily consecutively."
  66. u8 tile_part_index { 0 }; // "TPsot" in spec.
  67. // "Number of tile-parts of a tile in the codestream. Two values are allowed: the correct number of tile-
  68. // parts for that tile and zero. A zero value indicates that the number of tile-parts of this tile is not
  69. // specified in this tile-part.
  70. u8 number_of_tile_parts { 0 }; // "TNsot" in spec.
  71. };
  72. static ErrorOr<StartOfTilePart> read_start_of_tile_part(ReadonlyBytes data)
  73. {
  74. if (data.size() < 8)
  75. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for SOT marker segment");
  76. StartOfTilePart sot;
  77. sot.tile_index = *reinterpret_cast<AK::BigEndian<u16> const*>(data.data());
  78. sot.tile_part_length = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 2);
  79. sot.tile_part_index = data[6];
  80. sot.number_of_tile_parts = data[7];
  81. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: SOT marker segment: tile_index={}, tile_part_length={}, tile_part_index={}, number_of_tile_parts={}", sot.tile_index, sot.tile_part_length, sot.tile_part_index, sot.number_of_tile_parts);
  82. return sot;
  83. }
  84. // A.5.1 Image and tile size (SIZ)
  85. struct ImageAndTileSize {
  86. // "Denotes capabilities that a decoder needs to properly decode the codestream."
  87. u16 needed_decoder_capabilities { 0 }; // "Rsiz" in spec.
  88. // "Width of the reference grid."
  89. u32 width { 0 }; // "Xsiz" in spec.
  90. // "Height of the reference grid."
  91. u32 height { 0 }; // "Ysiz" in spec.
  92. // "Horizontal offset from the origin of the reference grid to the left side of the image area."
  93. u32 x_offset { 0 }; // "XOsiz" in spec.
  94. // "Vertical offset from the origin of the reference grid to the top side of the image area."
  95. u32 y_offset { 0 }; // "YOsiz" in spec.
  96. // "Width of one reference tile with respect to the reference grid."
  97. u32 tile_width { 0 }; // "XTsiz" in spec.
  98. // "Height of one reference tile with respect to the reference grid."
  99. u32 tile_height { 0 }; // "YTsiz" in spec.
  100. // "Horizontal offset from the origin of the reference grid to the left side of the first tile."
  101. u32 tile_x_offset { 0 }; // "XTOsiz" in spec.
  102. // "Vertical offset from the origin of the reference grid to the top side of the first tile."
  103. u32 tile_y_offset { 0 }; // "YTOsiz" in spec.
  104. // "Csiz" isn't stored in this struct. It corresponds to `components.size()`.
  105. struct ComponentInformation {
  106. // "Precision (depth) in bits and sign of the ith component samples."
  107. u8 depth_and_sign { 0 }; // "Ssiz" in spec.
  108. // Table A.11 – Component Ssiz parameter
  109. u8 bit_depth() const { return (depth_and_sign & 0x7F) + 1; }
  110. bool is_signed() const { return depth_and_sign & 0x80; }
  111. // "Horizontal separation of a sample of the ith component with respect to the reference grid."
  112. u8 horizontal_separation { 0 }; // "XRsiz" in spec.
  113. // "Vertical separation of a sample of the ith component with respect to the reference grid."
  114. u8 vertical_separation { 0 }; // "YRsiz" in spec.
  115. };
  116. Vector<ComponentInformation> components;
  117. };
  118. static ErrorOr<ImageAndTileSize> read_image_and_tile_size(ReadonlyBytes data)
  119. {
  120. if (data.size() < 36)
  121. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for SIZ marker segment");
  122. ImageAndTileSize siz;
  123. siz.needed_decoder_capabilities = *reinterpret_cast<AK::BigEndian<u16> const*>(data.data());
  124. siz.width = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 2);
  125. siz.height = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 6);
  126. siz.x_offset = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 10);
  127. siz.y_offset = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 14);
  128. siz.tile_width = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 18);
  129. siz.tile_height = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 22);
  130. siz.tile_x_offset = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 26);
  131. siz.tile_y_offset = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 30);
  132. u16 component_count = *reinterpret_cast<AK::BigEndian<u16> const*>(data.data() + 34); // "Csiz" in spec.
  133. if (data.size() < 36u + component_count * 3u)
  134. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for SIZ marker segment component information");
  135. for (size_t i = 0; i < component_count; ++i) {
  136. ImageAndTileSize::ComponentInformation component;
  137. component.depth_and_sign = data[36 + i * 3];
  138. if (component.bit_depth() > 38)
  139. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid component depth");
  140. component.horizontal_separation = data[37 + i * 3];
  141. component.vertical_separation = data[38 + i * 3];
  142. siz.components.append(component);
  143. }
  144. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: SIZ marker segment: needed_decoder_capabilities={}, width={}, height={}, x_offset={}, y_offset={}, tile_width={}, tile_height={}, tile_x_offset={}, tile_y_offset={}", siz.needed_decoder_capabilities, siz.width, siz.height, siz.x_offset, siz.y_offset, siz.tile_width, siz.tile_height, siz.tile_x_offset, siz.tile_y_offset);
  145. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: SIZ marker segment: {} components:", component_count);
  146. for (auto [i, component] : enumerate(siz.components))
  147. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: SIZ marker segment: component[{}]: is_signed={}, bit_depth={}, horizontal_separation={}, vertical_separation={}", i, component.is_signed(), component.bit_depth(), component.horizontal_separation, component.vertical_separation);
  148. return siz;
  149. }
  150. // A.6.1 Coding style default (COD)
  151. struct CodingStyleDefault {
  152. // Table A.13 – Coding style parameter values for the Scod parameter
  153. bool has_explicit_precinct_size { false };
  154. bool may_use_SOP_marker { false };
  155. bool may_use_EPH_marker { false };
  156. // Table A.16 – Progression order for the SGcod, SPcoc, and Ppoc parameters
  157. enum ProgressionOrder {
  158. LayerResolutionComponentPosition = 0,
  159. ResolutionLayerComponentPosition = 1,
  160. ResolutionPositionComponentLayer = 2,
  161. PositionComponentResolutionLayer = 3,
  162. ComponentPositionResolutionLayer = 4,
  163. };
  164. // Table A.17 – Multiple component transformation for the SGcod parameters
  165. enum MultipleComponentTransformationType {
  166. None = 0,
  167. MultipleComponentTransformationUsed = 1, // See Annex G
  168. };
  169. // Table A.14 – Coding style parameter values of the SGcod parameter
  170. ProgressionOrder progression_order { LayerResolutionComponentPosition };
  171. u16 number_of_layers { 0 };
  172. MultipleComponentTransformationType multiple_component_transformation_type { None };
  173. // Table A.20 – Transformation for the SPcod and SPcoc parameters
  174. enum Transformation {
  175. Irreversible_9_7_Filter = 0,
  176. Reversible_5_3_Filter = 1,
  177. };
  178. // Table A.15 – Coding style parameter values of the SPcod and SPcoc parameters
  179. // "Number of decomposition levels, NL, Zero implies no transformation."
  180. u8 number_of_decomposition_levels { 0 };
  181. u8 code_block_width_exponent { 0 }; // "xcb" in spec; 2 already added.
  182. u8 code_block_height_exponent { 0 }; // "ycb" in spec; 2 already added.
  183. u8 code_block_style { 0 };
  184. Transformation transformation { Irreversible_9_7_Filter };
  185. // Table A.19 – Code-block style for the SPcod and SPcoc parameters
  186. bool uses_selective_arithmetic_coding_bypass() const { return code_block_style & 1; }
  187. bool reset_context_probabilities() const { return code_block_style & 2; }
  188. bool uses_termination_on_each_coding_pass() const { return code_block_style & 4; }
  189. bool uses_vertically_causal_context() const { return code_block_style & 8; }
  190. bool uses_predictable_termination() const { return code_block_style & 0x10; }
  191. bool uses_segmentation_symbols() const { return code_block_style & 0x20; }
  192. // If has_explicit_precinct_size is false, this contains the default { 15, 15 } number_of_decomposition_levels + 1 times.
  193. // If has_explicit_precinct_size is true, this contains number_of_decomposition_levels + 1 explicit values stored in the COD marker segment.
  194. struct PrecinctSize {
  195. u8 PPx { 0 };
  196. u8 PPy { 0 };
  197. };
  198. Vector<PrecinctSize> precinct_sizes;
  199. };
  200. static ErrorOr<CodingStyleDefault> read_coding_style_default(ReadonlyBytes data)
  201. {
  202. if (data.size() < 10)
  203. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for COD marker segment");
  204. CodingStyleDefault cod;
  205. u8 Scod = data[0];
  206. cod.has_explicit_precinct_size = Scod & 1;
  207. cod.may_use_SOP_marker = Scod & 2;
  208. cod.may_use_EPH_marker = Scod & 4;
  209. u32 SGcod = *reinterpret_cast<AK::BigEndian<u32> const*>(data.data() + 1);
  210. u8 progression_order = SGcod >> 24;
  211. if (progression_order > 4)
  212. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid progression order");
  213. cod.progression_order = static_cast<CodingStyleDefault::ProgressionOrder>(progression_order);
  214. cod.number_of_layers = (SGcod >> 8) & 0xFFFF;
  215. if (cod.number_of_layers == 0)
  216. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid number of layers");
  217. u8 multiple_component_transformation_type = SGcod & 0xFF;
  218. if (multiple_component_transformation_type > 1)
  219. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid multiple component transformation type");
  220. cod.multiple_component_transformation_type = static_cast<CodingStyleDefault::MultipleComponentTransformationType>(multiple_component_transformation_type);
  221. cod.number_of_decomposition_levels = data[5];
  222. if (cod.number_of_decomposition_levels > 32)
  223. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid number of decomposition levels");
  224. // Table A.18 – Width or height exponent of the code-blocks for the SPcod and SPcoc parameters
  225. u8 xcb = (data[6] & 0xF) + 2;
  226. u8 ycb = (data[7] & 0xF) + 2;
  227. if (xcb > 10 || ycb > 10 || xcb + ycb > 12)
  228. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid code block size");
  229. cod.code_block_width_exponent = xcb;
  230. cod.code_block_height_exponent = ycb;
  231. cod.code_block_style = data[8];
  232. u8 transformation = data[9];
  233. if (transformation > 1)
  234. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid transformation");
  235. cod.transformation = static_cast<CodingStyleDefault::Transformation>(transformation);
  236. if (cod.has_explicit_precinct_size) {
  237. if (data.size() < 10u + cod.number_of_decomposition_levels + 1u)
  238. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for COD marker segment precinct sizes");
  239. for (size_t i = 0; i < cod.number_of_decomposition_levels + 1u; ++i) {
  240. u8 b = data[10 + i];
  241. // Table A.21 – Precinct width and height for the SPcod and SPcoc parameters
  242. CodingStyleDefault::PrecinctSize precinct_size;
  243. precinct_size.PPx = b & 0xF;
  244. precinct_size.PPy = b >> 4;
  245. if ((precinct_size.PPx == 0 || precinct_size.PPy == 0) && i > 0)
  246. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid precinct size");
  247. cod.precinct_sizes.append(precinct_size);
  248. }
  249. } else {
  250. for (size_t i = 0; i < cod.number_of_decomposition_levels + 1u; ++i)
  251. cod.precinct_sizes.append({ 15, 15 });
  252. }
  253. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: has_explicit_precinct_size={}, may_use_SOP_marker={}, may_use_EPH_marker={}, progression_order={}, number_of_layers={}", cod.has_explicit_precinct_size, cod.may_use_SOP_marker, cod.may_use_EPH_marker, (int)cod.progression_order, cod.number_of_layers);
  254. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: multiple_component_transformation_type={}, number_of_decomposition_levels={}, code_block_width_exponent={}, code_block_height_exponent={}", (int)cod.multiple_component_transformation_type, cod.number_of_decomposition_levels, cod.code_block_width_exponent, cod.code_block_height_exponent);
  255. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: code_block_style={}, transformation={}", cod.code_block_style, (int)cod.transformation);
  256. if (cod.has_explicit_precinct_size) {
  257. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: {} explicit precinct sizes:", cod.precinct_sizes.size());
  258. for (auto [i, precinct_size] : enumerate(cod.precinct_sizes))
  259. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: precinct_size[{}]: PPx={}, PPy={}", i, precinct_size.PPx, precinct_size.PPy);
  260. }
  261. return cod;
  262. }
  263. // A.6.4 Quantization default (QCD)
  264. struct QuantizationDefault {
  265. enum QuantizationStyle {
  266. NoQuantization = 0,
  267. ScalarDerived = 1,
  268. ScalarExpounded = 2,
  269. };
  270. QuantizationStyle quantization_style { NoQuantization };
  271. u8 number_of_guard_bits { 0 };
  272. struct ReversibleStepSize {
  273. u8 exponent { 0 };
  274. };
  275. struct IrreversibleStepSize {
  276. u16 mantissa { 0 };
  277. u8 exponent { 0 };
  278. };
  279. // Stores a Vector<ReversibleStepSize> if quantization_style is NoQuantization, and a Vector<IrreversibleStepSize> otherwise.
  280. // The size of the vector is >= 3*number_of_decomposition_levels + 1 if quantization_style is not ScalarDerived, and 1 otherwise.
  281. using StepSizeType = Variant<Empty, Vector<ReversibleStepSize>, Vector<IrreversibleStepSize>>;
  282. StepSizeType step_sizes;
  283. };
  284. static ErrorOr<QuantizationDefault> read_quantization_default(ReadonlyBytes data)
  285. {
  286. if (data.size() < 1)
  287. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for COD marker segment");
  288. QuantizationDefault qcd;
  289. u8 sqcd = data[0];
  290. u8 quantization_style = sqcd & 0x1F;
  291. if (quantization_style > 2)
  292. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid quantization style");
  293. qcd.quantization_style = static_cast<QuantizationDefault::QuantizationStyle>(quantization_style);
  294. qcd.number_of_guard_bits = sqcd >> 5;
  295. qcd.step_sizes = TRY([&]() -> ErrorOr<QuantizationDefault::StepSizeType> {
  296. if (quantization_style == QuantizationDefault::NoQuantization) {
  297. // Table A.29 – Reversible step size values for the SPqcd and SPqcc parameters (reversible transform only)
  298. if (data.size() < 2)
  299. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for QCD marker segment");
  300. u8 number_of_decomposition_levels = (data.size() - 2) / 3;
  301. Vector<QuantizationDefault::ReversibleStepSize> reversible_step_sizes;
  302. for (size_t i = 0; i < 1u + 3u * number_of_decomposition_levels; ++i)
  303. reversible_step_sizes.append({ static_cast<u8>(data[1 + i] >> 3) });
  304. return reversible_step_sizes;
  305. }
  306. // Table A.30 – Quantization values for the SPqcd and SPqcc parameters (irreversible transformation only)
  307. if (data.size() < 3)
  308. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for QCD marker segment");
  309. u8 number_of_decomposition_levels = 0;
  310. if (quantization_style == QuantizationDefault::ScalarExpounded)
  311. number_of_decomposition_levels = (data.size() - 3) / 6;
  312. Vector<QuantizationDefault::IrreversibleStepSize> irreversible_step_sizes;
  313. for (size_t i = 0; i < 1u + 3u * number_of_decomposition_levels; ++i) {
  314. u16 value = *reinterpret_cast<AK::BigEndian<u16> const*>(data.data() + 1 + i * 2);
  315. QuantizationDefault::IrreversibleStepSize step_size;
  316. step_size.mantissa = value & 0x7FF;
  317. step_size.exponent = value >> 11;
  318. irreversible_step_sizes.append(step_size);
  319. }
  320. return irreversible_step_sizes;
  321. }());
  322. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCD marker segment: quantization_style={}, number_of_guard_bits={}", (int)qcd.quantization_style, qcd.number_of_guard_bits);
  323. qcd.step_sizes.visit(
  324. [](Empty) { VERIFY_NOT_REACHED(); },
  325. [](Vector<QuantizationDefault::ReversibleStepSize> const& step_sizes) {
  326. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCD marker segment: {} step sizes:", step_sizes.size());
  327. for (auto [i, step_size] : enumerate(step_sizes)) {
  328. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCD marker segment: step_size[{}]: exponent={}", i, step_size.exponent);
  329. }
  330. },
  331. [](Vector<QuantizationDefault::IrreversibleStepSize> const& step_sizes) {
  332. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCD marker segment: {} step sizes:", step_sizes.size());
  333. for (auto [i, step_size] : enumerate(step_sizes)) {
  334. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCD marker segment: step_size[{}]: mantissa={}, exponent={}", i, step_size.mantissa, step_size.exponent);
  335. }
  336. });
  337. return qcd;
  338. }
  339. struct JPEG2000LoadingContext {
  340. enum class State {
  341. NotDecoded = 0,
  342. DecodedTileHeaders,
  343. Error,
  344. };
  345. State state { State::NotDecoded };
  346. ReadonlyBytes codestream_data;
  347. size_t codestream_cursor { 0 };
  348. Optional<ReadonlyBytes> icc_data;
  349. IntSize size;
  350. ISOBMFF::BoxList boxes;
  351. // Data from marker segments:
  352. ImageAndTileSize siz;
  353. CodingStyleDefault cod;
  354. QuantizationDefault qcd;
  355. };
  356. struct MarkerSegment {
  357. u16 marker;
  358. // OptionalNone for markers that don't have data.
  359. // For markers that do have data, this does not include the marker length data. (`data.size() + 2` is the value of the marker length field.)
  360. Optional<ReadonlyBytes> data;
  361. };
  362. static ErrorOr<u16> peek_marker(JPEG2000LoadingContext& context)
  363. {
  364. if (context.codestream_cursor + 2 > context.codestream_data.size())
  365. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker");
  366. return *reinterpret_cast<AK::BigEndian<u16> const*>(context.codestream_data.data() + context.codestream_cursor);
  367. }
  368. static ErrorOr<MarkerSegment> read_marker_at_cursor(JPEG2000LoadingContext& context)
  369. {
  370. u16 marker = TRY(peek_marker(context));
  371. // "All markers with the marker code between 0xFF30 and 0xFF3F have no marker segment parameters. They shall be skipped by the decoder."
  372. // "The SOC, SOD and EOC are delimiting markers not marker segments, and have no explicit length information or other parameters."
  373. bool is_marker_segment = !(marker >= 0xFF30 && marker <= 0xFF3F) && marker != J2K_SOC && marker != J2K_SOD && marker != J2K_EOC;
  374. MarkerSegment marker_segment;
  375. marker_segment.marker = marker;
  376. if (is_marker_segment) {
  377. if (context.codestream_cursor + 4 > context.codestream_data.size())
  378. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker segment length");
  379. u16 marker_length = *reinterpret_cast<AK::BigEndian<u16> const*>(context.codestream_data.data() + context.codestream_cursor + 2);
  380. if (marker_length < 2)
  381. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Marker segment length too small");
  382. if (context.codestream_cursor + 2 + marker_length > context.codestream_data.size())
  383. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker segment data");
  384. marker_segment.data = ReadonlyBytes { context.codestream_data.data() + context.codestream_cursor + 4, marker_length - 2u };
  385. }
  386. context.codestream_cursor += 2;
  387. if (is_marker_segment)
  388. context.codestream_cursor += 2 + marker_segment.data->size();
  389. return marker_segment;
  390. }
  391. static ErrorOr<void> parse_codestream_main_header(JPEG2000LoadingContext& context)
  392. {
  393. // Figure A.3 – Construction of the main header
  394. // "Required as the first marker"
  395. auto marker = TRY(read_marker_at_cursor(context));
  396. if (marker.marker != J2K_SOC)
  397. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SOC marker");
  398. // "Required as the second marker segment"
  399. marker = TRY(read_marker_at_cursor(context));
  400. if (marker.marker != J2K_SIZ)
  401. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SIZ marker");
  402. context.siz = TRY(read_image_and_tile_size(marker.data.value()));
  403. bool saw_COD_marker = false;
  404. bool saw_QCD_marker = false;
  405. while (true) {
  406. u16 marker = TRY(peek_marker(context));
  407. switch (marker) {
  408. case J2K_COD:
  409. case J2K_COC:
  410. case J2K_QCD:
  411. case J2K_QCC:
  412. case J2K_RGN:
  413. case J2K_POC:
  414. case J2K_PPM:
  415. case J2K_TLM:
  416. case J2K_PLM:
  417. case J2K_CRG:
  418. case J2K_COM: {
  419. auto marker = TRY(read_marker_at_cursor(context));
  420. if (marker.marker == J2K_COD) {
  421. if (saw_COD_marker)
  422. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple COD markers in main header");
  423. context.cod = TRY(read_coding_style_default(marker.data.value()));
  424. saw_COD_marker = true;
  425. } else if (marker.marker == J2K_QCD) {
  426. if (saw_QCD_marker)
  427. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple QCD markers in main header");
  428. context.qcd = TRY(read_quantization_default(marker.data.value()));
  429. saw_QCD_marker = true;
  430. } else {
  431. // FIXME: These are valid main header markers. Parse contents.
  432. dbgln("JPEG2000ImageDecoderPlugin: marker {:#04x} not yet implemented", marker.marker);
  433. }
  434. break;
  435. }
  436. case J2K_SOT: {
  437. // SOT terminates the main header.
  438. if (!saw_COD_marker)
  439. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Required COD marker not present in main header");
  440. if (!saw_QCD_marker)
  441. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Required QCD marker not present in main header");
  442. size_t step_sizes_count = context.qcd.step_sizes.visit(
  443. [](Empty) -> size_t { VERIFY_NOT_REACHED(); },
  444. [](Vector<QuantizationDefault::ReversibleStepSize> const& step_sizes) { return step_sizes.size(); },
  445. [](Vector<QuantizationDefault::IrreversibleStepSize> const& step_sizes) { return step_sizes.size(); });
  446. if (context.qcd.quantization_style != QuantizationDefault::ScalarDerived && step_sizes_count < context.cod.number_of_decomposition_levels * 3u + 1u)
  447. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough step sizes for number of decomposition levels");
  448. return {};
  449. }
  450. default:
  451. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected marker in main header");
  452. }
  453. }
  454. }
  455. static ErrorOr<void> parse_codestream_tile_header(JPEG2000LoadingContext& context)
  456. {
  457. // Figure A.4 – Construction of the first tile-part header of a given tile
  458. // Figure A.5 – Construction of a non-first tile-part header
  459. // "Required as the first marker segment of every tile-part header"
  460. auto tile_start = context.codestream_cursor;
  461. auto marker = TRY(read_marker_at_cursor(context));
  462. if (marker.marker != J2K_SOT)
  463. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SOT marker");
  464. auto start_of_tile = TRY(read_start_of_tile_part(marker.data.value()));
  465. // FIXME: Store start_of_tile on context somewhere.
  466. bool found_start_of_data = false;
  467. while (!found_start_of_data) {
  468. u16 marker = TRY(peek_marker(context));
  469. switch (marker) {
  470. case J2K_SOD:
  471. // "Required as the last marker segment of every tile-part header"
  472. context.codestream_cursor += 2;
  473. found_start_of_data = true;
  474. break;
  475. // FIXME: COD, COC, QCD, QCC are only valid on the first tile part header, reject them in non-first tile part headers.
  476. case J2K_COD:
  477. case J2K_COC:
  478. case J2K_QCD:
  479. case J2K_QCC:
  480. case J2K_RGN:
  481. case J2K_POC:
  482. case J2K_PPT:
  483. case J2K_PLT:
  484. case J2K_COM: {
  485. // FIXME: These are valid tile part header markers. Parse contents.
  486. auto marker = TRY(read_marker_at_cursor(context));
  487. dbgln("JPEG2000ImageDecoderPlugin: marker {:#04x} not yet implemented in tile header", marker.marker);
  488. break;
  489. }
  490. default:
  491. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected marker in tile header");
  492. }
  493. }
  494. u32 tile_bitstream_length;
  495. if (start_of_tile.tile_part_length == 0) {
  496. // Leave room for EOC marker.
  497. if (context.codestream_data.size() - context.codestream_cursor < 2)
  498. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for EOC marker");
  499. tile_bitstream_length = context.codestream_data.size() - context.codestream_cursor - 2;
  500. } else {
  501. u32 tile_header_length = context.codestream_cursor - tile_start;
  502. if (start_of_tile.tile_part_length < tile_header_length)
  503. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid tile part length");
  504. tile_bitstream_length = start_of_tile.tile_part_length - tile_header_length;
  505. }
  506. if (context.codestream_cursor + tile_bitstream_length > context.codestream_data.size())
  507. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for tile bitstream");
  508. // FIXME: Store context.codestream_data.slice(context.codestream_cursor, tile_bitstream_length) somewhere on the context.
  509. context.codestream_cursor += tile_bitstream_length;
  510. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: Tile bitstream length: {}", tile_bitstream_length);
  511. return {};
  512. }
  513. static ErrorOr<void> parse_codestream_tile_headers(JPEG2000LoadingContext& context)
  514. {
  515. while (true) {
  516. auto marker = TRY(peek_marker(context));
  517. if (marker == J2K_EOC) {
  518. context.codestream_cursor += 2;
  519. break;
  520. }
  521. TRY(parse_codestream_tile_header(context));
  522. }
  523. if (context.codestream_cursor < context.codestream_data.size())
  524. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected data after EOC marker");
  525. return {};
  526. }
  527. static ErrorOr<void> decode_jpeg2000_header(JPEG2000LoadingContext& context, ReadonlyBytes data)
  528. {
  529. if (!JPEG2000ImageDecoderPlugin::sniff(data))
  530. return Error::from_string_literal("JPEG2000LoadingContext: Invalid JPEG2000 header");
  531. auto reader = TRY(Gfx::ISOBMFF::Reader::create(TRY(try_make<FixedMemoryStream>(data))));
  532. context.boxes = TRY(reader.read_entire_file());
  533. // I.2.2 File organization
  534. // "A particular order of those boxes in the file is not generally implied. However, the JPEG 2000 Signature box
  535. // shall be the first box in a JP2 file, the File Type box shall immediately follow the JPEG 2000 Signature box
  536. // and the JP2 Header box shall fall before the Contiguous Codestream box."
  537. if (context.boxes.size() < 4)
  538. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected at least four boxes");
  539. // Required toplevel boxes: signature box, file type box, jp2 header box, contiguous codestream box.
  540. if (context.boxes[0]->box_type() != ISOBMFF::BoxType::JPEG2000SignatureBox)
  541. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected JPEG2000SignatureBox as first box");
  542. if (context.boxes[1]->box_type() != ISOBMFF::BoxType::FileTypeBox)
  543. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected FileTypeBox as second box");
  544. Optional<size_t> jp2_header_box_index;
  545. Optional<size_t> contiguous_codestream_box_index;
  546. for (size_t i = 2; i < context.boxes.size(); ++i) {
  547. if (context.boxes[i]->box_type() == ISOBMFF::BoxType::JPEG2000HeaderBox) {
  548. // "Within a JP2 file, there shall be one and only one JP2 Header box."
  549. if (jp2_header_box_index.has_value())
  550. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple JP2 Header boxes");
  551. jp2_header_box_index = i;
  552. }
  553. if (context.boxes[i]->box_type() == ISOBMFF::BoxType::JPEG2000ContiguousCodestreamBox && !contiguous_codestream_box_index.has_value()) {
  554. // "a conforming reader shall ignore all codestreams after the first codestream found in the file.
  555. // Contiguous Codestream boxes may be found anywhere in the file except before the JP2 Header box."
  556. contiguous_codestream_box_index = i;
  557. if (!jp2_header_box_index.has_value() || contiguous_codestream_box_index.value() < jp2_header_box_index.value())
  558. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: JP2 Header box must come before Contiguous Codestream box");
  559. }
  560. }
  561. if (!jp2_header_box_index.has_value())
  562. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected JP2 Header box");
  563. if (!contiguous_codestream_box_index.has_value())
  564. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Contiguous Codestream box");
  565. // FIXME: JPEG2000ContiguousCodestreamBox makes a copy of the codestream data. That's too heavy for header scanning.
  566. // Add a mode to ISOBMFF::Reader where it only stores offsets for the codestream data and the ICC profile.
  567. auto const& codestream_box = static_cast<ISOBMFF::JPEG2000ContiguousCodestreamBox const&>(*context.boxes[contiguous_codestream_box_index.value()]);
  568. context.codestream_data = codestream_box.codestream.bytes();
  569. // Required child boxes of the jp2 header box: image header box, color box.
  570. Optional<size_t> image_header_box_index;
  571. Optional<size_t> color_header_box_index;
  572. auto const& header_box = static_cast<ISOBMFF::JPEG2000HeaderBox const&>(*context.boxes[jp2_header_box_index.value()]);
  573. for (size_t i = 0; i < header_box.child_boxes().size(); ++i) {
  574. auto const& subbox = header_box.child_boxes()[i];
  575. if (subbox->box_type() == ISOBMFF::BoxType::JPEG2000ImageHeaderBox) {
  576. if (image_header_box_index.has_value())
  577. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple Image Header boxes");
  578. image_header_box_index = i;
  579. }
  580. if (subbox->box_type() == ISOBMFF::BoxType::JPEG2000ColorSpecificationBox) {
  581. // T.800 says there should be just one 'colr' box, but T.801 allows several and says to pick the one with highest precedence.
  582. bool use_this_color_box;
  583. if (!color_header_box_index.has_value()) {
  584. use_this_color_box = true;
  585. } else {
  586. auto const& new_header_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[i]);
  587. auto const& current_color_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[color_header_box_index.value()]);
  588. use_this_color_box = new_header_box.precedence > current_color_box.precedence;
  589. }
  590. if (use_this_color_box)
  591. color_header_box_index = i;
  592. }
  593. }
  594. if (!image_header_box_index.has_value())
  595. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Image Header box");
  596. if (!color_header_box_index.has_value())
  597. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Color Specification box");
  598. auto const& image_header_box = static_cast<ISOBMFF::JPEG2000ImageHeaderBox const&>(*header_box.child_boxes()[image_header_box_index.value()]);
  599. context.size = { image_header_box.width, image_header_box.height };
  600. auto const& color_header_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[color_header_box_index.value()]);
  601. if (color_header_box.method == 2 || color_header_box.method == 3)
  602. context.icc_data = color_header_box.icc_data.bytes();
  603. TRY(parse_codestream_main_header(context));
  604. return {};
  605. }
  606. bool JPEG2000ImageDecoderPlugin::sniff(ReadonlyBytes data)
  607. {
  608. return data.starts_with(jp2_id_string);
  609. }
  610. JPEG2000ImageDecoderPlugin::JPEG2000ImageDecoderPlugin()
  611. {
  612. m_context = make<JPEG2000LoadingContext>();
  613. }
  614. IntSize JPEG2000ImageDecoderPlugin::size()
  615. {
  616. return m_context->size;
  617. }
  618. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEG2000ImageDecoderPlugin::create(ReadonlyBytes data)
  619. {
  620. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEG2000ImageDecoderPlugin()));
  621. TRY(decode_jpeg2000_header(*plugin->m_context, data));
  622. return plugin;
  623. }
  624. ErrorOr<ImageFrameDescriptor> JPEG2000ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  625. {
  626. if (index != 0)
  627. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid frame index");
  628. if (m_context->state == JPEG2000LoadingContext::State::Error)
  629. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Decoding failed");
  630. if (m_context->state < JPEG2000LoadingContext::State::DecodedTileHeaders) {
  631. TRY(parse_codestream_tile_headers(*m_context));
  632. m_context->state = JPEG2000LoadingContext::State::DecodedTileHeaders;
  633. }
  634. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Draw the rest of the owl");
  635. }
  636. ErrorOr<Optional<ReadonlyBytes>> JPEG2000ImageDecoderPlugin::icc_data()
  637. {
  638. return m_context->icc_data;
  639. }
  640. }