JPEGXLLoader.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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. };
  312. static ErrorOr<ToneMapping> read_tone_mapping(LittleEndianInputBitStream&)
  313. {
  314. TODO();
  315. }
  316. struct OpsinInverseMatrix {
  317. };
  318. static ErrorOr<OpsinInverseMatrix> read_opsin_inverse_matrix(LittleEndianInputBitStream&)
  319. {
  320. TODO();
  321. }
  322. struct ImageMetadata {
  323. u8 orientation { 1 };
  324. Optional<SizeHeader> intrinsic_size;
  325. Optional<PreviewHeader> preview;
  326. Optional<AnimationHeader> animation;
  327. BitDepth bit_depth;
  328. bool modular_16bit_buffers { true };
  329. u16 num_extra_channels {};
  330. Vector<ExtraChannelInfo, 4> ec_info;
  331. bool xyb_encoded { true };
  332. ColourEncoding colour_encoding;
  333. ToneMapping tone_mapping;
  334. Extensions extensions;
  335. bool default_m;
  336. OpsinInverseMatrix opsin_inverse_matrix;
  337. u8 cw_mask { 0 };
  338. Array<double, 15> up2_weight = s_d_up2;
  339. Array<double, 55> up4_weight = s_d_up4;
  340. Array<double, 210> up8_weight = s_d_up8;
  341. u16 number_of_color_channels() const
  342. {
  343. if (!xyb_encoded && colour_encoding.colour_space == ColourEncoding::ColourSpace::kGrey)
  344. return 1;
  345. return 3;
  346. }
  347. u16 number_of_channels() const
  348. {
  349. return number_of_color_channels() + num_extra_channels;
  350. }
  351. Optional<u16> alpha_channel() const
  352. {
  353. for (u16 i = 0; i < ec_info.size(); ++i) {
  354. if (ec_info[i].type == ExtraChannelInfo::ExtraChannelType::kAlpha)
  355. return i + number_of_color_channels();
  356. }
  357. return OptionalNone {};
  358. }
  359. };
  360. static ErrorOr<ImageMetadata> read_metadata_header(LittleEndianInputBitStream& stream)
  361. {
  362. ImageMetadata metadata;
  363. bool const all_default = TRY(stream.read_bit());
  364. if (!all_default) {
  365. bool const extra_fields = TRY(stream.read_bit());
  366. if (extra_fields) {
  367. metadata.orientation = 1 + TRY(stream.read_bits(3));
  368. bool const have_intr_size = TRY(stream.read_bit());
  369. if (have_intr_size)
  370. metadata.intrinsic_size = TRY(read_size_header(stream));
  371. bool const have_preview = TRY(stream.read_bit());
  372. if (have_preview)
  373. TODO();
  374. bool const have_animation = TRY(stream.read_bit());
  375. if (have_animation)
  376. TODO();
  377. }
  378. metadata.bit_depth = TRY(read_bit_depth(stream));
  379. metadata.modular_16bit_buffers = TRY(stream.read_bit());
  380. metadata.num_extra_channels = U32(0, 1, 2 + TRY(stream.read_bits(4)), 1 + TRY(stream.read_bits(12)));
  381. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  382. metadata.ec_info.append(TRY(read_extra_channel_info(stream)));
  383. metadata.xyb_encoded = TRY(stream.read_bit());
  384. metadata.colour_encoding = TRY(read_colour_encoding(stream));
  385. if (extra_fields)
  386. metadata.tone_mapping = TRY(read_tone_mapping(stream));
  387. metadata.extensions = TRY(read_extensions(stream));
  388. }
  389. metadata.default_m = TRY(stream.read_bit());
  390. if (!metadata.default_m && metadata.xyb_encoded)
  391. metadata.opsin_inverse_matrix = TRY(read_opsin_inverse_matrix(stream));
  392. if (!metadata.default_m)
  393. metadata.cw_mask = TRY(stream.read_bits(3));
  394. if (metadata.cw_mask != 0)
  395. TODO();
  396. return metadata;
  397. }
  398. ///
  399. /// Table F.7 — BlendingInfo bundle
  400. struct BlendingInfo {
  401. enum class BlendMode {
  402. kReplace = 0,
  403. kAdd = 1,
  404. kBlend = 2,
  405. kMulAdd = 3,
  406. kMul = 4,
  407. };
  408. BlendMode mode {};
  409. u8 alpha_channel {};
  410. bool clamp { false };
  411. u8 source {};
  412. };
  413. static ErrorOr<BlendingInfo> read_blending_info(LittleEndianInputBitStream& stream, ImageMetadata const& metadata, bool full_frame)
  414. {
  415. BlendingInfo blending_info;
  416. blending_info.mode = static_cast<BlendingInfo::BlendMode>(U32(0, 1, 2, 3 + TRY(stream.read_bits(2))));
  417. bool const extra = metadata.num_extra_channels > 0;
  418. if (extra) {
  419. auto const blend_or_mul_add = blending_info.mode == BlendingInfo::BlendMode::kBlend
  420. || blending_info.mode == BlendingInfo::BlendMode::kMulAdd;
  421. if (blend_or_mul_add)
  422. blending_info.alpha_channel = U32(0, 1, 2, 3 + TRY(stream.read_bits(3)));
  423. if (blend_or_mul_add || blending_info.mode == BlendingInfo::BlendMode::kMul)
  424. blending_info.clamp = TRY(stream.read_bit());
  425. }
  426. if (blending_info.mode != BlendingInfo::BlendMode::kReplace
  427. || !full_frame) {
  428. blending_info.source = TRY(stream.read_bits(2));
  429. }
  430. return blending_info;
  431. }
  432. ///
  433. /// J.1 - General
  434. struct RestorationFilter {
  435. bool gab { true };
  436. u8 epf_iters { 2 };
  437. Extensions extensions;
  438. };
  439. static ErrorOr<RestorationFilter> read_restoration_filter(LittleEndianInputBitStream& stream)
  440. {
  441. RestorationFilter restoration_filter;
  442. auto const all_defaults = TRY(stream.read_bit());
  443. if (!all_defaults) {
  444. restoration_filter.gab = TRY(stream.read_bit());
  445. if (restoration_filter.gab) {
  446. TODO();
  447. }
  448. restoration_filter.epf_iters = TRY(stream.read_bits(2));
  449. if (restoration_filter.epf_iters != 0) {
  450. TODO();
  451. }
  452. restoration_filter.extensions = TRY(read_extensions(stream));
  453. }
  454. return restoration_filter;
  455. }
  456. ///
  457. /// Table F.6 — Passes bundle
  458. struct Passes {
  459. u8 num_passes { 1 };
  460. };
  461. static ErrorOr<Passes> read_passes(LittleEndianInputBitStream& stream)
  462. {
  463. Passes passes;
  464. passes.num_passes = U32(1, 2, 3, 4 + TRY(stream.read_bits(3)));
  465. if (passes.num_passes != 1) {
  466. TODO();
  467. }
  468. return passes;
  469. }
  470. ///
  471. /// F.2 - FrameHeader
  472. struct FrameHeader {
  473. enum class FrameType {
  474. kRegularFrame = 0,
  475. kLFFrame = 1,
  476. kReferenceOnly = 2,
  477. kSkipProgressive = 3,
  478. };
  479. enum class Encoding {
  480. kVarDCT = 0,
  481. kModular = 1,
  482. };
  483. enum class Flags {
  484. None = 0,
  485. kNoise = 1,
  486. kPatches = 1 << 1,
  487. kSplines = 1 << 4,
  488. kUseLfFrame = 1 << 5,
  489. kSkipAdaptiveLFSmoothing = 1 << 7,
  490. };
  491. FrameType frame_type { FrameType::kRegularFrame };
  492. Encoding encoding { Encoding::kVarDCT };
  493. Flags flags { Flags::None };
  494. bool do_YCbCr { false };
  495. Array<u8, 3> jpeg_upsampling {};
  496. u8 upsampling {};
  497. FixedArray<u8> ec_upsampling {};
  498. u8 group_size_shift { 1 };
  499. Passes passes {};
  500. u8 lf_level {};
  501. bool have_crop { false };
  502. BlendingInfo blending_info {};
  503. FixedArray<BlendingInfo> ec_blending_info {};
  504. u32 duration {};
  505. bool is_last { true };
  506. u8 save_as_reference {};
  507. bool save_before_ct {};
  508. String name {};
  509. RestorationFilter restoration_filter {};
  510. Extensions extensions {};
  511. };
  512. static int operator&(FrameHeader::Flags first, FrameHeader::Flags second)
  513. {
  514. return static_cast<int>(first) & static_cast<int>(second);
  515. }
  516. static ErrorOr<FrameHeader> read_frame_header(LittleEndianInputBitStream& stream, ImageMetadata const& metadata)
  517. {
  518. FrameHeader frame_header;
  519. bool const all_default = TRY(stream.read_bit());
  520. if (!all_default) {
  521. frame_header.frame_type = static_cast<FrameHeader::FrameType>(TRY(stream.read_bits(2)));
  522. frame_header.encoding = static_cast<FrameHeader::Encoding>(TRY(stream.read_bits(1)));
  523. frame_header.flags = static_cast<FrameHeader::Flags>(TRY(U64(stream)));
  524. if (!metadata.xyb_encoded)
  525. frame_header.do_YCbCr = TRY(stream.read_bit());
  526. if (!(frame_header.flags & FrameHeader::Flags::kUseLfFrame)) {
  527. if (frame_header.do_YCbCr) {
  528. frame_header.jpeg_upsampling[0] = TRY(stream.read_bits(2));
  529. frame_header.jpeg_upsampling[1] = TRY(stream.read_bits(2));
  530. frame_header.jpeg_upsampling[2] = TRY(stream.read_bits(2));
  531. }
  532. frame_header.upsampling = U32(1, 2, 4, 8);
  533. frame_header.ec_upsampling = TRY(FixedArray<u8>::create(metadata.num_extra_channels));
  534. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  535. frame_header.ec_upsampling[i] = U32(1, 2, 4, 8);
  536. }
  537. if (frame_header.encoding == FrameHeader::Encoding::kModular)
  538. frame_header.group_size_shift = TRY(stream.read_bits(2));
  539. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  540. TODO();
  541. if (frame_header.frame_type != FrameHeader::FrameType::kReferenceOnly)
  542. frame_header.passes = TRY(read_passes(stream));
  543. if (frame_header.frame_type == FrameHeader::FrameType::kLFFrame)
  544. TODO();
  545. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame)
  546. frame_header.have_crop = TRY(stream.read_bit());
  547. if (frame_header.have_crop)
  548. TODO();
  549. bool const normal_frame = frame_header.frame_type == FrameHeader::FrameType::kRegularFrame
  550. || frame_header.frame_type == FrameHeader::FrameType::kSkipProgressive;
  551. // FIXME: also consider "cropped" image of the dimension of the frame
  552. VERIFY(!frame_header.have_crop);
  553. bool const full_frame = !frame_header.have_crop;
  554. if (normal_frame) {
  555. frame_header.blending_info = TRY(read_blending_info(stream, metadata, full_frame));
  556. frame_header.ec_blending_info = TRY(FixedArray<BlendingInfo>::create(metadata.num_extra_channels));
  557. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  558. frame_header.ec_blending_info[i] = TRY(read_blending_info(stream, metadata, full_frame));
  559. if (metadata.animation.has_value())
  560. TODO();
  561. frame_header.is_last = TRY(stream.read_bit());
  562. }
  563. // FIXME: Ensure that is_last has the correct default value
  564. VERIFY(normal_frame);
  565. auto const resets_canvas = full_frame && frame_header.blending_info.mode == BlendingInfo::BlendMode::kReplace;
  566. 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;
  567. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame) {
  568. if (!frame_header.is_last)
  569. TODO();
  570. }
  571. frame_header.save_before_ct = !normal_frame;
  572. if (frame_header.frame_type == FrameHeader::FrameType::kReferenceOnly || (resets_canvas && can_reference))
  573. frame_header.save_before_ct = TRY(stream.read_bit());
  574. frame_header.name = TRY(read_string(stream));
  575. frame_header.restoration_filter = TRY(read_restoration_filter(stream));
  576. frame_header.extensions = TRY(read_extensions(stream));
  577. }
  578. return frame_header;
  579. }
  580. ///
  581. /// F.3 TOC
  582. struct TOC {
  583. FixedArray<u32> entries;
  584. FixedArray<u32> group_offsets;
  585. };
  586. static u64 num_toc_entries(FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  587. {
  588. // F.3.1 - General
  589. if (num_groups == 1 && frame_header.passes.num_passes == 1)
  590. return 1;
  591. return 1 + num_lf_groups + 1 + num_groups * frame_header.passes.num_passes;
  592. }
  593. static ErrorOr<TOC> read_toc(LittleEndianInputBitStream& stream, FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  594. {
  595. TOC toc;
  596. bool const permuted_toc = TRY(stream.read_bit());
  597. if (permuted_toc) {
  598. // Read permutations
  599. TODO();
  600. }
  601. // F.3.3 - Decoding TOC
  602. stream.align_to_byte_boundary();
  603. auto const toc_entries = num_toc_entries(frame_header, num_groups, num_lf_groups);
  604. toc.entries = TRY(FixedArray<u32>::create(toc_entries));
  605. toc.group_offsets = TRY(FixedArray<u32>::create(toc_entries));
  606. for (u32 i {}; i < toc_entries; ++i) {
  607. auto const new_entry = U32(
  608. TRY(stream.read_bits(10)),
  609. 1024 + TRY(stream.read_bits(14)),
  610. 17408 + TRY(stream.read_bits(22)),
  611. 4211712 + TRY(stream.read_bits(30)));
  612. toc.entries[i] = new_entry;
  613. toc.group_offsets[i] = (i == 0 ? 0 : toc.group_offsets[i - 1]) + new_entry;
  614. }
  615. if (permuted_toc)
  616. TODO();
  617. stream.align_to_byte_boundary();
  618. return toc;
  619. }
  620. ///
  621. /// G.1.2 - LF channel dequantization weights
  622. struct LfChannelDequantization {
  623. float m_x_lf_unscaled { 4096 };
  624. float m_y_lf_unscaled { 512 };
  625. float m_b_lf_unscaled { 256 };
  626. };
  627. static ErrorOr<LfChannelDequantization> read_lf_channel_dequantization(LittleEndianInputBitStream& stream)
  628. {
  629. LfChannelDequantization lf_channel_dequantization;
  630. auto const all_default = TRY(stream.read_bit());
  631. if (!all_default) {
  632. TODO();
  633. }
  634. return lf_channel_dequantization;
  635. }
  636. ///
  637. /// C - Entropy decoding
  638. class EntropyDecoder {
  639. using BrotliCanonicalCode = Compress::Brotli::CanonicalCode;
  640. public:
  641. static ErrorOr<EntropyDecoder> create(LittleEndianInputBitStream& stream, u8 initial_num_distrib)
  642. {
  643. EntropyDecoder entropy_decoder;
  644. // C.2 - Distribution decoding
  645. entropy_decoder.m_lz77_enabled = TRY(stream.read_bit());
  646. if (entropy_decoder.m_lz77_enabled) {
  647. TODO();
  648. }
  649. TRY(entropy_decoder.read_pre_clustered_distributions(stream, initial_num_distrib));
  650. bool const use_prefix_code = TRY(stream.read_bit());
  651. if (!use_prefix_code)
  652. entropy_decoder.m_log_alphabet_size = 5 + TRY(stream.read_bits(2));
  653. for (auto& config : entropy_decoder.m_configs)
  654. config = TRY(entropy_decoder.read_config(stream));
  655. Vector<u16> counts;
  656. TRY(counts.try_resize(entropy_decoder.m_configs.size()));
  657. TRY(entropy_decoder.m_distributions.try_resize(entropy_decoder.m_configs.size()));
  658. if (use_prefix_code) {
  659. for (auto& count : counts) {
  660. if (TRY(stream.read_bit())) {
  661. auto const n = TRY(stream.read_bits(4));
  662. count = 1 + (1 << n) + TRY(stream.read_bits(n));
  663. } else {
  664. count = 1;
  665. }
  666. }
  667. // After reading the counts, the decoder reads each D[i] (implicitly
  668. // described by a prefix code) as specified in C.2.4, with alphabet_size = count[i].
  669. for (u32 i {}; i < entropy_decoder.m_distributions.size(); ++i) {
  670. // The alphabet size mentioned in the [Brotli] RFC is explicitly specified as parameter alphabet_size
  671. // when the histogram is being decoded, except in the special case of alphabet_size == 1, where no
  672. // histogram is read, and all decoded symbols are zero without reading any bits at all.
  673. if (counts[i] != 1) {
  674. entropy_decoder.m_distributions[i] = TRY(BrotliCanonicalCode::read_prefix_code(stream, counts[i]));
  675. } else {
  676. entropy_decoder.m_distributions[i] = BrotliCanonicalCode { { 1 }, { 0 } };
  677. }
  678. }
  679. } else {
  680. TODO();
  681. }
  682. return entropy_decoder;
  683. }
  684. ErrorOr<u32> decode_hybrid_uint(LittleEndianInputBitStream& stream, u16 context)
  685. {
  686. // C.3.3 - Hybrid integer decoding
  687. if (m_lz77_enabled)
  688. TODO();
  689. // Read symbol from entropy coded stream using D[clusters[ctx]]
  690. auto const token = TRY(m_distributions[m_clusters[context]].read_symbol(stream));
  691. auto r = TRY(read_uint(stream, m_configs[m_clusters[context]], token));
  692. return r;
  693. }
  694. private:
  695. struct HybridUint {
  696. u32 split_exponent {};
  697. u32 split {};
  698. u32 msb_in_token {};
  699. u32 lsb_in_token {};
  700. };
  701. static ErrorOr<u32> read_uint(LittleEndianInputBitStream& stream, HybridUint const& config, u32 token)
  702. {
  703. if (token < config.split)
  704. return token;
  705. auto const n = config.split_exponent
  706. - config.msb_in_token
  707. - config.lsb_in_token
  708. + ((token - config.split) >> (config.msb_in_token + config.lsb_in_token));
  709. VERIFY(n < 32);
  710. u32 const low_bits = token & ((1 << config.lsb_in_token) - 1);
  711. token = token >> config.lsb_in_token;
  712. token &= (1 << config.msb_in_token) - 1;
  713. token |= (1 << config.msb_in_token);
  714. auto const result = ((token << n | TRY(stream.read_bits(n))) << config.lsb_in_token) | low_bits;
  715. VERIFY(result < (1ul << 32));
  716. return result;
  717. }
  718. ErrorOr<void> read_pre_clustered_distributions(LittleEndianInputBitStream& stream, u8 num_distrib)
  719. {
  720. // C.2.2 Distribution clustering
  721. if (num_distrib == 1)
  722. TODO();
  723. TRY(m_clusters.try_resize(num_distrib));
  724. bool const is_simple = TRY(stream.read_bit());
  725. u16 num_clusters = 0;
  726. if (is_simple) {
  727. u8 const nbits = TRY(stream.read_bits(2));
  728. for (u8 i {}; i < num_distrib; ++i) {
  729. m_clusters[i] = TRY(stream.read_bits(nbits));
  730. if (m_clusters[i] >= num_clusters)
  731. num_clusters = m_clusters[i] + 1;
  732. }
  733. } else {
  734. TODO();
  735. }
  736. TRY(m_configs.try_resize(num_clusters));
  737. return {};
  738. }
  739. ErrorOr<HybridUint> read_config(LittleEndianInputBitStream& stream) const
  740. {
  741. // C.2.3 - Hybrid integer configuration
  742. HybridUint config {};
  743. config.split_exponent = TRY(stream.read_bits(ceil(log2(m_log_alphabet_size + 1))));
  744. if (config.split_exponent != m_log_alphabet_size) {
  745. auto nbits = ceil(log2(config.split_exponent + 1));
  746. config.msb_in_token = TRY(stream.read_bits(nbits));
  747. nbits = ceil(log2(config.split_exponent - config.msb_in_token + 1));
  748. config.lsb_in_token = TRY(stream.read_bits(nbits));
  749. } else {
  750. config.msb_in_token = 0;
  751. config.lsb_in_token = 0;
  752. }
  753. config.split = 1 << config.split_exponent;
  754. return config;
  755. }
  756. bool m_lz77_enabled {};
  757. Vector<u32> m_clusters;
  758. Vector<HybridUint> m_configs;
  759. u8 m_log_alphabet_size { 15 };
  760. Vector<BrotliCanonicalCode> m_distributions; // D in the spec
  761. };
  762. ///
  763. /// H.4.2 - MA tree decoding
  764. class MATree {
  765. public:
  766. struct LeafNode {
  767. u8 ctx {};
  768. u8 predictor {};
  769. i32 offset {};
  770. u32 multiplier {};
  771. };
  772. static ErrorOr<MATree> decode(LittleEndianInputBitStream& stream, Optional<EntropyDecoder>& decoder)
  773. {
  774. // G.1.3 - GlobalModular
  775. MATree tree;
  776. // 1 / 2 Read the 6 pre-clustered distributions
  777. auto const num_distrib = 6;
  778. if (!decoder.has_value())
  779. decoder = TRY(EntropyDecoder::create(stream, num_distrib));
  780. // 2 / 2 Decode the tree
  781. u64 ctx_id = 0;
  782. u64 nodes_left = 1;
  783. tree.m_tree.clear();
  784. while (nodes_left > 0) {
  785. nodes_left--;
  786. i32 const property = TRY(decoder->decode_hybrid_uint(stream, 1)) - 1;
  787. if (property >= 0) {
  788. DecisionNode decision_node;
  789. decision_node.property = property;
  790. decision_node.value = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 0)));
  791. decision_node.left_child = tree.m_tree.size() + nodes_left + 1;
  792. decision_node.right_child = tree.m_tree.size() + nodes_left + 2;
  793. tree.m_tree.empend(decision_node);
  794. nodes_left += 2;
  795. } else {
  796. LeafNode leaf_node;
  797. leaf_node.ctx = ctx_id++;
  798. leaf_node.predictor = TRY(decoder->decode_hybrid_uint(stream, 2));
  799. leaf_node.offset = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 3)));
  800. auto const mul_log = TRY(decoder->decode_hybrid_uint(stream, 4));
  801. auto const mul_bits = TRY(decoder->decode_hybrid_uint(stream, 5));
  802. leaf_node.multiplier = (mul_bits + 1) << mul_log;
  803. tree.m_tree.empend(leaf_node);
  804. }
  805. }
  806. // Finally, the decoder reads (tree.size() + 1) / 2 pre-clustered distributions D as specified in C.1.
  807. auto const num_pre_clustered_distributions = (tree.m_tree.size() + 1) / 2;
  808. decoder = TRY(decoder->create(stream, num_pre_clustered_distributions));
  809. return tree;
  810. }
  811. LeafNode get_leaf(Vector<i32> const& properties) const
  812. {
  813. // To find the MA leaf node, the MA tree is traversed, starting at the root node tree[0]
  814. // and for each decision node d, testing if property[d.property] > d.value, proceeding to
  815. // the node tree[d.left_child] if the test evaluates to true and to the node tree[d.right_child]
  816. // otherwise, until a leaf node is reached.
  817. DecisionNode node { m_tree[0].get<DecisionNode>() };
  818. while (true) {
  819. auto const next_node = [this, &properties, &node]() {
  820. // Note: The behavior when trying to access a non-existing property is taken from jxl-oxide
  821. if (node.property < properties.size() && properties[node.property] > node.value)
  822. return m_tree[node.left_child];
  823. return m_tree[node.right_child];
  824. }();
  825. if (next_node.has<LeafNode>())
  826. return next_node.get<LeafNode>();
  827. node = next_node.get<DecisionNode>();
  828. }
  829. }
  830. private:
  831. struct DecisionNode {
  832. u64 property {};
  833. i64 value {};
  834. u64 left_child {};
  835. u64 right_child {};
  836. };
  837. Vector<Variant<DecisionNode, LeafNode>> m_tree;
  838. };
  839. ///
  840. /// H.5 - Self-correcting predictor
  841. struct WPHeader {
  842. u8 wp_p1 { 16 };
  843. u8 wp_p2 { 10 };
  844. u8 wp_p3a { 7 };
  845. u8 wp_p3b { 7 };
  846. u8 wp_p3c { 7 };
  847. u8 wp_p3d { 0 };
  848. u8 wp_p3e { 0 };
  849. u8 wp_w0 { 13 };
  850. u8 wp_w1 { 12 };
  851. u8 wp_w2 { 12 };
  852. u8 wp_w3 { 12 };
  853. };
  854. static ErrorOr<WPHeader> read_self_correcting_predictor(LittleEndianInputBitStream& stream)
  855. {
  856. WPHeader self_correcting_predictor {};
  857. bool const default_wp = TRY(stream.read_bit());
  858. if (!default_wp) {
  859. TODO();
  860. }
  861. return self_correcting_predictor;
  862. }
  863. ///
  864. ///
  865. struct TransformInfo {
  866. enum class TransformId {
  867. kRCT = 0,
  868. kPalette = 1,
  869. kSqueeze = 2,
  870. };
  871. TransformId tr {};
  872. u32 begin_c {};
  873. u32 rct_type {};
  874. };
  875. static ErrorOr<TransformInfo> read_transform_info(LittleEndianInputBitStream& stream)
  876. {
  877. TransformInfo transform_info;
  878. transform_info.tr = static_cast<TransformInfo::TransformId>(TRY(stream.read_bits(2)));
  879. if (transform_info.tr != TransformInfo::TransformId::kSqueeze) {
  880. transform_info.begin_c = U32(
  881. TRY(stream.read_bits(3)),
  882. 8 + TRY(stream.read_bits(3)),
  883. 72 + TRY(stream.read_bits(10)),
  884. 1096 + TRY(stream.read_bits(13)));
  885. }
  886. if (transform_info.tr == TransformInfo::TransformId::kRCT) {
  887. transform_info.rct_type = U32(
  888. 6,
  889. TRY(stream.read_bits(2)),
  890. 2 + TRY(stream.read_bits(4)),
  891. 10 + TRY(stream.read_bits(6)));
  892. }
  893. if (transform_info.tr != TransformInfo::TransformId::kRCT)
  894. TODO();
  895. return transform_info;
  896. }
  897. ///
  898. /// Local abstractions to store the decoded image
  899. class Channel {
  900. public:
  901. static ErrorOr<Channel> create(u32 width, u32 height)
  902. {
  903. Channel channel;
  904. channel.m_width = width;
  905. channel.m_height = height;
  906. TRY(channel.m_pixels.try_resize(channel.m_width * channel.m_height));
  907. return channel;
  908. }
  909. i32 get(u32 x, u32 y) const
  910. {
  911. return m_pixels[x * m_width + y];
  912. }
  913. void set(u32 x, u32 y, i32 value)
  914. {
  915. m_pixels[x * m_width + y] = value;
  916. }
  917. u32 width() const
  918. {
  919. return m_width;
  920. }
  921. u32 height() const
  922. {
  923. return m_height;
  924. }
  925. u32 hshift() const
  926. {
  927. return m_hshift;
  928. }
  929. u32 vshift() const
  930. {
  931. return m_vshift;
  932. }
  933. bool decoded() const
  934. {
  935. return m_decoded;
  936. }
  937. void set_decoded(bool decoded)
  938. {
  939. m_decoded = decoded;
  940. }
  941. private:
  942. u32 m_width {};
  943. u32 m_height {};
  944. u32 m_hshift {};
  945. u32 m_vshift {};
  946. bool m_decoded { false };
  947. Vector<i32> m_pixels {};
  948. };
  949. class Image {
  950. public:
  951. static ErrorOr<Image> create(IntSize size, ImageMetadata const& metadata)
  952. {
  953. Image image {};
  954. for (u16 i = 0; i < metadata.number_of_channels(); ++i) {
  955. if (i < metadata.number_of_color_channels()) {
  956. TRY(image.m_channels.try_append(TRY(Channel::create(size.width(), size.height()))));
  957. } else {
  958. auto const dim_shift = metadata.ec_info[i - metadata.number_of_color_channels()].dim_shift;
  959. TRY(image.m_channels.try_append(TRY(Channel::create(size.width() >> dim_shift, size.height() >> dim_shift))));
  960. }
  961. }
  962. return image;
  963. }
  964. ErrorOr<NonnullRefPtr<Bitmap>> to_bitmap(ImageMetadata& metadata) const
  965. {
  966. // FIXME: which channel size should we use?
  967. auto const width = m_channels[0].width();
  968. auto const height = m_channels[0].height();
  969. auto bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { width, height }));
  970. auto const alpha_channel = metadata.alpha_channel();
  971. auto const bits_per_sample = metadata.bit_depth.bits_per_sample;
  972. VERIFY(bits_per_sample >= 8);
  973. for (u32 y {}; y < height; ++y) {
  974. for (u32 x {}; x < width; ++x) {
  975. auto const to_u8 = [&, bits_per_sample](i32 sample) -> u8 {
  976. // FIXME: Don't truncate the result to 8 bits
  977. static constexpr auto maximum_supported_bit_depth = 8;
  978. if (bits_per_sample > maximum_supported_bit_depth)
  979. sample >>= (bits_per_sample - maximum_supported_bit_depth);
  980. return clamp(sample + .5, 0, (1 << maximum_supported_bit_depth) - 1);
  981. };
  982. auto const color = [&]() -> Color {
  983. if (!alpha_channel.has_value()) {
  984. return { to_u8(m_channels[0].get(x, y)),
  985. to_u8(m_channels[1].get(x, y)),
  986. to_u8(m_channels[2].get(x, y)) };
  987. }
  988. return {
  989. to_u8(m_channels[0].get(x, y)),
  990. to_u8(m_channels[1].get(x, y)),
  991. to_u8(m_channels[2].get(x, y)),
  992. to_u8(m_channels[*alpha_channel].get(x, y)),
  993. };
  994. }();
  995. bitmap->set_pixel(x, y, color);
  996. }
  997. }
  998. return bitmap;
  999. }
  1000. Vector<Channel>& channels()
  1001. {
  1002. return m_channels;
  1003. }
  1004. private:
  1005. Vector<Channel> m_channels;
  1006. };
  1007. ///
  1008. /// H.2 - Image decoding
  1009. struct ModularHeader {
  1010. bool use_global_tree {};
  1011. WPHeader wp_params {};
  1012. Vector<TransformInfo> transform {};
  1013. };
  1014. static ErrorOr<Vector<i32>> get_properties(Vector<Channel> const& channels, u16 i, u32 x, u32 y)
  1015. {
  1016. Vector<i32> properties;
  1017. // Table H.4 - Property definitions
  1018. TRY(properties.try_append(i));
  1019. // FIXME: Handle other cases than GlobalModular
  1020. TRY(properties.try_append(0));
  1021. TRY(properties.try_append(y));
  1022. TRY(properties.try_append(x));
  1023. i32 const W = x > 0 ? channels[i].get(x - 1, y) : (y > 0 ? channels[i].get(x, y - 1) : 0);
  1024. i32 const N = y > 0 ? channels[i].get(x, y - 1) : W;
  1025. i32 const NW = x > 0 && y > 0 ? channels[i].get(x - 1, y - 1) : W;
  1026. i32 const NE = x + 1 < channels[i].width() && y > 0 ? channels[i].get(x + 1, y - 1) : N;
  1027. i32 const NN = y > 1 ? channels[i].get(x, y - 2) : N;
  1028. i32 const WW = x > 1 ? channels[i].get(x - 2, y) : W;
  1029. TRY(properties.try_append(abs(N)));
  1030. TRY(properties.try_append(abs(W)));
  1031. TRY(properties.try_append(N));
  1032. TRY(properties.try_append(W));
  1033. // x > 0 ? W - /* (the value of property 9 at position (x - 1, y)) */ : W
  1034. i32 x_1 = x - 1;
  1035. i32 const W_x_1 = x_1 > 0 ? channels[i].get(x_1 - 1, y) : (x_1 >= 0 && y > 0 ? channels[i].get(x_1, y - 1) : 0);
  1036. i32 const N_x_1 = x_1 >= 0 && y > 0 ? channels[i].get(x_1, y - 1) : W_x_1;
  1037. i32 const NW_x_1 = x_1 > 0 && y > 0 ? channels[i].get(x_1 - 1, y - 1) : W_x_1;
  1038. TRY(properties.try_append(W_x_1 + N_x_1 - NW_x_1));
  1039. TRY(properties.try_append(W + N - NW));
  1040. TRY(properties.try_append(W - NW));
  1041. TRY(properties.try_append(NW - N));
  1042. TRY(properties.try_append(N - NE));
  1043. TRY(properties.try_append(N - NN));
  1044. TRY(properties.try_append(W - WW));
  1045. // FIXME: Correctly compute max_error
  1046. TRY(properties.try_append(0));
  1047. for (i16 j = i - 1; j >= 0; j--) {
  1048. if (channels[j].width() != channels[i].width())
  1049. continue;
  1050. if (channels[j].height() != channels[i].height())
  1051. continue;
  1052. if (channels[j].hshift() != channels[i].hshift())
  1053. continue;
  1054. if (channels[j].vshift() != channels[i].vshift())
  1055. continue;
  1056. auto rC = channels[j].get(x, y);
  1057. auto rW = (x > 0 ? channels[j].get(x - 1, y) : 0);
  1058. auto rN = (y > 0 ? channels[j].get(x, y - 1) : rW);
  1059. auto rNW = (x > 0 && y > 0 ? channels[j].get(x - 1, y - 1) : rW);
  1060. auto rG = clamp(rW + rN - rNW, min(rW, rN), max(rW, rN));
  1061. TRY(properties.try_append(abs(rC)));
  1062. TRY(properties.try_append(rC));
  1063. TRY(properties.try_append(abs(rC - rG)));
  1064. TRY(properties.try_append(rC - rG));
  1065. }
  1066. return properties;
  1067. }
  1068. static i32 prediction(Channel const& channel, u32 x, u32 y, u32 predictor)
  1069. {
  1070. i32 const W = x > 0 ? channel.get(x - 1, y) : (y > 0 ? channel.get(x, y - 1) : 0);
  1071. i32 const N = y > 0 ? channel.get(x, y - 1) : W;
  1072. i32 const NW = x > 0 && y > 0 ? channel.get(x - 1, y - 1) : W;
  1073. i32 const NE = x + 1 < channel.width() && y > 0 ? channel.get(x + 1, y - 1) : N;
  1074. i32 const NN = y > 1 ? channel.get(x, y - 2) : N;
  1075. i32 const NEE = x + 2 < channel.width() and y > 0 ? channel.get(x + 2, y - 1) : NE;
  1076. i32 const WW = x > 1 ? channel.get(x - 2, y) : W;
  1077. switch (predictor) {
  1078. case 0:
  1079. return 0;
  1080. case 1:
  1081. return W;
  1082. case 2:
  1083. return N;
  1084. case 3:
  1085. return (W + N) / 2;
  1086. case 4:
  1087. return abs(N - NW) < abs(W - NW) ? W : N;
  1088. case 5:
  1089. return clamp(W + N - NW, min(W, N), max(W, N));
  1090. case 6:
  1091. TODO();
  1092. return (0 + 3) >> 3;
  1093. case 7:
  1094. return NE;
  1095. case 8:
  1096. return NW;
  1097. case 9:
  1098. return WW;
  1099. case 10:
  1100. return (W + NW) / 2;
  1101. case 11:
  1102. return (N + NW) / 2;
  1103. case 12:
  1104. return (N + NE) / 2;
  1105. case 13:
  1106. return (6 * N - 2 * NN + 7 * W + WW + NEE + 3 * NE + 8) / 16;
  1107. }
  1108. VERIFY_NOT_REACHED();
  1109. }
  1110. static ErrorOr<ModularHeader> read_modular_header(LittleEndianInputBitStream& stream,
  1111. Image& image,
  1112. Optional<EntropyDecoder>& decoder,
  1113. MATree const& global_tree,
  1114. u16 num_channels)
  1115. {
  1116. ModularHeader modular_header;
  1117. modular_header.use_global_tree = TRY(stream.read_bit());
  1118. modular_header.wp_params = TRY(read_self_correcting_predictor(stream));
  1119. auto const nb_transforms = U32(0, 1, 2 + TRY(stream.read_bits(4)), 18 + TRY(stream.read_bits(8)));
  1120. TRY(modular_header.transform.try_resize(nb_transforms));
  1121. for (u32 i {}; i < nb_transforms; ++i)
  1122. modular_header.transform[i] = TRY(read_transform_info(stream));
  1123. Optional<MATree> local_tree;
  1124. if (!modular_header.use_global_tree)
  1125. TODO();
  1126. // The decoder then starts an entropy-coded stream (C.1) and decodes the data for each channel
  1127. // (in ascending order of index) as specified in H.3, skipping any channels having width or height
  1128. // zero. Finally, the inverse transformations are applied (from last to first) as described in H.6.
  1129. auto const& tree = local_tree.has_value() ? *local_tree : global_tree;
  1130. for (u16 i {}; i < num_channels; ++i) {
  1131. for (u32 y {}; y < image.channels()[i].height(); y++) {
  1132. for (u32 x {}; x < image.channels()[i].width(); x++) {
  1133. auto const properties = TRY(get_properties(image.channels(), i, x, y));
  1134. auto const leaf_node = tree.get_leaf(properties);
  1135. auto diff = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, leaf_node.ctx)));
  1136. diff = (diff * leaf_node.multiplier) + leaf_node.offset;
  1137. auto const total = diff + prediction(image.channels()[i], x, y, leaf_node.predictor);
  1138. image.channels()[i].set(x, y, total);
  1139. }
  1140. }
  1141. image.channels()[i].set_decoded(true);
  1142. }
  1143. return modular_header;
  1144. }
  1145. ///
  1146. /// G.1.2 - LF channel dequantization weights
  1147. struct GlobalModular {
  1148. MATree ma_tree;
  1149. ModularHeader modular_header;
  1150. };
  1151. static ErrorOr<GlobalModular> read_global_modular(LittleEndianInputBitStream& stream,
  1152. Image& image,
  1153. FrameHeader const& frame_header,
  1154. ImageMetadata const& metadata,
  1155. Optional<EntropyDecoder>& entropy_decoder)
  1156. {
  1157. GlobalModular global_modular;
  1158. auto const decode_ma_tree = TRY(stream.read_bit());
  1159. if (decode_ma_tree)
  1160. global_modular.ma_tree = TRY(MATree::decode(stream, entropy_decoder));
  1161. // The decoder then decodes a modular sub-bitstream (Annex H), where
  1162. // the number of channels is computed as follows:
  1163. auto num_channels = metadata.num_extra_channels;
  1164. if (frame_header.encoding == FrameHeader::Encoding::kModular) {
  1165. if (!frame_header.do_YCbCr && !metadata.xyb_encoded
  1166. && metadata.colour_encoding.colour_space == ColourEncoding::ColourSpace::kGrey) {
  1167. num_channels += 1;
  1168. } else {
  1169. num_channels += 3;
  1170. }
  1171. }
  1172. // FIXME: Ensure this spec comment:
  1173. // However, the decoder only decodes the first nb_meta_channels channels and any further channels
  1174. // that have a width and height that are both at most group_dim. At that point, it stops decoding.
  1175. // No inverse transforms are applied yet.
  1176. global_modular.modular_header = TRY(read_modular_header(stream, image, entropy_decoder, global_modular.ma_tree, num_channels));
  1177. return global_modular;
  1178. }
  1179. ///
  1180. /// G.1 - LfGlobal
  1181. struct LfGlobal {
  1182. LfChannelDequantization lf_dequant;
  1183. GlobalModular gmodular;
  1184. };
  1185. static ErrorOr<LfGlobal> read_lf_global(LittleEndianInputBitStream& stream,
  1186. Image& image,
  1187. FrameHeader const& frame_header,
  1188. ImageMetadata const& metadata,
  1189. Optional<EntropyDecoder>& entropy_decoder)
  1190. {
  1191. LfGlobal lf_global;
  1192. if (frame_header.flags != FrameHeader::Flags::None)
  1193. TODO();
  1194. lf_global.lf_dequant = TRY(read_lf_channel_dequantization(stream));
  1195. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  1196. TODO();
  1197. lf_global.gmodular = TRY(read_global_modular(stream, image, frame_header, metadata, entropy_decoder));
  1198. return lf_global;
  1199. }
  1200. ///
  1201. /// G.2 - LfGroup
  1202. static ErrorOr<void> read_lf_group(LittleEndianInputBitStream&,
  1203. Image& image,
  1204. FrameHeader const& frame_header)
  1205. {
  1206. // LF coefficients
  1207. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1208. TODO();
  1209. }
  1210. // ModularLfGroup
  1211. for (auto const& channel : image.channels()) {
  1212. if (channel.decoded())
  1213. continue;
  1214. if (channel.hshift() < 3 || channel.vshift() < 3)
  1215. continue;
  1216. // This code actually only detect that we need to read a null image
  1217. // so a no-op. It should be fully rewritten when we add proper support
  1218. // for LfGroup.
  1219. TODO();
  1220. }
  1221. // HF metadata
  1222. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1223. TODO();
  1224. }
  1225. return {};
  1226. }
  1227. ///
  1228. /// H.6 - Transformations
  1229. static void apply_rct(Image& image, TransformInfo const& transformation)
  1230. {
  1231. auto& channels = image.channels();
  1232. for (u32 y {}; y < channels[transformation.begin_c].height(); y++) {
  1233. for (u32 x {}; x < channels[transformation.begin_c].width(); x++) {
  1234. auto a = channels[transformation.begin_c + 0].get(x, y);
  1235. auto b = channels[transformation.begin_c + 1].get(x, y);
  1236. auto c = channels[transformation.begin_c + 2].get(x, y);
  1237. i32 d {};
  1238. i32 e {};
  1239. i32 f {};
  1240. auto const permutation = transformation.rct_type / 7;
  1241. auto const type = transformation.rct_type % 7;
  1242. if (type == 6) { // YCgCo
  1243. auto const tmp = a - (c >> 1);
  1244. e = c + tmp;
  1245. f = tmp - (b >> 1);
  1246. d = f + b;
  1247. } else {
  1248. if (type & 1)
  1249. c = c + a;
  1250. if ((type >> 1) == 1)
  1251. b = b + a;
  1252. if ((type >> 1) == 2)
  1253. b = b + ((a + c) >> 1);
  1254. d = a;
  1255. e = b;
  1256. f = c;
  1257. }
  1258. Array<i32, 3> v {};
  1259. v[permutation % 3] = d;
  1260. v[(permutation + 1 + (permutation / 3)) % 3] = e;
  1261. v[(permutation + 2 - (permutation / 3)) % 3] = f;
  1262. channels[transformation.begin_c + 0].set(x, y, v[0]);
  1263. channels[transformation.begin_c + 1].set(x, y, v[1]);
  1264. channels[transformation.begin_c + 2].set(x, y, v[2]);
  1265. }
  1266. }
  1267. }
  1268. static void apply_transformation(Image& image, TransformInfo const& transformation)
  1269. {
  1270. switch (transformation.tr) {
  1271. case TransformInfo::TransformId::kRCT:
  1272. apply_rct(image, transformation);
  1273. break;
  1274. case TransformInfo::TransformId::kPalette:
  1275. case TransformInfo::TransformId::kSqueeze:
  1276. TODO();
  1277. default:
  1278. VERIFY_NOT_REACHED();
  1279. }
  1280. }
  1281. ///
  1282. /// G.3.2 - PassGroup
  1283. static ErrorOr<void> read_pass_group(LittleEndianInputBitStream& stream,
  1284. Image& image,
  1285. FrameHeader const& frame_header,
  1286. u32 group_dim)
  1287. {
  1288. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1289. (void)stream;
  1290. TODO();
  1291. }
  1292. auto& channels = image.channels();
  1293. for (u16 i {}; i < channels.size(); ++i) {
  1294. // Skip meta-channels
  1295. // FIXME: Also test if the channel has already been decoded
  1296. // See: nb_meta_channels in the spec
  1297. bool const is_meta_channel = channels[i].width() <= group_dim
  1298. || channels[i].height() <= group_dim
  1299. || channels[i].hshift() >= 3
  1300. || channels[i].vshift() >= 3;
  1301. if (!is_meta_channel)
  1302. TODO();
  1303. }
  1304. return {};
  1305. }
  1306. ///
  1307. /// Table F.1 — Frame bundle
  1308. struct Frame {
  1309. FrameHeader frame_header;
  1310. TOC toc;
  1311. LfGlobal lf_global;
  1312. u64 width {};
  1313. u64 height {};
  1314. u64 num_groups {};
  1315. u64 num_lf_groups {};
  1316. };
  1317. static ErrorOr<Frame> read_frame(LittleEndianInputBitStream& stream,
  1318. Image& image,
  1319. SizeHeader const& size_header,
  1320. ImageMetadata const& metadata,
  1321. Optional<EntropyDecoder>& entropy_decoder)
  1322. {
  1323. // F.1 - General
  1324. // Each Frame is byte-aligned by invoking ZeroPadToByte() (B.2.7)
  1325. stream.align_to_byte_boundary();
  1326. Frame frame;
  1327. frame.frame_header = TRY(read_frame_header(stream, metadata));
  1328. if (!frame.frame_header.have_crop) {
  1329. frame.width = size_header.width;
  1330. frame.height = size_header.height;
  1331. } else {
  1332. TODO();
  1333. }
  1334. if (frame.frame_header.upsampling > 1) {
  1335. frame.width = ceil(static_cast<double>(frame.width) / frame.frame_header.upsampling);
  1336. frame.height = ceil(static_cast<double>(frame.height) / frame.frame_header.upsampling);
  1337. }
  1338. if (frame.frame_header.lf_level > 0)
  1339. TODO();
  1340. // F.2 - FrameHeader
  1341. auto const group_dim = 128 << frame.frame_header.group_size_shift;
  1342. auto const frame_width = static_cast<double>(frame.width);
  1343. auto const frame_height = static_cast<double>(frame.height);
  1344. frame.num_groups = ceil(frame_width / group_dim) * ceil(frame_height / group_dim);
  1345. frame.num_lf_groups = ceil(frame_width / (group_dim * 8)) * ceil(frame_height / (group_dim * 8));
  1346. frame.toc = TRY(read_toc(stream, frame.frame_header, frame.num_groups, frame.num_lf_groups));
  1347. image = TRY(Image::create({ frame.width, frame.height }, metadata));
  1348. frame.lf_global = TRY(read_lf_global(stream, image, frame.frame_header, metadata, entropy_decoder));
  1349. for (u32 i {}; i < frame.num_lf_groups; ++i)
  1350. TRY(read_lf_group(stream, image, frame.frame_header));
  1351. if (frame.frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1352. TODO();
  1353. }
  1354. auto const num_pass_group = frame.num_groups * frame.frame_header.passes.num_passes;
  1355. auto const& transform_infos = frame.lf_global.gmodular.modular_header.transform;
  1356. for (u64 i {}; i < num_pass_group; ++i)
  1357. TRY(read_pass_group(stream, image, frame.frame_header, group_dim));
  1358. // G.4.2 - Modular group data
  1359. // When all modular groups are decoded, the inverse transforms are applied to
  1360. // the at that point fully decoded GlobalModular image, as specified in H.6.
  1361. for (auto const& transformation : transform_infos.in_reverse())
  1362. apply_transformation(image, transformation);
  1363. return frame;
  1364. }
  1365. ///
  1366. /// 5.2 - Mirroring
  1367. static u32 mirror_1d(i32 coord, u32 size)
  1368. {
  1369. if (coord < 0)
  1370. return mirror_1d(-coord - 1, size);
  1371. else if (static_cast<u32>(coord) >= size)
  1372. return mirror_1d(2 * size - 1 - coord, size);
  1373. else
  1374. return coord;
  1375. }
  1376. ///
  1377. /// K - Image features
  1378. static ErrorOr<void> apply_upsampling(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1379. {
  1380. Optional<u32> ec_max;
  1381. for (auto upsampling : frame.frame_header.ec_upsampling) {
  1382. if (!ec_max.has_value() || upsampling > *ec_max)
  1383. ec_max = upsampling;
  1384. }
  1385. if (frame.frame_header.upsampling > 1 || ec_max.value_or(0) > 1) {
  1386. if (ec_max.value_or(0) > 2)
  1387. TODO();
  1388. auto const k = frame.frame_header.upsampling;
  1389. auto weight = [k, &metadata](u8 index) -> double {
  1390. if (k == 2)
  1391. return metadata.up2_weight[index];
  1392. if (k == 4)
  1393. return metadata.up4_weight[index];
  1394. return metadata.up8_weight[index];
  1395. };
  1396. // FIXME: Use ec_upsampling for extra-channels
  1397. for (auto& channel : image.channels()) {
  1398. auto upsampled = TRY(Channel::create(k * channel.width(), k * channel.height()));
  1399. // Loop over the original image
  1400. for (u32 y {}; y < channel.height(); y++) {
  1401. for (u32 x {}; x < channel.width(); x++) {
  1402. // Loop over the upsampling factor
  1403. for (u8 kx {}; kx < k; ++kx) {
  1404. for (u8 ky {}; ky < k; ++ky) {
  1405. double sum {};
  1406. // Loop over the W window
  1407. double W_min = NumericLimits<double>::max();
  1408. double W_max = -NumericLimits<double>::max();
  1409. for (u8 ix {}; ix < 5; ++ix) {
  1410. for (u8 iy {}; iy < 5; ++iy) {
  1411. auto const j = (ky < k / 2) ? (iy + 5 * ky) : ((4 - iy) + 5 * (k - 1 - ky));
  1412. auto const i = (kx < k / 2) ? (ix + 5 * kx) : ((4 - ix) + 5 * (k - 1 - kx));
  1413. auto const minimum = min(i, j);
  1414. auto const maximum = max(i, j);
  1415. auto const index = 5 * k * minimum / 2 - minimum * (minimum - 1) / 2 + maximum - minimum;
  1416. auto const origin_sample_x = mirror_1d(x + ix - 2, channel.width());
  1417. auto const origin_sample_y = mirror_1d(y + iy - 2, channel.height());
  1418. auto const origin_sample = channel.get(origin_sample_x, origin_sample_y);
  1419. W_min = min(W_min, origin_sample);
  1420. W_max = max(W_max, origin_sample);
  1421. sum += origin_sample * weight(index);
  1422. }
  1423. }
  1424. // The resulting sample is clamped to the range [a, b] where a and b are
  1425. // the minimum and maximum of the samples in W.
  1426. sum = clamp(sum, W_min, W_max);
  1427. upsampled.set(x * k + kx, y * k + ky, sum);
  1428. }
  1429. }
  1430. }
  1431. }
  1432. channel = move(upsampled);
  1433. }
  1434. }
  1435. return {};
  1436. }
  1437. static ErrorOr<void> apply_image_features(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1438. {
  1439. TRY(apply_upsampling(image, metadata, frame));
  1440. if (frame.frame_header.flags != FrameHeader::Flags::None)
  1441. TODO();
  1442. return {};
  1443. }
  1444. ///
  1445. /// L.4 - Extra channel rendering
  1446. static ErrorOr<void> render_extra_channels(Image&, ImageMetadata const& metadata)
  1447. {
  1448. for (u16 i = metadata.number_of_color_channels(); i < metadata.number_of_channels(); ++i) {
  1449. auto const ec_index = i - metadata.number_of_color_channels();
  1450. if (metadata.ec_info[ec_index].dim_shift != 0)
  1451. TODO();
  1452. }
  1453. return {};
  1454. }
  1455. ///
  1456. class JPEGXLLoadingContext {
  1457. public:
  1458. JPEGXLLoadingContext(NonnullOwnPtr<Stream> stream)
  1459. : m_stream(move(stream))
  1460. {
  1461. }
  1462. ErrorOr<void> decode_image_header()
  1463. {
  1464. constexpr auto JPEGXL_SIGNATURE = 0xFF0A;
  1465. auto const signature = TRY(m_stream.read_value<BigEndian<u16>>());
  1466. if (signature != JPEGXL_SIGNATURE)
  1467. return Error::from_string_literal("Unrecognized signature");
  1468. m_header = TRY(read_size_header(m_stream));
  1469. m_metadata = TRY(read_metadata_header(m_stream));
  1470. m_state = State::HeaderDecoded;
  1471. return {};
  1472. }
  1473. ErrorOr<void> decode_frame()
  1474. {
  1475. Image image {};
  1476. auto const frame = TRY(read_frame(m_stream, image, m_header, m_metadata, m_entropy_decoder));
  1477. if (frame.frame_header.restoration_filter.gab || frame.frame_header.restoration_filter.epf_iters != 0)
  1478. TODO();
  1479. TRY(apply_image_features(image, m_metadata, frame));
  1480. // FIXME: Do a proper color transformation with metadata.colour_encoding
  1481. if (m_metadata.xyb_encoded || frame.frame_header.do_YCbCr)
  1482. TODO();
  1483. TRY(render_extra_channels(image, m_metadata));
  1484. m_bitmap = TRY(image.to_bitmap(m_metadata));
  1485. return {};
  1486. }
  1487. ErrorOr<void> decode()
  1488. {
  1489. auto result = [this]() -> ErrorOr<void> {
  1490. // A.1 - Codestream structure
  1491. // The header is already decoded in JPEGXLImageDecoderPlugin::create()
  1492. if (m_metadata.colour_encoding.want_icc)
  1493. TODO();
  1494. if (m_metadata.preview.has_value())
  1495. TODO();
  1496. TRY(decode_frame());
  1497. return {};
  1498. }();
  1499. m_state = result.is_error() ? State::Error : State::FrameDecoded;
  1500. return result;
  1501. }
  1502. enum class State {
  1503. NotDecoded = 0,
  1504. Error,
  1505. HeaderDecoded,
  1506. FrameDecoded,
  1507. };
  1508. State state() const
  1509. {
  1510. return m_state;
  1511. }
  1512. IntSize size() const
  1513. {
  1514. return { m_header.width, m_header.height };
  1515. }
  1516. RefPtr<Bitmap> bitmap() const
  1517. {
  1518. return m_bitmap;
  1519. }
  1520. private:
  1521. State m_state { State::NotDecoded };
  1522. LittleEndianInputBitStream m_stream;
  1523. RefPtr<Gfx::Bitmap> m_bitmap;
  1524. Optional<EntropyDecoder> m_entropy_decoder {};
  1525. SizeHeader m_header;
  1526. ImageMetadata m_metadata;
  1527. FrameHeader m_frame_header;
  1528. TOC m_toc;
  1529. };
  1530. JPEGXLImageDecoderPlugin::JPEGXLImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  1531. {
  1532. m_context = make<JPEGXLLoadingContext>(move(stream));
  1533. }
  1534. JPEGXLImageDecoderPlugin::~JPEGXLImageDecoderPlugin() = default;
  1535. IntSize JPEGXLImageDecoderPlugin::size()
  1536. {
  1537. return m_context->size();
  1538. }
  1539. bool JPEGXLImageDecoderPlugin::sniff(ReadonlyBytes data)
  1540. {
  1541. return data.size() > 2
  1542. && data.data()[0] == 0xFF
  1543. && data.data()[1] == 0x0A;
  1544. }
  1545. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGXLImageDecoderPlugin::create(ReadonlyBytes data)
  1546. {
  1547. auto stream = TRY(try_make<FixedMemoryStream>(data));
  1548. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEGXLImageDecoderPlugin(move(stream))));
  1549. TRY(plugin->m_context->decode_image_header());
  1550. return plugin;
  1551. }
  1552. bool JPEGXLImageDecoderPlugin::is_animated()
  1553. {
  1554. return false;
  1555. }
  1556. size_t JPEGXLImageDecoderPlugin::loop_count()
  1557. {
  1558. return 0;
  1559. }
  1560. size_t JPEGXLImageDecoderPlugin::frame_count()
  1561. {
  1562. return 1;
  1563. }
  1564. size_t JPEGXLImageDecoderPlugin::first_animated_frame_index()
  1565. {
  1566. return 0;
  1567. }
  1568. ErrorOr<ImageFrameDescriptor> JPEGXLImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  1569. {
  1570. if (index > 0)
  1571. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid frame index");
  1572. if (m_context->state() == JPEGXLLoadingContext::State::Error)
  1573. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Decoding failed");
  1574. if (m_context->state() < JPEGXLLoadingContext::State::FrameDecoded)
  1575. TRY(m_context->decode());
  1576. return ImageFrameDescriptor { m_context->bitmap(), 0 };
  1577. }
  1578. ErrorOr<Optional<ReadonlyBytes>> JPEGXLImageDecoderPlugin::icc_data()
  1579. {
  1580. return OptionalNone {};
  1581. }
  1582. }