JBIG2Loader.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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/Utf16View.h>
  8. #include <LibGfx/ImageFormats/JBIG2Loader.h>
  9. #include <LibTextCodec/Decoder.h>
  10. // Spec: ITU-T_T_88__08_2018.pdf in the zip file here:
  11. // https://www.itu.int/rec/T-REC-T.88-201808-I
  12. // Annex H has a datastream example.
  13. namespace Gfx {
  14. // JBIG2 spec, Annex D, D.4.1 ID string
  15. static constexpr u8 id_string[] = { 0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A };
  16. // 7.3 Segment types
  17. enum SegmentType {
  18. SymbolDictionary = 0,
  19. IntermediateTextRegion = 4,
  20. ImmediateTextRegion = 6,
  21. ImmediateLosslessTextRegion = 7,
  22. PatternDictionary = 16,
  23. IntermediateHalftoneRegion = 20,
  24. ImmediateHalftoneRegion = 22,
  25. ImmediateLosslessHalftoneRegion = 23,
  26. IntermediateGenericRegion = 36,
  27. ImmediateGenericRegion = 38,
  28. ImmediateLosslessGenericRegion = 39,
  29. IntermediateGenericRefinementRegion = 40,
  30. ImmediateGenericRefinementRegion = 42,
  31. ImmediateLosslessGenericRefinementRegion = 43,
  32. PageInformation = 48,
  33. EndOfPage = 49,
  34. EndOfStripe = 50,
  35. EndOfFile = 51,
  36. Profiles = 52,
  37. Tables = 53,
  38. ColorPalette = 54,
  39. Extension = 62,
  40. };
  41. // Annex D
  42. enum class Organization {
  43. // D.1 Sequential organization
  44. Sequential,
  45. // D.2 Random-access organization
  46. RandomAccess,
  47. // D.3 Embedded organization
  48. Embedded,
  49. };
  50. struct SegmentHeader {
  51. u32 segment_number;
  52. SegmentType type;
  53. Vector<u32> referred_to_segment_numbers;
  54. // 7.2.6 Segment page association
  55. // "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."
  56. u32 page_association;
  57. Optional<u32> data_length;
  58. };
  59. struct SegmentData {
  60. SegmentHeader header;
  61. ReadonlyBytes data;
  62. };
  63. class BitBuffer {
  64. public:
  65. static ErrorOr<NonnullOwnPtr<BitBuffer>> create(size_t width, size_t height);
  66. bool get_bit(size_t x, size_t y) const;
  67. void set_bit(size_t x, size_t y, bool b);
  68. void fill(bool b);
  69. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_gfx_bitmap() const;
  70. ErrorOr<ByteBuffer> to_byte_buffer() const;
  71. private:
  72. BitBuffer(ByteBuffer, size_t width, size_t height, size_t pitch);
  73. ByteBuffer m_bits;
  74. size_t m_width;
  75. size_t m_height;
  76. size_t m_pitch;
  77. };
  78. ErrorOr<NonnullOwnPtr<BitBuffer>> BitBuffer::create(size_t width, size_t height)
  79. {
  80. size_t pitch = ceil_div(width, 8ull);
  81. auto bits = TRY(ByteBuffer::create_uninitialized(pitch * height));
  82. return adopt_nonnull_own_or_enomem(new (nothrow) BitBuffer(move(bits), width, height, pitch));
  83. }
  84. bool BitBuffer::get_bit(size_t x, size_t y) const
  85. {
  86. VERIFY(x < m_width);
  87. VERIFY(y < m_height);
  88. size_t byte_offset = x / 8;
  89. size_t bit_offset = x % 8;
  90. u8 byte = m_bits[y * m_pitch + byte_offset];
  91. byte = (byte >> (8 - 1 - bit_offset)) & 1;
  92. return byte != 0;
  93. }
  94. void BitBuffer::set_bit(size_t x, size_t y, bool b)
  95. {
  96. VERIFY(x < m_width);
  97. VERIFY(y < m_height);
  98. size_t byte_offset = x / 8;
  99. size_t bit_offset = x % 8;
  100. u8 byte = m_bits[y * m_pitch + byte_offset];
  101. u8 mask = 1u << (8 - 1 - bit_offset);
  102. if (b)
  103. byte |= mask;
  104. else
  105. byte &= ~mask;
  106. m_bits[y * m_pitch + byte_offset] = byte;
  107. }
  108. void BitBuffer::fill(bool b)
  109. {
  110. u8 fill_byte = b ? 0xff : 0;
  111. for (auto& byte : m_bits.bytes())
  112. byte = fill_byte;
  113. }
  114. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> BitBuffer::to_gfx_bitmap() const
  115. {
  116. auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { m_width, m_height }));
  117. for (size_t y = 0; y < m_height; ++y) {
  118. for (size_t x = 0; x < m_width; ++x) {
  119. auto color = get_bit(x, y) ? Color::Black : Color::White;
  120. bitmap->set_pixel(x, y, color);
  121. }
  122. }
  123. return bitmap;
  124. }
  125. ErrorOr<ByteBuffer> BitBuffer::to_byte_buffer() const
  126. {
  127. return ByteBuffer::copy(m_bits);
  128. }
  129. BitBuffer::BitBuffer(ByteBuffer bits, size_t width, size_t height, size_t pitch)
  130. : m_bits(move(bits))
  131. , m_width(width)
  132. , m_height(height)
  133. , m_pitch(pitch)
  134. {
  135. }
  136. // 7.4.8.5 Page segment flags
  137. enum class CombinationOperator {
  138. Or = 0,
  139. And = 1,
  140. Xor = 2,
  141. XNor = 3,
  142. };
  143. struct Page {
  144. IntSize size;
  145. CombinationOperator default_combination_operator;
  146. OwnPtr<BitBuffer> bits;
  147. };
  148. struct JBIG2LoadingContext {
  149. enum class State {
  150. NotDecoded = 0,
  151. Error,
  152. Decoded,
  153. };
  154. State state { State::NotDecoded };
  155. Organization organization { Organization::Sequential };
  156. Page page;
  157. Optional<u32> number_of_pages;
  158. Vector<SegmentData> segments;
  159. };
  160. static ErrorOr<void> decode_jbig2_header(JBIG2LoadingContext& context, ReadonlyBytes data)
  161. {
  162. if (!JBIG2ImageDecoderPlugin::sniff(data))
  163. return Error::from_string_literal("JBIG2LoadingContext: Invalid JBIG2 header");
  164. FixedMemoryStream stream(data.slice(sizeof(id_string)));
  165. // D.4.2 File header flags
  166. u8 header_flags = TRY(stream.read_value<u8>());
  167. if (header_flags & 0b11110000)
  168. return Error::from_string_literal("JBIG2LoadingContext: Invalid header flags");
  169. context.organization = (header_flags & 1) ? Organization::Sequential : Organization::RandomAccess;
  170. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Organization: {} ({})", (int)context.organization, context.organization == Organization::Sequential ? "Sequential" : "Random-access");
  171. bool has_known_number_of_pages = (header_flags & 2) ? false : true;
  172. bool uses_templates_with_12_AT_pixels = (header_flags & 4) ? true : false;
  173. bool contains_colored_region_segments = (header_flags & 8) ? true : false;
  174. // FIXME: Do something with these?
  175. (void)uses_templates_with_12_AT_pixels;
  176. (void)contains_colored_region_segments;
  177. // D.4.3 Number of pages
  178. if (has_known_number_of_pages) {
  179. context.number_of_pages = TRY(stream.read_value<BigEndian<u32>>());
  180. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Number of pages: {}", context.number_of_pages.value());
  181. }
  182. return {};
  183. }
  184. static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
  185. {
  186. // 7.2.2 Segment number
  187. u32 segment_number = TRY(stream.read_value<BigEndian<u32>>());
  188. dbgln_if(JBIG2_DEBUG, "Segment number: {}", segment_number);
  189. // 7.2.3 Segment header flags
  190. u8 flags = TRY(stream.read_value<u8>());
  191. SegmentType type = static_cast<SegmentType>(flags & 0b11'1111);
  192. dbgln_if(JBIG2_DEBUG, "Segment type: {}", (int)type);
  193. bool segment_page_association_size_is_32_bits = (flags & 0b100'0000) != 0;
  194. bool segment_retained_only_by_itself_and_extension_segments = (flags & 0b1000'00000) != 0;
  195. // FIXME: Do something with these.
  196. (void)segment_page_association_size_is_32_bits;
  197. (void)segment_retained_only_by_itself_and_extension_segments;
  198. // 7.2.4 Referred-to segment count and retention flags
  199. u8 referred_to_segment_count_and_retention_flags = TRY(stream.read_value<u8>());
  200. u32 count_of_referred_to_segments = referred_to_segment_count_and_retention_flags >> 5;
  201. if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
  202. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid count_of_referred_to_segments");
  203. u32 extra_count = 0;
  204. if (count_of_referred_to_segments == 7) {
  205. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  206. count_of_referred_to_segments = TRY(stream.read_value<BigEndian<u32>>()) & 0x1FFF'FFFF;
  207. extra_count = ceil_div(count_of_referred_to_segments + 1, 8);
  208. TRY(stream.seek(extra_count, SeekMode::FromCurrentPosition));
  209. }
  210. dbgln_if(JBIG2_DEBUG, "Referred-to segment count: {}", count_of_referred_to_segments);
  211. // 7.2.5 Referred-to segment numbers
  212. Vector<u32> referred_to_segment_numbers;
  213. for (u32 i = 0; i < count_of_referred_to_segments; ++i) {
  214. u32 referred_to_segment_number;
  215. if (segment_number <= 256)
  216. referred_to_segment_number = TRY(stream.read_value<u8>());
  217. else if (segment_number <= 65536)
  218. referred_to_segment_number = TRY(stream.read_value<BigEndian<u16>>());
  219. else
  220. referred_to_segment_number = TRY(stream.read_value<BigEndian<u32>>());
  221. referred_to_segment_numbers.append(referred_to_segment_number);
  222. dbgln_if(JBIG2_DEBUG, "Referred-to segment number: {}", referred_to_segment_number);
  223. }
  224. // 7.2.6 Segment page association
  225. u32 segment_page_association;
  226. if (segment_page_association_size_is_32_bits) {
  227. segment_page_association = TRY(stream.read_value<BigEndian<u32>>());
  228. } else {
  229. segment_page_association = TRY(stream.read_value<u8>());
  230. }
  231. dbgln_if(JBIG2_DEBUG, "Segment page association: {}", segment_page_association);
  232. // 7.2.7 Segment data length
  233. u32 data_length = TRY(stream.read_value<BigEndian<u32>>());
  234. dbgln_if(JBIG2_DEBUG, "Segment data length: {}", data_length);
  235. // FIXME: Add some validity checks:
  236. // - check type is valid
  237. // - check referred_to_segment_numbers are smaller than segment_number
  238. // - 7.3.1 Rules for segment references
  239. // - 7.3.2 Rules for page associations
  240. Optional<u32> opt_data_length;
  241. if (data_length != 0xffff'ffff)
  242. opt_data_length = data_length;
  243. else if (type != ImmediateGenericRegion)
  244. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown data length only allowed for ImmediateGenericRegion");
  245. return SegmentHeader { segment_number, type, move(referred_to_segment_numbers), segment_page_association, opt_data_length };
  246. }
  247. static ErrorOr<size_t> scan_for_immediate_generic_region_size(ReadonlyBytes data)
  248. {
  249. // 7.2.7 Segment data length
  250. // "If the segment's type is "Immediate generic region", then the length field may contain the value 0xFFFFFFFF.
  251. // 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 (...).
  252. // In this case, the true length of the segment's data part shall be determined through examination of the data:
  253. // 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.
  254. // 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.
  255. // The form of encoding used by the segment may be determined by examining the eighteenth byte of its segment data part,
  256. // and the end sequences can occur anywhere after that eighteenth byte."
  257. // 7.4.6.4 Decoding a generic region segment
  258. // "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.
  259. // Thus, those sequences cannot occur by chance in the data that is decoded to generate the contents of the generic region."
  260. dbgln_if(JBIG2_DEBUG, "(Unknown data length, computing it)");
  261. if (data.size() < 18)
  262. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Data too short to contain segment data header");
  263. // Per 7.4.6.1 Generic region segment data header, this starts with the 17 bytes described in
  264. // 7.4.1 Region segment information field, followed the byte described in 7.4.6.2 Generic region segment flags.
  265. // That byte's lowest bit stores if the segment uses MMR.
  266. u8 flags = data[17];
  267. bool uses_mmr = (flags & 1) != 0;
  268. auto end_sequence = uses_mmr ? to_array<u8>({ 0x00, 0x00 }) : to_array<u8>({ 0xFF, 0xAC });
  269. u8 const* end = static_cast<u8 const*>(memmem(data.data() + 19, data.size() - 19 - sizeof(u32), end_sequence.data(), end_sequence.size()));
  270. if (!end)
  271. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Could not find end sequence in segment data");
  272. size_t size = end - data.data() + end_sequence.size() + sizeof(u32);
  273. dbgln_if(JBIG2_DEBUG, "(Computed size is {})", size);
  274. return size;
  275. }
  276. static ErrorOr<void> decode_segment_headers(JBIG2LoadingContext& context, ReadonlyBytes data)
  277. {
  278. FixedMemoryStream stream(data);
  279. Vector<ReadonlyBytes> segment_datas;
  280. auto store_and_skip_segment_data = [&](SegmentHeader const& segment_header) -> ErrorOr<void> {
  281. size_t start_offset = TRY(stream.tell());
  282. u32 data_length = TRY(segment_header.data_length.try_value_or_lazy_evaluated([&]() {
  283. return scan_for_immediate_generic_region_size(data.slice(start_offset));
  284. }));
  285. if (start_offset + data_length > data.size()) {
  286. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: start_offset={}, data_length={}, data.size()={}", start_offset, data_length, data.size());
  287. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment data length exceeds file size");
  288. }
  289. ReadonlyBytes segment_data = data.slice(start_offset, data_length);
  290. segment_datas.append(segment_data);
  291. TRY(stream.seek(data_length, SeekMode::FromCurrentPosition));
  292. return {};
  293. };
  294. Vector<SegmentHeader> segment_headers;
  295. while (!stream.is_eof()) {
  296. auto segment_header = TRY(decode_segment_header(stream));
  297. segment_headers.append(segment_header);
  298. if (context.organization != Organization::RandomAccess)
  299. TRY(store_and_skip_segment_data(segment_header));
  300. // Required per spec for files with RandomAccess organization.
  301. if (segment_header.type == SegmentType::EndOfFile)
  302. break;
  303. }
  304. if (context.organization == Organization::RandomAccess) {
  305. for (auto const& segment_header : segment_headers)
  306. TRY(store_and_skip_segment_data(segment_header));
  307. }
  308. if (segment_headers.size() != segment_datas.size())
  309. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment headers and segment datas have different sizes");
  310. for (size_t i = 0; i < segment_headers.size(); ++i)
  311. context.segments.append({ segment_headers[i], segment_datas[i] });
  312. return {};
  313. }
  314. // 7.4.8 Page information segment syntax
  315. struct [[gnu::packed]] PageInformationSegment {
  316. BigEndian<u32> bitmap_width;
  317. BigEndian<u32> bitmap_height;
  318. BigEndian<u32> page_x_resolution; // In pixels/meter.
  319. BigEndian<u32> page_y_resolution; // In pixels/meter.
  320. u8 flags;
  321. BigEndian<u16> striping_information;
  322. };
  323. static_assert(AssertSize<PageInformationSegment, 19>());
  324. static ErrorOr<PageInformationSegment> decode_page_information_segment(ReadonlyBytes data)
  325. {
  326. // 7.4.8 Page information segment syntax
  327. if (data.size() != sizeof(PageInformationSegment))
  328. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid page information segment size");
  329. return *(PageInformationSegment const*)data.data();
  330. }
  331. static ErrorOr<void> scan_for_page_size(JBIG2LoadingContext& context)
  332. {
  333. // We only decode the first page at the moment.
  334. bool found_size = false;
  335. for (auto const& segment : context.segments) {
  336. if (segment.header.type != SegmentType::PageInformation || segment.header.page_association != 1)
  337. continue;
  338. auto page_information = TRY(decode_page_information_segment(segment.data));
  339. // FIXME: We're supposed to compute this from the striping information if it's not set.
  340. if (page_information.bitmap_height == 0xffff'ffff)
  341. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle unknown page height yet");
  342. context.page.size = { page_information.bitmap_width, page_information.bitmap_height };
  343. found_size = true;
  344. }
  345. if (!found_size)
  346. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No page information segment found for page 1");
  347. return {};
  348. }
  349. static ErrorOr<void> warn_about_multiple_pages(JBIG2LoadingContext& context)
  350. {
  351. HashTable<u32> seen_pages;
  352. Vector<u32> pages;
  353. for (auto const& segment : context.segments) {
  354. if (segment.header.page_association == 0)
  355. continue;
  356. if (seen_pages.contains(segment.header.page_association))
  357. continue;
  358. seen_pages.set(segment.header.page_association);
  359. pages.append(segment.header.page_association);
  360. }
  361. // scan_for_page_size() already checked that there's a page 1.
  362. VERIFY(seen_pages.contains(1));
  363. if (pages.size() == 1)
  364. return {};
  365. StringBuilder builder;
  366. builder.appendff("JBIG2 file contains {} pages ({}", pages.size(), pages[0]);
  367. size_t i;
  368. for (i = 1; i < min(pages.size(), 10); ++i)
  369. builder.appendff(" {}", pages[i]);
  370. if (i != pages.size())
  371. builder.append(" ..."sv);
  372. builder.append("). We will only render page 1."sv);
  373. dbgln("JBIG2ImageDecoderPlugin: {}", TRY(builder.to_string()));
  374. return {};
  375. }
  376. static ErrorOr<void> decode_symbol_dictionary(JBIG2LoadingContext&, SegmentData const&)
  377. {
  378. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode symbol dictionary yet");
  379. }
  380. static ErrorOr<void> decode_intermediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  381. {
  382. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate text region yet");
  383. }
  384. static ErrorOr<void> decode_immediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  385. {
  386. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate text region yet");
  387. }
  388. static ErrorOr<void> decode_immediate_lossless_text_region(JBIG2LoadingContext&, SegmentData const&)
  389. {
  390. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless text region yet");
  391. }
  392. static ErrorOr<void> decode_pattern_dictionary(JBIG2LoadingContext&, SegmentData const&)
  393. {
  394. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode pattern dictionary yet");
  395. }
  396. static ErrorOr<void> decode_intermediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  397. {
  398. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate halftone region yet");
  399. }
  400. static ErrorOr<void> decode_immediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  401. {
  402. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate halftone region yet");
  403. }
  404. static ErrorOr<void> decode_immediate_lossless_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  405. {
  406. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless halftone region yet");
  407. }
  408. static ErrorOr<void> decode_intermediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  409. {
  410. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic region yet");
  411. }
  412. static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  413. {
  414. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic region yet");
  415. }
  416. static ErrorOr<void> decode_immediate_lossless_generic_region(JBIG2LoadingContext&, SegmentData const&)
  417. {
  418. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic region yet");
  419. }
  420. static ErrorOr<void> decode_intermediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  421. {
  422. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic refinement region yet");
  423. }
  424. static ErrorOr<void> decode_immediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  425. {
  426. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic refinement region yet");
  427. }
  428. static ErrorOr<void> decode_immediate_lossless_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  429. {
  430. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic refinement region yet");
  431. }
  432. static ErrorOr<void> decode_page_information(JBIG2LoadingContext& context, SegmentData const& segment)
  433. {
  434. // 7.4.8 Page information segment syntax and 8.1 Decoder model steps 1) - 3).
  435. // "1) Decode the page information segment.""
  436. auto page_information = TRY(decode_page_information_segment(segment.data));
  437. bool page_is_striped = (page_information.striping_information & 0x80) != 0;
  438. if (page_information.bitmap_height == 0xffff'ffff && !page_is_striped)
  439. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Non-striped bitmaps of indeterminate height not allowed");
  440. u8 default_color = (page_information.flags >> 2) & 1;
  441. u8 default_combination_operator = (page_information.flags >> 3) & 3;
  442. context.page.default_combination_operator = static_cast<CombinationOperator>(default_combination_operator);
  443. // FIXME: Do something with the other fields in page_information.
  444. // "2) Create the page buffer, of the size given in the page information segment.
  445. //
  446. // If the page height is unknown, then this is not possible. However, in this case the page must be striped,
  447. // and the maximum stripe height specified, and the initial page buffer can be created with height initially
  448. // equal to this maximum stripe height."
  449. size_t height = page_information.bitmap_height;
  450. if (height == 0xffff'ffff)
  451. height = page_information.striping_information & 0x7F;
  452. context.page.bits = TRY(BitBuffer::create(page_information.bitmap_width, height));
  453. // "3) Fill the page buffer with the page's default pixel value."
  454. context.page.bits->fill(default_color != 0);
  455. return {};
  456. }
  457. static ErrorOr<void> decode_end_of_page(JBIG2LoadingContext&, SegmentData const&)
  458. {
  459. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of page yet");
  460. }
  461. static ErrorOr<void> decode_end_of_stripe(JBIG2LoadingContext&, SegmentData const&)
  462. {
  463. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of stripe yet");
  464. }
  465. static ErrorOr<void> decode_end_of_file(JBIG2LoadingContext&, SegmentData const&)
  466. {
  467. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of file yet");
  468. }
  469. static ErrorOr<void> decode_profiles(JBIG2LoadingContext&, SegmentData const&)
  470. {
  471. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode profiles yet");
  472. }
  473. static ErrorOr<void> decode_tables(JBIG2LoadingContext&, SegmentData const&)
  474. {
  475. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode tables yet");
  476. }
  477. static ErrorOr<void> decode_color_palette(JBIG2LoadingContext&, SegmentData const&)
  478. {
  479. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode color palette yet");
  480. }
  481. static ErrorOr<void> decode_extension(JBIG2LoadingContext&, SegmentData const& segment)
  482. {
  483. // 7.4.14 Extension segment syntax
  484. FixedMemoryStream stream { segment.data };
  485. enum ExtensionType {
  486. SingleByteCodedComment = 0x20000000,
  487. MultiByteCodedComment = 0x20000002,
  488. };
  489. u32 type = TRY(stream.read_value<BigEndian<u32>>());
  490. auto read_string = [&]<class T>() -> ErrorOr<Vector<T>> {
  491. Vector<T> result;
  492. do {
  493. result.append(TRY(stream.read_value<BigEndian<T>>()));
  494. } while (result.last());
  495. result.take_last();
  496. return result;
  497. };
  498. switch (type) {
  499. case SingleByteCodedComment: {
  500. // 7.4.15.1 Single-byte coded comment
  501. // Pairs of zero-terminated ISO/IEC 8859-1 (latin1) pairs, terminated by another \0.
  502. while (true) {
  503. auto first_bytes = TRY(read_string.template operator()<u8>());
  504. if (first_bytes.is_empty())
  505. break;
  506. auto second_bytes = TRY(read_string.template operator()<u8>());
  507. auto first = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { first_bytes }));
  508. auto second = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { second_bytes }));
  509. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  510. }
  511. if (!stream.is_eof())
  512. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after SingleByteCodedComment");
  513. return {};
  514. }
  515. case MultiByteCodedComment: {
  516. // 7.4.15.2 Multi-byte coded comment
  517. // Pairs of (two-byte-)zero-terminated UCS-2 pairs, terminated by another \0\0.
  518. while (true) {
  519. auto first_ucs2 = TRY(read_string.template operator()<u16>());
  520. if (first_ucs2.is_empty())
  521. break;
  522. auto second_ucs2 = TRY(read_string.template operator()<u16>());
  523. auto first = TRY(Utf16View(first_ucs2).to_utf8());
  524. auto second = TRY(Utf16View(second_ucs2).to_utf8());
  525. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  526. }
  527. if (!stream.is_eof())
  528. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after MultiByteCodedComment");
  529. return {};
  530. }
  531. }
  532. // FIXME: If bit 31 in `type` is not set, the extension isn't necessary, and we could ignore it.
  533. dbgln("JBIG2ImageDecoderPlugin: Unknown extension type {:#x}", type);
  534. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown extension type");
  535. }
  536. static ErrorOr<void> decode_data(JBIG2LoadingContext& context)
  537. {
  538. TRY(warn_about_multiple_pages(context));
  539. for (auto const& segment : context.segments) {
  540. if (segment.header.page_association != 0 && segment.header.page_association != 1)
  541. continue;
  542. switch (segment.header.type) {
  543. case SegmentType::SymbolDictionary:
  544. TRY(decode_symbol_dictionary(context, segment));
  545. break;
  546. case SegmentType::IntermediateTextRegion:
  547. TRY(decode_intermediate_text_region(context, segment));
  548. break;
  549. case SegmentType::ImmediateTextRegion:
  550. TRY(decode_immediate_text_region(context, segment));
  551. break;
  552. case SegmentType::ImmediateLosslessTextRegion:
  553. TRY(decode_immediate_lossless_text_region(context, segment));
  554. break;
  555. case SegmentType::PatternDictionary:
  556. TRY(decode_pattern_dictionary(context, segment));
  557. break;
  558. case SegmentType::IntermediateHalftoneRegion:
  559. TRY(decode_intermediate_halftone_region(context, segment));
  560. break;
  561. case SegmentType::ImmediateHalftoneRegion:
  562. TRY(decode_immediate_halftone_region(context, segment));
  563. break;
  564. case SegmentType::ImmediateLosslessHalftoneRegion:
  565. TRY(decode_immediate_lossless_halftone_region(context, segment));
  566. break;
  567. case SegmentType::IntermediateGenericRegion:
  568. TRY(decode_intermediate_generic_region(context, segment));
  569. break;
  570. case SegmentType::ImmediateGenericRegion:
  571. TRY(decode_immediate_generic_region(context, segment));
  572. break;
  573. case SegmentType::ImmediateLosslessGenericRegion:
  574. TRY(decode_immediate_lossless_generic_region(context, segment));
  575. break;
  576. case SegmentType::IntermediateGenericRefinementRegion:
  577. TRY(decode_intermediate_generic_refinement_region(context, segment));
  578. break;
  579. case SegmentType::ImmediateGenericRefinementRegion:
  580. TRY(decode_immediate_generic_refinement_region(context, segment));
  581. break;
  582. case SegmentType::ImmediateLosslessGenericRefinementRegion:
  583. TRY(decode_immediate_lossless_generic_refinement_region(context, segment));
  584. break;
  585. case SegmentType::PageInformation:
  586. TRY(decode_page_information(context, segment));
  587. break;
  588. case SegmentType::EndOfPage:
  589. TRY(decode_end_of_page(context, segment));
  590. break;
  591. case SegmentType::EndOfStripe:
  592. TRY(decode_end_of_stripe(context, segment));
  593. break;
  594. case SegmentType::EndOfFile:
  595. TRY(decode_end_of_file(context, segment));
  596. break;
  597. case SegmentType::Profiles:
  598. TRY(decode_profiles(context, segment));
  599. break;
  600. case SegmentType::Tables:
  601. TRY(decode_tables(context, segment));
  602. break;
  603. case SegmentType::ColorPalette:
  604. TRY(decode_color_palette(context, segment));
  605. break;
  606. case SegmentType::Extension:
  607. TRY(decode_extension(context, segment));
  608. break;
  609. }
  610. }
  611. return {};
  612. }
  613. JBIG2ImageDecoderPlugin::JBIG2ImageDecoderPlugin()
  614. {
  615. m_context = make<JBIG2LoadingContext>();
  616. }
  617. IntSize JBIG2ImageDecoderPlugin::size()
  618. {
  619. return m_context->page.size;
  620. }
  621. bool JBIG2ImageDecoderPlugin::sniff(ReadonlyBytes data)
  622. {
  623. return data.starts_with(id_string);
  624. }
  625. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JBIG2ImageDecoderPlugin::create(ReadonlyBytes data)
  626. {
  627. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  628. TRY(decode_jbig2_header(*plugin->m_context, data));
  629. data = data.slice(sizeof(id_string) + sizeof(u8) + (plugin->m_context->number_of_pages.has_value() ? sizeof(u32) : 0));
  630. TRY(decode_segment_headers(*plugin->m_context, data));
  631. TRY(scan_for_page_size(*plugin->m_context));
  632. return plugin;
  633. }
  634. ErrorOr<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  635. {
  636. // FIXME: Use this for multi-page JBIG2 files?
  637. if (index != 0)
  638. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid frame index");
  639. if (m_context->state == JBIG2LoadingContext::State::Error)
  640. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoding failed");
  641. if (m_context->state < JBIG2LoadingContext::State::Decoded) {
  642. auto result = decode_data(*m_context);
  643. if (result.is_error()) {
  644. m_context->state = JBIG2LoadingContext::State::Error;
  645. return result.release_error();
  646. }
  647. m_context->state = JBIG2LoadingContext::State::Decoded;
  648. }
  649. auto bitmap = TRY(m_context->page.bits->to_gfx_bitmap());
  650. return ImageFrameDescriptor { move(bitmap), 0 };
  651. }
  652. ErrorOr<ByteBuffer> JBIG2ImageDecoderPlugin::decode_embedded(Vector<ReadonlyBytes> data)
  653. {
  654. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  655. plugin->m_context->organization = Organization::Embedded;
  656. for (auto const& segment_data : data)
  657. TRY(decode_segment_headers(*plugin->m_context, segment_data));
  658. TRY(scan_for_page_size(*plugin->m_context));
  659. TRY(decode_data(*plugin->m_context));
  660. return plugin->m_context->page.bits->to_byte_buffer();
  661. }
  662. }