JBIG2Loader.cpp 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  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 <LibGfx/ImageFormats/QMArithmeticDecoder.h>
  11. #include <LibTextCodec/Decoder.h>
  12. // Spec: ITU-T_T_88__08_2018.pdf in the zip file here:
  13. // https://www.itu.int/rec/T-REC-T.88-201808-I
  14. // Annex H has a datastream example.
  15. // That spec was published in 2018 and contains all previous amendments. Its history is:
  16. // * 2002: Original spec published, describes decoding only. Has generic regions,
  17. // symbol regions, text regions, halftone regions, and pattern regions.
  18. // * 2003: Amendment 1 approved. Describes encoding. Not interesting for us.
  19. // * 2004: (Amendment 1 erratum 1 approved. Not interesting for us.)
  20. // * 2003: Amendment 2 approved. Added support for EXTTEMPLATE.
  21. // * 2011: Amendment 3 approved. Added support for color coding
  22. // (COLEXTFLAG, CPCOMPLEN, CPDEFCOLS, CPEXCOLS, CPNCOMP, CPNVALS, GBCOLS,
  23. // GBCOMBOP, GBFGCOLID, SBCOLS, SBCOLSECTSIZE and SBFGCOLID).
  24. // This history might explain why EXTTEMPLATE and colors are very rare in practice.
  25. namespace Gfx {
  26. namespace JBIG2 {
  27. // Annex A, Arithmetic integer decoding procedure
  28. class ArithmeticIntegerDecoder {
  29. public:
  30. ArithmeticIntegerDecoder(QMArithmeticDecoder&);
  31. // A.2 Procedure for decoding values (except IAID)
  32. // Returns OptionalNone for OOB.
  33. Optional<i32> decode();
  34. // Returns Error for OOB.
  35. ErrorOr<i32> decode_non_oob();
  36. private:
  37. QMArithmeticDecoder& m_decoder;
  38. u16 PREV { 0 };
  39. Vector<QMArithmeticDecoder::Context> contexts;
  40. };
  41. ArithmeticIntegerDecoder::ArithmeticIntegerDecoder(QMArithmeticDecoder& decoder)
  42. : m_decoder(decoder)
  43. {
  44. contexts.resize(1 << 9);
  45. }
  46. Optional<int> ArithmeticIntegerDecoder::decode()
  47. {
  48. // A.2 Procedure for decoding values (except IAID)
  49. // "1) Set:
  50. // PREV = 1"
  51. u16 PREV = 1;
  52. // "2) Follow the flowchart in Figure A.1. Decode each bit with CX equal to "IAx + PREV" where "IAx" represents the identifier
  53. // of the current arithmetic integer decoding procedure, "+" represents concatenation, and the rightmost 9 bits of PREV are used."
  54. auto decode_bit = [&]() {
  55. bool D = m_decoder.get_next_bit(contexts[PREV & 0x1FF]);
  56. // "3) After each bit is decoded:
  57. // If PREV < 256 set:
  58. // PREV = (PREV << 1) OR D
  59. // Otherwise set:
  60. // PREV = (((PREV << 1) OR D) AND 511) OR 256
  61. // where D represents the value of the just-decoded bit.
  62. if (PREV < 256)
  63. PREV = (PREV << 1) | (u16)D;
  64. else
  65. PREV = (((PREV << 1) | (u16)D) & 511) | 256;
  66. return D;
  67. };
  68. auto decode_bits = [&](int n) {
  69. u32 result = 0;
  70. for (int i = 0; i < n; ++i)
  71. result = (result << 1) | decode_bit();
  72. return result;
  73. };
  74. // Figure A.1 – Flowchart for the integer arithmetic decoding procedures (except IAID)
  75. u8 S = decode_bit();
  76. u32 V;
  77. if (!decode_bit())
  78. V = decode_bits(2);
  79. else if (!decode_bit())
  80. V = decode_bits(4) + 4;
  81. else if (!decode_bit())
  82. V = decode_bits(6) + 20;
  83. else if (!decode_bit())
  84. V = decode_bits(8) + 84;
  85. else if (!decode_bit())
  86. V = decode_bits(12) + 340;
  87. else
  88. V = decode_bits(32) + 4436;
  89. // "4) The sequence of bits decoded, interpreted according to Table A.1, gives the value that is the result of this invocation
  90. // of the integer arithmetic decoding procedure."
  91. if (S == 1 && V == 0)
  92. return {};
  93. return S ? -V : V;
  94. }
  95. ErrorOr<i32> ArithmeticIntegerDecoder::decode_non_oob()
  96. {
  97. auto result = decode();
  98. if (!result.has_value())
  99. return Error::from_string_literal("ArithmeticIntegerDecoder: Unexpected OOB");
  100. return result.value();
  101. }
  102. class ArithmeticIntegerIDDecoder {
  103. public:
  104. ArithmeticIntegerIDDecoder(QMArithmeticDecoder&, u32 code_length);
  105. // A.3 The IAID decoding procedure
  106. u32 decode();
  107. private:
  108. QMArithmeticDecoder& m_decoder;
  109. u32 m_code_length { 0 };
  110. Vector<QMArithmeticDecoder::Context> contexts;
  111. };
  112. ArithmeticIntegerIDDecoder::ArithmeticIntegerIDDecoder(QMArithmeticDecoder& decoder, u32 code_length)
  113. : m_decoder(decoder)
  114. , m_code_length(code_length)
  115. {
  116. contexts.resize(1 << (code_length + 1));
  117. }
  118. u32 ArithmeticIntegerIDDecoder::decode()
  119. {
  120. // A.3 The IAID decoding procedure
  121. u32 prev = 1;
  122. for (u8 i = 0; i < m_code_length; ++i) {
  123. bool bit = m_decoder.get_next_bit(contexts[prev]);
  124. prev = (prev << 1) | bit;
  125. }
  126. prev = prev - (1 << m_code_length);
  127. return prev;
  128. }
  129. }
  130. static u8 number_of_context_bits_for_template(u8 template_)
  131. {
  132. if (template_ == 0)
  133. return 16;
  134. if (template_ == 1)
  135. return 13;
  136. VERIFY(template_ == 2 || template_ == 3);
  137. return 10;
  138. }
  139. // JBIG2 spec, Annex D, D.4.1 ID string
  140. static constexpr u8 id_string[] = { 0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A };
  141. // 7.3 Segment types
  142. enum SegmentType {
  143. SymbolDictionary = 0,
  144. IntermediateTextRegion = 4,
  145. ImmediateTextRegion = 6,
  146. ImmediateLosslessTextRegion = 7,
  147. PatternDictionary = 16,
  148. IntermediateHalftoneRegion = 20,
  149. ImmediateHalftoneRegion = 22,
  150. ImmediateLosslessHalftoneRegion = 23,
  151. IntermediateGenericRegion = 36,
  152. ImmediateGenericRegion = 38,
  153. ImmediateLosslessGenericRegion = 39,
  154. IntermediateGenericRefinementRegion = 40,
  155. ImmediateGenericRefinementRegion = 42,
  156. ImmediateLosslessGenericRefinementRegion = 43,
  157. PageInformation = 48,
  158. EndOfPage = 49,
  159. EndOfStripe = 50,
  160. EndOfFile = 51,
  161. Profiles = 52,
  162. Tables = 53,
  163. ColorPalette = 54,
  164. Extension = 62,
  165. };
  166. // Annex D
  167. enum class Organization {
  168. // D.1 Sequential organization
  169. Sequential,
  170. // D.2 Random-access organization
  171. RandomAccess,
  172. // D.3 Embedded organization
  173. Embedded,
  174. };
  175. struct SegmentHeader {
  176. u32 segment_number { 0 };
  177. SegmentType type { SegmentType::Extension };
  178. Vector<u32> referred_to_segment_numbers;
  179. // 7.2.6 Segment page association
  180. // "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."
  181. u32 page_association { 0 };
  182. Optional<u32> data_length;
  183. };
  184. class BitBuffer {
  185. public:
  186. static ErrorOr<NonnullOwnPtr<BitBuffer>> create(size_t width, size_t height);
  187. bool get_bit(size_t x, size_t y) const;
  188. void set_bit(size_t x, size_t y, bool b);
  189. void fill(bool b);
  190. ErrorOr<NonnullOwnPtr<BitBuffer>> subbitmap(Gfx::IntRect const& rect) const;
  191. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_gfx_bitmap() const;
  192. ErrorOr<ByteBuffer> to_byte_buffer() const;
  193. size_t width() const { return m_width; }
  194. size_t height() const { return m_height; }
  195. private:
  196. BitBuffer(ByteBuffer, size_t width, size_t height, size_t pitch);
  197. ByteBuffer m_bits;
  198. size_t m_width { 0 };
  199. size_t m_height { 0 };
  200. size_t m_pitch { 0 };
  201. };
  202. ErrorOr<NonnullOwnPtr<BitBuffer>> BitBuffer::create(size_t width, size_t height)
  203. {
  204. size_t pitch = ceil_div(width, static_cast<size_t>(8));
  205. auto bits = TRY(ByteBuffer::create_uninitialized(pitch * height));
  206. return adopt_nonnull_own_or_enomem(new (nothrow) BitBuffer(move(bits), width, height, pitch));
  207. }
  208. bool BitBuffer::get_bit(size_t x, size_t y) const
  209. {
  210. VERIFY(x < m_width);
  211. VERIFY(y < m_height);
  212. size_t byte_offset = x / 8;
  213. size_t bit_offset = x % 8;
  214. u8 byte = m_bits[y * m_pitch + byte_offset];
  215. byte = (byte >> (8 - 1 - bit_offset)) & 1;
  216. return byte != 0;
  217. }
  218. void BitBuffer::set_bit(size_t x, size_t y, bool b)
  219. {
  220. VERIFY(x < m_width);
  221. VERIFY(y < m_height);
  222. size_t byte_offset = x / 8;
  223. size_t bit_offset = x % 8;
  224. u8 byte = m_bits[y * m_pitch + byte_offset];
  225. u8 mask = 1u << (8 - 1 - bit_offset);
  226. if (b)
  227. byte |= mask;
  228. else
  229. byte &= ~mask;
  230. m_bits[y * m_pitch + byte_offset] = byte;
  231. }
  232. void BitBuffer::fill(bool b)
  233. {
  234. u8 fill_byte = b ? 0xff : 0;
  235. for (auto& byte : m_bits.bytes())
  236. byte = fill_byte;
  237. }
  238. ErrorOr<NonnullOwnPtr<BitBuffer>> BitBuffer::subbitmap(Gfx::IntRect const& rect) const
  239. {
  240. VERIFY(rect.x() >= 0);
  241. VERIFY(rect.width() >= 0);
  242. VERIFY(static_cast<size_t>(rect.right()) <= width());
  243. VERIFY(rect.y() >= 0);
  244. VERIFY(rect.height() >= 0);
  245. VERIFY(static_cast<size_t>(rect.bottom()) <= height());
  246. auto subbitmap = TRY(create(rect.width(), rect.height()));
  247. for (int y = 0; y < rect.height(); ++y)
  248. for (int x = 0; x < rect.width(); ++x)
  249. subbitmap->set_bit(x, y, get_bit(rect.x() + x, rect.y() + y));
  250. return subbitmap;
  251. }
  252. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> BitBuffer::to_gfx_bitmap() const
  253. {
  254. auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { m_width, m_height }));
  255. for (size_t y = 0; y < m_height; ++y) {
  256. for (size_t x = 0; x < m_width; ++x) {
  257. auto color = get_bit(x, y) ? Color::Black : Color::White;
  258. bitmap->set_pixel(x, y, color);
  259. }
  260. }
  261. return bitmap;
  262. }
  263. ErrorOr<ByteBuffer> BitBuffer::to_byte_buffer() const
  264. {
  265. return ByteBuffer::copy(m_bits);
  266. }
  267. BitBuffer::BitBuffer(ByteBuffer bits, size_t width, size_t height, size_t pitch)
  268. : m_bits(move(bits))
  269. , m_width(width)
  270. , m_height(height)
  271. , m_pitch(pitch)
  272. {
  273. }
  274. class Symbol : public RefCounted<Symbol> {
  275. public:
  276. static NonnullRefPtr<Symbol> create(NonnullOwnPtr<BitBuffer> bitmap)
  277. {
  278. return adopt_ref(*new Symbol(move(bitmap)));
  279. }
  280. BitBuffer const& bitmap() const { return *m_bitmap; }
  281. private:
  282. Symbol(NonnullOwnPtr<BitBuffer> bitmap)
  283. : m_bitmap(move(bitmap))
  284. {
  285. }
  286. NonnullOwnPtr<BitBuffer> m_bitmap;
  287. };
  288. struct SegmentData {
  289. SegmentHeader header;
  290. ReadonlyBytes data;
  291. // Set on dictionary segments after they've been decoded.
  292. Optional<Vector<NonnullRefPtr<Symbol>>> symbols;
  293. // Set on pattern segments after they've been decoded.
  294. Optional<Vector<NonnullRefPtr<Symbol>>> patterns;
  295. };
  296. // 7.4.8.5 Page segment flags
  297. enum class CombinationOperator {
  298. Or = 0,
  299. And = 1,
  300. Xor = 2,
  301. XNor = 3,
  302. Replace = 4,
  303. };
  304. static void composite_bitbuffer(BitBuffer& out, BitBuffer const& bitmap, Gfx::IntPoint position, CombinationOperator operator_)
  305. {
  306. if (!IntRect { position, { bitmap.width(), bitmap.height() } }.intersects(IntRect { { 0, 0 }, { out.width(), out.height() } }))
  307. return;
  308. size_t start_x = 0, end_x = bitmap.width();
  309. size_t start_y = 0, end_y = bitmap.height();
  310. if (position.x() < 0) {
  311. start_x = -position.x();
  312. position.set_x(0);
  313. }
  314. if (position.y() < 0) {
  315. start_y = -position.y();
  316. position.set_y(0);
  317. }
  318. if (position.x() + bitmap.width() > out.width())
  319. end_x = out.width() - position.x();
  320. if (position.y() + bitmap.height() > out.height())
  321. end_y = out.height() - position.y();
  322. for (size_t y = start_y; y < end_y; ++y) {
  323. for (size_t x = start_x; x < end_x; ++x) {
  324. bool bit = bitmap.get_bit(x, y);
  325. switch (operator_) {
  326. case CombinationOperator::Or:
  327. bit = bit || out.get_bit(position.x() + x, position.y() + y);
  328. break;
  329. case CombinationOperator::And:
  330. bit = bit && out.get_bit(position.x() + x, position.y() + y);
  331. break;
  332. case CombinationOperator::Xor:
  333. bit = bit ^ out.get_bit(position.x() + x, position.y() + y);
  334. break;
  335. case CombinationOperator::XNor:
  336. bit = !(bit ^ out.get_bit(position.x() + x, position.y() + y));
  337. break;
  338. case CombinationOperator::Replace:
  339. // Nothing to do.
  340. break;
  341. }
  342. out.set_bit(position.x() + x, position.y() + y, bit);
  343. }
  344. }
  345. }
  346. struct Page {
  347. IntSize size;
  348. // This is never CombinationOperator::Replace for Pages.
  349. CombinationOperator default_combination_operator { CombinationOperator::Or };
  350. OwnPtr<BitBuffer> bits;
  351. };
  352. struct JBIG2LoadingContext {
  353. enum class State {
  354. NotDecoded = 0,
  355. Error,
  356. Decoded,
  357. };
  358. State state { State::NotDecoded };
  359. Organization organization { Organization::Sequential };
  360. Page page;
  361. Optional<u32> number_of_pages;
  362. Vector<SegmentData> segments;
  363. HashMap<u32, u32> segments_by_number;
  364. };
  365. static ErrorOr<void> decode_jbig2_header(JBIG2LoadingContext& context, ReadonlyBytes data)
  366. {
  367. if (!JBIG2ImageDecoderPlugin::sniff(data))
  368. return Error::from_string_literal("JBIG2LoadingContext: Invalid JBIG2 header");
  369. FixedMemoryStream stream(data.slice(sizeof(id_string)));
  370. // D.4.2 File header flags
  371. u8 header_flags = TRY(stream.read_value<u8>());
  372. if (header_flags & 0b11110000)
  373. return Error::from_string_literal("JBIG2LoadingContext: Invalid header flags");
  374. context.organization = (header_flags & 1) ? Organization::Sequential : Organization::RandomAccess;
  375. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Organization: {} ({})", (int)context.organization, context.organization == Organization::Sequential ? "Sequential" : "Random-access");
  376. bool has_known_number_of_pages = (header_flags & 2) ? false : true;
  377. bool uses_templates_with_12_AT_pixels = (header_flags & 4) ? true : false;
  378. bool contains_colored_region_segments = (header_flags & 8) ? true : false;
  379. // FIXME: Do something with these?
  380. (void)uses_templates_with_12_AT_pixels;
  381. (void)contains_colored_region_segments;
  382. // D.4.3 Number of pages
  383. if (has_known_number_of_pages) {
  384. context.number_of_pages = TRY(stream.read_value<BigEndian<u32>>());
  385. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Number of pages: {}", context.number_of_pages.value());
  386. }
  387. return {};
  388. }
  389. static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
  390. {
  391. // 7.2.2 Segment number
  392. u32 segment_number = TRY(stream.read_value<BigEndian<u32>>());
  393. dbgln_if(JBIG2_DEBUG, "Segment number: {}", segment_number);
  394. // 7.2.3 Segment header flags
  395. u8 flags = TRY(stream.read_value<u8>());
  396. SegmentType type = static_cast<SegmentType>(flags & 0b11'1111);
  397. dbgln_if(JBIG2_DEBUG, "Segment type: {}", (int)type);
  398. bool segment_page_association_size_is_32_bits = (flags & 0b100'0000) != 0;
  399. bool segment_retained_only_by_itself_and_extension_segments = (flags & 0b1000'00000) != 0;
  400. // FIXME: Do something with this?
  401. (void)segment_retained_only_by_itself_and_extension_segments;
  402. // 7.2.4 Referred-to segment count and retention flags
  403. u8 referred_to_segment_count_and_retention_flags = TRY(stream.read_value<u8>());
  404. u32 count_of_referred_to_segments = referred_to_segment_count_and_retention_flags >> 5;
  405. if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
  406. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid count_of_referred_to_segments");
  407. u32 extra_count = 0;
  408. if (count_of_referred_to_segments == 7) {
  409. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  410. count_of_referred_to_segments = TRY(stream.read_value<BigEndian<u32>>()) & 0x1FFF'FFFF;
  411. extra_count = ceil_div(count_of_referred_to_segments + 1, 8);
  412. TRY(stream.seek(extra_count, SeekMode::FromCurrentPosition));
  413. }
  414. dbgln_if(JBIG2_DEBUG, "Referred-to segment count: {}", count_of_referred_to_segments);
  415. // 7.2.5 Referred-to segment numbers
  416. Vector<u32> referred_to_segment_numbers;
  417. for (u32 i = 0; i < count_of_referred_to_segments; ++i) {
  418. u32 referred_to_segment_number;
  419. if (segment_number <= 256)
  420. referred_to_segment_number = TRY(stream.read_value<u8>());
  421. else if (segment_number <= 65536)
  422. referred_to_segment_number = TRY(stream.read_value<BigEndian<u16>>());
  423. else
  424. referred_to_segment_number = TRY(stream.read_value<BigEndian<u32>>());
  425. referred_to_segment_numbers.append(referred_to_segment_number);
  426. dbgln_if(JBIG2_DEBUG, "Referred-to segment number: {}", referred_to_segment_number);
  427. }
  428. // 7.2.6 Segment page association
  429. u32 segment_page_association;
  430. if (segment_page_association_size_is_32_bits) {
  431. segment_page_association = TRY(stream.read_value<BigEndian<u32>>());
  432. } else {
  433. segment_page_association = TRY(stream.read_value<u8>());
  434. }
  435. dbgln_if(JBIG2_DEBUG, "Segment page association: {}", segment_page_association);
  436. // 7.2.7 Segment data length
  437. u32 data_length = TRY(stream.read_value<BigEndian<u32>>());
  438. dbgln_if(JBIG2_DEBUG, "Segment data length: {}", data_length);
  439. // FIXME: Add some validity checks:
  440. // - check type is valid
  441. // - check referred_to_segment_numbers are smaller than segment_number
  442. // - 7.3.1 Rules for segment references
  443. // - 7.3.2 Rules for page associations
  444. Optional<u32> opt_data_length;
  445. if (data_length != 0xffff'ffff)
  446. opt_data_length = data_length;
  447. else if (type != ImmediateGenericRegion)
  448. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown data length only allowed for ImmediateGenericRegion");
  449. return SegmentHeader { segment_number, type, move(referred_to_segment_numbers), segment_page_association, opt_data_length };
  450. }
  451. static ErrorOr<size_t> scan_for_immediate_generic_region_size(ReadonlyBytes data)
  452. {
  453. // 7.2.7 Segment data length
  454. // "If the segment's type is "Immediate generic region", then the length field may contain the value 0xFFFFFFFF.
  455. // 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 (...).
  456. // In this case, the true length of the segment's data part shall be determined through examination of the data:
  457. // 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.
  458. // 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.
  459. // The form of encoding used by the segment may be determined by examining the eighteenth byte of its segment data part,
  460. // and the end sequences can occur anywhere after that eighteenth byte."
  461. // 7.4.6.4 Decoding a generic region segment
  462. // "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.
  463. // Thus, those sequences cannot occur by chance in the data that is decoded to generate the contents of the generic region."
  464. dbgln_if(JBIG2_DEBUG, "(Unknown data length, computing it)");
  465. if (data.size() < 19 + sizeof(u32))
  466. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Data too short to contain segment data header and end sequence");
  467. // Per 7.4.6.1 Generic region segment data header, this starts with the 17 bytes described in
  468. // 7.4.1 Region segment information field, followed the byte described in 7.4.6.2 Generic region segment flags.
  469. // That byte's lowest bit stores if the segment uses MMR.
  470. u8 flags = data[17];
  471. bool uses_mmr = (flags & 1) != 0;
  472. auto end_sequence = uses_mmr ? to_array<u8>({ 0x00, 0x00 }) : to_array<u8>({ 0xFF, 0xAC });
  473. u8 const* end = static_cast<u8 const*>(memmem(data.data() + 19, data.size() - 19 - sizeof(u32), end_sequence.data(), end_sequence.size()));
  474. if (!end)
  475. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Could not find end sequence in segment data");
  476. size_t size = end - data.data() + end_sequence.size() + sizeof(u32);
  477. dbgln_if(JBIG2_DEBUG, "(Computed size is {})", size);
  478. return size;
  479. }
  480. static ErrorOr<void> decode_segment_headers(JBIG2LoadingContext& context, ReadonlyBytes data)
  481. {
  482. FixedMemoryStream stream(data);
  483. Vector<ReadonlyBytes> segment_datas;
  484. auto store_and_skip_segment_data = [&](SegmentHeader const& segment_header) -> ErrorOr<void> {
  485. size_t start_offset = TRY(stream.tell());
  486. u32 data_length = TRY(segment_header.data_length.try_value_or_lazy_evaluated([&]() {
  487. return scan_for_immediate_generic_region_size(data.slice(start_offset));
  488. }));
  489. if (start_offset + data_length > data.size()) {
  490. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: start_offset={}, data_length={}, data.size()={}", start_offset, data_length, data.size());
  491. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment data length exceeds file size");
  492. }
  493. ReadonlyBytes segment_data = data.slice(start_offset, data_length);
  494. segment_datas.append(segment_data);
  495. TRY(stream.seek(data_length, SeekMode::FromCurrentPosition));
  496. return {};
  497. };
  498. Vector<SegmentHeader> segment_headers;
  499. while (!stream.is_eof()) {
  500. auto segment_header = TRY(decode_segment_header(stream));
  501. segment_headers.append(segment_header);
  502. if (context.organization != Organization::RandomAccess)
  503. TRY(store_and_skip_segment_data(segment_header));
  504. // Required per spec for files with RandomAccess organization.
  505. if (segment_header.type == SegmentType::EndOfFile)
  506. break;
  507. }
  508. if (context.organization == Organization::RandomAccess) {
  509. for (auto const& segment_header : segment_headers)
  510. TRY(store_and_skip_segment_data(segment_header));
  511. }
  512. if (segment_headers.size() != segment_datas.size())
  513. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment headers and segment datas have different sizes");
  514. for (size_t i = 0; i < segment_headers.size(); ++i) {
  515. context.segments.append({ segment_headers[i], segment_datas[i], {}, {} });
  516. context.segments_by_number.set(segment_headers[i].segment_number, context.segments.size() - 1);
  517. }
  518. return {};
  519. }
  520. // 7.4.1 Region segment information field
  521. struct [[gnu::packed]] RegionSegmentInformationField {
  522. BigEndian<u32> width;
  523. BigEndian<u32> height;
  524. BigEndian<u32> x_location;
  525. BigEndian<u32> y_location;
  526. u8 flags;
  527. CombinationOperator external_combination_operator() const
  528. {
  529. VERIFY((flags & 0x7) <= 4);
  530. return static_cast<CombinationOperator>(flags & 0x7);
  531. }
  532. bool is_color_bitmap() const
  533. {
  534. return (flags & 0x8) != 0;
  535. }
  536. };
  537. static_assert(AssertSize<RegionSegmentInformationField, 17>());
  538. static ErrorOr<RegionSegmentInformationField> decode_region_segment_information_field(ReadonlyBytes data)
  539. {
  540. // 7.4.8 Page information segment syntax
  541. if (data.size() < sizeof(RegionSegmentInformationField))
  542. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field size");
  543. auto result = *(RegionSegmentInformationField const*)data.data();
  544. if ((result.flags & 0b1111'0000) != 0)
  545. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field flags");
  546. if ((result.flags & 0x7) > 4)
  547. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field operator");
  548. // NOTE 3 – If the colour extension flag (COLEXTFLAG) is equal to 1, the external combination operator must be REPLACE.
  549. if (result.is_color_bitmap() && result.external_combination_operator() != CombinationOperator::Replace)
  550. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid colored region segment information field operator");
  551. return result;
  552. }
  553. // 7.4.8 Page information segment syntax
  554. struct [[gnu::packed]] PageInformationSegment {
  555. BigEndian<u32> bitmap_width;
  556. BigEndian<u32> bitmap_height;
  557. BigEndian<u32> page_x_resolution; // In pixels/meter.
  558. BigEndian<u32> page_y_resolution; // In pixels/meter.
  559. u8 flags;
  560. BigEndian<u16> striping_information;
  561. };
  562. static_assert(AssertSize<PageInformationSegment, 19>());
  563. static ErrorOr<PageInformationSegment> decode_page_information_segment(ReadonlyBytes data)
  564. {
  565. // 7.4.8 Page information segment syntax
  566. if (data.size() != sizeof(PageInformationSegment))
  567. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid page information segment size");
  568. return *(PageInformationSegment const*)data.data();
  569. }
  570. static ErrorOr<void> scan_for_page_size(JBIG2LoadingContext& context)
  571. {
  572. // We only decode the first page at the moment.
  573. bool found_size = false;
  574. for (auto const& segment : context.segments) {
  575. if (segment.header.type != SegmentType::PageInformation || segment.header.page_association != 1)
  576. continue;
  577. auto page_information = TRY(decode_page_information_segment(segment.data));
  578. // FIXME: We're supposed to compute this from the striping information if it's not set.
  579. if (page_information.bitmap_height == 0xffff'ffff)
  580. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle unknown page height yet");
  581. context.page.size = { page_information.bitmap_width, page_information.bitmap_height };
  582. found_size = true;
  583. }
  584. if (!found_size)
  585. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No page information segment found for page 1");
  586. return {};
  587. }
  588. static ErrorOr<void> warn_about_multiple_pages(JBIG2LoadingContext& context)
  589. {
  590. HashTable<u32> seen_pages;
  591. Vector<u32> pages;
  592. for (auto const& segment : context.segments) {
  593. if (segment.header.page_association == 0)
  594. continue;
  595. if (seen_pages.contains(segment.header.page_association))
  596. continue;
  597. seen_pages.set(segment.header.page_association);
  598. pages.append(segment.header.page_association);
  599. }
  600. // scan_for_page_size() already checked that there's a page 1.
  601. VERIFY(seen_pages.contains(1));
  602. if (pages.size() == 1)
  603. return {};
  604. StringBuilder builder;
  605. builder.appendff("JBIG2 file contains {} pages ({}", pages.size(), pages[0]);
  606. size_t i;
  607. for (i = 1; i < min(pages.size(), 10); ++i)
  608. builder.appendff(" {}", pages[i]);
  609. if (i != pages.size())
  610. builder.append(" ..."sv);
  611. builder.append("). We will only render page 1."sv);
  612. dbgln("JBIG2ImageDecoderPlugin: {}", TRY(builder.to_string()));
  613. return {};
  614. }
  615. struct AdaptiveTemplatePixel {
  616. i8 x { 0 };
  617. i8 y { 0 };
  618. };
  619. // Figure 7 – Field to which AT pixel locations are restricted
  620. static ErrorOr<void> check_valid_adaptive_template_pixel(AdaptiveTemplatePixel const& adaptive_template_pixel)
  621. {
  622. // Don't have to check < -127 or > 127: The offsets are stored in an i8, so they can't be out of those bounds.
  623. if (adaptive_template_pixel.y > 0)
  624. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel y too big");
  625. if (adaptive_template_pixel.y == 0 && adaptive_template_pixel.x > -1)
  626. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Adaptive pixel x too big");
  627. return {};
  628. }
  629. // 6.2.2 Input parameters
  630. // Table 2 – Parameters for the generic region decoding procedure
  631. struct GenericRegionDecodingInputParameters {
  632. bool is_modified_modified_read { false }; // "MMR" in spec.
  633. u32 region_width { 0 }; // "GBW" in spec.
  634. u32 region_height { 0 }; // "GBH" in spec.
  635. u8 gb_template { 0 };
  636. bool is_typical_prediction_used { false }; // "TPGDON" in spec.
  637. bool is_extended_reference_template_used { false }; // "EXTTEMPLATE" in spec.
  638. Optional<BitBuffer const&> skip_pattern; // "USESKIP", "SKIP" in spec.
  639. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels; // "GBATX" / "GBATY" in spec.
  640. // FIXME: GBCOLS, GBCOMBOP, COLEXTFLAG
  641. // If is_modified_modified_read is false, generic_region_decoding_procedure() reads data off this decoder.
  642. QMArithmeticDecoder* arithmetic_decoder { nullptr };
  643. };
  644. // 6.2 Generic region decoding procedure
  645. static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(GenericRegionDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<QMArithmeticDecoder::Context>& contexts)
  646. {
  647. if (inputs.is_modified_modified_read) {
  648. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: MMR image data");
  649. // 6.2.6 Decoding using MMR coding
  650. auto buffer = TRY(CCITT::decode_ccitt_group4(data, inputs.region_width, inputs.region_height));
  651. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  652. size_t bytes_per_row = ceil_div(inputs.region_width, 8);
  653. if (buffer.size() != bytes_per_row * inputs.region_height)
  654. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoded MMR data has wrong size");
  655. // FIXME: Could probably just copy the ByteBuffer directly into the BitBuffer's internal ByteBuffer instead.
  656. for (size_t y = 0; y < inputs.region_height; ++y) {
  657. for (size_t x = 0; x < inputs.region_width; ++x) {
  658. bool bit = buffer[y * bytes_per_row + x / 8] & (1 << (7 - x % 8));
  659. result->set_bit(x, y, bit);
  660. }
  661. }
  662. return result;
  663. }
  664. // 6.2.5 Decoding using a template and arithmetic coding
  665. if (inputs.is_extended_reference_template_used)
  666. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode EXTTEMPLATE yet");
  667. int number_of_adaptive_template_pixels = inputs.gb_template == 0 ? 4 : 1;
  668. for (int i = 0; i < number_of_adaptive_template_pixels; ++i)
  669. TRY(check_valid_adaptive_template_pixel(inputs.adaptive_template_pixels[i]));
  670. if (inputs.skip_pattern.has_value())
  671. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode USESKIP yet");
  672. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  673. static constexpr auto get_pixel = [](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> bool {
  674. if (x < 0 || x >= (int)buffer->width() || y < 0)
  675. return false;
  676. return buffer->get_bit(x, y);
  677. };
  678. // Figure 3(a) – Template when GBTEMPLATE = 0 and EXTTEMPLATE = 0,
  679. constexpr auto compute_context_0 = [](NonnullOwnPtr<BitBuffer> const& buffer, ReadonlySpan<AdaptiveTemplatePixel> adaptive_pixels, int x, int y) -> u16 {
  680. u16 result = 0;
  681. for (int i = 0; i < 4; ++i)
  682. result = (result << 1) | (u16)get_pixel(buffer, x + adaptive_pixels[i].x, y + adaptive_pixels[i].y);
  683. for (int i = 0; i < 3; ++i)
  684. result = (result << 1) | (u16)get_pixel(buffer, x - 1 + i, y - 2);
  685. for (int i = 0; i < 5; ++i)
  686. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y - 1);
  687. for (int i = 0; i < 4; ++i)
  688. result = (result << 1) | (u16)get_pixel(buffer, x - 4 + i, y);
  689. return result;
  690. };
  691. // Figure 4 – Template when GBTEMPLATE = 1
  692. auto compute_context_1 = [](NonnullOwnPtr<BitBuffer> const& buffer, ReadonlySpan<AdaptiveTemplatePixel> adaptive_pixels, int x, int y) -> u16 {
  693. u16 result = 0;
  694. result = (result << 1) | (u16)get_pixel(buffer, x + adaptive_pixels[0].x, y + adaptive_pixels[0].y);
  695. for (int i = 0; i < 4; ++i)
  696. result = (result << 1) | (u16)get_pixel(buffer, x - 1 + i, y - 2);
  697. for (int i = 0; i < 5; ++i)
  698. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y - 1);
  699. for (int i = 0; i < 3; ++i)
  700. result = (result << 1) | (u16)get_pixel(buffer, x - 3 + i, y);
  701. return result;
  702. };
  703. // Figure 5 – Template when GBTEMPLATE = 2
  704. auto compute_context_2 = [](NonnullOwnPtr<BitBuffer> const& buffer, ReadonlySpan<AdaptiveTemplatePixel> adaptive_pixels, int x, int y) -> u16 {
  705. u16 result = 0;
  706. result = (result << 1) | (u16)get_pixel(buffer, x + adaptive_pixels[0].x, y + adaptive_pixels[0].y);
  707. for (int i = 0; i < 3; ++i)
  708. result = (result << 1) | (u16)get_pixel(buffer, x - 1 + i, y - 2);
  709. for (int i = 0; i < 4; ++i)
  710. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y - 1);
  711. for (int i = 0; i < 2; ++i)
  712. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y);
  713. return result;
  714. };
  715. // Figure 6 – Template when GBTEMPLATE = 3
  716. auto compute_context_3 = [](NonnullOwnPtr<BitBuffer> const& buffer, ReadonlySpan<AdaptiveTemplatePixel> adaptive_pixels, int x, int y) -> u16 {
  717. u16 result = 0;
  718. result = (result << 1) | (u16)get_pixel(buffer, x + adaptive_pixels[0].x, y + adaptive_pixels[0].y);
  719. for (int i = 0; i < 5; ++i)
  720. result = (result << 1) | (u16)get_pixel(buffer, x - 3 + i, y - 1);
  721. for (int i = 0; i < 4; ++i)
  722. result = (result << 1) | (u16)get_pixel(buffer, x - 4 + i, y);
  723. return result;
  724. };
  725. u16 (*compute_context)(NonnullOwnPtr<BitBuffer> const&, ReadonlySpan<AdaptiveTemplatePixel>, int, int);
  726. if (inputs.gb_template == 0)
  727. compute_context = compute_context_0;
  728. else if (inputs.gb_template == 1)
  729. compute_context = compute_context_1;
  730. else if (inputs.gb_template == 2)
  731. compute_context = compute_context_2;
  732. else {
  733. VERIFY(inputs.gb_template == 3);
  734. compute_context = compute_context_3;
  735. }
  736. // "The values of the pixels in this neighbourhood define a context. Each context has its own adaptive probability estimate
  737. // used by the arithmetic coder (see Annex E)."
  738. // "* Decode the current pixel by invoking the arithmetic entropy decoding procedure, with CX set to the value formed by
  739. // concatenating the label "GB" and the 10-16 pixel values gathered in CONTEXT."
  740. // Implementor's note: What this is supposed to mean is that we have a bunch of independent contexts, and we pick the
  741. // context for the current pixel based on pixel values in the neighborhood. The "GB" part just means this context is
  742. // independent from other contexts in the spec. They are passed in to this function.
  743. // Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
  744. constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
  745. // Figure 9 – Reused context for coding the SLTP value when GBTEMPLATE is 1
  746. constexpr u16 sltp_context_for_template_1 = 0b0011'110010'101;
  747. // Figure 10 – Reused context for coding the SLTP value when GBTEMPLATE is 2
  748. constexpr u16 sltp_context_for_template_2 = 0b001'11001'01;
  749. // Figure 11 – Reused context for coding the SLTP value when GBTEMPLATE is 3
  750. constexpr u16 sltp_context_for_template_3 = 0b011001'0101;
  751. u16 sltp_context = [](u8 gb_template) {
  752. if (gb_template == 0)
  753. return sltp_context_for_template_0;
  754. if (gb_template == 1)
  755. return sltp_context_for_template_1;
  756. if (gb_template == 2)
  757. return sltp_context_for_template_2;
  758. VERIFY(gb_template == 3);
  759. return sltp_context_for_template_3;
  760. }(inputs.gb_template);
  761. // 6.2.5.7 Decoding the bitmap
  762. QMArithmeticDecoder& decoder = *inputs.arithmetic_decoder;
  763. bool ltp = false; // "LTP" in spec. "Line (uses) Typical Prediction" maybe?
  764. for (size_t y = 0; y < inputs.region_height; ++y) {
  765. if (inputs.is_typical_prediction_used) {
  766. // "SLTP" in spec. "Swap LTP" or "Switch LTP" maybe?
  767. bool sltp = decoder.get_next_bit(contexts[sltp_context]);
  768. ltp = ltp ^ sltp;
  769. if (ltp) {
  770. for (size_t x = 0; x < inputs.region_width; ++x)
  771. result->set_bit(x, y, get_pixel(result, (int)x, (int)y - 1));
  772. continue;
  773. }
  774. }
  775. for (size_t x = 0; x < inputs.region_width; ++x) {
  776. u16 context = compute_context(result, inputs.adaptive_template_pixels, x, y);
  777. bool bit = decoder.get_next_bit(contexts[context]);
  778. result->set_bit(x, y, bit);
  779. }
  780. }
  781. return result;
  782. }
  783. // 6.3.2 Input parameters
  784. // Table 6 – Parameters for the generic refinement region decoding procedure
  785. struct GenericRefinementRegionDecodingInputParameters {
  786. u32 region_width { 0 }; // "GRW" in spec.
  787. u32 region_height { 0 }; // "GRH" in spec.
  788. u8 gr_template { 0 }; // "GRTEMPLATE" in spec.
  789. BitBuffer const* reference_bitmap { nullptr }; // "GRREFERENCE" in spec.
  790. i32 reference_x_offset { 0 }; // "GRREFERENCEDX" in spec.
  791. i32 reference_y_offset { 0 }; // "GRREFERENCEDY" in spec.
  792. bool is_typical_prediction_used { false }; // "TPGDON" in spec.
  793. Array<AdaptiveTemplatePixel, 2> adaptive_template_pixels; // "GRATX" / "GRATY" in spec.
  794. };
  795. // 6.3 Generic Refinement Region Decoding Procedure
  796. static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_refinement_region_decoding_procedure(GenericRefinementRegionDecodingInputParameters& inputs, QMArithmeticDecoder& decoder, Vector<QMArithmeticDecoder::Context>& contexts)
  797. {
  798. VERIFY(inputs.gr_template == 0 || inputs.gr_template == 1);
  799. if (inputs.is_typical_prediction_used)
  800. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode typical prediction in generic refinement regions yet");
  801. if (inputs.gr_template == 0) {
  802. TRY(check_valid_adaptive_template_pixel(inputs.adaptive_template_pixels[0]));
  803. // inputs.adaptive_template_pixels[1] is allowed to contain any value.
  804. }
  805. // GRTEMPLATE 1 never uses adaptive pixels.
  806. // 6.3.5.3 Fixed templates and adaptive templates
  807. static constexpr auto get_pixel = [](BitBuffer const& buffer, int x, int y) -> bool {
  808. if (x < 0 || x >= (int)buffer.width() || y < 0 || y >= (int)buffer.height())
  809. return false;
  810. return buffer.get_bit(x, y);
  811. };
  812. // Figure 12 – 13-pixel refinement template showing the AT pixels at their nominal locations
  813. constexpr auto compute_context_0 = [](ReadonlySpan<AdaptiveTemplatePixel> adaptive_pixels, BitBuffer const& reference, int reference_x, int reference_y, BitBuffer const& buffer, int x, int y) -> u16 {
  814. u16 result = 0;
  815. for (int dy = -1; dy <= 1; ++dy) {
  816. for (int dx = -1; dx <= 1; ++dx) {
  817. if (dy == -1 && dx == -1)
  818. result = (result << 1) | (u16)get_pixel(reference, reference_x + adaptive_pixels[1].x, reference_y + adaptive_pixels[1].y);
  819. else
  820. result = (result << 1) | (u16)get_pixel(reference, reference_x + dx, reference_y + dy);
  821. }
  822. }
  823. result = (result << 1) | (u16)get_pixel(buffer, x + adaptive_pixels[0].x, y + adaptive_pixels[0].y);
  824. for (int i = 0; i < 2; ++i)
  825. result = (result << 1) | (u16)get_pixel(buffer, x + i, y - 1);
  826. result = (result << 1) | (u16)get_pixel(buffer, x - 1, y);
  827. return result;
  828. };
  829. // Figure 13 – 10-pixel refinement template
  830. constexpr auto compute_context_1 = [](ReadonlySpan<AdaptiveTemplatePixel>, BitBuffer const& reference, int reference_x, int reference_y, BitBuffer const& buffer, int x, int y) -> u16 {
  831. u16 result = 0;
  832. for (int dy = -1; dy <= 1; ++dy) {
  833. for (int dx = -1; dx <= 1; ++dx) {
  834. if ((dy == -1 && (dx == -1 || dx == 1)) || (dy == 1 && dx == -1))
  835. continue;
  836. result = (result << 1) | (u16)get_pixel(reference, reference_x + dx, reference_y + dy);
  837. }
  838. }
  839. for (int i = 0; i < 3; ++i)
  840. result = (result << 1) | (u16)get_pixel(buffer, x - 1 + i, y - 1);
  841. result = (result << 1) | (u16)get_pixel(buffer, x - 1, y);
  842. return result;
  843. };
  844. auto compute_context = inputs.gr_template == 0 ? compute_context_0 : compute_context_1;
  845. // 6.3.5.6 Decoding the refinement bitmap
  846. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  847. for (size_t y = 0; y < result->height(); ++y) {
  848. for (size_t x = 0; x < result->width(); ++x) {
  849. u16 context = compute_context(inputs.adaptive_template_pixels, *inputs.reference_bitmap, x - inputs.reference_x_offset, y - inputs.reference_y_offset, *result, x, y);
  850. bool bit = decoder.get_next_bit(contexts[context]);
  851. result->set_bit(x, y, bit);
  852. }
  853. }
  854. return result;
  855. }
  856. // 6.4.2 Input parameters
  857. // Table 9 – Parameters for the text region decoding procedure
  858. struct TextRegionDecodingInputParameters {
  859. bool uses_huffman_encoding { false }; // "SBHUFF" in spec.
  860. bool uses_refinement_coding { false }; // "SBREFINE" in spec.
  861. u32 region_width { 0 }; // "SBW" in spec.
  862. u32 region_height { 0 }; // "SBH" in spec.
  863. u32 number_of_instances { 0 }; // "SBNUMINSTANCES" in spec.
  864. u32 size_of_symbol_instance_strips { 0 }; // "SBSTRIPS" in spec.
  865. // "SBNUMSYMS" is `symbols.size()` below.
  866. // FIXME: SBSYMCODES
  867. u32 id_symbol_code_length { 0 }; // "SBSYMCODELEN" in spec.
  868. Vector<NonnullRefPtr<Symbol>> symbols; // "SBNUMSYMS" / "SBSYMS" in spec.
  869. u8 default_pixel { 0 }; // "SBDEFPIXEL" in spec.
  870. CombinationOperator operator_ { CombinationOperator::Or }; // "SBCOMBOP" in spec.
  871. bool is_transposed { false }; // "TRANSPOSED" in spec.
  872. enum class Corner {
  873. BottomLeft = 0,
  874. TopLeft = 1,
  875. BottomRight = 2,
  876. TopRight = 3,
  877. };
  878. Corner reference_corner { Corner::TopLeft }; // "REFCORNER" in spec.
  879. i8 delta_s_offset { 0 }; // "SBDSOFFSET" in spec.
  880. // FIXME: SBHUFFFS, SBHUFFFDS, SBHUFFDT, SBHUFFRDW, SBHUFFRDH, SBHUFFRDX, SBHUFFRDY, SBHUFFRSIZE
  881. u8 refinement_template { 0 }; // "SBRTEMPLATE" in spec.
  882. Array<AdaptiveTemplatePixel, 2> refinement_adaptive_template_pixels; // "SBRATX" / "SBRATY" in spec.
  883. // FIXME: COLEXTFLAG, SBCOLS
  884. };
  885. // 6.4 Text Region Decoding Procedure
  886. static ErrorOr<NonnullOwnPtr<BitBuffer>> text_region_decoding_procedure(TextRegionDecodingInputParameters const& inputs, ReadonlyBytes data)
  887. {
  888. if (inputs.uses_huffman_encoding)
  889. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman text regions yet");
  890. auto decoder = TRY(QMArithmeticDecoder::initialize(data));
  891. // 6.4.6 Strip delta T
  892. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFDT and multiply the resulting value by SBSTRIPS.
  893. // If SBHUFF is 0, decode a value using the IADT integer arithmetic decoding procedure (see Annex A) and multiply the resulting value by SBSTRIPS."
  894. // FIXME: Implement support for SBHUFF = 1.
  895. JBIG2::ArithmeticIntegerDecoder delta_t_integer_decoder(decoder);
  896. auto read_delta_t = [&]() -> ErrorOr<i32> {
  897. return TRY(delta_t_integer_decoder.decode_non_oob()) * inputs.size_of_symbol_instance_strips;
  898. };
  899. // 6.4.7 First symbol instance S coordinate
  900. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFFS.
  901. // If SBHUFF is 0, decode a value using the IAFS integer arithmetic decoding procedure (see Annex A)."
  902. // FIXME: Implement support for SBHUFF = 1.
  903. JBIG2::ArithmeticIntegerDecoder first_s_integer_decoder(decoder);
  904. auto read_first_s = [&]() -> ErrorOr<i32> {
  905. return first_s_integer_decoder.decode_non_oob();
  906. };
  907. // 6.4.8 Subsequent symbol instance S coordinate
  908. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFDS.
  909. // If SBHUFF is 0, decode a value using the IADS integer arithmetic decoding procedure (see Annex A).
  910. // In either case it is possible that the result of this decoding is the out-of-band value OOB.""
  911. // FIXME: Implement support for SBHUFF = 1.
  912. JBIG2::ArithmeticIntegerDecoder subsequent_s_integer_decoder(decoder);
  913. auto read_subsequent_s = [&]() -> Optional<i32> {
  914. return subsequent_s_integer_decoder.decode();
  915. };
  916. // 6.4.9 Symbol instance T coordinate
  917. // "If SBSTRIPS == 1, then the value decoded is always zero. Otherwise:
  918. // • If SBHUFF is 1, decode a value by reading ceil(log2(SBSTRIPS)) bits directly from the bitstream.
  919. // • If SBHUFF is 0, decode a value using the IAIT integer arithmetic decoding procedure (see Annex A)."
  920. // FIXME: Implement support for SBHUFF = 1.
  921. JBIG2::ArithmeticIntegerDecoder instance_t_integer_decoder(decoder);
  922. auto read_instance_t = [&]() -> ErrorOr<i32> {
  923. if (inputs.size_of_symbol_instance_strips == 1)
  924. return 0;
  925. return instance_t_integer_decoder.decode_non_oob();
  926. };
  927. // 6.4.10 Symbol instance symbol ID
  928. // "If SBHUFF is 1, decode a value by reading one bit at a time until the resulting bit string is equal to one of the entries in
  929. // SBSYMCODES. The resulting value, which is IDI, is the index of the entry in SBSYMCODES that is read.
  930. // If SBHUFF is 0, decode a value using the IAID integer arithmetic decoding procedure (see Annex A). Set IDI to the
  931. // resulting value.""
  932. // FIXME: Implement support for SBHUFF = 1.
  933. JBIG2::ArithmeticIntegerIDDecoder id_decoder(decoder, inputs.id_symbol_code_length);
  934. // 6.4.11.1 Symbol instance refinement delta width
  935. // FIXME: Implement support for SBHUFF = 1.
  936. JBIG2::ArithmeticIntegerDecoder refinement_delta_width_decoder(decoder);
  937. auto read_refinement_delta_width = [&]() -> ErrorOr<i32> {
  938. return refinement_delta_width_decoder.decode_non_oob();
  939. };
  940. // 6.4.11.2 Symbol instance refinement delta width
  941. // FIXME: Implement support for SBHUFF = 1.
  942. JBIG2::ArithmeticIntegerDecoder refinement_delta_height_decoder(decoder);
  943. auto read_refinement_delta_height = [&]() -> ErrorOr<i32> {
  944. return refinement_delta_height_decoder.decode_non_oob();
  945. };
  946. // 6.4.11.3 Symbol instance refinement X offset
  947. // FIXME: Implement support for SBHUFF = 1.
  948. JBIG2::ArithmeticIntegerDecoder refinement_x_offset_decoder(decoder);
  949. auto read_refinement_x_offset = [&]() -> ErrorOr<i32> {
  950. return refinement_x_offset_decoder.decode_non_oob();
  951. };
  952. // 6.4.11.4 Symbol instance refinement Y offset
  953. // FIXME: Implement support for SBHUFF = 1.
  954. JBIG2::ArithmeticIntegerDecoder refinement_y_offset_decoder(decoder);
  955. auto read_refinement_y_offset = [&]() -> ErrorOr<i32> {
  956. return refinement_y_offset_decoder.decode_non_oob();
  957. };
  958. // 6.4.11 Symbol instance bitmap
  959. JBIG2::ArithmeticIntegerDecoder has_refinement_image_decoder(decoder);
  960. Vector<QMArithmeticDecoder::Context> refinement_contexts;
  961. if (inputs.uses_refinement_coding)
  962. refinement_contexts.resize(1 << (inputs.refinement_template == 0 ? 13 : 10));
  963. OwnPtr<BitBuffer> refinement_result;
  964. auto read_bitmap = [&](u32 id) -> ErrorOr<BitBuffer const*> {
  965. if (id >= inputs.symbols.size())
  966. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Symbol ID out of range");
  967. auto const& symbol = inputs.symbols[id]->bitmap();
  968. bool has_refinement_image = false; // "R_I" in spec.
  969. if (inputs.uses_refinement_coding) {
  970. // "• If SBHUFF is 1, then read one bit and set RI to the value of that bit.
  971. // • If SBHUFF is 0, then decode one bit using the IARI integer arithmetic decoding procedure and set RI to the value of that bit."
  972. // FIXME: Implement support for SBHUFF = 1.
  973. has_refinement_image = TRY(has_refinement_image_decoder.decode_non_oob());
  974. }
  975. if (!has_refinement_image)
  976. return &symbol;
  977. auto refinement_delta_width = TRY(read_refinement_delta_width());
  978. auto refinement_delta_height = TRY(read_refinement_delta_height());
  979. auto refinement_x_offset = TRY(read_refinement_x_offset());
  980. auto refinement_y_offset = TRY(read_refinement_y_offset());
  981. // FIXME: This is missing some steps needed for the SBHUFF = 1 case.
  982. dbgln_if(JBIG2_DEBUG, "refinement delta width: {}, refinement delta height: {}, refinement x offset: {}, refinement y offset: {}", refinement_delta_width, refinement_delta_height, refinement_x_offset, refinement_y_offset);
  983. // Table 12 – Parameters used to decode a symbol instance's bitmap using refinement
  984. GenericRefinementRegionDecodingInputParameters refinement_inputs;
  985. refinement_inputs.region_width = symbol.width() + refinement_delta_width;
  986. refinement_inputs.region_height = symbol.height() + refinement_delta_height;
  987. refinement_inputs.gr_template = inputs.refinement_template;
  988. refinement_inputs.reference_bitmap = &symbol;
  989. refinement_inputs.reference_x_offset = refinement_delta_width / 2 + refinement_x_offset;
  990. refinement_inputs.reference_y_offset = refinement_delta_height / 2 + refinement_y_offset;
  991. refinement_inputs.is_typical_prediction_used = false;
  992. refinement_inputs.adaptive_template_pixels = inputs.refinement_adaptive_template_pixels;
  993. refinement_result = TRY(generic_refinement_region_decoding_procedure(refinement_inputs, decoder, refinement_contexts));
  994. return refinement_result.ptr();
  995. };
  996. // 6.4.5 Decoding the text region
  997. // "1) Fill a bitmap SBREG, of the size given by SBW and SBH, with the SBDEFPIXEL value."
  998. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  999. if (inputs.default_pixel != 0)
  1000. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle SBDEFPIXEL not equal to 0 yet");
  1001. result->fill(inputs.default_pixel != 0);
  1002. // "2) Decode the initial STRIPT value as described in 6.4.6. Negate the decoded value and assign this negated value to the variable STRIPT.
  1003. // Assign the value 0 to FIRSTS. Assign the value 0 to NINSTANCES."
  1004. i32 strip_t = -TRY(read_delta_t());
  1005. i32 first_s = 0;
  1006. u32 n_instances = 0;
  1007. // "3) If COLEXTFLAG is 1, decode the colour section as described in 6.4.12."
  1008. // FIXME: Implement support for colors one day.
  1009. // "4) Decode each strip as follows:
  1010. // a) If NINSTANCES is equal to SBNUMINSTANCES then there are no more strips to decode,
  1011. // and the process of decoding the text region is complete; proceed to step 4)."
  1012. // Implementor's note. The spec means "proceed to step 5)" at the end of 4a).
  1013. while (n_instances < inputs.number_of_instances) {
  1014. // "b) Decode the strip's delta T value as described in 6.4.6. Let DT be the decoded value. Set:
  1015. // STRIPT = STRIPT + DT"
  1016. i32 delta_t = TRY(read_delta_t());
  1017. strip_t += delta_t;
  1018. i32 cur_s;
  1019. bool is_first_symbol = true;
  1020. while (true) {
  1021. // "c) Decode each symbol instance in the strip as follows:
  1022. // i) If the current symbol instance is the first symbol instance in the strip, then decode the first
  1023. // symbol instance's S coordinate as described in 6.4.7. Let DFS be the decoded value. Set:
  1024. // FIRSTS = FIRSTS + DFS
  1025. // CURS = FIRSTS
  1026. // ii) Otherwise, if the current symbol instance is not the first symbol instance in the strip, decode
  1027. // the symbol instance's S coordinate as described in 6.4.8. If the result of this decoding is OOB
  1028. // then the last symbol instance of the strip has been decoded; proceed to step 3 d). Otherwise, let
  1029. // IDS be the decoded value. Set:
  1030. // CURS = CURS + IDS + SBDSOFFSET"
  1031. // Implementor's note: The spec means "proceed to step 4 d)" in 4c ii).
  1032. if (is_first_symbol) {
  1033. i32 delta_first_s = TRY(read_first_s());
  1034. first_s += delta_first_s;
  1035. cur_s = first_s;
  1036. is_first_symbol = false;
  1037. } else {
  1038. auto subsequent_s = read_subsequent_s();
  1039. if (!subsequent_s.has_value())
  1040. break;
  1041. i32 instance_delta_s = subsequent_s.value();
  1042. cur_s += instance_delta_s + inputs.delta_s_offset;
  1043. }
  1044. // "iii) Decode the symbol instance's T coordinate as described in 6.4.9. Let CURT be the decoded value. Set:
  1045. // TI = STRIPT + CURT"
  1046. i32 cur_t = TRY(read_instance_t());
  1047. i32 t_instance = strip_t + cur_t;
  1048. // "iv) Decode the symbol instance's symbol ID as described in 6.4.10. Let IDI be the decoded value."
  1049. u32 id = id_decoder.decode();
  1050. // "v) Determine the symbol instance's bitmap IBI as described in 6.4.11. The width and height of this
  1051. // bitmap shall be denoted as WI and HI respectively."
  1052. auto const& symbol = *TRY(read_bitmap(id));
  1053. // "vi) Update CURS as follows:
  1054. // • If TRANSPOSED is 0, and REFCORNER is TOPRIGHT or BOTTOMRIGHT, set:
  1055. // CURS = CURS + WI – 1
  1056. // • If TRANSPOSED is 1, and REFCORNER is BOTTOMLEFT or BOTTOMRIGHT, set:
  1057. // CURS = CURS + HI – 1
  1058. // • Otherwise, do not change CURS in this step."
  1059. using enum TextRegionDecodingInputParameters::Corner;
  1060. if (!inputs.is_transposed && (inputs.reference_corner == TopRight || inputs.reference_corner == BottomRight))
  1061. cur_s += symbol.width() - 1;
  1062. if (inputs.is_transposed && (inputs.reference_corner == BottomLeft || inputs.reference_corner == BottomRight))
  1063. cur_s += symbol.height() - 1;
  1064. // "vii) Set:
  1065. // SI = CURS"
  1066. auto s_instance = cur_s;
  1067. // "viii) Determine the location of the symbol instance bitmap with respect to SBREG as follows:
  1068. // • If TRANSPOSED is 0, then:
  1069. // – If REFCORNER is TOPLEFT then the top left pixel of the symbol instance bitmap
  1070. // IBI shall be placed at SBREG[SI, TI].
  1071. // – If REFCORNER is TOPRIGHT then the top right pixel of the symbol instance
  1072. // bitmap IBI shall be placed at SBREG[SI, TI].
  1073. // – If REFCORNER is BOTTOMLEFT then the bottom left pixel of the symbol
  1074. // instance bitmap IBI shall be placed at SBREG[SI, TI].
  1075. // – If REFCORNER is BOTTOMRIGHT then the bottom right pixel of the symbol
  1076. // instance bitmap IBI shall be placed at SBREG[SI, TI].
  1077. // • If TRANSPOSED is 1, then:
  1078. // – If REFCORNER is TOPLEFT then the top left pixel of the symbol instance bitmap
  1079. // IBI shall be placed at SBREG[TI, SI].
  1080. // – If REFCORNER is TOPRIGHT then the top right pixel of the symbol instance
  1081. // bitmap IBI shall be placed at SBREG[TI, SI].
  1082. // – If REFCORNER is BOTTOMLEFT then the bottom left pixel of the symbol
  1083. // instance bitmap IBI shall be placed at SBREG[TI, SI].
  1084. // – If REFCORNER is BOTTOMRIGHT then the bottom right pixel of the symbol
  1085. // instance bitmap IBI shall be placed at SBREG[TI, SI].
  1086. // If any part of IBI, when placed at this location, lies outside the bounds of SBREG, then ignore
  1087. // this part of IBI in step 3 c) ix)."
  1088. // Implementor's note: The spec means "ignore this part of IBI in step 3 c) x)" in 3c viii)'s last sentence.
  1089. if (inputs.is_transposed)
  1090. swap(s_instance, t_instance);
  1091. if (inputs.reference_corner == TopRight || inputs.reference_corner == BottomRight)
  1092. s_instance -= symbol.width() - 1;
  1093. if (inputs.reference_corner == BottomLeft || inputs.reference_corner == BottomRight)
  1094. t_instance -= symbol.height() - 1;
  1095. // "ix) If COLEXTFLAG is 1, set the colour specified by SBCOLS[SBFGCOLID[NINSTANCES]]
  1096. // to the foreground colour of the symbol instance bitmap IBI."
  1097. // FIXME: Implement support for colors one day.
  1098. // "x) Draw IBI into SBREG. Combine each pixel of IBI with the current value of the corresponding
  1099. // pixel in SBREG, using the combination operator specified by SBCOMBOP. Write the results
  1100. // of each combination into that pixel in SBREG."
  1101. dbgln_if(JBIG2_DEBUG, "combining symbol {} ({}x{}) at ({}, {}) with operator {}", id, symbol.width(), symbol.height(), s_instance, t_instance, (int)inputs.operator_);
  1102. composite_bitbuffer(*result, symbol, { s_instance, t_instance }, inputs.operator_);
  1103. // "xi) Update CURS as follows:
  1104. // • If TRANSPOSED is 0, and REFCORNER is TOPLEFT or BOTTOMLEFT, set:
  1105. // CURS = CURS + WI – 1
  1106. // • If TRANSPOSED is 1, and REFCORNER is TOPLEFT or TOPRIGHT, set:
  1107. // CURS = CURS + HI – 1
  1108. // • Otherwise, do not change CURS in this step."
  1109. if (!inputs.is_transposed && (inputs.reference_corner == TopLeft || inputs.reference_corner == BottomLeft))
  1110. cur_s += symbol.width() - 1;
  1111. if (inputs.is_transposed && (inputs.reference_corner == TopLeft || inputs.reference_corner == TopRight))
  1112. cur_s += symbol.height() - 1;
  1113. // "xii) Set:
  1114. // NINSTANCES = NINSTANCES + 1"
  1115. ++n_instances;
  1116. }
  1117. // "d) When the strip has been completely decoded, decode the next strip."
  1118. // (Done in the next loop iteration.)
  1119. }
  1120. // "5) After all the strips have been decoded, the current contents of SBREG are the results that shall be
  1121. // obtained by every decoder, whether it performs this exact sequence of steps or not."
  1122. return result;
  1123. }
  1124. // 6.5.2 Input parameters
  1125. // Table 13 – Parameters for the symbol dictionary decoding procedure
  1126. struct SymbolDictionaryDecodingInputParameters {
  1127. bool uses_huffman_encoding { false }; // "SDHUFF" in spec.
  1128. bool uses_refinement_or_aggregate_coding { false }; // "SDREFAGG" in spec.
  1129. Vector<NonnullRefPtr<Symbol>> input_symbols; // "SDNUMINSYMS", "SDINSYMS" in spec.
  1130. u32 number_of_new_symbols { 0 }; // "SDNUMNEWSYMS" in spec.
  1131. u32 number_of_exported_symbols { 0 }; // "SDNUMEXSYMS" in spec.
  1132. // FIXME: SDHUFFDH, SDHUFFDW, SDHUFFBMSIZE, SDHUFFAGGINST
  1133. u8 symbol_template { 0 }; // "SDTEMPLATE" in spec.
  1134. Array<AdaptiveTemplatePixel, 4> adaptive_template_pixels; // "SDATX" / "SDATY" in spec.
  1135. u8 refinement_template { 0 }; // "SDRTEMPLATE" in spec;
  1136. Array<AdaptiveTemplatePixel, 2> refinement_adaptive_template_pixels; // "SDRATX" / "SDRATY" in spec.
  1137. };
  1138. // 6.5 Symbol Dictionary Decoding Procedure
  1139. static ErrorOr<Vector<NonnullRefPtr<Symbol>>> symbol_dictionary_decoding_procedure(SymbolDictionaryDecodingInputParameters const& inputs, ReadonlyBytes data)
  1140. {
  1141. if (inputs.uses_huffman_encoding)
  1142. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman symbol dictionaries yet");
  1143. auto decoder = TRY(QMArithmeticDecoder::initialize(data));
  1144. Vector<QMArithmeticDecoder::Context> contexts;
  1145. contexts.resize(1 << number_of_context_bits_for_template(inputs.symbol_template));
  1146. // 6.5.6 Height class delta height
  1147. // "If SDHUFF is 1, decode a value using the Huffman table specified by SDHUFFDH.
  1148. // If SDHUFF is 0, decode a value using the IADH integer arithmetic decoding procedure (see Annex A)."
  1149. // FIXME: Implement support for SDHUFF = 1.
  1150. JBIG2::ArithmeticIntegerDecoder delta_height_integer_decoder(decoder);
  1151. auto read_delta_height = [&]() -> ErrorOr<i32> {
  1152. return delta_height_integer_decoder.decode_non_oob();
  1153. };
  1154. // 6.5.7 Delta width
  1155. // "If SDHUFF is 1, decode a value using the Huffman table specified by SDHUFFDW.
  1156. // If SDHUFF is 0, decode a value using the IADW integer arithmetic decoding procedure (see Annex A).
  1157. // In either case it is possible that the result of this decoding is the out-of-band value OOB."
  1158. // FIXME: Implement support for SDHUFF = 1.
  1159. JBIG2::ArithmeticIntegerDecoder delta_width_integer_decoder(decoder);
  1160. auto read_delta_width = [&]() -> Optional<i32> {
  1161. return delta_width_integer_decoder.decode();
  1162. };
  1163. // 6.5.8 Symbol bitmap
  1164. // "This field is only present if SDHUFF = 0 or SDREFAGG = 1. This field takes one of two forms; SDREFAGG
  1165. // determines which form is used."
  1166. // 6.5.8.2.1 Number of symbol instances in aggregation
  1167. // If SDHUFF is 1, decode a value using the Huffman table specified by SDHUFFAGGINST.
  1168. // If SDHUFF is 0, decode a value using the IAAI integer arithmetic decoding procedure (see Annex A).
  1169. // FIXME: Implement support for SDHUFF = 1.
  1170. Optional<JBIG2::ArithmeticIntegerDecoder> number_of_symbol_instances_decoder;
  1171. auto read_number_of_symbol_instances = [&]() -> ErrorOr<i32> {
  1172. if (!number_of_symbol_instances_decoder.has_value())
  1173. number_of_symbol_instances_decoder = JBIG2::ArithmeticIntegerDecoder(decoder);
  1174. return number_of_symbol_instances_decoder->decode_non_oob();
  1175. };
  1176. // 6.5.8.1 Direct-coded symbol bitmap
  1177. Optional<JBIG2::ArithmeticIntegerIDDecoder> id_decoder;
  1178. Optional<JBIG2::ArithmeticIntegerDecoder> refinement_x_offset_decoder;
  1179. Optional<JBIG2::ArithmeticIntegerDecoder> refinement_y_offset_decoder;
  1180. // FIXME: When we implement REFAGGNINST > 1 support, do these need to be shared with
  1181. // text_region_decoding_procedure() then?
  1182. Vector<QMArithmeticDecoder::Context> refinement_contexts;
  1183. // This belongs in 6.5.5 1) below, but also needs to be captured by read_bitmap here.
  1184. Vector<NonnullRefPtr<Symbol>> new_symbols;
  1185. auto read_symbol_bitmap = [&](u32 width, u32 height) -> ErrorOr<NonnullOwnPtr<BitBuffer>> {
  1186. // "If SDREFAGG is 0, then decode the symbol's bitmap using a generic region decoding procedure as described in 6.2.
  1187. // Set the parameters to this decoding procedure as shown in Table 16."
  1188. if (!inputs.uses_refinement_or_aggregate_coding) {
  1189. // Table 16 – Parameters used to decode a symbol's bitmap using generic bitmap decoding
  1190. GenericRegionDecodingInputParameters generic_inputs;
  1191. generic_inputs.is_modified_modified_read = false;
  1192. generic_inputs.region_width = width;
  1193. generic_inputs.region_height = height;
  1194. generic_inputs.gb_template = inputs.symbol_template;
  1195. generic_inputs.is_extended_reference_template_used = false; // Missing from spec in table 16.
  1196. for (int i = 0; i < 4; ++i)
  1197. generic_inputs.adaptive_template_pixels[i] = inputs.adaptive_template_pixels[i];
  1198. generic_inputs.arithmetic_decoder = &decoder;
  1199. return generic_region_decoding_procedure(generic_inputs, {}, contexts);
  1200. }
  1201. // 6.5.8.2 Refinement/aggregate-coded symbol bitmap
  1202. // "1) Decode the number of symbol instances contained in the aggregation, as specified in 6.5.8.2.1. Let REFAGGNINST be the value decoded."
  1203. auto number_of_symbol_instances = TRY(read_number_of_symbol_instances()); // "REFAGGNINST" in spec.
  1204. dbgln_if(JBIG2_DEBUG, "Number of symbol instances: {}", number_of_symbol_instances);
  1205. if (number_of_symbol_instances > 1) {
  1206. // "2) If REFAGGNINST is greater than one, then decode the bitmap itself using a text region decoding procedure
  1207. // as described in 6.4. Set the parameters to this decoding procedure as shown in Table 17."
  1208. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode symbol bitmaps with more than one symbol instance yet");
  1209. }
  1210. // "3) If REFAGGNINST is equal to one, then decode the bitmap as described in 6.5.8.2.2."
  1211. // 6.5.8.2.3 Setting SBSYMCODES and SBSYMCODELEN
  1212. // FIXME: Implement support for SDHUFF = 1
  1213. u32 code_length = ceil(log2(inputs.input_symbols.size() + inputs.number_of_new_symbols));
  1214. // 6.5.8.2.2 Decoding a bitmap when REFAGGNINST = 1
  1215. // FIXME: This is missing some steps for the SDHUFF = 1 case.
  1216. if (number_of_symbol_instances != 1)
  1217. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unexpected number of symbol instances");
  1218. if (!id_decoder.has_value())
  1219. id_decoder = JBIG2::ArithmeticIntegerIDDecoder(decoder, code_length);
  1220. u32 symbol_id = id_decoder->decode();
  1221. if (!refinement_x_offset_decoder.has_value())
  1222. refinement_x_offset_decoder = JBIG2::ArithmeticIntegerDecoder(decoder);
  1223. i32 refinement_x_offset = TRY(refinement_x_offset_decoder->decode_non_oob());
  1224. if (!refinement_y_offset_decoder.has_value())
  1225. refinement_y_offset_decoder = JBIG2::ArithmeticIntegerDecoder(decoder);
  1226. i32 refinement_y_offset = TRY(refinement_y_offset_decoder->decode_non_oob());
  1227. if (symbol_id >= inputs.input_symbols.size() && symbol_id - inputs.input_symbols.size() >= new_symbols.size())
  1228. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Refinement/aggregate symbol ID out of range");
  1229. auto IBO = (symbol_id < inputs.input_symbols.size()) ? inputs.input_symbols[symbol_id] : new_symbols[symbol_id - inputs.input_symbols.size()];
  1230. // Table 18 – Parameters used to decode a symbol's bitmap when REFAGGNINST = 1
  1231. GenericRefinementRegionDecodingInputParameters refinement_inputs;
  1232. refinement_inputs.region_width = width;
  1233. refinement_inputs.region_height = height;
  1234. refinement_inputs.gr_template = inputs.refinement_template;
  1235. refinement_inputs.reference_bitmap = &IBO->bitmap();
  1236. refinement_inputs.reference_x_offset = refinement_x_offset;
  1237. refinement_inputs.reference_y_offset = refinement_y_offset;
  1238. refinement_inputs.is_typical_prediction_used = false;
  1239. refinement_inputs.adaptive_template_pixels = inputs.refinement_adaptive_template_pixels;
  1240. if (refinement_contexts.is_empty())
  1241. refinement_contexts.resize(1 << (inputs.refinement_template == 0 ? 13 : 10));
  1242. return generic_refinement_region_decoding_procedure(refinement_inputs, decoder, refinement_contexts);
  1243. };
  1244. // 6.5.5 Decoding the symbol dictionary
  1245. // "1) Create an array SDNEWSYMS of bitmaps, having SDNUMNEWSYMS entries."
  1246. // Done above read_symbol_bitmap's definition.
  1247. // "2) If SDHUFF is 1 and SDREFAGG is 0, create an array SDNEWSYMWIDTHS of integers, having SDNUMNEWSYMS entries."
  1248. // FIXME: Implement support for SDHUFF = 1.
  1249. // "3) Set:
  1250. // HCHEIGHT = 0
  1251. // NSYMSDECODED = 0"
  1252. u32 height_class_height = 0;
  1253. u32 number_of_symbols_decoded = 0;
  1254. // "4) Decode each height class as follows:
  1255. // a) If NSYMSDECODED == SDNUMNEWSYMS then all the symbols in the dictionary have been decoded; proceed to step 5)."
  1256. while (number_of_symbols_decoded < inputs.number_of_new_symbols) {
  1257. // "b) Decode the height class delta height as described in 6.5.6. Let HCDH be the decoded value. Set:
  1258. // HCHEIGHT = HCEIGHT + HCDH
  1259. // SYMWIDTH = 0
  1260. // TOTWIDTH = 0
  1261. // HCFIRSTSYM = NSYMSDECODED"
  1262. i32 delta_height = TRY(read_delta_height());
  1263. height_class_height += delta_height;
  1264. u32 symbol_width = 0;
  1265. u32 total_width = 0;
  1266. u32 height_class_first_symbol = number_of_symbols_decoded;
  1267. // "c) Decode each symbol within the height class as follows:"
  1268. while (true) {
  1269. // "i) Decode the delta width for the symbol as described in 6.5.7."
  1270. auto opt_delta_width = read_delta_width();
  1271. // " If the result of this decoding is OOB then all the symbols in this height class have been decoded; proceed to step 4 d)."
  1272. if (!opt_delta_width.has_value())
  1273. break;
  1274. VERIFY(number_of_symbols_decoded < inputs.number_of_new_symbols);
  1275. // " Otherwise let DW be the decoded value and set:"
  1276. // SYMWIDTH = SYMWIDTH + DW
  1277. // TOTWIDTH = TOTWIDTH + SYMWIDTH"
  1278. i32 delta_width = opt_delta_width.value();
  1279. symbol_width += delta_width;
  1280. total_width += symbol_width;
  1281. // "ii) If SDHUFF is 0 or SDREFAGG is 1, then decode the symbol's bitmap as described in 6.5.8.
  1282. // Let BS be the decoded bitmap (this bitmap has width SYMWIDTH and height HCHEIGHT). Set:
  1283. // SDNEWSYMS[NSYMSDECODED] = BS"
  1284. // FIXME: Implement support for SDHUFF = 1.
  1285. // FIXME: Doing this eagerly is pretty wasteful. Decode on demand instead?
  1286. auto bitmap = TRY(read_symbol_bitmap(symbol_width, height_class_height));
  1287. new_symbols.append(Symbol::create(move(bitmap)));
  1288. // "iii) If SDHUFF is 1 and SDREFAGG is 0, then set:
  1289. // SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH"
  1290. // FIXME: Implement support for SDHUFF = 1.
  1291. (void)total_width;
  1292. (void)height_class_first_symbol;
  1293. // "iv) Set:
  1294. // NSYMSDECODED = NSYMSDECODED + 1"
  1295. number_of_symbols_decoded++;
  1296. }
  1297. // "d) If SDHUFF is 1 and SDREFAGG is 0, [...long text elided...]"
  1298. // FIXME: Implement support for SDHUFF = 1.
  1299. }
  1300. // "5) Determine which symbol bitmaps are exported from this symbol dictionary, as described in 6.5.10. These
  1301. // bitmaps can be drawn from the symbols that are used as input to the symbol dictionary decoding
  1302. // procedure as well as the new symbols produced by the decoding procedure."
  1303. JBIG2::ArithmeticIntegerDecoder export_integer_decoder(decoder);
  1304. // 6.5.10 Exported symbols
  1305. Vector<bool> export_flags;
  1306. export_flags.resize(inputs.input_symbols.size() + inputs.number_of_new_symbols);
  1307. // "1) Set:
  1308. // EXINDEX = 0
  1309. // CUREXFLAG = 0"
  1310. u32 exported_index = 0;
  1311. bool current_export_flag = false;
  1312. do {
  1313. // "2) Decode a value using Table B.1 if SDHUFF is 1, or the IAEX integer arithmetic decoding procedure if
  1314. // SDHUFF is 0. Let EXRUNLENGTH be the decoded value."
  1315. // FIXME: Implement support for SDHUFF = 1.
  1316. i32 export_run_length = TRY(export_integer_decoder.decode_non_oob());
  1317. // "3) Set EXFLAGS[EXINDEX] through EXFLAGS[EXINDEX + EXRUNLENGTH – 1] to CUREXFLAG.
  1318. // If EXRUNLENGTH = 0, then this step does not change any values."
  1319. for (int i = 0; i < export_run_length; ++i)
  1320. export_flags[exported_index + i] = current_export_flag;
  1321. // "4) Set:
  1322. // EXINDEX = EXINDEX + EXRUNLENGTH
  1323. // CUREXFLAG = NOT(CUREXFLAG)"
  1324. exported_index += export_run_length;
  1325. current_export_flag = !current_export_flag;
  1326. // 5) Repeat steps 2) through 4) until EXINDEX == SDNUMINSYMS + SDNUMNEWSYMS.
  1327. } while (exported_index < inputs.input_symbols.size() + inputs.number_of_new_symbols);
  1328. // "6) The array EXFLAGS now contains 1 for each symbol that is exported from the dictionary, and 0 for each
  1329. // symbol that is not exported."
  1330. Vector<NonnullRefPtr<Symbol>> exported_symbols;
  1331. // "7) Set:
  1332. // I = 0
  1333. // J = 0
  1334. // 8) For each value of I from 0 to SDNUMINSYMS + SDNUMNEWSYMS – 1,"
  1335. for (size_t i = 0; i < inputs.input_symbols.size() + inputs.number_of_new_symbols; ++i) {
  1336. // "if EXFLAGS[I] == 1 then perform the following steps:"
  1337. if (!export_flags[i])
  1338. continue;
  1339. // "a) If I < SDNUMINSYMS then set:
  1340. // SDEXSYMS[J] = SDINSYMS[I]
  1341. // J = J + 1"
  1342. if (i < inputs.input_symbols.size())
  1343. exported_symbols.append(inputs.input_symbols[i]);
  1344. // "b) If I >= SDNUMINSYMS then set:
  1345. // SDEXSYMS[J] = SDNEWSYMS[I – SDNUMINSYMS]
  1346. // J = J + 1"
  1347. if (i >= inputs.input_symbols.size())
  1348. exported_symbols.append(move(new_symbols[i - inputs.input_symbols.size()]));
  1349. }
  1350. if (exported_symbols.size() != inputs.number_of_exported_symbols)
  1351. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unexpected number of exported symbols");
  1352. return exported_symbols;
  1353. }
  1354. // Annex C Gray-scale image decoding procedure
  1355. // C.2 Input parameters
  1356. // Table C.1 – Parameters for the gray-scale image decoding procedure
  1357. struct GrayscaleInputParameters {
  1358. bool uses_mmr { false }; // "GSMMR" in spec.
  1359. Optional<BitBuffer const&> skip_pattern; // "GSUSESKIP" / "GSKIP" in spec.
  1360. u8 bpp { 0 }; // "GSBPP" in spec.
  1361. u32 width { 0 }; // "GSW" in spec.
  1362. u32 height { 0 }; // "GSH" in spec.
  1363. u8 template_id { 0 }; // "GSTEMPLATE" in spec.
  1364. // If uses_mmr is false, grayscale_image_decoding_procedure() reads data off this decoder.
  1365. QMArithmeticDecoder* arithmetic_decoder { nullptr };
  1366. };
  1367. static ErrorOr<Vector<u8>> grayscale_image_decoding_procedure(GrayscaleInputParameters const& inputs, ReadonlyBytes data, Vector<QMArithmeticDecoder::Context>& contexts)
  1368. {
  1369. // FIXME: Support this. generic_region_decoding_procedure() currently doesn't tell us how much data it
  1370. // reads for MMR bitmaps, so we can't currently read more than one MMR bitplane here.
  1371. if (inputs.uses_mmr)
  1372. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode MMR grayscale images yet");
  1373. // Table C.4 – Parameters used to decode a bitplane of the gray-scale image
  1374. GenericRegionDecodingInputParameters generic_inputs;
  1375. generic_inputs.is_modified_modified_read = inputs.uses_mmr;
  1376. generic_inputs.region_width = inputs.width;
  1377. generic_inputs.region_height = inputs.height;
  1378. generic_inputs.gb_template = inputs.template_id;
  1379. generic_inputs.is_typical_prediction_used = false;
  1380. generic_inputs.is_extended_reference_template_used = false; // Missing from spec.
  1381. generic_inputs.skip_pattern = inputs.skip_pattern;
  1382. generic_inputs.adaptive_template_pixels[0].x = inputs.template_id <= 1 ? 3 : 2;
  1383. generic_inputs.adaptive_template_pixels[0].y = -1;
  1384. generic_inputs.adaptive_template_pixels[1].x = -3;
  1385. generic_inputs.adaptive_template_pixels[1].y = -1;
  1386. generic_inputs.adaptive_template_pixels[2].x = 2;
  1387. generic_inputs.adaptive_template_pixels[2].y = -2;
  1388. generic_inputs.adaptive_template_pixels[3].x = -2;
  1389. generic_inputs.adaptive_template_pixels[3].y = -2;
  1390. generic_inputs.arithmetic_decoder = inputs.arithmetic_decoder;
  1391. // C.5 Decoding the gray-scale image
  1392. // "The gray-scale image is obtained by decoding GSBPP bitplanes. These bitplanes are denoted (from least significant to
  1393. // most significant) GSPLANES[0], GSPLANES[1], . . . , GSPLANES[GSBPP – 1]. The bitplanes are Gray-coded, so
  1394. // that each bitplane's true value is equal to its coded value XORed with the next-more-significant bitplane."
  1395. Vector<OwnPtr<BitBuffer>> bitplanes;
  1396. bitplanes.resize(inputs.bpp);
  1397. // "1) Decode GSPLANES[GSBPP – 1] using the generic region decoding procedure. The parameters to the
  1398. // generic region decoding procedure are as shown in Table C.4."
  1399. bitplanes[inputs.bpp - 1] = TRY(generic_region_decoding_procedure(generic_inputs, data, contexts));
  1400. // "2) Set J = GSBPP – 2."
  1401. int j = inputs.bpp - 2;
  1402. // "3) While J >= 0, perform the following steps:"
  1403. while (j >= 0) {
  1404. // "a) Decode GSPLANES[J] using the generic region decoding procedure. The parameters to the generic
  1405. // region decoding procedure are as shown in Table C.4."
  1406. bitplanes[j] = TRY(generic_region_decoding_procedure(generic_inputs, data, contexts));
  1407. // "b) For each pixel (x, y) in GSPLANES[J], set:
  1408. // GSPLANES[J][x, y] = GSPLANES[J + 1][x, y] XOR GSPLANES[J][x, y]"
  1409. for (u32 y = 0; y < inputs.height; ++y) {
  1410. for (u32 x = 0; x < inputs.width; ++x) {
  1411. bool bit = bitplanes[j + 1]->get_bit(x, y) ^ bitplanes[j]->get_bit(x, y);
  1412. bitplanes[j]->set_bit(x, y, bit);
  1413. }
  1414. }
  1415. // "c) Set J = J – 1."
  1416. j = j - 1;
  1417. }
  1418. // "4) For each (x, y), set:
  1419. // GSVALS [x, y] = sum_{J = 0}^{GSBPP - 1} GSPLANES[J][x,y] × 2**J)"
  1420. Vector<u8> result;
  1421. result.resize(inputs.width * inputs.height);
  1422. for (u32 y = 0; y < inputs.height; ++y) {
  1423. for (u32 x = 0; x < inputs.width; ++x) {
  1424. u8 value = 0;
  1425. for (int j = 0; j < inputs.bpp; ++j) {
  1426. if (bitplanes[j]->get_bit(x, y))
  1427. value |= 1 << j;
  1428. }
  1429. result[y * inputs.width + x] = value;
  1430. }
  1431. }
  1432. return result;
  1433. }
  1434. // 6.6.2 Input parameters
  1435. // Table 20 – Parameters for the halftone region decoding procedure
  1436. struct HalftoneRegionDecodingInputParameters {
  1437. u32 region_width { 0 }; // "HBW" in spec.
  1438. u32 region_height { 0 }; // "HBH" in spec.
  1439. bool uses_mmr { false }; // "HMMR" in spec.
  1440. u8 halftone_template { 0 }; // "HTEMPLATE" in spec.
  1441. Vector<NonnullRefPtr<Symbol>> patterns; // "HNUMPATS" / "HPATS" in spec.
  1442. bool default_pixel_value { false }; // "HDEFPIXEL" in spec.
  1443. CombinationOperator combination_operator { CombinationOperator::Or }; // "HCOMBOP" in spec.
  1444. bool enable_skip { false }; // "HENABLESKIP" in spec.
  1445. u32 grayscale_width { 0 }; // "HGW" in spec.
  1446. u32 grayscale_height { 0 }; // "HGH" in spec.
  1447. i32 grid_origin_x_offset { 0 }; // "HGX" in spec.
  1448. i32 grid_origin_y_offset { 0 }; // "HGY" in spec.
  1449. u16 grid_vector_x { 0 }; // "HRY" in spec.
  1450. u16 grid_vector_y { 0 }; // "HRX" in spec.
  1451. u8 pattern_width { 0 }; // "HPW" in spec.
  1452. u8 pattern_height { 0 }; // "HPH" in spec.
  1453. };
  1454. // 6.6 Halftone Region Decoding Procedure
  1455. static ErrorOr<NonnullOwnPtr<BitBuffer>> halftone_region_decoding_procedure(HalftoneRegionDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<QMArithmeticDecoder::Context>& contexts)
  1456. {
  1457. // 6.6.5 Decoding the halftone region
  1458. // "1) Fill a bitmap HTREG, of the size given by HBW and HBH, with the HDEFPIXEL value."
  1459. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  1460. result->fill(inputs.default_pixel_value);
  1461. // "2) If HENABLESKIP equals 1, compute a bitmap HSKIP as shown in 6.6.5.1."
  1462. Optional<BitBuffer const&> skip_pattern;
  1463. OwnPtr<BitBuffer> skip_pattern_storage;
  1464. if (inputs.enable_skip) {
  1465. // FIXME: This is untested; I haven't found a sample that uses HENABLESKIP yet.
  1466. // But generic_region_decoding_procedure() currently doesn't implement skip_pattern anyways
  1467. // and errors out on it, so we'll notice when this gets hit.
  1468. skip_pattern_storage = TRY(BitBuffer::create(inputs.pattern_width, inputs.pattern_height));
  1469. skip_pattern = *skip_pattern_storage;
  1470. // 6.6.5.1 Computing HSKIP
  1471. // "1) For each value of mg between 0 and HGH – 1, beginning from 0, perform the following steps:"
  1472. for (int m_g = 0; m_g < (int)inputs.grayscale_height; ++m_g) {
  1473. // "a) For each value of ng between 0 and HGW – 1, beginning from 0, perform the following steps:"
  1474. for (int n_g = 0; n_g < (int)inputs.grayscale_width; ++n_g) {
  1475. // "i) Set:
  1476. // x = (HGX + m_g × HRY + n_g × HRX) >> 8
  1477. // y = (HGY + m_g × HRX – n_g × HRY) >> 8"
  1478. auto x = (inputs.grid_origin_x_offset + m_g * inputs.grid_vector_y + n_g * inputs.grid_vector_x) >> 8;
  1479. auto y = (inputs.grid_origin_y_offset + m_g * inputs.grid_vector_x - n_g * inputs.grid_vector_y) >> 8;
  1480. // "ii) If ((x + HPW <= 0) OR (x >= HBW) OR (y + HPH <= 0) OR (y >= HBH)) then set:
  1481. // HSKIP[n_g, m_g] = 1
  1482. // Otherwise, set:
  1483. // HSKIP[n_g, m_g] = 0"
  1484. if (x + inputs.pattern_width <= 0 || x >= (int)inputs.region_width || y + inputs.pattern_height <= 0 || y >= (int)inputs.region_height)
  1485. skip_pattern_storage->set_bit(n_g, m_g, true);
  1486. else
  1487. skip_pattern_storage->set_bit(n_g, m_g, false);
  1488. }
  1489. }
  1490. }
  1491. // "3) Set HBPP to ⌈log2 (HNUMPATS)⌉."
  1492. u8 bits_per_pattern = ceil(log2(inputs.patterns.size()));
  1493. // "4) Decode an image GI of size HGW by HGH with HBPP bits per pixel using the gray-scale image decoding
  1494. // procedure as described in Annex C. Set the parameters to this decoding procedure as shown in Table 23.
  1495. // Let GI be the results of invoking this decoding procedure."
  1496. GrayscaleInputParameters grayscale_inputs;
  1497. grayscale_inputs.uses_mmr = inputs.uses_mmr;
  1498. grayscale_inputs.width = inputs.grayscale_width;
  1499. grayscale_inputs.height = inputs.grayscale_height;
  1500. grayscale_inputs.bpp = bits_per_pattern;
  1501. grayscale_inputs.skip_pattern = skip_pattern;
  1502. grayscale_inputs.template_id = inputs.halftone_template;
  1503. Optional<QMArithmeticDecoder> decoder;
  1504. if (!inputs.uses_mmr) {
  1505. decoder = TRY(QMArithmeticDecoder::initialize(data));
  1506. grayscale_inputs.arithmetic_decoder = &decoder.value();
  1507. }
  1508. auto grayscale_image = TRY(grayscale_image_decoding_procedure(grayscale_inputs, data, contexts));
  1509. // "5) Place sequentially the patterns corresponding to the values in GI into HTREG by the procedure described in 6.6.5.2.
  1510. // The rendering procedure is illustrated in Figure 26. The outline of two patterns are marked by dotted boxes."
  1511. {
  1512. // 6.6.5.2 Rendering the patterns
  1513. // "Draw the patterns into HTREG using the following procedure:
  1514. // 1) For each value of m_g between 0 and HGH – 1, beginning from 0, perform the following steps."
  1515. for (int m_g = 0; m_g < (int)inputs.grayscale_height; ++m_g) {
  1516. // "a) For each value of n_g between 0 and HGW – 1, beginning from 0, perform the following steps."
  1517. for (int n_g = 0; n_g < (int)inputs.grayscale_width; ++n_g) {
  1518. // "i) Set:
  1519. // x = (HGX + m_g × HRY + n_g × HRX) >> 8
  1520. // y = (HGY + m_g × HRX – n_g × HRY) >> 8"
  1521. auto x = (inputs.grid_origin_x_offset + m_g * inputs.grid_vector_y + n_g * inputs.grid_vector_x) >> 8;
  1522. auto y = (inputs.grid_origin_y_offset + m_g * inputs.grid_vector_x - n_g * inputs.grid_vector_y) >> 8;
  1523. // "ii) Draw the pattern HPATS[GI[n_g, m_g]] into HTREG such that its upper left pixel is at location (x, y) in HTREG.
  1524. //
  1525. // A pattern is drawn into HTREG as follows. Each pixel of the pattern shall be combined with
  1526. // the current value of the corresponding pixel in the halftone-coded bitmap, using the
  1527. // combination operator specified by HCOMBOP. The results of each combination shall be
  1528. // written into that pixel in the halftone-coded bitmap.
  1529. //
  1530. // If any part of a decoded pattern, when placed at location (x, y) lies outside the actual halftone-
  1531. // coded bitmap, then this part of the pattern shall be ignored in the process of combining the
  1532. // pattern with the bitmap."
  1533. u8 grayscale_value = grayscale_image[n_g + m_g * inputs.grayscale_width];
  1534. if (grayscale_value >= inputs.patterns.size())
  1535. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Grayscale value out of range");
  1536. auto const& pattern = inputs.patterns[grayscale_value];
  1537. composite_bitbuffer(*result, pattern->bitmap(), { x, y }, inputs.combination_operator);
  1538. }
  1539. }
  1540. }
  1541. // "6) After all the patterns have been placed on the bitmap, the current contents of the halftone-coded bitmap are
  1542. // the results that shall be obtained by every decoder, whether it performs this exact sequence of steps or not."
  1543. return result;
  1544. }
  1545. // 6.7.2 Input parameters
  1546. // Table 24 – Parameters for the pattern dictionary decoding procedure
  1547. struct PatternDictionaryDecodingInputParameters {
  1548. bool uses_mmr { false }; // "HDMMR" in spec.
  1549. u32 width { 0 }; // "HDPW" in spec.
  1550. u32 height { 0 }; // "HDPH" in spec.
  1551. u32 gray_max { 0 }; // "GRAYMAX" in spec.
  1552. u8 hd_template { 0 }; // "HDTEMPLATE" in spec.
  1553. };
  1554. // 6.7 Pattern Dictionary Decoding Procedure
  1555. static ErrorOr<Vector<NonnullRefPtr<Symbol>>> pattern_dictionary_decoding_procedure(PatternDictionaryDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<QMArithmeticDecoder::Context>& contexts)
  1556. {
  1557. // Table 27 – Parameters used to decode a pattern dictionary's collective bitmap
  1558. GenericRegionDecodingInputParameters generic_inputs;
  1559. generic_inputs.is_modified_modified_read = inputs.uses_mmr;
  1560. generic_inputs.region_width = (inputs.gray_max + 1) * inputs.width;
  1561. generic_inputs.region_height = inputs.height;
  1562. generic_inputs.gb_template = inputs.hd_template;
  1563. generic_inputs.is_typical_prediction_used = false;
  1564. generic_inputs.is_extended_reference_template_used = false; // Missing from spec in table 27.
  1565. generic_inputs.skip_pattern = OptionalNone {};
  1566. generic_inputs.adaptive_template_pixels[0].x = -inputs.width;
  1567. generic_inputs.adaptive_template_pixels[0].y = 0;
  1568. generic_inputs.adaptive_template_pixels[1].x = -3;
  1569. generic_inputs.adaptive_template_pixels[1].y = -1;
  1570. generic_inputs.adaptive_template_pixels[2].x = 2;
  1571. generic_inputs.adaptive_template_pixels[2].y = -2;
  1572. generic_inputs.adaptive_template_pixels[3].x = -2;
  1573. generic_inputs.adaptive_template_pixels[3].y = -2;
  1574. Optional<QMArithmeticDecoder> decoder;
  1575. if (!inputs.uses_mmr) {
  1576. decoder = TRY(QMArithmeticDecoder::initialize(data));
  1577. generic_inputs.arithmetic_decoder = &decoder.value();
  1578. }
  1579. auto bitmap = TRY(generic_region_decoding_procedure(generic_inputs, data, contexts));
  1580. Vector<NonnullRefPtr<Symbol>> patterns;
  1581. for (u32 gray = 0; gray <= inputs.gray_max; ++gray) {
  1582. int x = gray * inputs.width;
  1583. auto pattern = TRY(bitmap->subbitmap({ x, 0, static_cast<int>(inputs.width), static_cast<int>(inputs.height) }));
  1584. patterns.append(Symbol::create(move(pattern)));
  1585. }
  1586. dbgln_if(JBIG2_DEBUG, "Pattern dictionary: {} patterns", patterns.size());
  1587. return patterns;
  1588. }
  1589. static ErrorOr<void> decode_symbol_dictionary(JBIG2LoadingContext& context, SegmentData& segment)
  1590. {
  1591. // 7.4.2 Symbol dictionary segment syntax
  1592. // 7.4.2.1 Symbol dictionary segment data header
  1593. FixedMemoryStream stream(segment.data);
  1594. // 7.4.2.1.1 Symbol dictionary flags
  1595. u16 flags = TRY(stream.read_value<BigEndian<u16>>());
  1596. bool uses_huffman_encoding = (flags & 1) != 0; // "SDHUFF" in spec.
  1597. bool uses_refinement_or_aggregate_coding = (flags & 2) != 0; // "SDREFAGG" in spec.
  1598. u8 huffman_table_selection_for_height_differences = (flags >> 2) & 0b11; // "SDHUFFDH" in spec.
  1599. if (huffman_table_selection_for_height_differences == 2)
  1600. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_height_differences");
  1601. if (!uses_huffman_encoding && huffman_table_selection_for_height_differences != 0)
  1602. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_height_differences");
  1603. u8 huffman_table_selection_for_width_differences = (flags >> 4) & 0b11; // "SDHUFFDW" in spec.
  1604. if (huffman_table_selection_for_width_differences == 2)
  1605. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_width_differences");
  1606. if (!uses_huffman_encoding && huffman_table_selection_for_width_differences != 0)
  1607. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_width_differences");
  1608. bool uses_user_supplied_size_table = (flags >> 6) & 1; // "SDHUFFBMSIZE" in spec.
  1609. if (!uses_huffman_encoding && uses_user_supplied_size_table)
  1610. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid uses_user_supplied_size_table");
  1611. bool uses_user_supplied_aggregate_table = (flags >> 7) & 1; // "SDHUFFAGGINST" in spec.
  1612. if (!uses_huffman_encoding && uses_user_supplied_aggregate_table)
  1613. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid uses_user_supplied_aggregate_table");
  1614. bool bitmap_coding_context_used = (flags >> 8) & 1;
  1615. if (uses_huffman_encoding && !uses_refinement_or_aggregate_coding && bitmap_coding_context_used)
  1616. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid bitmap_coding_context_used");
  1617. bool bitmap_coding_context_retained = (flags >> 9) & 1;
  1618. if (uses_huffman_encoding && !uses_refinement_or_aggregate_coding && bitmap_coding_context_retained)
  1619. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid bitmap_coding_context_retained");
  1620. u8 template_used = (flags >> 10) & 0b11; // "SDTEMPLATE" in spec.
  1621. if (uses_huffman_encoding && template_used != 0)
  1622. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid template_used");
  1623. u8 refinement_template_used = (flags >> 12) & 0b11; // "SDREFTEMPLATE" in spec.
  1624. if (!uses_refinement_or_aggregate_coding && refinement_template_used != 0)
  1625. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid refinement_template_used");
  1626. if (flags & 0b1110'0000'0000'0000)
  1627. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid symbol dictionary flags");
  1628. // 7.4.2.1.2 Symbol dictionary AT flags
  1629. Array<AdaptiveTemplatePixel, 4> adaptive_template {};
  1630. if (!uses_huffman_encoding) {
  1631. int number_of_adaptive_template_pixels = template_used == 0 ? 4 : 1;
  1632. for (int i = 0; i < number_of_adaptive_template_pixels; ++i) {
  1633. adaptive_template[i].x = TRY(stream.read_value<i8>());
  1634. adaptive_template[i].y = TRY(stream.read_value<i8>());
  1635. }
  1636. }
  1637. // 7.4.2.1.3 Symbol dictionary refinement AT flags
  1638. Array<AdaptiveTemplatePixel, 2> adaptive_refinement_template {};
  1639. if (uses_refinement_or_aggregate_coding && refinement_template_used == 0) {
  1640. for (size_t i = 0; i < adaptive_refinement_template.size(); ++i) {
  1641. adaptive_refinement_template[i].x = TRY(stream.read_value<i8>());
  1642. adaptive_refinement_template[i].y = TRY(stream.read_value<i8>());
  1643. }
  1644. }
  1645. // 7.4.2.1.4 Number of exported symbols (SDNUMEXSYMS)
  1646. u32 number_of_exported_symbols = TRY(stream.read_value<BigEndian<u32>>());
  1647. // 7.4.2.1.5 Number of new symbols (SDNUMNEWSYMS)
  1648. u32 number_of_new_symbols = TRY(stream.read_value<BigEndian<u32>>());
  1649. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_huffman_encoding={}", uses_huffman_encoding);
  1650. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_refinement_or_aggregate_coding={}", uses_refinement_or_aggregate_coding);
  1651. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: huffman_table_selection_for_height_differences={}", huffman_table_selection_for_height_differences);
  1652. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: huffman_table_selection_for_width_differences={}", huffman_table_selection_for_width_differences);
  1653. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_user_supplied_size_table={}", uses_user_supplied_size_table);
  1654. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_user_supplied_aggregate_table={}", uses_user_supplied_aggregate_table);
  1655. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: bitmap_coding_context_used={}", bitmap_coding_context_used);
  1656. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: bitmap_coding_context_retained={}", bitmap_coding_context_retained);
  1657. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: template_used={}", template_used);
  1658. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: refinement_template_used={}", refinement_template_used);
  1659. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: number_of_exported_symbols={}", number_of_exported_symbols);
  1660. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: number_of_new_symbols={}", number_of_new_symbols);
  1661. // 7.4.2.1.6 Symbol dictionary segment Huffman table selection
  1662. // FIXME
  1663. // 7.4.2.2 Decoding a symbol dictionary segment
  1664. // "1) Interpret its header, as described in 7.4.2.1."
  1665. // Done!
  1666. // "2) Decode (or retrieve the results of decoding) any referred-to symbol dictionary and tables segments."
  1667. Vector<NonnullRefPtr<Symbol>> symbols;
  1668. for (auto referred_to_segment_number : segment.header.referred_to_segment_numbers) {
  1669. auto opt_referred_to_segment = context.segments_by_number.get(referred_to_segment_number);
  1670. if (!opt_referred_to_segment.has_value())
  1671. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Symbol segment refers to non-existent segment");
  1672. dbgln_if(JBIG2_DEBUG, "Symbol segment refers to segment id {} index {}", referred_to_segment_number, opt_referred_to_segment.value());
  1673. auto const& referred_to_segment = context.segments[opt_referred_to_segment.value()];
  1674. if (!referred_to_segment.symbols.has_value())
  1675. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Symbol segment referred-to segment without symbols");
  1676. symbols.extend(referred_to_segment.symbols.value());
  1677. }
  1678. // "3) If the "bitmap coding context used" bit in the header was 1, ..."
  1679. if (bitmap_coding_context_used)
  1680. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode bitmap coding context segment yet");
  1681. // "4) If the "bitmap coding context used" bit in the header was 0, then, as described in E.3.7,
  1682. // reset all the arithmetic coding statistics for the generic region and generic refinement region decoding procedures to zero."
  1683. // Nothing to do.
  1684. // "5) Reset the arithmetic coding statistics for all the contexts of all the arithmetic integer coders to zero."
  1685. // FIXME
  1686. // "6) Invoke the symbol dictionary decoding procedure described in 6.5, with the parameters to the symbol dictionary decoding procedure set as shown in Table 31."
  1687. SymbolDictionaryDecodingInputParameters inputs;
  1688. inputs.uses_huffman_encoding = uses_huffman_encoding;
  1689. inputs.uses_refinement_or_aggregate_coding = uses_refinement_or_aggregate_coding;
  1690. inputs.input_symbols = move(symbols);
  1691. inputs.number_of_new_symbols = number_of_new_symbols;
  1692. inputs.number_of_exported_symbols = number_of_exported_symbols;
  1693. // FIXME: SDHUFFDH, SDHUFFDW, SDHUFFBMSIZE, SDHUFFAGGINST
  1694. inputs.symbol_template = template_used;
  1695. inputs.adaptive_template_pixels = adaptive_template;
  1696. inputs.refinement_template = refinement_template_used;
  1697. inputs.refinement_adaptive_template_pixels = adaptive_refinement_template;
  1698. auto result = TRY(symbol_dictionary_decoding_procedure(inputs, segment.data.slice(TRY(stream.tell()))));
  1699. // "7) If the "bitmap coding context retained" bit in the header was 1, then, as described in E.3.8, preserve the current contents
  1700. // of the arithmetic coding statistics for the generic region and generic refinement region decoding procedures."
  1701. if (bitmap_coding_context_retained)
  1702. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot retain bitmap coding context yet");
  1703. segment.symbols = move(result);
  1704. return {};
  1705. }
  1706. static ErrorOr<void> decode_intermediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  1707. {
  1708. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate text region yet");
  1709. }
  1710. static ErrorOr<void> decode_immediate_text_region(JBIG2LoadingContext& context, SegmentData const& segment)
  1711. {
  1712. // 7.4.3 Text region segment syntax
  1713. auto data = segment.data;
  1714. auto information_field = TRY(decode_region_segment_information_field(data));
  1715. data = data.slice(sizeof(information_field));
  1716. dbgln_if(JBIG2_DEBUG, "Text region: width={}, height={}, x={}, y={}, flags={:#x}", information_field.width, information_field.height, information_field.x_location, information_field.y_location, information_field.flags);
  1717. FixedMemoryStream stream(data);
  1718. // 7.4.3.1.1 Text region segment flags
  1719. u16 text_region_segment_flags = TRY(stream.read_value<BigEndian<u16>>());
  1720. bool uses_huffman_encoding = (text_region_segment_flags & 1) != 0; // "SBHUFF" in spec.
  1721. bool uses_refinement_coding = (text_region_segment_flags >> 1) & 1; // "SBREFINE" in spec.
  1722. u8 log_strip_size = (text_region_segment_flags >> 2) & 3; // "LOGSBSTRIPS" in spec.
  1723. u8 strip_size = 1u << log_strip_size;
  1724. u8 reference_corner = (text_region_segment_flags >> 4) & 3; // "REFCORNER"
  1725. bool is_transposed = (text_region_segment_flags >> 6) & 1; // "TRANSPOSED" in spec.
  1726. u8 combination_operator = (text_region_segment_flags >> 7) & 3; // "SBCOMBOP" in spec.
  1727. if (combination_operator > 4)
  1728. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid text region combination operator");
  1729. u8 default_pixel_value = (text_region_segment_flags >> 9) & 1; // "SBDEFPIXEL" in spec.
  1730. u8 delta_s_offset_value = (text_region_segment_flags >> 10) & 0x1f; // "SBDSOFFSET" in spec.
  1731. i8 delta_s_offset = delta_s_offset_value;
  1732. if (delta_s_offset_value & 0x10) {
  1733. // This is converting a 5-bit two's complement number ot i8.
  1734. // FIXME: There's probably a simpler way to do this? Probably just sign-extend by or-ing in the top 3 bits?
  1735. delta_s_offset_value = (~delta_s_offset_value + 1) & 0x1f;
  1736. delta_s_offset = -delta_s_offset_value;
  1737. }
  1738. u8 refinement_template = (text_region_segment_flags >> 15) != 0; // "SBRTEMPLATE" in spec.
  1739. if (!uses_refinement_coding && refinement_template != 0)
  1740. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid refinement_template");
  1741. // 7.4.3.1.2 Text region segment Huffman flags
  1742. // "This field is only present if SBHUFF is 1."
  1743. // FIXME: Support this eventually.
  1744. if (uses_huffman_encoding)
  1745. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman text regions yet");
  1746. // 7.4.3.1.3 Text region refinement AT flags
  1747. // "This field is only present if SBREFINE is 1 and SBRTEMPLATE is 0."
  1748. Array<AdaptiveTemplatePixel, 2> adaptive_refinement_template {};
  1749. if (uses_refinement_coding && refinement_template == 0) {
  1750. for (size_t i = 0; i < adaptive_refinement_template.size(); ++i) {
  1751. adaptive_refinement_template[i].x = TRY(stream.read_value<i8>());
  1752. adaptive_refinement_template[i].y = TRY(stream.read_value<i8>());
  1753. }
  1754. }
  1755. // 7.4.3.1.4 Number of symbol instances (SBNUMINSTANCES)
  1756. u32 number_of_symbol_instances = TRY(stream.read_value<BigEndian<u32>>());
  1757. // 7.4.3.1.5 Text region segment symbol ID Huffman decoding table
  1758. // "It is only present if SBHUFF is 1."
  1759. // FIXME: Support this eventually.
  1760. dbgln_if(JBIG2_DEBUG, "Text region: uses_huffman_encoding={}, uses_refinement_coding={}, strip_size={}, reference_corner={}, is_transposed={}", uses_huffman_encoding, uses_refinement_coding, strip_size, reference_corner, is_transposed);
  1761. dbgln_if(JBIG2_DEBUG, "Text region: combination_operator={}, default_pixel_value={}, delta_s_offset={}, refinement_template={}, number_of_symbol_instances={}", combination_operator, default_pixel_value, delta_s_offset, refinement_template, number_of_symbol_instances);
  1762. dbgln_if(JBIG2_DEBUG, "Text region: number_of_symbol_instances={}", number_of_symbol_instances);
  1763. // 7.4.3.2 Decoding a text region segment
  1764. // "1) Interpret its header, as described in 7.4.3.1."
  1765. // Done!
  1766. // "2) Decode (or retrieve the results of decoding) any referred-to symbol dictionary and tables segments."
  1767. Vector<NonnullRefPtr<Symbol>> symbols;
  1768. for (auto referred_to_segment_number : segment.header.referred_to_segment_numbers) {
  1769. auto opt_referred_to_segment = context.segments_by_number.get(referred_to_segment_number);
  1770. if (!opt_referred_to_segment.has_value())
  1771. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Text segment refers to non-existent segment");
  1772. dbgln_if(JBIG2_DEBUG, "Text segment refers to segment id {} index {}", referred_to_segment_number, opt_referred_to_segment.value());
  1773. auto const& referred_to_segment = context.segments[opt_referred_to_segment.value()];
  1774. if (!referred_to_segment.symbols.has_value())
  1775. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Text segment referred-to segment without symbols");
  1776. symbols.extend(referred_to_segment.symbols.value());
  1777. }
  1778. // "3) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1779. // FIXME
  1780. // "4) Invoke the text region decoding procedure described in 6.4, with the parameters to the text region decoding procedure set as shown in Table 34."
  1781. TextRegionDecodingInputParameters inputs;
  1782. inputs.uses_huffman_encoding = uses_huffman_encoding;
  1783. inputs.uses_refinement_coding = uses_refinement_coding;
  1784. inputs.default_pixel = default_pixel_value;
  1785. inputs.operator_ = static_cast<CombinationOperator>(combination_operator);
  1786. inputs.is_transposed = is_transposed;
  1787. inputs.reference_corner = static_cast<TextRegionDecodingInputParameters::Corner>(reference_corner);
  1788. inputs.delta_s_offset = delta_s_offset;
  1789. inputs.region_width = information_field.width;
  1790. inputs.region_height = information_field.height;
  1791. inputs.number_of_instances = number_of_symbol_instances;
  1792. inputs.size_of_symbol_instance_strips = strip_size;
  1793. inputs.id_symbol_code_length = ceil(log2(symbols.size()));
  1794. inputs.symbols = move(symbols);
  1795. // FIXME: Huffman tables.
  1796. inputs.refinement_template = refinement_template;
  1797. inputs.refinement_adaptive_template_pixels = adaptive_refinement_template;
  1798. auto result = TRY(text_region_decoding_procedure(inputs, data.slice(TRY(stream.tell()))));
  1799. composite_bitbuffer(*context.page.bits, *result, { information_field.x_location, information_field.y_location }, information_field.external_combination_operator());
  1800. return {};
  1801. }
  1802. static ErrorOr<void> decode_pattern_dictionary(JBIG2LoadingContext&, SegmentData& segment)
  1803. {
  1804. // 7.4.4 Pattern dictionary segment syntax
  1805. FixedMemoryStream stream(segment.data);
  1806. // 7.4.4.1.1 Pattern dictionary flags
  1807. u8 flags = TRY(stream.read_value<u8>());
  1808. bool uses_mmr = flags & 1;
  1809. u8 hd_template = (flags >> 1) & 3;
  1810. if (uses_mmr && hd_template != 0)
  1811. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid hd_template");
  1812. if (flags & 0b1111'1000)
  1813. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid flags");
  1814. // 7.4.4.1.2 Width of the patterns in the pattern dictionary (HDPW)
  1815. u8 width = TRY(stream.read_value<u8>());
  1816. if (width == 0)
  1817. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid width");
  1818. // 7.4.4.1.3 Height of the patterns in the pattern dictionary (HDPH)
  1819. u8 height = TRY(stream.read_value<u8>());
  1820. if (height == 0)
  1821. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid height");
  1822. // 7.4.4.1.4 Largest gray-scale value (GRAYMAX)
  1823. u32 gray_max = TRY(stream.read_value<BigEndian<u32>>());
  1824. // 7.4.4.2 Decoding a pattern dictionary segment
  1825. dbgln_if(JBIG2_DEBUG, "Pattern dictionary: uses_mmr={}, hd_template={}, width={}, height={}, gray_max={}", uses_mmr, hd_template, width, height, gray_max);
  1826. auto data = segment.data.slice(TRY(stream.tell()));
  1827. // "1) Interpret its header, as described in 7.4.4.1."
  1828. // Done!
  1829. // "2) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1830. Vector<QMArithmeticDecoder::Context> contexts;
  1831. if (!uses_mmr)
  1832. contexts.resize(1 << number_of_context_bits_for_template(hd_template));
  1833. // "3) Invoke the pattern dictionary decoding procedure described in 6.7, with the parameters to the pattern
  1834. // dictionary decoding procedure set as shown in Table 35."
  1835. PatternDictionaryDecodingInputParameters inputs;
  1836. inputs.uses_mmr = uses_mmr;
  1837. inputs.width = width;
  1838. inputs.height = height;
  1839. inputs.gray_max = gray_max;
  1840. inputs.hd_template = hd_template;
  1841. auto result = TRY(pattern_dictionary_decoding_procedure(inputs, data, contexts));
  1842. segment.patterns = move(result);
  1843. return {};
  1844. }
  1845. static ErrorOr<void> decode_intermediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  1846. {
  1847. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate halftone region yet");
  1848. }
  1849. static ErrorOr<void> decode_immediate_halftone_region(JBIG2LoadingContext& context, SegmentData const& segment)
  1850. {
  1851. // 7.4.5 Halftone region segment syntax
  1852. auto data = segment.data;
  1853. auto information_field = TRY(decode_region_segment_information_field(data));
  1854. data = data.slice(sizeof(information_field));
  1855. dbgln_if(JBIG2_DEBUG, "Halftone region: width={}, height={}, x={}, y={}, flags={:#x}", information_field.width, information_field.height, information_field.x_location, information_field.y_location, information_field.flags);
  1856. FixedMemoryStream stream(data);
  1857. // 7.4.5.1.1 Halftone region segment flags
  1858. u8 flags = TRY(stream.read_value<u8>());
  1859. bool uses_mmr = flags & 1; // "HMMR" in spec.
  1860. u8 template_used = (flags >> 1) & 3; // "HTTEMPLATE" in spec.
  1861. if (uses_mmr && template_used != 0)
  1862. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid template_used");
  1863. bool enable_skip = (flags >> 3) & 1; // "HENABLESKIP" in spec.
  1864. u8 combination_operator = (flags >> 4) & 7; // "HCOMBOP" in spec.
  1865. if (combination_operator > 4)
  1866. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid combination_operator");
  1867. bool default_pixel_value = (flags >> 7) & 1; // "HDEFPIXEL" in spec.
  1868. dbgln_if(JBIG2_DEBUG, "Halftone region: uses_mmr={}, template_used={}, enable_skip={}, combination_operator={}, default_pixel_value={}", uses_mmr, template_used, enable_skip, combination_operator, default_pixel_value);
  1869. // 7.4.5.1.2 Halftone grid position and size
  1870. // 7.4.5.1.2.1 Width of the gray-scale image (HGW)
  1871. u32 gray_width = TRY(stream.read_value<BigEndian<u32>>());
  1872. // 7.4.5.1.2.2 Height of the gray-scale image (HGH)
  1873. u32 gray_height = TRY(stream.read_value<BigEndian<u32>>());
  1874. // 7.4.5.1.2.3 Horizontal offset of the grid (HGX)
  1875. i32 grid_x = TRY(stream.read_value<BigEndian<i32>>());
  1876. // 7.4.5.1.2.4 Vertical offset of the grid (HGY)
  1877. i32 grid_y = TRY(stream.read_value<BigEndian<i32>>());
  1878. // 7.4.5.1.3 Halftone grid vector
  1879. // 7.4.5.1.3.1 Horizontal coordinate of the halftone grid vector (HRX)
  1880. u16 grid_vector_x = TRY(stream.read_value<BigEndian<u16>>());
  1881. // 7.4.5.1.3.2 Vertical coordinate of the halftone grid vector (HRY)
  1882. u16 grid_vector_y = TRY(stream.read_value<BigEndian<u16>>());
  1883. dbgln_if(JBIG2_DEBUG, "Halftone region: gray_width={}, gray_height={}, grid_x={}, grid_y={}, grid_vector_x={}, grid_vector_y={}", gray_width, gray_height, grid_x, grid_y, grid_vector_x, grid_vector_y);
  1884. // 7.4.5.2 Decoding a halftone region segment
  1885. // "1) Interpret its header, as described in 7.4.5.1."
  1886. // Done!
  1887. // "2) Decode (or retrieve the results of decoding) the referred-to pattern dictionary segment."
  1888. if (segment.header.referred_to_segment_numbers.size() != 1)
  1889. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Halftone segment refers to wrong number of segments");
  1890. auto opt_referred_to_segment = context.segments_by_number.get(segment.header.referred_to_segment_numbers[0]);
  1891. if (!opt_referred_to_segment.has_value())
  1892. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Halftone segment refers to non-existent segment");
  1893. dbgln_if(JBIG2_DEBUG, "Halftone segment refers to segment id {} index {}", segment.header.referred_to_segment_numbers[0], opt_referred_to_segment.value());
  1894. auto const& referred_to_segment = context.segments[opt_referred_to_segment.value()];
  1895. if (!referred_to_segment.patterns.has_value())
  1896. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Halftone segment referred-to segment without patterns");
  1897. Vector<NonnullRefPtr<Symbol>> patterns = referred_to_segment.patterns.value();
  1898. if (patterns.is_empty())
  1899. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Halftone segment without patterns");
  1900. // "3) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1901. Vector<QMArithmeticDecoder::Context> contexts;
  1902. if (!uses_mmr)
  1903. contexts.resize(1 << number_of_context_bits_for_template(template_used));
  1904. // "4) Invoke the halftone region decoding procedure described in 6.6, with the parameters to the halftone
  1905. // region decoding procedure set as shown in Table 36."
  1906. data = data.slice(TRY(stream.tell()));
  1907. HalftoneRegionDecodingInputParameters inputs;
  1908. inputs.region_width = information_field.width;
  1909. inputs.region_height = information_field.height;
  1910. inputs.uses_mmr = uses_mmr;
  1911. inputs.halftone_template = template_used;
  1912. inputs.enable_skip = enable_skip;
  1913. inputs.combination_operator = static_cast<CombinationOperator>(combination_operator);
  1914. inputs.default_pixel_value = default_pixel_value;
  1915. inputs.grayscale_width = gray_width;
  1916. inputs.grayscale_height = gray_height;
  1917. inputs.grid_origin_x_offset = grid_x;
  1918. inputs.grid_origin_y_offset = grid_y;
  1919. inputs.grid_vector_x = grid_vector_x;
  1920. inputs.grid_vector_y = grid_vector_y;
  1921. inputs.patterns = move(patterns);
  1922. inputs.pattern_width = inputs.patterns[0]->bitmap().width();
  1923. inputs.pattern_height = inputs.patterns[0]->bitmap().height();
  1924. auto result = TRY(halftone_region_decoding_procedure(inputs, data, contexts));
  1925. composite_bitbuffer(*context.page.bits, *result, { information_field.x_location, information_field.y_location }, information_field.external_combination_operator());
  1926. return {};
  1927. }
  1928. static ErrorOr<void> decode_immediate_lossless_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  1929. {
  1930. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless halftone region yet");
  1931. }
  1932. static ErrorOr<void> decode_intermediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  1933. {
  1934. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic region yet");
  1935. }
  1936. static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& context, SegmentData const& segment)
  1937. {
  1938. // 7.4.6 Generic region segment syntax
  1939. auto data = segment.data;
  1940. auto information_field = TRY(decode_region_segment_information_field(data));
  1941. data = data.slice(sizeof(information_field));
  1942. 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);
  1943. // 7.4.6.2 Generic region segment flags
  1944. if (data.is_empty())
  1945. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No segment data");
  1946. u8 flags = data[0];
  1947. bool uses_mmr = (flags & 1) != 0;
  1948. u8 arithmetic_coding_template = (flags >> 1) & 3; // "GBTEMPLATE"
  1949. bool typical_prediction_generic_decoding_on = (flags >> 3) & 1; // "TPGDON"; "TPGD" is short for "Typical Prediction for Generic Direct coding"
  1950. bool uses_extended_reference_template = (flags >> 4) & 1; // "EXTTEMPLATE"
  1951. if (flags & 0b1110'0000)
  1952. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid flags");
  1953. data = data.slice(sizeof(flags));
  1954. // 7.4.6.3 Generic region segment AT flags
  1955. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels {};
  1956. if (!uses_mmr) {
  1957. dbgln_if(JBIG2_DEBUG, "Non-MMR generic region, GBTEMPLATE={} TPGDON={} EXTTEMPLATE={}", arithmetic_coding_template, typical_prediction_generic_decoding_on, uses_extended_reference_template);
  1958. if (arithmetic_coding_template == 0 && uses_extended_reference_template) {
  1959. // This was added in T.88 Amendment 2 (https://www.itu.int/rec/T-REC-T.88-200306-S!Amd2/en) mid-2003.
  1960. // 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,
  1961. // so it's not clear how much data to read.
  1962. return Error::from_string_literal("JBIG2ImageDecoderPlugin: GBTEMPLATE=0 EXTTEMPLATE=1 not yet implemented");
  1963. }
  1964. size_t number_of_adaptive_template_pixels = arithmetic_coding_template == 0 ? 4 : 1;
  1965. if (data.size() < 2 * number_of_adaptive_template_pixels)
  1966. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No adaptive template data");
  1967. for (size_t i = 0; i < number_of_adaptive_template_pixels; ++i) {
  1968. adaptive_template_pixels[i].x = static_cast<i8>(data[2 * i]);
  1969. adaptive_template_pixels[i].y = static_cast<i8>(data[2 * i + 1]);
  1970. }
  1971. data = data.slice(2 * number_of_adaptive_template_pixels);
  1972. }
  1973. // 7.4.6.4 Decoding a generic region segment
  1974. // "1) Interpret its header, as described in 7.4.6.1"
  1975. // Done above.
  1976. // "2) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1977. Vector<QMArithmeticDecoder::Context> contexts;
  1978. contexts.resize(1 << number_of_context_bits_for_template(arithmetic_coding_template));
  1979. // "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."
  1980. GenericRegionDecodingInputParameters inputs;
  1981. inputs.is_modified_modified_read = uses_mmr;
  1982. inputs.region_width = information_field.width;
  1983. inputs.region_height = information_field.height;
  1984. inputs.gb_template = arithmetic_coding_template;
  1985. inputs.is_typical_prediction_used = typical_prediction_generic_decoding_on;
  1986. inputs.is_extended_reference_template_used = uses_extended_reference_template;
  1987. inputs.skip_pattern = OptionalNone {};
  1988. inputs.adaptive_template_pixels = adaptive_template_pixels;
  1989. Optional<QMArithmeticDecoder> decoder;
  1990. if (!uses_mmr) {
  1991. decoder = TRY(QMArithmeticDecoder::initialize(data));
  1992. inputs.arithmetic_decoder = &decoder.value();
  1993. }
  1994. auto result = TRY(generic_region_decoding_procedure(inputs, data, contexts));
  1995. // 8.2 Page image composition step 5)
  1996. if (information_field.x_location + information_field.width > (u32)context.page.size.width()
  1997. || information_field.y_location + information_field.height > (u32)context.page.size.height()) {
  1998. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Region bounds outsize of page bounds");
  1999. }
  2000. composite_bitbuffer(*context.page.bits, *result, { information_field.x_location, information_field.y_location }, information_field.external_combination_operator());
  2001. return {};
  2002. }
  2003. static ErrorOr<void> decode_intermediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  2004. {
  2005. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic refinement region yet");
  2006. }
  2007. static ErrorOr<void> decode_immediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  2008. {
  2009. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic refinement region yet");
  2010. }
  2011. static ErrorOr<void> decode_immediate_lossless_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  2012. {
  2013. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic refinement region yet");
  2014. }
  2015. static ErrorOr<void> decode_page_information(JBIG2LoadingContext& context, SegmentData const& segment)
  2016. {
  2017. // 7.4.8 Page information segment syntax and 8.1 Decoder model steps 1) - 3).
  2018. // "1) Decode the page information segment.""
  2019. auto page_information = TRY(decode_page_information_segment(segment.data));
  2020. bool page_is_striped = (page_information.striping_information & 0x80) != 0;
  2021. if (page_information.bitmap_height == 0xffff'ffff && !page_is_striped)
  2022. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Non-striped bitmaps of indeterminate height not allowed");
  2023. u16 maximum_stripe_height = page_information.striping_information & 0x7F;
  2024. u8 default_color = (page_information.flags >> 2) & 1;
  2025. u8 default_combination_operator = (page_information.flags >> 3) & 3;
  2026. context.page.default_combination_operator = static_cast<CombinationOperator>(default_combination_operator);
  2027. dbgln_if(JBIG2_DEBUG, "Page information: width={}, height={}, is_striped={}, max_stripe_height={}, default_color={}, default_combination_operator={}", page_information.bitmap_width, page_information.bitmap_height, page_is_striped, maximum_stripe_height, default_color, default_combination_operator);
  2028. // FIXME: Do something with the other fields in page_information.
  2029. // "2) Create the page buffer, of the size given in the page information segment.
  2030. //
  2031. // If the page height is unknown, then this is not possible. However, in this case the page must be striped,
  2032. // and the maximum stripe height specified, and the initial page buffer can be created with height initially
  2033. // equal to this maximum stripe height."
  2034. size_t height = page_information.bitmap_height;
  2035. if (height == 0xffff'ffff)
  2036. height = maximum_stripe_height;
  2037. context.page.bits = TRY(BitBuffer::create(page_information.bitmap_width, height));
  2038. // "3) Fill the page buffer with the page's default pixel value."
  2039. context.page.bits->fill(default_color != 0);
  2040. return {};
  2041. }
  2042. static ErrorOr<void> decode_end_of_page(JBIG2LoadingContext&, SegmentData const& segment)
  2043. {
  2044. // 7.4.9 End of page segment syntax
  2045. if (segment.data.size() != 0)
  2046. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of page segment has non-zero size");
  2047. // FIXME: If the page had unknown height, check that previous segment was end-of-stripe.
  2048. // FIXME: Maybe mark page as completed and error if we see more segments for it?
  2049. return {};
  2050. }
  2051. static ErrorOr<void> decode_end_of_stripe(JBIG2LoadingContext&, SegmentData const& segment)
  2052. {
  2053. // 7.4.10 End of stripe segment syntax
  2054. // "The segment data of an end of stripe segment consists of one four-byte value, specifying the Y coordinate of the end row."
  2055. if (segment.data.size() != 4)
  2056. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of strip segment has wrong size");
  2057. // FIXME: Once we implement support for images with initially indeterminate height, we need these values to determine the height at the end.
  2058. u32 y_coordinate = *reinterpret_cast<BigEndian<u32> const*>(segment.data.data());
  2059. dbgln_if(JBIG2_DEBUG, "End of stripe: y={}", y_coordinate);
  2060. return {};
  2061. }
  2062. static ErrorOr<void> decode_end_of_file(JBIG2LoadingContext&, SegmentData const& segment)
  2063. {
  2064. // 7.4.11 End of file segment syntax
  2065. if (segment.data.size() != 0)
  2066. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment has non-zero size");
  2067. return {};
  2068. }
  2069. static ErrorOr<void> decode_profiles(JBIG2LoadingContext&, SegmentData const&)
  2070. {
  2071. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode profiles yet");
  2072. }
  2073. static ErrorOr<void> decode_tables(JBIG2LoadingContext&, SegmentData const&)
  2074. {
  2075. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode tables yet");
  2076. }
  2077. static ErrorOr<void> decode_color_palette(JBIG2LoadingContext&, SegmentData const&)
  2078. {
  2079. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode color palette yet");
  2080. }
  2081. static ErrorOr<void> decode_extension(JBIG2LoadingContext&, SegmentData const& segment)
  2082. {
  2083. // 7.4.14 Extension segment syntax
  2084. FixedMemoryStream stream { segment.data };
  2085. enum ExtensionType {
  2086. SingleByteCodedComment = 0x20000000,
  2087. MultiByteCodedComment = 0x20000002,
  2088. };
  2089. u32 type = TRY(stream.read_value<BigEndian<u32>>());
  2090. auto read_string = [&]<class T>() -> ErrorOr<Vector<T>> {
  2091. Vector<T> result;
  2092. do {
  2093. result.append(TRY(stream.read_value<BigEndian<T>>()));
  2094. } while (result.last());
  2095. result.take_last();
  2096. return result;
  2097. };
  2098. switch (type) {
  2099. case SingleByteCodedComment: {
  2100. // 7.4.15.1 Single-byte coded comment
  2101. // Pairs of zero-terminated ISO/IEC 8859-1 (latin1) pairs, terminated by another \0.
  2102. while (true) {
  2103. auto first_bytes = TRY(read_string.template operator()<u8>());
  2104. if (first_bytes.is_empty())
  2105. break;
  2106. auto second_bytes = TRY(read_string.template operator()<u8>());
  2107. auto first = TRY(TextCodec::decoder_for_exact_name("ISO-8859-1"sv)->to_utf8(StringView { first_bytes }));
  2108. auto second = TRY(TextCodec::decoder_for_exact_name("ISO-8859-1"sv)->to_utf8(StringView { second_bytes }));
  2109. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  2110. }
  2111. if (!stream.is_eof())
  2112. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after SingleByteCodedComment");
  2113. return {};
  2114. }
  2115. case MultiByteCodedComment: {
  2116. // 7.4.15.2 Multi-byte coded comment
  2117. // Pairs of (two-byte-)zero-terminated UCS-2 pairs, terminated by another \0\0.
  2118. while (true) {
  2119. auto first_ucs2 = TRY(read_string.template operator()<u16>());
  2120. if (first_ucs2.is_empty())
  2121. break;
  2122. auto second_ucs2 = TRY(read_string.template operator()<u16>());
  2123. auto first = TRY(Utf16View(first_ucs2).to_utf8());
  2124. auto second = TRY(Utf16View(second_ucs2).to_utf8());
  2125. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  2126. }
  2127. if (!stream.is_eof())
  2128. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after MultiByteCodedComment");
  2129. return {};
  2130. }
  2131. }
  2132. // FIXME: If bit 31 in `type` is not set, the extension isn't necessary, and we could ignore it.
  2133. dbgln("JBIG2ImageDecoderPlugin: Unknown extension type {:#x}", type);
  2134. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown extension type");
  2135. }
  2136. static ErrorOr<void> decode_data(JBIG2LoadingContext& context)
  2137. {
  2138. TRY(warn_about_multiple_pages(context));
  2139. for (size_t i = 0; i < context.segments.size(); ++i) {
  2140. auto& segment = context.segments[i];
  2141. if (segment.header.page_association != 0 && segment.header.page_association != 1)
  2142. continue;
  2143. switch (segment.header.type) {
  2144. case SegmentType::SymbolDictionary:
  2145. TRY(decode_symbol_dictionary(context, segment));
  2146. break;
  2147. case SegmentType::IntermediateTextRegion:
  2148. TRY(decode_intermediate_text_region(context, segment));
  2149. break;
  2150. case SegmentType::ImmediateTextRegion:
  2151. case SegmentType::ImmediateLosslessTextRegion:
  2152. // 7.4.3 Text region segment syntax
  2153. // "The data parts of all three of the text region segment types ("intermediate text region", "immediate text region" and
  2154. // "immediate lossless text region") are coded identically, but are acted upon differently, see 8.2."
  2155. // But 8.2 only describes a difference between intermediate and immediate regions as far as I can tell,
  2156. // and calling the immediate text region handler for immediate lossless text regions seems to do the right thing (?).
  2157. TRY(decode_immediate_text_region(context, segment));
  2158. break;
  2159. case SegmentType::PatternDictionary:
  2160. TRY(decode_pattern_dictionary(context, segment));
  2161. break;
  2162. case SegmentType::IntermediateHalftoneRegion:
  2163. TRY(decode_intermediate_halftone_region(context, segment));
  2164. break;
  2165. case SegmentType::ImmediateHalftoneRegion:
  2166. TRY(decode_immediate_halftone_region(context, segment));
  2167. break;
  2168. case SegmentType::ImmediateLosslessHalftoneRegion:
  2169. TRY(decode_immediate_lossless_halftone_region(context, segment));
  2170. break;
  2171. case SegmentType::IntermediateGenericRegion:
  2172. TRY(decode_intermediate_generic_region(context, segment));
  2173. break;
  2174. case SegmentType::ImmediateGenericRegion:
  2175. case SegmentType::ImmediateLosslessGenericRegion:
  2176. // 7.4.6 Generic region segment syntax
  2177. // "The data parts of all three of the generic region segment types ("intermediate generic region", "immediate generic region" and
  2178. // "immediate lossless generic region") are coded identically, but are acted upon differently, see 8.2."
  2179. // But 8.2 only describes a difference between intermediate and immediate regions as far as I can tell,
  2180. // and calling the immediate generic region handler for immediate generic lossless regions seems to do the right thing (?).
  2181. TRY(decode_immediate_generic_region(context, segment));
  2182. break;
  2183. case SegmentType::IntermediateGenericRefinementRegion:
  2184. TRY(decode_intermediate_generic_refinement_region(context, segment));
  2185. break;
  2186. case SegmentType::ImmediateGenericRefinementRegion:
  2187. TRY(decode_immediate_generic_refinement_region(context, segment));
  2188. break;
  2189. case SegmentType::ImmediateLosslessGenericRefinementRegion:
  2190. TRY(decode_immediate_lossless_generic_refinement_region(context, segment));
  2191. break;
  2192. case SegmentType::PageInformation:
  2193. TRY(decode_page_information(context, segment));
  2194. break;
  2195. case SegmentType::EndOfPage:
  2196. TRY(decode_end_of_page(context, segment));
  2197. break;
  2198. case SegmentType::EndOfStripe:
  2199. TRY(decode_end_of_stripe(context, segment));
  2200. break;
  2201. case SegmentType::EndOfFile:
  2202. TRY(decode_end_of_file(context, segment));
  2203. // "If a file contains an end of file segment, it must be the last segment."
  2204. if (i != context.segments.size() - 1)
  2205. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment not last segment");
  2206. break;
  2207. case SegmentType::Profiles:
  2208. TRY(decode_profiles(context, segment));
  2209. break;
  2210. case SegmentType::Tables:
  2211. TRY(decode_tables(context, segment));
  2212. break;
  2213. case SegmentType::ColorPalette:
  2214. TRY(decode_color_palette(context, segment));
  2215. break;
  2216. case SegmentType::Extension:
  2217. TRY(decode_extension(context, segment));
  2218. break;
  2219. }
  2220. }
  2221. return {};
  2222. }
  2223. JBIG2ImageDecoderPlugin::JBIG2ImageDecoderPlugin()
  2224. {
  2225. m_context = make<JBIG2LoadingContext>();
  2226. }
  2227. JBIG2ImageDecoderPlugin::~JBIG2ImageDecoderPlugin() = default;
  2228. IntSize JBIG2ImageDecoderPlugin::size()
  2229. {
  2230. return m_context->page.size;
  2231. }
  2232. bool JBIG2ImageDecoderPlugin::sniff(ReadonlyBytes data)
  2233. {
  2234. return data.starts_with(id_string);
  2235. }
  2236. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JBIG2ImageDecoderPlugin::create(ReadonlyBytes data)
  2237. {
  2238. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  2239. TRY(decode_jbig2_header(*plugin->m_context, data));
  2240. data = data.slice(sizeof(id_string) + sizeof(u8) + (plugin->m_context->number_of_pages.has_value() ? sizeof(u32) : 0));
  2241. TRY(decode_segment_headers(*plugin->m_context, data));
  2242. TRY(scan_for_page_size(*plugin->m_context));
  2243. return plugin;
  2244. }
  2245. ErrorOr<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  2246. {
  2247. // FIXME: Use this for multi-page JBIG2 files?
  2248. if (index != 0)
  2249. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid frame index");
  2250. if (m_context->state == JBIG2LoadingContext::State::Error)
  2251. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoding failed");
  2252. if (m_context->state < JBIG2LoadingContext::State::Decoded) {
  2253. auto result = decode_data(*m_context);
  2254. if (result.is_error()) {
  2255. m_context->state = JBIG2LoadingContext::State::Error;
  2256. return result.release_error();
  2257. }
  2258. m_context->state = JBIG2LoadingContext::State::Decoded;
  2259. }
  2260. auto bitmap = TRY(m_context->page.bits->to_gfx_bitmap());
  2261. return ImageFrameDescriptor { move(bitmap), 0 };
  2262. }
  2263. ErrorOr<ByteBuffer> JBIG2ImageDecoderPlugin::decode_embedded(Vector<ReadonlyBytes> data)
  2264. {
  2265. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  2266. plugin->m_context->organization = Organization::Embedded;
  2267. for (auto const& segment_data : data)
  2268. TRY(decode_segment_headers(*plugin->m_context, segment_data));
  2269. TRY(scan_for_page_size(*plugin->m_context));
  2270. TRY(decode_data(*plugin->m_context));
  2271. return plugin->m_context->page.bits->to_byte_buffer();
  2272. }
  2273. }