JPEG2000Loader.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. #include <LibTextCodec/Decoder.h>
  13. // Core coding system spec (.jp2 format): T-REC-T.800-201511-S!!PDF-E.pdf available here:
  14. // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.800-201511-S!!PDF-E&type=items
  15. // There is a useful example bitstream in the spec in:
  16. // J.10 An example of decoding showing intermediate
  17. // Extensions (.jpx format): T-REC-T.801-202106-S!!PDF-E.pdf available here:
  18. // https://handle.itu.int/11.1002/1000/14666-en?locatt=format:pdf&auth
  19. // rfc3745 lists the MIME type. It only mentions the jp2_id_string as magic number.
  20. namespace Gfx {
  21. // A JPEG2000 image can be stored in a codestream with markers, similar to a JPEG image,
  22. // or in a JP2 file, which is a container format based on boxes similar to ISOBMFF.
  23. // This is the marker for the codestream version. We don't support this yet.
  24. // T.800 Annex A, Codestream syntax, A.2 Information in the marker segments and A.3 Construction of the codestream
  25. static constexpr u8 marker_id_string[] = { 0xFF, 0x4F, 0xFF, 0x51 };
  26. // This is the marker for the box version.
  27. // T.800 Annex I, JP2 file format syntax, I.5.1 JPEG 2000 Signature box
  28. static constexpr u8 jp2_id_string[] = { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };
  29. // Table A.2 – List of markers and marker segments
  30. // "Delimiting markers and marker segments"
  31. #define J2K_SOC 0xFF4F // "Start of codestream"
  32. #define J2K_SOT 0xFF90 // "Start of tile-part"
  33. #define J2K_SOD 0xFF93 // "Start of data"
  34. #define J2K_EOC 0xFFD9 // "End of codestream"
  35. // "Fixed information marker segments"
  36. #define J2K_SIZ 0xFF51 // "Image and tile size"
  37. // "Functional marker segments"
  38. #define J2K_COD 0xFF52 // "Coding style default"
  39. #define J2K_COC 0xFF53 // "Coding style component"
  40. #define J2K_RGN 0xFF5E // "Region-of-interest"
  41. #define J2K_QCD 0xFF5C // "Quantization default"
  42. #define J2K_QCC 0xFF5D // "Quantization component"
  43. #define J2K_POC 0xFF5F // "Progression order change"
  44. // "Pointer marker segments"
  45. #define J2K_TLM 0xFF55 // "Tile-part lengths"
  46. #define J2K_PLM 0xFF57 // "Packet length, main header"
  47. #define J2K_PLT 0xFF58 // "Packet length, tile-part header"
  48. #define J2K_PPM 0xFF60 // "Packed packet headers, main header"
  49. #define J2K_PPT 0xFF61 // "Packed packet headers, tile-part header"
  50. // "In-bit-stream markers and marker segments"
  51. #define J2K_SOP 0xFF91 // "Start of packet"
  52. #define J2K_EPH 0xFF92 // "End of packet header"
  53. // "Informational marker segments"
  54. #define J2K_CRG 0xFF63 // "Component registration"
  55. #define J2K_COM 0xFF64 // "Comment"
  56. // A.4.2 Start of tile-part (SOT)
  57. struct StartOfTilePart {
  58. // "Tile index. This number refers to the tiles in raster order starting at the number 0."
  59. u16 tile_index { 0 }; // "Isot" in spec.
  60. // "Length, in bytes, from the beginning of the first byte of this SOT marker segment of the tile-part to
  61. // the end of the data of that tile-part. Figure A.16 shows this alignment. Only the last tile-part in the
  62. // codestream may contain a 0 for Psot. If the Psot is 0, this tile-part is assumed to contain all data until
  63. // the EOC marker."
  64. u32 tile_part_length { 0 }; // "Psot" in spec.
  65. // "Tile-part index. There is a specific order required for decoding tile-parts; this index denotes the order
  66. // from 0. If there is only one tile-part for a tile, then this value is zero. The tile-parts of this tile shall
  67. // appear in the codestream in this order, although not necessarily consecutively."
  68. u8 tile_part_index { 0 }; // "TPsot" in spec.
  69. // "Number of tile-parts of a tile in the codestream. Two values are allowed: the correct number of tile-
  70. // parts for that tile and zero. A zero value indicates that the number of tile-parts of this tile is not
  71. // specified in this tile-part.
  72. u8 number_of_tile_parts { 0 }; // "TNsot" in spec.
  73. };
  74. static ErrorOr<StartOfTilePart> read_start_of_tile_part(ReadonlyBytes data)
  75. {
  76. FixedMemoryStream stream { data };
  77. StartOfTilePart sot;
  78. sot.tile_index = TRY(stream.read_value<BigEndian<u16>>());
  79. sot.tile_part_length = TRY(stream.read_value<BigEndian<u32>>());
  80. sot.tile_part_index = TRY(stream.read_value<u8>());
  81. sot.number_of_tile_parts = TRY(stream.read_value<u8>());
  82. 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);
  83. return sot;
  84. }
  85. // A.5.1 Image and tile size (SIZ)
  86. struct ImageAndTileSize {
  87. // "Denotes capabilities that a decoder needs to properly decode the codestream."
  88. u16 needed_decoder_capabilities { 0 }; // "Rsiz" in spec.
  89. // "Width of the reference grid."
  90. u32 width { 0 }; // "Xsiz" in spec.
  91. // "Height of the reference grid."
  92. u32 height { 0 }; // "Ysiz" in spec.
  93. // "Horizontal offset from the origin of the reference grid to the left side of the image area."
  94. u32 x_offset { 0 }; // "XOsiz" in spec.
  95. // "Vertical offset from the origin of the reference grid to the top side of the image area."
  96. u32 y_offset { 0 }; // "YOsiz" in spec.
  97. // "Width of one reference tile with respect to the reference grid."
  98. u32 tile_width { 0 }; // "XTsiz" in spec.
  99. // "Height of one reference tile with respect to the reference grid."
  100. u32 tile_height { 0 }; // "YTsiz" in spec.
  101. // "Horizontal offset from the origin of the reference grid to the left side of the first tile."
  102. u32 tile_x_offset { 0 }; // "XTOsiz" in spec.
  103. // "Vertical offset from the origin of the reference grid to the top side of the first tile."
  104. u32 tile_y_offset { 0 }; // "YTOsiz" in spec.
  105. // "Csiz" isn't stored in this struct. It corresponds to `components.size()`.
  106. struct ComponentInformation {
  107. // "Precision (depth) in bits and sign of the ith component samples."
  108. u8 depth_and_sign { 0 }; // "Ssiz" in spec.
  109. // Table A.11 – Component Ssiz parameter
  110. u8 bit_depth() const { return (depth_and_sign & 0x7F) + 1; }
  111. bool is_signed() const { return depth_and_sign & 0x80; }
  112. // "Horizontal separation of a sample of the ith component with respect to the reference grid."
  113. u8 horizontal_separation { 0 }; // "XRsiz" in spec.
  114. // "Vertical separation of a sample of the ith component with respect to the reference grid."
  115. u8 vertical_separation { 0 }; // "YRsiz" in spec.
  116. };
  117. Vector<ComponentInformation> components;
  118. };
  119. static ErrorOr<ImageAndTileSize> read_image_and_tile_size(ReadonlyBytes data)
  120. {
  121. FixedMemoryStream stream { data };
  122. ImageAndTileSize siz;
  123. siz.needed_decoder_capabilities = TRY(stream.read_value<BigEndian<u16>>());
  124. siz.width = TRY(stream.read_value<BigEndian<u32>>());
  125. siz.height = TRY(stream.read_value<BigEndian<u32>>());
  126. siz.x_offset = TRY(stream.read_value<BigEndian<u32>>());
  127. siz.y_offset = TRY(stream.read_value<BigEndian<u32>>());
  128. siz.tile_width = TRY(stream.read_value<BigEndian<u32>>());
  129. siz.tile_height = TRY(stream.read_value<BigEndian<u32>>());
  130. siz.tile_x_offset = TRY(stream.read_value<BigEndian<u32>>());
  131. siz.tile_y_offset = TRY(stream.read_value<BigEndian<u32>>());
  132. u16 component_count = TRY(stream.read_value<BigEndian<u16>>()); // "Csiz" in spec.
  133. for (size_t i = 0; i < component_count; ++i) {
  134. ImageAndTileSize::ComponentInformation component;
  135. component.depth_and_sign = TRY(stream.read_value<u8>());
  136. if (component.bit_depth() > 38)
  137. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid component depth");
  138. component.horizontal_separation = TRY(stream.read_value<u8>());
  139. component.vertical_separation = TRY(stream.read_value<u8>());
  140. siz.components.append(component);
  141. }
  142. 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);
  143. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: SIZ marker segment: {} components:", component_count);
  144. for (auto [i, component] : enumerate(siz.components))
  145. 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);
  146. return siz;
  147. }
  148. // Data shared by COD and COC marker segments
  149. struct CodingStyleParameters {
  150. // Table A.20 – Transformation for the SPcod and SPcoc parameters
  151. enum Transformation {
  152. Irreversible_9_7_Filter = 0,
  153. Reversible_5_3_Filter = 1,
  154. };
  155. // Table A.15 – Coding style parameter values of the SPcod and SPcoc parameters
  156. // "Number of decomposition levels, NL, Zero implies no transformation."
  157. u8 number_of_decomposition_levels { 0 };
  158. u8 code_block_width_exponent { 0 }; // "xcb" in spec; 2 already added.
  159. u8 code_block_height_exponent { 0 }; // "ycb" in spec; 2 already added.
  160. u8 code_block_style { 0 };
  161. Transformation transformation { Irreversible_9_7_Filter };
  162. // Table A.19 – Code-block style for the SPcod and SPcoc parameters
  163. bool uses_selective_arithmetic_coding_bypass() const { return code_block_style & 1; }
  164. bool reset_context_probabilities() const { return code_block_style & 2; }
  165. bool uses_termination_on_each_coding_pass() const { return code_block_style & 4; }
  166. bool uses_vertically_causal_context() const { return code_block_style & 8; }
  167. bool uses_predictable_termination() const { return code_block_style & 0x10; }
  168. bool uses_segmentation_symbols() const { return code_block_style & 0x20; }
  169. // If has_explicit_precinct_size is false, this contains the default { 15, 15 } number_of_decomposition_levels + 1 times.
  170. // If has_explicit_precinct_size is true, this contains number_of_decomposition_levels + 1 explicit values stored in the COD marker segment.
  171. struct PrecinctSize {
  172. u8 PPx { 0 };
  173. u8 PPy { 0 };
  174. };
  175. Vector<PrecinctSize> precinct_sizes;
  176. };
  177. static ErrorOr<CodingStyleParameters> read_coding_style_parameters(ReadonlyBytes data, StringView name, bool has_explicit_precinct_size)
  178. {
  179. FixedMemoryStream stream { data };
  180. CodingStyleParameters parameters;
  181. parameters.number_of_decomposition_levels = TRY(stream.read_value<u8>());
  182. if (parameters.number_of_decomposition_levels > 32)
  183. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid number of decomposition levels");
  184. // Table A.18 – Width or height exponent of the code-blocks for the SPcod and SPcoc parameters
  185. u8 xcb = (TRY(stream.read_value<u8>()) & 0xF) + 2;
  186. u8 ycb = (TRY(stream.read_value<u8>()) & 0xF) + 2;
  187. if (xcb > 10 || ycb > 10 || xcb + ycb > 12)
  188. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid code block size");
  189. parameters.code_block_width_exponent = xcb;
  190. parameters.code_block_height_exponent = ycb;
  191. parameters.code_block_style = TRY(stream.read_value<u8>());
  192. u8 transformation = TRY(stream.read_value<u8>());
  193. if (transformation > 1)
  194. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid transformation");
  195. parameters.transformation = static_cast<CodingStyleParameters::Transformation>(transformation);
  196. if (has_explicit_precinct_size) {
  197. for (size_t i = 0; i < parameters.number_of_decomposition_levels + 1u; ++i) {
  198. u8 b = TRY(stream.read_value<u8>());
  199. // Table A.21 – Precinct width and height for the SPcod and SPcoc parameters
  200. CodingStyleParameters::PrecinctSize precinct_size;
  201. precinct_size.PPx = b & 0xF;
  202. precinct_size.PPy = b >> 4;
  203. if ((precinct_size.PPx == 0 || precinct_size.PPy == 0) && i > 0)
  204. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid precinct size");
  205. parameters.precinct_sizes.append(precinct_size);
  206. }
  207. } else {
  208. for (size_t i = 0; i < parameters.number_of_decomposition_levels + 1u; ++i)
  209. parameters.precinct_sizes.append({ 15, 15 });
  210. }
  211. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: number_of_decomposition_levels={}, code_block_width_exponent={}, code_block_height_exponent={}", name, parameters.number_of_decomposition_levels, parameters.code_block_width_exponent, parameters.code_block_height_exponent);
  212. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: code_block_style={}, transformation={}", name, parameters.code_block_style, (int)parameters.transformation);
  213. if (has_explicit_precinct_size) {
  214. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: {} explicit precinct sizes:", name, parameters.precinct_sizes.size());
  215. for (auto [i, precinct_size] : enumerate(parameters.precinct_sizes))
  216. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: precinct_size[{}]: PPx={}, PPy={}", name, i, precinct_size.PPx, precinct_size.PPy);
  217. }
  218. return parameters;
  219. }
  220. // A.6.1 Coding style default (COD)
  221. struct CodingStyleDefault {
  222. // Table A.13 – Coding style parameter values for the Scod parameter
  223. bool has_explicit_precinct_size { false };
  224. bool may_use_SOP_marker { false };
  225. bool may_use_EPH_marker { false };
  226. // Table A.16 – Progression order for the SGcod, SPcoc, and Ppoc parameters
  227. // B.12 Progression order
  228. enum ProgressionOrder {
  229. LayerResolutionComponentPosition = 0,
  230. ResolutionLayerComponentPosition = 1,
  231. ResolutionPositionComponentLayer = 2,
  232. PositionComponentResolutionLayer = 3,
  233. ComponentPositionResolutionLayer = 4,
  234. };
  235. // Table A.17 – Multiple component transformation for the SGcod parameters
  236. enum MultipleComponentTransformationType {
  237. None = 0,
  238. MultipleComponentTransformationUsed = 1, // See Annex G
  239. };
  240. // Table A.14 – Coding style parameter values of the SGcod parameter
  241. ProgressionOrder progression_order { LayerResolutionComponentPosition };
  242. u16 number_of_layers { 0 };
  243. MultipleComponentTransformationType multiple_component_transformation_type { None };
  244. CodingStyleParameters parameters;
  245. };
  246. static ErrorOr<CodingStyleDefault> read_coding_style_default(ReadonlyBytes data)
  247. {
  248. FixedMemoryStream stream { data };
  249. CodingStyleDefault cod;
  250. u8 Scod = TRY(stream.read_value<u8>());
  251. cod.has_explicit_precinct_size = Scod & 1;
  252. cod.may_use_SOP_marker = Scod & 2;
  253. cod.may_use_EPH_marker = Scod & 4;
  254. u32 SGcod = TRY(stream.read_value<BigEndian<u32>>());
  255. u8 progression_order = SGcod >> 24;
  256. if (progression_order > 4)
  257. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid progression order");
  258. cod.progression_order = static_cast<CodingStyleDefault::ProgressionOrder>(progression_order);
  259. cod.number_of_layers = (SGcod >> 8) & 0xFFFF;
  260. if (cod.number_of_layers == 0)
  261. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid number of layers");
  262. u8 multiple_component_transformation_type = SGcod & 0xFF;
  263. if (multiple_component_transformation_type > 1)
  264. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid multiple component transformation type");
  265. cod.multiple_component_transformation_type = static_cast<CodingStyleDefault::MultipleComponentTransformationType>(multiple_component_transformation_type);
  266. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: has_explicit_precinct_size={}, may_use_SOP_marker={}, may_use_EPH_marker={}", cod.has_explicit_precinct_size, cod.may_use_SOP_marker, cod.may_use_EPH_marker);
  267. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COD marker segment: progression_order={}, number_of_layers={}, multiple_component_transformation_type={}", (int)cod.progression_order, cod.number_of_layers, (int)cod.multiple_component_transformation_type);
  268. cod.parameters = TRY(read_coding_style_parameters(data.slice(stream.offset()), "COD"sv, cod.has_explicit_precinct_size));
  269. return cod;
  270. }
  271. // A.6.2 Coding style component (COC)
  272. struct CodingStyleComponent {
  273. u16 component_index { 0 }; // "Ccoc" in spec.
  274. // Table A.23 – Coding style parameter values for the Scoc parameter
  275. bool has_explicit_precinct_size { false }; // "Scoc" in spec.
  276. CodingStyleParameters parameters;
  277. };
  278. static ErrorOr<CodingStyleComponent> read_coding_style_component(ReadonlyBytes data, size_t number_of_components)
  279. {
  280. FixedMemoryStream stream { data };
  281. // Table A.22 – Coding style component parameter values
  282. CodingStyleComponent coc;
  283. if (number_of_components < 257)
  284. coc.component_index = TRY(stream.read_value<u8>());
  285. else
  286. coc.component_index = TRY(stream.read_value<BigEndian<u16>>());
  287. u8 Scoc = TRY(stream.read_value<u8>());
  288. coc.has_explicit_precinct_size = Scoc & 1;
  289. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COC marker segment: component_index={}", coc.component_index);
  290. coc.parameters = TRY(read_coding_style_parameters(data.slice(TRY(stream.tell())), "COC"sv, coc.has_explicit_precinct_size));
  291. return coc;
  292. }
  293. // A.6.4 Quantization default (QCD)
  294. struct QuantizationDefault {
  295. enum QuantizationStyle {
  296. NoQuantization = 0,
  297. ScalarDerived = 1,
  298. ScalarExpounded = 2,
  299. };
  300. QuantizationStyle quantization_style { NoQuantization };
  301. u8 number_of_guard_bits { 0 };
  302. struct ReversibleStepSize {
  303. u8 exponent { 0 };
  304. };
  305. struct IrreversibleStepSize {
  306. u16 mantissa { 0 };
  307. u8 exponent { 0 };
  308. };
  309. // Stores a Vector<ReversibleStepSize> if quantization_style is NoQuantization, and a Vector<IrreversibleStepSize> otherwise.
  310. // The size of the vector is >= 3*number_of_decomposition_levels + 1 if quantization_style is not ScalarDerived, and 1 otherwise.
  311. using StepSizeType = Variant<Empty, Vector<ReversibleStepSize>, Vector<IrreversibleStepSize>>;
  312. StepSizeType step_sizes;
  313. };
  314. static ErrorOr<QuantizationDefault> read_quantization_default(ReadonlyBytes data, StringView marker_name = "QCD"sv)
  315. {
  316. FixedMemoryStream stream { data };
  317. QuantizationDefault qcd;
  318. u8 sqcd = TRY(stream.read_value<u8>());
  319. u8 quantization_style = sqcd & 0x1F;
  320. if (quantization_style > 2)
  321. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid quantization style");
  322. qcd.quantization_style = static_cast<QuantizationDefault::QuantizationStyle>(quantization_style);
  323. qcd.number_of_guard_bits = sqcd >> 5;
  324. qcd.step_sizes = TRY([&]() -> ErrorOr<QuantizationDefault::StepSizeType> {
  325. if (quantization_style == QuantizationDefault::NoQuantization) {
  326. // Table A.29 – Reversible step size values for the SPqcd and SPqcc parameters (reversible transform only)
  327. if (data.size() < 2)
  328. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for QCD marker segment");
  329. u8 number_of_decomposition_levels = (data.size() - 2) / 3;
  330. Vector<QuantizationDefault::ReversibleStepSize> reversible_step_sizes;
  331. for (size_t i = 0; i < 1u + 3u * number_of_decomposition_levels; ++i)
  332. reversible_step_sizes.append({ static_cast<u8>(TRY(stream.read_value<u8>()) >> 3) });
  333. return reversible_step_sizes;
  334. }
  335. // Table A.30 – Quantization values for the SPqcd and SPqcc parameters (irreversible transformation only)
  336. if (data.size() < 3)
  337. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for QCD marker segment");
  338. u8 number_of_decomposition_levels = 0;
  339. if (quantization_style == QuantizationDefault::ScalarExpounded)
  340. number_of_decomposition_levels = (data.size() - 3) / 6;
  341. Vector<QuantizationDefault::IrreversibleStepSize> irreversible_step_sizes;
  342. for (size_t i = 0; i < 1u + 3u * number_of_decomposition_levels; ++i) {
  343. u16 value = TRY(stream.read_value<BigEndian<u16>>());
  344. QuantizationDefault::IrreversibleStepSize step_size;
  345. step_size.mantissa = value & 0x7FF;
  346. step_size.exponent = value >> 11;
  347. irreversible_step_sizes.append(step_size);
  348. }
  349. return irreversible_step_sizes;
  350. }());
  351. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: quantization_style={}, number_of_guard_bits={}", marker_name, (int)qcd.quantization_style, qcd.number_of_guard_bits);
  352. qcd.step_sizes.visit(
  353. [](Empty) { VERIFY_NOT_REACHED(); },
  354. [&](Vector<QuantizationDefault::ReversibleStepSize> const& step_sizes) {
  355. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: {} step sizes:", marker_name, step_sizes.size());
  356. for (auto [i, step_size] : enumerate(step_sizes)) {
  357. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: step_size[{}]: exponent={}", marker_name, i, step_size.exponent);
  358. }
  359. },
  360. [&](Vector<QuantizationDefault::IrreversibleStepSize> const& step_sizes) {
  361. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: {} step sizes:", marker_name, step_sizes.size());
  362. for (auto [i, step_size] : enumerate(step_sizes)) {
  363. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: {} marker segment: step_size[{}]: mantissa={}, exponent={}", marker_name, i, step_size.mantissa, step_size.exponent);
  364. }
  365. });
  366. return qcd;
  367. }
  368. // A.6.5 Quantization component (QCC)
  369. struct QuantizationComponent {
  370. u16 component_index { 0 }; // "Cqcc" in spec.
  371. QuantizationDefault qcd;
  372. };
  373. static ErrorOr<QuantizationComponent> read_quantization_component(ReadonlyBytes data, size_t number_of_components)
  374. {
  375. FixedMemoryStream stream { data };
  376. QuantizationComponent qcc;
  377. if (number_of_components < 257)
  378. qcc.component_index = TRY(stream.read_value<u8>());
  379. else
  380. qcc.component_index = TRY(stream.read_value<BigEndian<u16>>());
  381. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: QCC marker segment: component_index={}", qcc.component_index);
  382. qcc.qcd = TRY(read_quantization_default(data.slice(TRY(stream.tell())), "QCC"sv));
  383. return qcc;
  384. }
  385. // A.9.2 Comment (COM)
  386. struct Comment {
  387. enum CommentType {
  388. Binary = 0,
  389. ISO_IEC_8859_15 = 1,
  390. };
  391. CommentType type { Binary }; // "Rcom" in spec.
  392. ReadonlyBytes data;
  393. };
  394. static ErrorOr<Comment> read_comment(ReadonlyBytes data)
  395. {
  396. FixedMemoryStream stream { data };
  397. Comment com;
  398. u16 comment_type = TRY(stream.read_value<BigEndian<u16>>());
  399. if (comment_type > 1)
  400. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid comment type");
  401. com.type = static_cast<Comment::CommentType>(comment_type);
  402. com.data = data.slice(TRY(stream.tell()));
  403. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COM marker segment: comment_type={}, size()={}", (int)com.type, com.data.size());
  404. if (com.type == Comment::ISO_IEC_8859_15)
  405. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: COM marker segment, ISO/IEC 8859-15 text: '{}'", TRY(TextCodec::decoder_for("ISO-8859-15"sv)->to_utf8(StringView { com.data })));
  406. return com;
  407. }
  408. struct TilePartData {
  409. StartOfTilePart sot;
  410. Vector<Comment> coms;
  411. ReadonlyBytes data;
  412. };
  413. struct TileData {
  414. Optional<CodingStyleDefault> cod;
  415. Vector<CodingStyleComponent> cocs;
  416. Optional<QuantizationDefault> qcd;
  417. Vector<QuantizationComponent> qccs;
  418. Vector<TilePartData> tile_parts;
  419. };
  420. struct JPEG2000LoadingContext {
  421. enum class State {
  422. NotDecoded = 0,
  423. DecodedTileHeaders,
  424. Error,
  425. };
  426. State state { State::NotDecoded };
  427. ReadonlyBytes codestream_data;
  428. size_t codestream_cursor { 0 };
  429. Optional<ReadonlyBytes> icc_data;
  430. IntSize size;
  431. ISOBMFF::BoxList boxes;
  432. // Data from marker segments:
  433. ImageAndTileSize siz;
  434. CodingStyleDefault cod;
  435. Vector<CodingStyleComponent> cocs;
  436. QuantizationDefault qcd;
  437. Vector<QuantizationComponent> qccs;
  438. Vector<Comment> coms;
  439. Vector<TileData> tiles;
  440. };
  441. struct MarkerSegment {
  442. u16 marker;
  443. // OptionalNone for markers that don't have data.
  444. // 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.)
  445. Optional<ReadonlyBytes> data;
  446. };
  447. static ErrorOr<u16> peek_marker(JPEG2000LoadingContext& context)
  448. {
  449. if (context.codestream_cursor + 2 > context.codestream_data.size())
  450. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker");
  451. return *reinterpret_cast<BigEndian<u16> const*>(context.codestream_data.data() + context.codestream_cursor);
  452. }
  453. static ErrorOr<MarkerSegment> read_marker_at_cursor(JPEG2000LoadingContext& context)
  454. {
  455. u16 marker = TRY(peek_marker(context));
  456. // "All markers with the marker code between 0xFF30 and 0xFF3F have no marker segment parameters. They shall be skipped by the decoder."
  457. // "The SOC, SOD and EOC are delimiting markers not marker segments, and have no explicit length information or other parameters."
  458. bool is_marker_segment = !(marker >= 0xFF30 && marker <= 0xFF3F) && marker != J2K_SOC && marker != J2K_SOD && marker != J2K_EOC;
  459. MarkerSegment marker_segment;
  460. marker_segment.marker = marker;
  461. if (is_marker_segment) {
  462. if (context.codestream_cursor + 4 > context.codestream_data.size())
  463. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker segment length");
  464. u16 marker_length = *reinterpret_cast<BigEndian<u16> const*>(context.codestream_data.data() + context.codestream_cursor + 2);
  465. if (marker_length < 2)
  466. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Marker segment length too small");
  467. if (context.codestream_cursor + 2 + marker_length > context.codestream_data.size())
  468. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for marker segment data");
  469. marker_segment.data = ReadonlyBytes { context.codestream_data.data() + context.codestream_cursor + 4, marker_length - 2u };
  470. }
  471. context.codestream_cursor += 2;
  472. if (is_marker_segment)
  473. context.codestream_cursor += 2 + marker_segment.data->size();
  474. return marker_segment;
  475. }
  476. static ErrorOr<void> parse_codestream_main_header(JPEG2000LoadingContext& context)
  477. {
  478. // Figure A.3 – Construction of the main header
  479. // "Required as the first marker"
  480. auto marker = TRY(read_marker_at_cursor(context));
  481. if (marker.marker != J2K_SOC)
  482. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SOC marker");
  483. // "Required as the second marker segment"
  484. marker = TRY(read_marker_at_cursor(context));
  485. if (marker.marker != J2K_SIZ)
  486. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SIZ marker");
  487. context.siz = TRY(read_image_and_tile_size(marker.data.value()));
  488. bool saw_COD_marker = false;
  489. bool saw_QCD_marker = false;
  490. while (true) {
  491. u16 marker = TRY(peek_marker(context));
  492. switch (marker) {
  493. case J2K_COD:
  494. case J2K_COC:
  495. case J2K_QCD:
  496. case J2K_QCC:
  497. case J2K_RGN:
  498. case J2K_POC:
  499. case J2K_PPM:
  500. case J2K_TLM:
  501. case J2K_PLM:
  502. case J2K_CRG:
  503. case J2K_COM: {
  504. auto marker = TRY(read_marker_at_cursor(context));
  505. if (marker.marker == J2K_COD) {
  506. if (saw_COD_marker)
  507. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple COD markers in main header");
  508. context.cod = TRY(read_coding_style_default(marker.data.value()));
  509. saw_COD_marker = true;
  510. } else if (marker.marker == J2K_COC) {
  511. context.cocs.append(TRY(read_coding_style_component(marker.data.value(), context.siz.components.size())));
  512. } else if (marker.marker == J2K_QCD) {
  513. if (saw_QCD_marker)
  514. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple QCD markers in main header");
  515. context.qcd = TRY(read_quantization_default(marker.data.value()));
  516. saw_QCD_marker = true;
  517. } else if (marker.marker == J2K_QCC) {
  518. context.qccs.append(TRY(read_quantization_component(marker.data.value(), context.siz.components.size())));
  519. } else if (marker.marker == J2K_COM) {
  520. context.coms.append(TRY(read_comment(marker.data.value())));
  521. } else {
  522. // FIXME: These are valid main header markers. Parse contents.
  523. dbgln("JPEG2000ImageDecoderPlugin: marker {:#04x} not yet implemented", marker.marker);
  524. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: marker not yet implemented");
  525. }
  526. break;
  527. }
  528. case J2K_SOT: {
  529. // SOT terminates the main header.
  530. // A.4.2: "There shall be at least one SOT in a codestream."
  531. if (!saw_COD_marker)
  532. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Required COD marker not present in main header");
  533. if (!saw_QCD_marker)
  534. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Required QCD marker not present in main header");
  535. // A.6.4: "there is not necessarily a correspondence with the number of sub-bands present because the sub-bands
  536. // can be truncated with no requirement to correct [the QCD] marker segment."
  537. size_t step_sizes_count = context.qcd.step_sizes.visit(
  538. [](Empty) -> size_t { VERIFY_NOT_REACHED(); },
  539. [](Vector<QuantizationDefault::ReversibleStepSize> const& step_sizes) { return step_sizes.size(); },
  540. [](Vector<QuantizationDefault::IrreversibleStepSize> const& step_sizes) { return step_sizes.size(); });
  541. // FIXME: What if number_of_decomposition_levels is in context.cocs and varies by component?
  542. if (context.qcd.quantization_style != QuantizationDefault::ScalarDerived && step_sizes_count < context.cod.parameters.number_of_decomposition_levels * 3u + 1u)
  543. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough step sizes for number of decomposition levels");
  544. return {};
  545. }
  546. default:
  547. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected marker in main header");
  548. }
  549. }
  550. }
  551. static ErrorOr<void> parse_codestream_tile_header(JPEG2000LoadingContext& context)
  552. {
  553. // Figure A.4 – Construction of the first tile-part header of a given tile
  554. // Figure A.5 – Construction of a non-first tile-part header
  555. // "Required as the first marker segment of every tile-part header"
  556. auto tile_start = context.codestream_cursor;
  557. auto marker = TRY(read_marker_at_cursor(context));
  558. if (marker.marker != J2K_SOT)
  559. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected SOT marker");
  560. auto start_of_tile = TRY(read_start_of_tile_part(marker.data.value()));
  561. // FIXME: Store start_of_tile on context somewhere.
  562. context.tiles.resize(max(context.tiles.size(), (size_t)start_of_tile.tile_index + 1));
  563. auto& tile = context.tiles[start_of_tile.tile_index];
  564. if (tile.tile_parts.size() != start_of_tile.tile_part_index)
  565. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Tile part index out of order");
  566. tile.tile_parts.append({});
  567. auto& tile_part = tile.tile_parts.last();
  568. tile_part.sot = start_of_tile;
  569. bool found_start_of_data = false;
  570. while (!found_start_of_data) {
  571. u16 marker = TRY(peek_marker(context));
  572. switch (marker) {
  573. case J2K_SOD:
  574. // "Required as the last marker segment of every tile-part header"
  575. context.codestream_cursor += 2;
  576. found_start_of_data = true;
  577. break;
  578. case J2K_COD:
  579. case J2K_COC:
  580. case J2K_QCD:
  581. case J2K_QCC:
  582. case J2K_RGN:
  583. if (start_of_tile.tile_part_index != 0)
  584. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: COD, COC, QCD, QCC, RGN markers are only valid in the first tile-part header");
  585. [[fallthrough]];
  586. case J2K_POC:
  587. case J2K_PPT:
  588. case J2K_PLT:
  589. case J2K_COM: {
  590. auto marker = TRY(read_marker_at_cursor(context));
  591. if (marker.marker == J2K_COD) {
  592. if (tile.cod.has_value())
  593. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple COD markers in tile header");
  594. tile.cod = TRY(read_coding_style_default(marker.data.value()));
  595. } else if (marker.marker == J2K_COC) {
  596. tile.cocs.append(TRY(read_coding_style_component(marker.data.value(), context.siz.components.size())));
  597. } else if (marker.marker == J2K_QCD) {
  598. if (tile.qcd.has_value())
  599. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple QCD markers in tile header");
  600. tile.qcd = TRY(read_quantization_default(marker.data.value()));
  601. } else if (marker.marker == J2K_QCC) {
  602. tile.qccs.append(TRY(read_quantization_component(marker.data.value(), context.siz.components.size())));
  603. } else if (marker.marker == J2K_COM) {
  604. tile_part.coms.append(TRY(read_comment(marker.data.value())));
  605. } else {
  606. // FIXME: These are valid main header markers. Parse contents.
  607. dbgln("JPEG2000ImageDecoderPlugin: marker {:#04x} not yet implemented in tile header", marker.marker);
  608. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: marker not yet implemented in tile header");
  609. }
  610. break;
  611. }
  612. default:
  613. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected marker in tile header");
  614. }
  615. }
  616. u32 tile_bitstream_length;
  617. if (start_of_tile.tile_part_length == 0) {
  618. // Leave room for EOC marker.
  619. if (context.codestream_data.size() - context.codestream_cursor < 2)
  620. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for EOC marker");
  621. tile_bitstream_length = context.codestream_data.size() - context.codestream_cursor - 2;
  622. } else {
  623. u32 tile_header_length = context.codestream_cursor - tile_start;
  624. if (start_of_tile.tile_part_length < tile_header_length)
  625. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid tile part length");
  626. tile_bitstream_length = start_of_tile.tile_part_length - tile_header_length;
  627. }
  628. if (context.codestream_cursor + tile_bitstream_length > context.codestream_data.size())
  629. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Not enough data for tile bitstream");
  630. tile_part.data = context.codestream_data.slice(context.codestream_cursor, tile_bitstream_length);
  631. context.codestream_cursor += tile_bitstream_length;
  632. dbgln_if(JPEG2000_DEBUG, "JPEG2000ImageDecoderPlugin: Tile bitstream length: {}", tile_bitstream_length);
  633. return {};
  634. }
  635. static ErrorOr<void> parse_codestream_tile_headers(JPEG2000LoadingContext& context)
  636. {
  637. while (true) {
  638. auto marker = TRY(peek_marker(context));
  639. if (marker == J2K_EOC) {
  640. context.codestream_cursor += 2;
  641. break;
  642. }
  643. TRY(parse_codestream_tile_header(context));
  644. }
  645. if (context.codestream_cursor < context.codestream_data.size())
  646. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Unexpected data after EOC marker");
  647. return {};
  648. }
  649. static ErrorOr<void> decode_jpeg2000_header(JPEG2000LoadingContext& context, ReadonlyBytes data)
  650. {
  651. if (!JPEG2000ImageDecoderPlugin::sniff(data))
  652. return Error::from_string_literal("JPEG2000LoadingContext: Invalid JPEG2000 header");
  653. if (data.starts_with(marker_id_string)) {
  654. context.codestream_data = data;
  655. TRY(parse_codestream_main_header(context));
  656. context.size = { context.siz.width, context.siz.height };
  657. return {};
  658. }
  659. auto reader = TRY(Gfx::ISOBMFF::Reader::create(TRY(try_make<FixedMemoryStream>(data))));
  660. context.boxes = TRY(reader.read_entire_file());
  661. // I.2.2 File organization
  662. // "A particular order of those boxes in the file is not generally implied. However, the JPEG 2000 Signature box
  663. // shall be the first box in a JP2 file, the File Type box shall immediately follow the JPEG 2000 Signature box
  664. // and the JP2 Header box shall fall before the Contiguous Codestream box."
  665. if (context.boxes.size() < 4)
  666. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected at least four boxes");
  667. // Required toplevel boxes: signature box, file type box, jp2 header box, contiguous codestream box.
  668. if (context.boxes[0]->box_type() != ISOBMFF::BoxType::JPEG2000SignatureBox)
  669. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected JPEG2000SignatureBox as first box");
  670. if (context.boxes[1]->box_type() != ISOBMFF::BoxType::FileTypeBox)
  671. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected FileTypeBox as second box");
  672. Optional<size_t> jp2_header_box_index;
  673. Optional<size_t> contiguous_codestream_box_index;
  674. for (size_t i = 2; i < context.boxes.size(); ++i) {
  675. if (context.boxes[i]->box_type() == ISOBMFF::BoxType::JPEG2000HeaderBox) {
  676. // "Within a JP2 file, there shall be one and only one JP2 Header box."
  677. if (jp2_header_box_index.has_value())
  678. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple JP2 Header boxes");
  679. jp2_header_box_index = i;
  680. }
  681. if (context.boxes[i]->box_type() == ISOBMFF::BoxType::JPEG2000ContiguousCodestreamBox && !contiguous_codestream_box_index.has_value()) {
  682. // "a conforming reader shall ignore all codestreams after the first codestream found in the file.
  683. // Contiguous Codestream boxes may be found anywhere in the file except before the JP2 Header box."
  684. contiguous_codestream_box_index = i;
  685. if (!jp2_header_box_index.has_value() || contiguous_codestream_box_index.value() < jp2_header_box_index.value())
  686. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: JP2 Header box must come before Contiguous Codestream box");
  687. }
  688. }
  689. if (!jp2_header_box_index.has_value())
  690. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected JP2 Header box");
  691. if (!contiguous_codestream_box_index.has_value())
  692. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Contiguous Codestream box");
  693. // FIXME: JPEG2000ContiguousCodestreamBox makes a copy of the codestream data. That's too heavy for header scanning.
  694. // Add a mode to ISOBMFF::Reader where it only stores offsets for the codestream data and the ICC profile.
  695. auto const& codestream_box = static_cast<ISOBMFF::JPEG2000ContiguousCodestreamBox const&>(*context.boxes[contiguous_codestream_box_index.value()]);
  696. context.codestream_data = codestream_box.codestream.bytes();
  697. // Required child boxes of the jp2 header box: image header box, color box.
  698. Optional<size_t> image_header_box_index;
  699. Optional<size_t> color_header_box_index;
  700. auto const& header_box = static_cast<ISOBMFF::JPEG2000HeaderBox const&>(*context.boxes[jp2_header_box_index.value()]);
  701. for (size_t i = 0; i < header_box.child_boxes().size(); ++i) {
  702. auto const& subbox = header_box.child_boxes()[i];
  703. if (subbox->box_type() == ISOBMFF::BoxType::JPEG2000ImageHeaderBox) {
  704. if (image_header_box_index.has_value())
  705. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Multiple Image Header boxes");
  706. image_header_box_index = i;
  707. }
  708. if (subbox->box_type() == ISOBMFF::BoxType::JPEG2000ColorSpecificationBox) {
  709. // T.800 says there should be just one 'colr' box, but T.801 allows several and says to pick the one with highest precedence.
  710. bool use_this_color_box;
  711. if (!color_header_box_index.has_value()) {
  712. use_this_color_box = true;
  713. } else {
  714. auto const& new_header_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[i]);
  715. auto const& current_color_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[color_header_box_index.value()]);
  716. use_this_color_box = new_header_box.precedence > current_color_box.precedence;
  717. }
  718. if (use_this_color_box)
  719. color_header_box_index = i;
  720. }
  721. }
  722. if (!image_header_box_index.has_value())
  723. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Image Header box");
  724. if (!color_header_box_index.has_value())
  725. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Expected Color Specification box");
  726. auto const& image_header_box = static_cast<ISOBMFF::JPEG2000ImageHeaderBox const&>(*header_box.child_boxes()[image_header_box_index.value()]);
  727. context.size = { image_header_box.width, image_header_box.height };
  728. auto const& color_header_box = static_cast<ISOBMFF::JPEG2000ColorSpecificationBox const&>(*header_box.child_boxes()[color_header_box_index.value()]);
  729. if (color_header_box.method == 2 || color_header_box.method == 3)
  730. context.icc_data = color_header_box.icc_data.bytes();
  731. TRY(parse_codestream_main_header(context));
  732. auto size_from_siz = IntSize { context.siz.width, context.siz.height };
  733. if (size_from_siz != context.size) {
  734. // FIXME: If this is common, warn and use size from SIZ marker.
  735. dbgln("JPEG2000ImageDecoderPlugin: Image size from SIZ marker ({}) does not match image size from JP2 header ({})", size_from_siz, context.size);
  736. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Image size from SIZ marker does not match image size from JP2 header");
  737. }
  738. return {};
  739. }
  740. namespace JPEG2000 {
  741. // Tag trees are used to store the code-block inclusion bits and the zero bit-plane information.
  742. // B.10.2 Tag trees
  743. // "At every node of this tree the minimum integer of the (up to four) nodes below it is recorded. [...]
  744. // Level 0 is the lowest level of the tag tree; it contains the top node. [...]
  745. // Each node has a [...] current value, [...] initialized to zero. A 0 bit in the tag tree means that the minimum
  746. // (or the value in the case of the highest level) is larger than the current value and a 1 bit means that the minimum
  747. // (or the value in the case of the highest level) is equal to the current value.
  748. // For each contiguous 0 bit in the tag tree the current value is incremented by one.
  749. // Nodes at higher levels cannot be coded until lower level node values are fixed (i.e, a 1 bit is coded). [...]
  750. // Only the information needed for the current code-block is stored at the current point in the packet header."
  751. // The example in Figure B.13 / Table B.5 is useful to understand what exactly "only the information needed" means.
  752. struct TagTreeNode {
  753. u32 value { 0 };
  754. enum State {
  755. Pending,
  756. Final,
  757. };
  758. State state { Pending };
  759. Array<OwnPtr<TagTreeNode>, 4> children {};
  760. u32 level { 0 }; // 0 for leaf nodes, 1 for the next level, etc.
  761. bool is_leaf() const { return level == 0; }
  762. ErrorOr<u32> read_value(u32 x, u32 y, Function<ErrorOr<bool>()> const& read_bit, u32 start_value, Optional<u32> stop_at = {})
  763. {
  764. value = max(value, start_value);
  765. while (true) {
  766. if (stop_at.has_value() && value == stop_at.value())
  767. return value;
  768. if (state == Final) {
  769. if (is_leaf())
  770. return value;
  771. u32 x_index = (x >> (level - 1)) & 1;
  772. u32 y_index = (y >> (level - 1)) & 1;
  773. return children[y_index * 2 + x_index]->read_value(x, y, read_bit, value, stop_at);
  774. }
  775. bool bit = TRY(read_bit());
  776. if (!bit)
  777. value++;
  778. else
  779. state = Final;
  780. }
  781. }
  782. static ErrorOr<NonnullOwnPtr<TagTreeNode>> create(u32 x_count, u32 y_count, u32 level)
  783. {
  784. VERIFY(x_count > 0);
  785. VERIFY(y_count > 0);
  786. auto node = TRY(try_make<TagTreeNode>());
  787. node->level = level;
  788. if (node->is_leaf()) {
  789. VERIFY(x_count == 1);
  790. VERIFY(y_count == 1);
  791. return node;
  792. }
  793. u32 top_left_x_child_count = min(x_count, 1u << (max(level, 1) - 1));
  794. u32 top_left_y_child_count = min(y_count, 1u << (max(level, 1) - 1));
  795. for (u32 y = 0; y < 2; ++y) {
  796. for (u32 x = 0; x < 2; ++x) {
  797. u32 child_x_count = x == 1 ? x_count - top_left_x_child_count : top_left_x_child_count;
  798. u32 child_y_count = y == 1 ? y_count - top_left_y_child_count : top_left_y_child_count;
  799. if (child_x_count == 0 || child_y_count == 0)
  800. continue;
  801. node->children[y * 2 + x] = TRY(create(child_x_count, child_y_count, level - 1));
  802. }
  803. }
  804. return node;
  805. }
  806. };
  807. TagTree::TagTree(NonnullOwnPtr<TagTreeNode> root)
  808. : m_root(move(root))
  809. {
  810. }
  811. TagTree::TagTree(TagTree&&) = default;
  812. TagTree::~TagTree() = default;
  813. ErrorOr<TagTree> TagTree::create(u32 x_count, u32 y_count)
  814. {
  815. auto level = ceil(log2(max(x_count, y_count)));
  816. return TagTree { TRY(TagTreeNode::create(x_count, y_count, level)) };
  817. }
  818. ErrorOr<u32> TagTree::read_value(u32 x, u32 y, Function<ErrorOr<bool>()> const& read_bit, Optional<u32> stop_at) const
  819. {
  820. return m_root->read_value(x, y, read_bit, m_root->value, stop_at);
  821. }
  822. }
  823. bool JPEG2000ImageDecoderPlugin::sniff(ReadonlyBytes data)
  824. {
  825. return data.starts_with(jp2_id_string) || data.starts_with(marker_id_string);
  826. }
  827. JPEG2000ImageDecoderPlugin::JPEG2000ImageDecoderPlugin()
  828. {
  829. m_context = make<JPEG2000LoadingContext>();
  830. }
  831. JPEG2000ImageDecoderPlugin::~JPEG2000ImageDecoderPlugin() = default;
  832. IntSize JPEG2000ImageDecoderPlugin::size()
  833. {
  834. return m_context->size;
  835. }
  836. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEG2000ImageDecoderPlugin::create(ReadonlyBytes data)
  837. {
  838. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEG2000ImageDecoderPlugin()));
  839. TRY(decode_jpeg2000_header(*plugin->m_context, data));
  840. return plugin;
  841. }
  842. ErrorOr<ImageFrameDescriptor> JPEG2000ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  843. {
  844. if (index != 0)
  845. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Invalid frame index");
  846. if (m_context->state == JPEG2000LoadingContext::State::Error)
  847. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Decoding failed");
  848. if (m_context->state < JPEG2000LoadingContext::State::DecodedTileHeaders) {
  849. TRY(parse_codestream_tile_headers(*m_context));
  850. m_context->state = JPEG2000LoadingContext::State::DecodedTileHeaders;
  851. }
  852. return Error::from_string_literal("JPEG2000ImageDecoderPlugin: Draw the rest of the owl");
  853. }
  854. ErrorOr<Optional<ReadonlyBytes>> JPEG2000ImageDecoderPlugin::icc_data()
  855. {
  856. return m_context->icc_data;
  857. }
  858. }