JPEG2000Loader.cpp 40 KB

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