JBIG2Loader.cpp 133 KB

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