JBIG2Loader.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * Copyright (c) 2024, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Utf16View.h>
  8. #include <LibGfx/ImageFormats/CCITTDecoder.h>
  9. #include <LibGfx/ImageFormats/JBIG2Loader.h>
  10. #include <LibTextCodec/Decoder.h>
  11. // Spec: ITU-T_T_88__08_2018.pdf in the zip file here:
  12. // https://www.itu.int/rec/T-REC-T.88-201808-I
  13. // Annex H has a datastream example.
  14. namespace Gfx {
  15. namespace JBIG2 {
  16. // Table E.1 – Qe values and probability estimation process
  17. // See also E.1.2 Coding conventions and approximations
  18. // and E.2.5 Probability estimation.
  19. struct QeEntry {
  20. u16 qe; // Sub-interval for the less probable symbol.
  21. u16 nmps; // Next index if the more probable symbol is decoded
  22. u16 nlps; // Next index if the less probable symbol is decoded
  23. u16 switch_flag; // See second-to-last paragraph in E.1.2.
  24. };
  25. constexpr auto qe_table = to_array<QeEntry>({
  26. { 0x5601, 1, 1, 1 },
  27. { 0x3401, 2, 6, 0 },
  28. { 0x1801, 3, 9, 0 },
  29. { 0x0AC1, 4, 12, 0 },
  30. { 0x0521, 5, 29, 0 },
  31. { 0x0221, 38, 33, 0 },
  32. { 0x5601, 7, 6, 1 },
  33. { 0x5401, 8, 14, 0 },
  34. { 0x4801, 9, 14, 0 },
  35. { 0x3801, 10, 14, 0 },
  36. { 0x3001, 11, 17, 0 },
  37. { 0x2401, 12, 18, 0 },
  38. { 0x1C01, 13, 20, 0 },
  39. { 0x1601, 29, 21, 0 },
  40. { 0x5601, 15, 14, 1 },
  41. { 0x5401, 16, 14, 0 },
  42. { 0x5101, 17, 15, 0 },
  43. { 0x4801, 18, 16, 0 },
  44. { 0x3801, 19, 17, 0 },
  45. { 0x3401, 20, 18, 0 },
  46. { 0x3001, 21, 19, 0 },
  47. { 0x2801, 22, 19, 0 },
  48. { 0x2401, 23, 20, 0 },
  49. { 0x2201, 24, 21, 0 },
  50. { 0x1C01, 25, 22, 0 },
  51. { 0x1801, 26, 23, 0 },
  52. { 0x1601, 27, 24, 0 },
  53. { 0x1401, 28, 25, 0 },
  54. { 0x1201, 29, 26, 0 },
  55. { 0x1101, 30, 27, 0 },
  56. { 0x0AC1, 31, 28, 0 },
  57. { 0x09C1, 32, 29, 0 },
  58. { 0x08A1, 33, 30, 0 },
  59. { 0x0521, 34, 31, 0 },
  60. { 0x0441, 35, 32, 0 },
  61. { 0x02A1, 36, 33, 0 },
  62. { 0x0221, 37, 34, 0 },
  63. { 0x0141, 38, 35, 0 },
  64. { 0x0111, 39, 36, 0 },
  65. { 0x0085, 40, 37, 0 },
  66. { 0x0049, 41, 38, 0 },
  67. { 0x0025, 42, 39, 0 },
  68. { 0x0015, 43, 40, 0 },
  69. { 0x0009, 44, 41, 0 },
  70. { 0x0005, 45, 42, 0 },
  71. { 0x0001, 45, 43, 0 },
  72. { 0x5601, 46, 46, 0 },
  73. });
  74. ErrorOr<ArithmeticDecoder> ArithmeticDecoder::initialize(ReadonlyBytes data)
  75. {
  76. ArithmeticDecoder decoder { data };
  77. decoder.INITDEC();
  78. return decoder;
  79. }
  80. bool ArithmeticDecoder::get_next_bit(Context& context)
  81. {
  82. CX = &context;
  83. // Useful for comparing to Table H.1 – Encoder and decoder trace data.
  84. // dbg("I={} MPS={} A={:#x} C={:#x} CT={} B={:#x}", I(CX), MPS(CX), A, C, CT, B());
  85. u8 D = DECODE();
  86. // dbgln(" -> D={}", D);
  87. return D;
  88. }
  89. u16 ArithmeticDecoder::Qe(u16 index) { return qe_table[index].qe; }
  90. u8 ArithmeticDecoder::NMPS(u16 index) { return qe_table[index].nmps; }
  91. u8 ArithmeticDecoder::NLPS(u16 index) { return qe_table[index].nlps; }
  92. u8 ArithmeticDecoder::SWITCH(u16 index) { return qe_table[index].switch_flag; }
  93. u8 ArithmeticDecoder::B(size_t offset) const
  94. {
  95. // E.2.10 Minimization of the compressed data
  96. // "the convention is used in the decoder that when a marker code is encountered,
  97. // 1-bits (without bit stuffing) are supplied to the decoder until the coding interval is complete."
  98. if (BP + offset >= m_data.size())
  99. return 0xFF;
  100. return m_data[BP + offset];
  101. }
  102. void ArithmeticDecoder::INITDEC()
  103. {
  104. // E.3.5 Initialization of the decoder (INITDEC)
  105. // Figure G.1 – Initialization of the software conventions decoder
  106. // "BP, the pointer to the compressed data, is initialized to BPST (pointing to the first compressed byte)."
  107. auto const BPST = 0;
  108. BP = BPST;
  109. C = (B() ^ 0xFF) << 16;
  110. BYTEIN();
  111. C = C << 7;
  112. CT = CT - 7;
  113. A = 0x8000;
  114. }
  115. u8 ArithmeticDecoder::DECODE()
  116. {
  117. // E.3.2 Decoding a decision (DECODE)
  118. // Figure G.2 – Decoding an MPS or an LPS in the software-conventions decoder
  119. u8 D;
  120. A = A - Qe(I(CX));
  121. if (C < ((u32)A << 16)) { // `(C_high < A)` in spec
  122. if ((A & 0x8000) == 0) {
  123. D = MPS_EXCHANGE();
  124. RENORMD();
  125. } else {
  126. D = MPS(CX);
  127. }
  128. } else {
  129. C = C - ((u32)A << 16); // `C_high = C_high - A` in spec
  130. D = LPS_EXCHANGE();
  131. RENORMD();
  132. }
  133. return D;
  134. }
  135. u8 ArithmeticDecoder::MPS_EXCHANGE()
  136. {
  137. // Figure E.16 – Decoder MPS path conditional exchange procedure
  138. u8 D;
  139. if (A < Qe(I(CX))) {
  140. D = 1 - MPS(CX);
  141. if (SWITCH(I(CX)) == 1) {
  142. MPS(CX) = 1 - MPS(CX);
  143. }
  144. I(CX) = NLPS(I(CX));
  145. } else {
  146. D = MPS(CX);
  147. I(CX) = NMPS(I(CX));
  148. }
  149. return D;
  150. }
  151. u8 ArithmeticDecoder::LPS_EXCHANGE()
  152. {
  153. // Figure E.17 – Decoder LPS path conditional exchange procedure
  154. u8 D;
  155. if (A < Qe(I(CX))) {
  156. A = Qe(I(CX));
  157. D = MPS(CX);
  158. I(CX) = NMPS(I(CX));
  159. } else {
  160. A = Qe(I(CX));
  161. D = 1 - MPS(CX);
  162. if (SWITCH(I(CX)) == 1) {
  163. MPS(CX) = 1 - MPS(CX);
  164. }
  165. I(CX) = NLPS(I(CX));
  166. }
  167. return D;
  168. }
  169. void ArithmeticDecoder::RENORMD()
  170. {
  171. // E.3.3 Renormalization in the decoder (RENORMD)
  172. // Figure E.18 – Decoder renormalization procedure
  173. do {
  174. if (CT == 0)
  175. BYTEIN();
  176. A = A << 1;
  177. C = C << 1;
  178. CT = CT - 1;
  179. } while ((A & 0x8000) == 0);
  180. }
  181. void ArithmeticDecoder::BYTEIN()
  182. {
  183. // E.3.4 Compressed data input (BYTEIN)
  184. // Figure G.3 – Inserting a new byte into the C register in the software-conventions decoder
  185. if (B() == 0xFF) {
  186. if (B(1) > 0x8F) {
  187. CT = 8;
  188. } else {
  189. BP = BP + 1;
  190. C = C + 0xFE00 - (B() << 9);
  191. CT = 7;
  192. }
  193. } else {
  194. BP = BP + 1;
  195. C = C + 0xFF00 - (B() << 8);
  196. CT = 8;
  197. }
  198. }
  199. // Annex A, Arithmetic integer decoding procedure
  200. class ArithmeticIntegerDecoder {
  201. public:
  202. ArithmeticIntegerDecoder(ArithmeticDecoder&);
  203. // A.2 Procedure for decoding values (except IAID)
  204. // Returns OptionalNone for OOB.
  205. Optional<i32> decode();
  206. private:
  207. ArithmeticDecoder& m_decoder;
  208. u16 PREV { 0 };
  209. Vector<ArithmeticDecoder::Context> contexts;
  210. };
  211. ArithmeticIntegerDecoder::ArithmeticIntegerDecoder(ArithmeticDecoder& decoder)
  212. : m_decoder(decoder)
  213. {
  214. contexts.resize(1 << 9);
  215. }
  216. Optional<int> ArithmeticIntegerDecoder::decode()
  217. {
  218. // A.2 Procedure for decoding values (except IAID)
  219. // "1) Set:
  220. // PREV = 1"
  221. u16 PREV = 1;
  222. // "2) Follow the flowchart in Figure A.1. Decode each bit with CX equal to "IAx + PREV" where "IAx" represents the identifier
  223. // of the current arithmetic integer decoding procedure, "+" represents concatenation, and the rightmost 9 bits of PREV are used."
  224. auto decode_bit = [&]() {
  225. bool D = m_decoder.get_next_bit(contexts[PREV & 0x1FF]);
  226. // "3) After each bit is decoded:
  227. // If PREV < 256 set:
  228. // PREV = (PREV << 1) OR D
  229. // Otherwise set:
  230. // PREV = (((PREV << 1) OR D) AND 511) OR 256
  231. // where D represents the value of the just-decoded bit.
  232. if (PREV < 256)
  233. PREV = (PREV << 1) | (u16)D;
  234. else
  235. PREV = (((PREV << 1) | (u16)D) & 511) | 256;
  236. return D;
  237. };
  238. auto decode_bits = [&](int n) {
  239. u32 result = 0;
  240. for (int i = 0; i < n; ++i)
  241. result = (result << 1) | decode_bit();
  242. return result;
  243. };
  244. // Figure A.1 – Flowchart for the integer arithmetic decoding procedures (except IAID)
  245. u8 S = decode_bit();
  246. u32 V;
  247. if (!decode_bit())
  248. V = decode_bits(2);
  249. else if (!decode_bit())
  250. V = decode_bits(4) + 4;
  251. else if (!decode_bit())
  252. V = decode_bits(6) + 20;
  253. else if (!decode_bit())
  254. V = decode_bits(8) + 84;
  255. else if (!decode_bit())
  256. V = decode_bits(12) + 340;
  257. else
  258. V = decode_bits(32) + 4436;
  259. // "4) The sequence of bits decoded, interpreted according to Table A.1, gives the value that is the result of this invocation
  260. // of the integer arithmetic decoding procedure."
  261. if (S == 1 && V == 0)
  262. return {};
  263. return S ? -V : V;
  264. }
  265. }
  266. // JBIG2 spec, Annex D, D.4.1 ID string
  267. static constexpr u8 id_string[] = { 0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A };
  268. // 7.3 Segment types
  269. enum SegmentType {
  270. SymbolDictionary = 0,
  271. IntermediateTextRegion = 4,
  272. ImmediateTextRegion = 6,
  273. ImmediateLosslessTextRegion = 7,
  274. PatternDictionary = 16,
  275. IntermediateHalftoneRegion = 20,
  276. ImmediateHalftoneRegion = 22,
  277. ImmediateLosslessHalftoneRegion = 23,
  278. IntermediateGenericRegion = 36,
  279. ImmediateGenericRegion = 38,
  280. ImmediateLosslessGenericRegion = 39,
  281. IntermediateGenericRefinementRegion = 40,
  282. ImmediateGenericRefinementRegion = 42,
  283. ImmediateLosslessGenericRefinementRegion = 43,
  284. PageInformation = 48,
  285. EndOfPage = 49,
  286. EndOfStripe = 50,
  287. EndOfFile = 51,
  288. Profiles = 52,
  289. Tables = 53,
  290. ColorPalette = 54,
  291. Extension = 62,
  292. };
  293. // Annex D
  294. enum class Organization {
  295. // D.1 Sequential organization
  296. Sequential,
  297. // D.2 Random-access organization
  298. RandomAccess,
  299. // D.3 Embedded organization
  300. Embedded,
  301. };
  302. struct SegmentHeader {
  303. u32 segment_number { 0 };
  304. SegmentType type { SegmentType::Extension };
  305. Vector<u32> referred_to_segment_numbers;
  306. // 7.2.6 Segment page association
  307. // "The first page must be numbered "1". This field may contain a value of zero; this value indicates that this segment is not associated with any page."
  308. u32 page_association { 0 };
  309. Optional<u32> data_length;
  310. };
  311. class BitBuffer {
  312. public:
  313. static ErrorOr<NonnullOwnPtr<BitBuffer>> create(size_t width, size_t height);
  314. bool get_bit(size_t x, size_t y) const;
  315. void set_bit(size_t x, size_t y, bool b);
  316. void fill(bool b);
  317. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_gfx_bitmap() const;
  318. ErrorOr<ByteBuffer> to_byte_buffer() const;
  319. size_t width() const { return m_width; }
  320. size_t height() const { return m_height; }
  321. private:
  322. BitBuffer(ByteBuffer, size_t width, size_t height, size_t pitch);
  323. ByteBuffer m_bits;
  324. size_t m_width { 0 };
  325. size_t m_height { 0 };
  326. size_t m_pitch { 0 };
  327. };
  328. ErrorOr<NonnullOwnPtr<BitBuffer>> BitBuffer::create(size_t width, size_t height)
  329. {
  330. size_t pitch = ceil_div(width, 8ull);
  331. auto bits = TRY(ByteBuffer::create_uninitialized(pitch * height));
  332. return adopt_nonnull_own_or_enomem(new (nothrow) BitBuffer(move(bits), width, height, pitch));
  333. }
  334. bool BitBuffer::get_bit(size_t x, size_t y) const
  335. {
  336. VERIFY(x < m_width);
  337. VERIFY(y < m_height);
  338. size_t byte_offset = x / 8;
  339. size_t bit_offset = x % 8;
  340. u8 byte = m_bits[y * m_pitch + byte_offset];
  341. byte = (byte >> (8 - 1 - bit_offset)) & 1;
  342. return byte != 0;
  343. }
  344. void BitBuffer::set_bit(size_t x, size_t y, bool b)
  345. {
  346. VERIFY(x < m_width);
  347. VERIFY(y < m_height);
  348. size_t byte_offset = x / 8;
  349. size_t bit_offset = x % 8;
  350. u8 byte = m_bits[y * m_pitch + byte_offset];
  351. u8 mask = 1u << (8 - 1 - bit_offset);
  352. if (b)
  353. byte |= mask;
  354. else
  355. byte &= ~mask;
  356. m_bits[y * m_pitch + byte_offset] = byte;
  357. }
  358. void BitBuffer::fill(bool b)
  359. {
  360. u8 fill_byte = b ? 0xff : 0;
  361. for (auto& byte : m_bits.bytes())
  362. byte = fill_byte;
  363. }
  364. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> BitBuffer::to_gfx_bitmap() const
  365. {
  366. auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { m_width, m_height }));
  367. for (size_t y = 0; y < m_height; ++y) {
  368. for (size_t x = 0; x < m_width; ++x) {
  369. auto color = get_bit(x, y) ? Color::Black : Color::White;
  370. bitmap->set_pixel(x, y, color);
  371. }
  372. }
  373. return bitmap;
  374. }
  375. ErrorOr<ByteBuffer> BitBuffer::to_byte_buffer() const
  376. {
  377. return ByteBuffer::copy(m_bits);
  378. }
  379. BitBuffer::BitBuffer(ByteBuffer bits, size_t width, size_t height, size_t pitch)
  380. : m_bits(move(bits))
  381. , m_width(width)
  382. , m_height(height)
  383. , m_pitch(pitch)
  384. {
  385. }
  386. class Symbol : public RefCounted<Symbol> {
  387. public:
  388. static NonnullRefPtr<Symbol> create(NonnullOwnPtr<BitBuffer> bitmap)
  389. {
  390. return adopt_ref(*new Symbol(move(bitmap)));
  391. }
  392. BitBuffer const& bitmap() const { return *m_bitmap; }
  393. private:
  394. Symbol(NonnullOwnPtr<BitBuffer> bitmap)
  395. : m_bitmap(move(bitmap))
  396. {
  397. }
  398. NonnullOwnPtr<BitBuffer> m_bitmap;
  399. };
  400. struct SegmentData {
  401. SegmentHeader header;
  402. ReadonlyBytes data;
  403. // Set on dictionary segments after they've been decoded.
  404. Optional<Vector<NonnullRefPtr<Symbol>>> symbols;
  405. };
  406. // 7.4.8.5 Page segment flags
  407. enum class CombinationOperator {
  408. Or = 0,
  409. And = 1,
  410. Xor = 2,
  411. XNor = 3,
  412. Replace = 4,
  413. };
  414. static void composite_bitbuffer(BitBuffer& out, BitBuffer const& bitmap, Gfx::IntPoint position, CombinationOperator operator_)
  415. {
  416. size_t start_x = 0, end_x = bitmap.width();
  417. size_t start_y = 0, end_y = bitmap.height();
  418. if (position.x() < 0) {
  419. start_x = -position.x();
  420. position.set_x(0);
  421. }
  422. if (position.y() < 0) {
  423. start_y = -position.y();
  424. position.set_y(0);
  425. }
  426. if (position.x() + bitmap.width() > out.width())
  427. end_x = out.width() - position.x();
  428. if (position.y() + bitmap.height() > out.height())
  429. end_y = out.height() - position.y();
  430. for (size_t y = start_y; y < end_y; ++y) {
  431. for (size_t x = start_x; x < end_x; ++x) {
  432. bool bit = bitmap.get_bit(x, y);
  433. switch (operator_) {
  434. case CombinationOperator::Or:
  435. bit = bit || out.get_bit(position.x() + x, position.y() + y);
  436. break;
  437. case CombinationOperator::And:
  438. bit = bit && out.get_bit(position.x() + x, position.y() + y);
  439. break;
  440. case CombinationOperator::Xor:
  441. bit = bit ^ out.get_bit(position.x() + x, position.y() + y);
  442. break;
  443. case CombinationOperator::XNor:
  444. bit = !(bit ^ out.get_bit(position.x() + x, position.y() + y));
  445. break;
  446. case CombinationOperator::Replace:
  447. // Nothing to do.
  448. break;
  449. }
  450. out.set_bit(position.x() + x, position.y() + y, bit);
  451. }
  452. }
  453. }
  454. struct Page {
  455. IntSize size;
  456. // This is never CombinationOperator::Replace for Pages.
  457. CombinationOperator default_combination_operator { CombinationOperator::Or };
  458. OwnPtr<BitBuffer> bits;
  459. };
  460. struct JBIG2LoadingContext {
  461. enum class State {
  462. NotDecoded = 0,
  463. Error,
  464. Decoded,
  465. };
  466. State state { State::NotDecoded };
  467. Organization organization { Organization::Sequential };
  468. Page page;
  469. Optional<u32> number_of_pages;
  470. Vector<SegmentData> segments;
  471. HashMap<u32, u32> segments_by_number;
  472. };
  473. static ErrorOr<void> decode_jbig2_header(JBIG2LoadingContext& context, ReadonlyBytes data)
  474. {
  475. if (!JBIG2ImageDecoderPlugin::sniff(data))
  476. return Error::from_string_literal("JBIG2LoadingContext: Invalid JBIG2 header");
  477. FixedMemoryStream stream(data.slice(sizeof(id_string)));
  478. // D.4.2 File header flags
  479. u8 header_flags = TRY(stream.read_value<u8>());
  480. if (header_flags & 0b11110000)
  481. return Error::from_string_literal("JBIG2LoadingContext: Invalid header flags");
  482. context.organization = (header_flags & 1) ? Organization::Sequential : Organization::RandomAccess;
  483. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Organization: {} ({})", (int)context.organization, context.organization == Organization::Sequential ? "Sequential" : "Random-access");
  484. bool has_known_number_of_pages = (header_flags & 2) ? false : true;
  485. bool uses_templates_with_12_AT_pixels = (header_flags & 4) ? true : false;
  486. bool contains_colored_region_segments = (header_flags & 8) ? true : false;
  487. // FIXME: Do something with these?
  488. (void)uses_templates_with_12_AT_pixels;
  489. (void)contains_colored_region_segments;
  490. // D.4.3 Number of pages
  491. if (has_known_number_of_pages) {
  492. context.number_of_pages = TRY(stream.read_value<BigEndian<u32>>());
  493. dbgln_if(JBIG2_DEBUG, "JBIG2LoadingContext: Number of pages: {}", context.number_of_pages.value());
  494. }
  495. return {};
  496. }
  497. static ErrorOr<SegmentHeader> decode_segment_header(SeekableStream& stream)
  498. {
  499. // 7.2.2 Segment number
  500. u32 segment_number = TRY(stream.read_value<BigEndian<u32>>());
  501. dbgln_if(JBIG2_DEBUG, "Segment number: {}", segment_number);
  502. // 7.2.3 Segment header flags
  503. u8 flags = TRY(stream.read_value<u8>());
  504. SegmentType type = static_cast<SegmentType>(flags & 0b11'1111);
  505. dbgln_if(JBIG2_DEBUG, "Segment type: {}", (int)type);
  506. bool segment_page_association_size_is_32_bits = (flags & 0b100'0000) != 0;
  507. bool segment_retained_only_by_itself_and_extension_segments = (flags & 0b1000'00000) != 0;
  508. // FIXME: Do something with these.
  509. (void)segment_page_association_size_is_32_bits;
  510. (void)segment_retained_only_by_itself_and_extension_segments;
  511. // 7.2.4 Referred-to segment count and retention flags
  512. u8 referred_to_segment_count_and_retention_flags = TRY(stream.read_value<u8>());
  513. u32 count_of_referred_to_segments = referred_to_segment_count_and_retention_flags >> 5;
  514. if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6)
  515. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid count_of_referred_to_segments");
  516. u32 extra_count = 0;
  517. if (count_of_referred_to_segments == 7) {
  518. TRY(stream.seek(-1, SeekMode::FromCurrentPosition));
  519. count_of_referred_to_segments = TRY(stream.read_value<BigEndian<u32>>()) & 0x1FFF'FFFF;
  520. extra_count = ceil_div(count_of_referred_to_segments + 1, 8);
  521. TRY(stream.seek(extra_count, SeekMode::FromCurrentPosition));
  522. }
  523. dbgln_if(JBIG2_DEBUG, "Referred-to segment count: {}", count_of_referred_to_segments);
  524. // 7.2.5 Referred-to segment numbers
  525. Vector<u32> referred_to_segment_numbers;
  526. for (u32 i = 0; i < count_of_referred_to_segments; ++i) {
  527. u32 referred_to_segment_number;
  528. if (segment_number <= 256)
  529. referred_to_segment_number = TRY(stream.read_value<u8>());
  530. else if (segment_number <= 65536)
  531. referred_to_segment_number = TRY(stream.read_value<BigEndian<u16>>());
  532. else
  533. referred_to_segment_number = TRY(stream.read_value<BigEndian<u32>>());
  534. referred_to_segment_numbers.append(referred_to_segment_number);
  535. dbgln_if(JBIG2_DEBUG, "Referred-to segment number: {}", referred_to_segment_number);
  536. }
  537. // 7.2.6 Segment page association
  538. u32 segment_page_association;
  539. if (segment_page_association_size_is_32_bits) {
  540. segment_page_association = TRY(stream.read_value<BigEndian<u32>>());
  541. } else {
  542. segment_page_association = TRY(stream.read_value<u8>());
  543. }
  544. dbgln_if(JBIG2_DEBUG, "Segment page association: {}", segment_page_association);
  545. // 7.2.7 Segment data length
  546. u32 data_length = TRY(stream.read_value<BigEndian<u32>>());
  547. dbgln_if(JBIG2_DEBUG, "Segment data length: {}", data_length);
  548. // FIXME: Add some validity checks:
  549. // - check type is valid
  550. // - check referred_to_segment_numbers are smaller than segment_number
  551. // - 7.3.1 Rules for segment references
  552. // - 7.3.2 Rules for page associations
  553. Optional<u32> opt_data_length;
  554. if (data_length != 0xffff'ffff)
  555. opt_data_length = data_length;
  556. else if (type != ImmediateGenericRegion)
  557. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown data length only allowed for ImmediateGenericRegion");
  558. return SegmentHeader { segment_number, type, move(referred_to_segment_numbers), segment_page_association, opt_data_length };
  559. }
  560. static ErrorOr<size_t> scan_for_immediate_generic_region_size(ReadonlyBytes data)
  561. {
  562. // 7.2.7 Segment data length
  563. // "If the segment's type is "Immediate generic region", then the length field may contain the value 0xFFFFFFFF.
  564. // 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 (...).
  565. // In this case, the true length of the segment's data part shall be determined through examination of the data:
  566. // 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.
  567. // 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.
  568. // The form of encoding used by the segment may be determined by examining the eighteenth byte of its segment data part,
  569. // and the end sequences can occur anywhere after that eighteenth byte."
  570. // 7.4.6.4 Decoding a generic region segment
  571. // "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.
  572. // Thus, those sequences cannot occur by chance in the data that is decoded to generate the contents of the generic region."
  573. dbgln_if(JBIG2_DEBUG, "(Unknown data length, computing it)");
  574. if (data.size() < 19 + sizeof(u32))
  575. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Data too short to contain segment data header and end sequence");
  576. // Per 7.4.6.1 Generic region segment data header, this starts with the 17 bytes described in
  577. // 7.4.1 Region segment information field, followed the byte described in 7.4.6.2 Generic region segment flags.
  578. // That byte's lowest bit stores if the segment uses MMR.
  579. u8 flags = data[17];
  580. bool uses_mmr = (flags & 1) != 0;
  581. auto end_sequence = uses_mmr ? to_array<u8>({ 0x00, 0x00 }) : to_array<u8>({ 0xFF, 0xAC });
  582. u8 const* end = static_cast<u8 const*>(memmem(data.data() + 19, data.size() - 19 - sizeof(u32), end_sequence.data(), end_sequence.size()));
  583. if (!end)
  584. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Could not find end sequence in segment data");
  585. size_t size = end - data.data() + end_sequence.size() + sizeof(u32);
  586. dbgln_if(JBIG2_DEBUG, "(Computed size is {})", size);
  587. return size;
  588. }
  589. static ErrorOr<void> decode_segment_headers(JBIG2LoadingContext& context, ReadonlyBytes data)
  590. {
  591. FixedMemoryStream stream(data);
  592. Vector<ReadonlyBytes> segment_datas;
  593. auto store_and_skip_segment_data = [&](SegmentHeader const& segment_header) -> ErrorOr<void> {
  594. size_t start_offset = TRY(stream.tell());
  595. u32 data_length = TRY(segment_header.data_length.try_value_or_lazy_evaluated([&]() {
  596. return scan_for_immediate_generic_region_size(data.slice(start_offset));
  597. }));
  598. if (start_offset + data_length > data.size()) {
  599. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: start_offset={}, data_length={}, data.size()={}", start_offset, data_length, data.size());
  600. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment data length exceeds file size");
  601. }
  602. ReadonlyBytes segment_data = data.slice(start_offset, data_length);
  603. segment_datas.append(segment_data);
  604. TRY(stream.seek(data_length, SeekMode::FromCurrentPosition));
  605. return {};
  606. };
  607. Vector<SegmentHeader> segment_headers;
  608. while (!stream.is_eof()) {
  609. auto segment_header = TRY(decode_segment_header(stream));
  610. segment_headers.append(segment_header);
  611. if (context.organization != Organization::RandomAccess)
  612. TRY(store_and_skip_segment_data(segment_header));
  613. // Required per spec for files with RandomAccess organization.
  614. if (segment_header.type == SegmentType::EndOfFile)
  615. break;
  616. }
  617. if (context.organization == Organization::RandomAccess) {
  618. for (auto const& segment_header : segment_headers)
  619. TRY(store_and_skip_segment_data(segment_header));
  620. }
  621. if (segment_headers.size() != segment_datas.size())
  622. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Segment headers and segment datas have different sizes");
  623. for (size_t i = 0; i < segment_headers.size(); ++i) {
  624. context.segments.append({ segment_headers[i], segment_datas[i], {} });
  625. context.segments_by_number.set(segment_headers[i].segment_number, context.segments.size() - 1);
  626. }
  627. return {};
  628. }
  629. // 7.4.1 Region segment information field
  630. struct [[gnu::packed]] RegionSegmentInformationField {
  631. BigEndian<u32> width;
  632. BigEndian<u32> height;
  633. BigEndian<u32> x_location;
  634. BigEndian<u32> y_location;
  635. u8 flags;
  636. CombinationOperator external_combination_operator() const
  637. {
  638. VERIFY((flags & 0x7) <= 4);
  639. return static_cast<CombinationOperator>(flags & 0x7);
  640. }
  641. bool is_color_bitmap() const
  642. {
  643. return (flags & 0x8) != 0;
  644. }
  645. };
  646. static_assert(AssertSize<RegionSegmentInformationField, 17>());
  647. static ErrorOr<RegionSegmentInformationField> decode_region_segment_information_field(ReadonlyBytes data)
  648. {
  649. // 7.4.8 Page information segment syntax
  650. if (data.size() < sizeof(RegionSegmentInformationField))
  651. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field size");
  652. auto result = *(RegionSegmentInformationField const*)data.data();
  653. if ((result.flags & 0b1111'0000) != 0)
  654. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field flags");
  655. if ((result.flags & 0x7) > 4)
  656. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid region segment information field operator");
  657. // NOTE 3 – If the colour extension flag (COLEXTFLAG) is equal to 1, the external combination operator must be REPLACE.
  658. if (result.is_color_bitmap() && result.external_combination_operator() != CombinationOperator::Replace)
  659. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid colored region segment information field operator");
  660. return result;
  661. }
  662. // 7.4.8 Page information segment syntax
  663. struct [[gnu::packed]] PageInformationSegment {
  664. BigEndian<u32> bitmap_width;
  665. BigEndian<u32> bitmap_height;
  666. BigEndian<u32> page_x_resolution; // In pixels/meter.
  667. BigEndian<u32> page_y_resolution; // In pixels/meter.
  668. u8 flags;
  669. BigEndian<u16> striping_information;
  670. };
  671. static_assert(AssertSize<PageInformationSegment, 19>());
  672. static ErrorOr<PageInformationSegment> decode_page_information_segment(ReadonlyBytes data)
  673. {
  674. // 7.4.8 Page information segment syntax
  675. if (data.size() != sizeof(PageInformationSegment))
  676. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid page information segment size");
  677. return *(PageInformationSegment const*)data.data();
  678. }
  679. static ErrorOr<void> scan_for_page_size(JBIG2LoadingContext& context)
  680. {
  681. // We only decode the first page at the moment.
  682. bool found_size = false;
  683. for (auto const& segment : context.segments) {
  684. if (segment.header.type != SegmentType::PageInformation || segment.header.page_association != 1)
  685. continue;
  686. auto page_information = TRY(decode_page_information_segment(segment.data));
  687. // FIXME: We're supposed to compute this from the striping information if it's not set.
  688. if (page_information.bitmap_height == 0xffff'ffff)
  689. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle unknown page height yet");
  690. context.page.size = { page_information.bitmap_width, page_information.bitmap_height };
  691. found_size = true;
  692. }
  693. if (!found_size)
  694. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No page information segment found for page 1");
  695. return {};
  696. }
  697. static ErrorOr<void> warn_about_multiple_pages(JBIG2LoadingContext& context)
  698. {
  699. HashTable<u32> seen_pages;
  700. Vector<u32> pages;
  701. for (auto const& segment : context.segments) {
  702. if (segment.header.page_association == 0)
  703. continue;
  704. if (seen_pages.contains(segment.header.page_association))
  705. continue;
  706. seen_pages.set(segment.header.page_association);
  707. pages.append(segment.header.page_association);
  708. }
  709. // scan_for_page_size() already checked that there's a page 1.
  710. VERIFY(seen_pages.contains(1));
  711. if (pages.size() == 1)
  712. return {};
  713. StringBuilder builder;
  714. builder.appendff("JBIG2 file contains {} pages ({}", pages.size(), pages[0]);
  715. size_t i;
  716. for (i = 1; i < min(pages.size(), 10); ++i)
  717. builder.appendff(" {}", pages[i]);
  718. if (i != pages.size())
  719. builder.append(" ..."sv);
  720. builder.append("). We will only render page 1."sv);
  721. dbgln("JBIG2ImageDecoderPlugin: {}", TRY(builder.to_string()));
  722. return {};
  723. }
  724. struct AdaptiveTemplatePixel {
  725. i8 x { 0 };
  726. i8 y { 0 };
  727. };
  728. // 6.2.2 Input parameters
  729. // Table 2 – Parameters for the generic region decoding procedure
  730. struct GenericRegionDecodingInputParameters {
  731. bool is_modified_modified_read { false }; // "MMR" in spec.
  732. u32 region_width { 0 }; // "GBW" in spec.
  733. u32 region_height { 0 }; // "GBH" in spec.
  734. u8 gb_template { 0 };
  735. bool is_typical_prediction_used { false }; // "TPGDON" in spec.
  736. bool is_extended_reference_template_used { false }; // "EXTTEMPLATE" in spec.
  737. Optional<NonnullOwnPtr<BitBuffer>> skip_pattern; // "USESKIP", "SKIP" in spec.
  738. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels; // "GBATX" / "GBATY" in spec.
  739. // FIXME: GBCOLS, GBCOMBOP, COLEXTFLAG
  740. // If is_modified_modified_read is false, generic_region_decoding_procedure() reads data off this decoder.
  741. JBIG2::ArithmeticDecoder* arithmetic_decoder { nullptr };
  742. };
  743. // 6.2 Generic region decoding procedure
  744. static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(GenericRegionDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<JBIG2::ArithmeticDecoder::Context>& contexts)
  745. {
  746. if (inputs.is_modified_modified_read) {
  747. dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: MMR image data");
  748. // 6.2.6 Decoding using MMR coding
  749. auto buffer = TRY(CCITT::decode_ccitt_group4(data, inputs.region_width, inputs.region_height));
  750. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  751. size_t bytes_per_row = ceil_div(inputs.region_width, 8);
  752. if (buffer.size() != bytes_per_row * inputs.region_height)
  753. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoded MMR data has wrong size");
  754. // FIXME: Could probably just copy the ByteBuffer directly into the BitBuffer's internal ByteBuffer instead.
  755. for (size_t y = 0; y < inputs.region_height; ++y) {
  756. for (size_t x = 0; x < inputs.region_width; ++x) {
  757. bool bit = buffer[y * bytes_per_row + x / 8] & (1 << (7 - x % 8));
  758. result->set_bit(x, y, bit);
  759. }
  760. }
  761. return result;
  762. }
  763. // 6.2.5 Decoding using a template and arithmetic coding
  764. if (inputs.gb_template != 0)
  765. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode GBTEMPLATE != 0 yet");
  766. if (inputs.adaptive_template_pixels[0].x != 3 || inputs.adaptive_template_pixels[0].y != -1
  767. || inputs.adaptive_template_pixels[1].x != -3 || inputs.adaptive_template_pixels[1].y != -1
  768. || inputs.adaptive_template_pixels[2].x != 2 || inputs.adaptive_template_pixels[2].y != -2
  769. || inputs.adaptive_template_pixels[3].x != -2 || inputs.adaptive_template_pixels[3].y != -2)
  770. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle custom adaptive pixels yet");
  771. if (inputs.is_extended_reference_template_used)
  772. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode EXTTEMPLATE yet");
  773. if (inputs.skip_pattern.has_value())
  774. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode USESKIP yet");
  775. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  776. auto get_pixel = [&inputs](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> bool {
  777. if (x < 0 || x >= (int)inputs.region_width || y < 0)
  778. return false;
  779. return buffer->get_bit(x, y);
  780. };
  781. // Figure 3(a) – Template when GBTEMPLATE = 0 and EXTTEMPLATE = 0,
  782. auto compute_context = [&get_pixel](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> u16 {
  783. u16 result = 0;
  784. for (int i = 0; i < 5; ++i)
  785. result = (result << 1) | (u16)get_pixel(buffer, x - 2 + i, y - 2);
  786. for (int i = 0; i < 7; ++i)
  787. result = (result << 1) | (u16)get_pixel(buffer, x - 3 + i, y - 1);
  788. for (int i = 0; i < 4; ++i)
  789. result = (result << 1) | (u16)get_pixel(buffer, x - 4 + i, y);
  790. return result;
  791. };
  792. // "The values of the pixels in this neighbourhood define a context. Each context has its own adaptive probability estimate
  793. // used by the arithmetic coder (see Annex E)."
  794. // "* Decode the current pixel by invoking the arithmetic entropy decoding procedure, with CX set to the value formed by
  795. // concatenating the label "GB" and the 10-16 pixel values gathered in CONTEXT."
  796. // Implementor's note: What this is supposed to mean is that we have a bunch of independent contexts, and we pick the
  797. // context for the current pixel based on pixel values in the neighborhood. The "GB" part just means this context is
  798. // independent from other contexts in the spec. They are passed in to this function.
  799. // Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
  800. constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
  801. // 6.2.5.7 Decoding the bitmap
  802. JBIG2::ArithmeticDecoder& decoder = *inputs.arithmetic_decoder;
  803. bool ltp = false; // "LTP" in spec. "Line (uses) Typical Prediction" maybe?
  804. for (size_t y = 0; y < inputs.region_height; ++y) {
  805. if (inputs.is_typical_prediction_used) {
  806. // "SLTP" in spec. "Swap LTP" or "Switch LTP" maybe?
  807. bool sltp = decoder.get_next_bit(contexts[sltp_context_for_template_0]);
  808. ltp = ltp ^ sltp;
  809. if (ltp) {
  810. for (size_t x = 0; x < inputs.region_width; ++x)
  811. result->set_bit(x, y, get_pixel(result, (int)x, (int)y - 1));
  812. continue;
  813. }
  814. }
  815. for (size_t x = 0; x < inputs.region_width; ++x) {
  816. u16 context = compute_context(result, x, y);
  817. bool bit = decoder.get_next_bit(contexts[context]);
  818. result->set_bit(x, y, bit);
  819. }
  820. }
  821. return result;
  822. }
  823. // 6.4.2 Input parameters
  824. // Table 9 – Parameters for the text region decoding procedure
  825. struct TextRegionDecodingInputParameters {
  826. bool uses_huffman_encoding { false }; // "SBHUFF" in spec.
  827. bool uses_refinement_coding { false }; // "SBREFINE" in spec.
  828. u32 region_width { 0 }; // "SBW" in spec.
  829. u32 region_height { 0 }; // "SBH" in spec.
  830. u32 number_of_instances { 0 }; // "SBNUMINSTANCES" in spec.
  831. u32 size_of_symbol_instance_strips { 0 }; // "SBSTRIPS" in spec.
  832. // "SBNUMSYMS" is `symbols.size()` below.
  833. // FIXME: SBSYMCODES
  834. u32 id_symbol_code_length { 0 }; // "SBSYMCODELEN" in spec.
  835. Vector<NonnullRefPtr<Symbol>> symbols; // "SBNUMSYMS" / "SBSYMS" in spec.
  836. u8 default_pixel { 0 }; // "SBDEFPIXEL" in spec.
  837. CombinationOperator operator_ { CombinationOperator::Or }; // "SBCOMBOP" in spec.
  838. bool is_transposed { false }; // "TRANSPOSED" in spec.
  839. enum class Corner {
  840. BottomLeft = 0,
  841. TopLeft = 1,
  842. BottomRight = 2,
  843. TopRight = 3,
  844. };
  845. Corner reference_corner { Corner::TopLeft }; // "REFCORNER" in spec.
  846. i8 delta_s_offset { 0 }; // "SBDSOFFSET" in spec.
  847. // FIXME: SBHUFFFS, SBHUFFFDS, SBHUFFDT, SBHUFFRDW, SBHUFFRDH, SBHUFFRDX, SBHUFFRDY, SBHUFFRSIZE
  848. u8 refinement_template { 0 }; // "SBRTEMPLATE" in spec.
  849. Array<AdaptiveTemplatePixel, 2> refinement_adaptive_template_pixels; // "SBRATX" / "SBRATY" in spec.
  850. // FIXME: COLEXTFLAG, SBCOLS
  851. };
  852. // 6.4 Text Region Decoding Procedure
  853. static ErrorOr<NonnullOwnPtr<BitBuffer>> text_region_decoding_procedure(TextRegionDecodingInputParameters const& inputs, ReadonlyBytes data)
  854. {
  855. if (inputs.uses_huffman_encoding)
  856. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman text regions yet");
  857. if (inputs.is_transposed)
  858. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode transposed text regions yet");
  859. auto decoder = TRY(JBIG2::ArithmeticDecoder::initialize(data));
  860. // 6.4.6 Strip delta T
  861. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFDT and multiply the resulting value by SBSTRIPS.
  862. // If SBHUFF is 0, decode a value using the IADT integer arithmetic decoding procedure (see Annex A) and multiply the resulting value by SBSTRIPS."
  863. // FIXME: Implement support for SBHUFF = 1.
  864. JBIG2::ArithmeticIntegerDecoder delta_t_integer_decoder(decoder);
  865. auto read_delta_t = [&]() -> i32 {
  866. return delta_t_integer_decoder.decode().value() * inputs.size_of_symbol_instance_strips;
  867. };
  868. // 6.4.7 First symbol instance S coordinate
  869. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFFS.
  870. // If SBHUFF is 0, decode a value using the IAFS integer arithmetic decoding procedure (see Annex A)."
  871. // FIXME: Implement support for SBHUFF = 1.
  872. JBIG2::ArithmeticIntegerDecoder first_s_integer_decoder(decoder);
  873. auto read_first_s = [&]() -> i32 {
  874. return first_s_integer_decoder.decode().value();
  875. };
  876. // 6.4.8 Subsequent symbol instance S coordinate
  877. // "If SBHUFF is 1, decode a value using the Huffman table specified by SBHUFFDS.
  878. // If SBHUFF is 0, decode a value using the IADS integer arithmetic decoding procedure (see Annex A).
  879. // In either case it is possible that the result of this decoding is the out-of-band value OOB.""
  880. // FIXME: Implement support for SBHUFF = 1.
  881. JBIG2::ArithmeticIntegerDecoder subsequent_s_integer_decoder(decoder);
  882. auto read_subsequent_s = [&]() -> Optional<i32> {
  883. return subsequent_s_integer_decoder.decode();
  884. };
  885. // 6.4.9 Symbol instance T coordinate
  886. // "If SBSTRIPS == 1, then the value decoded is always zero. Otherwise:
  887. // • If SBHUFF is 1, decode a value by reading ceil(log2(SBSTRIPS)) bits directly from the bitstream.
  888. // • If SBHUFF is 0, decode a value using the IAIT integer arithmetic decoding procedure (see Annex A)."
  889. // FIXME: Implement support for SBHUFF = 1.
  890. JBIG2::ArithmeticIntegerDecoder instance_t_integer_decoder(decoder);
  891. auto read_instance_t = [&]() -> i32 {
  892. if (inputs.size_of_symbol_instance_strips == 1)
  893. return 0;
  894. return instance_t_integer_decoder.decode().value();
  895. };
  896. // 6.4.10 Symbol instance symbol ID
  897. // "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
  898. // SBSYMCODES. The resulting value, which is IDI, is the index of the entry in SBSYMCODES that is read.
  899. // If SBHUFF is 0, decode a value using the IAID integer arithmetic decoding procedure (see Annex A). Set IDI to the
  900. // resulting value.""
  901. // FIXME: Implement support for SBHUFF = 1.
  902. Vector<JBIG2::ArithmeticDecoder::Context> id_contexts;
  903. id_contexts.resize(1 << (inputs.id_symbol_code_length + 1));
  904. auto read_id = [&]() -> u32 {
  905. // A.3 The IAID decoding procedure
  906. u32 prev = 1;
  907. for (u8 i = 0; i < inputs.id_symbol_code_length; ++i) {
  908. bool bit = decoder.get_next_bit(id_contexts[prev]);
  909. prev = (prev << 1) | bit;
  910. }
  911. prev = prev - (1 << inputs.id_symbol_code_length);
  912. return prev;
  913. };
  914. // 6.4.11 Symbol instance bitmap
  915. JBIG2::ArithmeticIntegerDecoder has_refinement_image_decoder(decoder);
  916. auto read_bitmap = [&](u32 id) -> ErrorOr<BitBuffer const*> {
  917. bool has_refinement_image = false; // "R_I" in spec.
  918. if (inputs.uses_refinement_coding) {
  919. // "• If SBHUFF is 1, then read one bit and set RI to the value of that bit.
  920. // • If SBHUFF is 0, then decode one bit using the IARI integer arithmetic decoding procedure and set RI to the value of that bit."
  921. // FIXME: Implement support for SBHUFF = 1.
  922. has_refinement_image = has_refinement_image_decoder.decode().value();
  923. }
  924. if (!has_refinement_image) {
  925. if (id >= inputs.symbols.size())
  926. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Symbol ID out of range");
  927. auto const& symbol = inputs.symbols[id]->bitmap();
  928. return &symbol;
  929. }
  930. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode refined text regions yet");
  931. };
  932. // 6.4.5 Decoding the text region
  933. // "1) Fill a bitmap SBREG, of the size given by SBW and SBH, with the SBDEFPIXEL value."
  934. auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
  935. if (inputs.default_pixel != 0)
  936. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot handle SBDEFPIXEL not equal to 0 yet");
  937. result->fill(inputs.default_pixel != 0);
  938. // "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.
  939. // Assign the value 0 to FIRSTS. Assign the value 0 to NINSTANCES."
  940. i32 strip_t = -read_delta_t();
  941. i32 first_s = 0;
  942. u32 n_instances = 0;
  943. // "3) If COLEXTFLAG is 1, decode the colour section as described in 6.4.12."
  944. // FIXME: Implement support for colors one day.
  945. // "4) Decode each strip as follows:
  946. // a) If NINSTANCES is equal to SBNUMINSTANCES then there are no more strips to decode,
  947. // and the process of decoding the text region is complete; proceed to step 4)."
  948. // Implementor's note. The spec means "proceed to step 5)" at the end of 4a).
  949. while (n_instances < inputs.number_of_instances) {
  950. // "b) Decode the strip's delta T value as described in 6.4.6. Let DT be the decoded value. Set:
  951. // STRIPT = STRIPT + DT"
  952. i32 delta_t = read_delta_t();
  953. strip_t += delta_t;
  954. i32 cur_s;
  955. bool is_first_symbol = true;
  956. while (true) {
  957. // "c) Decode each symbol instance in the strip as follows:
  958. // i) If the current symbol instance is the first symbol instance in the strip, then decode the first
  959. // symbol instance's S coordinate as described in 6.4.7. Let DFS be the decoded value. Set:
  960. // FIRSTS = FIRSTS + DFS
  961. // CURS = FIRSTS
  962. // ii) Otherwise, if the current symbol instance is not the first symbol instance in the strip, decode
  963. // the symbol instance's S coordinate as described in 6.4.8. If the result of this decoding is OOB
  964. // then the last symbol instance of the strip has been decoded; proceed to step 3 d). Otherwise, let
  965. // IDS be the decoded value. Set:
  966. // CURS = CURS + IDS + SBDSOFFSET"
  967. // Implementor's note: The spec means "proceed to step 4 d)" in 4c ii).
  968. if (is_first_symbol) {
  969. i32 delta_first_s = read_first_s();
  970. first_s += delta_first_s;
  971. cur_s = first_s;
  972. is_first_symbol = false;
  973. } else {
  974. auto subsequent_s = read_subsequent_s();
  975. if (!subsequent_s.has_value())
  976. break;
  977. i32 instance_delta_s = subsequent_s.value();
  978. cur_s += instance_delta_s + inputs.delta_s_offset;
  979. }
  980. // "iii) Decode the symbol instance's T coordinate as described in 6.4.9. Let CURT be the decoded value. Set:
  981. // TI = STRIPT + CURT"
  982. i32 cur_t = read_instance_t();
  983. i32 t_instance = strip_t + cur_t;
  984. // "iv) Decode the symbol instance's symbol ID as described in 6.4.10. Let IDI be the decoded value."
  985. u32 id = read_id();
  986. // "v) Determine the symbol instance's bitmap IBI as described in 6.4.11. The width and height of this
  987. // bitmap shall be denoted as WI and HI respectively."
  988. auto const& symbol = *TRY(read_bitmap(id));
  989. // "vi) Update CURS as follows:
  990. // • If TRANSPOSED is 0, and REFCORNER is TOPRIGHT or BOTTOMRIGHT, set:
  991. // CURS = CURS + WI – 1
  992. // • If TRANSPOSED is 1, and REFCORNER is BOTTOMLEFT or BOTTOMRIGHT, set:
  993. // CURS = CURS + HI –1
  994. // • Otherwise, do not change CURS in this step."
  995. using enum TextRegionDecodingInputParameters::Corner;
  996. if (!inputs.is_transposed && (inputs.reference_corner == TopRight || inputs.reference_corner == BottomRight))
  997. cur_s += symbol.width() - 1;
  998. if (inputs.is_transposed && (inputs.reference_corner == BottomLeft || inputs.reference_corner == BottomRight))
  999. cur_s += symbol.height() - 1;
  1000. // "vii) Set:
  1001. // SI = CURS"
  1002. auto s_instance = cur_s;
  1003. // "viii) Determine the location of the symbol instance bitmap with respect to SBREG as follows:
  1004. // • If TRANSPOSED is 0, then:
  1005. // – If REFCORNER is TOPLEFT then the top left pixel of the symbol instance bitmap
  1006. // IBI shall be placed at SBREG[SI, TI].
  1007. // – If REFCORNER is TOPRIGHT then the top right pixel of the symbol instance
  1008. // bitmap IBI shall be placed at SBREG[SI, TI].
  1009. // – If REFCORNER is BOTTOMLEFT then the bottom left pixel of the symbol
  1010. // instance bitmap IBI shall be placed at SBREG[SI, TI].
  1011. // – If REFCORNER is BOTTOMRIGHT then the bottom right pixel of the symbol
  1012. // instance bitmap IBI shall be placed at SBREG[SI, TI].
  1013. // • If TRANSPOSED is 1, then:
  1014. // – If REFCORNER is TOPLEFT then the top left pixel of the symbol instance bitmap
  1015. // IBI shall be placed at SBREG[TI, SI].
  1016. // – If REFCORNER is TOPRIGHT then the top right pixel of the symbol instance
  1017. // bitmap IBI shall be placed at SBREG[TI, SI].
  1018. // – If REFCORNER is BOTTOMLEFT then the bottom left pixel of the symbol
  1019. // instance bitmap IBI shall be placed at SBREG[TI, SI].
  1020. // – If REFCORNER is BOTTOMRIGHT then the bottom right pixel of the symbol
  1021. // instance bitmap IBI shall be placed at SBREG[TI, SI].
  1022. // If any part of IBI, when placed at this location, lies outside the bounds of SBREG, then ignore
  1023. // this part of IBI in step 3 c) ix)."
  1024. // Implementor's note: The spec means "ignore this part of IBI in step 3 c) x)" in 3c viii)'s last sentence.
  1025. // FIXME: Support all reference corners and transpose values.
  1026. if (!inputs.is_transposed) {
  1027. switch (inputs.reference_corner) {
  1028. case TopLeft:
  1029. break;
  1030. case TopRight:
  1031. s_instance -= symbol.width() - 1;
  1032. break;
  1033. case BottomLeft:
  1034. t_instance -= symbol.height() - 1;
  1035. break;
  1036. case BottomRight:
  1037. s_instance -= symbol.width() - 1;
  1038. t_instance -= symbol.height() - 1;
  1039. break;
  1040. }
  1041. } else {
  1042. TODO();
  1043. }
  1044. // "ix) If COLEXTFLAG is 1, set the colour specified by SBCOLS[SBFGCOLID[NINSTANCES]]
  1045. // to the foreground colour of the symbol instance bitmap IBI."
  1046. // FIXME: Implement support for colors one day.
  1047. // "x) Draw IBI into SBREG. Combine each pixel of IBI with the current value of the corresponding
  1048. // pixel in SBREG, using the combination operator specified by SBCOMBOP. Write the results
  1049. // of each combination into that pixel in SBREG."
  1050. composite_bitbuffer(*result, symbol, { s_instance, t_instance }, inputs.operator_);
  1051. // "xi) Update CURS as follows:
  1052. // • If TRANSPOSED is 0, and REFCORNER is TOPLEFT or BOTTOMLEFT, set:
  1053. // CURS = CURS + WI –1
  1054. // • If TRANSPOSED is 1, and REFCORNER is TOPLEFT or TOPRIGHT, set:
  1055. // CURS = CURS + HI –1
  1056. // • Otherwise, do not change CURS in this step."
  1057. if (!inputs.is_transposed && (inputs.reference_corner == TopLeft || inputs.reference_corner == BottomLeft))
  1058. cur_s += symbol.width() - 1;
  1059. if (inputs.is_transposed && (inputs.reference_corner == TopLeft || inputs.reference_corner == TopRight))
  1060. cur_s += symbol.height() - 1;
  1061. // "xii) Set:
  1062. // NINSTANCES = NINSTANCES + 1"
  1063. ++n_instances;
  1064. }
  1065. // "d) When the strip has been completely decoded, decode the next strip."
  1066. // (Done in the next loop iteration.)
  1067. }
  1068. // "5) After all the strips have been decoded, the current contents of SBREG are the results that shall be
  1069. // obtained by every decoder, whether it performs this exact sequence of steps or not."
  1070. return result;
  1071. }
  1072. // 6.5.2 Input parameters
  1073. // Table 13 – Parameters for the symbol dictionary decoding procedure
  1074. struct SymbolDictionaryDecodingInputParameters {
  1075. bool uses_huffman_encoding { false }; // "SDHUFF" in spec.
  1076. bool uses_refinement_or_aggregate_coding { false }; // "SDREFAGG" in spec.
  1077. Vector<NonnullRefPtr<Symbol>> input_symbols; // "SDNUMINSYMS", "SDINSYMS" in spec.
  1078. u32 number_of_new_symbols { 0 }; // "SDNUMNEWSYMS" in spec.
  1079. u32 number_of_exported_symbols { 0 }; // "SDNUMEXSYMS" in spec.
  1080. // FIXME: SDHUFFDH, SDHUFFDW, SDHUFFBMSIZE, SDHUFFAGGINST
  1081. u8 symbol_template { 0 }; // "SDTEMPLATE" in spec.
  1082. Array<AdaptiveTemplatePixel, 4> adaptive_template_pixels; // "SDATX" / "SDATY" in spec.
  1083. u8 refinement_template { 0 }; // "SDRTEMPLATE" in spec;
  1084. Array<AdaptiveTemplatePixel, 2> refinement_adaptive_template_pixels; // "SDRATX" / "SDRATY" in spec.
  1085. };
  1086. // 6.5 Symbol Dictionary Decoding Procedure
  1087. static ErrorOr<Vector<NonnullRefPtr<Symbol>>> symbol_dictionary_decoding_procedure(SymbolDictionaryDecodingInputParameters const& inputs, ReadonlyBytes data)
  1088. {
  1089. if (inputs.uses_huffman_encoding)
  1090. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman symbol dictionaries yet");
  1091. if (inputs.uses_refinement_or_aggregate_coding)
  1092. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode SDREFAGG symbol dictionaries yet");
  1093. auto decoder = TRY(JBIG2::ArithmeticDecoder::initialize(data));
  1094. Vector<JBIG2::ArithmeticDecoder::Context> contexts;
  1095. u8 template_size = inputs.symbol_template == 0 ? 16 : (inputs.symbol_template == 1 ? 13 : 10);
  1096. contexts.resize(1 << template_size);
  1097. // 6.5.6 Height class delta height
  1098. // "If SDHUFF is 1, decode a value using the Huffman table specified by SDHUFFDH.
  1099. // If SDHUFF is 0, decode a value using the IADH integer arithmetic decoding procedure (see Annex A)."
  1100. // FIXME: Implement support for SDHUFF = 1.
  1101. JBIG2::ArithmeticIntegerDecoder delta_height_integer_decoder(decoder);
  1102. auto read_delta_height = [&]() -> i32 {
  1103. // No OOB values for delta height.
  1104. return delta_height_integer_decoder.decode().value();
  1105. };
  1106. // 6.5.7 Delta width
  1107. // "If SDHUFF is 1, decode a value using the Huffman table specified by SDHUFFDW.
  1108. // If SDHUFF is 0, decode a value using the IADW integer arithmetic decoding procedure (see Annex A).
  1109. // In either case it is possible that the result of this decoding is the out-of-band value OOB."
  1110. // FIXME: Implement support for SDHUFF = 1.
  1111. JBIG2::ArithmeticIntegerDecoder delta_width_integer_decoder(decoder);
  1112. auto read_delta_width = [&]() -> Optional<i32> {
  1113. return delta_width_integer_decoder.decode();
  1114. };
  1115. // 6.5.8 Symbol bitmap
  1116. // "This field is only present if SDHUFF = 0 or SDREFAGG = 1. This field takes one of two forms; SDREFAGG
  1117. // determines which form is used."
  1118. // FIXME: Add support for SDEFRAG = 1.
  1119. // 6.5.8.1 Direct-coded symbol bitmap
  1120. // "If SDREFAGG is 0, then decode the symbol's bitmap using a generic region decoding procedure as described in 6.2.
  1121. // Set the parameters to this decoding procedure as shown in Table 16."
  1122. auto read_bitmap = [&](u32 width, u32 height) {
  1123. GenericRegionDecodingInputParameters generic_inputs;
  1124. generic_inputs.is_modified_modified_read = false;
  1125. generic_inputs.region_width = width;
  1126. generic_inputs.region_height = height;
  1127. generic_inputs.gb_template = inputs.symbol_template;
  1128. for (int i = 0; i < 4; ++i)
  1129. generic_inputs.adaptive_template_pixels[i] = inputs.adaptive_template_pixels[i];
  1130. generic_inputs.arithmetic_decoder = &decoder;
  1131. return generic_region_decoding_procedure(generic_inputs, {}, contexts);
  1132. };
  1133. // 6.5.5 Decoding the symbol dictionary
  1134. // "1) Create an array SDNEWSYMS of bitmaps, having SDNUMNEWSYMS entries."
  1135. Vector<NonnullRefPtr<Symbol>> new_symbols;
  1136. // "2) If SDHUFF is 1 and SDREFAGG is 0, create an array SDNEWSYMWIDTHS of integers, having SDNUMNEWSYMS entries."
  1137. // FIXME: Implement support for SDHUFF = 1.
  1138. // "3) Set:
  1139. // HCHEIGHT = 0
  1140. // NSYMSDECODED = 0"
  1141. u32 height_class_height = 0;
  1142. u32 number_of_symbols_decoded = 0;
  1143. // "4) Decode each height class as follows:
  1144. // a) If NSYMSDECODED == SDNUMNEWSYMS then all the symbols in the dictionary have been decoded; proceed to step 5)."
  1145. while (number_of_symbols_decoded < inputs.number_of_new_symbols) {
  1146. // "b) Decode the height class delta height as described in 6.5.6. Let HCDH be the decoded value. Set:
  1147. // HCHEIGHT = HCEIGHT + HCDH
  1148. // SYMWIDTH = 0
  1149. // TOTWIDTH = 0
  1150. // HCFIRSTSYM = NSYMSDECODED"
  1151. i32 delta_height = read_delta_height();
  1152. height_class_height += delta_height;
  1153. u32 symbol_width = 0;
  1154. u32 total_width = 0;
  1155. u32 height_class_first_symbol = number_of_symbols_decoded;
  1156. // "c) Decode each symbol within the height class as follows:"
  1157. while (true) {
  1158. // "i) Decode the delta width for the symbol as described in 6.5.7."
  1159. auto opt_delta_width = read_delta_width();
  1160. // " If the result of this decoding is OOB then all the symbols in this height class have been decoded; proceed to step 4 d)."
  1161. if (!opt_delta_width.has_value())
  1162. break;
  1163. VERIFY(number_of_symbols_decoded < inputs.number_of_new_symbols);
  1164. // " Otherwise let DW be the decoded value and set:"
  1165. // SYMWIDTH = SYMWIDTH + DW
  1166. // TOTWIDTH = TOTWIDTH + SYMWIDTH"
  1167. i32 delta_width = opt_delta_width.value();
  1168. symbol_width += delta_width;
  1169. total_width += symbol_width;
  1170. // "ii) If SDHUFF is 0 or SDREFAGG is 1, then decode the symbol's bitmap as described in 6.5.8.
  1171. // Let BS be the decoded bitmap (this bitmap has width SYMWIDTH and height HCHEIGHT). Set:
  1172. // SDNEWSYMS[NSYMSDECODED] = BS"
  1173. // FIXME: Implement support for SDHUFF = 1.
  1174. // FIXME: Doing this eagerly is pretty wasteful. Decode on demand instead?
  1175. auto bitmap = TRY(read_bitmap(symbol_width, height_class_height));
  1176. new_symbols.append(Symbol::create(move(bitmap)));
  1177. // "iii) If SDHUFF is 1 and SDREFAGG is 0, then set:
  1178. // SDNEWSYMWIDTHS[NSYMSDECODED] = SYMWIDTH"
  1179. // FIXME: Implement support for SDHUFF = 1.
  1180. (void)total_width;
  1181. (void)height_class_first_symbol;
  1182. // "iv) Set:
  1183. // NSYMSDECODED = NSYMSDECODED + 1"
  1184. number_of_symbols_decoded++;
  1185. }
  1186. // d) If SDHUFF is 1 and SDREFAGG is 0, [...long text elided...]
  1187. // FIXME: Implement support for SDHUFF = 1.
  1188. }
  1189. // 5) Determine which symbol bitmaps are exported from this symbol dictionary, as described in 6.5.10. These
  1190. // bitmaps can be drawn from the symbols that are used as input to the symbol dictionary decoding
  1191. // procedure as well as the new symbols produced by the decoding procedure."
  1192. JBIG2::ArithmeticIntegerDecoder export_integer_decoder(decoder);
  1193. // 6.5.10 Exported symbols
  1194. Vector<bool> export_flags;
  1195. export_flags.resize(inputs.input_symbols.size() + inputs.number_of_new_symbols);
  1196. // "1) Set:
  1197. // EXINDEX = 0
  1198. // CUREXFLAG = 0"
  1199. u32 exported_index = 0;
  1200. bool current_export_flag = false;
  1201. do {
  1202. // "2) Decode a value using Table B.1 if SDHUFF is 1, or the IAEX integer arithmetic decoding procedure if
  1203. // SDHUFF is 0. Let EXRUNLENGTH be the decoded value."
  1204. // FIXME: Implement support for SDHUFF = 1.
  1205. i32 export_run_length = export_integer_decoder.decode().value(); // No OOB value.
  1206. // "3) Set EXFLAGS[EXINDEX] through EXFLAGS[EXINDEX + EXRUNLENGTH – 1] to CUREXFLAG.
  1207. // If EXRUNLENGTH = 0, then this step does not change any values."
  1208. for (int i = 0; i < export_run_length; ++i)
  1209. export_flags[exported_index + i] = current_export_flag;
  1210. // "4) Set:
  1211. // EXINDEX = EXINDEX + EXRUNLENGTH
  1212. // CUREXFLAG = NOT(CUREXFLAG)"
  1213. exported_index += export_run_length;
  1214. current_export_flag = !current_export_flag;
  1215. // 5) Repeat steps 2) through 4) until EXINDEX == SDNUMINSYMS + SDNUMNEWSYMS.
  1216. } while (exported_index < inputs.input_symbols.size() + inputs.number_of_new_symbols);
  1217. // "6) The array EXFLAGS now contains 1 for each symbol that is exported from the dictionary, and 0 for each
  1218. // symbol that is not exported."
  1219. Vector<NonnullRefPtr<Symbol>> exported_symbols;
  1220. // "7) Set:
  1221. // I = 0
  1222. // J = 0
  1223. // 8) For each value of I from 0 to SDNUMINSYMS + SDNUMNEWSYMS – 1,"
  1224. for (size_t i = 0; i < inputs.input_symbols.size() + inputs.number_of_new_symbols; ++i) {
  1225. // "if EXFLAGS[I] == 1 then perform the following steps:"
  1226. if (!export_flags[i])
  1227. continue;
  1228. // "a) If I < SDNUMINSYMS then set:
  1229. // SDEXSYMS[J] = SDINSYMS[I]
  1230. // J = J + 1"
  1231. if (i < inputs.input_symbols.size())
  1232. exported_symbols.append(inputs.input_symbols[i]);
  1233. // "b) If I >= SDNUMINSYMS then set:
  1234. // SDEXSYMS[J] = SDNEWSYMS[I – SDNUMINSYMS]
  1235. // J = J + 1"
  1236. if (i >= inputs.input_symbols.size())
  1237. exported_symbols.append(move(new_symbols[i - inputs.input_symbols.size()]));
  1238. }
  1239. if (exported_symbols.size() != inputs.number_of_exported_symbols)
  1240. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unexpected number of exported symbols");
  1241. return exported_symbols;
  1242. }
  1243. static ErrorOr<void> decode_symbol_dictionary(JBIG2LoadingContext&, SegmentData& segment)
  1244. {
  1245. // 7.4.2 Symbol dictionary segment syntax
  1246. // 7.4.2.1 Symbol dictionary segment data header
  1247. FixedMemoryStream stream(segment.data);
  1248. // 7.4.2.1.1 Symbol dictionary flags
  1249. u16 flags = TRY(stream.read_value<BigEndian<u16>>());
  1250. bool uses_huffman_encoding = (flags & 1) != 0; // "SDHUFF" in spec.
  1251. bool uses_refinement_or_aggregate_coding = (flags & 2) != 0; // "SDREFAGG" in spec.
  1252. u8 huffman_table_selection_for_height_differences = (flags >> 2) & 0b11; // "SDHUFFDH" in spec.
  1253. if (huffman_table_selection_for_height_differences == 2)
  1254. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_height_differences");
  1255. if (!uses_huffman_encoding && huffman_table_selection_for_height_differences != 0)
  1256. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_height_differences");
  1257. u8 huffman_table_selection_for_width_differences = (flags >> 4) & 0b11; // "SDHUFFDW" in spec.
  1258. if (huffman_table_selection_for_width_differences == 2)
  1259. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_width_differences");
  1260. if (!uses_huffman_encoding && huffman_table_selection_for_width_differences != 0)
  1261. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid huffman_table_selection_for_width_differences");
  1262. bool uses_user_supplied_size_table = (flags >> 6) & 1; // "SDHUFFBMSIZE" in spec.
  1263. if (!uses_huffman_encoding && uses_user_supplied_size_table)
  1264. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid uses_user_supplied_size_table");
  1265. bool uses_user_supplied_aggregate_table = (flags >> 7) & 1; // "SDHUFFAGGINST" in spec.
  1266. if (!uses_huffman_encoding && uses_user_supplied_aggregate_table)
  1267. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid uses_user_supplied_aggregate_table");
  1268. bool bitmap_coding_context_used = (flags >> 8) & 1;
  1269. if (uses_huffman_encoding && !uses_refinement_or_aggregate_coding && bitmap_coding_context_used)
  1270. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid bitmap_coding_context_used");
  1271. bool bitmap_coding_context_retained = (flags >> 9) & 1;
  1272. if (uses_huffman_encoding && !uses_refinement_or_aggregate_coding && bitmap_coding_context_retained)
  1273. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid bitmap_coding_context_retained");
  1274. u8 template_used = (flags >> 10) & 0b11; // "SDTEMPLATE" in spec.
  1275. if (uses_huffman_encoding && template_used != 0)
  1276. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid template_used");
  1277. u8 refinement_template_used = (flags >> 12) & 0b11; // "SDREFTEMPLATE" in spec.
  1278. if (!uses_refinement_or_aggregate_coding && refinement_template_used != 0)
  1279. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid refinement_template_used");
  1280. if (flags & 0b1110'0000'0000'0000)
  1281. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid symbol dictionary flags");
  1282. // 7.4.2.1.2 Symbol dictionary AT flags
  1283. Array<AdaptiveTemplatePixel, 4> adaptive_template {};
  1284. if (!uses_huffman_encoding) {
  1285. int number_of_adaptive_template_pixels = template_used == 0 ? 4 : 1;
  1286. for (int i = 0; i < number_of_adaptive_template_pixels; ++i) {
  1287. adaptive_template[i].x = TRY(stream.read_value<i8>());
  1288. adaptive_template[i].y = TRY(stream.read_value<i8>());
  1289. }
  1290. }
  1291. // 7.4.2.1.3 Symbol dictionary refinement AT flags
  1292. Array<AdaptiveTemplatePixel, 2> adaptive_refinement_template {};
  1293. if (uses_refinement_or_aggregate_coding && refinement_template_used == 0) {
  1294. for (size_t i = 0; i < adaptive_refinement_template.size(); ++i) {
  1295. adaptive_refinement_template[i].x = TRY(stream.read_value<i8>());
  1296. adaptive_refinement_template[i].y = TRY(stream.read_value<i8>());
  1297. }
  1298. }
  1299. // 7.4.2.1.4 Number of exported symbols (SDNUMEXSYMS)
  1300. u32 number_of_exported_symbols = TRY(stream.read_value<BigEndian<u32>>());
  1301. // 7.4.2.1.5 Number of new symbols (SDNUMNEWSYMS)
  1302. u32 number_of_new_symbols = TRY(stream.read_value<BigEndian<u32>>());
  1303. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_huffman_encoding={}", uses_huffman_encoding);
  1304. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_refinement_or_aggregate_coding={}", uses_refinement_or_aggregate_coding);
  1305. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: huffman_table_selection_for_height_differences={}", huffman_table_selection_for_height_differences);
  1306. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: huffman_table_selection_for_width_differences={}", huffman_table_selection_for_width_differences);
  1307. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_user_supplied_size_table={}", uses_user_supplied_size_table);
  1308. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: uses_user_supplied_aggregate_table={}", uses_user_supplied_aggregate_table);
  1309. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: bitmap_coding_context_used={}", bitmap_coding_context_used);
  1310. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: bitmap_coding_context_retained={}", bitmap_coding_context_retained);
  1311. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: template_used={}", template_used);
  1312. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: refinement_template_used={}", refinement_template_used);
  1313. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: number_of_exported_symbols={}", number_of_exported_symbols);
  1314. dbgln_if(JBIG2_DEBUG, "Symbol dictionary: number_of_new_symbols={}", number_of_new_symbols);
  1315. // 7.4.2.1.6 Symbol dictionary segment Huffman table selection
  1316. // FIXME
  1317. // 7.4.2.2 Decoding a symbol dictionary segment
  1318. // "1) Interpret its header, as described in 7.4.2.1."
  1319. // Done!
  1320. // "2) Decode (or retrieve the results of decoding) any referred-to symbol dictionary and tables segments."
  1321. if (segment.header.referred_to_segment_numbers.size() != 0)
  1322. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode referred-to symbol dictionary segments yet");
  1323. // "3) If the "bitmap coding context used" bit in the header was 1, ..."
  1324. if (bitmap_coding_context_used)
  1325. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode bitmap coding context segment yet");
  1326. // "4) If the "bitmap coding context used" bit in the header was 0, then, as described in E.3.7,
  1327. // reset all the arithmetic coding statistics for the generic region and generic refinement region decoding procedures to zero."
  1328. // Nothing to do.
  1329. // "5) Reset the arithmetic coding statistics for all the contexts of all the arithmetic integer coders to zero."
  1330. // FIXME
  1331. // "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."
  1332. SymbolDictionaryDecodingInputParameters inputs;
  1333. inputs.uses_huffman_encoding = uses_huffman_encoding;
  1334. inputs.uses_refinement_or_aggregate_coding = uses_refinement_or_aggregate_coding;
  1335. inputs.input_symbols = {};
  1336. inputs.number_of_new_symbols = number_of_new_symbols;
  1337. inputs.number_of_exported_symbols = number_of_exported_symbols;
  1338. // FIXME: SDHUFFDH, SDHUFFDW, SDHUFFBMSIZE, SDHUFFAGGINST
  1339. inputs.symbol_template = template_used;
  1340. inputs.adaptive_template_pixels = adaptive_template;
  1341. inputs.refinement_template = refinement_template_used;
  1342. inputs.refinement_adaptive_template_pixels = adaptive_refinement_template;
  1343. auto result = TRY(symbol_dictionary_decoding_procedure(inputs, segment.data.slice(TRY(stream.tell()))));
  1344. // "7) If the "bitmap coding context retained" bit in the header was 1, then, as described in E.3.8, preserve the current contents
  1345. // of the arithmetic coding statistics for the generic region and generic refinement region decoding procedures."
  1346. if (bitmap_coding_context_retained)
  1347. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot retain bitmap coding context yet");
  1348. segment.symbols = move(result);
  1349. return {};
  1350. }
  1351. static ErrorOr<void> decode_intermediate_text_region(JBIG2LoadingContext&, SegmentData const&)
  1352. {
  1353. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate text region yet");
  1354. }
  1355. static ErrorOr<void> decode_immediate_text_region(JBIG2LoadingContext& context, SegmentData const& segment)
  1356. {
  1357. // 7.4.3 Text region segment syntax
  1358. auto data = segment.data;
  1359. auto information_field = TRY(decode_region_segment_information_field(data));
  1360. data = data.slice(sizeof(information_field));
  1361. 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);
  1362. FixedMemoryStream stream(data);
  1363. // 7.4.3.1.1 Text region segment flags
  1364. u16 text_region_segment_flags = TRY(stream.read_value<BigEndian<u16>>());
  1365. bool uses_huffman_encoding = (text_region_segment_flags & 1) != 0; // "SBHUFF" in spec.
  1366. bool uses_refinement_coding = (text_region_segment_flags >> 1) & 1; // "SBREFINE" in spec.
  1367. u8 log_strip_size = (text_region_segment_flags >> 2) & 3; // "LOGSBSTRIPS" in spec.
  1368. u8 strip_size = 1u << log_strip_size;
  1369. u8 reference_corner = (text_region_segment_flags >> 4) & 3; // "REFCORNER"
  1370. bool is_transposed = (text_region_segment_flags >> 6) & 1; // "TRANSPOSED" in spec.
  1371. u8 combination_operator = (text_region_segment_flags >> 7) & 3; // "SBCOMBOP" in spec.
  1372. if (combination_operator > 4)
  1373. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid text region combination operator");
  1374. u8 default_pixel_value = (text_region_segment_flags >> 9) & 1; // "SBDEFPIXEL" in spec.
  1375. u8 delta_s_offset_value = (text_region_segment_flags >> 10) & 0x1f; // "SBDSOFFSET" in spec.
  1376. i8 delta_s_offset = delta_s_offset_value;
  1377. if (delta_s_offset_value & 0x10) {
  1378. // This is converting a 5-bit two's complement number ot i8.
  1379. // FIXME: There's probably a simpler way to do this? Probably just sign-extend by or-ing in the top 3 bits?
  1380. delta_s_offset_value = (~delta_s_offset_value + 1) & 0x1f;
  1381. delta_s_offset = -delta_s_offset_value;
  1382. }
  1383. u8 refinement_template = (text_region_segment_flags >> 15) != 0; // "SBRTEMPLATE" in spec.
  1384. if (!uses_refinement_coding && refinement_template != 0)
  1385. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid refinement_template");
  1386. // 7.4.3.1.2 Text region segment Huffman flags
  1387. // "This field is only present if SBHUFF is 1."
  1388. // FIXME: Support this eventually.
  1389. if (uses_huffman_encoding)
  1390. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode huffman text regions yet");
  1391. // 7.4.3.1.3 Text region refinement AT flags
  1392. // "This field is only present if SBREFINE is 1 and SBRTEMPLATE is 0."
  1393. Array<AdaptiveTemplatePixel, 2> adaptive_refinement_template {};
  1394. if (uses_refinement_coding && refinement_template == 0) {
  1395. for (size_t i = 0; i < adaptive_refinement_template.size(); ++i) {
  1396. adaptive_refinement_template[i].x = TRY(stream.read_value<i8>());
  1397. adaptive_refinement_template[i].y = TRY(stream.read_value<i8>());
  1398. }
  1399. }
  1400. // 7.4.3.1.4 Number of symbol instances (SBNUMINSTANCES)
  1401. u32 number_of_symbol_instances = TRY(stream.read_value<BigEndian<u32>>());
  1402. // 7.4.3.1.5 Text region segment symbol ID Huffman decoding table
  1403. // "It is only present if SBHUFF is 1."
  1404. // FIXME: Support this eventually.
  1405. // 7.4.3.2 Decoding a text region segment
  1406. // "1) Interpret its header, as described in 7.4.3.1."
  1407. // Done!
  1408. // "2) Decode (or retrieve the results of decoding) any referred-to symbol dictionary and tables segments."
  1409. Vector<NonnullRefPtr<Symbol>> symbols;
  1410. for (auto referred_to_segment_number : segment.header.referred_to_segment_numbers) {
  1411. auto opt_referred_to_segment = context.segments_by_number.get(referred_to_segment_number);
  1412. if (!opt_referred_to_segment.has_value())
  1413. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Text segment refers to non-existent segment");
  1414. dbgln_if(JBIG2_DEBUG, "Text segment refers to segment id {} index {}", referred_to_segment_number, opt_referred_to_segment.value());
  1415. auto const& referred_to_segment = context.segments[opt_referred_to_segment.value()];
  1416. if (!referred_to_segment.symbols.has_value())
  1417. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Text segment referred-to segment without symbols");
  1418. symbols.extend(referred_to_segment.symbols.value());
  1419. }
  1420. // "3) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1421. // FIXME
  1422. // "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."
  1423. TextRegionDecodingInputParameters inputs;
  1424. inputs.uses_huffman_encoding = uses_huffman_encoding;
  1425. inputs.uses_refinement_coding = uses_refinement_coding;
  1426. inputs.default_pixel = default_pixel_value;
  1427. inputs.operator_ = static_cast<CombinationOperator>(combination_operator);
  1428. inputs.is_transposed = is_transposed;
  1429. inputs.reference_corner = static_cast<TextRegionDecodingInputParameters::Corner>(reference_corner);
  1430. inputs.delta_s_offset = delta_s_offset;
  1431. inputs.region_width = information_field.width;
  1432. inputs.region_height = information_field.height;
  1433. inputs.number_of_instances = number_of_symbol_instances;
  1434. inputs.size_of_symbol_instance_strips = strip_size;
  1435. inputs.id_symbol_code_length = ceil(log2(symbols.size()));
  1436. inputs.symbols = move(symbols);
  1437. // FIXME: Huffman tables.
  1438. inputs.refinement_template = refinement_template;
  1439. inputs.refinement_adaptive_template_pixels = adaptive_refinement_template;
  1440. auto result = TRY(text_region_decoding_procedure(inputs, data.slice(TRY(stream.tell()))));
  1441. composite_bitbuffer(*context.page.bits, *result, { information_field.x_location, information_field.y_location }, information_field.external_combination_operator());
  1442. return {};
  1443. }
  1444. static ErrorOr<void> decode_pattern_dictionary(JBIG2LoadingContext&, SegmentData const&)
  1445. {
  1446. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode pattern dictionary yet");
  1447. }
  1448. static ErrorOr<void> decode_intermediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  1449. {
  1450. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate halftone region yet");
  1451. }
  1452. static ErrorOr<void> decode_immediate_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  1453. {
  1454. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate halftone region yet");
  1455. }
  1456. static ErrorOr<void> decode_immediate_lossless_halftone_region(JBIG2LoadingContext&, SegmentData const&)
  1457. {
  1458. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless halftone region yet");
  1459. }
  1460. static ErrorOr<void> decode_intermediate_generic_region(JBIG2LoadingContext&, SegmentData const&)
  1461. {
  1462. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic region yet");
  1463. }
  1464. static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& context, SegmentData const& segment)
  1465. {
  1466. // 7.4.6 Generic region segment syntax
  1467. auto data = segment.data;
  1468. auto information_field = TRY(decode_region_segment_information_field(data));
  1469. data = data.slice(sizeof(information_field));
  1470. 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);
  1471. // 7.4.6.2 Generic region segment flags
  1472. if (data.is_empty())
  1473. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No segment data");
  1474. u8 flags = data[0];
  1475. bool uses_mmr = (flags & 1) != 0;
  1476. u8 arithmetic_coding_template = (flags >> 1) & 3; // "GBTEMPLATE"
  1477. bool typical_prediction_generic_decoding_on = (flags >> 3) & 1; // "TPGDON"; "TPGD" is short for "Typical Prediction for Generic Direct coding"
  1478. bool uses_extended_reference_template = (flags >> 4) & 1; // "EXTTEMPLATE"
  1479. if (flags & 0b1110'0000)
  1480. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid flags");
  1481. data = data.slice(sizeof(flags));
  1482. // 7.4.6.3 Generic region segment AT flags
  1483. Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels {};
  1484. if (!uses_mmr) {
  1485. dbgln_if(JBIG2_DEBUG, "Non-MMR generic region, GBTEMPLATE={} TPGDON={} EXTTEMPLATE={}", arithmetic_coding_template, typical_prediction_generic_decoding_on, uses_extended_reference_template);
  1486. if (arithmetic_coding_template == 0 && uses_extended_reference_template) {
  1487. // This was added in T.88 Amendment 2 (https://www.itu.int/rec/T-REC-T.88-200306-S!Amd2/en) mid-2003.
  1488. // 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,
  1489. // so it's not clear how much data to read.
  1490. return Error::from_string_literal("JBIG2ImageDecoderPlugin: GBTEMPLATE=0 EXTTEMPLATE=1 not yet implemented");
  1491. }
  1492. size_t number_of_adaptive_template_pixels = arithmetic_coding_template == 0 ? 4 : 1;
  1493. if (data.size() < 2 * number_of_adaptive_template_pixels)
  1494. return Error::from_string_literal("JBIG2ImageDecoderPlugin: No adaptive template data");
  1495. for (size_t i = 0; i < number_of_adaptive_template_pixels; ++i) {
  1496. adaptive_template_pixels[i].x = static_cast<i8>(data[2 * i]);
  1497. adaptive_template_pixels[i].y = static_cast<i8>(data[2 * i + 1]);
  1498. }
  1499. data = data.slice(2 * number_of_adaptive_template_pixels);
  1500. }
  1501. // 7.4.6.4 Decoding a generic region segment
  1502. // "1) Interpret its header, as described in 7.4.6.1"
  1503. // Done above.
  1504. // "2) As described in E.3.7, reset all the arithmetic coding statistics to zero."
  1505. Vector<JBIG2::ArithmeticDecoder::Context> contexts;
  1506. contexts.resize(1 << 16);
  1507. // "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."
  1508. GenericRegionDecodingInputParameters inputs;
  1509. inputs.is_modified_modified_read = uses_mmr;
  1510. inputs.region_width = information_field.width;
  1511. inputs.region_height = information_field.height;
  1512. inputs.gb_template = arithmetic_coding_template;
  1513. inputs.is_typical_prediction_used = typical_prediction_generic_decoding_on;
  1514. inputs.is_extended_reference_template_used = uses_extended_reference_template;
  1515. inputs.skip_pattern = OptionalNone {};
  1516. inputs.adaptive_template_pixels = adaptive_template_pixels;
  1517. Optional<JBIG2::ArithmeticDecoder> decoder;
  1518. if (!uses_mmr) {
  1519. decoder = TRY(JBIG2::ArithmeticDecoder::initialize(data));
  1520. inputs.arithmetic_decoder = &decoder.value();
  1521. }
  1522. auto result = TRY(generic_region_decoding_procedure(inputs, data, contexts));
  1523. // 8.2 Page image composition step 5)
  1524. if (information_field.x_location + information_field.width > (u32)context.page.size.width()
  1525. || information_field.y_location + information_field.height > (u32)context.page.size.height()) {
  1526. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Region bounds outsize of page bounds");
  1527. }
  1528. composite_bitbuffer(*context.page.bits, *result, { information_field.x_location, information_field.y_location }, information_field.external_combination_operator());
  1529. return {};
  1530. }
  1531. static ErrorOr<void> decode_intermediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  1532. {
  1533. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode intermediate generic refinement region yet");
  1534. }
  1535. static ErrorOr<void> decode_immediate_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  1536. {
  1537. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate generic refinement region yet");
  1538. }
  1539. static ErrorOr<void> decode_immediate_lossless_generic_refinement_region(JBIG2LoadingContext&, SegmentData const&)
  1540. {
  1541. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode immediate lossless generic refinement region yet");
  1542. }
  1543. static ErrorOr<void> decode_page_information(JBIG2LoadingContext& context, SegmentData const& segment)
  1544. {
  1545. // 7.4.8 Page information segment syntax and 8.1 Decoder model steps 1) - 3).
  1546. // "1) Decode the page information segment.""
  1547. auto page_information = TRY(decode_page_information_segment(segment.data));
  1548. bool page_is_striped = (page_information.striping_information & 0x80) != 0;
  1549. if (page_information.bitmap_height == 0xffff'ffff && !page_is_striped)
  1550. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Non-striped bitmaps of indeterminate height not allowed");
  1551. u8 default_color = (page_information.flags >> 2) & 1;
  1552. u8 default_combination_operator = (page_information.flags >> 3) & 3;
  1553. context.page.default_combination_operator = static_cast<CombinationOperator>(default_combination_operator);
  1554. // FIXME: Do something with the other fields in page_information.
  1555. // "2) Create the page buffer, of the size given in the page information segment.
  1556. //
  1557. // If the page height is unknown, then this is not possible. However, in this case the page must be striped,
  1558. // and the maximum stripe height specified, and the initial page buffer can be created with height initially
  1559. // equal to this maximum stripe height."
  1560. size_t height = page_information.bitmap_height;
  1561. if (height == 0xffff'ffff)
  1562. height = page_information.striping_information & 0x7F;
  1563. context.page.bits = TRY(BitBuffer::create(page_information.bitmap_width, height));
  1564. // "3) Fill the page buffer with the page's default pixel value."
  1565. context.page.bits->fill(default_color != 0);
  1566. return {};
  1567. }
  1568. static ErrorOr<void> decode_end_of_page(JBIG2LoadingContext&, SegmentData const& segment)
  1569. {
  1570. // 7.4.9 End of page segment syntax
  1571. if (segment.data.size() != 0)
  1572. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of page segment has non-zero size");
  1573. // FIXME: If the page had unknown height, check that previous segment was end-of-stripe.
  1574. // FIXME: Maybe mark page as completed and error if we see more segments for it?
  1575. return {};
  1576. }
  1577. static ErrorOr<void> decode_end_of_stripe(JBIG2LoadingContext&, SegmentData const&)
  1578. {
  1579. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode end of stripe yet");
  1580. }
  1581. static ErrorOr<void> decode_end_of_file(JBIG2LoadingContext&, SegmentData const& segment)
  1582. {
  1583. // 7.4.11 End of file segment syntax
  1584. if (segment.data.size() != 0)
  1585. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment has non-zero size");
  1586. return {};
  1587. }
  1588. static ErrorOr<void> decode_profiles(JBIG2LoadingContext&, SegmentData const&)
  1589. {
  1590. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode profiles yet");
  1591. }
  1592. static ErrorOr<void> decode_tables(JBIG2LoadingContext&, SegmentData const&)
  1593. {
  1594. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode tables yet");
  1595. }
  1596. static ErrorOr<void> decode_color_palette(JBIG2LoadingContext&, SegmentData const&)
  1597. {
  1598. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Cannot decode color palette yet");
  1599. }
  1600. static ErrorOr<void> decode_extension(JBIG2LoadingContext&, SegmentData const& segment)
  1601. {
  1602. // 7.4.14 Extension segment syntax
  1603. FixedMemoryStream stream { segment.data };
  1604. enum ExtensionType {
  1605. SingleByteCodedComment = 0x20000000,
  1606. MultiByteCodedComment = 0x20000002,
  1607. };
  1608. u32 type = TRY(stream.read_value<BigEndian<u32>>());
  1609. auto read_string = [&]<class T>() -> ErrorOr<Vector<T>> {
  1610. Vector<T> result;
  1611. do {
  1612. result.append(TRY(stream.read_value<BigEndian<T>>()));
  1613. } while (result.last());
  1614. result.take_last();
  1615. return result;
  1616. };
  1617. switch (type) {
  1618. case SingleByteCodedComment: {
  1619. // 7.4.15.1 Single-byte coded comment
  1620. // Pairs of zero-terminated ISO/IEC 8859-1 (latin1) pairs, terminated by another \0.
  1621. while (true) {
  1622. auto first_bytes = TRY(read_string.template operator()<u8>());
  1623. if (first_bytes.is_empty())
  1624. break;
  1625. auto second_bytes = TRY(read_string.template operator()<u8>());
  1626. auto first = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { first_bytes }));
  1627. auto second = TRY(TextCodec::decoder_for("ISO-8859-1"sv)->to_utf8(StringView { second_bytes }));
  1628. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  1629. }
  1630. if (!stream.is_eof())
  1631. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after SingleByteCodedComment");
  1632. return {};
  1633. }
  1634. case MultiByteCodedComment: {
  1635. // 7.4.15.2 Multi-byte coded comment
  1636. // Pairs of (two-byte-)zero-terminated UCS-2 pairs, terminated by another \0\0.
  1637. while (true) {
  1638. auto first_ucs2 = TRY(read_string.template operator()<u16>());
  1639. if (first_ucs2.is_empty())
  1640. break;
  1641. auto second_ucs2 = TRY(read_string.template operator()<u16>());
  1642. auto first = TRY(Utf16View(first_ucs2).to_utf8());
  1643. auto second = TRY(Utf16View(second_ucs2).to_utf8());
  1644. dbgln("JBIG2ImageDecoderPlugin: key '{}', value '{}'", first, second);
  1645. }
  1646. if (!stream.is_eof())
  1647. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Trailing data after MultiByteCodedComment");
  1648. return {};
  1649. }
  1650. }
  1651. // FIXME: If bit 31 in `type` is not set, the extension isn't necessary, and we could ignore it.
  1652. dbgln("JBIG2ImageDecoderPlugin: Unknown extension type {:#x}", type);
  1653. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Unknown extension type");
  1654. }
  1655. static ErrorOr<void> decode_data(JBIG2LoadingContext& context)
  1656. {
  1657. TRY(warn_about_multiple_pages(context));
  1658. for (size_t i = 0; i < context.segments.size(); ++i) {
  1659. auto& segment = context.segments[i];
  1660. if (segment.header.page_association != 0 && segment.header.page_association != 1)
  1661. continue;
  1662. switch (segment.header.type) {
  1663. case SegmentType::SymbolDictionary:
  1664. TRY(decode_symbol_dictionary(context, segment));
  1665. break;
  1666. case SegmentType::IntermediateTextRegion:
  1667. TRY(decode_intermediate_text_region(context, segment));
  1668. break;
  1669. case SegmentType::ImmediateTextRegion:
  1670. case SegmentType::ImmediateLosslessTextRegion:
  1671. // 7.4.3 Text region segment syntax
  1672. // "The data parts of all three of the text region segment types ("intermediate text region", "immediate text region" and
  1673. // "immediate lossless text region") are coded identically, but are acted upon differently, see 8.2."
  1674. // But 8.2 only describes a difference between intermediate and immediate regions as far as I can tell,
  1675. // and calling the immediate text region handler for immediate lossless text regions seems to do the right thing (?).
  1676. TRY(decode_immediate_text_region(context, segment));
  1677. break;
  1678. case SegmentType::PatternDictionary:
  1679. TRY(decode_pattern_dictionary(context, segment));
  1680. break;
  1681. case SegmentType::IntermediateHalftoneRegion:
  1682. TRY(decode_intermediate_halftone_region(context, segment));
  1683. break;
  1684. case SegmentType::ImmediateHalftoneRegion:
  1685. TRY(decode_immediate_halftone_region(context, segment));
  1686. break;
  1687. case SegmentType::ImmediateLosslessHalftoneRegion:
  1688. TRY(decode_immediate_lossless_halftone_region(context, segment));
  1689. break;
  1690. case SegmentType::IntermediateGenericRegion:
  1691. TRY(decode_intermediate_generic_region(context, segment));
  1692. break;
  1693. case SegmentType::ImmediateGenericRegion:
  1694. case SegmentType::ImmediateLosslessGenericRegion:
  1695. // 7.4.6 Generic region segment syntax
  1696. // "The data parts of all three of the generic region segment types ("intermediate generic region", "immediate generic region" and
  1697. // "immediate lossless generic region") are coded identically, but are acted upon differently, see 8.2."
  1698. // But 8.2 only describes a difference between intermediate and immediate regions as far as I can tell,
  1699. // and calling the immediate generic region handler for immediate generic lossless regions seems to do the right thing (?).
  1700. TRY(decode_immediate_generic_region(context, segment));
  1701. break;
  1702. case SegmentType::IntermediateGenericRefinementRegion:
  1703. TRY(decode_intermediate_generic_refinement_region(context, segment));
  1704. break;
  1705. case SegmentType::ImmediateGenericRefinementRegion:
  1706. TRY(decode_immediate_generic_refinement_region(context, segment));
  1707. break;
  1708. case SegmentType::ImmediateLosslessGenericRefinementRegion:
  1709. TRY(decode_immediate_lossless_generic_refinement_region(context, segment));
  1710. break;
  1711. case SegmentType::PageInformation:
  1712. TRY(decode_page_information(context, segment));
  1713. break;
  1714. case SegmentType::EndOfPage:
  1715. TRY(decode_end_of_page(context, segment));
  1716. break;
  1717. case SegmentType::EndOfStripe:
  1718. TRY(decode_end_of_stripe(context, segment));
  1719. break;
  1720. case SegmentType::EndOfFile:
  1721. TRY(decode_end_of_file(context, segment));
  1722. // "If a file contains an end of file segment, it must be the last segment."
  1723. if (i != context.segments.size() - 1)
  1724. return Error::from_string_literal("JBIG2ImageDecoderPlugin: End of file segment not last segment");
  1725. break;
  1726. case SegmentType::Profiles:
  1727. TRY(decode_profiles(context, segment));
  1728. break;
  1729. case SegmentType::Tables:
  1730. TRY(decode_tables(context, segment));
  1731. break;
  1732. case SegmentType::ColorPalette:
  1733. TRY(decode_color_palette(context, segment));
  1734. break;
  1735. case SegmentType::Extension:
  1736. TRY(decode_extension(context, segment));
  1737. break;
  1738. }
  1739. }
  1740. return {};
  1741. }
  1742. JBIG2ImageDecoderPlugin::JBIG2ImageDecoderPlugin()
  1743. {
  1744. m_context = make<JBIG2LoadingContext>();
  1745. }
  1746. IntSize JBIG2ImageDecoderPlugin::size()
  1747. {
  1748. return m_context->page.size;
  1749. }
  1750. bool JBIG2ImageDecoderPlugin::sniff(ReadonlyBytes data)
  1751. {
  1752. return data.starts_with(id_string);
  1753. }
  1754. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JBIG2ImageDecoderPlugin::create(ReadonlyBytes data)
  1755. {
  1756. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  1757. TRY(decode_jbig2_header(*plugin->m_context, data));
  1758. data = data.slice(sizeof(id_string) + sizeof(u8) + (plugin->m_context->number_of_pages.has_value() ? sizeof(u32) : 0));
  1759. TRY(decode_segment_headers(*plugin->m_context, data));
  1760. TRY(scan_for_page_size(*plugin->m_context));
  1761. return plugin;
  1762. }
  1763. ErrorOr<ImageFrameDescriptor> JBIG2ImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  1764. {
  1765. // FIXME: Use this for multi-page JBIG2 files?
  1766. if (index != 0)
  1767. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Invalid frame index");
  1768. if (m_context->state == JBIG2LoadingContext::State::Error)
  1769. return Error::from_string_literal("JBIG2ImageDecoderPlugin: Decoding failed");
  1770. if (m_context->state < JBIG2LoadingContext::State::Decoded) {
  1771. auto result = decode_data(*m_context);
  1772. if (result.is_error()) {
  1773. m_context->state = JBIG2LoadingContext::State::Error;
  1774. return result.release_error();
  1775. }
  1776. m_context->state = JBIG2LoadingContext::State::Decoded;
  1777. }
  1778. auto bitmap = TRY(m_context->page.bits->to_gfx_bitmap());
  1779. return ImageFrameDescriptor { move(bitmap), 0 };
  1780. }
  1781. ErrorOr<ByteBuffer> JBIG2ImageDecoderPlugin::decode_embedded(Vector<ReadonlyBytes> data)
  1782. {
  1783. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JBIG2ImageDecoderPlugin()));
  1784. plugin->m_context->organization = Organization::Embedded;
  1785. for (auto const& segment_data : data)
  1786. TRY(decode_segment_headers(*plugin->m_context, segment_data));
  1787. TRY(scan_for_page_size(*plugin->m_context));
  1788. TRY(decode_data(*plugin->m_context));
  1789. return plugin->m_context->page.bits->to_byte_buffer();
  1790. }
  1791. }