JPEGXLLoader.cpp 51 KB

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