JBIG2Loader.cpp 39 KB

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