JPEGXLLoader.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. ///
  63. /// D.2 - Image dimensions
  64. struct SizeHeader {
  65. u32 height {};
  66. u32 width {};
  67. };
  68. static u32 aspect_ratio(u32 height, u32 ratio)
  69. {
  70. if (ratio == 1)
  71. return height;
  72. if (ratio == 2)
  73. return height * 12 / 10;
  74. if (ratio == 3)
  75. return height * 4 / 3;
  76. if (ratio == 4)
  77. return height * 3 / 2;
  78. if (ratio == 5)
  79. return height * 16 / 9;
  80. if (ratio == 6)
  81. return height * 5 / 4;
  82. if (ratio == 7)
  83. return height * 2 / 1;
  84. VERIFY_NOT_REACHED();
  85. }
  86. static ErrorOr<SizeHeader> read_size_header(LittleEndianInputBitStream& stream)
  87. {
  88. SizeHeader size {};
  89. auto const div8 = TRY(stream.read_bit());
  90. if (div8) {
  91. auto const h_div8 = 1 + TRY(stream.read_bits(5));
  92. size.height = 8 * h_div8;
  93. } else {
  94. size.height = U32(
  95. 1 + TRY(stream.read_bits(9)),
  96. 1 + TRY(stream.read_bits(13)),
  97. 1 + TRY(stream.read_bits(18)),
  98. 1 + TRY(stream.read_bits(30)));
  99. }
  100. auto const ratio = TRY(stream.read_bits(3));
  101. if (ratio == 0) {
  102. if (div8) {
  103. auto const w_div8 = 1 + TRY(stream.read_bits(5));
  104. size.width = 8 * w_div8;
  105. } else {
  106. size.width = U32(
  107. 1 + TRY(stream.read_bits(9)),
  108. 1 + TRY(stream.read_bits(13)),
  109. 1 + TRY(stream.read_bits(18)),
  110. 1 + TRY(stream.read_bits(30)));
  111. }
  112. } else {
  113. size.width = aspect_ratio(size.height, ratio);
  114. }
  115. return size;
  116. }
  117. ///
  118. /// D.3.5 - BitDepth
  119. struct BitDepth {
  120. u32 bits_per_sample { 8 };
  121. u8 exp_bits {};
  122. };
  123. static ErrorOr<BitDepth> read_bit_depth(LittleEndianInputBitStream& stream)
  124. {
  125. BitDepth bit_depth;
  126. bool const float_sample = TRY(stream.read_bit());
  127. if (float_sample) {
  128. bit_depth.bits_per_sample = U32(32, 16, 24, 1 + TRY(stream.read_bits(6)));
  129. bit_depth.exp_bits = 1 + TRY(stream.read_bits(4));
  130. } else {
  131. bit_depth.bits_per_sample = U32(8, 10, 12, 1 + TRY(stream.read_bits(6)));
  132. }
  133. return bit_depth;
  134. }
  135. ///
  136. /// E.2 - ColourEncoding
  137. struct ColourEncoding {
  138. enum class ColourSpace {
  139. kRGB = 0,
  140. kGrey = 1,
  141. kXYB = 2,
  142. kUnknown = 3,
  143. };
  144. enum class WhitePoint {
  145. kD65 = 1,
  146. kCustom = 2,
  147. kE = 10,
  148. kDCI = 11,
  149. };
  150. enum class Primaries {
  151. kSRGB = 1,
  152. kCustom = 2,
  153. k2100 = 3,
  154. kP3 = 11,
  155. };
  156. enum class RenderingIntent {
  157. kPerceptual = 0,
  158. kRelative = 1,
  159. kSaturation = 2,
  160. kAbsolute = 3,
  161. };
  162. struct Customxy {
  163. u32 ux {};
  164. u32 uy {};
  165. };
  166. bool want_icc = false;
  167. ColourSpace colour_space { ColourSpace::kRGB };
  168. WhitePoint white_point { WhitePoint::kD65 };
  169. Primaries primaries { Primaries::kSRGB };
  170. Customxy white {};
  171. Customxy red {};
  172. Customxy green {};
  173. Customxy blue {};
  174. RenderingIntent rendering_intent { RenderingIntent::kRelative };
  175. };
  176. [[maybe_unused]] static ErrorOr<ColourEncoding::Customxy> read_custom_xy(LittleEndianInputBitStream& stream)
  177. {
  178. ColourEncoding::Customxy custom_xy;
  179. auto const read_custom = [&stream]() -> ErrorOr<u32> {
  180. return U32(
  181. TRY(stream.read_bits(19)),
  182. 524288 + TRY(stream.read_bits(19)),
  183. 1048576 + TRY(stream.read_bits(20)),
  184. 2097152 + TRY(stream.read_bits(21)));
  185. };
  186. custom_xy.ux = TRY(read_custom());
  187. custom_xy.uy = TRY(read_custom());
  188. return custom_xy;
  189. }
  190. static ErrorOr<ColourEncoding> read_colour_encoding(LittleEndianInputBitStream& stream)
  191. {
  192. ColourEncoding colour_encoding;
  193. bool const all_default = TRY(stream.read_bit());
  194. if (!all_default) {
  195. TODO();
  196. }
  197. return colour_encoding;
  198. }
  199. ///
  200. /// B.3 - Extensions
  201. struct Extensions {
  202. u64 extensions {};
  203. };
  204. static ErrorOr<Extensions> read_extensions(LittleEndianInputBitStream& stream)
  205. {
  206. Extensions extensions;
  207. extensions.extensions = TRY(U64(stream));
  208. if (extensions.extensions != 0)
  209. TODO();
  210. return extensions;
  211. }
  212. ///
  213. /// K.2 - Non-separable upsampling
  214. Array s_d_up2 {
  215. -0.01716200, -0.03452303, -0.04022174, -0.02921014, -0.00624645,
  216. 0.14111091, 0.28896755, 0.00278718, -0.01610267, 0.56661550,
  217. 0.03777607, -0.01986694, -0.03144731, -0.01185068, -0.00213539
  218. };
  219. Array s_d_up4 = {
  220. -0.02419067, -0.03491987, -0.03693351, -0.03094285, -0.00529785,
  221. -0.01663432, -0.03556863, -0.03888905, -0.03516850, -0.00989469,
  222. 0.23651958, 0.33392945, -0.01073543, -0.01313181, -0.03556694,
  223. 0.13048175, 0.40103025, 0.03951150, -0.02077584, 0.46914198,
  224. -0.00209270, -0.01484589, -0.04064806, 0.18942530, 0.56279892,
  225. 0.06674400, -0.02335494, -0.03551682, -0.00754830, -0.02267919,
  226. -0.02363578, 0.00315804, -0.03399098, -0.01359519, -0.00091653,
  227. -0.00335467, -0.01163294, -0.01610294, -0.00974088, -0.00191622,
  228. -0.01095446, -0.03198464, -0.04455121, -0.02799790, -0.00645912,
  229. 0.06390599, 0.22963888, 0.00630981, -0.01897349, 0.67537268,
  230. 0.08483369, -0.02534994, -0.02205197, -0.01667999, -0.00384443
  231. };
  232. Array s_d_up8 {
  233. -0.02928613, -0.03706353, -0.03783812, -0.03324558, -0.00447632, -0.02519406, -0.03752601, -0.03901508, -0.03663285, -0.00646649,
  234. -0.02066407, -0.03838633, -0.04002101, -0.03900035, -0.00901973, -0.01626393, -0.03954148, -0.04046620, -0.03979621, -0.01224485,
  235. 0.29895328, 0.35757708, -0.02447552, -0.01081748, -0.04314594, 0.23903219, 0.41119301, -0.00573046, -0.01450239, -0.04246845,
  236. 0.17567618, 0.45220643, 0.02287757, -0.01936783, -0.03583255, 0.11572472, 0.47416733, 0.06284440, -0.02685066, 0.42720050,
  237. -0.02248939, -0.01155273, -0.04562755, 0.28689496, 0.49093869, -0.00007891, -0.01545926, -0.04562659, 0.21238920, 0.53980934,
  238. 0.03369474, -0.02070211, -0.03866988, 0.14229550, 0.56593398, 0.08045181, -0.02888298, -0.03680918, -0.00542229, -0.02920477,
  239. -0.02788574, -0.02118180, -0.03942402, -0.00775547, -0.02433614, -0.03193943, -0.02030828, -0.04044014, -0.01074016, -0.01930822,
  240. -0.03620399, -0.01974125, -0.03919545, -0.01456093, -0.00045072, -0.00360110, -0.01020207, -0.01231907, -0.00638988, -0.00071592,
  241. -0.00279122, -0.00957115, -0.01288327, -0.00730937, -0.00107783, -0.00210156, -0.00890705, -0.01317668, -0.00813895, -0.00153491,
  242. -0.02128481, -0.04173044, -0.04831487, -0.03293190, -0.00525260, -0.01720322, -0.04052736, -0.05045706, -0.03607317, -0.00738030,
  243. -0.01341764, -0.03965629, -0.05151616, -0.03814886, -0.01005819, 0.18968273, 0.33063684, -0.01300105, -0.01372950, -0.04017465,
  244. 0.13727832, 0.36402234, 0.01027890, -0.01832107, -0.03365072, 0.08734506, 0.38194295, 0.04338228, -0.02525993, 0.56408126,
  245. 0.00458352, -0.01648227, -0.04887868, 0.24585519, 0.62026135, 0.04314807, -0.02213737, -0.04158014, 0.16637289, 0.65027023,
  246. 0.09621636, -0.03101388, -0.04082742, -0.00904519, -0.02790922, -0.02117818, 0.00798662, -0.03995711, -0.01243427, -0.02231705,
  247. -0.02946266, 0.00992055, -0.03600283, -0.01684920, -0.00111684, -0.00411204, -0.01297130, -0.01723725, -0.01022545, -0.00165306,
  248. -0.00313110, -0.01218016, -0.01763266, -0.01125620, -0.00231663, -0.01374149, -0.03797620, -0.05142937, -0.03117307, -0.00581914,
  249. -0.01064003, -0.03608089, -0.05272168, -0.03375670, -0.00795586, 0.09628104, 0.27129991, -0.00353779, -0.01734151, -0.03153981,
  250. 0.05686230, 0.28500998, 0.02230594, -0.02374955, 0.68214326, 0.05018048, -0.02320852, -0.04383616, 0.18459474, 0.71517975,
  251. 0.10805613, -0.03263677, -0.03637639, -0.01394373, -0.02511203, -0.01728636, 0.05407331, -0.02867568, -0.01893131, -0.00240854,
  252. -0.00446511, -0.01636187, -0.02377053, -0.01522848, -0.00333334, -0.00819975, -0.02964169, -0.04499287, -0.02745350, -0.00612408,
  253. 0.02727416, 0.19446600, 0.00159832, -0.02232473, 0.74982506, 0.11452620, -0.03348048, -0.01605681, -0.02070339, -0.00458223
  254. };
  255. ///
  256. /// D.3 - Image metadata
  257. struct PreviewHeader {
  258. };
  259. struct AnimationHeader {
  260. };
  261. struct ExtraChannelInfo {
  262. };
  263. static ErrorOr<ExtraChannelInfo> read_extra_channel_info(LittleEndianInputBitStream&)
  264. {
  265. TODO();
  266. }
  267. struct ToneMapping {
  268. };
  269. static ErrorOr<ToneMapping> read_tone_mapping(LittleEndianInputBitStream&)
  270. {
  271. TODO();
  272. }
  273. struct OpsinInverseMatrix {
  274. };
  275. static ErrorOr<OpsinInverseMatrix> read_opsin_inverse_matrix(LittleEndianInputBitStream&)
  276. {
  277. TODO();
  278. }
  279. struct ImageMetadata {
  280. u8 orientation { 1 };
  281. Optional<SizeHeader> intrinsic_size;
  282. Optional<PreviewHeader> preview;
  283. Optional<AnimationHeader> animation;
  284. BitDepth bit_depth;
  285. bool modular_16bit_buffers { true };
  286. u16 num_extra_channels {};
  287. Vector<ExtraChannelInfo, 4> ec_info;
  288. bool xyb_encoded { true };
  289. ColourEncoding colour_encoding;
  290. ToneMapping tone_mapping;
  291. Extensions extensions;
  292. bool default_m;
  293. OpsinInverseMatrix opsin_inverse_matrix;
  294. u8 cw_mask { 0 };
  295. Array<double, 15> up2_weight = s_d_up2;
  296. Array<double, 55> up4_weight = s_d_up4;
  297. Array<double, 210> up8_weight = s_d_up8;
  298. };
  299. static ErrorOr<ImageMetadata> read_metadata_header(LittleEndianInputBitStream& stream)
  300. {
  301. ImageMetadata metadata;
  302. bool const all_default = TRY(stream.read_bit());
  303. if (!all_default) {
  304. bool const extra_fields = TRY(stream.read_bit());
  305. if (extra_fields) {
  306. metadata.orientation = 1 + TRY(stream.read_bits(3));
  307. bool const have_intr_size = TRY(stream.read_bit());
  308. if (have_intr_size)
  309. metadata.intrinsic_size = TRY(read_size_header(stream));
  310. bool const have_preview = TRY(stream.read_bit());
  311. if (have_preview)
  312. TODO();
  313. bool const have_animation = TRY(stream.read_bit());
  314. if (have_animation)
  315. TODO();
  316. }
  317. metadata.bit_depth = TRY(read_bit_depth(stream));
  318. metadata.modular_16bit_buffers = TRY(stream.read_bit());
  319. metadata.num_extra_channels = U32(0, 1, 2 + TRY(stream.read_bits(4)), 1 + TRY(stream.read_bits(12)));
  320. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  321. metadata.ec_info.append(TRY(read_extra_channel_info(stream)));
  322. metadata.xyb_encoded = TRY(stream.read_bit());
  323. metadata.colour_encoding = TRY(read_colour_encoding(stream));
  324. if (extra_fields)
  325. metadata.tone_mapping = TRY(read_tone_mapping(stream));
  326. metadata.extensions = TRY(read_extensions(stream));
  327. }
  328. metadata.default_m = TRY(stream.read_bit());
  329. if (!metadata.default_m && metadata.xyb_encoded)
  330. metadata.opsin_inverse_matrix = TRY(read_opsin_inverse_matrix(stream));
  331. if (!metadata.default_m)
  332. metadata.cw_mask = TRY(stream.read_bits(3));
  333. if (metadata.cw_mask != 0)
  334. TODO();
  335. return metadata;
  336. }
  337. ///
  338. /// Table F.7 — BlendingInfo bundle
  339. struct BlendingInfo {
  340. enum class BlendMode {
  341. kReplace = 0,
  342. kAdd = 1,
  343. kBlend = 2,
  344. kMulAdd = 3,
  345. kMul = 4,
  346. };
  347. BlendMode mode {};
  348. u8 alpha_channel {};
  349. u8 clamp {};
  350. u8 source {};
  351. };
  352. static ErrorOr<BlendingInfo> read_blending_info(LittleEndianInputBitStream& stream, ImageMetadata const& metadata, bool have_crop)
  353. {
  354. BlendingInfo blending_info;
  355. blending_info.mode = static_cast<BlendingInfo::BlendMode>(U32(0, 1, 2, 3 + TRY(stream.read_bits(2))));
  356. bool const extra = metadata.num_extra_channels > 0;
  357. // FIXME: also consider "cropped" image of the dimension of the frame
  358. VERIFY(!have_crop);
  359. bool const full_frame = !have_crop;
  360. if (extra) {
  361. TODO();
  362. }
  363. if (blending_info.mode != BlendingInfo::BlendMode::kReplace
  364. || !full_frame) {
  365. blending_info.source = TRY(stream.read_bits(2));
  366. }
  367. return blending_info;
  368. }
  369. ///
  370. /// J.1 - General
  371. struct RestorationFilter {
  372. bool gab { true };
  373. u8 epf_iters { 2 };
  374. Extensions extensions;
  375. };
  376. static ErrorOr<RestorationFilter> read_restoration_filter(LittleEndianInputBitStream& stream)
  377. {
  378. RestorationFilter restoration_filter;
  379. auto const all_defaults = TRY(stream.read_bit());
  380. if (!all_defaults) {
  381. restoration_filter.gab = TRY(stream.read_bit());
  382. if (restoration_filter.gab) {
  383. TODO();
  384. }
  385. restoration_filter.epf_iters = TRY(stream.read_bits(2));
  386. if (restoration_filter.epf_iters != 0) {
  387. TODO();
  388. }
  389. restoration_filter.extensions = TRY(read_extensions(stream));
  390. }
  391. return restoration_filter;
  392. }
  393. ///
  394. /// Table F.6 — Passes bundle
  395. struct Passes {
  396. u8 num_passes { 1 };
  397. };
  398. static ErrorOr<Passes> read_passes(LittleEndianInputBitStream& stream)
  399. {
  400. Passes passes;
  401. passes.num_passes = U32(1, 2, 3, 4 + TRY(stream.read_bits(3)));
  402. if (passes.num_passes != 1) {
  403. TODO();
  404. }
  405. return passes;
  406. }
  407. ///
  408. /// F.2 - FrameHeader
  409. struct FrameHeader {
  410. enum class FrameType {
  411. kRegularFrame = 0,
  412. kLFFrame = 1,
  413. kReferenceOnly = 2,
  414. kSkipProgressive = 3,
  415. };
  416. enum class Encoding {
  417. kVarDCT = 0,
  418. kModular = 1,
  419. };
  420. enum class Flags {
  421. None = 0,
  422. kNoise = 1,
  423. kPatches = 1 << 1,
  424. kSplines = 1 << 4,
  425. kUseLfFrame = 1 << 5,
  426. kSkipAdaptiveLFSmoothing = 1 << 7,
  427. };
  428. FrameType frame_type { FrameType::kRegularFrame };
  429. Encoding encoding { Encoding::kVarDCT };
  430. Flags flags { Flags::None };
  431. bool do_YCbCr { false };
  432. Array<u8, 3> jpeg_upsampling {};
  433. u8 upsampling {};
  434. Vector<u8> ec_upsampling {};
  435. u8 group_size_shift { 1 };
  436. Passes passes {};
  437. u8 lf_level {};
  438. bool have_crop { false };
  439. BlendingInfo blending_info {};
  440. bool is_last { true };
  441. bool save_before_ct {};
  442. String name {};
  443. RestorationFilter restoration_filter {};
  444. Extensions extensions {};
  445. };
  446. static int operator&(FrameHeader::Flags first, FrameHeader::Flags second)
  447. {
  448. return static_cast<int>(first) & static_cast<int>(second);
  449. }
  450. static ErrorOr<FrameHeader> read_frame_header(LittleEndianInputBitStream& stream, ImageMetadata const& metadata)
  451. {
  452. FrameHeader frame_header;
  453. bool const all_default = TRY(stream.read_bit());
  454. if (!all_default) {
  455. frame_header.frame_type = static_cast<FrameHeader::FrameType>(TRY(stream.read_bits(2)));
  456. frame_header.encoding = static_cast<FrameHeader::Encoding>(TRY(stream.read_bits(1)));
  457. frame_header.flags = static_cast<FrameHeader::Flags>(TRY(U64(stream)));
  458. if (!metadata.xyb_encoded)
  459. frame_header.do_YCbCr = TRY(stream.read_bit());
  460. if (!(frame_header.flags & FrameHeader::Flags::kUseLfFrame)) {
  461. if (frame_header.do_YCbCr) {
  462. frame_header.jpeg_upsampling[0] = TRY(stream.read_bits(2));
  463. frame_header.jpeg_upsampling[1] = TRY(stream.read_bits(2));
  464. frame_header.jpeg_upsampling[2] = TRY(stream.read_bits(2));
  465. }
  466. frame_header.upsampling = U32(1, 2, 4, 8);
  467. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  468. TODO();
  469. }
  470. if (frame_header.encoding == FrameHeader::Encoding::kModular)
  471. frame_header.group_size_shift = TRY(stream.read_bits(2));
  472. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  473. TODO();
  474. if (frame_header.frame_type != FrameHeader::FrameType::kReferenceOnly)
  475. frame_header.passes = TRY(read_passes(stream));
  476. if (frame_header.frame_type == FrameHeader::FrameType::kLFFrame)
  477. TODO();
  478. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame)
  479. frame_header.have_crop = TRY(stream.read_bit());
  480. if (frame_header.have_crop)
  481. TODO();
  482. bool const normal_frame = frame_header.frame_type == FrameHeader::FrameType::kRegularFrame
  483. || frame_header.frame_type == FrameHeader::FrameType::kSkipProgressive;
  484. if (normal_frame) {
  485. frame_header.blending_info = TRY(read_blending_info(stream, metadata, frame_header.have_crop));
  486. for (u16 i {}; i < metadata.num_extra_channels; ++i)
  487. TODO();
  488. if (metadata.animation.has_value())
  489. TODO();
  490. frame_header.is_last = TRY(stream.read_bit());
  491. }
  492. // FIXME: Ensure that is_last has the correct default value
  493. VERIFY(normal_frame);
  494. if (frame_header.frame_type != FrameHeader::FrameType::kLFFrame) {
  495. if (!frame_header.is_last)
  496. TODO();
  497. frame_header.save_before_ct = TRY(stream.read_bit());
  498. }
  499. // FIXME: Ensure that save_before_ct has the correct default value
  500. VERIFY(frame_header.frame_type != FrameHeader::FrameType::kLFFrame);
  501. auto const name_length = U32(0, TRY(stream.read_bits(4)), 16 + TRY(stream.read_bits(5)), 48 + TRY(stream.read_bits(10)));
  502. auto string_buffer = TRY(FixedArray<u8>::create(name_length));
  503. TRY(stream.read_until_filled(string_buffer.span()));
  504. frame_header.name = TRY(String::from_utf8(StringView { string_buffer.span() }));
  505. frame_header.restoration_filter = TRY(read_restoration_filter(stream));
  506. frame_header.extensions = TRY(read_extensions(stream));
  507. }
  508. return frame_header;
  509. }
  510. ///
  511. /// F.3 TOC
  512. struct TOC {
  513. FixedArray<u32> entries;
  514. FixedArray<u32> group_offsets;
  515. };
  516. static u64 num_toc_entries(FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  517. {
  518. if (num_groups == 1 && frame_header.passes.num_passes == 1)
  519. return 1;
  520. // Otherwise, there is one entry for each of the following sections,
  521. // in the order they are listed: LfGlobal, one per LfGroup in raster
  522. // order, one for HfGlobal followed by HfPass data for all the passes,
  523. // and num_groups * frame_header.passes.num_passes for the PassGroup sections.
  524. auto const hf_contribution = frame_header.encoding == FrameHeader::Encoding::kVarDCT ? (1 + frame_header.passes.num_passes) : 0;
  525. return 1 + num_lf_groups + hf_contribution + num_groups * frame_header.passes.num_passes;
  526. }
  527. static ErrorOr<TOC> read_toc(LittleEndianInputBitStream& stream, FrameHeader const& frame_header, u64 num_groups, u64 num_lf_groups)
  528. {
  529. TOC toc;
  530. bool const permuted_toc = TRY(stream.read_bit());
  531. if (permuted_toc) {
  532. // Read permutations
  533. TODO();
  534. }
  535. // F.3.3 - Decoding TOC
  536. stream.align_to_byte_boundary();
  537. auto const toc_entries = num_toc_entries(frame_header, num_groups, num_lf_groups);
  538. toc.entries = TRY(FixedArray<u32>::create(toc_entries));
  539. toc.group_offsets = TRY(FixedArray<u32>::create(toc_entries));
  540. for (u32 i {}; i < toc_entries; ++i) {
  541. auto const new_entry = U32(
  542. TRY(stream.read_bits(10)),
  543. 1024 + TRY(stream.read_bits(14)),
  544. 17408 + TRY(stream.read_bits(22)),
  545. 4211712 + TRY(stream.read_bits(30)));
  546. toc.entries[i] = new_entry;
  547. toc.group_offsets[i] = (i == 0 ? 0 : toc.group_offsets[i - 1]) + new_entry;
  548. }
  549. if (permuted_toc)
  550. TODO();
  551. stream.align_to_byte_boundary();
  552. return toc;
  553. }
  554. ///
  555. /// G.1.2 - LF channel dequantization weights
  556. struct LfChannelDequantization {
  557. float m_x_lf_unscaled { 4096 };
  558. float m_y_lf_unscaled { 512 };
  559. float m_b_lf_unscaled { 256 };
  560. };
  561. static ErrorOr<LfChannelDequantization> read_lf_channel_dequantization(LittleEndianInputBitStream& stream)
  562. {
  563. LfChannelDequantization lf_channel_dequantization;
  564. auto const all_default = TRY(stream.read_bit());
  565. if (!all_default) {
  566. TODO();
  567. }
  568. return lf_channel_dequantization;
  569. }
  570. ///
  571. /// C - Entropy decoding
  572. class EntropyDecoder {
  573. using BrotliCanonicalCode = Compress::Brotli::CanonicalCode;
  574. public:
  575. static ErrorOr<EntropyDecoder> create(LittleEndianInputBitStream& stream, u8 initial_num_distrib)
  576. {
  577. EntropyDecoder entropy_decoder;
  578. // C.2 - Distribution decoding
  579. entropy_decoder.m_lz77_enabled = TRY(stream.read_bit());
  580. if (entropy_decoder.m_lz77_enabled) {
  581. TODO();
  582. }
  583. TRY(entropy_decoder.read_pre_clustered_distributions(stream, initial_num_distrib));
  584. bool const use_prefix_code = TRY(stream.read_bit());
  585. if (!use_prefix_code)
  586. entropy_decoder.m_log_alphabet_size = 5 + TRY(stream.read_bits(2));
  587. for (auto& config : entropy_decoder.m_configs)
  588. config = TRY(entropy_decoder.read_config(stream));
  589. Vector<u16> counts;
  590. TRY(counts.try_resize(entropy_decoder.m_configs.size()));
  591. TRY(entropy_decoder.m_distributions.try_resize(entropy_decoder.m_configs.size()));
  592. if (use_prefix_code) {
  593. for (auto& count : counts) {
  594. if (TRY(stream.read_bit())) {
  595. auto const n = TRY(stream.read_bits(4));
  596. count = 1 + (1 << n) + TRY(stream.read_bits(n));
  597. } else {
  598. count = 1;
  599. }
  600. }
  601. // After reading the counts, the decoder reads each D[i] (implicitly
  602. // described by a prefix code) as specified in C.2.4, with alphabet_size = count[i].
  603. for (u32 i {}; i < entropy_decoder.m_distributions.size(); ++i) {
  604. // The alphabet size mentioned in the [Brotli] RFC is explicitly specified as parameter alphabet_size
  605. // when the histogram is being decoded, except in the special case of alphabet_size == 1, where no
  606. // histogram is read, and all decoded symbols are zero without reading any bits at all.
  607. if (counts[i] != 1) {
  608. entropy_decoder.m_distributions[i] = TRY(BrotliCanonicalCode::read_prefix_code(stream, counts[i]));
  609. } else {
  610. entropy_decoder.m_distributions[i] = BrotliCanonicalCode { { 1 }, { 0 } };
  611. }
  612. }
  613. } else {
  614. TODO();
  615. }
  616. return entropy_decoder;
  617. }
  618. ErrorOr<u32> decode_hybrid_uint(LittleEndianInputBitStream& stream, u16 context)
  619. {
  620. // C.3.3 - Hybrid integer decoding
  621. if (m_lz77_enabled)
  622. TODO();
  623. // Read symbol from entropy coded stream using D[clusters[ctx]]
  624. auto const token = TRY(m_distributions[m_clusters[context]].read_symbol(stream));
  625. auto r = TRY(read_uint(stream, m_configs[m_clusters[context]], token));
  626. return r;
  627. }
  628. private:
  629. struct HybridUint {
  630. u32 split_exponent {};
  631. u32 split {};
  632. u32 msb_in_token {};
  633. u32 lsb_in_token {};
  634. };
  635. static ErrorOr<u32> read_uint(LittleEndianInputBitStream& stream, HybridUint const& config, u32 token)
  636. {
  637. if (token < config.split)
  638. return token;
  639. auto const n = config.split_exponent
  640. - config.msb_in_token
  641. - config.lsb_in_token
  642. + ((token - config.split) >> (config.msb_in_token + config.lsb_in_token));
  643. VERIFY(n < 32);
  644. u32 const low_bits = token & ((1 << config.lsb_in_token) - 1);
  645. token = token >> config.lsb_in_token;
  646. token &= (1 << config.msb_in_token) - 1;
  647. token |= (1 << config.msb_in_token);
  648. auto const result = ((token << n | TRY(stream.read_bits(n))) << config.lsb_in_token) | low_bits;
  649. VERIFY(result < (1ul << 32));
  650. return result;
  651. }
  652. ErrorOr<void> read_pre_clustered_distributions(LittleEndianInputBitStream& stream, u8 num_distrib)
  653. {
  654. // C.2.2 Distribution clustering
  655. if (num_distrib == 1)
  656. TODO();
  657. TRY(m_clusters.try_resize(num_distrib));
  658. bool const is_simple = TRY(stream.read_bit());
  659. u16 num_clusters = 0;
  660. if (is_simple) {
  661. u8 const nbits = TRY(stream.read_bits(2));
  662. for (u8 i {}; i < num_distrib; ++i) {
  663. m_clusters[i] = TRY(stream.read_bits(nbits));
  664. if (m_clusters[i] >= num_clusters)
  665. num_clusters = m_clusters[i] + 1;
  666. }
  667. } else {
  668. TODO();
  669. }
  670. TRY(m_configs.try_resize(num_clusters));
  671. return {};
  672. }
  673. ErrorOr<HybridUint> read_config(LittleEndianInputBitStream& stream) const
  674. {
  675. // C.2.3 - Hybrid integer configuration
  676. HybridUint config {};
  677. config.split_exponent = TRY(stream.read_bits(ceil(log2(m_log_alphabet_size + 1))));
  678. if (config.split_exponent != m_log_alphabet_size) {
  679. auto nbits = ceil(log2(config.split_exponent + 1));
  680. config.msb_in_token = TRY(stream.read_bits(nbits));
  681. nbits = ceil(log2(config.split_exponent - config.msb_in_token + 1));
  682. config.lsb_in_token = TRY(stream.read_bits(nbits));
  683. } else {
  684. config.msb_in_token = 0;
  685. config.lsb_in_token = 0;
  686. }
  687. config.split = 1 << config.split_exponent;
  688. return config;
  689. }
  690. bool m_lz77_enabled {};
  691. Vector<u32> m_clusters;
  692. Vector<HybridUint> m_configs;
  693. u8 m_log_alphabet_size { 15 };
  694. Vector<BrotliCanonicalCode> m_distributions; // D in the spec
  695. };
  696. ///
  697. /// H.4.2 - MA tree decoding
  698. class MATree {
  699. public:
  700. struct LeafNode {
  701. u8 ctx {};
  702. u8 predictor {};
  703. i32 offset {};
  704. u32 multiplier {};
  705. };
  706. static ErrorOr<MATree> decode(LittleEndianInputBitStream& stream, Optional<EntropyDecoder>& decoder)
  707. {
  708. // G.1.3 - GlobalModular
  709. MATree tree;
  710. // 1 / 2 Read the 6 pre-clustered distributions
  711. auto const num_distrib = 6;
  712. if (!decoder.has_value())
  713. decoder = TRY(EntropyDecoder::create(stream, num_distrib));
  714. // 2 / 2 Decode the tree
  715. u64 ctx_id = 0;
  716. u64 nodes_left = 1;
  717. tree.m_tree.clear();
  718. while (nodes_left > 0) {
  719. nodes_left--;
  720. i32 const property = TRY(decoder->decode_hybrid_uint(stream, 1)) - 1;
  721. if (property >= 0) {
  722. DecisionNode decision_node;
  723. decision_node.property = property;
  724. decision_node.value = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 0)));
  725. decision_node.left_child = tree.m_tree.size() + nodes_left + 1;
  726. decision_node.right_child = tree.m_tree.size() + nodes_left + 2;
  727. tree.m_tree.empend(decision_node);
  728. nodes_left += 2;
  729. } else {
  730. LeafNode leaf_node;
  731. leaf_node.ctx = ctx_id++;
  732. leaf_node.predictor = TRY(decoder->decode_hybrid_uint(stream, 2));
  733. leaf_node.offset = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, 3)));
  734. auto const mul_log = TRY(decoder->decode_hybrid_uint(stream, 4));
  735. auto const mul_bits = TRY(decoder->decode_hybrid_uint(stream, 5));
  736. leaf_node.multiplier = (mul_bits + 1) << mul_log;
  737. tree.m_tree.empend(leaf_node);
  738. }
  739. }
  740. // Finally, the decoder reads (tree.size() + 1) / 2 pre-clustered distributions D as specified in C.1.
  741. auto const num_pre_clustered_distributions = (tree.m_tree.size() + 1) / 2;
  742. decoder = TRY(decoder->create(stream, num_pre_clustered_distributions));
  743. return tree;
  744. }
  745. LeafNode get_leaf(Vector<i32> const& properties) const
  746. {
  747. // To find the MA leaf node, the MA tree is traversed, starting at the root node tree[0]
  748. // and for each decision node d, testing if property[d.property] > d.value, proceeding to
  749. // the node tree[d.left_child] if the test evaluates to true and to the node tree[d.right_child]
  750. // otherwise, until a leaf node is reached.
  751. DecisionNode node { m_tree[0].get<DecisionNode>() };
  752. while (true) {
  753. auto const next_node = [this, &properties, &node]() {
  754. // Note: The behavior when trying to access a non-existing property is taken from jxl-oxide
  755. if (node.property < properties.size() && properties[node.property] > node.value)
  756. return m_tree[node.left_child];
  757. return m_tree[node.right_child];
  758. }();
  759. if (next_node.has<LeafNode>())
  760. return next_node.get<LeafNode>();
  761. node = next_node.get<DecisionNode>();
  762. }
  763. }
  764. private:
  765. struct DecisionNode {
  766. u64 property {};
  767. i64 value {};
  768. u64 left_child {};
  769. u64 right_child {};
  770. };
  771. Vector<Variant<DecisionNode, LeafNode>> m_tree;
  772. };
  773. ///
  774. /// H.5 - Self-correcting predictor
  775. struct WPHeader {
  776. u8 wp_p1 { 16 };
  777. u8 wp_p2 { 10 };
  778. u8 wp_p3a { 7 };
  779. u8 wp_p3b { 7 };
  780. u8 wp_p3c { 7 };
  781. u8 wp_p3d { 0 };
  782. u8 wp_p3e { 0 };
  783. u8 wp_w0 { 13 };
  784. u8 wp_w1 { 12 };
  785. u8 wp_w2 { 12 };
  786. u8 wp_w3 { 12 };
  787. };
  788. static ErrorOr<WPHeader> read_self_correcting_predictor(LittleEndianInputBitStream& stream)
  789. {
  790. WPHeader self_correcting_predictor {};
  791. bool const default_wp = TRY(stream.read_bit());
  792. if (!default_wp) {
  793. TODO();
  794. }
  795. return self_correcting_predictor;
  796. }
  797. ///
  798. ///
  799. struct TransformInfo {
  800. enum class TransformId {
  801. kRCT = 0,
  802. kPalette = 1,
  803. kSqueeze = 2,
  804. };
  805. TransformId tr {};
  806. u32 begin_c {};
  807. u32 rct_type {};
  808. };
  809. static ErrorOr<TransformInfo> read_transform_info(LittleEndianInputBitStream& stream)
  810. {
  811. TransformInfo transform_info;
  812. transform_info.tr = static_cast<TransformInfo::TransformId>(TRY(stream.read_bits(2)));
  813. if (transform_info.tr != TransformInfo::TransformId::kSqueeze) {
  814. transform_info.begin_c = U32(
  815. TRY(stream.read_bits(3)),
  816. 8 + TRY(stream.read_bits(3)),
  817. 72 + TRY(stream.read_bits(10)),
  818. 1096 + TRY(stream.read_bits(13)));
  819. }
  820. if (transform_info.tr == TransformInfo::TransformId::kRCT) {
  821. transform_info.rct_type = U32(
  822. 6,
  823. TRY(stream.read_bits(2)),
  824. 2 + TRY(stream.read_bits(4)),
  825. 10 + TRY(stream.read_bits(6)));
  826. }
  827. if (transform_info.tr != TransformInfo::TransformId::kRCT)
  828. TODO();
  829. return transform_info;
  830. }
  831. ///
  832. /// Local abstractions to store the decoded image
  833. class Channel {
  834. public:
  835. static ErrorOr<Channel> create(u32 width, u32 height)
  836. {
  837. Channel channel;
  838. channel.m_width = width;
  839. channel.m_height = height;
  840. TRY(channel.m_pixels.try_resize(channel.m_width * channel.m_height));
  841. return channel;
  842. }
  843. i32 get(u32 x, u32 y) const
  844. {
  845. return m_pixels[x * m_width + y];
  846. }
  847. void set(u32 x, u32 y, i32 value)
  848. {
  849. m_pixels[x * m_width + y] = value;
  850. }
  851. u32 width() const
  852. {
  853. return m_width;
  854. }
  855. u32 height() const
  856. {
  857. return m_height;
  858. }
  859. u32 hshift() const
  860. {
  861. return m_hshift;
  862. }
  863. u32 vshift() const
  864. {
  865. return m_vshift;
  866. }
  867. private:
  868. u32 m_width {};
  869. u32 m_height {};
  870. u32 m_hshift {};
  871. u32 m_vshift {};
  872. Vector<i32> m_pixels {};
  873. };
  874. class Image {
  875. public:
  876. static ErrorOr<Image> create(IntSize size)
  877. {
  878. Image image {};
  879. // FIXME: Don't assume three channels and a fixed size
  880. TRY(image.m_channels.try_append(TRY(Channel::create(size.width(), size.height()))));
  881. TRY(image.m_channels.try_append(TRY(Channel::create(size.width(), size.height()))));
  882. TRY(image.m_channels.try_append(TRY(Channel::create(size.width(), size.height()))));
  883. return image;
  884. }
  885. ErrorOr<NonnullRefPtr<Bitmap>> to_bitmap(u8 bits_per_sample) const
  886. {
  887. // FIXME: which channel size should we use?
  888. auto const width = m_channels[0].width();
  889. auto const height = m_channels[0].height();
  890. auto bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { width, height }));
  891. // FIXME: This assumes a raw image with RGB channels, other cases are possible
  892. VERIFY(bits_per_sample >= 8);
  893. for (u32 y {}; y < height; ++y) {
  894. for (u32 x {}; x < width; ++x) {
  895. auto const to_u8 = [&, bits_per_sample](i32 sample) -> u8 {
  896. // FIXME: Don't truncate the result to 8 bits
  897. static constexpr auto maximum_supported_bit_depth = 8;
  898. if (bits_per_sample > maximum_supported_bit_depth)
  899. sample >>= (bits_per_sample - maximum_supported_bit_depth);
  900. return clamp(sample + .5, 0, (1 << maximum_supported_bit_depth) - 1);
  901. };
  902. Color const color {
  903. to_u8(m_channels[0].get(x, y)),
  904. to_u8(m_channels[1].get(x, y)),
  905. to_u8(m_channels[2].get(x, y)),
  906. };
  907. bitmap->set_pixel(x, y, color);
  908. }
  909. }
  910. return bitmap;
  911. }
  912. Vector<Channel>& channels()
  913. {
  914. return m_channels;
  915. }
  916. private:
  917. Vector<Channel> m_channels;
  918. };
  919. ///
  920. /// H.2 - Image decoding
  921. struct ModularHeader {
  922. bool use_global_tree {};
  923. WPHeader wp_params {};
  924. Vector<TransformInfo> transform {};
  925. };
  926. static ErrorOr<Vector<i32>> get_properties(Vector<Channel> const& channels, u16 i, u32 x, u32 y)
  927. {
  928. Vector<i32> properties;
  929. // Table H.4 - Property definitions
  930. TRY(properties.try_append(i));
  931. // FIXME: Handle other cases than GlobalModular
  932. TRY(properties.try_append(0));
  933. TRY(properties.try_append(y));
  934. TRY(properties.try_append(x));
  935. i32 const W = x > 0 ? channels[i].get(x - 1, y) : (y > 0 ? channels[i].get(x, y - 1) : 0);
  936. i32 const N = y > 0 ? channels[i].get(x, y - 1) : W;
  937. i32 const NW = x > 0 && y > 0 ? channels[i].get(x - 1, y - 1) : W;
  938. i32 const NE = x + 1 < channels[i].width() && y > 0 ? channels[i].get(x + 1, y - 1) : N;
  939. i32 const NN = y > 1 ? channels[i].get(x, y - 2) : N;
  940. i32 const WW = x > 1 ? channels[i].get(x - 2, y) : W;
  941. TRY(properties.try_append(abs(N)));
  942. TRY(properties.try_append(abs(W)));
  943. TRY(properties.try_append(N));
  944. TRY(properties.try_append(W));
  945. // x > 0 ? W - /* (the value of property 9 at position (x - 1, y)) */ : W
  946. i32 x_1 = x - 1;
  947. 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);
  948. i32 const N_x_1 = x_1 >= 0 && y > 0 ? channels[i].get(x_1, y - 1) : W_x_1;
  949. i32 const NW_x_1 = x_1 > 0 && y > 0 ? channels[i].get(x_1 - 1, y - 1) : W_x_1;
  950. TRY(properties.try_append(W_x_1 + N_x_1 - NW_x_1));
  951. TRY(properties.try_append(W + N - NW));
  952. TRY(properties.try_append(W - NW));
  953. TRY(properties.try_append(NW - N));
  954. TRY(properties.try_append(N - NE));
  955. TRY(properties.try_append(N - NN));
  956. TRY(properties.try_append(W - WW));
  957. // FIXME: Correctly compute max_error
  958. TRY(properties.try_append(0));
  959. for (i16 j = i - 1; j >= 0; j--) {
  960. if (channels[j].width() != channels[i].width())
  961. continue;
  962. if (channels[j].height() != channels[i].height())
  963. continue;
  964. if (channels[j].hshift() != channels[i].hshift())
  965. continue;
  966. if (channels[j].vshift() != channels[i].vshift())
  967. continue;
  968. auto rC = channels[j].get(x, y);
  969. auto rW = (x > 0 ? channels[j].get(x - 1, y) : 0);
  970. auto rN = (y > 0 ? channels[j].get(x, y - 1) : rW);
  971. auto rNW = (x > 0 && y > 0 ? channels[j].get(x - 1, y - 1) : rW);
  972. auto rG = clamp(rW + rN - rNW, min(rW, rN), max(rW, rN));
  973. TRY(properties.try_append(abs(rC)));
  974. TRY(properties.try_append(rC));
  975. TRY(properties.try_append(abs(rC - rG)));
  976. TRY(properties.try_append(rC - rG));
  977. }
  978. return properties;
  979. }
  980. static i32 prediction(Channel const& channel, u32 x, u32 y, u32 predictor)
  981. {
  982. i32 const W = x > 0 ? channel.get(x - 1, y) : (y > 0 ? channel.get(x, y - 1) : 0);
  983. i32 const N = y > 0 ? channel.get(x, y - 1) : W;
  984. i32 const NW = x > 0 && y > 0 ? channel.get(x - 1, y - 1) : W;
  985. i32 const NE = x + 1 < channel.width() && y > 0 ? channel.get(x + 1, y - 1) : N;
  986. i32 const NN = y > 1 ? channel.get(x, y - 2) : N;
  987. i32 const NEE = x + 2 < channel.width() and y > 0 ? channel.get(x + 2, y - 1) : NE;
  988. i32 const WW = x > 1 ? channel.get(x - 2, y) : W;
  989. switch (predictor) {
  990. case 0:
  991. return 0;
  992. case 1:
  993. return W;
  994. case 2:
  995. return N;
  996. case 3:
  997. return (W + N) / 2;
  998. case 4:
  999. return abs(N - NW) < abs(W - NW) ? W : N;
  1000. case 5:
  1001. return clamp(W + N - NW, min(W, N), max(W, N));
  1002. case 6:
  1003. TODO();
  1004. return (0 + 3) >> 3;
  1005. case 7:
  1006. return NE;
  1007. case 8:
  1008. return NW;
  1009. case 9:
  1010. return WW;
  1011. case 10:
  1012. return (W + NW) / 2;
  1013. case 11:
  1014. return (N + NW) / 2;
  1015. case 12:
  1016. return (N + NE) / 2;
  1017. case 13:
  1018. return (6 * N - 2 * NN + 7 * W + WW + NEE + 3 * NE + 8) / 16;
  1019. }
  1020. VERIFY_NOT_REACHED();
  1021. }
  1022. static ErrorOr<ModularHeader> read_modular_header(LittleEndianInputBitStream& stream,
  1023. Image& image,
  1024. Optional<EntropyDecoder>& decoder,
  1025. MATree const& global_tree,
  1026. u16 num_channels)
  1027. {
  1028. ModularHeader modular_header;
  1029. modular_header.use_global_tree = TRY(stream.read_bit());
  1030. modular_header.wp_params = TRY(read_self_correcting_predictor(stream));
  1031. auto const nb_transforms = U32(0, 1, 2 + TRY(stream.read_bits(4)), 18 + TRY(stream.read_bits(8)));
  1032. TRY(modular_header.transform.try_resize(nb_transforms));
  1033. for (u32 i {}; i < nb_transforms; ++i)
  1034. modular_header.transform[i] = TRY(read_transform_info(stream));
  1035. Optional<MATree> local_tree;
  1036. if (!modular_header.use_global_tree)
  1037. TODO();
  1038. // The decoder then starts an entropy-coded stream (C.1) and decodes the data for each channel
  1039. // (in ascending order of index) as specified in H.3, skipping any channels having width or height
  1040. // zero. Finally, the inverse transformations are applied (from last to first) as described in H.6.
  1041. auto const& tree = local_tree.has_value() ? *local_tree : global_tree;
  1042. for (u16 i {}; i < num_channels; ++i) {
  1043. for (u32 y {}; y < image.channels()[i].height(); y++) {
  1044. for (u32 x {}; x < image.channels()[i].width(); x++) {
  1045. auto const properties = TRY(get_properties(image.channels(), i, x, y));
  1046. auto const leaf_node = tree.get_leaf(properties);
  1047. auto diff = unpack_signed(TRY(decoder->decode_hybrid_uint(stream, leaf_node.ctx)));
  1048. diff = (diff * leaf_node.multiplier) + leaf_node.offset;
  1049. auto const total = diff + prediction(image.channels()[i], x, y, leaf_node.predictor);
  1050. image.channels()[i].set(x, y, total);
  1051. }
  1052. }
  1053. }
  1054. return modular_header;
  1055. }
  1056. ///
  1057. /// G.1.2 - LF channel dequantization weights
  1058. struct GlobalModular {
  1059. MATree ma_tree;
  1060. ModularHeader modular_header;
  1061. };
  1062. static ErrorOr<GlobalModular> read_global_modular(LittleEndianInputBitStream& stream,
  1063. Image& image,
  1064. FrameHeader const& frame_header,
  1065. ImageMetadata const& metadata,
  1066. Optional<EntropyDecoder>& entropy_decoder)
  1067. {
  1068. GlobalModular global_modular;
  1069. auto const decode_ma_tree = TRY(stream.read_bit());
  1070. if (decode_ma_tree)
  1071. global_modular.ma_tree = TRY(MATree::decode(stream, entropy_decoder));
  1072. // The decoder then decodes a modular sub-bitstream (Annex H), where
  1073. // the number of channels is computed as follows:
  1074. auto num_channels = metadata.num_extra_channels;
  1075. if (frame_header.encoding == FrameHeader::Encoding::kModular) {
  1076. if (!frame_header.do_YCbCr && !metadata.xyb_encoded
  1077. && metadata.colour_encoding.colour_space == ColourEncoding::ColourSpace::kGrey) {
  1078. num_channels += 1;
  1079. } else {
  1080. num_channels += 3;
  1081. }
  1082. }
  1083. // FIXME: Ensure this spec comment:
  1084. // However, the decoder only decodes the first nb_meta_channels channels and any further channels
  1085. // that have a width and height that are both at most group_dim. At that point, it stops decoding.
  1086. // No inverse transforms are applied yet.
  1087. global_modular.modular_header = TRY(read_modular_header(stream, image, entropy_decoder, global_modular.ma_tree, num_channels));
  1088. return global_modular;
  1089. }
  1090. ///
  1091. /// G.1 - LfGlobal
  1092. struct LfGlobal {
  1093. LfChannelDequantization lf_dequant;
  1094. GlobalModular gmodular;
  1095. };
  1096. static ErrorOr<LfGlobal> read_lf_global(LittleEndianInputBitStream& stream,
  1097. Image& image,
  1098. FrameHeader const& frame_header,
  1099. ImageMetadata const& metadata,
  1100. Optional<EntropyDecoder>& entropy_decoder)
  1101. {
  1102. LfGlobal lf_global;
  1103. if (frame_header.flags != FrameHeader::Flags::None)
  1104. TODO();
  1105. lf_global.lf_dequant = TRY(read_lf_channel_dequantization(stream));
  1106. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT)
  1107. TODO();
  1108. lf_global.gmodular = TRY(read_global_modular(stream, image, frame_header, metadata, entropy_decoder));
  1109. return lf_global;
  1110. }
  1111. ///
  1112. /// H.6 - Transformations
  1113. static void apply_rct(Image& image, TransformInfo const& transformation)
  1114. {
  1115. auto& channels = image.channels();
  1116. for (u32 y {}; y < channels[transformation.begin_c].height(); y++) {
  1117. for (u32 x {}; x < channels[transformation.begin_c].width(); x++) {
  1118. auto a = channels[transformation.begin_c + 0].get(x, y);
  1119. auto b = channels[transformation.begin_c + 1].get(x, y);
  1120. auto c = channels[transformation.begin_c + 2].get(x, y);
  1121. i32 d {};
  1122. i32 e {};
  1123. i32 f {};
  1124. auto const permutation = transformation.rct_type / 7;
  1125. auto const type = transformation.rct_type % 7;
  1126. if (type == 6) { // YCgCo
  1127. auto const tmp = a - (c >> 1);
  1128. e = c + tmp;
  1129. f = tmp - (b >> 1);
  1130. d = f + b;
  1131. } else {
  1132. if (type & 1)
  1133. c = c + a;
  1134. if ((type >> 1) == 1)
  1135. b = b + a;
  1136. if ((type >> 1) == 2)
  1137. b = b + ((a + c) >> 1);
  1138. d = a;
  1139. e = b;
  1140. f = c;
  1141. }
  1142. Array<i32, 3> v {};
  1143. v[permutation % 3] = d;
  1144. v[(permutation + 1 + (permutation / 3)) % 3] = e;
  1145. v[(permutation + 2 - (permutation / 3)) % 3] = f;
  1146. channels[transformation.begin_c + 0].set(x, y, v[0]);
  1147. channels[transformation.begin_c + 1].set(x, y, v[1]);
  1148. channels[transformation.begin_c + 2].set(x, y, v[2]);
  1149. }
  1150. }
  1151. }
  1152. static void apply_transformation(Image& image, TransformInfo const& transformation)
  1153. {
  1154. switch (transformation.tr) {
  1155. case TransformInfo::TransformId::kRCT:
  1156. apply_rct(image, transformation);
  1157. break;
  1158. case TransformInfo::TransformId::kPalette:
  1159. case TransformInfo::TransformId::kSqueeze:
  1160. TODO();
  1161. default:
  1162. VERIFY_NOT_REACHED();
  1163. }
  1164. }
  1165. ///
  1166. /// G.3.2 - PassGroup
  1167. static ErrorOr<void> read_pass_group(LittleEndianInputBitStream& stream,
  1168. Image& image,
  1169. FrameHeader const& frame_header,
  1170. u32 group_dim)
  1171. {
  1172. if (frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1173. (void)stream;
  1174. TODO();
  1175. }
  1176. auto& channels = image.channels();
  1177. for (u16 i {}; i < channels.size(); ++i) {
  1178. // Skip meta-channels
  1179. // FIXME: Also test if the channel has already been decoded
  1180. // See: nb_meta_channels in the spec
  1181. bool const is_meta_channel = channels[i].width() <= group_dim
  1182. || channels[i].height() <= group_dim
  1183. || channels[i].hshift() >= 3
  1184. || channels[i].vshift() >= 3;
  1185. if (!is_meta_channel)
  1186. TODO();
  1187. }
  1188. return {};
  1189. }
  1190. ///
  1191. /// Table F.1 — Frame bundle
  1192. struct Frame {
  1193. FrameHeader frame_header;
  1194. TOC toc;
  1195. LfGlobal lf_global;
  1196. u64 width {};
  1197. u64 height {};
  1198. u64 num_groups {};
  1199. u64 num_lf_groups {};
  1200. };
  1201. static ErrorOr<Frame> read_frame(LittleEndianInputBitStream& stream,
  1202. Image& image,
  1203. SizeHeader const& size_header,
  1204. ImageMetadata const& metadata,
  1205. Optional<EntropyDecoder>& entropy_decoder)
  1206. {
  1207. // F.1 - General
  1208. // Each Frame is byte-aligned by invoking ZeroPadToByte() (B.2.7)
  1209. stream.align_to_byte_boundary();
  1210. Frame frame;
  1211. frame.frame_header = TRY(read_frame_header(stream, metadata));
  1212. if (!frame.frame_header.have_crop) {
  1213. frame.width = size_header.width;
  1214. frame.height = size_header.height;
  1215. } else {
  1216. TODO();
  1217. }
  1218. if (frame.frame_header.upsampling > 1) {
  1219. frame.width = ceil(frame.width / frame.frame_header.upsampling);
  1220. frame.height = ceil(frame.height / frame.frame_header.upsampling);
  1221. }
  1222. if (frame.frame_header.lf_level > 0)
  1223. TODO();
  1224. // F.2 - FrameHeader
  1225. auto const group_dim = 128 << frame.frame_header.group_size_shift;
  1226. frame.num_groups = ceil(frame.width / group_dim) * ceil(frame.height / group_dim);
  1227. frame.num_lf_groups = ceil(frame.width / (group_dim * 8)) * ceil(frame.height / (group_dim * 8));
  1228. frame.toc = TRY(read_toc(stream, frame.frame_header, frame.num_groups, frame.num_lf_groups));
  1229. image = TRY(Image::create({ frame.width, frame.height }));
  1230. frame.lf_global = TRY(read_lf_global(stream, image, frame.frame_header, metadata, entropy_decoder));
  1231. for (u32 i {}; i < frame.num_lf_groups; ++i)
  1232. TODO();
  1233. if (frame.frame_header.encoding == FrameHeader::Encoding::kVarDCT) {
  1234. TODO();
  1235. }
  1236. auto const num_pass_group = frame.num_groups * frame.frame_header.passes.num_passes;
  1237. auto const& transform_infos = frame.lf_global.gmodular.modular_header.transform;
  1238. for (u64 i {}; i < num_pass_group; ++i)
  1239. TRY(read_pass_group(stream, image, frame.frame_header, group_dim));
  1240. // G.4.2 - Modular group data
  1241. // When all modular groups are decoded, the inverse transforms are applied to
  1242. // the at that point fully decoded GlobalModular image, as specified in H.6.
  1243. for (auto const& transformation : transform_infos.in_reverse())
  1244. apply_transformation(image, transformation);
  1245. return frame;
  1246. }
  1247. ///
  1248. /// 5.2 - Mirroring
  1249. static u32 mirror_1d(i32 coord, u32 size)
  1250. {
  1251. if (coord < 0)
  1252. return mirror_1d(-coord - 1, size);
  1253. else if (static_cast<u32>(coord) >= size)
  1254. return mirror_1d(2 * size - 1 - coord, size);
  1255. else
  1256. return coord;
  1257. }
  1258. ///
  1259. /// K - Image features
  1260. static ErrorOr<void> apply_upsampling(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1261. {
  1262. Optional<u32> ec_max;
  1263. for (auto upsampling : frame.frame_header.ec_upsampling) {
  1264. if (!ec_max.has_value() || upsampling > *ec_max)
  1265. ec_max = upsampling;
  1266. }
  1267. if (frame.frame_header.upsampling > 1 || ec_max.value_or(0) > 1) {
  1268. if (ec_max.value_or(0) > 2)
  1269. TODO();
  1270. auto const k = frame.frame_header.upsampling;
  1271. auto weight = [k, &metadata](u8 index) -> double {
  1272. if (k == 2)
  1273. return metadata.up2_weight[index];
  1274. if (k == 4)
  1275. return metadata.up4_weight[index];
  1276. return metadata.up8_weight[index];
  1277. };
  1278. // FIXME: Use ec_upsampling for extra-channels
  1279. for (auto& channel : image.channels()) {
  1280. auto upsampled = TRY(Channel::create(k * channel.width(), k * channel.height()));
  1281. // Loop over the original image
  1282. for (u32 y {}; y < channel.height(); y++) {
  1283. for (u32 x {}; x < channel.width(); x++) {
  1284. // Loop over the upsampling factor
  1285. for (u8 kx {}; kx < k; ++kx) {
  1286. for (u8 ky {}; ky < k; ++ky) {
  1287. double sum {};
  1288. // Loop over the W window
  1289. double W_min = NumericLimits<double>::max();
  1290. double W_max = -NumericLimits<double>::max();
  1291. for (u8 ix {}; ix < 5; ++ix) {
  1292. for (u8 iy {}; iy < 5; ++iy) {
  1293. auto const j = (ky < k / 2) ? (iy + 5 * ky) : ((4 - iy) + 5 * (k - 1 - ky));
  1294. auto const i = (kx < k / 2) ? (ix + 5 * kx) : ((4 - ix) + 5 * (k - 1 - kx));
  1295. auto const minimum = min(i, j);
  1296. auto const maximum = max(i, j);
  1297. auto const index = 5 * k * minimum / 2 - minimum * (minimum - 1) / 2 + maximum - minimum;
  1298. auto const origin_sample_x = mirror_1d(x + ix - 2, channel.width());
  1299. auto const origin_sample_y = mirror_1d(y + iy - 2, channel.height());
  1300. auto const origin_sample = channel.get(origin_sample_x, origin_sample_y);
  1301. W_min = min(W_min, origin_sample);
  1302. W_max = max(W_max, origin_sample);
  1303. sum += origin_sample * weight(index);
  1304. }
  1305. }
  1306. // The resulting sample is clamped to the range [a, b] where a and b are
  1307. // the minimum and maximum of the samples in W.
  1308. sum = clamp(sum, W_min, W_max);
  1309. upsampled.set(x * k + kx, y * k + ky, sum);
  1310. }
  1311. }
  1312. }
  1313. }
  1314. channel = move(upsampled);
  1315. }
  1316. }
  1317. return {};
  1318. }
  1319. static ErrorOr<void> apply_image_features(Image& image, ImageMetadata const& metadata, Frame const& frame)
  1320. {
  1321. TRY(apply_upsampling(image, metadata, frame));
  1322. if (frame.frame_header.flags != FrameHeader::Flags::None)
  1323. TODO();
  1324. return {};
  1325. }
  1326. ///
  1327. class JPEGXLLoadingContext {
  1328. public:
  1329. JPEGXLLoadingContext(NonnullOwnPtr<Stream> stream)
  1330. : m_stream(move(stream))
  1331. {
  1332. }
  1333. ErrorOr<void> decode_image_header()
  1334. {
  1335. constexpr auto JPEGXL_SIGNATURE = 0xFF0A;
  1336. auto const signature = TRY(m_stream.read_value<BigEndian<u16>>());
  1337. if (signature != JPEGXL_SIGNATURE)
  1338. return Error::from_string_literal("Unrecognized signature");
  1339. m_header = TRY(read_size_header(m_stream));
  1340. m_metadata = TRY(read_metadata_header(m_stream));
  1341. m_state = State::HeaderDecoded;
  1342. return {};
  1343. }
  1344. ErrorOr<void> decode_frame()
  1345. {
  1346. Image image {};
  1347. auto const frame = TRY(read_frame(m_stream, image, m_header, m_metadata, m_entropy_decoder));
  1348. if (frame.frame_header.restoration_filter.gab || frame.frame_header.restoration_filter.epf_iters != 0)
  1349. TODO();
  1350. TRY(apply_image_features(image, m_metadata, frame));
  1351. // FIXME: Do a proper color transformation with metadata.colour_encoding
  1352. if (m_metadata.xyb_encoded || frame.frame_header.do_YCbCr)
  1353. TODO();
  1354. m_bitmap = TRY(image.to_bitmap(m_metadata.bit_depth.bits_per_sample));
  1355. return {};
  1356. }
  1357. ErrorOr<void> decode()
  1358. {
  1359. auto result = [this]() -> ErrorOr<void> {
  1360. // A.1 - Codestream structure
  1361. // The header is already decoded in JPEGXLImageDecoderPlugin::create()
  1362. if (m_metadata.colour_encoding.want_icc)
  1363. TODO();
  1364. if (m_metadata.preview.has_value())
  1365. TODO();
  1366. TRY(decode_frame());
  1367. return {};
  1368. }();
  1369. m_state = result.is_error() ? State::Error : State::FrameDecoded;
  1370. return result;
  1371. }
  1372. enum class State {
  1373. NotDecoded = 0,
  1374. Error,
  1375. HeaderDecoded,
  1376. FrameDecoded,
  1377. BitmapDecoded
  1378. };
  1379. State state() const
  1380. {
  1381. return m_state;
  1382. }
  1383. IntSize size() const
  1384. {
  1385. return { m_header.width, m_header.height };
  1386. }
  1387. RefPtr<Bitmap> bitmap() const
  1388. {
  1389. return m_bitmap;
  1390. }
  1391. private:
  1392. State m_state { State::NotDecoded };
  1393. LittleEndianInputBitStream m_stream;
  1394. RefPtr<Gfx::Bitmap> m_bitmap;
  1395. Optional<EntropyDecoder> m_entropy_decoder {};
  1396. SizeHeader m_header;
  1397. ImageMetadata m_metadata;
  1398. FrameHeader m_frame_header;
  1399. TOC m_toc;
  1400. };
  1401. JPEGXLImageDecoderPlugin::JPEGXLImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  1402. {
  1403. m_context = make<JPEGXLLoadingContext>(move(stream));
  1404. }
  1405. JPEGXLImageDecoderPlugin::~JPEGXLImageDecoderPlugin() = default;
  1406. IntSize JPEGXLImageDecoderPlugin::size()
  1407. {
  1408. return m_context->size();
  1409. }
  1410. bool JPEGXLImageDecoderPlugin::sniff(ReadonlyBytes data)
  1411. {
  1412. return data.size() > 2
  1413. && data.data()[0] == 0xFF
  1414. && data.data()[1] == 0x0A;
  1415. }
  1416. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGXLImageDecoderPlugin::create(ReadonlyBytes data)
  1417. {
  1418. auto stream = TRY(try_make<FixedMemoryStream>(data));
  1419. auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) JPEGXLImageDecoderPlugin(move(stream))));
  1420. TRY(plugin->m_context->decode_image_header());
  1421. return plugin;
  1422. }
  1423. bool JPEGXLImageDecoderPlugin::is_animated()
  1424. {
  1425. return false;
  1426. }
  1427. size_t JPEGXLImageDecoderPlugin::loop_count()
  1428. {
  1429. return 0;
  1430. }
  1431. size_t JPEGXLImageDecoderPlugin::frame_count()
  1432. {
  1433. return 1;
  1434. }
  1435. size_t JPEGXLImageDecoderPlugin::first_animated_frame_index()
  1436. {
  1437. return 0;
  1438. }
  1439. ErrorOr<ImageFrameDescriptor> JPEGXLImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
  1440. {
  1441. if (index > 0)
  1442. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Invalid frame index");
  1443. if (m_context->state() == JPEGXLLoadingContext::State::Error)
  1444. return Error::from_string_literal("JPEGXLImageDecoderPlugin: Decoding failed");
  1445. if (m_context->state() < JPEGXLLoadingContext::State::BitmapDecoded)
  1446. TRY(m_context->decode());
  1447. return ImageFrameDescriptor { m_context->bitmap(), 0 };
  1448. }
  1449. ErrorOr<Optional<ReadonlyBytes>> JPEGXLImageDecoderPlugin::icc_data()
  1450. {
  1451. return OptionalNone {};
  1452. }
  1453. }