JBIG2Loader.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 <LibGfx/ImageFormats/JBIG2Loader.h>
  8. // Spec: ITU-T_T_88__08_2018.pdf in the zip file here:
  9. // https://www.itu.int/rec/T-REC-T.88-201808-I
  10. // Annex H has a datastream example.
  11. namespace Gfx {
  12. // JBIG2 spec, Annex D, D.4.1 ID string
  13. static constexpr u8 id_string[] = { 0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A };
  14. // 7.3 Segment types
  15. enum SegmentType {
  16. SymbolDictionary = 0,
  17. IntermediateTextRegion = 4,
  18. ImmediateTextRegion = 6,
  19. ImmediateLosslessTextRegion = 7,
  20. PatternDictionary = 16,
  21. IntermediateHalftoneRegion = 20,
  22. ImmediateHalftoneRegion = 22,
  23. ImmediateLosslessHalftoneRegion = 23,
  24. IntermediateGenericRegion = 36,
  25. ImmediateGenericRegion = 38,
  26. ImmediateLosslessGenericRegion = 39,
  27. IntermediateGenericRefinementRegion = 40,
  28. ImmediateGenericRefinementRegion = 42,
  29. ImmediateLosslessGenericRefinementRegion = 43,
  30. PageInformation = 48,
  31. EndOfPage = 49,
  32. EndOfStripe = 50,
  33. EndOfFile = 51,
  34. Profiles = 52,
  35. Tables = 53,
  36. ColorPalette = 54,
  37. Extension = 62,
  38. };
  39. // Annex D
  40. enum class Organization {
  41. // D.1 Sequential organization
  42. Sequential,
  43. // D.2 Random-access organization
  44. RandomAccess,
  45. // D.3 Embedded organization
  46. Embedded,
  47. };
  48. struct SegmentHeader {
  49. u32 segment_number;
  50. SegmentType type;
  51. Vector<u32> referred_to_segment_numbers;
  52. // 7.2.6 Segment page association
  53. // "The first page must be numbered "1". This field may contain a value of zero; this value indicates that this segment is not associated with any page."
  54. u32 page_association;
  55. Optional<u32> data_length;
  56. };
  57. struct SegmentData {
  58. SegmentHeader header;
  59. ReadonlyBytes data;
  60. };
  61. struct JBIG2LoadingContext {
  62. enum class State {
  63. NotDecoded = 0,
  64. Error,
  65. Decoded,
  66. };
  67. State state { State::NotDecoded };
  68. Organization organization { Organization::Sequential };
  69. IntSize size;
  70. Optional<u32> number_of_pages;
  71. Vector<SegmentData> segments;
  72. };
  73. static ErrorOr<void> decode_jbig2_header(JBIG2LoadingContext& context, ReadonlyBytes data)
  74. {
  75. if (!JBIG2ImageDecoderPlugin::sniff(data))
  76. return Error::from_string_literal("JBIG2LoadingContext: Invalid JBIG2 header");
  77. FixedMemoryStream stream(data.slice(sizeof(id_string)));
  78. // D.4.2 File header flags
  79. u8 header_flags = TRY(stream.read_value<u8>());
  80. if (header_flags & 0b11110000)
  81. return Error::from_string_literal("JBIG2LoadingContext: Invalid header flags");
  82. context.organization = (header_flags & 1) ? Organization::Sequential : Organization::RandomAccess;
  83. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Organization: {} ({})", (int)context.organization, context.organization == Organization::Sequential ? "Sequential" : "Random-access");
  84. bool has_known_number_of_pages = (header_flags & 2) ? false : true;
  85. bool uses_templates_with_12_AT_pixels = (header_flags & 4) ? true : false;
  86. bool contains_colored_region_segments = (header_flags & 8) ? true : false;
  87. // FIXME: Do something with these?
  88. (void)uses_templates_with_12_AT_pixels;
  89. (void)contains_colored_region_segments;
  90. // D.4.3 Number of pages
  91. if (has_known_number_of_pages) {
  92. context.number_of_pages = TRY(stream.read_value<BigEndian<u32>>());
  93. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Number of pages: {}", context.number_of_pages.value());
  94. }
  95. return {};
  96. }
  97. static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
  98. {
  99. // 7.2.2 Segment number
  100. u32 segment_number = TRY(stream.read_value<BigEndian<u32>>());
  101. dbgln_if(JBIG2_DEBUG, "Segment number: {}", segment_number);
  102. // 7.2.3 Segment header flags
  103. u8 flags = TRY(stream.read_value<u8>());
  104. SegmentType type = static_cast<SegmentType>(flags & 0b11'1111);
  105. dbgln_if(JBIG2_DEBUG, "Segment type: {}", (int)type);
  106. bool segment_page_association_size_is_32_bits = (flags & 0b100'0000) != 0;
  107. bool segment_retained_only_by_itself_and_extension_segments = (flags & 0b1000'00000) != 0;
  108. // FIXME: Do something with these.
  109. (void)segment_page_association_size_is_32_bits;
  110. (void)segment_retained_only_by_itself_and_extension_segments;
  111. // 7.2.4 Referred-to segment count and retention flags
  112. u8 referred_to_segment_count_and_retention_flags = TRY(stream.read_value<u8>());
  113. u32 count_of_referred_to_segments = referred_to_segment_count_and_retention_flags >> 5;
  114. if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
  115. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid count_of_referred_to_segments");
  116. u32 extra_count = 0;
  117. if (count_of_referred_to_segments == 7) {
  118. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  119. count_of_referred_to_segments = TRY(stream.read_value<BigEndian<u32>>()) & 0x1FFF'FFFF;
  120. extra_count = ceil_div(count_of_referred_to_segments + 1, 8);
  121. TRY(stream.seek(extra_count, SeekMode::FromCurrentPosition));
  122. }
  123. dbgln_if(JBIG2_DEBUG, "Referred-to segment count: {}", count_of_referred_to_segments);
  124. // 7.2.5 Referred-to segment numbers
  125. Vector<u32> referred_to_segment_numbers;
  126. for (u32 i = 0; i < count_of_referred_to_segments; ++i) {
  127. u32 referred_to_segment_number;
  128. if (segment_number <= 256)
  129. referred_to_segment_number = TRY(stream.read_value<u8>());
  130. else if (segment_number <= 65536)
  131. referred_to_segment_number = TRY(stream.read_value<BigEndian<u16>>());
  132. else
  133. referred_to_segment_number = TRY(stream.read_value<BigEndian<u32>>());
  134. referred_to_segment_numbers.append(referred_to_segment_number);
  135. dbgln_if(JBIG2_DEBUG, "Referred-to segment number: {}", referred_to_segment_number);
  136. }
  137. // 7.2.6 Segment page association
  138. u32 segment_page_association;
  139. if (segment_page_association_size_is_32_bits) {
  140. segment_page_association = TRY(stream.read_value<BigEndian<u32>>());
  141. } else {
  142. segment_page_association = TRY(stream.read_value<u8>());
  143. }
  144. dbgln_if(JBIG2_DEBUG, "Segment page association: {}", segment_page_association);
  145. // 7.2.7 Segment data length
  146. u32 data_length = TRY(stream.read_value<BigEndian<u32>>());
  147. dbgln_if(JBIG2_DEBUG, "Segment data length: {}", data_length);
  148. // FIXME: Add some validity checks:
  149. // - check type is valid
  150. // - check referred_to_segment_numbers are smaller than segment_number
  151. // - 7.3.1 Rules for segment references
  152. // - 7.3.2 Rules for page associations
  153. Optional<u32> opt_data_length;
  154. if (data_length != 0xffff'ffff)
  155. opt_data_length = data_length;
  156. else if (type != ImmediateGenericRegion)
  157. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown data length only allowed for ImmediateGenericRegion");
  158. return SegmentHeader { segment_number, type, move(referred_to_segment_numbers), segment_page_association, opt_data_length };
  159. }
  160. static ErrorOr<size_t> scan_for_immediate_generic_region_size(ReadonlyBytes data)
  161. {
  162. // 7.2.7 Segment data length
  163. // "If the segment's type is "Immediate generic region", then the length field may contain the value 0xFFFFFFFF.
  164. // This value is intended to mean that the length of the segment's data part is unknown at the time that the segment header is written (...).
  165. // In this case, the true length of the segment's data part shall be determined through examination of the data:
  166. // if the segment uses template-based arithmetic coding, then the segment's data part ends with the two-byte sequence 0xFF 0xAC followed by a four-byte row count.
  167. // If the segment uses MMR coding, then the segment's data part ends with the two-byte sequence 0x00 0x00 followed by a four-byte row count.
  168. // The form of encoding used by the segment may be determined by examining the eighteenth byte of its segment data part,
  169. // and the end sequences can occur anywhere after that eighteenth byte."
  170. // 7.4.6.4 Decoding a generic region segment
  171. // "NOTE – The sequence 0x00 0x00 cannot occur within MMR-encoded data; the sequence 0xFF 0xAC can occur only at the end of arithmetically-coded data.
  172. // Thus, those sequences cannot occur by chance in the data that is decoded to generate the contents of the generic region."
  173. dbgln_if(JBIG2_DEBUG, "(Unknown data length, computing it)");
  174. if (data.size() < 18)
  175. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Data too short to contain segment data header");
  176. // Per 7.4.6.1 Generic region segment data header, this starts with the 17 bytes described in
  177. // 7.4.1 Region segment information field, followed the byte described in 7.4.6.2 Generic region segment flags.
  178. // That byte's lowest bit stores if the segment uses MMR.
  179. u8 flags = data[17];
  180. bool uses_mmr = (flags & 1) != 0;
  181. auto end_sequence = uses_mmr ? to_array<u8>({ 0x00, 0x00 }) : to_array<u8>({ 0xFF, 0xAC });
  182. u8 const* end = static_cast<u8 const*>(memmem(data.data() + 19, data.size() - 19 - sizeof(u32), end_sequence.data(), end_sequence.size()));
  183. if (!end)
  184. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Could not find end sequence in segment data");
  185. size_t size = end - data.data() + end_sequence.size() + sizeof(u32);
  186. dbgln_if(JBIG2_DEBUG, "(Computed size is {})", size);
  187. return size;
  188. }
  189. static ErrorOr<void> decode_segment_headers(JBIG2LoadingContext& context, ReadonlyBytes data)
  190. {
  191. FixedMemoryStream stream(data);
  192. Vector<ReadonlyBytes> segment_datas;
  193. auto store_and_skip_segment_data = [&](SegmentHeader const& segment_header) -> ErrorOr<void> {
  194. size_t start_offset = TRY(stream.tell());
  195. u32 data_length = TRY(segment_header.data_length.try_value_or_lazy_evaluated([&]() {
  196. return scan_for_immediate_generic_region_size(data.slice(start_offset));
  197. }));
  198. if (start_offset + data_length > data.size()) {
  199. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: start_offset={}, data_length={}, data.size()={}", start_offset, data_length, data.size());
  200. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment data length exceeds file size");
  201. }
  202. ReadonlyBytes segment_data = data.slice(start_offset, data_length);
  203. segment_datas.append(segment_data);
  204. TRY(stream.seek(data_length, SeekMode::FromCurrentPosition));
  205. return {};
  206. };
  207. Vector<SegmentHeader> segment_headers;
  208. while (!stream.is_eof()) {
  209. auto segment_header = TRY(decode_segment_header(stream));
  210. segment_headers.append(segment_header);
  211. if (context.organization != Organization::RandomAccess)
  212. TRY(store_and_skip_segment_data(segment_header));
  213. // Required per spec for files with RandomAccess organization.
  214. if (segment_header.type == SegmentType::EndOfFile)
  215. break;
  216. }
  217. if (context.organization == Organization::RandomAccess) {
  218. for (auto const& segment_header : segment_headers)
  219. TRY(store_and_skip_segment_data(segment_header));
  220. }
  221. if (segment_headers.size() != segment_datas.size())
  222. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment headers and segment datas have different sizes");
  223. for (size_t i = 0; i < segment_headers.size(); ++i)
  224. context.segments.append({ segment_headers[i], segment_datas[i] });
  225. return {};
  226. }
  227. // 7.4.8 Page information segment syntax
  228. struct [[gnu::packed]] PageInformationSegment {
  229. BigEndian<u32> bitmap_width;
  230. BigEndian<u32> bitmap_height;
  231. BigEndian<u32> page_x_resolution; // In pixels/meter.
  232. BigEndian<u32> page_y_resolution; // In pixels/meter.
  233. u8 flags;
  234. BigEndian<u16> striping_information;
  235. };
  236. static_assert(AssertSize<PageInformationSegment, 19>());
  237. static ErrorOr<PageInformationSegment> decode_page_information_segment(ReadonlyBytes data)
  238. {
  239. // 7.4.8 Page information segment syntax
  240. if (data.size() != sizeof(PageInformationSegment))
  241. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid page information segment size");
  242. return *(PageInformationSegment const*)data.data();
  243. }
  244. static ErrorOr<void> scan_for_page_size(JBIG2LoadingContext& context)
  245. {
  246. // We only decode the first page at the moment.
  247. for (auto const& segment : context.segments) {
  248. if (segment.header.type != SegmentType::PageInformation || segment.header.page_association != 1)
  249. continue;
  250. auto page_information = TRY(decode_page_information_segment(segment.data));
  251. // FIXME: We're supposed to compute this from the striping information if it's not set.
  252. if (page_information.bitmap_height == 0xffff'ffff)
  253. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle unknown page height yet");
  254. context.size = { page_information.bitmap_width, page_information.bitmap_height };
  255. return {};
  256. }
  257. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No page information segment found for page 1");
  258. }
  259. static ErrorOr<void> warn_about_multiple_pages(JBIG2LoadingContext& context)
  260. {
  261. HashTable<u32> seen_pages;
  262. Vector<u32> pages;
  263. for (auto const& segment : context.segments) {
  264. if (segment.header.page_association == 0)
  265. continue;
  266. if (seen_pages.contains(segment.header.page_association))
  267. continue;
  268. seen_pages.set(segment.header.page_association);
  269. pages.append(segment.header.page_association);
  270. }
  271. // scan_for_page_size() already checked that there's a page 1.
  272. VERIFY(seen_pages.contains(1));
  273. if (pages.size() == 1)
  274. return {};
  275. StringBuilder builder;
  276. builder.appendff("JBIG2 file contains {} pages ({}", pages.size(), pages[0]);
  277. size_t i;
  278. for (i = 1; i < min(pages.size(), 10); ++i)
  279. builder.appendff(" {}", pages[i]);
  280. if (i != pages.size())
  281. builder.append(" ..."sv);
  282. builder.append("). We will only render page 1."sv);
  283. dbgln("JBIG2ImageDecoderPlugin: {}", TRY(builder.to_string()));
  284. return {};
  285. }
  286. static ErrorOr<void> decode_symbol_dictionary(JBIG2LoadingContext&, SegmentData const&)
  287. {
  288. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode symbol dictionary yet");
  289. }
  290. static ErrorOr<void> decode_intermediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  291. {
  292. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate text region yet");
  293. }
  294. static ErrorOr<void> decode_immediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  295. {
  296. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate text region yet");
  297. }
  298. static ErrorOr<void> decode_immediate_lossless_text_region(JBIG2LoadingContext&, SegmentData const&)
  299. {
  300. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless text region yet");
  301. }
  302. static ErrorOr<void> decode_pattern_dictionary(JBIG2LoadingContext&, SegmentData const&)
  303. {
  304. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode pattern dictionary yet");
  305. }
  306. static ErrorOr<void> decode_intermediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  307. {
  308. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate halftone region yet");
  309. }
  310. static ErrorOr<void> decode_immediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  311. {
  312. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate halftone region yet");
  313. }
  314. static ErrorOr<void> decode_immediate_lossless_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  315. {
  316. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless halftone region yet");
  317. }
  318. static ErrorOr<void> decode_intermediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  319. {
  320. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic region yet");
  321. }
  322. static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  323. {
  324. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic region yet");
  325. }
  326. static ErrorOr<void> decode_immediate_lossless_generic_region(JBIG2LoadingContext&, SegmentData const&)
  327. {
  328. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic region yet");
  329. }
  330. static ErrorOr<void> decode_intermediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  331. {
  332. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic refinement region yet");
  333. }
  334. static ErrorOr<void> decode_immediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  335. {
  336. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic refinement region yet");
  337. }
  338. static ErrorOr<void> decode_immediate_lossless_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  339. {
  340. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic refinement region yet");
  341. }
  342. static ErrorOr<void> decode_page_information(JBIG2LoadingContext&, SegmentData const&)
  343. {
  344. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode page information yet");
  345. }
  346. static ErrorOr<void> decode_end_of_page(JBIG2LoadingContext&, SegmentData const&)
  347. {
  348. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of page yet");
  349. }
  350. static ErrorOr<void> decode_end_of_stripe(JBIG2LoadingContext&, SegmentData const&)
  351. {
  352. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of stripe yet");
  353. }
  354. static ErrorOr<void> decode_end_of_file(JBIG2LoadingContext&, SegmentData const&)
  355. {
  356. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of file yet");
  357. }
  358. static ErrorOr<void> decode_profiles(JBIG2LoadingContext&, SegmentData const&)
  359. {
  360. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode profiles yet");
  361. }
  362. static ErrorOr<void> decode_tables(JBIG2LoadingContext&, SegmentData const&)
  363. {
  364. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode tables yet");
  365. }
  366. static ErrorOr<void> decode_color_palette(JBIG2LoadingContext&, SegmentData const&)
  367. {
  368. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode color palette yet");
  369. }
  370. static ErrorOr<void> decode_extension(JBIG2LoadingContext&, SegmentData const&)
  371. {
  372. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode extension yet");
  373. }
  374. static ErrorOr<void> decode_data(JBIG2LoadingContext& context)
  375. {
  376. TRY(warn_about_multiple_pages(context));
  377. for (auto const& segment : context.segments) {
  378. if (segment.header.page_association != 0 && segment.header.page_association != 1)
  379. continue;
  380. switch (segment.header.type) {
  381. case SegmentType::SymbolDictionary:
  382. TRY(decode_symbol_dictionary(context, segment));
  383. break;
  384. case SegmentType::IntermediateTextRegion:
  385. TRY(decode_intermediate_text_region(context, segment));
  386. break;
  387. case SegmentType::ImmediateTextRegion:
  388. TRY(decode_immediate_text_region(context, segment));
  389. break;
  390. case SegmentType::ImmediateLosslessTextRegion:
  391. TRY(decode_immediate_lossless_text_region(context, segment));
  392. break;
  393. case SegmentType::PatternDictionary:
  394. TRY(decode_pattern_dictionary(context, segment));
  395. break;
  396. case SegmentType::IntermediateHalftoneRegion:
  397. TRY(decode_intermediate_halftone_region(context, segment));
  398. break;
  399. case SegmentType::ImmediateHalftoneRegion:
  400. TRY(decode_immediate_halftone_region(context, segment));
  401. break;
  402. case SegmentType::ImmediateLosslessHalftoneRegion:
  403. TRY(decode_immediate_lossless_halftone_region(context, segment));
  404. break;
  405. case SegmentType::IntermediateGenericRegion:
  406. TRY(decode_intermediate_generic_region(context, segment));
  407. break;
  408. case SegmentType::ImmediateGenericRegion:
  409. TRY(decode_immediate_generic_region(context, segment));
  410. break;
  411. case SegmentType::ImmediateLosslessGenericRegion:
  412. TRY(decode_immediate_lossless_generic_region(context, segment));
  413. break;
  414. case SegmentType::IntermediateGenericRefinementRegion:
  415. TRY(decode_intermediate_generic_refinement_region(context, segment));
  416. break;
  417. case SegmentType::ImmediateGenericRefinementRegion:
  418. TRY(decode_immediate_generic_refinement_region(context, segment));
  419. break;
  420. case SegmentType::ImmediateLosslessGenericRefinementRegion:
  421. TRY(decode_immediate_lossless_generic_refinement_region(context, segment));
  422. break;
  423. case SegmentType::PageInformation:
  424. TRY(decode_page_information(context, segment));
  425. break;
  426. case SegmentType::EndOfPage:
  427. TRY(decode_end_of_page(context, segment));
  428. break;
  429. case SegmentType::EndOfStripe:
  430. TRY(decode_end_of_stripe(context, segment));
  431. break;
  432. case SegmentType::EndOfFile:
  433. TRY(decode_end_of_file(context, segment));
  434. break;
  435. case SegmentType::Profiles:
  436. TRY(decode_profiles(context, segment));
  437. break;
  438. case SegmentType::Tables:
  439. TRY(decode_tables(context, segment));
  440. break;
  441. case SegmentType::ColorPalette:
  442. TRY(decode_color_palette(context, segment));
  443. break;
  444. case SegmentType::Extension:
  445. TRY(decode_extension(context, segment));
  446. break;
  447. }
  448. }
  449. return {};
  450. }
  451. JBIG2ImageDecoderPlugin::JBIG2ImageDecoderPlugin()
  452. {
  453. m_context = make<JBIG2LoadingContext>();
  454. }
  455. IntSize JBIG2ImageDecoderPlugin::size()
  456. {
  457. return m_context->size;
  458. }
  459. bool JBIG2ImageDecoderPlugin::sniff(ReadonlyBytes data)
  460. {
  461. return data.starts_with(id_string);
  462. }
  463. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JBIG2ImageDecoderPlugin::create(ReadonlyBytes data)
  464. {
  465. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  466. TRY(decode_jbig2_header(*plugin->m_context, data));
  467. data = data.slice(sizeof(id_string) + sizeof(u8) + (plugin->m_context->number_of_pages.has_value() ? sizeof(u32) : 0));
  468. TRY(decode_segment_headers(*plugin->m_context, data));
  469. TRY(scan_for_page_size(*plugin->m_context));
  470. return plugin;
  471. }
  472. ErrorOr<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  473. {
  474. // FIXME: Use this for multi-page JBIG2 files?
  475. if (index != 0)
  476. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid frame index");
  477. if (m_context->state == JBIG2LoadingContext::State::Error)
  478. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoding failed");
  479. if (m_context->state < JBIG2LoadingContext::State::Decoded) {
  480. auto result = decode_data(*m_context);
  481. if (result.is_error()) {
  482. m_context->state = JBIG2LoadingContext::State::Error;
  483. return result.release_error();
  484. }
  485. m_context->state = JBIG2LoadingContext::State::Decoded;
  486. }
  487. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Draw the rest of the owl");
  488. }
  489. ErrorOr<ByteBuffer> JBIG2ImageDecoderPlugin::decode_embedded(Vector<ReadonlyBytes> data)
  490. {
  491. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  492. plugin->m_context->organization = Organization::Embedded;
  493. for (auto const& segment_data : data)
  494. TRY(decode_segment_headers(*plugin->m_context, segment_data));
  495. TRY(scan_for_page_size(*plugin->m_context));
  496. TRY(decode_data(*plugin->m_context));
  497. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode embedded JBIG2 yet");
  498. }
  499. }