JPEGXLLoader.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  1. /*
  2. * Copyright (c) 2023, Lucas Chollet <lucas.chollet@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/BitStream.h>
  7. #include <AK/Endian.h>
  8. #include <AK/FixedArray.h>
  9. #include <AK/String.h>
  10. #include <LibCompress/Brotli.h>
  11. #include <LibGfx/ImageFormats/JPEGXLLoader.h>
  12. namespace Gfx {
  13. /// 4.2 - Functions
  14. static ALWAYS_INLINE i32 unpack_signed(u32 u)
  15. {
  16. if (u % 2 == 0)
  17. return static_cast<i32>(u / 2);
  18. return -static_cast<i32>((u + 1) / 2);
  19. }
  20. ///
  21. /// B.2 - Field types
  22. // This is defined as a macro in order to get lazy-evaluated parameter
  23. // Note that the lambda will capture your context by reference.
  24. #define U32(d0, d1, d2, d3) \
  25. ({ \
  26. u8 const selector = TRY(stream.read_bits(2)); \
  27. auto value = [&, selector]() -> ErrorOr<u32> { \
  28. if (selector == 0) \
  29. return (d0); \
  30. if (selector == 1) \
  31. return (d1); \
  32. if (selector == 2) \
  33. return (d2); \
  34. if (selector == 3) \
  35. return (d3); \
  36. VERIFY_NOT_REACHED(); \
  37. }(); \
  38. TRY(value); \
  39. })
  40. static ALWAYS_INLINE ErrorOr<u64> U64(LittleEndianInputBitStream& stream)
  41. {
  42. u8 const selector = TRY(stream.read_bits(2));
  43. if (selector == 0)
  44. return 0;
  45. if (selector == 1)
  46. return 1 + TRY(stream.read_bits(4));
  47. if (selector == 2)
  48. return 17 + TRY(stream.read_bits(8));
  49. VERIFY(selector == 3);
  50. u64 value = TRY(stream.read_bits(12));
  51. u8 shift = 12;
  52. while (TRY(stream.read_bits(1)) == 1) {
  53. if (shift == 60) {
  54. value += TRY(stream.read_bits(4)) << shift;
  55. break;
  56. }
  57. value += TRY(stream.read_bits(8)) << shift;
  58. shift += 8;
  59. }
  60. return value;
  61. }
  62. template<Enum E>
  63. ErrorOr<E> read_enum(LittleEndianInputBitStream& stream)
  64. {
  65. return static_cast<E>(U32(0, 1, 2 + TRY(stream.read_bits(4)), 18 + TRY(stream.read_bits(6))));
  66. }
  67. // This is not specified
  68. static ErrorOr<String> read_string(LittleEndianInputBitStream& stream)
  69. {
  70. auto const name_length = U32(0, TRY(stream.read_bits(4)), 16 + TRY(stream.read_bits(5)), 48 + TRY(stream.read_bits(10)));
  71. auto string_buffer = TRY(FixedArray<u8>::create(name_length));
  72. TRY(stream.read_until_filled(string_buffer.span()));
  73. return String::from_utf8(StringView { string_buffer.span() });
  74. }
  75. ///
  76. /// D.2 - Image dimensions
  77. struct SizeHeader {
  78. u32 height {};
  79. u32 width {};
  80. };
  81. static u32 aspect_ratio(u32 height, u32 ratio)
  82. {
  83. if (ratio == 1)
  84. return height;
  85. if (ratio == 2)
  86. return height * 12 / 10;
  87. if (ratio == 3)
  88. return height * 4 / 3;
  89. if (ratio == 4)
  90. return height * 3 / 2;
  91. if (ratio == 5)
  92. return height * 16 / 9;
  93. if (ratio == 6)
  94. return height * 5 / 4;
  95. if (ratio == 7)
  96. return height * 2 / 1;
  97. VERIFY_NOT_REACHED();
  98. }
  99. static ErrorOr<SizeHeader> read_size_header(LittleEndianInputBitStream& stream)
  100. {
  101. SizeHeader size {};
  102. auto const div8 = TRY(stream.read_bit());
  103. if (div8) {
  104. auto const h_div8 = 1 + TRY(stream.read_bits(5));
  105. size.height = 8 * h_div8;
  106. } else {
  107. size.height = U32(
  108. 1 + TRY(stream.read_bits(9)),
  109. 1 + TRY(stream.read_bits(13)),
  110. 1 + TRY(stream.read_bits(18)),
  111. 1 + TRY(stream.read_bits(30)));
  112. }
  113. auto const ratio = TRY(stream.read_bits(3));
  114. if (ratio == 0) {
  115. if (div8) {
  116. auto const w_div8 = 1 + TRY(stream.read_bits(5));
  117. size.width = 8 * w_div8;
  118. } else {
  119. size.width = U32(
  120. 1 + TRY(stream.read_bits(9)),
  121. 1 + TRY(stream.read_bits(13)),
  122. 1 + TRY(stream.read_bits(18)),
  123. 1 + TRY(stream.read_bits(30)));
  124. }
  125. } else {
  126. size.width = aspect_ratio(size.height, ratio);
  127. }
  128. return size;
  129. }
  130. ///
  131. /// D.3.5 - BitDepth
  132. struct BitDepth {
  133. u32 bits_per_sample { 8 };
  134. u8 exp_bits {};
  135. };
  136. static ErrorOr<BitDepth> read_bit_depth(LittleEndianInputBitStream& stream)
  137. {
  138. BitDepth bit_depth;
  139. bool const float_sample = TRY(stream.read_bit());
  140. if (float_sample) {
  141. bit_depth.bits_per_sample = U32(32, 16, 24, 1 + TRY(stream.read_bits(6)));
  142. bit_depth.exp_bits = 1 + TRY(stream.read_bits(4));
  143. } else {
  144. bit_depth.bits_per_sample = U32(8, 10, 12, 1 + TRY(stream.read_bits(6)));
  145. }
  146. return bit_depth;
  147. }
  148. ///
  149. /// E.2 - ColourEncoding
  150. struct ColourEncoding {
  151. enum class ColourSpace {
  152. kRGB = 0,
  153. kGrey = 1,
  154. kXYB = 2,
  155. kUnknown = 3,
  156. };
  157. enum class WhitePoint {
  158. kD65 = 1,
  159. kCustom = 2,
  160. kE = 10,
  161. kDCI = 11,
  162. };
  163. enum class Primaries {
  164. kSRGB = 1,
  165. kCustom = 2,
  166. k2100 = 3,
  167. kP3 = 11,
  168. };
  169. enum class RenderingIntent {
  170. kPerceptual = 0,
  171. kRelative = 1,
  172. kSaturation = 2,
  173. kAbsolute = 3,
  174. };
  175. struct Customxy {
  176. u32 ux {};
  177. u32 uy {};
  178. };
  179. bool want_icc = false;
  180. ColourSpace colour_space { ColourSpace::kRGB };
  181. WhitePoint white_point { WhitePoint::kD65 };
  182. Primaries primaries { Primaries::kSRGB };
  183. Customxy white {};
  184. Customxy red {};
  185. Customxy green {};
  186. Customxy blue {};
  187. RenderingIntent rendering_intent { RenderingIntent::kRelative };
  188. };
  189. [[maybe_unused]] static ErrorOr<ColourEncoding::Customxy> read_custom_xy(LittleEndianInputBitStream& stream)
  190. {
  191. ColourEncoding::Customxy custom_xy;
  192. auto const read_custom = [&stream]() -> ErrorOr<u32> {
  193. return U32(
  194. TRY(stream.read_bits(19)),
  195. 524288 + TRY(stream.read_bits(19)),
  196. 1048576 + TRY(stream.read_bits(20)),
  197. 2097152 + TRY(stream.read_bits(21)));
  198. };
  199. custom_xy.ux = TRY(read_custom());
  200. custom_xy.uy = TRY(read_custom());
  201. return custom_xy;
  202. }
  203. static ErrorOr<ColourEncoding> read_colour_encoding(LittleEndianInputBitStream& stream)
  204. {
  205. ColourEncoding colour_encoding;
  206. bool const all_default = TRY(stream.read_bit());
  207. if (!all_default) {
  208. TODO();
  209. }
  210. return colour_encoding;
  211. }
  212. ///
  213. /// B.3 - Extensions
  214. struct Extensions {
  215. u64 extensions {};
  216. };
  217. static ErrorOr<Extensions> read_extensions(LittleEndianInputBitStream& stream)
  218. {
  219. Extensions extensions;
  220. extensions.extensions = TRY(U64(stream));
  221. if (extensions.extensions != 0)
  222. TODO();
  223. return extensions;
  224. }
  225. ///
  226. /// K.2 - Non-separable upsampling
  227. Array s_d_up2 {
  228. -0.01716200, -0.03452303, -0.04022174, -0.02921014, -0.00624645,
  229. 0.14111091, 0.28896755, 0.00278718, -0.01610267, 0.56661550,
  230. 0.03777607, -0.01986694, -0.03144731, -0.01185068, -0.00213539
  231. };
  232. Array s_d_up4 = {
  233. -0.02419067, -0.03491987, -0.03693351, -0.03094285, -0.00529785,
  234. -0.01663432, -0.03556863, -0.03888905, -0.03516850, -0.00989469,
  235. 0.23651958, 0.33392945, -0.01073543, -0.01313181, -0.03556694,
  236. 0.13048175, 0.40103025, 0.03951150, -0.02077584, 0.46914198,
  237. -0.00209270, -0.01484589, -0.04064806, 0.18942530, 0.56279892,
  238. 0.06674400, -0.02335494, -0.03551682, -0.00754830, -0.02267919,
  239. -0.02363578, 0.00315804, -0.03399098, -0.01359519, -0.00091653,
  240. -0.00335467, -0.01163294, -0.01610294, -0.00974088, -0.00191622,
  241. -0.01095446, -0.03198464, -0.04455121, -0.02799790, -0.00645912,
  242. 0.06390599, 0.22963888, 0.00630981, -0.01897349, 0.67537268,
  243. 0.08483369, -0.02534994, -0.02205197, -0.01667999, -0.00384443
  244. };
  245. Array s_d_up8 {
  246. -0.02928613, -0.03706353, -0.03783812, -0.03324558, -0.00447632, -0.02519406, -0.03752601, -0.03901508, -0.03663285, -0.00646649,
  247. -0.02066407, -0.03838633, -0.04002101, -0.03900035, -0.00901973, -0.01626393, -0.03954148, -0.04046620, -0.03979621, -0.01224485,
  248. 0.29895328, 0.35757708, -0.02447552, -0.01081748, -0.04314594, 0.23903219, 0.41119301, -0.00573046, -0.01450239, -0.04246845,
  249. 0.17567618, 0.45220643, 0.02287757, -0.01936783, -0.03583255, 0.11572472, 0.47416733, 0.06284440, -0.02685066, 0.42720050,
  250. -0.02248939, -0.01155273, -0.04562755, 0.28689496, 0.49093869, -0.00007891, -0.01545926, -0.04562659, 0.21238920, 0.53980934,
  251. 0.03369474, -0.02070211, -0.03866988, 0.14229550, 0.56593398, 0.08045181, -0.02888298, -0.03680918, -0.00542229, -0.02920477,
  252. -0.02788574, -0.02118180, -0.03942402, -0.00775547, -0.02433614, -0.03193943, -0.02030828, -0.04044014, -0.01074016, -0.01930822,
  253. -0.03620399, -0.01974125, -0.03919545, -0.01456093, -0.00045072, -0.00360110, -0.01020207, -0.01231907, -0.00638988, -0.00071592,
  254. -0.00279122, -0.00957115, -0.01288327, -0.00730937, -0.00107783, -0.00210156, -0.00890705, -0.01317668, -0.00813895, -0.00153491,
  255. -0.02128481, -0.04173044, -0.04831487, -0.03293190, -0.00525260, -0.01720322, -0.04052736, -0.05045706, -0.03607317, -0.00738030,
  256. -0.01341764, -0.03965629, -0.05151616, -0.03814886, -0.01005819, 0.18968273, 0.33063684, -0.01300105, -0.01372950, -0.04017465,
  257. 0.13727832, 0.36402234, 0.01027890, -0.01832107, -0.03365072, 0.08734506, 0.38194295, 0.04338228, -0.02525993, 0.56408126,
  258. 0.00458352, -0.01648227, -0.04887868, 0.24585519, 0.62026135, 0.04314807, -0.02213737, -0.04158014, 0.16637289, 0.65027023,
  259. 0.09621636, -0.03101388, -0.04082742, -0.00904519, -0.02790922, -0.02117818, 0.00798662, -0.03995711, -0.01243427, -0.02231705,
  260. -0.02946266, 0.00992055, -0.03600283, -0.01684920, -0.00111684, -0.00411204, -0.01297130, -0.01723725, -0.01022545, -0.00165306,
  261. -0.00313110, -0.01218016, -0.01763266, -0.01125620, -0.00231663, -0.01374149, -0.03797620, -0.05142937, -0.03117307, -0.00581914,
  262. -0.01064003, -0.03608089, -0.05272168, -0.03375670, -0.00795586, 0.09628104, 0.27129991, -0.00353779, -0.01734151, -0.03153981,
  263. 0.05686230, 0.28500998, 0.02230594, -0.02374955, 0.68214326, 0.05018048, -0.02320852, -0.04383616, 0.18459474, 0.71517975,
  264. 0.10805613, -0.03263677, -0.03637639, -0.01394373, -0.02511203, -0.01728636, 0.05407331, -0.02867568, -0.01893131, -0.00240854,
  265. -0.00446511, -0.01636187, -0.02377053, -0.01522848, -0.00333334, -0.00819975, -0.02964169, -0.04499287, -0.02745350, -0.00612408,
  266. 0.02727416, 0.19446600, 0.00159832, -0.02232473, 0.74982506, 0.11452620, -0.03348048, -0.01605681, -0.02070339, -0.00458223
  267. };
  268. ///
  269. /// D.3 - Image metadata
  270. struct PreviewHeader {
  271. };
  272. struct AnimationHeader {
  273. };
  274. struct ExtraChannelInfo {
  275. enum class ExtraChannelType {
  276. kAlpha = 0,
  277. kDepth = 1,
  278. kSpotColour = 2,
  279. kSelectionMask = 3,
  280. kBlack = 4,
  281. kCFA = 5,
  282. kThermal = 6,
  283. kNonOptional = 15,
  284. kOptional = 16,
  285. };
  286. bool d_alpha { true };
  287. ExtraChannelType type { ExtraChannelType::kAlpha };
  288. BitDepth bit_depth {};
  289. u32 dim_shift {};
  290. String name;
  291. bool alpha_associated { false };
  292. };
  293. static ErrorOr<ExtraChannelInfo> read_extra_channel_info(LittleEndianInputBitStream& stream)
  294. {
  295. ExtraChannelInfo extra_channel_info;
  296. extra_channel_info.d_alpha = TRY(stream.read_bit());
  297. if (!extra_channel_info.d_alpha) {
  298. extra_channel_info.type = TRY(read_enum<ExtraChannelInfo::ExtraChannelType>(stream));
  299. extra_channel_info.bit_depth = TRY(read_bit_depth(stream));
  300. extra_channel_info.dim_shift = U32(0, 3, 4, 1 + TRY(stream.read_bits(3)));
  301. extra_channel_info.name = TRY(read_string(stream));
  302. if (extra_channel_info.type == ExtraChannelInfo::ExtraChannelType::kAlpha)
  303. extra_channel_info.alpha_associated = TRY(stream.read_bit());
  304. }
  305. if (extra_channel_info.type != ExtraChannelInfo::ExtraChannelType::kAlpha) {
  306. TODO();
  307. }
  308. return extra_channel_info;
  309. }
  310. struct ToneMapping {
  311. float intensity_target { 255 };
  312. float min_nits { 0 };
  313. bool relative_to_max_display { false };
  314. float linear_below { 0 };
  315. };
  316. static ErrorOr<ToneMapping> read_tone_mapping(LittleEndianInputBitStream& stream)
  317. {
  318. ToneMapping tone_mapping;
  319. bool const all_default = TRY(stream.read_bit());
  320. if (!all_default) {
  321. TODO();
  322. }
  323. return tone_mapping;
  324. }
  325. struct OpsinInverseMatrix {
  326. };
  327. static ErrorOr<OpsinInverseMatrix> read_opsin_inverse_matrix(LittleEndianInputBitStream&)
  328. {
  329. TODO();
  330. }
  331. struct ImageMetadata {
  332. u8 orientation { 1 };
  333. Optional<SizeHeader> intrinsic_size;
  334. Optional<PreviewHeader> preview;
  335. Optional<AnimationHeader> animation;
  336. BitDepth bit_depth;
  337. bool modular_16bit_buffers { true };
  338. u16 num_extra_channels {};
  339. Vector<ExtraChannelInfo, 4> ec_info;
  340. bool xyb_encoded { true };
  341. ColourEncoding colour_encoding;
  342. ToneMapping tone_mapping;
  343. Extensions extensions;
  344. bool default_m;
  345. OpsinInverseMatrix opsin_inverse_matrix;
  346. u8 cw_mask { 0 };
  347. Array<double, 15> up2_weight = s_d_up2;
  348. Array<double, 55> up4_weight = s_d_up4;
  349. Array<double, 210> up8_weight = s_d_up8;
  350. u16 number_of_color_channels() const
  351. {
  352. if (!xyb_encoded && colour_encoding.colour_space == ColourEncoding::ColourSpace::kGrey)
  353. return 1;
  354. return 3;
  355. }
  356. u16 number_of_channels() const
  357. {
  358. return number_of_color_channels() + num_extra_channels;
  359. }
  360. Optional<u16> alpha_channel() const
  361. {
  362. for (u16 i = 0; i < ec_info.size(); ++i) {
  363. if (ec_info[i].type == ExtraChannelInfo::ExtraChannelType::kAlpha)
  364. return i + number_of_color_channels();
  365. }
  366. return OptionalNone {};
  367. }
  368. };
  369. static ErrorOr<ImageMetadata> read_metadata_header(LittleEndianInputBitStream& stream)
  370. {
  371. ImageMetadata metadata;
  372. bool const all_default = TRY(stream.read_bit());
  373. if (!all_default) {
  374. bool const extra_fields = TRY(stream.read_bit());
  375. if (extra_fields) {
  376. metadata.orientation = 1 + TRY(stream.read_bits(3));
  377. bool const have_intr_size = TRY(stream.read_bit());
  378. if (have_intr_size)
  379. metadata.intrinsic_size = TRY(read_size_header(stream));
  380. bool const have_preview = TRY(stream.read_bit());
  381. if (have_preview)
  382. TODO();
  383. bool const have_animation = TRY(stream.read_bit());
  384. if (have_animation)
  385. TODO();
  386. }
  387. metadata.bit_depth = TRY(read_bit_depth(stream));
  388. metadata.modular_16bit_buffers = TRY(stream.read_bit());
  389. metadata.num_extra_channels = U32(0, 1, 2 + TRY(stream.read_bits(4)), 1 + TRY(stream.read_bits(12)));
  390. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  391. metadata.ec_info.append(TRY(read_extra_channel_info(stream)));
  392. metadata.xyb_encoded = TRY(stream.read_bit());
  393. metadata.colour_encoding = TRY(read_colour_encoding(stream));
  394. if (extra_fields)
  395. metadata.tone_mapping = TRY(read_tone_mapping(stream));
  396. metadata.extensions = TRY(read_extensions(stream));
  397. }
  398. metadata.default_m = TRY(stream.read_bit());
  399. if (!metadata.default_m && metadata.xyb_encoded)
  400. metadata.opsin_inverse_matrix = TRY(read_opsin_inverse_matrix(stream));
  401. if (!metadata.default_m)
  402. metadata.cw_mask = TRY(stream.read_bits(3));
  403. if (metadata.cw_mask != 0)
  404. TODO();
  405. return metadata;
  406. }
  407. ///
  408. /// Table F.7 — BlendingInfo bundle
  409. struct BlendingInfo {
  410. enum class BlendMode {
  411. kReplace = 0,
  412. kAdd = 1,
  413. kBlend = 2,
  414. kMulAdd = 3,
  415. kMul = 4,
  416. };
  417. BlendMode mode {};
  418. u8 alpha_channel {};
  419. bool clamp { false };
  420. u8 source {};
  421. };
  422. static ErrorOr<BlendingInfo> read_blending_info(LittleEndianInputBitStream& stream, ImageMetadata const& metadata, bool full_frame)
  423. {
  424. BlendingInfo blending_info;
  425. blending_info.mode = static_cast<BlendingInfo::BlendMode>(U32(0, 1, 2, 3 + TRY(stream.read_bits(2))));
  426. bool const extra = metadata.num_extra_channels > 0;
  427. if (extra) {
  428. auto const blend_or_mul_add = blending_info.mode == BlendingInfo::BlendMode::kBlend
  429. || blending_info.mode == BlendingInfo::BlendMode::kMulAdd;
  430. if (blend_or_mul_add)
  431. blending_info.alpha_channel = U32(0, 1, 2, 3 + TRY(stream.read_bits(3)));
  432. if (blend_or_mul_add || blending_info.mode == BlendingInfo::BlendMode::kMul)
  433. blending_info.clamp = TRY(stream.read_bit());
  434. }
  435. if (blending_info.mode != BlendingInfo::BlendMode::kReplace
  436. || !full_frame) {
  437. blending_info.source = TRY(stream.read_bits(2));
  438. }
  439. return blending_info;
  440. }
  441. ///
  442. /// J.1 - General
  443. struct RestorationFilter {
  444. bool gab { true };
  445. u8 epf_iters { 2 };
  446. Extensions extensions;
  447. };
  448. static ErrorOr<RestorationFilter> read_restoration_filter(LittleEndianInputBitStream& stream)
  449. {
  450. RestorationFilter restoration_filter;
  451. auto const all_defaults = TRY(stream.read_bit());
  452. if (!all_defaults) {
  453. restoration_filter.gab = TRY(stream.read_bit());
  454. if (restoration_filter.gab) {
  455. TODO();
  456. }
  457. restoration_filter.epf_iters = TRY(stream.read_bits(2));
  458. if (restoration_filter.epf_iters != 0) {
  459. TODO();
  460. }
  461. restoration_filter.extensions = TRY(read_extensions(stream));
  462. }
  463. return restoration_filter;
  464. }
  465. ///
  466. /// Table F.6 — Passes bundle
  467. struct Passes {
  468. u8 num_passes { 1 };
  469. };
  470. static ErrorOr<Passes> read_passes(LittleEndianInputBitStream& stream)
  471. {
  472. Passes passes;
  473. passes.num_passes = U32(1, 2, 3, 4 + TRY(stream.read_bits(3)));
  474. if (passes.num_passes != 1) {
  475. TODO();
  476. }
  477. return passes;
  478. }
  479. ///
  480. /// F.2 - FrameHeader
  481. struct FrameHeader {
  482. enum class FrameType {
  483. kRegularFrame = 0,
  484. kLFFrame = 1,
  485. kReferenceOnly = 2,
  486. kSkipProgressive = 3,
  487. };
  488. enum class Encoding {
  489. kVarDCT = 0,
  490. kModular = 1,
  491. };
  492. enum class Flags {
  493. None = 0,
  494. kNoise = 1,
  495. kPatches = 1 << 1,
  496. kSplines = 1 << 4,
  497. kUseLfFrame = 1 << 5,
  498. kSkipAdaptiveLFSmoothing = 1 << 7,
  499. };
  500. FrameType frame_type { FrameType::kRegularFrame };
  501. Encoding encoding { Encoding::kVarDCT };
  502. Flags flags { Flags::None };
  503. bool do_YCbCr { false };
  504. Array<u8, 3> jpeg_upsampling {};
  505. u8 upsampling {};
  506. FixedArray<u8> ec_upsampling {};
  507. u8 group_size_shift { 1 };
  508. Passes passes {};
  509. u8 lf_level {};
  510. bool have_crop { false };
  511. BlendingInfo blending_info {};
  512. FixedArray<BlendingInfo> ec_blending_info {};
  513. u32 duration {};
  514. bool is_last { true };
  515. u8 save_as_reference {};
  516. bool save_before_ct {};
  517. String name {};
  518. RestorationFilter restoration_filter {};
  519. Extensions extensions {};
  520. };
  521. static int operator&(FrameHeader::Flags first, FrameHeader::Flags second)
  522. {
  523. return static_cast<int>(first) & static_cast<int>(second);
  524. }
  525. static ErrorOr<FrameHeader> read_frame_header(LittleEndianInputBitStream& stream, ImageMetadata const& metadata)
  526. {
  527. FrameHeader frame_header;
  528. bool const all_default = TRY(stream.read_bit());
  529. if (!all_default) {
  530. frame_header.frame_type = static_cast<FrameHeader::FrameType>(TRY(stream.read_bits(2)));
  531. frame_header.encoding = static_cast<FrameHeader::Encoding>(TRY(stream.read_bits(1)));
  532. frame_header.flags = static_cast<FrameHeader::Flags>(TRY(U64(stream)));
  533. if (!metadata.xyb_encoded)
  534. frame_header.do_YCbCr = TRY(stream.read_bit());
  535. if (!(frame_header.flags & FrameHeader::Flags::kUseLfFrame)) {
  536. if (frame_header.do_YCbCr) {
  537. frame_header.jpeg_upsampling[0] = TRY(stream.read_bits(2));
  538. frame_header.jpeg_upsampling[1] = TRY(stream.read_bits(2));
  539. frame_header.jpeg_upsampling[2] = TRY(stream.read_bits(2));
  540. }
  541. frame_header.upsampling = U32(1, 2, 4, 8);
  542. frame_header.ec_upsampling = TRY(FixedArray<u8>::create(metadata.num_extra_channels));
  543. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  544. frame_header.ec_upsampling[i] = U32(1, 2, 4, 8);
  545. }
  546. if (frame_header.encoding == FrameHeader::Encoding::kModular)
  547. frame_header.group_size_shift = TRY(stream.read_bits(2));
  548. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  549. TODO();
  550. if (frame_header.frame_type != FrameHeader::FrameType::kReferenceOnly)
  551. frame_header.passes = TRY(read_passes(stream));
  552. if (frame_header.frame_type == FrameHeader::FrameType::kLFFrame)
  553. TODO();
  554. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame)
  555. frame_header.have_crop = TRY(stream.read_bit());
  556. if (frame_header.have_crop)
  557. TODO();
  558. bool const normal_frame = frame_header.frame_type == FrameHeader::FrameType::kRegularFrame
  559. || frame_header.frame_type == FrameHeader::FrameType::kSkipProgressive;
  560. // FIXME: also consider "cropped" image of the dimension of the frame
  561. VERIFY(!frame_header.have_crop);
  562. bool const full_frame = !frame_header.have_crop;
  563. if (normal_frame) {
  564. frame_header.blending_info = TRY(read_blending_info(stream, metadata, full_frame));
  565. frame_header.ec_blending_info = TRY(FixedArray<BlendingInfo>::create(metadata.num_extra_channels));
  566. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  567. frame_header.ec_blending_info[i] = TRY(read_blending_info(stream, metadata, full_frame));
  568. if (metadata.animation.has_value())
  569. TODO();
  570. frame_header.is_last = TRY(stream.read_bit());
  571. }
  572. // FIXME: Ensure that is_last has the correct default value
  573. VERIFY(normal_frame);
  574. auto const resets_canvas = full_frame && frame_header.blending_info.mode == BlendingInfo::BlendMode::kReplace;
  575. auto const can_reference = !frame_header.is_last && (frame_header.duration == 0 || frame_header.save_as_reference != 0) && frame_header.frame_type != FrameHeader::FrameType::kLFFrame;
  576. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame) {
  577. if (!frame_header.is_last)
  578. TODO();
  579. }
  580. frame_header.save_before_ct = !normal_frame;
  581. if (frame_header.frame_type == FrameHeader::FrameType::kReferenceOnly || (resets_canvas && can_reference))
  582. frame_header.save_before_ct = TRY(stream.read_bit());
  583. frame_header.name = TRY(read_string(stream));
  584. frame_header.restoration_filter = TRY(read_restoration_filter(stream));
  585. frame_header.extensions = TRY(read_extensions(stream));
  586. }
  587. return frame_header;
  588. }
  589. ///
  590. /// F.3 TOC
  591. struct TOC {
  592. FixedArray<u32> entries;
  593. FixedArray<u32> group_offsets;
  594. };
  595. static u64 num_toc_entries(FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  596. {
  597. // F.3.1 - General
  598. if (num_groups == 1 && frame_header.passes.num_passes == 1)
  599. return 1;
  600. return 1 + num_lf_groups + 1 + num_groups * frame_header.passes.num_passes;
  601. }
  602. static ErrorOr<TOC> read_toc(LittleEndianInputBitStream& stream, FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  603. {
  604. TOC toc;
  605. bool const permuted_toc = TRY(stream.read_bit());
  606. if (permuted_toc) {
  607. // Read permutations
  608. TODO();
  609. }
  610. // F.3.3 - Decoding TOC
  611. stream.align_to_byte_boundary();
  612. auto const toc_entries = num_toc_entries(frame_header, num_groups, num_lf_groups);
  613. toc.entries = TRY(FixedArray<u32>::create(toc_entries));
  614. toc.group_offsets = TRY(FixedArray<u32>::create(toc_entries));
  615. for (u32 i {}; i < toc_entries; ++i) {
  616. auto const new_entry = U32(
  617. TRY(stream.read_bits(10)),
  618. 1024 + TRY(stream.read_bits(14)),
  619. 17408 + TRY(stream.read_bits(22)),
  620. 4211712 + TRY(stream.read_bits(30)));
  621. toc.entries[i] = new_entry;
  622. toc.group_offsets[i] = (i == 0 ? 0 : toc.group_offsets[i - 1]) + new_entry;
  623. }
  624. if (permuted_toc)
  625. TODO();
  626. stream.align_to_byte_boundary();
  627. return toc;
  628. }
  629. ///
  630. /// G.1.2 - LF channel dequantization weights
  631. struct LfChannelDequantization {
  632. float m_x_lf_unscaled { 4096 };
  633. float m_y_lf_unscaled { 512 };
  634. float m_b_lf_unscaled { 256 };
  635. };
  636. static ErrorOr<LfChannelDequantization> read_lf_channel_dequantization(LittleEndianInputBitStream& stream)
  637. {
  638. LfChannelDequantization lf_channel_dequantization;
  639. auto const all_default = TRY(stream.read_bit());
  640. if (!all_default) {
  641. TODO();
  642. }
  643. return lf_channel_dequantization;
  644. }
  645. ///
  646. /// C - Entropy decoding
  647. class ANSHistogram {
  648. public:
  649. static ErrorOr<ANSHistogram> read_histogram(LittleEndianInputBitStream& stream, u8 log_alphabet_size)
  650. {
  651. ANSHistogram histogram;
  652. auto const alphabet_size = TRY(histogram.read_ans_distribution(stream, log_alphabet_size));
  653. // C.2.6 - Alias mapping
  654. histogram.m_log_bucket_size = 12 - log_alphabet_size;
  655. histogram.m_bucket_size = 1 << histogram.m_log_bucket_size;
  656. auto const table_size = 1 << log_alphabet_size;
  657. Optional<u64> index_of_unique_symbol {};
  658. for (u64 i {}; i < histogram.m_distribution.size(); ++i) {
  659. if (histogram.m_distribution[i] == 1 << 12)
  660. index_of_unique_symbol = i;
  661. }
  662. TRY(histogram.m_symbols.try_resize(table_size));
  663. TRY(histogram.m_offsets.try_resize(table_size));
  664. TRY(histogram.m_cutoffs.try_resize(table_size));
  665. if (index_of_unique_symbol.has_value()) {
  666. auto const s = *index_of_unique_symbol;
  667. for (i32 i = 0; i < table_size; i++) {
  668. histogram.m_symbols[i] = s;
  669. histogram.m_offsets[i] = histogram.m_bucket_size * i;
  670. histogram.m_cutoffs[i] = 0;
  671. }
  672. return histogram;
  673. }
  674. Vector<u16> overfull;
  675. Vector<u16> underfull;
  676. for (u16 i {}; i < alphabet_size; i++) {
  677. histogram.m_cutoffs[i] = histogram.m_distribution[i];
  678. histogram.m_symbols[i] = i;
  679. if (histogram.m_cutoffs[i] > histogram.m_bucket_size)
  680. TRY(overfull.try_append(i));
  681. else if (histogram.m_cutoffs[i] < histogram.m_bucket_size)
  682. TRY(underfull.try_append(i));
  683. }
  684. for (u16 i = alphabet_size; i < table_size; i++) {
  685. histogram.m_cutoffs[i] = 0;
  686. TRY(underfull.try_append(i));
  687. }
  688. while (overfull.size() > 0) {
  689. VERIFY(underfull.size() > 0);
  690. auto const o = overfull.take_last();
  691. auto const u = underfull.take_last();
  692. auto const by = histogram.m_bucket_size - histogram.m_cutoffs[u];
  693. histogram.m_cutoffs[o] -= by;
  694. histogram.m_symbols[u] = o;
  695. histogram.m_offsets[u] = histogram.m_cutoffs[o];
  696. if (histogram.m_cutoffs[o] < histogram.m_bucket_size)
  697. TRY(underfull.try_append(o));
  698. else if (histogram.m_cutoffs[o] > histogram.m_bucket_size)
  699. TRY(overfull.try_append(o));
  700. }
  701. for (u16 i {}; i < table_size; i++) {
  702. if (histogram.m_cutoffs[i] == histogram.m_bucket_size) {
  703. histogram.m_symbols[i] = i;
  704. histogram.m_offsets[i] = 0;
  705. histogram.m_cutoffs[i] = 0;
  706. } else {
  707. histogram.m_offsets[i] -= histogram.m_cutoffs[i];
  708. }
  709. }
  710. return histogram;
  711. }
  712. ErrorOr<u16> read_symbol(LittleEndianInputBitStream& stream, Optional<u32>& state) const
  713. {
  714. if (!state.has_value())
  715. state = TRY(stream.read_bits(32));
  716. auto const index = *state & 0xFFF;
  717. auto const symbol_and_offset = alias_mapping(index);
  718. state = m_distribution[symbol_and_offset.symbol] * (*state >> 12) + symbol_and_offset.offset;
  719. if (*state < (1 << 16))
  720. state = (*state << 16) | TRY(stream.read_bits(16));
  721. return symbol_and_offset.symbol;
  722. }
  723. private:
  724. static ErrorOr<u8> U8(LittleEndianInputBitStream& stream)
  725. {
  726. if (TRY(stream.read_bit()) == 0)
  727. return 0;
  728. auto const n = TRY(stream.read_bits(3));
  729. return TRY(stream.read_bits(n)) + (1 << n);
  730. }
  731. struct SymbolAndOffset {
  732. u16 symbol {};
  733. u16 offset {};
  734. };
  735. SymbolAndOffset alias_mapping(u32 x) const
  736. {
  737. // C.2.6 - Alias mapping
  738. auto const i = x >> m_log_bucket_size;
  739. auto const pos = x & (m_bucket_size - 1);
  740. u16 const symbol = pos >= m_cutoffs[i] ? m_symbols[i] : i;
  741. u16 const offset = pos >= m_cutoffs[i] ? m_offsets[i] + pos : pos;
  742. return { symbol, offset };
  743. }
  744. static ErrorOr<u16> read_with_prefix(LittleEndianInputBitStream& stream)
  745. {
  746. auto const prefix = TRY(stream.read_bits(3));
  747. switch (prefix) {
  748. case 0:
  749. return 10;
  750. case 1:
  751. for (auto const possibility : { 4, 0, 11, 13 }) {
  752. if (TRY(stream.read_bit()))
  753. return possibility;
  754. }
  755. return 12;
  756. case 2:
  757. return 7;
  758. case 3:
  759. return TRY(stream.read_bit()) ? 1 : 3;
  760. case 4:
  761. return 6;
  762. case 5:
  763. return 8;
  764. case 6:
  765. return 9;
  766. case 7:
  767. return TRY(stream.read_bit()) ? 2 : 5;
  768. default:
  769. VERIFY_NOT_REACHED();
  770. }
  771. }
  772. ErrorOr<u16> read_ans_distribution(LittleEndianInputBitStream& stream, u8 log_alphabet_size)
  773. {
  774. // C.2.5 ANS distribution decoding
  775. auto const table_size = 1 << log_alphabet_size;
  776. m_distribution = TRY(FixedArray<i32>::create(table_size));
  777. if (TRY(stream.read_bit())) {
  778. u16 alphabet_size {};
  779. if (TRY(stream.read_bit())) {
  780. auto const v1 = TRY(U8(stream));
  781. auto const v2 = TRY(U8(stream));
  782. VERIFY(v1 != v2);
  783. m_distribution[v1] = TRY(stream.read_bits(12));
  784. m_distribution[v2] = (1 << 12) - m_distribution[v1];
  785. alphabet_size = 1 + max(v1, v2);
  786. } else {
  787. auto const x = TRY(U8(stream));
  788. m_distribution[x] = 1 << 12;
  789. alphabet_size = 1 + x;
  790. }
  791. return alphabet_size;
  792. }
  793. if (TRY(stream.read_bit())) {
  794. auto const alphabet_size = TRY(U8(stream)) + 1;
  795. for (u16 i = 0; i < alphabet_size; i++)
  796. m_distribution[i] = (1 << 12) / alphabet_size;
  797. for (u16 i = 0; i < ((1 << 12) % alphabet_size); i++)
  798. m_distribution[i]++;
  799. return alphabet_size;
  800. }
  801. u8 len = 0;
  802. while (len < 3) {
  803. if (TRY(stream.read_bit()))
  804. len++;
  805. else
  806. break;
  807. }
  808. u8 const shift = TRY(stream.read_bits(len)) + (1 << len) - 1;
  809. VERIFY(shift <= 13);
  810. auto const alphabet_size = TRY(U8(stream)) + 3;
  811. i32 omit_log = -1;
  812. i32 omit_pos = -1;
  813. auto same = TRY(FixedArray<i32>::create(alphabet_size));
  814. auto logcounts = TRY(FixedArray<i32>::create(alphabet_size));
  815. u8 rle {};
  816. for (u16 i = 0; i < alphabet_size; i++) {
  817. logcounts[i] = TRY(read_with_prefix(stream));
  818. if (logcounts[i] == 13) {
  819. rle = TRY(U8(stream));
  820. same[i] = rle + 5;
  821. i += rle + 3;
  822. continue;
  823. }
  824. if (logcounts[i] > omit_log) {
  825. omit_log = logcounts[i];
  826. omit_pos = i;
  827. }
  828. }
  829. VERIFY(m_distribution[omit_pos] >= 0);
  830. VERIFY(omit_pos + 1 >= alphabet_size || logcounts[omit_pos + 1] != 13);
  831. i32 prev = 0;
  832. i32 numsame = 0;
  833. i64 total_count {};
  834. for (u16 i = 0; i < alphabet_size; i++) {
  835. if (same[i] != 0) {
  836. numsame = same[i] - 1;
  837. prev = i > 0 ? m_distribution[i - 1] : 0;
  838. }
  839. if (numsame > 0) {
  840. m_distribution[i] = prev;
  841. numsame--;
  842. } else {
  843. auto const code = logcounts[i];
  844. if (i == omit_pos || code == 0)
  845. continue;
  846. if (code == 1) {
  847. m_distribution[i] = 1;
  848. } else {
  849. auto const bitcount = min(max(0, shift - ((12 - code + 1) >> 1)), code - 1);
  850. m_distribution[i] = (1 << (code - 1)) + (TRY(stream.read_bits(bitcount)) << (code - 1 - bitcount));
  851. }
  852. }
  853. total_count += m_distribution[i];
  854. }
  855. m_distribution[omit_pos] = (1 << 12) - total_count;
  856. VERIFY(m_distribution[omit_pos] >= 0);
  857. return alphabet_size;
  858. }
  859. Vector<u16> m_symbols;
  860. Vector<u16> m_offsets;
  861. Vector<u16> m_cutoffs;
  862. FixedArray<i32> m_distribution;
  863. u16 m_log_bucket_size {};
  864. u16 m_bucket_size {};
  865. };
  866. class EntropyDecoder {
  867. AK_MAKE_NONCOPYABLE(EntropyDecoder);
  868. AK_MAKE_DEFAULT_MOVABLE(EntropyDecoder);
  869. public:
  870. EntropyDecoder() = default;
  871. ~EntropyDecoder()
  872. {
  873. if (m_state.has_value() && *m_state != 0x130000)
  874. dbgln("JPEGXLLoader: ANS decoder left in invalid state");
  875. }
  876. static ErrorOr<EntropyDecoder> create(LittleEndianInputBitStream& stream, u8 initial_num_distrib)
  877. {
  878. EntropyDecoder entropy_decoder;
  879. // C.2 - Distribution decoding
  880. entropy_decoder.m_lz77_enabled = TRY(stream.read_bit());
  881. if (entropy_decoder.m_lz77_enabled) {
  882. TODO();
  883. }
  884. TRY(entropy_decoder.read_pre_clustered_distributions(stream, initial_num_distrib));
  885. bool const use_prefix_code = TRY(stream.read_bit());
  886. if (!use_prefix_code)
  887. entropy_decoder.m_log_alphabet_size = 5 + TRY(stream.read_bits(2));
  888. for (auto& config : entropy_decoder.m_configs)
  889. config = TRY(entropy_decoder.read_config(stream));
  890. if (use_prefix_code) {
  891. entropy_decoder.m_distributions = Vector<BrotliCanonicalCode> {};
  892. auto& distributions = entropy_decoder.m_distributions.get<Vector<BrotliCanonicalCode>>();
  893. TRY(distributions.try_resize(entropy_decoder.m_configs.size()));
  894. Vector<u16> counts;
  895. TRY(counts.try_resize(entropy_decoder.m_configs.size()));
  896. for (auto& count : counts) {
  897. if (TRY(stream.read_bit())) {
  898. auto const n = TRY(stream.read_bits(4));
  899. count = 1 + (1 << n) + TRY(stream.read_bits(n));
  900. } else {
  901. count = 1;
  902. }
  903. }
  904. // After reading the counts, the decoder reads each D[i] (implicitly
  905. // described by a prefix code) as specified in C.2.4, with alphabet_size = count[i].
  906. for (u32 i {}; i < distributions.size(); ++i) {
  907. // The alphabet size mentioned in the [Brotli] RFC is explicitly specified as parameter alphabet_size
  908. // when the histogram is being decoded, except in the special case of alphabet_size == 1, where no
  909. // histogram is read, and all decoded symbols are zero without reading any bits at all.
  910. if (counts[i] != 1)
  911. distributions[i] = TRY(BrotliCanonicalCode::read_prefix_code(stream, counts[i]));
  912. else
  913. distributions[i] = BrotliCanonicalCode { { 1 }, { 0 } };
  914. }
  915. } else {
  916. entropy_decoder.m_distributions = Vector<ANSHistogram> {};
  917. auto& distributions = entropy_decoder.m_distributions.get<Vector<ANSHistogram>>();
  918. TRY(distributions.try_ensure_capacity(entropy_decoder.m_configs.size()));
  919. for (u32 i = 0; i < entropy_decoder.m_configs.size(); ++i)
  920. distributions.empend(TRY(ANSHistogram::read_histogram(stream, entropy_decoder.m_log_alphabet_size)));
  921. }
  922. return entropy_decoder;
  923. }
  924. ErrorOr<u32> decode_hybrid_uint(LittleEndianInputBitStream& stream, u16 context)
  925. {
  926. // C.3.3 - Hybrid integer decoding
  927. if (m_lz77_enabled)
  928. TODO();
  929. // Read symbol from entropy coded stream using D[clusters[ctx]]
  930. u32 token {};
  931. TRY(m_distributions.visit(
  932. [&](Vector<BrotliCanonicalCode> const& distributions) -> ErrorOr<void> {
  933. token = TRY(distributions[m_clusters[context]].read_symbol(stream));
  934. return {};
  935. },
  936. [&](Vector<ANSHistogram> const& distributions) -> ErrorOr<void> {
  937. token = TRY(distributions[m_clusters[context]].read_symbol(stream, m_state));
  938. return {};
  939. }));
  940. auto r = TRY(read_uint(stream, m_configs[m_clusters[context]], token));
  941. return r;
  942. }
  943. private:
  944. using BrotliCanonicalCode = Compress::Brotli::CanonicalCode;
  945. struct HybridUint {
  946. u32 split_exponent {};
  947. u32 split {};
  948. u32 msb_in_token {};
  949. u32 lsb_in_token {};
  950. };
  951. static ErrorOr<u32> read_uint(LittleEndianInputBitStream& stream, HybridUint const& config, u32 token)
  952. {
  953. if (token < config.split)
  954. return token;
  955. auto const n = config.split_exponent
  956. - config.msb_in_token
  957. - config.lsb_in_token
  958. + ((token - config.split) >> (config.msb_in_token + config.lsb_in_token));
  959. VERIFY(n < 32);
  960. u32 const low_bits = token & ((1 << config.lsb_in_token) - 1);
  961. token = token >> config.lsb_in_token;
  962. token &= (1 << config.msb_in_token) - 1;
  963. token |= (1 << config.msb_in_token);
  964. auto const result = ((token << n | TRY(stream.read_bits(n))) << config.lsb_in_token) | low_bits;
  965. VERIFY(result < (1ul << 32));
  966. return result;
  967. }
  968. ErrorOr<void> read_pre_clustered_distributions(LittleEndianInputBitStream& stream, u8 num_distrib)
  969. {
  970. // C.2.2 Distribution clustering
  971. if (num_distrib == 1) {
  972. // If num_dist == 1, then num_clusters = 1 and clusters[0] = 0, and the remainder of this subclause is skipped.
  973. m_clusters = { 0 };
  974. TRY(m_configs.try_resize(1));
  975. return {};
  976. };
  977. TRY(m_clusters.try_resize(num_distrib));
  978. bool const is_simple = TRY(stream.read_bit());
  979. u16 num_clusters = 0;
  980. if (is_simple) {
  981. u8 const nbits = TRY(stream.read_bits(2));
  982. for (u8 i {}; i < num_distrib; ++i) {
  983. m_clusters[i] = TRY(stream.read_bits(nbits));
  984. if (m_clusters[i] >= num_clusters)
  985. num_clusters = m_clusters[i] + 1;
  986. }
  987. } else {
  988. TODO();
  989. }
  990. TRY(m_configs.try_resize(num_clusters));
  991. return {};
  992. }
  993. ErrorOr<HybridUint> read_config(LittleEndianInputBitStream& stream) const
  994. {
  995. // C.2.3 - Hybrid integer configuration
  996. HybridUint config {};
  997. config.split_exponent = TRY(stream.read_bits(ceil(log2(m_log_alphabet_size + 1))));
  998. if (config.split_exponent != m_log_alphabet_size) {
  999. auto nbits = ceil(log2(config.split_exponent + 1));
  1000. config.msb_in_token = TRY(stream.read_bits(nbits));
  1001. nbits = ceil(log2(config.split_exponent - config.msb_in_token + 1));
  1002. config.lsb_in_token = TRY(stream.read_bits(nbits));
  1003. } else {
  1004. config.msb_in_token = 0;
  1005. config.lsb_in_token = 0;
  1006. }
  1007. config.split = 1 << config.split_exponent;
  1008. return config;
  1009. }
  1010. bool m_lz77_enabled {};
  1011. Vector<u32> m_clusters;
  1012. Vector<HybridUint> m_configs;
  1013. u8 m_log_alphabet_size { 15 };
  1014. Variant<Vector<BrotliCanonicalCode>, Vector<ANSHistogram>> m_distributions { Vector<BrotliCanonicalCode> {} }; // D in the spec
  1015. Optional<u32> m_state {};
  1016. };
  1017. ///
  1018. /// H.4.2 - MA tree decoding
  1019. class MATree {
  1020. public:
  1021. struct LeafNode {
  1022. u8 ctx {};
  1023. u8 predictor {};
  1024. i32 offset {};
  1025. u32 multiplier {};
  1026. };
  1027. static ErrorOr<MATree> decode(LittleEndianInputBitStream& stream, Optional<EntropyDecoder>& decoder)
  1028. {
  1029. // G.1.3 - GlobalModular
  1030. MATree tree;
  1031. // 1 / 2 Read the 6 pre-clustered distributions
  1032. auto const num_distrib = 6;
  1033. if (!decoder.has_value())
  1034. decoder = TRY(EntropyDecoder::create(stream, num_distrib));
  1035. // 2 / 2 Decode the tree
  1036. u64 ctx_id = 0;
  1037. u64 nodes_left = 1;
  1038. tree.m_tree.clear();
  1039. while (nodes_left > 0) {
  1040. nodes_left--;
  1041. i32 const property = TRY(decoder->decode_hybrid_uint(stream, 1)) - 1;
  1042. if (property >= 0) {
  1043. DecisionNode decision_node;
  1044. decision_node.property = property;
  1045. decision_node.value = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 0)));
  1046. decision_node.left_child = tree.m_tree.size() + nodes_left + 1;
  1047. decision_node.right_child = tree.m_tree.size() + nodes_left + 2;
  1048. tree.m_tree.empend(decision_node);
  1049. nodes_left += 2;
  1050. } else {
  1051. LeafNode leaf_node;
  1052. leaf_node.ctx = ctx_id++;
  1053. leaf_node.predictor = TRY(decoder->decode_hybrid_uint(stream, 2));
  1054. leaf_node.offset = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 3)));
  1055. auto const mul_log = TRY(decoder->decode_hybrid_uint(stream, 4));
  1056. auto const mul_bits = TRY(decoder->decode_hybrid_uint(stream, 5));
  1057. leaf_node.multiplier = (mul_bits + 1) << mul_log;
  1058. tree.m_tree.empend(leaf_node);
  1059. }
  1060. }
  1061. // Finally, the decoder reads (tree.size() + 1) / 2 pre-clustered distributions D as specified in C.1.
  1062. auto const num_pre_clustered_distributions = (tree.m_tree.size() + 1) / 2;
  1063. decoder = TRY(decoder->create(stream, num_pre_clustered_distributions));
  1064. return tree;
  1065. }
  1066. LeafNode get_leaf(Vector<i32> const& properties) const
  1067. {
  1068. // To find the MA leaf node, the MA tree is traversed, starting at the root node tree[0]
  1069. // and for each decision node d, testing if property[d.property] > d.value, proceeding to
  1070. // the node tree[d.left_child] if the test evaluates to true and to the node tree[d.right_child]
  1071. // otherwise, until a leaf node is reached.
  1072. DecisionNode node { m_tree[0].get<DecisionNode>() };
  1073. while (true) {
  1074. auto const next_node = [this, &properties, &node]() {
  1075. // Note: The behavior when trying to access a non-existing property is taken from jxl-oxide
  1076. if (node.property < properties.size() && properties[node.property] > node.value)
  1077. return m_tree[node.left_child];
  1078. return m_tree[node.right_child];
  1079. }();
  1080. if (next_node.has<LeafNode>())
  1081. return next_node.get<LeafNode>();
  1082. node = next_node.get<DecisionNode>();
  1083. }
  1084. }
  1085. private:
  1086. struct DecisionNode {
  1087. u64 property {};
  1088. i64 value {};
  1089. u64 left_child {};
  1090. u64 right_child {};
  1091. };
  1092. Vector<Variant<DecisionNode, LeafNode>> m_tree;
  1093. };
  1094. ///
  1095. /// H.5 - Self-correcting predictor
  1096. struct WPHeader {
  1097. u8 wp_p1 { 16 };
  1098. u8 wp_p2 { 10 };
  1099. u8 wp_p3a { 7 };
  1100. u8 wp_p3b { 7 };
  1101. u8 wp_p3c { 7 };
  1102. u8 wp_p3d { 0 };
  1103. u8 wp_p3e { 0 };
  1104. Array<u8, 4> wp_w { 13, 12, 12, 12 };
  1105. };
  1106. static ErrorOr<WPHeader> read_self_correcting_predictor(LittleEndianInputBitStream& stream)
  1107. {
  1108. WPHeader self_correcting_predictor {};
  1109. bool const default_wp = TRY(stream.read_bit());
  1110. if (!default_wp) {
  1111. TODO();
  1112. }
  1113. return self_correcting_predictor;
  1114. }
  1115. ///
  1116. ///
  1117. struct TransformInfo {
  1118. enum class TransformId {
  1119. kRCT = 0,
  1120. kPalette = 1,
  1121. kSqueeze = 2,
  1122. };
  1123. TransformId tr {};
  1124. u32 begin_c {};
  1125. u32 rct_type {};
  1126. };
  1127. static ErrorOr<TransformInfo> read_transform_info(LittleEndianInputBitStream& stream)
  1128. {
  1129. TransformInfo transform_info;
  1130. transform_info.tr = static_cast<TransformInfo::TransformId>(TRY(stream.read_bits(2)));
  1131. if (transform_info.tr != TransformInfo::TransformId::kSqueeze) {
  1132. transform_info.begin_c = U32(
  1133. TRY(stream.read_bits(3)),
  1134. 8 + TRY(stream.read_bits(3)),
  1135. 72 + TRY(stream.read_bits(10)),
  1136. 1096 + TRY(stream.read_bits(13)));
  1137. }
  1138. if (transform_info.tr == TransformInfo::TransformId::kRCT) {
  1139. transform_info.rct_type = U32(
  1140. 6,
  1141. TRY(stream.read_bits(2)),
  1142. 2 + TRY(stream.read_bits(4)),
  1143. 10 + TRY(stream.read_bits(6)));
  1144. }
  1145. if (transform_info.tr != TransformInfo::TransformId::kRCT)
  1146. TODO();
  1147. return transform_info;
  1148. }
  1149. ///
  1150. /// Local abstractions to store the decoded image
  1151. class Channel {
  1152. public:
  1153. static ErrorOr<Channel> create(u32 width, u32 height)
  1154. {
  1155. Channel channel;
  1156. channel.m_width = width;
  1157. channel.m_height = height;
  1158. TRY(channel.m_pixels.try_resize(channel.m_width * channel.m_height));
  1159. return channel;
  1160. }
  1161. i32 get(u32 x, u32 y) const
  1162. {
  1163. return m_pixels[x * m_width + y];
  1164. }
  1165. void set(u32 x, u32 y, i32 value)
  1166. {
  1167. m_pixels[x * m_width + y] = value;
  1168. }
  1169. u32 width() const
  1170. {
  1171. return m_width;
  1172. }
  1173. u32 height() const
  1174. {
  1175. return m_height;
  1176. }
  1177. u32 hshift() const
  1178. {
  1179. return m_hshift;
  1180. }
  1181. u32 vshift() const
  1182. {
  1183. return m_vshift;
  1184. }
  1185. bool decoded() const
  1186. {
  1187. return m_decoded;
  1188. }
  1189. void set_decoded(bool decoded)
  1190. {
  1191. m_decoded = decoded;
  1192. }
  1193. private:
  1194. u32 m_width {};
  1195. u32 m_height {};
  1196. u32 m_hshift {};
  1197. u32 m_vshift {};
  1198. bool m_decoded { false };
  1199. Vector<i32> m_pixels {};
  1200. };
  1201. class Image {
  1202. public:
  1203. static ErrorOr<Image> create(IntSize size, ImageMetadata const& metadata)
  1204. {
  1205. Image image {};
  1206. for (u16 i = 0; i < metadata.number_of_channels(); ++i) {
  1207. if (i < metadata.number_of_color_channels()) {
  1208. TRY(image.m_channels.try_append(TRY(Channel::create(size.width(), size.height()))));
  1209. } else {
  1210. auto const dim_shift = metadata.ec_info[i - metadata.number_of_color_channels()].dim_shift;
  1211. TRY(image.m_channels.try_append(TRY(Channel::create(size.width() >> dim_shift, size.height() >> dim_shift))));
  1212. }
  1213. }
  1214. return image;
  1215. }
  1216. ErrorOr<NonnullRefPtr<Bitmap>> to_bitmap(ImageMetadata& metadata) const
  1217. {
  1218. // FIXME: which channel size should we use?
  1219. auto const width = m_channels[0].width();
  1220. auto const height = m_channels[0].height();
  1221. auto bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { width, height }));
  1222. auto const alpha_channel = metadata.alpha_channel();
  1223. auto const bits_per_sample = metadata.bit_depth.bits_per_sample;
  1224. VERIFY(bits_per_sample >= 8);
  1225. for (u32 y {}; y < height; ++y) {
  1226. for (u32 x {}; x < width; ++x) {
  1227. auto const to_u8 = [&, bits_per_sample](i32 sample) -> u8 {
  1228. // FIXME: Don't truncate the result to 8 bits
  1229. static constexpr auto maximum_supported_bit_depth = 8;
  1230. if (bits_per_sample > maximum_supported_bit_depth)
  1231. sample >>= (bits_per_sample - maximum_supported_bit_depth);
  1232. return clamp(sample + .5, 0, (1 << maximum_supported_bit_depth) - 1);
  1233. };
  1234. auto const color = [&]() -> Color {
  1235. if (!alpha_channel.has_value()) {
  1236. return { to_u8(m_channels[0].get(x, y)),
  1237. to_u8(m_channels[1].get(x, y)),
  1238. to_u8(m_channels[2].get(x, y)) };
  1239. }
  1240. return {
  1241. to_u8(m_channels[0].get(x, y)),
  1242. to_u8(m_channels[1].get(x, y)),
  1243. to_u8(m_channels[2].get(x, y)),
  1244. to_u8(m_channels[*alpha_channel].get(x, y)),
  1245. };
  1246. }();
  1247. bitmap->set_pixel(x, y, color);
  1248. }
  1249. }
  1250. return bitmap;
  1251. }
  1252. Vector<Channel>& channels()
  1253. {
  1254. return m_channels;
  1255. }
  1256. private:
  1257. Vector<Channel> m_channels;
  1258. };
  1259. ///
  1260. /// H.5 - Self-correcting predictor
  1261. struct Neighborhood {
  1262. i32 N {};
  1263. i32 NW {};
  1264. i32 NE {};
  1265. i32 W {};
  1266. i32 NN {};
  1267. i32 WW {};
  1268. i32 NEE {};
  1269. };
  1270. class SelfCorrectingData {
  1271. public:
  1272. struct Predictions {
  1273. i32 prediction {};
  1274. Array<i32, 4> subpred {};
  1275. i32 max_error {};
  1276. i32 true_err {};
  1277. Array<i32, 4> err {};
  1278. };
  1279. static ErrorOr<SelfCorrectingData> create(WPHeader const& wp_params, u32 width)
  1280. {
  1281. SelfCorrectingData self_correcting_data { wp_params };
  1282. self_correcting_data.m_width = width;
  1283. self_correcting_data.m_previous = TRY(FixedArray<Predictions>::create(width));
  1284. self_correcting_data.m_current_row = TRY(FixedArray<Predictions>::create(width));
  1285. self_correcting_data.m_next_row = TRY(FixedArray<Predictions>::create(width));
  1286. return self_correcting_data;
  1287. }
  1288. void register_next_row()
  1289. {
  1290. auto tmp = move(m_previous);
  1291. m_previous = move(m_current_row);
  1292. m_current_row = move(m_next_row);
  1293. // We reuse m_previous to avoid an allocation, no values are kept
  1294. // everything will be overridden.
  1295. m_next_row = move(tmp);
  1296. m_current_row_index++;
  1297. }
  1298. Predictions compute_predictions(Neighborhood const& neighborhood, u32 x)
  1299. {
  1300. auto& current_predictions = m_next_row[x];
  1301. auto const N3 = neighborhood.N << 3;
  1302. auto const NW3 = neighborhood.NW << 3;
  1303. auto const NE3 = neighborhood.NE << 3;
  1304. auto const W3 = neighborhood.W << 3;
  1305. auto const NN3 = neighborhood.NN << 3;
  1306. auto const predictions_W = predictions_for(x, Direction::West);
  1307. auto const predictions_N = predictions_for(x, Direction::North);
  1308. auto const predictions_NE = predictions_for(x, Direction::NorthEast);
  1309. auto const predictions_NW = predictions_for(x, Direction::NorthWest);
  1310. auto const predictions_WW = predictions_for(x, Direction::WestWest);
  1311. current_predictions.subpred[0] = W3 + NE3 - N3;
  1312. current_predictions.subpred[1] = N3 - (((predictions_W.true_err + predictions_N.true_err + predictions_NE.true_err) * wp_params.wp_p1) >> 5);
  1313. current_predictions.subpred[2] = W3 - (((predictions_W.true_err + predictions_N.true_err + predictions_NW.true_err) * wp_params.wp_p2) >> 5);
  1314. current_predictions.subpred[3] = N3 - ((predictions_NW.true_err * wp_params.wp_p3a + predictions_N.true_err * wp_params.wp_p3b + predictions_NE.true_err * wp_params.wp_p3c + (NN3 - N3) * wp_params.wp_p3d + (NW3 - W3) * wp_params.wp_p3e) >> 5);
  1315. auto const error2weight = [](i32 err_sum, u8 maxweight) -> i32 {
  1316. i32 shift = floor(log2(err_sum + 1)) - 5;
  1317. if (shift < 0)
  1318. shift = 0;
  1319. return 4 + ((static_cast<u64>(maxweight) * ((1 << 24) / ((err_sum >> shift) + 1))) >> shift);
  1320. };
  1321. Array<i32, 4> weight {};
  1322. for (u8 i = 0; i < weight.size(); ++i) {
  1323. auto err_sum = predictions_N.err[i] + predictions_W.err[i] + predictions_NW.err[i] + predictions_WW.err[i] + predictions_NE.err[i];
  1324. if (x == m_width - 1)
  1325. err_sum += predictions_W.err[i];
  1326. weight[i] = error2weight(err_sum, wp_params.wp_w[i]);
  1327. }
  1328. auto sum_weights = weight[0] + weight[1] + weight[2] + weight[3];
  1329. i32 const log_weight = floor(log2(sum_weights)) + 1;
  1330. for (u8 i = 0; i < 4; i++)
  1331. weight[i] = weight[i] >> (log_weight - 5);
  1332. sum_weights = weight[0] + weight[1] + weight[2] + weight[3];
  1333. auto s = (sum_weights >> 1) - 1;
  1334. for (u8 i = 0; i < 4; i++)
  1335. s += current_predictions.subpred[i] * weight[i];
  1336. current_predictions.prediction = static_cast<u64>(s) * ((1 << 24) / sum_weights) >> 24;
  1337. // if true_err_N, true_err_W and true_err_NW don't have the same sign
  1338. if (((predictions_N.true_err ^ predictions_W.true_err) | (predictions_N.true_err ^ predictions_NW.true_err)) <= 0) {
  1339. current_predictions.prediction = clamp(current_predictions.prediction, min(W3, min(N3, NE3)), max(W3, max(N3, NE3)));
  1340. }
  1341. auto& max_error = current_predictions.max_error;
  1342. max_error = predictions_W.true_err;
  1343. if (abs(predictions_N.true_err) > abs(max_error))
  1344. max_error = predictions_N.true_err;
  1345. if (abs(predictions_NW.true_err) > abs(max_error))
  1346. max_error = predictions_NW.true_err;
  1347. if (abs(predictions_NE.true_err) > abs(max_error))
  1348. max_error = predictions_NE.true_err;
  1349. return current_predictions;
  1350. }
  1351. // H.5.1 - General
  1352. void compute_errors(u32 x, i32 true_value)
  1353. {
  1354. auto& current_predictions = m_next_row[x];
  1355. current_predictions.true_err = current_predictions.prediction - (true_value << 3);
  1356. for (u8 i = 0; i < 4; ++i)
  1357. current_predictions.err[i] = (abs(current_predictions.subpred[i] - (true_value << 3)) + 3) >> 3;
  1358. }
  1359. private:
  1360. SelfCorrectingData(WPHeader const& wp)
  1361. : wp_params(wp)
  1362. {
  1363. }
  1364. enum class Direction {
  1365. North,
  1366. NorthWest,
  1367. NorthEast,
  1368. West,
  1369. NorthNorth,
  1370. WestWest
  1371. };
  1372. Predictions predictions_for(u32 x, Direction direction) const
  1373. {
  1374. // H.5.2 - Prediction
  1375. auto const north = [&]() {
  1376. return m_current_row_index < 1 ? Predictions {} : m_current_row[x];
  1377. };
  1378. switch (direction) {
  1379. case Direction::North:
  1380. return north();
  1381. case Direction::NorthWest:
  1382. return x < 1 ? north() : m_current_row[x - 1];
  1383. case Direction::NorthEast:
  1384. return x + 1 >= m_current_row.size() ? north() : m_current_row[x + 1];
  1385. case Direction::West:
  1386. return x < 1 ? Predictions {} : m_next_row[x - 1];
  1387. case Direction::NorthNorth:
  1388. return m_current_row_index < 2 ? Predictions {} : m_previous[x];
  1389. case Direction::WestWest:
  1390. return x < 2 ? Predictions {} : m_next_row[x - 2];
  1391. }
  1392. VERIFY_NOT_REACHED();
  1393. }
  1394. WPHeader const& wp_params {};
  1395. u32 m_width {};
  1396. u32 m_current_row_index {};
  1397. FixedArray<Predictions> m_previous {};
  1398. FixedArray<Predictions> m_current_row {};
  1399. FixedArray<Predictions> m_next_row {};
  1400. };
  1401. ///
  1402. /// H.2 - Image decoding
  1403. struct ModularHeader {
  1404. bool use_global_tree {};
  1405. WPHeader wp_params {};
  1406. Vector<TransformInfo> transform {};
  1407. };
  1408. static ErrorOr<Vector<i32>> get_properties(Vector<Channel> const& channels, u16 i, u32 x, u32 y, i32 max_error)
  1409. {
  1410. Vector<i32> properties;
  1411. // Table H.4 - Property definitions
  1412. TRY(properties.try_append(i));
  1413. // FIXME: Handle other cases than GlobalModular
  1414. TRY(properties.try_append(0));
  1415. TRY(properties.try_append(y));
  1416. TRY(properties.try_append(x));
  1417. i32 const W = x > 0 ? channels[i].get(x - 1, y) : (y > 0 ? channels[i].get(x, y - 1) : 0);
  1418. i32 const N = y > 0 ? channels[i].get(x, y - 1) : W;
  1419. i32 const NW = x > 0 && y > 0 ? channels[i].get(x - 1, y - 1) : W;
  1420. i32 const NE = x + 1 < channels[i].width() && y > 0 ? channels[i].get(x + 1, y - 1) : N;
  1421. i32 const NN = y > 1 ? channels[i].get(x, y - 2) : N;
  1422. i32 const WW = x > 1 ? channels[i].get(x - 2, y) : W;
  1423. TRY(properties.try_append(abs(N)));
  1424. TRY(properties.try_append(abs(W)));
  1425. TRY(properties.try_append(N));
  1426. TRY(properties.try_append(W));
  1427. // x > 0 ? W - /* (the value of property 9 at position (x - 1, y)) */ : W
  1428. if (x > 0) {
  1429. auto const x_1 = x - 1;
  1430. i32 const W_x_1 = x_1 > 0 ? channels[i].get(x_1 - 1, y) : (y > 0 ? channels[i].get(x_1, y - 1) : 0);
  1431. i32 const N_x_1 = y > 0 ? channels[i].get(x_1, y - 1) : W_x_1;
  1432. i32 const NW_x_1 = x_1 > 0 && y > 0 ? channels[i].get(x_1 - 1, y - 1) : W_x_1;
  1433. TRY(properties.try_append(W - (W_x_1 + N_x_1 - NW_x_1)));
  1434. } else {
  1435. TRY(properties.try_append(W));
  1436. }
  1437. TRY(properties.try_append(W + N - NW));
  1438. TRY(properties.try_append(W - NW));
  1439. TRY(properties.try_append(NW - N));
  1440. TRY(properties.try_append(N - NE));
  1441. TRY(properties.try_append(N - NN));
  1442. TRY(properties.try_append(W - WW));
  1443. TRY(properties.try_append(max_error));
  1444. for (i16 j = i - 1; j >= 0; j--) {
  1445. if (channels[j].width() != channels[i].width())
  1446. continue;
  1447. if (channels[j].height() != channels[i].height())
  1448. continue;
  1449. if (channels[j].hshift() != channels[i].hshift())
  1450. continue;
  1451. if (channels[j].vshift() != channels[i].vshift())
  1452. continue;
  1453. auto rC = channels[j].get(x, y);
  1454. auto rW = (x > 0 ? channels[j].get(x - 1, y) : 0);
  1455. auto rN = (y > 0 ? channels[j].get(x, y - 1) : rW);
  1456. auto rNW = (x > 0 && y > 0 ? channels[j].get(x - 1, y - 1) : rW);
  1457. auto rG = clamp(rW + rN - rNW, min(rW, rN), max(rW, rN));
  1458. TRY(properties.try_append(abs(rC)));
  1459. TRY(properties.try_append(rC));
  1460. TRY(properties.try_append(abs(rC - rG)));
  1461. TRY(properties.try_append(rC - rG));
  1462. }
  1463. return properties;
  1464. }
  1465. static i32 prediction(Neighborhood const& neighborhood, i32 self_correcting, u32 predictor)
  1466. {
  1467. switch (predictor) {
  1468. case 0:
  1469. return 0;
  1470. case 1:
  1471. return neighborhood.W;
  1472. case 2:
  1473. return neighborhood.N;
  1474. case 3:
  1475. return (neighborhood.W + neighborhood.N) / 2;
  1476. case 4:
  1477. return abs(neighborhood.N - neighborhood.NW) < abs(neighborhood.W - neighborhood.NW) ? neighborhood.W : neighborhood.N;
  1478. case 5:
  1479. return clamp(neighborhood.W + neighborhood.N - neighborhood.NW, min(neighborhood.W, neighborhood.N), max(neighborhood.W, neighborhood.N));
  1480. case 6:
  1481. return (self_correcting + 3) >> 3;
  1482. case 7:
  1483. return neighborhood.NE;
  1484. case 8:
  1485. return neighborhood.NW;
  1486. case 9:
  1487. return neighborhood.WW;
  1488. case 10:
  1489. return (neighborhood.W + neighborhood.NW) / 2;
  1490. case 11:
  1491. return (neighborhood.N + neighborhood.NW) / 2;
  1492. case 12:
  1493. return (neighborhood.N + neighborhood.NE) / 2;
  1494. case 13:
  1495. return (6 * neighborhood.N - 2 * neighborhood.NN + 7 * neighborhood.W + neighborhood.WW + neighborhood.NEE + 3 * neighborhood.NE + 8) / 16;
  1496. }
  1497. VERIFY_NOT_REACHED();
  1498. }
  1499. static Neighborhood retrieve_neighborhood(Channel const& channel, u32 x, u32 y)
  1500. {
  1501. i32 const W = x > 0 ? channel.get(x - 1, y) : (y > 0 ? channel.get(x, y - 1) : 0);
  1502. i32 const N = y > 0 ? channel.get(x, y - 1) : W;
  1503. i32 const NW = x > 0 && y > 0 ? channel.get(x - 1, y - 1) : W;
  1504. i32 const NE = x + 1 < channel.width() && y > 0 ? channel.get(x + 1, y - 1) : N;
  1505. i32 const NN = y > 1 ? channel.get(x, y - 2) : N;
  1506. i32 const WW = x > 1 ? channel.get(x - 2, y) : W;
  1507. i32 const NEE = x + 2 < channel.width() && y > 0 ? channel.get(x + 2, y - 1) : NE;
  1508. Neighborhood const neighborhood {
  1509. .N = N,
  1510. .NW = NW,
  1511. .NE = NE,
  1512. .W = W,
  1513. .NN = NN,
  1514. .WW = WW,
  1515. .NEE = NEE,
  1516. };
  1517. return neighborhood;
  1518. }
  1519. static ErrorOr<ModularHeader> read_modular_header(LittleEndianInputBitStream& stream,
  1520. Image& image,
  1521. Optional<EntropyDecoder>& decoder,
  1522. MATree const& global_tree,
  1523. u16 num_channels)
  1524. {
  1525. ModularHeader modular_header;
  1526. modular_header.use_global_tree = TRY(stream.read_bit());
  1527. modular_header.wp_params = TRY(read_self_correcting_predictor(stream));
  1528. auto const nb_transforms = U32(0, 1, 2 + TRY(stream.read_bits(4)), 18 + TRY(stream.read_bits(8)));
  1529. TRY(modular_header.transform.try_resize(nb_transforms));
  1530. for (u32 i {}; i < nb_transforms; ++i)
  1531. modular_header.transform[i] = TRY(read_transform_info(stream));
  1532. Optional<MATree> local_tree;
  1533. if (!modular_header.use_global_tree)
  1534. TODO();
  1535. // The decoder then starts an entropy-coded stream (C.1) and decodes the data for each channel
  1536. // (in ascending order of index) as specified in H.3, skipping any channels having width or height
  1537. // zero. Finally, the inverse transformations are applied (from last to first) as described in H.6.
  1538. auto const& tree = local_tree.has_value() ? *local_tree : global_tree;
  1539. for (u16 i {}; i < num_channels; ++i) {
  1540. auto self_correcting_data = TRY(SelfCorrectingData::create(modular_header.wp_params, image.channels()[i].width()));
  1541. for (u32 y {}; y < image.channels()[i].height(); y++) {
  1542. for (u32 x {}; x < image.channels()[i].width(); x++) {
  1543. auto const neighborhood = retrieve_neighborhood(image.channels()[i], x, y);
  1544. auto const self_prediction = self_correcting_data.compute_predictions(neighborhood, x);
  1545. auto const properties = TRY(get_properties(image.channels(), i, x, y, self_prediction.max_error));
  1546. auto const leaf_node = tree.get_leaf(properties);
  1547. auto diff = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, leaf_node.ctx)));
  1548. diff = (diff * leaf_node.multiplier) + leaf_node.offset;
  1549. auto const total = diff + prediction(neighborhood, self_prediction.prediction, leaf_node.predictor);
  1550. self_correcting_data.compute_errors(x, total);
  1551. image.channels()[i].set(x, y, total);
  1552. }
  1553. self_correcting_data.register_next_row();
  1554. }
  1555. image.channels()[i].set_decoded(true);
  1556. }
  1557. return modular_header;
  1558. }
  1559. ///
  1560. /// G.1.2 - LF channel dequantization weights
  1561. struct GlobalModular {
  1562. MATree ma_tree;
  1563. ModularHeader modular_header;
  1564. };
  1565. static ErrorOr<GlobalModular> read_global_modular(LittleEndianInputBitStream& stream,
  1566. Image& image,
  1567. FrameHeader const& frame_header,
  1568. ImageMetadata const& metadata,
  1569. Optional<EntropyDecoder>& entropy_decoder)
  1570. {
  1571. GlobalModular global_modular;
  1572. auto const decode_ma_tree = TRY(stream.read_bit());
  1573. if (decode_ma_tree)
  1574. global_modular.ma_tree = TRY(MATree::decode(stream, entropy_decoder));
  1575. // The decoder then decodes a modular sub-bitstream (Annex H), where
  1576. // the number of channels is computed as follows:
  1577. auto num_channels = metadata.num_extra_channels;
  1578. if (frame_header.encoding == FrameHeader::Encoding::kModular) {
  1579. if (!frame_header.do_YCbCr && !metadata.xyb_encoded
  1580. && metadata.colour_encoding.colour_space == ColourEncoding::ColourSpace::kGrey) {
  1581. num_channels += 1;
  1582. } else {
  1583. num_channels += 3;
  1584. }
  1585. }
  1586. // FIXME: Ensure this spec comment:
  1587. // However, the decoder only decodes the first nb_meta_channels channels and any further channels
  1588. // that have a width and height that are both at most group_dim. At that point, it stops decoding.
  1589. // No inverse transforms are applied yet.
  1590. global_modular.modular_header = TRY(read_modular_header(stream, image, entropy_decoder, global_modular.ma_tree, num_channels));
  1591. return global_modular;
  1592. }
  1593. ///
  1594. /// G.1 - LfGlobal
  1595. struct LfGlobal {
  1596. LfChannelDequantization lf_dequant;
  1597. GlobalModular gmodular;
  1598. };
  1599. static ErrorOr<LfGlobal> read_lf_global(LittleEndianInputBitStream& stream,
  1600. Image& image,
  1601. FrameHeader const& frame_header,
  1602. ImageMetadata const& metadata,
  1603. Optional<EntropyDecoder>& entropy_decoder)
  1604. {
  1605. LfGlobal lf_global;
  1606. if (frame_header.flags != FrameHeader::Flags::None)
  1607. TODO();
  1608. lf_global.lf_dequant = TRY(read_lf_channel_dequantization(stream));
  1609. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  1610. TODO();
  1611. lf_global.gmodular = TRY(read_global_modular(stream, image, frame_header, metadata, entropy_decoder));
  1612. return lf_global;
  1613. }
  1614. ///
  1615. /// G.2 - LfGroup
  1616. static ErrorOr<void> read_lf_group(LittleEndianInputBitStream&,
  1617. Image& image,
  1618. FrameHeader const& frame_header)
  1619. {
  1620. // LF coefficients
  1621. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1622. TODO();
  1623. }
  1624. // ModularLfGroup
  1625. for (auto const& channel : image.channels()) {
  1626. if (channel.decoded())
  1627. continue;
  1628. if (channel.hshift() < 3 || channel.vshift() < 3)
  1629. continue;
  1630. // This code actually only detect that we need to read a null image
  1631. // so a no-op. It should be fully rewritten when we add proper support
  1632. // for LfGroup.
  1633. TODO();
  1634. }
  1635. // HF metadata
  1636. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1637. TODO();
  1638. }
  1639. return {};
  1640. }
  1641. ///
  1642. /// H.6 - Transformations
  1643. static void apply_rct(Image& image, TransformInfo const& transformation)
  1644. {
  1645. auto& channels = image.channels();
  1646. for (u32 y {}; y < channels[transformation.begin_c].height(); y++) {
  1647. for (u32 x {}; x < channels[transformation.begin_c].width(); x++) {
  1648. auto a = channels[transformation.begin_c + 0].get(x, y);
  1649. auto b = channels[transformation.begin_c + 1].get(x, y);
  1650. auto c = channels[transformation.begin_c + 2].get(x, y);
  1651. i32 d {};
  1652. i32 e {};
  1653. i32 f {};
  1654. auto const permutation = transformation.rct_type / 7;
  1655. auto const type = transformation.rct_type % 7;
  1656. if (type == 6) { // YCgCo
  1657. auto const tmp = a - (c >> 1);
  1658. e = c + tmp;
  1659. f = tmp - (b >> 1);
  1660. d = f + b;
  1661. } else {
  1662. if (type & 1)
  1663. c = c + a;
  1664. if ((type >> 1) == 1)
  1665. b = b + a;
  1666. if ((type >> 1) == 2)
  1667. b = b + ((a + c) >> 1);
  1668. d = a;
  1669. e = b;
  1670. f = c;
  1671. }
  1672. Array<i32, 3> v {};
  1673. v[permutation % 3] = d;
  1674. v[(permutation + 1 + (permutation / 3)) % 3] = e;
  1675. v[(permutation + 2 - (permutation / 3)) % 3] = f;
  1676. channels[transformation.begin_c + 0].set(x, y, v[0]);
  1677. channels[transformation.begin_c + 1].set(x, y, v[1]);
  1678. channels[transformation.begin_c + 2].set(x, y, v[2]);
  1679. }
  1680. }
  1681. }
  1682. static void apply_transformation(Image& image, TransformInfo const& transformation)
  1683. {
  1684. switch (transformation.tr) {
  1685. case TransformInfo::TransformId::kRCT:
  1686. apply_rct(image, transformation);
  1687. break;
  1688. case TransformInfo::TransformId::kPalette:
  1689. case TransformInfo::TransformId::kSqueeze:
  1690. TODO();
  1691. default:
  1692. VERIFY_NOT_REACHED();
  1693. }
  1694. }
  1695. ///
  1696. /// G.3.2 - PassGroup
  1697. static ErrorOr<void> read_pass_group(LittleEndianInputBitStream& stream,
  1698. Image& image,
  1699. FrameHeader const& frame_header,
  1700. u32 group_dim)
  1701. {
  1702. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1703. (void)stream;
  1704. TODO();
  1705. }
  1706. auto& channels = image.channels();
  1707. for (u16 i {}; i < channels.size(); ++i) {
  1708. // Skip meta-channels
  1709. // FIXME: Also test if the channel has already been decoded
  1710. // See: nb_meta_channels in the spec
  1711. bool const is_meta_channel = channels[i].width() <= group_dim
  1712. || channels[i].height() <= group_dim
  1713. || channels[i].hshift() >= 3
  1714. || channels[i].vshift() >= 3;
  1715. if (!is_meta_channel)
  1716. TODO();
  1717. }
  1718. return {};
  1719. }
  1720. ///
  1721. /// Table F.1 — Frame bundle
  1722. struct Frame {
  1723. FrameHeader frame_header;
  1724. TOC toc;
  1725. LfGlobal lf_global;
  1726. u64 width {};
  1727. u64 height {};
  1728. u64 num_groups {};
  1729. u64 num_lf_groups {};
  1730. };
  1731. static ErrorOr<Frame> read_frame(LittleEndianInputBitStream& stream,
  1732. Image& image,
  1733. SizeHeader const& size_header,
  1734. ImageMetadata const& metadata,
  1735. Optional<EntropyDecoder>& entropy_decoder)
  1736. {
  1737. // F.1 - General
  1738. // Each Frame is byte-aligned by invoking ZeroPadToByte() (B.2.7)
  1739. stream.align_to_byte_boundary();
  1740. Frame frame;
  1741. frame.frame_header = TRY(read_frame_header(stream, metadata));
  1742. if (!frame.frame_header.have_crop) {
  1743. frame.width = size_header.width;
  1744. frame.height = size_header.height;
  1745. } else {
  1746. TODO();
  1747. }
  1748. if (frame.frame_header.upsampling > 1) {
  1749. frame.width = ceil(static_cast<double>(frame.width) / frame.frame_header.upsampling);
  1750. frame.height = ceil(static_cast<double>(frame.height) / frame.frame_header.upsampling);
  1751. }
  1752. if (frame.frame_header.lf_level > 0)
  1753. TODO();
  1754. // F.2 - FrameHeader
  1755. auto const group_dim = 128 << frame.frame_header.group_size_shift;
  1756. auto const frame_width = static_cast<double>(frame.width);
  1757. auto const frame_height = static_cast<double>(frame.height);
  1758. frame.num_groups = ceil(frame_width / group_dim) * ceil(frame_height / group_dim);
  1759. frame.num_lf_groups = ceil(frame_width / (group_dim * 8)) * ceil(frame_height / (group_dim * 8));
  1760. frame.toc = TRY(read_toc(stream, frame.frame_header, frame.num_groups, frame.num_lf_groups));
  1761. image = TRY(Image::create({ frame.width, frame.height }, metadata));
  1762. frame.lf_global = TRY(read_lf_global(stream, image, frame.frame_header, metadata, entropy_decoder));
  1763. for (u32 i {}; i < frame.num_lf_groups; ++i)
  1764. TRY(read_lf_group(stream, image, frame.frame_header));
  1765. if (frame.frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1766. TODO();
  1767. }
  1768. auto const num_pass_group = frame.num_groups * frame.frame_header.passes.num_passes;
  1769. auto const& transform_infos = frame.lf_global.gmodular.modular_header.transform;
  1770. for (u64 i {}; i < num_pass_group; ++i)
  1771. TRY(read_pass_group(stream, image, frame.frame_header, group_dim));
  1772. // G.4.2 - Modular group data
  1773. // When all modular groups are decoded, the inverse transforms are applied to
  1774. // the at that point fully decoded GlobalModular image, as specified in H.6.
  1775. for (auto const& transformation : transform_infos.in_reverse())
  1776. apply_transformation(image, transformation);
  1777. return frame;
  1778. }
  1779. ///
  1780. /// 5.2 - Mirroring
  1781. static u32 mirror_1d(i32 coord, u32 size)
  1782. {
  1783. if (coord < 0)
  1784. return mirror_1d(-coord - 1, size);
  1785. else if (static_cast<u32>(coord) >= size)
  1786. return mirror_1d(2 * size - 1 - coord, size);
  1787. else
  1788. return coord;
  1789. }
  1790. ///
  1791. /// K - Image features
  1792. static ErrorOr<void> apply_upsampling(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1793. {
  1794. Optional<u32> ec_max;
  1795. for (auto upsampling : frame.frame_header.ec_upsampling) {
  1796. if (!ec_max.has_value() || upsampling > *ec_max)
  1797. ec_max = upsampling;
  1798. }
  1799. if (frame.frame_header.upsampling > 1 || ec_max.value_or(0) > 1) {
  1800. if (ec_max.value_or(0) > 2)
  1801. TODO();
  1802. auto const k = frame.frame_header.upsampling;
  1803. auto weight = [k, &metadata](u8 index) -> double {
  1804. if (k == 2)
  1805. return metadata.up2_weight[index];
  1806. if (k == 4)
  1807. return metadata.up4_weight[index];
  1808. return metadata.up8_weight[index];
  1809. };
  1810. // FIXME: Use ec_upsampling for extra-channels
  1811. for (auto& channel : image.channels()) {
  1812. auto upsampled = TRY(Channel::create(k * channel.width(), k * channel.height()));
  1813. // Loop over the original image
  1814. for (u32 y {}; y < channel.height(); y++) {
  1815. for (u32 x {}; x < channel.width(); x++) {
  1816. // Loop over the upsampling factor
  1817. for (u8 kx {}; kx < k; ++kx) {
  1818. for (u8 ky {}; ky < k; ++ky) {
  1819. double sum {};
  1820. // Loop over the W window
  1821. double W_min = NumericLimits<double>::max();
  1822. double W_max = -NumericLimits<double>::max();
  1823. for (u8 ix {}; ix < 5; ++ix) {
  1824. for (u8 iy {}; iy < 5; ++iy) {
  1825. auto const j = (ky < k / 2) ? (iy + 5 * ky) : ((4 - iy) + 5 * (k - 1 - ky));
  1826. auto const i = (kx < k / 2) ? (ix + 5 * kx) : ((4 - ix) + 5 * (k - 1 - kx));
  1827. auto const minimum = min(i, j);
  1828. auto const maximum = max(i, j);
  1829. auto const index = 5 * k * minimum / 2 - minimum * (minimum - 1) / 2 + maximum - minimum;
  1830. auto const origin_sample_x = mirror_1d(x + ix - 2, channel.width());
  1831. auto const origin_sample_y = mirror_1d(y + iy - 2, channel.height());
  1832. auto const origin_sample = channel.get(origin_sample_x, origin_sample_y);
  1833. W_min = min(W_min, origin_sample);
  1834. W_max = max(W_max, origin_sample);
  1835. sum += origin_sample * weight(index);
  1836. }
  1837. }
  1838. // The resulting sample is clamped to the range [a, b] where a and b are
  1839. // the minimum and maximum of the samples in W.
  1840. sum = clamp(sum, W_min, W_max);
  1841. upsampled.set(x * k + kx, y * k + ky, sum);
  1842. }
  1843. }
  1844. }
  1845. }
  1846. channel = move(upsampled);
  1847. }
  1848. }
  1849. return {};
  1850. }
  1851. static ErrorOr<void> apply_image_features(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1852. {
  1853. TRY(apply_upsampling(image, metadata, frame));
  1854. if (frame.frame_header.flags != FrameHeader::Flags::None)
  1855. TODO();
  1856. return {};
  1857. }
  1858. ///
  1859. /// L.4 - Extra channel rendering
  1860. static ErrorOr<void> render_extra_channels(Image&, ImageMetadata const& metadata)
  1861. {
  1862. for (u16 i = metadata.number_of_color_channels(); i < metadata.number_of_channels(); ++i) {
  1863. auto const ec_index = i - metadata.number_of_color_channels();
  1864. if (metadata.ec_info[ec_index].dim_shift != 0)
  1865. TODO();
  1866. }
  1867. return {};
  1868. }
  1869. ///
  1870. class JPEGXLLoadingContext {
  1871. public:
  1872. JPEGXLLoadingContext(NonnullOwnPtr<Stream> stream)
  1873. : m_stream(move(stream))
  1874. {
  1875. }
  1876. ErrorOr<void> decode_image_header()
  1877. {
  1878. constexpr auto JPEGXL_SIGNATURE = 0xFF0A;
  1879. auto const signature = TRY(m_stream.read_value<BigEndian<u16>>());
  1880. if (signature != JPEGXL_SIGNATURE)
  1881. return Error::from_string_literal("Unrecognized signature");
  1882. m_header = TRY(read_size_header(m_stream));
  1883. m_metadata = TRY(read_metadata_header(m_stream));
  1884. m_state = State::HeaderDecoded;
  1885. return {};
  1886. }
  1887. ErrorOr<void> decode_frame()
  1888. {
  1889. Image image {};
  1890. auto const frame = TRY(read_frame(m_stream, image, m_header, m_metadata, m_entropy_decoder));
  1891. if (frame.frame_header.restoration_filter.gab || frame.frame_header.restoration_filter.epf_iters != 0)
  1892. TODO();
  1893. TRY(apply_image_features(image, m_metadata, frame));
  1894. // FIXME: Do a proper color transformation with metadata.colour_encoding
  1895. if (m_metadata.xyb_encoded || frame.frame_header.do_YCbCr)
  1896. TODO();
  1897. TRY(render_extra_channels(image, m_metadata));
  1898. m_bitmap = TRY(image.to_bitmap(m_metadata));
  1899. return {};
  1900. }
  1901. ErrorOr<void> decode()
  1902. {
  1903. auto result = [this]() -> ErrorOr<void> {
  1904. // A.1 - Codestream structure
  1905. // The header is already decoded in JPEGXLImageDecoderPlugin::create()
  1906. if (m_metadata.colour_encoding.want_icc)
  1907. TODO();
  1908. if (m_metadata.preview.has_value())
  1909. TODO();
  1910. TRY(decode_frame());
  1911. return {};
  1912. }();
  1913. m_state = result.is_error() ? State::Error : State::FrameDecoded;
  1914. return result;
  1915. }
  1916. enum class State {
  1917. NotDecoded = 0,
  1918. Error,
  1919. HeaderDecoded,
  1920. FrameDecoded,
  1921. };
  1922. State state() const
  1923. {
  1924. return m_state;
  1925. }
  1926. IntSize size() const
  1927. {
  1928. return { m_header.width, m_header.height };
  1929. }
  1930. RefPtr<Bitmap> bitmap() const
  1931. {
  1932. return m_bitmap;
  1933. }
  1934. private:
  1935. State m_state { State::NotDecoded };
  1936. LittleEndianInputBitStream m_stream;
  1937. RefPtr<Gfx::Bitmap> m_bitmap;
  1938. Optional<EntropyDecoder> m_entropy_decoder {};
  1939. SizeHeader m_header;
  1940. ImageMetadata m_metadata;
  1941. FrameHeader m_frame_header;
  1942. TOC m_toc;
  1943. };
  1944. JPEGXLImageDecoderPlugin::JPEGXLImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  1945. {
  1946. m_context = make<JPEGXLLoadingContext>(move(stream));
  1947. }
  1948. JPEGXLImageDecoderPlugin::~JPEGXLImageDecoderPlugin() = default;
  1949. IntSize JPEGXLImageDecoderPlugin::size()
  1950. {
  1951. return m_context->size();
  1952. }
  1953. bool JPEGXLImageDecoderPlugin::sniff(ReadonlyBytes data)
  1954. {
  1955. return data.size() > 2
  1956. && data.data()[0] == 0xFF
  1957. && data.data()[1] == 0x0A;
  1958. }
  1959. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGXLImageDecoderPlugin::create(ReadonlyBytes data)
  1960. {
  1961. auto stream = TRY(try_make<FixedMemoryStream>(data));
  1962. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEGXLImageDecoderPlugin(move(stream))));
  1963. TRY(plugin->m_context->decode_image_header());
  1964. return plugin;
  1965. }
  1966. bool JPEGXLImageDecoderPlugin::is_animated()
  1967. {
  1968. return false;
  1969. }
  1970. size_t JPEGXLImageDecoderPlugin::loop_count()
  1971. {
  1972. return 0;
  1973. }
  1974. size_t JPEGXLImageDecoderPlugin::frame_count()
  1975. {
  1976. return 1;
  1977. }
  1978. size_t JPEGXLImageDecoderPlugin::first_animated_frame_index()
  1979. {
  1980. return 0;
  1981. }
  1982. ErrorOr<ImageFrameDescriptor> JPEGXLImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  1983. {
  1984. if (index > 0)
  1985. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid frame index");
  1986. if (m_context->state() == JPEGXLLoadingContext::State::Error)
  1987. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Decoding failed");
  1988. if (m_context->state() < JPEGXLLoadingContext::State::FrameDecoded)
  1989. TRY(m_context->decode());
  1990. return ImageFrameDescriptor { m_context->bitmap(), 0 };
  1991. }
  1992. ErrorOr<Optional<ReadonlyBytes>> JPEGXLImageDecoderPlugin::icc_data()
  1993. {
  1994. return OptionalNone {};
  1995. }
  1996. }