JBIG2Loader.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. namespace JBIG2 {
  16. // Table E.1 – Qe values and probability estimation process
  17. // See also E.1.2 Coding conventions and approximations
  18. // and E.2.5 Probability estimation.
  19. struct QeEntry {
  20. u16 qe; // Sub-interval for the less probable symbol.
  21. u16 nmps; // Next index if the more probable symbol is decoded
  22. u16 nlps; // Next index if the less probable symbol is decoded
  23. u16 switch_flag; // See second-to-last paragraph in E.1.2.
  24. };
  25. constexpr auto qe_table = to_array<QeEntry>({
  26. { 0x5601, 1, 1, 1 },
  27. { 0x3401, 2, 6, 0 },
  28. { 0x1801, 3, 9, 0 },
  29. { 0x0AC1, 4, 12, 0 },
  30. { 0x0521, 5, 29, 0 },
  31. { 0x0221, 38, 33, 0 },
  32. { 0x5601, 7, 6, 1 },
  33. { 0x5401, 8, 14, 0 },
  34. { 0x4801, 9, 14, 0 },
  35. { 0x3801, 10, 14, 0 },
  36. { 0x3001, 11, 17, 0 },
  37. { 0x2401, 12, 18, 0 },
  38. { 0x1C01, 13, 20, 0 },
  39. { 0x1601, 29, 21, 0 },
  40. { 0x5601, 15, 14, 1 },
  41. { 0x5401, 16, 14, 0 },
  42. { 0x5101, 17, 15, 0 },
  43. { 0x4801, 18, 16, 0 },
  44. { 0x3801, 19, 17, 0 },
  45. { 0x3401, 20, 18, 0 },
  46. { 0x3001, 21, 19, 0 },
  47. { 0x2801, 22, 19, 0 },
  48. { 0x2401, 23, 20, 0 },
  49. { 0x2201, 24, 21, 0 },
  50. { 0x1C01, 25, 22, 0 },
  51. { 0x1801, 26, 23, 0 },
  52. { 0x1601, 27, 24, 0 },
  53. { 0x1401, 28, 25, 0 },
  54. { 0x1201, 29, 26, 0 },
  55. { 0x1101, 30, 27, 0 },
  56. { 0x0AC1, 31, 28, 0 },
  57. { 0x09C1, 32, 29, 0 },
  58. { 0x08A1, 33, 30, 0 },
  59. { 0x0521, 34, 31, 0 },
  60. { 0x0441, 35, 32, 0 },
  61. { 0x02A1, 36, 33, 0 },
  62. { 0x0221, 37, 34, 0 },
  63. { 0x0141, 38, 35, 0 },
  64. { 0x0111, 39, 36, 0 },
  65. { 0x0085, 40, 37, 0 },
  66. { 0x0049, 41, 38, 0 },
  67. { 0x0025, 42, 39, 0 },
  68. { 0x0015, 43, 40, 0 },
  69. { 0x0009, 44, 41, 0 },
  70. { 0x0005, 45, 42, 0 },
  71. { 0x0001, 45, 43, 0 },
  72. { 0x5601, 46, 46, 0 },
  73. });
  74. ErrorOr<ArithmeticDecoder> ArithmeticDecoder::initialize(ReadonlyBytes data)
  75. {
  76. ArithmeticDecoder decoder { data };
  77. decoder.INITDEC();
  78. return decoder;
  79. }
  80. bool ArithmeticDecoder::get_next_bit(Context& context)
  81. {
  82. CX = &context;
  83. // Useful for comparing to Table H.1 – Encoder and decoder trace data.
  84. // dbg("I={} MPS={} A={:#x} C={:#x} CT={} B={:#x}", I(CX), MPS(CX), A, C, CT, B());
  85. u8 D = DECODE();
  86. // dbgln(" -> D={}", D);
  87. return D;
  88. }
  89. u16 ArithmeticDecoder::Qe(u16 index) { return qe_table[index].qe; }
  90. u8 ArithmeticDecoder::NMPS(u16 index) { return qe_table[index].nmps; }
  91. u8 ArithmeticDecoder::NLPS(u16 index) { return qe_table[index].nlps; }
  92. u8 ArithmeticDecoder::SWITCH(u16 index) { return qe_table[index].switch_flag; }
  93. u8 ArithmeticDecoder::B(size_t offset) const
  94. {
  95. // E.2.10 Minimization of the compressed data
  96. // "the convention is used in the decoder that when a marker code is encountered,
  97. // 1-bits (without bit stuffing) are supplied to the decoder until the coding interval is complete."
  98. if (BP + offset >= m_data.size())
  99. return 0xFF;
  100. return m_data[BP + offset];
  101. }
  102. void ArithmeticDecoder::INITDEC()
  103. {
  104. // E.3.5 Initialization of the decoder (INITDEC)
  105. // Figure G.1 – Initialization of the software conventions decoder
  106. // "BP, the pointer to the compressed data, is initialized to BPST (pointing to the first compressed byte)."
  107. auto const BPST = 0;
  108. BP = BPST;
  109. C = (B() ^ 0xFF) << 16;
  110. BYTEIN();
  111. C = C << 7;
  112. CT = CT - 7;
  113. A = 0x8000;
  114. }
  115. u8 ArithmeticDecoder::DECODE()
  116. {
  117. // E.3.2 Decoding a decision (DECODE)
  118. // Figure G.2 – Decoding an MPS or an LPS in the software-conventions decoder
  119. u8 D;
  120. A = A - Qe(I(CX));
  121. if (C < ((u32)A << 16)) { // `(C_high < A)` in spec
  122. if ((A & 0x8000) == 0) {
  123. D = MPS_EXCHANGE();
  124. RENORMD();
  125. } else {
  126. D = MPS(CX);
  127. }
  128. } else {
  129. C = C - ((u32)A << 16); // `C_high = C_high - A` in spec
  130. D = LPS_EXCHANGE();
  131. RENORMD();
  132. }
  133. return D;
  134. }
  135. u8 ArithmeticDecoder::MPS_EXCHANGE()
  136. {
  137. // Figure E.16 – Decoder MPS path conditional exchange procedure
  138. u8 D;
  139. if (A < Qe(I(CX))) {
  140. D = 1 - MPS(CX);
  141. if (SWITCH(I(CX)) == 1) {
  142. MPS(CX) = 1 - MPS(CX);
  143. }
  144. I(CX) = NLPS(I(CX));
  145. } else {
  146. D = MPS(CX);
  147. I(CX) = NMPS(I(CX));
  148. }
  149. return D;
  150. }
  151. u8 ArithmeticDecoder::LPS_EXCHANGE()
  152. {
  153. // Figure E.17 – Decoder LPS path conditional exchange procedure
  154. u8 D;
  155. if (A < Qe(I(CX))) {
  156. A = Qe(I(CX));
  157. D = MPS(CX);
  158. I(CX) = NMPS(I(CX));
  159. } else {
  160. A = Qe(I(CX));
  161. D = 1 - MPS(CX);
  162. if (SWITCH(I(CX)) == 1) {
  163. MPS(CX) = 1 - MPS(CX);
  164. }
  165. I(CX) = NLPS(I(CX));
  166. }
  167. return D;
  168. }
  169. void ArithmeticDecoder::RENORMD()
  170. {
  171. // E.3.3 Renormalization in the decoder (RENORMD)
  172. // Figure E.18 – Decoder renormalization procedure
  173. do {
  174. if (CT == 0)
  175. BYTEIN();
  176. A = A << 1;
  177. C = C << 1;
  178. CT = CT - 1;
  179. } while ((A & 0x8000) == 0);
  180. }
  181. void ArithmeticDecoder::BYTEIN()
  182. {
  183. // E.3.4 Compressed data input (BYTEIN)
  184. // Figure G.3 – Inserting a new byte into the C register in the software-conventions decoder
  185. if (B() == 0xFF) {
  186. if (B(1) > 0x8F) {
  187. CT = 8;
  188. } else {
  189. BP = BP + 1;
  190. C = C + 0xFE00 - (B() << 9);
  191. CT = 7;
  192. }
  193. } else {
  194. BP = BP + 1;
  195. C = C + 0xFF00 - (B() << 8);
  196. CT = 8;
  197. }
  198. }
  199. // Annex A, Arithmetic integer decoding procedure
  200. class ArithmeticIntegerDecoder {
  201. public:
  202. ArithmeticIntegerDecoder(ArithmeticDecoder&);
  203. // A.2 Procedure for decoding values (except IAID)
  204. // Returns OptionalNone for OOB.
  205. Optional<i32> decode();
  206. private:
  207. ArithmeticDecoder& m_decoder;
  208. u16 PREV { 0 };
  209. Vector<ArithmeticDecoder::Context> contexts;
  210. };
  211. ArithmeticIntegerDecoder::ArithmeticIntegerDecoder(ArithmeticDecoder& decoder)
  212. : m_decoder(decoder)
  213. {
  214. contexts.resize(1 << 9);
  215. }
  216. Optional<int> ArithmeticIntegerDecoder::decode()
  217. {
  218. // A.2 Procedure for decoding values (except IAID)
  219. // "1) Set:
  220. // PREV = 1"
  221. u16 PREV = 1;
  222. // "2) Follow the flowchart in Figure A.1. Decode each bit with CX equal to "IAx + PREV" where "IAx" represents the identifier
  223. // of the current arithmetic integer decoding procedure, "+" represents concatenation, and the rightmost 9 bits of PREV are used."
  224. auto decode_bit = [&]() {
  225. bool D = m_decoder.get_next_bit(contexts[PREV & 0x1FF]);
  226. // "3) After each bit is decoded:
  227. // If PREV < 256 set:
  228. // PREV = (PREV << 1) OR D
  229. // Otherwise set:
  230. // PREV = (((PREV << 1) OR D) AND 511) OR 256
  231. // where D represents the value of the just-decoded bit.
  232. if (PREV < 256)
  233. PREV = (PREV << 1) | (u16)D;
  234. else
  235. PREV = (((PREV << 1) | (u16)D) & 511) | 256;
  236. return D;
  237. };
  238. auto decode_bits = [&](int n) {
  239. u32 result = 0;
  240. for (int i = 0; i < n; ++i)
  241. result = (result << 1) | decode_bit();
  242. return result;
  243. };
  244. // Figure A.1 – Flowchart for the integer arithmetic decoding procedures (except IAID)
  245. u8 S = decode_bit();
  246. u32 V;
  247. if (!decode_bit())
  248. V = decode_bits(2);
  249. else if (!decode_bit())
  250. V = decode_bits(4) + 4;
  251. else if (!decode_bit())
  252. V = decode_bits(6) + 20;
  253. else if (!decode_bit())
  254. V = decode_bits(8) + 84;
  255. else if (!decode_bit())
  256. V = decode_bits(12) + 340;
  257. else
  258. V = decode_bits(32) + 4436;
  259. // "4) The sequence of bits decoded, interpreted according to Table A.1, gives the value that is the result of this invocation
  260. // of the integer arithmetic decoding procedure."
  261. if (S == 1 && V == 0)
  262. return {};
  263. return S ? -V : V;
  264. }
  265. }
  266. // JBIG2 spec, Annex D, D.4.1 ID string
  267. static constexpr u8 id_string[] = { 0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A };
  268. // 7.3 Segment types
  269. enum SegmentType {
  270. SymbolDictionary = 0,
  271. IntermediateTextRegion = 4,
  272. ImmediateTextRegion = 6,
  273. ImmediateLosslessTextRegion = 7,
  274. PatternDictionary = 16,
  275. IntermediateHalftoneRegion = 20,
  276. ImmediateHalftoneRegion = 22,
  277. ImmediateLosslessHalftoneRegion = 23,
  278. IntermediateGenericRegion = 36,
  279. ImmediateGenericRegion = 38,
  280. ImmediateLosslessGenericRegion = 39,
  281. IntermediateGenericRefinementRegion = 40,
  282. ImmediateGenericRefinementRegion = 42,
  283. ImmediateLosslessGenericRefinementRegion = 43,
  284. PageInformation = 48,
  285. EndOfPage = 49,
  286. EndOfStripe = 50,
  287. EndOfFile = 51,
  288. Profiles = 52,
  289. Tables = 53,
  290. ColorPalette = 54,
  291. Extension = 62,
  292. };
  293. // Annex D
  294. enum class Organization {
  295. // D.1 Sequential organization
  296. Sequential,
  297. // D.2 Random-access organization
  298. RandomAccess,
  299. // D.3 Embedded organization
  300. Embedded,
  301. };
  302. struct SegmentHeader {
  303. u32 segment_number { 0 };
  304. SegmentType type { SegmentType::Extension };
  305. Vector<u32> referred_to_segment_numbers;
  306. // 7.2.6 Segment page association
  307. // "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."
  308. u32 page_association { 0 };
  309. Optional<u32> data_length;
  310. };
  311. struct SegmentData {
  312. SegmentHeader header;
  313. ReadonlyBytes data;
  314. };
  315. class BitBuffer {
  316. public:
  317. static ErrorOr<NonnullOwnPtr<BitBuffer>> create(size_t width, size_t height);
  318. bool get_bit(size_t x, size_t y) const;
  319. void set_bit(size_t x, size_t y, bool b);
  320. void fill(bool b);
  321. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_gfx_bitmap() const;
  322. ErrorOr<ByteBuffer> to_byte_buffer() const;
  323. private:
  324. BitBuffer(ByteBuffer, size_t width, size_t height, size_t pitch);
  325. ByteBuffer m_bits;
  326. size_t m_width { 0 };
  327. size_t m_height { 0 };
  328. size_t m_pitch { 0 };
  329. };
  330. ErrorOr<NonnullOwnPtr<BitBuffer>> BitBuffer::create(size_t width, size_t height)
  331. {
  332. size_t pitch = ceil_div(width, 8ull);
  333. auto bits = TRY(ByteBuffer::create_uninitialized(pitch * height));
  334. return adopt_nonnull_own_or_enomem(new (nothrow) BitBuffer(move(bits), width, height, pitch));
  335. }
  336. bool BitBuffer::get_bit(size_t x, size_t y) const
  337. {
  338. VERIFY(x < m_width);
  339. VERIFY(y < m_height);
  340. size_t byte_offset = x / 8;
  341. size_t bit_offset = x % 8;
  342. u8 byte = m_bits[y * m_pitch + byte_offset];
  343. byte = (byte >> (8 - 1 - bit_offset)) & 1;
  344. return byte != 0;
  345. }
  346. void BitBuffer::set_bit(size_t x, size_t y, bool b)
  347. {
  348. VERIFY(x < m_width);
  349. VERIFY(y < m_height);
  350. size_t byte_offset = x / 8;
  351. size_t bit_offset = x % 8;
  352. u8 byte = m_bits[y * m_pitch + byte_offset];
  353. u8 mask = 1u << (8 - 1 - bit_offset);
  354. if (b)
  355. byte |= mask;
  356. else
  357. byte &= ~mask;
  358. m_bits[y * m_pitch + byte_offset] = byte;
  359. }
  360. void BitBuffer::fill(bool b)
  361. {
  362. u8 fill_byte = b ? 0xff : 0;
  363. for (auto& byte : m_bits.bytes())
  364. byte = fill_byte;
  365. }
  366. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> BitBuffer::to_gfx_bitmap() const
  367. {
  368. auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { m_width, m_height }));
  369. for (size_t y = 0; y < m_height; ++y) {
  370. for (size_t x = 0; x < m_width; ++x) {
  371. auto color = get_bit(x, y) ? Color::Black : Color::White;
  372. bitmap->set_pixel(x, y, color);
  373. }
  374. }
  375. return bitmap;
  376. }
  377. ErrorOr<ByteBuffer> BitBuffer::to_byte_buffer() const
  378. {
  379. return ByteBuffer::copy(m_bits);
  380. }
  381. BitBuffer::BitBuffer(ByteBuffer bits, size_t width, size_t height, size_t pitch)
  382. : m_bits(move(bits))
  383. , m_width(width)
  384. , m_height(height)
  385. , m_pitch(pitch)
  386. {
  387. }
  388. // 7.4.8.5 Page segment flags
  389. enum class CombinationOperator {
  390. Or = 0,
  391. And = 1,
  392. Xor = 2,
  393. XNor = 3,
  394. Replace = 4,
  395. };
  396. struct Page {
  397. IntSize size;
  398. // This is never CombinationOperator::Replace for Pages.
  399. CombinationOperator default_combination_operator { CombinationOperator::Or };
  400. OwnPtr<BitBuffer> bits;
  401. };
  402. struct JBIG2LoadingContext {
  403. enum class State {
  404. NotDecoded = 0,
  405. Error,
  406. Decoded,
  407. };
  408. State state { State::NotDecoded };
  409. Organization organization { Organization::Sequential };
  410. Page page;
  411. Optional<u32> number_of_pages;
  412. Vector<SegmentData> segments;
  413. };
  414. static ErrorOr<void> decode_jbig2_header(JBIG2LoadingContext& context, ReadonlyBytes data)
  415. {
  416. if (!JBIG2ImageDecoderPlugin::sniff(data))
  417. return Error::from_string_literal("JBIG2LoadingContext: Invalid JBIG2 header");
  418. FixedMemoryStream stream(data.slice(sizeof(id_string)));
  419. // D.4.2 File header flags
  420. u8 header_flags = TRY(stream.read_value<u8>());
  421. if (header_flags & 0b11110000)
  422. return Error::from_string_literal("JBIG2LoadingContext: Invalid header flags");
  423. context.organization = (header_flags & 1) ? Organization::Sequential : Organization::RandomAccess;
  424. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Organization: {} ({})", (int)context.organization, context.organization == Organization::Sequential ? "Sequential" : "Random-access");
  425. bool has_known_number_of_pages = (header_flags & 2) ? false : true;
  426. bool uses_templates_with_12_AT_pixels = (header_flags & 4) ? true : false;
  427. bool contains_colored_region_segments = (header_flags & 8) ? true : false;
  428. // FIXME: Do something with these?
  429. (void)uses_templates_with_12_AT_pixels;
  430. (void)contains_colored_region_segments;
  431. // D.4.3 Number of pages
  432. if (has_known_number_of_pages) {
  433. context.number_of_pages = TRY(stream.read_value<BigEndian<u32>>());
  434. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Number of pages: {}", context.number_of_pages.value());
  435. }
  436. return {};
  437. }
  438. static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
  439. {
  440. // 7.2.2 Segment number
  441. u32 segment_number = TRY(stream.read_value<BigEndian<u32>>());
  442. dbgln_if(JBIG2_DEBUG, "Segment number: {}", segment_number);
  443. // 7.2.3 Segment header flags
  444. u8 flags = TRY(stream.read_value<u8>());
  445. SegmentType type = static_cast<SegmentType>(flags & 0b11'1111);
  446. dbgln_if(JBIG2_DEBUG, "Segment type: {}", (int)type);
  447. bool segment_page_association_size_is_32_bits = (flags & 0b100'0000) != 0;
  448. bool segment_retained_only_by_itself_and_extension_segments = (flags & 0b1000'00000) != 0;
  449. // FIXME: Do something with these.
  450. (void)segment_page_association_size_is_32_bits;
  451. (void)segment_retained_only_by_itself_and_extension_segments;
  452. // 7.2.4 Referred-to segment count and retention flags
  453. u8 referred_to_segment_count_and_retention_flags = TRY(stream.read_value<u8>());
  454. u32 count_of_referred_to_segments = referred_to_segment_count_and_retention_flags >> 5;
  455. if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
  456. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid count_of_referred_to_segments");
  457. u32 extra_count = 0;
  458. if (count_of_referred_to_segments == 7) {
  459. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  460. count_of_referred_to_segments = TRY(stream.read_value<BigEndian<u32>>()) & 0x1FFF'FFFF;
  461. extra_count = ceil_div(count_of_referred_to_segments + 1, 8);
  462. TRY(stream.seek(extra_count, SeekMode::FromCurrentPosition));
  463. }
  464. dbgln_if(JBIG2_DEBUG, "Referred-to segment count: {}", count_of_referred_to_segments);
  465. // 7.2.5 Referred-to segment numbers
  466. Vector<u32> referred_to_segment_numbers;
  467. for (u32 i = 0; i < count_of_referred_to_segments; ++i) {
  468. u32 referred_to_segment_number;
  469. if (segment_number <= 256)
  470. referred_to_segment_number = TRY(stream.read_value<u8>());
  471. else if (segment_number <= 65536)
  472. referred_to_segment_number = TRY(stream.read_value<BigEndian<u16>>());
  473. else
  474. referred_to_segment_number = TRY(stream.read_value<BigEndian<u32>>());
  475. referred_to_segment_numbers.append(referred_to_segment_number);
  476. dbgln_if(JBIG2_DEBUG, "Referred-to segment number: {}", referred_to_segment_number);
  477. }
  478. // 7.2.6 Segment page association
  479. u32 segment_page_association;
  480. if (segment_page_association_size_is_32_bits) {
  481. segment_page_association = TRY(stream.read_value<BigEndian<u32>>());
  482. } else {
  483. segment_page_association = TRY(stream.read_value<u8>());
  484. }
  485. dbgln_if(JBIG2_DEBUG, "Segment page association: {}", segment_page_association);
  486. // 7.2.7 Segment data length
  487. u32 data_length = TRY(stream.read_value<BigEndian<u32>>());
  488. dbgln_if(JBIG2_DEBUG, "Segment data length: {}", data_length);
  489. // FIXME: Add some validity checks:
  490. // - check type is valid
  491. // - check referred_to_segment_numbers are smaller than segment_number
  492. // - 7.3.1 Rules for segment references
  493. // - 7.3.2 Rules for page associations
  494. Optional<u32> opt_data_length;
  495. if (data_length != 0xffff'ffff)
  496. opt_data_length = data_length;
  497. else if (type != ImmediateGenericRegion)
  498. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown data length only allowed for ImmediateGenericRegion");
  499. return SegmentHeader { segment_number, type, move(referred_to_segment_numbers), segment_page_association, opt_data_length };
  500. }
  501. static ErrorOr<size_t> scan_for_immediate_generic_region_size(ReadonlyBytes data)
  502. {
  503. // 7.2.7 Segment data length
  504. // "If the segment's type is "Immediate generic region", then the length field may contain the value 0xFFFFFFFF.
  505. // 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 (...).
  506. // In this case, the true length of the segment's data part shall be determined through examination of the data:
  507. // 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.
  508. // 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.
  509. // The form of encoding used by the segment may be determined by examining the eighteenth byte of its segment data part,
  510. // and the end sequences can occur anywhere after that eighteenth byte."
  511. // 7.4.6.4 Decoding a generic region segment
  512. // "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.
  513. // Thus, those sequences cannot occur by chance in the data that is decoded to generate the contents of the generic region."
  514. dbgln_if(JBIG2_DEBUG, "(Unknown data length, computing it)");
  515. if (data.size() < 19 + sizeof(u32))
  516. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Data too short to contain segment data header and end sequence");
  517. // Per 7.4.6.1 Generic region segment data header, this starts with the 17 bytes described in
  518. // 7.4.1 Region segment information field, followed the byte described in 7.4.6.2 Generic region segment flags.
  519. // That byte's lowest bit stores if the segment uses MMR.
  520. u8 flags = data[17];
  521. bool uses_mmr = (flags & 1) != 0;
  522. auto end_sequence = uses_mmr ? to_array<u8>({ 0x00, 0x00 }) : to_array<u8>({ 0xFF, 0xAC });
  523. u8 const* end = static_cast<u8 const*>(memmem(data.data() + 19, data.size() - 19 - sizeof(u32), end_sequence.data(), end_sequence.size()));
  524. if (!end)
  525. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Could not find end sequence in segment data");
  526. size_t size = end - data.data() + end_sequence.size() + sizeof(u32);
  527. dbgln_if(JBIG2_DEBUG, "(Computed size is {})", size);
  528. return size;
  529. }
  530. static ErrorOr<void> decode_segment_headers(JBIG2LoadingContext& context, ReadonlyBytes data)
  531. {
  532. FixedMemoryStream stream(data);
  533. Vector<ReadonlyBytes> segment_datas;
  534. auto store_and_skip_segment_data = [&](SegmentHeader const& segment_header) -> ErrorOr<void> {
  535. size_t start_offset = TRY(stream.tell());
  536. u32 data_length = TRY(segment_header.data_length.try_value_or_lazy_evaluated([&]() {
  537. return scan_for_immediate_generic_region_size(data.slice(start_offset));
  538. }));
  539. if (start_offset + data_length > data.size()) {
  540. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: start_offset={}, data_length={}, data.size()={}", start_offset, data_length, data.size());
  541. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment data length exceeds file size");
  542. }
  543. ReadonlyBytes segment_data = data.slice(start_offset, data_length);
  544. segment_datas.append(segment_data);
  545. TRY(stream.seek(data_length, SeekMode::FromCurrentPosition));
  546. return {};
  547. };
  548. Vector<SegmentHeader> segment_headers;
  549. while (!stream.is_eof()) {
  550. auto segment_header = TRY(decode_segment_header(stream));
  551. segment_headers.append(segment_header);
  552. if (context.organization != Organization::RandomAccess)
  553. TRY(store_and_skip_segment_data(segment_header));
  554. // Required per spec for files with RandomAccess organization.
  555. if (segment_header.type == SegmentType::EndOfFile)
  556. break;
  557. }
  558. if (context.organization == Organization::RandomAccess) {
  559. for (auto const& segment_header : segment_headers)
  560. TRY(store_and_skip_segment_data(segment_header));
  561. }
  562. if (segment_headers.size() != segment_datas.size())
  563. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment headers and segment datas have different sizes");
  564. for (size_t i = 0; i < segment_headers.size(); ++i)
  565. context.segments.append({ segment_headers[i], segment_datas[i] });
  566. return {};
  567. }
  568. // 7.4.1 Region segment information field
  569. struct [[gnu::packed]] RegionSegmentInformationField {
  570. BigEndian<u32> width;
  571. BigEndian<u32> height;
  572. BigEndian<u32> x_location;
  573. BigEndian<u32> y_location;
  574. u8 flags;
  575. CombinationOperator external_combination_operator() const
  576. {
  577. VERIFY((flags & 0x7) <= 4);
  578. return static_cast<CombinationOperator>(flags & 0x7);
  579. }
  580. bool is_color_bitmap() const
  581. {
  582. return (flags & 0x8) != 0;
  583. }
  584. };
  585. static_assert(AssertSize<RegionSegmentInformationField, 17>());
  586. static ErrorOr<RegionSegmentInformationField> decode_region_segment_information_field(ReadonlyBytes data)
  587. {
  588. // 7.4.8 Page information segment syntax
  589. if (data.size() < sizeof(RegionSegmentInformationField))
  590. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field size");
  591. auto result = *(RegionSegmentInformationField const*)data.data();
  592. if ((result.flags & 0b1111'0000) != 0)
  593. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field flags");
  594. if ((result.flags & 0x7) > 4)
  595. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field operator");
  596. // NOTE 3 – If the colour extension flag (COLEXTFLAG) is equal to 1, the external combination operator must be REPLACE.
  597. if (result.is_color_bitmap() && result.external_combination_operator() != CombinationOperator::Replace)
  598. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid colored region segment information field operator");
  599. return result;
  600. }
  601. // 7.4.8 Page information segment syntax
  602. struct [[gnu::packed]] PageInformationSegment {
  603. BigEndian<u32> bitmap_width;
  604. BigEndian<u32> bitmap_height;
  605. BigEndian<u32> page_x_resolution; // In pixels/meter.
  606. BigEndian<u32> page_y_resolution; // In pixels/meter.
  607. u8 flags;
  608. BigEndian<u16> striping_information;
  609. };
  610. static_assert(AssertSize<PageInformationSegment, 19>());
  611. static ErrorOr<PageInformationSegment> decode_page_information_segment(ReadonlyBytes data)
  612. {
  613. // 7.4.8 Page information segment syntax
  614. if (data.size() != sizeof(PageInformationSegment))
  615. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid page information segment size");
  616. return *(PageInformationSegment const*)data.data();
  617. }
  618. static ErrorOr<void> scan_for_page_size(JBIG2LoadingContext& context)
  619. {
  620. // We only decode the first page at the moment.
  621. bool found_size = false;
  622. for (auto const& segment : context.segments) {
  623. if (segment.header.type != SegmentType::PageInformation || segment.header.page_association != 1)
  624. continue;
  625. auto page_information = TRY(decode_page_information_segment(segment.data));
  626. // FIXME: We're supposed to compute this from the striping information if it's not set.
  627. if (page_information.bitmap_height == 0xffff'ffff)
  628. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle unknown page height yet");
  629. context.page.size = { page_information.bitmap_width, page_information.bitmap_height };
  630. found_size = true;
  631. }
  632. if (!found_size)
  633. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No page information segment found for page 1");
  634. return {};
  635. }
  636. static ErrorOr<void> warn_about_multiple_pages(JBIG2LoadingContext& context)
  637. {
  638. HashTable<u32> seen_pages;
  639. Vector<u32> pages;
  640. for (auto const& segment : context.segments) {
  641. if (segment.header.page_association == 0)
  642. continue;
  643. if (seen_pages.contains(segment.header.page_association))
  644. continue;
  645. seen_pages.set(segment.header.page_association);
  646. pages.append(segment.header.page_association);
  647. }
  648. // scan_for_page_size() already checked that there's a page 1.
  649. VERIFY(seen_pages.contains(1));
  650. if (pages.size() == 1)
  651. return {};
  652. StringBuilder builder;
  653. builder.appendff("JBIG2 file contains {} pages ({}", pages.size(), pages[0]);
  654. size_t i;
  655. for (i = 1; i < min(pages.size(), 10); ++i)
  656. builder.appendff(" {}", pages[i]);
  657. if (i != pages.size())
  658. builder.append(" ..."sv);
  659. builder.append("). We will only render page 1."sv);
  660. dbgln("JBIG2ImageDecoderPlugin: {}", TRY(builder.to_string()));
  661. return {};
  662. }
  663. struct AdaptiveTemplatePixel {
  664. i8 x { 0 };
  665. i8 y { 0 };
  666. };
  667. // 6.2.2 Input parameters
  668. // Table 2 – Parameters for the generic region decoding procedure
  669. struct GenericRegionDecodingInputParameters {
  670. bool is_modified_modified_read { false }; // "MMR" in spec.
  671. u32 region_width { 0 }; // "GBW" in spec.
  672. u32 region_height { 0 }; // "GBH" in spec.
  673. u8 gb_template { 0 };
  674. bool is_typical_prediction_used { false }; // "TPGDON" in spec.
  675. bool is_extended_reference_template_used { false }; // "EXTTEMPLATE" in spec.
  676. Optional<NonnullOwnPtr<BitBuffer>> skip_pattern; // "USESKIP", "SKIP" in spec.
  677. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels; // "GBATX" / "GBATY" in spec.
  678. // FIXME: GBCOLS, GBCOMBOP, COLEXTFLAG
  679. // If is_modified_modified_read is false, generic_region_decoding_procedure() reads data off this decoder.
  680. JBIG2::ArithmeticDecoder* arithmetic_decoder { nullptr };
  681. };
  682. // 6.2 Generic region decoding procedure
  683. static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(GenericRegionDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<JBIG2::ArithmeticDecoder::Context>& contexts)
  684. {
  685. if (inputs.is_modified_modified_read) {
  686. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: MMR image data");
  687. // 6.2.6 Decoding using MMR coding
  688. auto buffer = TRY(CCITT::decode_ccitt_group4(data, inputs.region_width, inputs.region_height));
  689. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  690. size_t bytes_per_row = ceil_div(inputs.region_width, 8);
  691. if (buffer.size() != bytes_per_row * inputs.region_height)
  692. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoded MMR data has wrong size");
  693. // FIXME: Could probably just copy the ByteBuffer directly into the BitBuffer's internal ByteBuffer instead.
  694. for (size_t y = 0; y < inputs.region_height; ++y) {
  695. for (size_t x = 0; x < inputs.region_width; ++x) {
  696. bool bit = buffer[y * bytes_per_row + x / 8] & (1 << (7 - x % 8));
  697. result->set_bit(x, y, bit);
  698. }
  699. }
  700. return result;
  701. }
  702. // 6.2.5 Decoding using a template and arithmetic coding
  703. if (inputs.gb_template != 0)
  704. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode GBTEMPLATE != 0 yet");
  705. if (inputs.adaptive_template_pixels[0].x != 3 || inputs.adaptive_template_pixels[0].y != -1
  706. || inputs.adaptive_template_pixels[1].x != -3 || inputs.adaptive_template_pixels[1].y != -1
  707. || inputs.adaptive_template_pixels[2].x != 2 || inputs.adaptive_template_pixels[2].y != -2
  708. || inputs.adaptive_template_pixels[3].x != -2 || inputs.adaptive_template_pixels[3].y != -2)
  709. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle custom adaptive pixels yet");
  710. if (inputs.is_extended_reference_template_used)
  711. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode EXTTEMPLATE yet");
  712. if (inputs.skip_pattern.has_value())
  713. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode USESKIP yet");
  714. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  715. auto get_pixel = [&inputs](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> bool {
  716. if (x < 0 || x >= (int)inputs.region_width || y < 0)
  717. return false;
  718. return buffer->get_bit(x, y);
  719. };
  720. // Figure 3(a) – Template when GBTEMPLATE = 0 and EXTTEMPLATE = 0,
  721. auto compute_context = [&get_pixel](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> u16 {
  722. u16 result = 0;
  723. for (int i = 0; i < 5; ++i)
  724. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y - 2);
  725. for (int i = 0; i < 7; ++i)
  726. result = (result << 1) | (u16)get_pixel(buffer, x - 3 + i, y - 1);
  727. for (int i = 0; i < 4; ++i)
  728. result = (result << 1) | (u16)get_pixel(buffer, x - 4 + i, y);
  729. return result;
  730. };
  731. // "The values of the pixels in this neighbourhood define a context. Each context has its own adaptive probability estimate
  732. // used by the arithmetic coder (see Annex E)."
  733. // "* Decode the current pixel by invoking the arithmetic entropy decoding procedure, with CX set to the value formed by
  734. // concatenating the label "GB" and the 10-16 pixel values gathered in CONTEXT."
  735. // Implementor's note: What this is supposed to mean is that we have a bunch of independent contexts, and we pick the
  736. // context for the current pixel based on pixel values in the neighborhood. The "GB" part just means this context is
  737. // independent from other contexts in the spec. They are passed in to this function.
  738. // Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
  739. constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
  740. // 6.2.5.7 Decoding the bitmap
  741. JBIG2::ArithmeticDecoder& decoder = *inputs.arithmetic_decoder;
  742. bool ltp = false; // "LTP" in spec. "Line (uses) Typical Prediction" maybe?
  743. for (size_t y = 0; y < inputs.region_height; ++y) {
  744. if (inputs.is_typical_prediction_used) {
  745. // "SLTP" in spec. "Swap LTP" or "Switch LTP" maybe?
  746. bool sltp = decoder.get_next_bit(contexts[sltp_context_for_template_0]);
  747. ltp = ltp ^ sltp;
  748. if (ltp) {
  749. for (size_t x = 0; x < inputs.region_width; ++x)
  750. result->set_bit(x, y, get_pixel(result, (int)x, (int)y - 1));
  751. continue;
  752. }
  753. }
  754. for (size_t x = 0; x < inputs.region_width; ++x) {
  755. u16 context = compute_context(result, x, y);
  756. bool bit = decoder.get_next_bit(contexts[context]);
  757. result->set_bit(x, y, bit);
  758. }
  759. }
  760. return result;
  761. }
  762. static ErrorOr<void> decode_symbol_dictionary(JBIG2LoadingContext&, SegmentData const&)
  763. {
  764. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode symbol dictionary yet");
  765. }
  766. static ErrorOr<void> decode_intermediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  767. {
  768. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate text region yet");
  769. }
  770. static ErrorOr<void> decode_immediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  771. {
  772. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate text region yet");
  773. }
  774. static ErrorOr<void> decode_immediate_lossless_text_region(JBIG2LoadingContext&, SegmentData const&)
  775. {
  776. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless text region yet");
  777. }
  778. static ErrorOr<void> decode_pattern_dictionary(JBIG2LoadingContext&, SegmentData const&)
  779. {
  780. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode pattern dictionary yet");
  781. }
  782. static ErrorOr<void> decode_intermediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  783. {
  784. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate halftone region yet");
  785. }
  786. static ErrorOr<void> decode_immediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  787. {
  788. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate halftone region yet");
  789. }
  790. static ErrorOr<void> decode_immediate_lossless_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  791. {
  792. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless halftone region yet");
  793. }
  794. static ErrorOr<void> decode_intermediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  795. {
  796. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic region yet");
  797. }
  798. static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& context, SegmentData const& segment)
  799. {
  800. // 7.4.6 Generic region segment syntax
  801. auto data = segment.data;
  802. auto information_field = TRY(decode_region_segment_information_field(data));
  803. data = data.slice(sizeof(information_field));
  804. dbgln_if(JBIG2_DEBUG, "Generic region: width={}, height={}, x={}, y={}, flags={:#x}", information_field.width, information_field.height, information_field.x_location, information_field.y_location, information_field.flags);
  805. // 7.4.6.2 Generic region segment flags
  806. if (data.is_empty())
  807. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No segment data");
  808. u8 flags = data[0];
  809. bool uses_mmr = (flags & 1) != 0;
  810. u8 arithmetic_coding_template = (flags >> 1) & 3; // "GBTEMPLATE"
  811. bool typical_prediction_generic_decoding_on = (flags >> 3) & 1; // "TPGDON"; "TPGD" is short for "Typical Prediction for Generic Direct coding"
  812. bool uses_extended_reference_template = (flags >> 4) & 1; // "EXTTEMPLATE"
  813. if (flags & 0b1110'0000)
  814. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid flags");
  815. data = data.slice(sizeof(flags));
  816. // 7.4.6.3 Generic region segment AT flags
  817. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels {};
  818. if (!uses_mmr) {
  819. dbgln_if(JBIG2_DEBUG, "Non-MMR generic region, GBTEMPLATE={} TPGDON={} EXTTEMPLATE={}", arithmetic_coding_template, typical_prediction_generic_decoding_on, uses_extended_reference_template);
  820. if (arithmetic_coding_template == 0 && uses_extended_reference_template) {
  821. // This was added in T.88 Amendment 2 (https://www.itu.int/rec/T-REC-T.88-200306-S!Amd2/en) mid-2003.
  822. // I haven't seen it being used in the wild, and the spec says "32-byte field as shown below" and then shows 24 bytes,
  823. // so it's not clear how much data to read.
  824. return Error::from_string_literal("JBIG2ImageDecoderPlugin: GBTEMPLATE=0 EXTTEMPLATE=1 not yet implemented");
  825. }
  826. size_t number_of_adaptive_template_pixels = arithmetic_coding_template == 0 ? 4 : 1;
  827. if (data.size() < 2 * number_of_adaptive_template_pixels)
  828. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No adaptive template data");
  829. for (size_t i = 0; i < number_of_adaptive_template_pixels; ++i) {
  830. adaptive_template_pixels[i].x = static_cast<i8>(data[2 * i]);
  831. adaptive_template_pixels[i].y = static_cast<i8>(data[2 * i + 1]);
  832. }
  833. data = data.slice(2 * number_of_adaptive_template_pixels);
  834. }
  835. // 7.4.6.4 Decoding a generic region segment
  836. // "1) Interpret its header, as described in 7.4.6.1"
  837. // Done above.
  838. // "2) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  839. Vector<JBIG2::ArithmeticDecoder::Context> contexts;
  840. contexts.resize(1 << 16);
  841. // "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."
  842. GenericRegionDecodingInputParameters inputs;
  843. inputs.is_modified_modified_read = uses_mmr;
  844. inputs.region_width = information_field.width;
  845. inputs.region_height = information_field.height;
  846. inputs.gb_template = arithmetic_coding_template;
  847. inputs.is_typical_prediction_used = typical_prediction_generic_decoding_on;
  848. inputs.is_extended_reference_template_used = uses_extended_reference_template;
  849. inputs.skip_pattern = OptionalNone {};
  850. inputs.adaptive_template_pixels = adaptive_template_pixels;
  851. Optional<JBIG2::ArithmeticDecoder> decoder;
  852. if (!uses_mmr) {
  853. decoder = TRY(JBIG2::ArithmeticDecoder::initialize(data));
  854. inputs.arithmetic_decoder = &decoder.value();
  855. }
  856. auto result = TRY(generic_region_decoding_procedure(inputs, data, contexts));
  857. // 8.2 Page image composition step 5)
  858. if (information_field.x_location + information_field.width > (u32)context.page.size.width()
  859. || information_field.y_location + information_field.height > (u32)context.page.size.height()) {
  860. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Region bounds outsize of page bounds");
  861. }
  862. if (information_field.external_combination_operator() != CombinationOperator::Or
  863. && information_field.external_combination_operator() != CombinationOperator::Xor
  864. && information_field.external_combination_operator() != CombinationOperator::Replace)
  865. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle external combination operator other than OR, XOR, and REPLACE yet");
  866. for (size_t y = 0; y < information_field.height; ++y) {
  867. for (size_t x = 0; x < information_field.width; ++x) {
  868. // FIXME: Honor segment's combination operator instead of just copying.
  869. context.page.bits->set_bit(information_field.x_location + x, information_field.y_location + y, result->get_bit(x, y));
  870. }
  871. }
  872. return {};
  873. }
  874. static ErrorOr<void> decode_intermediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  875. {
  876. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic refinement region yet");
  877. }
  878. static ErrorOr<void> decode_immediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  879. {
  880. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic refinement region yet");
  881. }
  882. static ErrorOr<void> decode_immediate_lossless_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  883. {
  884. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic refinement region yet");
  885. }
  886. static ErrorOr<void> decode_page_information(JBIG2LoadingContext& context, SegmentData const& segment)
  887. {
  888. // 7.4.8 Page information segment syntax and 8.1 Decoder model steps 1) - 3).
  889. // "1) Decode the page information segment.""
  890. auto page_information = TRY(decode_page_information_segment(segment.data));
  891. bool page_is_striped = (page_information.striping_information & 0x80) != 0;
  892. if (page_information.bitmap_height == 0xffff'ffff && !page_is_striped)
  893. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Non-striped bitmaps of indeterminate height not allowed");
  894. u8 default_color = (page_information.flags >> 2) & 1;
  895. u8 default_combination_operator = (page_information.flags >> 3) & 3;
  896. context.page.default_combination_operator = static_cast<CombinationOperator>(default_combination_operator);
  897. // FIXME: Do something with the other fields in page_information.
  898. // "2) Create the page buffer, of the size given in the page information segment.
  899. //
  900. // If the page height is unknown, then this is not possible. However, in this case the page must be striped,
  901. // and the maximum stripe height specified, and the initial page buffer can be created with height initially
  902. // equal to this maximum stripe height."
  903. size_t height = page_information.bitmap_height;
  904. if (height == 0xffff'ffff)
  905. height = page_information.striping_information & 0x7F;
  906. context.page.bits = TRY(BitBuffer::create(page_information.bitmap_width, height));
  907. // "3) Fill the page buffer with the page's default pixel value."
  908. if (default_color == 1)
  909. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Can only handle white default color for now");
  910. context.page.bits->fill(default_color != 0);
  911. return {};
  912. }
  913. static ErrorOr<void> decode_end_of_page(JBIG2LoadingContext&, SegmentData const& segment)
  914. {
  915. // 7.4.9 End of page segment syntax
  916. if (segment.data.size() != 0)
  917. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of page segment has non-zero size");
  918. // FIXME: If the page had unknown height, check that previous segment was end-of-stripe.
  919. // FIXME: Maybe mark page as completed and error if we see more segments for it?
  920. return {};
  921. }
  922. static ErrorOr<void> decode_end_of_stripe(JBIG2LoadingContext&, SegmentData const&)
  923. {
  924. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of stripe yet");
  925. }
  926. static ErrorOr<void> decode_end_of_file(JBIG2LoadingContext&, SegmentData const& segment)
  927. {
  928. // 7.4.11 End of file segment syntax
  929. if (segment.data.size() != 0)
  930. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment has non-zero size");
  931. return {};
  932. }
  933. static ErrorOr<void> decode_profiles(JBIG2LoadingContext&, SegmentData const&)
  934. {
  935. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode profiles yet");
  936. }
  937. static ErrorOr<void> decode_tables(JBIG2LoadingContext&, SegmentData const&)
  938. {
  939. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode tables yet");
  940. }
  941. static ErrorOr<void> decode_color_palette(JBIG2LoadingContext&, SegmentData const&)
  942. {
  943. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode color palette yet");
  944. }
  945. static ErrorOr<void> decode_extension(JBIG2LoadingContext&, SegmentData const& segment)
  946. {
  947. // 7.4.14 Extension segment syntax
  948. FixedMemoryStream stream { segment.data };
  949. enum ExtensionType {
  950. SingleByteCodedComment = 0x20000000,
  951. MultiByteCodedComment = 0x20000002,
  952. };
  953. u32 type = TRY(stream.read_value<BigEndian<u32>>());
  954. auto read_string = [&]<class T>() -> ErrorOr<Vector<T>> {
  955. Vector<T> result;
  956. do {
  957. result.append(TRY(stream.read_value<BigEndian<T>>()));
  958. } while (result.last());
  959. result.take_last();
  960. return result;
  961. };
  962. switch (type) {
  963. case SingleByteCodedComment: {
  964. // 7.4.15.1 Single-byte coded comment
  965. // Pairs of zero-terminated ISO/IEC 8859-1 (latin1) pairs, terminated by another \0.
  966. while (true) {
  967. auto first_bytes = TRY(read_string.template operator()<u8>());
  968. if (first_bytes.is_empty())
  969. break;
  970. auto second_bytes = TRY(read_string.template operator()<u8>());
  971. auto first = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { first_bytes }));
  972. auto second = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { second_bytes }));
  973. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  974. }
  975. if (!stream.is_eof())
  976. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after SingleByteCodedComment");
  977. return {};
  978. }
  979. case MultiByteCodedComment: {
  980. // 7.4.15.2 Multi-byte coded comment
  981. // Pairs of (two-byte-)zero-terminated UCS-2 pairs, terminated by another \0\0.
  982. while (true) {
  983. auto first_ucs2 = TRY(read_string.template operator()<u16>());
  984. if (first_ucs2.is_empty())
  985. break;
  986. auto second_ucs2 = TRY(read_string.template operator()<u16>());
  987. auto first = TRY(Utf16View(first_ucs2).to_utf8());
  988. auto second = TRY(Utf16View(second_ucs2).to_utf8());
  989. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  990. }
  991. if (!stream.is_eof())
  992. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after MultiByteCodedComment");
  993. return {};
  994. }
  995. }
  996. // FIXME: If bit 31 in `type` is not set, the extension isn't necessary, and we could ignore it.
  997. dbgln("JBIG2ImageDecoderPlugin: Unknown extension type {:#x}", type);
  998. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown extension type");
  999. }
  1000. static ErrorOr<void> decode_data(JBIG2LoadingContext& context)
  1001. {
  1002. TRY(warn_about_multiple_pages(context));
  1003. for (size_t i = 0; i < context.segments.size(); ++i) {
  1004. auto const& segment = context.segments[i];
  1005. if (segment.header.page_association != 0 && segment.header.page_association != 1)
  1006. continue;
  1007. switch (segment.header.type) {
  1008. case SegmentType::SymbolDictionary:
  1009. TRY(decode_symbol_dictionary(context, segment));
  1010. break;
  1011. case SegmentType::IntermediateTextRegion:
  1012. TRY(decode_intermediate_text_region(context, segment));
  1013. break;
  1014. case SegmentType::ImmediateTextRegion:
  1015. TRY(decode_immediate_text_region(context, segment));
  1016. break;
  1017. case SegmentType::ImmediateLosslessTextRegion:
  1018. TRY(decode_immediate_lossless_text_region(context, segment));
  1019. break;
  1020. case SegmentType::PatternDictionary:
  1021. TRY(decode_pattern_dictionary(context, segment));
  1022. break;
  1023. case SegmentType::IntermediateHalftoneRegion:
  1024. TRY(decode_intermediate_halftone_region(context, segment));
  1025. break;
  1026. case SegmentType::ImmediateHalftoneRegion:
  1027. TRY(decode_immediate_halftone_region(context, segment));
  1028. break;
  1029. case SegmentType::ImmediateLosslessHalftoneRegion:
  1030. TRY(decode_immediate_lossless_halftone_region(context, segment));
  1031. break;
  1032. case SegmentType::IntermediateGenericRegion:
  1033. TRY(decode_intermediate_generic_region(context, segment));
  1034. break;
  1035. case SegmentType::ImmediateGenericRegion:
  1036. case SegmentType::ImmediateLosslessGenericRegion:
  1037. // 7.4.6 Generic region segment syntax
  1038. // "The data parts of all three of the generic region segment types ("intermediate generic region", "immediate generic region" and
  1039. // "immediate lossless generic region") are coded identically, but are acted upon differently, see 8.2."
  1040. // But 8.2 only describes a difference between intermediate and immediate regions as far as I can tell,
  1041. // and calling the immediate generic region handler for immediate generic lossless regions seems to do the right thing (?).
  1042. TRY(decode_immediate_generic_region(context, segment));
  1043. break;
  1044. case SegmentType::IntermediateGenericRefinementRegion:
  1045. TRY(decode_intermediate_generic_refinement_region(context, segment));
  1046. break;
  1047. case SegmentType::ImmediateGenericRefinementRegion:
  1048. TRY(decode_immediate_generic_refinement_region(context, segment));
  1049. break;
  1050. case SegmentType::ImmediateLosslessGenericRefinementRegion:
  1051. TRY(decode_immediate_lossless_generic_refinement_region(context, segment));
  1052. break;
  1053. case SegmentType::PageInformation:
  1054. TRY(decode_page_information(context, segment));
  1055. break;
  1056. case SegmentType::EndOfPage:
  1057. TRY(decode_end_of_page(context, segment));
  1058. break;
  1059. case SegmentType::EndOfStripe:
  1060. TRY(decode_end_of_stripe(context, segment));
  1061. break;
  1062. case SegmentType::EndOfFile:
  1063. TRY(decode_end_of_file(context, segment));
  1064. // "If a file contains an end of file segment, it must be the last segment."
  1065. if (i != context.segments.size() - 1)
  1066. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment not last segment");
  1067. break;
  1068. case SegmentType::Profiles:
  1069. TRY(decode_profiles(context, segment));
  1070. break;
  1071. case SegmentType::Tables:
  1072. TRY(decode_tables(context, segment));
  1073. break;
  1074. case SegmentType::ColorPalette:
  1075. TRY(decode_color_palette(context, segment));
  1076. break;
  1077. case SegmentType::Extension:
  1078. TRY(decode_extension(context, segment));
  1079. break;
  1080. }
  1081. }
  1082. return {};
  1083. }
  1084. JBIG2ImageDecoderPlugin::JBIG2ImageDecoderPlugin()
  1085. {
  1086. m_context = make<JBIG2LoadingContext>();
  1087. }
  1088. IntSize JBIG2ImageDecoderPlugin::size()
  1089. {
  1090. return m_context->page.size;
  1091. }
  1092. bool JBIG2ImageDecoderPlugin::sniff(ReadonlyBytes data)
  1093. {
  1094. return data.starts_with(id_string);
  1095. }
  1096. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JBIG2ImageDecoderPlugin::create(ReadonlyBytes data)
  1097. {
  1098. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  1099. TRY(decode_jbig2_header(*plugin->m_context, data));
  1100. data = data.slice(sizeof(id_string) + sizeof(u8) + (plugin->m_context->number_of_pages.has_value() ? sizeof(u32) : 0));
  1101. TRY(decode_segment_headers(*plugin->m_context, data));
  1102. TRY(scan_for_page_size(*plugin->m_context));
  1103. return plugin;
  1104. }
  1105. ErrorOr<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  1106. {
  1107. // FIXME: Use this for multi-page JBIG2 files?
  1108. if (index != 0)
  1109. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid frame index");
  1110. if (m_context->state == JBIG2LoadingContext::State::Error)
  1111. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoding failed");
  1112. if (m_context->state < JBIG2LoadingContext::State::Decoded) {
  1113. auto result = decode_data(*m_context);
  1114. if (result.is_error()) {
  1115. m_context->state = JBIG2LoadingContext::State::Error;
  1116. return result.release_error();
  1117. }
  1118. m_context->state = JBIG2LoadingContext::State::Decoded;
  1119. }
  1120. auto bitmap = TRY(m_context->page.bits->to_gfx_bitmap());
  1121. return ImageFrameDescriptor { move(bitmap), 0 };
  1122. }
  1123. ErrorOr<ByteBuffer> JBIG2ImageDecoderPlugin::decode_embedded(Vector<ReadonlyBytes> data)
  1124. {
  1125. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  1126. plugin->m_context->organization = Organization::Embedded;
  1127. for (auto const& segment_data : data)
  1128. TRY(decode_segment_headers(*plugin->m_context, segment_data));
  1129. TRY(scan_for_page_size(*plugin->m_context));
  1130. TRY(decode_data(*plugin->m_context));
  1131. return plugin->m_context->page.bits->to_byte_buffer();
  1132. }
  1133. }