JPEGLoader.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2022-2023, Lucas Chollet <lucas.chollet@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/Error.h>
  10. #include <AK/FixedArray.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/Math.h>
  13. #include <AK/MemoryStream.h>
  14. #include <AK/NumericLimits.h>
  15. #include <AK/String.h>
  16. #include <AK/Try.h>
  17. #include <AK/Vector.h>
  18. #include <LibGfx/ImageFormats/JPEGLoader.h>
  19. // These names are defined in B.1.1.3 - Marker assignments
  20. #define JPEG_APPN0 0XFFE0
  21. #define JPEG_APPN1 0XFFE1
  22. #define JPEG_APPN2 0XFFE2
  23. #define JPEG_APPN3 0XFFE3
  24. #define JPEG_APPN4 0XFFE4
  25. #define JPEG_APPN5 0XFFE5
  26. #define JPEG_APPN6 0XFFE6
  27. #define JPEG_APPN7 0XFFE7
  28. #define JPEG_APPN8 0XFFE8
  29. #define JPEG_APPN9 0XFFE9
  30. #define JPEG_APPN10 0XFFEA
  31. #define JPEG_APPN11 0XFFEB
  32. #define JPEG_APPN12 0XFFEC
  33. #define JPEG_APPN13 0XFFED
  34. #define JPEG_APPN14 0xFFEE
  35. #define JPEG_APPN15 0xFFEF
  36. #define JPEG_RESERVED1 0xFFF1
  37. #define JPEG_RESERVED2 0xFFF2
  38. #define JPEG_RESERVED3 0xFFF3
  39. #define JPEG_RESERVED4 0xFFF4
  40. #define JPEG_RESERVED5 0xFFF5
  41. #define JPEG_RESERVED6 0xFFF6
  42. #define JPEG_RESERVED7 0xFFF7
  43. #define JPEG_RESERVED8 0xFFF8
  44. #define JPEG_RESERVED9 0xFFF9
  45. #define JPEG_RESERVEDA 0xFFFA
  46. #define JPEG_RESERVEDB 0xFFFB
  47. #define JPEG_RESERVEDC 0xFFFC
  48. #define JPEG_RESERVEDD 0xFFFD
  49. #define JPEG_RST0 0xFFD0
  50. #define JPEG_RST1 0xFFD1
  51. #define JPEG_RST2 0xFFD2
  52. #define JPEG_RST3 0xFFD3
  53. #define JPEG_RST4 0xFFD4
  54. #define JPEG_RST5 0xFFD5
  55. #define JPEG_RST6 0xFFD6
  56. #define JPEG_RST7 0xFFD7
  57. #define JPEG_ZRL 0xF0
  58. #define JPEG_DHP 0xFFDE
  59. #define JPEG_EXP 0xFFDF
  60. #define JPEG_DAC 0XFFCC
  61. #define JPEG_DHT 0XFFC4
  62. #define JPEG_DQT 0XFFDB
  63. #define JPEG_EOI 0xFFD9
  64. #define JPEG_DRI 0XFFDD
  65. #define JPEG_SOF0 0XFFC0
  66. #define JPEG_SOF1 0XFFC1
  67. #define JPEG_SOF2 0xFFC2
  68. #define JPEG_SOF15 0xFFCF
  69. #define JPEG_SOI 0XFFD8
  70. #define JPEG_SOS 0XFFDA
  71. #define JPEG_COM 0xFFFE
  72. namespace Gfx {
  73. constexpr static u8 zigzag_map[64] {
  74. 0, 1, 8, 16, 9, 2, 3, 10,
  75. 17, 24, 32, 25, 18, 11, 4, 5,
  76. 12, 19, 26, 33, 40, 48, 41, 34,
  77. 27, 20, 13, 6, 7, 14, 21, 28,
  78. 35, 42, 49, 56, 57, 50, 43, 36,
  79. 29, 22, 15, 23, 30, 37, 44, 51,
  80. 58, 59, 52, 45, 38, 31, 39, 46,
  81. 53, 60, 61, 54, 47, 55, 62, 63
  82. };
  83. using Marker = u16;
  84. /**
  85. * MCU means group of data units that are coded together. A data unit is an 8x8
  86. * block of component data. In interleaved scans, number of non-interleaved data
  87. * units of a component C is Ch * Cv, where Ch and Cv represent the horizontal &
  88. * vertical subsampling factors of the component, respectively. A MacroBlock is
  89. * an 8x8 block of RGB values before encoding, and 8x8 block of YCbCr values when
  90. * we're done decoding the huffman stream.
  91. */
  92. struct Macroblock {
  93. union {
  94. i16 y[64] = { 0 };
  95. i16 r[64];
  96. };
  97. union {
  98. i16 cb[64] = { 0 };
  99. i16 g[64];
  100. };
  101. union {
  102. i16 cr[64] = { 0 };
  103. i16 b[64];
  104. };
  105. i16 k[64] = { 0 };
  106. };
  107. struct MacroblockMeta {
  108. u32 total { 0 };
  109. u32 padded_total { 0 };
  110. u32 hcount { 0 };
  111. u32 vcount { 0 };
  112. u32 hpadded_count { 0 };
  113. u32 vpadded_count { 0 };
  114. };
  115. // In the JPEG format, components are defined first at the frame level, then
  116. // referenced in each scan and aggregated with scan-specific information. The
  117. // two following structs mimic this hierarchy.
  118. struct Component {
  119. // B.2.2 - Frame header syntax
  120. u8 id { 0 }; // Ci, Component identifier
  121. u8 hsample_factor { 1 }; // Hi, Horizontal sampling factor
  122. u8 vsample_factor { 1 }; // Vi, Vertical sampling factor
  123. u8 quantization_table_id { 0 }; // Tqi, Quantization table destination selector
  124. // The JPEG specification does not specify which component corresponds to
  125. // Y, Cb or Cr. This field (actually the index in the parent Vector) will
  126. // act as an authority to determine the *real* component.
  127. // Please note that this is implementation specific.
  128. u8 index { 0 };
  129. };
  130. struct ScanComponent {
  131. // B.2.3 - Scan header syntax
  132. Component& component;
  133. u8 dc_destination_id { 0 }; // Tdj, DC entropy coding table destination selector
  134. u8 ac_destination_id { 0 }; // Taj, AC entropy coding table destination selector
  135. };
  136. struct StartOfFrame {
  137. // Of these, only the first 3 are in mainstream use, and refers to SOF0-2.
  138. enum class FrameType {
  139. Baseline_DCT = 0,
  140. Extended_Sequential_DCT = 1,
  141. Progressive_DCT = 2,
  142. Sequential_Lossless = 3,
  143. Differential_Sequential_DCT = 5,
  144. Differential_Progressive_DCT = 6,
  145. Differential_Sequential_Lossless = 7,
  146. Extended_Sequential_DCT_Arithmetic = 9,
  147. Progressive_DCT_Arithmetic = 10,
  148. Sequential_Lossless_Arithmetic = 11,
  149. Differential_Sequential_DCT_Arithmetic = 13,
  150. Differential_Progressive_DCT_Arithmetic = 14,
  151. Differential_Sequential_Lossless_Arithmetic = 15,
  152. };
  153. FrameType type { FrameType::Baseline_DCT };
  154. u8 precision { 0 };
  155. u16 height { 0 };
  156. u16 width { 0 };
  157. };
  158. struct HuffmanTable {
  159. u8 type { 0 };
  160. u8 destination_id { 0 };
  161. u8 code_counts[16] = { 0 };
  162. Vector<u8> symbols;
  163. Vector<u16> codes;
  164. // Note: The value 8 is chosen quite arbitrarily, the only current constraint
  165. // is that both the symbol and the size fit in an u16. I've tested more
  166. // values but none stand out, and 8 is the value used by libjpeg-turbo.
  167. static constexpr u8 bits_per_cached_code = 8;
  168. static constexpr u8 maximum_bits_per_code = 16;
  169. u8 first_non_cached_code_index {};
  170. void generate_codes()
  171. {
  172. unsigned code = 0;
  173. for (auto number_of_codes : code_counts) {
  174. for (int i = 0; i < number_of_codes; i++)
  175. codes.append(code++);
  176. code <<= 1;
  177. }
  178. generate_lookup_table();
  179. }
  180. struct SymbolAndSize {
  181. u8 symbol {};
  182. u8 size {};
  183. };
  184. ErrorOr<SymbolAndSize> symbol_from_code(u16 code) const
  185. {
  186. static constexpr u8 shift_for_cache = maximum_bits_per_code - bits_per_cached_code;
  187. if (lookup_table[code >> shift_for_cache] != invalid_entry) {
  188. u8 const code_length = lookup_table[code >> shift_for_cache] >> bits_per_cached_code;
  189. return SymbolAndSize { static_cast<u8>(lookup_table[code >> shift_for_cache]), code_length };
  190. }
  191. u64 code_cursor = first_non_cached_code_index;
  192. for (u8 i = HuffmanTable::bits_per_cached_code; i < 16; i++) {
  193. auto const result = code >> (maximum_bits_per_code - 1 - i);
  194. for (u32 j = 0; j < code_counts[i]; j++) {
  195. if (result == codes[code_cursor])
  196. return SymbolAndSize { symbols[code_cursor], static_cast<u8>(i + 1) };
  197. code_cursor++;
  198. }
  199. }
  200. return Error::from_string_literal("This kind of JPEG is not yet supported by the decoder");
  201. }
  202. private:
  203. static constexpr u16 invalid_entry = 0xFF;
  204. void generate_lookup_table()
  205. {
  206. lookup_table.fill(invalid_entry);
  207. u32 code_offset = 0;
  208. for (u8 code_length = 1; code_length <= bits_per_cached_code; code_length++) {
  209. for (u32 i = 0; i < code_counts[code_length - 1]; i++, code_offset++) {
  210. u32 code_key = codes[code_offset] << (bits_per_cached_code - code_length);
  211. for (u8 duplicate_count = 1 << (bits_per_cached_code - code_length); duplicate_count > 0; duplicate_count--) {
  212. lookup_table[code_key] = (code_length << bits_per_cached_code) | symbols[code_offset];
  213. code_key++;
  214. }
  215. }
  216. }
  217. }
  218. Array<u16, 1 << bits_per_cached_code> lookup_table {};
  219. };
  220. class HuffmanStream;
  221. class JPEGStream {
  222. public:
  223. static ErrorOr<JPEGStream> create(NonnullOwnPtr<Stream> stream)
  224. {
  225. Vector<u8> buffer;
  226. TRY(buffer.try_resize(buffer_size));
  227. JPEGStream jpeg_stream { move(stream), move(buffer) };
  228. TRY(jpeg_stream.refill_buffer());
  229. return jpeg_stream;
  230. }
  231. ErrorOr<u8> read_u8()
  232. {
  233. if (m_byte_offset == m_current_size)
  234. TRY(refill_buffer());
  235. return m_buffer[m_byte_offset++];
  236. }
  237. ErrorOr<u16> read_u16()
  238. {
  239. if (m_saved_marker.has_value())
  240. return m_saved_marker.release_value();
  241. return (static_cast<u16>(TRY(read_u8())) << 8) | TRY(read_u8());
  242. }
  243. ErrorOr<void> discard(u64 bytes)
  244. {
  245. auto const discarded_from_buffer = min(m_current_size - m_byte_offset, bytes);
  246. m_byte_offset += discarded_from_buffer;
  247. if (discarded_from_buffer < bytes)
  248. TRY(m_stream->discard(bytes - discarded_from_buffer));
  249. return {};
  250. }
  251. ErrorOr<void> read_until_filled(Bytes bytes)
  252. {
  253. auto const copied = m_buffer.span().slice(m_byte_offset).copy_trimmed_to(bytes);
  254. m_byte_offset += copied;
  255. if (copied < bytes.size())
  256. TRY(m_stream->read_until_filled(bytes.slice(copied)));
  257. return {};
  258. }
  259. Optional<u16>& saved_marker(Badge<HuffmanStream>)
  260. {
  261. return m_saved_marker;
  262. }
  263. u64 byte_offset() const
  264. {
  265. return m_byte_offset;
  266. }
  267. private:
  268. JPEGStream(NonnullOwnPtr<Stream> stream, Vector<u8> buffer)
  269. : m_stream(move(stream))
  270. , m_buffer(move(buffer))
  271. {
  272. }
  273. ErrorOr<void> refill_buffer()
  274. {
  275. VERIFY(m_byte_offset == m_current_size);
  276. m_current_size = TRY(m_stream->read_some(m_buffer.span())).size();
  277. m_byte_offset = 0;
  278. return {};
  279. }
  280. static constexpr auto buffer_size = 4096;
  281. NonnullOwnPtr<Stream> m_stream;
  282. Optional<u16> m_saved_marker {};
  283. Vector<u8> m_buffer {};
  284. u64 m_byte_offset { buffer_size };
  285. u64 m_current_size { buffer_size };
  286. };
  287. class HuffmanStream {
  288. public:
  289. ErrorOr<u8> next_symbol(HuffmanTable const& table)
  290. {
  291. u16 const code = TRY(peek_bits(HuffmanTable::maximum_bits_per_code));
  292. auto const symbol_and_size = TRY(table.symbol_from_code(code));
  293. TRY(discard_bits(symbol_and_size.size));
  294. return symbol_and_size.symbol;
  295. }
  296. ErrorOr<u16> read_bits(u8 count = 1)
  297. {
  298. if (count > NumericLimits<u16>::digits()) {
  299. dbgln_if(JPEG_DEBUG, "Can't read {} bits at once!", count);
  300. return Error::from_string_literal("Reading too much huffman bits at once");
  301. }
  302. u16 const value = TRY(peek_bits(count));
  303. TRY(discard_bits(count));
  304. return value;
  305. }
  306. ErrorOr<u16> peek_bits(u8 count)
  307. {
  308. if (count == 0)
  309. return 0;
  310. if (count + m_bit_offset > bits_in_reservoir)
  311. TRY(refill_reservoir());
  312. auto const mask = NumericLimits<u16>::max() >> (NumericLimits<u16>::digits() - count);
  313. return static_cast<u16>((m_bit_reservoir >> (bits_in_reservoir - m_bit_offset - count)) & mask);
  314. }
  315. ErrorOr<void> discard_bits(u8 count)
  316. {
  317. m_bit_offset += count;
  318. if (m_bit_offset > bits_in_reservoir) {
  319. // FIXME: I can't find a test case for that so let's leave it for later
  320. // instead of inserting an hard-to-find bug.
  321. TODO();
  322. }
  323. return {};
  324. }
  325. ErrorOr<void> advance_to_byte_boundary()
  326. {
  327. if (auto remainder = m_bit_offset % 8; remainder != 0)
  328. TRY(discard_bits(bits_per_byte - remainder));
  329. return {};
  330. }
  331. HuffmanStream(JPEGStream& stream)
  332. : jpeg_stream(stream)
  333. {
  334. }
  335. private:
  336. ErrorOr<void> refill_reservoir()
  337. {
  338. auto const bytes_needed = m_bit_offset / bits_per_byte;
  339. u8 bytes_added {};
  340. auto const append_byte = [&](u8 byte) {
  341. m_last_byte_was_ff = false;
  342. m_bit_reservoir <<= 8;
  343. m_bit_reservoir |= byte;
  344. m_bit_offset -= 8;
  345. bytes_added++;
  346. };
  347. do {
  348. // Note: We fake zeroes when we have reached another segment
  349. // It allows us to continue peeking seamlessly.
  350. u8 const next_byte = jpeg_stream.saved_marker({}).has_value() ? 0 : TRY(jpeg_stream.read_u8());
  351. if (m_last_byte_was_ff) {
  352. if (next_byte == 0xFF)
  353. continue;
  354. if (next_byte == 0x00) {
  355. append_byte(0xFF);
  356. continue;
  357. }
  358. Marker const marker = 0xFF00 | next_byte;
  359. if (marker < JPEG_RST0 || marker > JPEG_RST7) {
  360. // Note: The only way to know that we reached the end of a segment is to read
  361. // the marker of the following one. So we store it for later use.
  362. jpeg_stream.saved_marker({}) = marker;
  363. m_last_byte_was_ff = false;
  364. continue;
  365. }
  366. }
  367. if (next_byte == 0xFF) {
  368. m_last_byte_was_ff = true;
  369. continue;
  370. }
  371. append_byte(next_byte);
  372. } while (bytes_added < bytes_needed);
  373. return {};
  374. }
  375. JPEGStream& jpeg_stream;
  376. using Reservoir = u64;
  377. static constexpr auto bits_per_byte = 8;
  378. static constexpr auto bits_in_reservoir = sizeof(Reservoir) * bits_per_byte;
  379. Reservoir m_bit_reservoir {};
  380. u8 m_bit_offset { bits_in_reservoir };
  381. bool m_last_byte_was_ff { false };
  382. };
  383. struct ICCMultiChunkState {
  384. u8 seen_number_of_icc_chunks { 0 };
  385. FixedArray<ByteBuffer> chunks;
  386. };
  387. struct Scan {
  388. Scan(HuffmanStream stream)
  389. : huffman_stream(stream)
  390. {
  391. }
  392. // B.2.3 - Scan header syntax
  393. Vector<ScanComponent, 4> components;
  394. u8 spectral_selection_start {}; // Ss
  395. u8 spectral_selection_end {}; // Se
  396. u8 successive_approximation_high {}; // Ah
  397. u8 successive_approximation_low {}; // Al
  398. HuffmanStream huffman_stream;
  399. u64 end_of_bands_run_count { 0 };
  400. // See the note on Figure B.4 - Scan header syntax
  401. bool are_components_interleaved() const
  402. {
  403. return components.size() != 1;
  404. }
  405. };
  406. enum class ColorTransform {
  407. // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.872-201206-I!!PDF-E&type=items
  408. // 6.5.3 - APP14 marker segment for colour encoding
  409. CmykOrRgb = 0,
  410. YCbCr = 1,
  411. YCCK = 2,
  412. };
  413. struct JPEGLoadingContext {
  414. JPEGLoadingContext(JPEGStream jpeg_stream)
  415. : stream(move(jpeg_stream))
  416. {
  417. }
  418. static ErrorOr<NonnullOwnPtr<JPEGLoadingContext>> create(NonnullOwnPtr<Stream> stream)
  419. {
  420. auto jpeg_stream = TRY(JPEGStream::create(move(stream)));
  421. return make<JPEGLoadingContext>(move(jpeg_stream));
  422. }
  423. enum State {
  424. NotDecoded = 0,
  425. Error,
  426. FrameDecoded,
  427. HeaderDecoded,
  428. BitmapDecoded
  429. };
  430. State state { State::NotDecoded };
  431. Array<Optional<Array<u16, 64>>, 4> quantization_tables {};
  432. StartOfFrame frame;
  433. u8 hsample_factor { 0 };
  434. u8 vsample_factor { 0 };
  435. Optional<Scan> current_scan {};
  436. Vector<Component, 4> components;
  437. RefPtr<Gfx::Bitmap> bitmap;
  438. u16 dc_restart_interval { 0 };
  439. HashMap<u8, HuffmanTable> dc_tables;
  440. HashMap<u8, HuffmanTable> ac_tables;
  441. Array<i16, 4> previous_dc_values {};
  442. MacroblockMeta mblock_meta;
  443. JPEGStream stream;
  444. Optional<ColorTransform> color_transform {};
  445. Optional<ICCMultiChunkState> icc_multi_chunk_state;
  446. Optional<ByteBuffer> icc_data;
  447. };
  448. static inline auto* get_component(Macroblock& block, unsigned component)
  449. {
  450. switch (component) {
  451. case 0:
  452. return block.y;
  453. case 1:
  454. return block.cb;
  455. case 2:
  456. return block.cr;
  457. case 3:
  458. return block.k;
  459. default:
  460. VERIFY_NOT_REACHED();
  461. }
  462. }
  463. static ErrorOr<void> refine_coefficient(Scan& scan, auto& coefficient)
  464. {
  465. // G.1.2.3 - Coding model for subsequent scans of successive approximation
  466. // See the correction bit from rule b.
  467. u8 const bit = TRY(scan.huffman_stream.read_bits(1));
  468. if (bit == 1)
  469. coefficient |= 1 << scan.successive_approximation_low;
  470. return {};
  471. }
  472. static ErrorOr<void> add_dc(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
  473. {
  474. auto maybe_table = context.dc_tables.get(scan_component.dc_destination_id);
  475. if (!maybe_table.has_value()) {
  476. dbgln_if(JPEG_DEBUG, "Unable to find a DC table with id: {}", scan_component.dc_destination_id);
  477. return Error::from_string_literal("Unable to find corresponding DC table");
  478. }
  479. auto& dc_table = maybe_table.value();
  480. auto& scan = *context.current_scan;
  481. auto* select_component = get_component(macroblock, scan_component.component.index);
  482. auto& coefficient = select_component[0];
  483. if (scan.successive_approximation_high > 0) {
  484. TRY(refine_coefficient(scan, coefficient));
  485. return {};
  486. }
  487. // For DC coefficients, symbol encodes the length of the coefficient.
  488. auto dc_length = TRY(scan.huffman_stream.next_symbol(dc_table));
  489. // F.1.2.1.2 - Defining Huffman tables for the DC coefficients
  490. // F.1.5.1 - Structure of DC code table for 12-bit sample precision
  491. if ((context.frame.precision == 8 && dc_length > 11)
  492. || (context.frame.precision == 12 && dc_length > 15)) {
  493. dbgln_if(JPEG_DEBUG, "DC coefficient too long: {}!", dc_length);
  494. return Error::from_string_literal("DC coefficient too long");
  495. }
  496. // DC coefficients are encoded as the difference between previous and current DC values.
  497. i16 dc_diff = TRY(scan.huffman_stream.read_bits(dc_length));
  498. // If MSB in diff is 0, the difference is -ve. Otherwise +ve.
  499. if (dc_length != 0 && dc_diff < (1 << (dc_length - 1)))
  500. dc_diff -= (1 << dc_length) - 1;
  501. auto& previous_dc = context.previous_dc_values[scan_component.component.index];
  502. previous_dc += dc_diff;
  503. coefficient = previous_dc << scan.successive_approximation_low;
  504. return {};
  505. }
  506. static ErrorOr<bool> read_eob(Scan& scan, u32 symbol)
  507. {
  508. // G.1.2.2 - Progressive encoding of AC coefficients with Huffman coding
  509. // Note: We also use it for non-progressive encoding as it supports both EOB and ZRL
  510. if (auto const eob = symbol & 0x0F; eob == 0 && symbol != JPEG_ZRL) {
  511. // We encountered an EOB marker
  512. auto const eob_base = symbol >> 4;
  513. auto const additional_value = TRY(scan.huffman_stream.read_bits(eob_base));
  514. scan.end_of_bands_run_count = additional_value + (1 << eob_base) - 1;
  515. // end_of_bands_run_count is decremented at the end of `build_macroblocks`.
  516. // And we need to now that we reached End of Block in `add_ac`.
  517. ++scan.end_of_bands_run_count;
  518. return true;
  519. }
  520. return false;
  521. }
  522. static bool is_progressive(StartOfFrame::FrameType frame_type)
  523. {
  524. return frame_type == StartOfFrame::FrameType::Progressive_DCT
  525. || frame_type == StartOfFrame::FrameType::Progressive_DCT_Arithmetic
  526. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT
  527. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT_Arithmetic;
  528. }
  529. static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
  530. {
  531. auto maybe_table = context.ac_tables.get(scan_component.ac_destination_id);
  532. if (!maybe_table.has_value()) {
  533. dbgln_if(JPEG_DEBUG, "Unable to find a AC table with id: {}", scan_component.ac_destination_id);
  534. return Error::from_string_literal("Unable to find corresponding AC table");
  535. }
  536. auto& ac_table = maybe_table.value();
  537. auto* select_component = get_component(macroblock, scan_component.component.index);
  538. auto& scan = *context.current_scan;
  539. // Compute the AC coefficients.
  540. // 0th coefficient is the dc, which is already handled
  541. auto first_coefficient = max(1, scan.spectral_selection_start);
  542. u32 to_skip = 0;
  543. Optional<u8> saved_symbol;
  544. Optional<u8> saved_bit_for_rule_a;
  545. bool in_zrl = false;
  546. for (int j = first_coefficient; j <= scan.spectral_selection_end; ++j) {
  547. auto& coefficient = select_component[zigzag_map[j]];
  548. // AC symbols encode 2 pieces of information, the high 4 bits represent
  549. // number of zeroes to be stuffed before reading the coefficient. Low 4
  550. // bits represent the magnitude of the coefficient.
  551. if (!in_zrl && scan.end_of_bands_run_count == 0 && !saved_symbol.has_value()) {
  552. saved_symbol = TRY(scan.huffman_stream.next_symbol(ac_table));
  553. if (!TRY(read_eob(scan, *saved_symbol))) {
  554. to_skip = *saved_symbol >> 4;
  555. in_zrl = *saved_symbol == JPEG_ZRL;
  556. if (in_zrl) {
  557. to_skip++;
  558. saved_symbol.clear();
  559. }
  560. if (!in_zrl && is_progressive(context.frame.type) && scan.successive_approximation_high != 0) {
  561. // G.1.2.3 - Coding model for subsequent scans of successive approximation
  562. // Bit sign from rule a
  563. saved_bit_for_rule_a = TRY(scan.huffman_stream.read_bits(1));
  564. }
  565. }
  566. }
  567. if (coefficient != 0) {
  568. TRY(refine_coefficient(scan, coefficient));
  569. continue;
  570. }
  571. if (to_skip > 0) {
  572. --to_skip;
  573. if (to_skip == 0)
  574. in_zrl = false;
  575. continue;
  576. }
  577. if (scan.end_of_bands_run_count > 0)
  578. continue;
  579. if (is_progressive(context.frame.type) && scan.successive_approximation_high != 0) {
  580. // G.1.2.3 - Coding model for subsequent scans of successive approximation
  581. if (auto const low_bits = *saved_symbol & 0x0F; low_bits != 1) {
  582. dbgln_if(JPEG_DEBUG, "AC coefficient low bits isn't equal to 1: {}!", low_bits);
  583. return Error::from_string_literal("AC coefficient low bits isn't equal to 1");
  584. }
  585. coefficient = (*saved_bit_for_rule_a == 0 ? -1 : 1) << scan.successive_approximation_low;
  586. saved_bit_for_rule_a.clear();
  587. } else {
  588. // F.1.2.2 - Huffman encoding of AC coefficients
  589. u8 const coeff_length = *saved_symbol & 0x0F;
  590. // F.1.2.2.1 - Structure of AC code table
  591. // F.1.5.2 - Structure of AC code table for 12-bit sample precision
  592. if ((context.frame.precision == 8 && coeff_length > 10)
  593. || (context.frame.precision == 12 && coeff_length > 14)) {
  594. dbgln_if(JPEG_DEBUG, "AC coefficient too long: {}!", coeff_length);
  595. return Error::from_string_literal("AC coefficient too long");
  596. }
  597. if (coeff_length != 0) {
  598. i32 ac_coefficient = TRY(scan.huffman_stream.read_bits(coeff_length));
  599. if (ac_coefficient < (1 << (coeff_length - 1)))
  600. ac_coefficient -= (1 << coeff_length) - 1;
  601. coefficient = ac_coefficient * (1 << scan.successive_approximation_low);
  602. }
  603. }
  604. saved_symbol.clear();
  605. }
  606. if (to_skip > 0) {
  607. dbgln_if(JPEG_DEBUG, "Run-length exceeded boundaries. Cursor: {}, Skipping: {}!", scan.spectral_selection_end + to_skip, to_skip);
  608. return Error::from_string_literal("Run-length exceeded boundaries");
  609. }
  610. return {};
  611. }
  612. /**
  613. * Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
  614. * Depending on the sampling factors, we may not see triples of y, cb, cr in that
  615. * order. If sample factors differ from one, we'll read more than one block of y-
  616. * coefficients before we get to read a cb-cr block.
  617. * In the function below, `hcursor` and `vcursor` denote the location of the block
  618. * we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
  619. * that iterate over the vertical and horizontal subsampling factors, respectively.
  620. * When we finish one iteration of the innermost loop, we'll have the coefficients
  621. * of one of the components of block at position `macroblock_index`. When the outermost
  622. * loop finishes first iteration, we'll have all the luminance coefficients for all the
  623. * macroblocks that share the chrominance data. Next two iterations (assuming that
  624. * we are dealing with three components) will fill up the blocks with chroma data.
  625. */
  626. static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
  627. {
  628. for (auto const& scan_component : context.current_scan->components) {
  629. for (u8 vfactor_i = 0; vfactor_i < scan_component.component.vsample_factor; vfactor_i++) {
  630. for (u8 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) {
  631. // A.2.3 - Interleaved order
  632. u32 macroblock_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  633. if (!context.current_scan->are_components_interleaved()) {
  634. macroblock_index = vcursor * context.mblock_meta.hpadded_count + (hfactor_i + (hcursor * scan_component.component.vsample_factor) + (vfactor_i * scan_component.component.hsample_factor));
  635. // A.2.4 Completion of partial MCU
  636. // If the component is [and only if!] to be interleaved, the encoding process
  637. // shall also extend the number of samples by one or more additional blocks.
  638. // Horizontally
  639. if (macroblock_index >= context.mblock_meta.hcount && macroblock_index % context.mblock_meta.hpadded_count >= context.mblock_meta.hcount)
  640. continue;
  641. // Vertically
  642. if (macroblock_index >= context.mblock_meta.hpadded_count * context.mblock_meta.vcount)
  643. continue;
  644. }
  645. Macroblock& block = macroblocks[macroblock_index];
  646. if (context.current_scan->spectral_selection_start == 0)
  647. TRY(add_dc(context, block, scan_component));
  648. if (context.current_scan->spectral_selection_end != 0)
  649. TRY(add_ac(context, block, scan_component));
  650. // G.1.2.2 - Progressive encoding of AC coefficients with Huffman coding
  651. if (context.current_scan->end_of_bands_run_count > 0) {
  652. --context.current_scan->end_of_bands_run_count;
  653. continue;
  654. }
  655. }
  656. }
  657. }
  658. return {};
  659. }
  660. static bool is_dct_based(StartOfFrame::FrameType frame_type)
  661. {
  662. return frame_type == StartOfFrame::FrameType::Baseline_DCT
  663. || frame_type == StartOfFrame::FrameType::Extended_Sequential_DCT
  664. || frame_type == StartOfFrame::FrameType::Progressive_DCT
  665. || frame_type == StartOfFrame::FrameType::Differential_Sequential_DCT
  666. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT
  667. || frame_type == StartOfFrame::FrameType::Progressive_DCT_Arithmetic
  668. || frame_type == StartOfFrame::FrameType::Differential_Sequential_DCT_Arithmetic
  669. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT_Arithmetic;
  670. }
  671. static void reset_decoder(JPEGLoadingContext& context)
  672. {
  673. // G.1.2.2 - Progressive encoding of AC coefficients with Huffman coding
  674. context.current_scan->end_of_bands_run_count = 0;
  675. // E.2.4 Control procedure for decoding a restart interval
  676. if (is_dct_based(context.frame.type)) {
  677. context.previous_dc_values = {};
  678. return;
  679. }
  680. VERIFY_NOT_REACHED();
  681. }
  682. static ErrorOr<void> decode_huffman_stream(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks)
  683. {
  684. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  685. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  686. u32 i = vcursor * context.mblock_meta.hpadded_count + hcursor;
  687. auto& huffman_stream = context.current_scan->huffman_stream;
  688. if (context.dc_restart_interval > 0) {
  689. if (i != 0 && i % (context.dc_restart_interval * context.vsample_factor * context.hsample_factor) == 0) {
  690. reset_decoder(context);
  691. // Restart markers are stored in byte boundaries. Advance the huffman stream cursor to
  692. // the 0th bit of the next byte.
  693. TRY(huffman_stream.advance_to_byte_boundary());
  694. // Skip the restart marker (RSTn).
  695. TRY(huffman_stream.discard_bits(8));
  696. }
  697. }
  698. if (auto result = build_macroblocks(context, macroblocks, hcursor, vcursor); result.is_error()) {
  699. if constexpr (JPEG_DEBUG) {
  700. dbgln("Failed to build Macroblock {}: {}", i, result.error());
  701. dbgln("Huffman stream byte offset {}", context.stream.byte_offset());
  702. }
  703. return result.release_error();
  704. }
  705. }
  706. }
  707. return {};
  708. }
  709. static bool is_frame_marker(Marker const marker)
  710. {
  711. // B.1.1.3 - Marker assignments
  712. bool const is_sof_marker = marker >= JPEG_SOF0 && marker <= JPEG_SOF15;
  713. // Start of frame markers are valid for JPEG_SOF0 to JPEG_SOF15 except number 4, 8 (reserved) and 12.
  714. bool const is_defined_marker = marker != JPEG_DHT && marker != 0xFFC8 && marker != JPEG_DAC;
  715. return is_sof_marker && is_defined_marker;
  716. }
  717. static inline bool is_supported_marker(Marker const marker)
  718. {
  719. if (marker >= JPEG_APPN0 && marker <= JPEG_APPN15) {
  720. if (marker != JPEG_APPN0 && marker != JPEG_APPN14)
  721. dbgln_if(JPEG_DEBUG, "{:#04x} not supported yet. The decoder may fail!", marker);
  722. return true;
  723. }
  724. if (marker >= JPEG_RESERVED1 && marker <= JPEG_RESERVEDD)
  725. return true;
  726. if (marker >= JPEG_RST0 && marker <= JPEG_RST7)
  727. return true;
  728. switch (marker) {
  729. case JPEG_COM:
  730. case JPEG_DHP:
  731. case JPEG_EXP:
  732. case JPEG_DHT:
  733. case JPEG_DQT:
  734. case JPEG_DRI:
  735. case JPEG_EOI:
  736. case JPEG_SOF0:
  737. case JPEG_SOF1:
  738. case JPEG_SOF2:
  739. case JPEG_SOI:
  740. case JPEG_SOS:
  741. return true;
  742. }
  743. if (is_frame_marker(marker))
  744. dbgln_if(JPEG_DEBUG, "Decoding this frame-type (SOF{}) is not currently supported. Decoder will fail!", marker & 0xf);
  745. return false;
  746. }
  747. static inline ErrorOr<Marker> read_marker_at_cursor(JPEGStream& stream)
  748. {
  749. u16 marker = TRY(stream.read_u16());
  750. if (marker == 0xFFFF) {
  751. u8 next { 0xFF };
  752. while (next == 0xFF)
  753. next = TRY(stream.read_u8());
  754. marker = 0xFF00 | next;
  755. }
  756. if (is_supported_marker(marker))
  757. return marker;
  758. return Error::from_string_literal("Reached an unsupported marker");
  759. }
  760. static ErrorOr<u16> read_effective_chunk_size(JPEGStream& stream)
  761. {
  762. // The stored chunk size includes the size of `stored_size` itself.
  763. u16 const stored_size = TRY(stream.read_u16());
  764. if (stored_size < 2)
  765. return Error::from_string_literal("Stored chunk size is too small");
  766. return stored_size - 2;
  767. }
  768. static ErrorOr<void> read_start_of_scan(JPEGStream& stream, JPEGLoadingContext& context)
  769. {
  770. // B.2.3 - Scan header syntax
  771. if (context.state < JPEGLoadingContext::State::FrameDecoded)
  772. return Error::from_string_literal("SOS found before reading a SOF");
  773. [[maybe_unused]] u16 const bytes_to_read = TRY(read_effective_chunk_size(stream));
  774. u8 const component_count = TRY(stream.read_u8());
  775. Scan current_scan(HuffmanStream { context.stream });
  776. Optional<u8> last_read;
  777. u8 component_read = 0;
  778. for (auto& component : context.components) {
  779. // See the Csj paragraph:
  780. // [...] the ordering in the scan header shall follow the ordering in the frame header.
  781. if (component_read == component_count)
  782. break;
  783. if (!last_read.has_value())
  784. last_read = TRY(stream.read_u8());
  785. if (component.id != *last_read)
  786. continue;
  787. u8 const table_ids = TRY(stream.read_u8());
  788. current_scan.components.empend(component, static_cast<u8>(table_ids >> 4), static_cast<u8>(table_ids & 0x0F));
  789. component_read++;
  790. last_read.clear();
  791. }
  792. if constexpr (JPEG_DEBUG) {
  793. StringBuilder builder;
  794. TRY(builder.try_append("Components in scan: "sv));
  795. for (auto const& scan_component : current_scan.components) {
  796. TRY(builder.try_append(TRY(String::number(scan_component.component.id))));
  797. TRY(builder.try_append(' '));
  798. }
  799. dbgln(builder.string_view());
  800. }
  801. current_scan.spectral_selection_start = TRY(stream.read_u8());
  802. current_scan.spectral_selection_end = TRY(stream.read_u8());
  803. auto const successive_approximation = TRY(stream.read_u8());
  804. current_scan.successive_approximation_high = successive_approximation >> 4;
  805. current_scan.successive_approximation_low = successive_approximation & 0x0F;
  806. dbgln_if(JPEG_DEBUG, "Start of Selection: {}, End of Selection: {}, Successive Approximation High: {}, Successive Approximation Low: {}",
  807. current_scan.spectral_selection_start,
  808. current_scan.spectral_selection_end,
  809. current_scan.successive_approximation_high,
  810. current_scan.successive_approximation_low);
  811. if (current_scan.spectral_selection_start > 63 || current_scan.spectral_selection_end > 63 || current_scan.successive_approximation_high > 13 || current_scan.successive_approximation_low > 13) {
  812. dbgln_if(JPEG_DEBUG, "ERROR! Start of Selection: {}, End of Selection: {}, Successive Approximation High: {}, Successive Approximation Low: {}!",
  813. current_scan.spectral_selection_start,
  814. current_scan.spectral_selection_end,
  815. current_scan.successive_approximation_high,
  816. current_scan.successive_approximation_low);
  817. return Error::from_string_literal("Spectral selection is not [0,63] or successive approximation is not null");
  818. }
  819. context.current_scan = move(current_scan);
  820. return {};
  821. }
  822. static ErrorOr<void> read_restart_interval(JPEGStream& stream, JPEGLoadingContext& context)
  823. {
  824. // B.2.4.4 - Restart interval definition syntax
  825. u16 bytes_to_read = TRY(read_effective_chunk_size(stream));
  826. if (bytes_to_read != 2) {
  827. dbgln_if(JPEG_DEBUG, "Malformed DRI marker found!");
  828. return Error::from_string_literal("Malformed DRI marker found");
  829. }
  830. context.dc_restart_interval = TRY(stream.read_u16());
  831. return {};
  832. }
  833. static ErrorOr<void> read_huffman_table(JPEGStream& stream, JPEGLoadingContext& context)
  834. {
  835. // B.2.4.2 - Huffman table-specification syntax
  836. u16 bytes_to_read = TRY(read_effective_chunk_size(stream));
  837. while (bytes_to_read > 0) {
  838. HuffmanTable table;
  839. u8 const table_info = TRY(stream.read_u8());
  840. u8 const table_type = table_info >> 4;
  841. u8 const table_destination_id = table_info & 0x0F;
  842. if (table_type > 1) {
  843. dbgln_if(JPEG_DEBUG, "Unrecognized huffman table: {}!", table_type);
  844. return Error::from_string_literal("Unrecognized huffman table");
  845. }
  846. if ((context.frame.type == StartOfFrame::FrameType::Baseline_DCT && table_destination_id > 1)
  847. || (context.frame.type != StartOfFrame::FrameType::Baseline_DCT && table_destination_id > 3)) {
  848. dbgln_if(JPEG_DEBUG, "Invalid huffman table destination id: {}!", table_destination_id);
  849. return Error::from_string_literal("Invalid huffman table destination id");
  850. }
  851. table.type = table_type;
  852. table.destination_id = table_destination_id;
  853. u32 total_codes = 0;
  854. // Read code counts. At each index K, the value represents the number of K+1 bit codes in this header.
  855. for (int i = 0; i < 16; i++) {
  856. if (i == HuffmanTable::bits_per_cached_code)
  857. table.first_non_cached_code_index = total_codes;
  858. u8 const count = TRY(stream.read_u8());
  859. total_codes += count;
  860. table.code_counts[i] = count;
  861. }
  862. table.codes.ensure_capacity(total_codes);
  863. table.symbols.ensure_capacity(total_codes);
  864. // Read symbols. Read X bytes, where X is the sum of the counts of codes read in the previous step.
  865. for (u32 i = 0; i < total_codes; i++) {
  866. u8 symbol = TRY(stream.read_u8());
  867. table.symbols.append(symbol);
  868. }
  869. table.generate_codes();
  870. auto& huffman_table = table.type == 0 ? context.dc_tables : context.ac_tables;
  871. huffman_table.set(table.destination_id, table);
  872. bytes_to_read -= 1 + 16 + total_codes;
  873. }
  874. if (bytes_to_read != 0) {
  875. dbgln_if(JPEG_DEBUG, "Extra bytes detected in huffman header!");
  876. return Error::from_string_literal("Extra bytes detected in huffman header");
  877. }
  878. return {};
  879. }
  880. static ErrorOr<void> read_icc_profile(JPEGStream& stream, JPEGLoadingContext& context, int bytes_to_read)
  881. {
  882. // https://www.color.org/technotes/ICC-Technote-ProfileEmbedding.pdf, page 5, "JFIF".
  883. if (bytes_to_read <= 2)
  884. return Error::from_string_literal("icc marker too small");
  885. auto chunk_sequence_number = TRY(stream.read_u8()); // 1-based
  886. auto number_of_chunks = TRY(stream.read_u8());
  887. bytes_to_read -= 2;
  888. if (!context.icc_multi_chunk_state.has_value())
  889. context.icc_multi_chunk_state.emplace(ICCMultiChunkState { 0, TRY(FixedArray<ByteBuffer>::create(number_of_chunks)) });
  890. auto& chunk_state = context.icc_multi_chunk_state;
  891. if (chunk_state->seen_number_of_icc_chunks >= number_of_chunks)
  892. return Error::from_string_literal("Too many ICC chunks");
  893. if (chunk_state->chunks.size() != number_of_chunks)
  894. return Error::from_string_literal("Inconsistent number of total ICC chunks");
  895. if (chunk_sequence_number == 0)
  896. return Error::from_string_literal("ICC chunk sequence number not 1 based");
  897. u8 index = chunk_sequence_number - 1;
  898. if (index >= chunk_state->chunks.size())
  899. return Error::from_string_literal("ICC chunk sequence number larger than number of chunks");
  900. if (!chunk_state->chunks[index].is_empty())
  901. return Error::from_string_literal("Duplicate ICC chunk at sequence number");
  902. chunk_state->chunks[index] = TRY(ByteBuffer::create_zeroed(bytes_to_read));
  903. TRY(stream.read_until_filled(chunk_state->chunks[index]));
  904. chunk_state->seen_number_of_icc_chunks++;
  905. if (chunk_state->seen_number_of_icc_chunks != chunk_state->chunks.size())
  906. return {};
  907. if (number_of_chunks == 1) {
  908. context.icc_data = move(chunk_state->chunks[0]);
  909. return {};
  910. }
  911. size_t total_size = 0;
  912. for (auto const& chunk : chunk_state->chunks)
  913. total_size += chunk.size();
  914. auto icc_bytes = TRY(ByteBuffer::create_zeroed(total_size));
  915. size_t start = 0;
  916. for (auto const& chunk : chunk_state->chunks) {
  917. memcpy(icc_bytes.data() + start, chunk.data(), chunk.size());
  918. start += chunk.size();
  919. }
  920. context.icc_data = move(icc_bytes);
  921. return {};
  922. }
  923. static ErrorOr<void> read_colour_encoding(JPEGStream& stream, [[maybe_unused]] JPEGLoadingContext& context, int bytes_to_read)
  924. {
  925. // The App 14 segment is application specific in the first JPEG standard.
  926. // However, the Adobe implementation is globally accepted and the value of the color transform
  927. // was latter standardized as a JPEG-1 extension.
  928. // For the structure of the App 14 segment, see:
  929. // https://www.pdfa.org/norm-refs/5116.DCT_Filter.pdf
  930. // 18 Adobe Application-Specific JPEG Marker
  931. // For the value of color_transform, see:
  932. // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.872-201206-I!!PDF-E&type=items
  933. // 6.5.3 - APP14 marker segment for colour encoding
  934. if (bytes_to_read < 6)
  935. return Error::from_string_literal("App14 segment too small");
  936. [[maybe_unused]] auto const version = TRY(stream.read_u8());
  937. [[maybe_unused]] u16 const flag0 = TRY(stream.read_u16());
  938. [[maybe_unused]] u16 const flag1 = TRY(stream.read_u16());
  939. auto const color_transform = TRY(stream.read_u8());
  940. if (bytes_to_read > 6) {
  941. dbgln_if(JPEG_DEBUG, "Unread bytes in App14 segment: {}", bytes_to_read - 6);
  942. TRY(stream.discard(bytes_to_read - 6));
  943. }
  944. switch (color_transform) {
  945. case 0:
  946. context.color_transform = ColorTransform::CmykOrRgb;
  947. break;
  948. case 1:
  949. context.color_transform = ColorTransform::YCbCr;
  950. break;
  951. case 2:
  952. context.color_transform = ColorTransform::YCCK;
  953. break;
  954. default:
  955. dbgln("0x{:x} is not a specified transform flag value, ignoring", color_transform);
  956. }
  957. return {};
  958. }
  959. static ErrorOr<void> read_app_marker(JPEGStream& stream, JPEGLoadingContext& context, int app_marker_number)
  960. {
  961. // B.2.4.6 - Application data syntax
  962. u16 bytes_to_read = TRY(read_effective_chunk_size(stream));
  963. StringBuilder builder;
  964. for (;;) {
  965. if (bytes_to_read == 0) {
  966. dbgln_if(JPEG_DEBUG, "app marker {} does not start with zero-terminated string", app_marker_number);
  967. return {};
  968. }
  969. auto c = TRY(stream.read_u8());
  970. bytes_to_read--;
  971. if (c == '\0')
  972. break;
  973. TRY(builder.try_append(c));
  974. }
  975. auto app_id = TRY(builder.to_string());
  976. if (app_marker_number == 2 && app_id == "ICC_PROFILE"sv)
  977. return read_icc_profile(stream, context, bytes_to_read);
  978. if (app_marker_number == 14 && app_id == "Adobe"sv)
  979. return read_colour_encoding(stream, context, bytes_to_read);
  980. return stream.discard(bytes_to_read);
  981. }
  982. static inline bool validate_luma_and_modify_context(Component const& luma, JPEGLoadingContext& context)
  983. {
  984. if ((luma.hsample_factor == 1 || luma.hsample_factor == 2) && (luma.vsample_factor == 1 || luma.vsample_factor == 2)) {
  985. context.mblock_meta.hpadded_count += luma.hsample_factor == 1 ? 0 : context.mblock_meta.hcount % 2;
  986. context.mblock_meta.vpadded_count += luma.vsample_factor == 1 ? 0 : context.mblock_meta.vcount % 2;
  987. context.mblock_meta.padded_total = context.mblock_meta.hpadded_count * context.mblock_meta.vpadded_count;
  988. // For easy reference to relevant sample factors.
  989. context.hsample_factor = luma.hsample_factor;
  990. context.vsample_factor = luma.vsample_factor;
  991. if constexpr (JPEG_DEBUG) {
  992. dbgln("Horizontal Subsampling Factor: {}", luma.hsample_factor);
  993. dbgln("Vertical Subsampling Factor: {}", luma.vsample_factor);
  994. }
  995. return true;
  996. }
  997. return false;
  998. }
  999. static inline void set_macroblock_metadata(JPEGLoadingContext& context)
  1000. {
  1001. context.mblock_meta.hcount = (context.frame.width + 7) / 8;
  1002. context.mblock_meta.vcount = (context.frame.height + 7) / 8;
  1003. context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
  1004. context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
  1005. context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
  1006. }
  1007. static ErrorOr<void> ensure_standard_precision(StartOfFrame const& frame)
  1008. {
  1009. // B.2.2 - Frame header syntax
  1010. // Table B.2 - Frame header parameter sizes and values
  1011. if (frame.precision == 8)
  1012. return {};
  1013. if (frame.type == StartOfFrame::FrameType::Extended_Sequential_DCT && frame.precision == 12)
  1014. return {};
  1015. if (frame.type == StartOfFrame::FrameType::Progressive_DCT && frame.precision == 12)
  1016. return {};
  1017. dbgln_if(JPEG_DEBUG, "Unsupported precision: {}, for SOF type: {}!", frame.precision, static_cast<int>(frame.type));
  1018. return Error::from_string_literal("Unsupported SOF precision.");
  1019. }
  1020. static ErrorOr<void> read_start_of_frame(JPEGStream& stream, JPEGLoadingContext& context)
  1021. {
  1022. if (context.state == JPEGLoadingContext::FrameDecoded) {
  1023. dbgln_if(JPEG_DEBUG, "SOF repeated!");
  1024. return Error::from_string_literal("SOF repeated");
  1025. }
  1026. [[maybe_unused]] u16 const bytes_to_read = TRY(read_effective_chunk_size(stream));
  1027. context.frame.precision = TRY(stream.read_u8());
  1028. TRY(ensure_standard_precision(context.frame));
  1029. context.frame.height = TRY(stream.read_u16());
  1030. context.frame.width = TRY(stream.read_u16());
  1031. if (!context.frame.width || !context.frame.height) {
  1032. dbgln_if(JPEG_DEBUG, "ERROR! Image height: {}, Image width: {}!", context.frame.height, context.frame.width);
  1033. return Error::from_string_literal("Image frame height of width null");
  1034. }
  1035. if (context.frame.width > maximum_width_for_decoded_images || context.frame.height > maximum_height_for_decoded_images) {
  1036. dbgln("This JPEG is too large for comfort: {}x{}", context.frame.width, context.frame.height);
  1037. return Error::from_string_literal("JPEG too large for comfort");
  1038. }
  1039. set_macroblock_metadata(context);
  1040. auto component_count = TRY(stream.read_u8());
  1041. if (component_count != 1 && component_count != 3 && component_count != 4) {
  1042. dbgln_if(JPEG_DEBUG, "Unsupported number of components in SOF: {}!", component_count);
  1043. return Error::from_string_literal("Unsupported number of components in SOF");
  1044. }
  1045. for (u8 i = 0; i < component_count; i++) {
  1046. Component component;
  1047. component.id = TRY(stream.read_u8());
  1048. component.index = i;
  1049. u8 subsample_factors = TRY(stream.read_u8());
  1050. component.hsample_factor = subsample_factors >> 4;
  1051. component.vsample_factor = subsample_factors & 0x0F;
  1052. if (i == 0) {
  1053. // By convention, downsampling is applied only on chroma components. So we should
  1054. // hope to see the maximum sampling factor in the luma component.
  1055. if (!validate_luma_and_modify_context(component, context)) {
  1056. dbgln_if(JPEG_DEBUG, "Unsupported luma subsampling factors: horizontal: {}, vertical: {}",
  1057. component.hsample_factor,
  1058. component.vsample_factor);
  1059. return Error::from_string_literal("Unsupported luma subsampling factors");
  1060. }
  1061. } else {
  1062. if (component.hsample_factor != 1 || component.vsample_factor != 1) {
  1063. dbgln_if(JPEG_DEBUG, "Unsupported chroma subsampling factors: horizontal: {}, vertical: {}",
  1064. component.hsample_factor,
  1065. component.vsample_factor);
  1066. return Error::from_string_literal("Unsupported chroma subsampling factors");
  1067. }
  1068. }
  1069. component.quantization_table_id = TRY(stream.read_u8());
  1070. context.components.append(move(component));
  1071. }
  1072. return {};
  1073. }
  1074. static ErrorOr<void> read_quantization_table(JPEGStream& stream, JPEGLoadingContext& context)
  1075. {
  1076. // B.2.4.1 - Quantization table-specification syntax
  1077. u16 bytes_to_read = TRY(read_effective_chunk_size(stream));
  1078. while (bytes_to_read > 0) {
  1079. u8 const info_byte = TRY(stream.read_u8());
  1080. u8 const element_unit_hint = info_byte >> 4;
  1081. if (element_unit_hint > 1) {
  1082. dbgln_if(JPEG_DEBUG, "Unsupported unit hint in quantization table: {}!", element_unit_hint);
  1083. return Error::from_string_literal("Unsupported unit hint in quantization table");
  1084. }
  1085. u8 const table_id = info_byte & 0x0F;
  1086. if (table_id > 3) {
  1087. dbgln_if(JPEG_DEBUG, "Unsupported quantization table id: {}!", table_id);
  1088. return Error::from_string_literal("Unsupported quantization table id");
  1089. }
  1090. auto& maybe_table = context.quantization_tables[table_id];
  1091. if (!maybe_table.has_value())
  1092. maybe_table = Array<u16, 64> {};
  1093. auto& table = maybe_table.value();
  1094. for (int i = 0; i < 64; i++) {
  1095. if (element_unit_hint == 0)
  1096. table[zigzag_map[i]] = TRY(stream.read_u8());
  1097. else
  1098. table[zigzag_map[i]] = TRY(stream.read_u16());
  1099. }
  1100. bytes_to_read -= 1 + (element_unit_hint == 0 ? 64 : 128);
  1101. }
  1102. if (bytes_to_read != 0) {
  1103. dbgln_if(JPEG_DEBUG, "Invalid length for one or more quantization tables!");
  1104. return Error::from_string_literal("Invalid length for one or more quantization tables");
  1105. }
  1106. return {};
  1107. }
  1108. static ErrorOr<void> skip_segment(JPEGStream& stream)
  1109. {
  1110. u16 bytes_to_skip = TRY(stream.read_u16()) - 2;
  1111. TRY(stream.discard(bytes_to_skip));
  1112. return {};
  1113. }
  1114. static ErrorOr<void> dequantize(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks)
  1115. {
  1116. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1117. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1118. for (u32 i = 0; i < context.components.size(); i++) {
  1119. auto const& component = context.components[i];
  1120. if (!context.quantization_tables[component.quantization_table_id].has_value()) {
  1121. dbgln_if(JPEG_DEBUG, "Unknown quantization table id: {}!", component.quantization_table_id);
  1122. return Error::from_string_literal("Unknown quantization table id");
  1123. }
  1124. auto const& table = context.quantization_tables[component.quantization_table_id].value();
  1125. for (u32 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  1126. for (u32 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  1127. u32 macroblock_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  1128. Macroblock& block = macroblocks[macroblock_index];
  1129. auto* block_component = get_component(block, i);
  1130. for (u32 k = 0; k < 64; k++)
  1131. block_component[k] *= table[k];
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. return {};
  1138. }
  1139. static void inverse_dct(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1140. {
  1141. static float const m0 = 2.0f * AK::cos(1.0f / 16.0f * 2.0f * AK::Pi<float>);
  1142. static float const m1 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  1143. static float const m3 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  1144. static float const m5 = 2.0f * AK::cos(3.0f / 16.0f * 2.0f * AK::Pi<float>);
  1145. static float const m2 = m0 - m5;
  1146. static float const m4 = m0 + m5;
  1147. static float const s0 = AK::cos(0.0f / 16.0f * AK::Pi<float>) * AK::rsqrt(8.0f);
  1148. static float const s1 = AK::cos(1.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1149. static float const s2 = AK::cos(2.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1150. static float const s3 = AK::cos(3.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1151. static float const s4 = AK::cos(4.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1152. static float const s5 = AK::cos(5.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1153. static float const s6 = AK::cos(6.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1154. static float const s7 = AK::cos(7.0f / 16.0f * AK::Pi<float>) / 2.0f;
  1155. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1156. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1157. for (u32 component_i = 0; component_i < context.components.size(); component_i++) {
  1158. auto& component = context.components[component_i];
  1159. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  1160. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  1161. u32 macroblock_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  1162. Macroblock& block = macroblocks[macroblock_index];
  1163. auto* block_component = get_component(block, component_i);
  1164. for (u32 k = 0; k < 8; ++k) {
  1165. float const g0 = block_component[0 * 8 + k] * s0;
  1166. float const g1 = block_component[4 * 8 + k] * s4;
  1167. float const g2 = block_component[2 * 8 + k] * s2;
  1168. float const g3 = block_component[6 * 8 + k] * s6;
  1169. float const g4 = block_component[5 * 8 + k] * s5;
  1170. float const g5 = block_component[1 * 8 + k] * s1;
  1171. float const g6 = block_component[7 * 8 + k] * s7;
  1172. float const g7 = block_component[3 * 8 + k] * s3;
  1173. float const f0 = g0;
  1174. float const f1 = g1;
  1175. float const f2 = g2;
  1176. float const f3 = g3;
  1177. float const f4 = g4 - g7;
  1178. float const f5 = g5 + g6;
  1179. float const f6 = g5 - g6;
  1180. float const f7 = g4 + g7;
  1181. float const e0 = f0;
  1182. float const e1 = f1;
  1183. float const e2 = f2 - f3;
  1184. float const e3 = f2 + f3;
  1185. float const e4 = f4;
  1186. float const e5 = f5 - f7;
  1187. float const e6 = f6;
  1188. float const e7 = f5 + f7;
  1189. float const e8 = f4 + f6;
  1190. float const d0 = e0;
  1191. float const d1 = e1;
  1192. float const d2 = e2 * m1;
  1193. float const d3 = e3;
  1194. float const d4 = e4 * m2;
  1195. float const d5 = e5 * m3;
  1196. float const d6 = e6 * m4;
  1197. float const d7 = e7;
  1198. float const d8 = e8 * m5;
  1199. float const c0 = d0 + d1;
  1200. float const c1 = d0 - d1;
  1201. float const c2 = d2 - d3;
  1202. float const c3 = d3;
  1203. float const c4 = d4 + d8;
  1204. float const c5 = d5 + d7;
  1205. float const c6 = d6 - d8;
  1206. float const c7 = d7;
  1207. float const c8 = c5 - c6;
  1208. float const b0 = c0 + c3;
  1209. float const b1 = c1 + c2;
  1210. float const b2 = c1 - c2;
  1211. float const b3 = c0 - c3;
  1212. float const b4 = c4 - c8;
  1213. float const b5 = c8;
  1214. float const b6 = c6 - c7;
  1215. float const b7 = c7;
  1216. block_component[0 * 8 + k] = b0 + b7;
  1217. block_component[1 * 8 + k] = b1 + b6;
  1218. block_component[2 * 8 + k] = b2 + b5;
  1219. block_component[3 * 8 + k] = b3 + b4;
  1220. block_component[4 * 8 + k] = b3 - b4;
  1221. block_component[5 * 8 + k] = b2 - b5;
  1222. block_component[6 * 8 + k] = b1 - b6;
  1223. block_component[7 * 8 + k] = b0 - b7;
  1224. }
  1225. for (u32 l = 0; l < 8; ++l) {
  1226. float const g0 = block_component[l * 8 + 0] * s0;
  1227. float const g1 = block_component[l * 8 + 4] * s4;
  1228. float const g2 = block_component[l * 8 + 2] * s2;
  1229. float const g3 = block_component[l * 8 + 6] * s6;
  1230. float const g4 = block_component[l * 8 + 5] * s5;
  1231. float const g5 = block_component[l * 8 + 1] * s1;
  1232. float const g6 = block_component[l * 8 + 7] * s7;
  1233. float const g7 = block_component[l * 8 + 3] * s3;
  1234. float const f0 = g0;
  1235. float const f1 = g1;
  1236. float const f2 = g2;
  1237. float const f3 = g3;
  1238. float const f4 = g4 - g7;
  1239. float const f5 = g5 + g6;
  1240. float const f6 = g5 - g6;
  1241. float const f7 = g4 + g7;
  1242. float const e0 = f0;
  1243. float const e1 = f1;
  1244. float const e2 = f2 - f3;
  1245. float const e3 = f2 + f3;
  1246. float const e4 = f4;
  1247. float const e5 = f5 - f7;
  1248. float const e6 = f6;
  1249. float const e7 = f5 + f7;
  1250. float const e8 = f4 + f6;
  1251. float const d0 = e0;
  1252. float const d1 = e1;
  1253. float const d2 = e2 * m1;
  1254. float const d3 = e3;
  1255. float const d4 = e4 * m2;
  1256. float const d5 = e5 * m3;
  1257. float const d6 = e6 * m4;
  1258. float const d7 = e7;
  1259. float const d8 = e8 * m5;
  1260. float const c0 = d0 + d1;
  1261. float const c1 = d0 - d1;
  1262. float const c2 = d2 - d3;
  1263. float const c3 = d3;
  1264. float const c4 = d4 + d8;
  1265. float const c5 = d5 + d7;
  1266. float const c6 = d6 - d8;
  1267. float const c7 = d7;
  1268. float const c8 = c5 - c6;
  1269. float const b0 = c0 + c3;
  1270. float const b1 = c1 + c2;
  1271. float const b2 = c1 - c2;
  1272. float const b3 = c0 - c3;
  1273. float const b4 = c4 - c8;
  1274. float const b5 = c8;
  1275. float const b6 = c6 - c7;
  1276. float const b7 = c7;
  1277. block_component[l * 8 + 0] = b0 + b7;
  1278. block_component[l * 8 + 1] = b1 + b6;
  1279. block_component[l * 8 + 2] = b2 + b5;
  1280. block_component[l * 8 + 3] = b3 + b4;
  1281. block_component[l * 8 + 4] = b3 - b4;
  1282. block_component[l * 8 + 5] = b2 - b5;
  1283. block_component[l * 8 + 6] = b1 - b6;
  1284. block_component[l * 8 + 7] = b0 - b7;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }
  1290. }
  1291. // F.2.1.5 - Inverse DCT (IDCT)
  1292. auto const level_shift = 1 << (context.frame.precision - 1);
  1293. auto const max_value = (1 << context.frame.precision) - 1;
  1294. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1295. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1296. for (u8 vfactor_i = 0; vfactor_i < context.vsample_factor; ++vfactor_i) {
  1297. for (u8 hfactor_i = 0; hfactor_i < context.hsample_factor; ++hfactor_i) {
  1298. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  1299. for (u8 i = 0; i < 8; ++i) {
  1300. for (u8 j = 0; j < 8; ++j) {
  1301. // FIXME: This just truncate all coefficients, it's an easy way to support (read hack)
  1302. // 12 bits JPEGs without rewriting all color transformations.
  1303. auto const clamp_to_8_bits = [&](u16 color) -> u8 {
  1304. if (context.frame.precision == 8)
  1305. return static_cast<u8>(color);
  1306. return static_cast<u8>(color >> 4);
  1307. };
  1308. macroblocks[mb_index].r[i * 8 + j] = clamp_to_8_bits(clamp(macroblocks[mb_index].r[i * 8 + j] + level_shift, 0, max_value));
  1309. macroblocks[mb_index].g[i * 8 + j] = clamp_to_8_bits(clamp(macroblocks[mb_index].g[i * 8 + j] + level_shift, 0, max_value));
  1310. macroblocks[mb_index].b[i * 8 + j] = clamp_to_8_bits(clamp(macroblocks[mb_index].b[i * 8 + j] + level_shift, 0, max_value));
  1311. macroblocks[mb_index].k[i * 8 + j] = clamp_to_8_bits(clamp(macroblocks[mb_index].k[i * 8 + j] + level_shift, 0, max_value));
  1312. }
  1313. }
  1314. }
  1315. }
  1316. }
  1317. }
  1318. }
  1319. static void ycbcr_to_rgb(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1320. {
  1321. // Conversion from YCbCr to RGB isn't specified in the first JPEG specification but in the JFIF extension:
  1322. // See: https://www.itu.int/rec/dologin_pub.asp?lang=f&id=T-REC-T.871-201105-I!!PDF-E&type=items
  1323. // 7 - Conversion to and from RGB
  1324. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1325. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1326. const u32 chroma_block_index = vcursor * context.mblock_meta.hpadded_count + hcursor;
  1327. Macroblock const& chroma = macroblocks[chroma_block_index];
  1328. // Overflows are intentional.
  1329. for (u8 vfactor_i = context.vsample_factor - 1; vfactor_i < context.vsample_factor; --vfactor_i) {
  1330. for (u8 hfactor_i = context.hsample_factor - 1; hfactor_i < context.hsample_factor; --hfactor_i) {
  1331. u32 macroblock_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  1332. auto* y = macroblocks[macroblock_index].y;
  1333. auto* cb = macroblocks[macroblock_index].cb;
  1334. auto* cr = macroblocks[macroblock_index].cr;
  1335. for (u8 i = 7; i < 8; --i) {
  1336. for (u8 j = 7; j < 8; --j) {
  1337. const u8 pixel = i * 8 + j;
  1338. const u32 chroma_pxrow = (i / context.vsample_factor) + 4 * vfactor_i;
  1339. const u32 chroma_pxcol = (j / context.hsample_factor) + 4 * hfactor_i;
  1340. const u32 chroma_pixel = chroma_pxrow * 8 + chroma_pxcol;
  1341. int r = y[pixel] + 1.402f * (chroma.cr[chroma_pixel] - 128);
  1342. int g = y[pixel] - 0.3441f * (chroma.cb[chroma_pixel] - 128) - 0.7141f * (chroma.cr[chroma_pixel] - 128);
  1343. int b = y[pixel] + 1.772f * (chroma.cb[chroma_pixel] - 128);
  1344. y[pixel] = clamp(r, 0, 255);
  1345. cb[pixel] = clamp(g, 0, 255);
  1346. cr[pixel] = clamp(b, 0, 255);
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. }
  1354. static void invert_colors_for_adobe_images(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1355. {
  1356. if (!context.color_transform.has_value())
  1357. return;
  1358. // From libjpeg-turbo's libjpeg.txt:
  1359. // https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/libjpeg.txt
  1360. // CAUTION: it appears that Adobe Photoshop writes inverted data in CMYK JPEG
  1361. // files: 0 represents 100% ink coverage, rather than 0% ink as you'd expect.
  1362. // This is arguably a bug in Photoshop, but if you need to work with Photoshop
  1363. // CMYK files, you will have to deal with it in your application.
  1364. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1365. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1366. for (u8 vfactor_i = 0; vfactor_i < context.vsample_factor; ++vfactor_i) {
  1367. for (u8 hfactor_i = 0; hfactor_i < context.hsample_factor; ++hfactor_i) {
  1368. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  1369. for (u8 i = 0; i < 8; ++i) {
  1370. for (u8 j = 0; j < 8; ++j) {
  1371. macroblocks[mb_index].r[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].r[i * 8 + j];
  1372. macroblocks[mb_index].g[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].g[i * 8 + j];
  1373. macroblocks[mb_index].b[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].b[i * 8 + j];
  1374. macroblocks[mb_index].k[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].k[i * 8 + j];
  1375. }
  1376. }
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. static void cmyk_to_rgb(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1383. {
  1384. invert_colors_for_adobe_images(context, macroblocks);
  1385. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1386. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1387. for (u8 vfactor_i = context.vsample_factor - 1; vfactor_i < context.vsample_factor; --vfactor_i) {
  1388. for (u8 hfactor_i = context.hsample_factor - 1; hfactor_i < context.hsample_factor; --hfactor_i) {
  1389. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  1390. auto* c = macroblocks[mb_index].y;
  1391. auto* m = macroblocks[mb_index].cb;
  1392. auto* y = macroblocks[mb_index].cr;
  1393. auto* k = macroblocks[mb_index].k;
  1394. for (u8 i = 0; i < 8; ++i) {
  1395. for (u8 j = 0; j < 8; ++j) {
  1396. u8 const pixel = i * 8 + j;
  1397. static constexpr auto max_value = NumericLimits<u8>::max();
  1398. auto const black_component = max_value - k[pixel];
  1399. int const r = ((max_value - c[pixel]) * black_component) / max_value;
  1400. int const g = ((max_value - m[pixel]) * black_component) / max_value;
  1401. int const b = ((max_value - y[pixel]) * black_component) / max_value;
  1402. c[pixel] = clamp(r, 0, max_value);
  1403. m[pixel] = clamp(g, 0, max_value);
  1404. y[pixel] = clamp(b, 0, max_value);
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. }
  1411. }
  1412. static void ycck_to_rgb(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1413. {
  1414. // 7 - Conversions between colour encodings
  1415. // YCCK is obtained from CMYK by converting the CMY channels to YCC channel.
  1416. // To convert back into RGB, we only need the 3 first components, which are baseline YCbCr
  1417. ycbcr_to_rgb(context, macroblocks);
  1418. // RGB to CMYK, as mentioned in https://www.smcm.iqfr.csic.es/docs/intel/ipp/ipp_manual/IPPI/ippi_ch15/functn_YCCKToCMYK_JPEG.htm#functn_YCCKToCMYK_JPEG
  1419. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  1420. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  1421. for (u8 vfactor_i = 0; vfactor_i < context.vsample_factor; ++vfactor_i) {
  1422. for (u8 hfactor_i = 0; hfactor_i < context.hsample_factor; ++hfactor_i) {
  1423. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  1424. for (u8 i = 0; i < 8; ++i) {
  1425. for (u8 j = 0; j < 8; ++j) {
  1426. macroblocks[mb_index].r[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].r[i * 8 + j];
  1427. macroblocks[mb_index].g[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].g[i * 8 + j];
  1428. macroblocks[mb_index].b[i * 8 + j] = NumericLimits<u8>::max() - macroblocks[mb_index].b[i * 8 + j];
  1429. }
  1430. }
  1431. }
  1432. }
  1433. }
  1434. }
  1435. cmyk_to_rgb(context, macroblocks);
  1436. }
  1437. static ErrorOr<void> handle_color_transform(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  1438. {
  1439. if (context.color_transform.has_value()) {
  1440. // https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-T.872-201206-I!!PDF-E&type=items
  1441. // 6.5.3 - APP14 marker segment for colour encoding
  1442. switch (*context.color_transform) {
  1443. case ColorTransform::CmykOrRgb:
  1444. if (context.components.size() == 4) {
  1445. cmyk_to_rgb(context, macroblocks);
  1446. } else if (context.components.size() == 3) {
  1447. // Note: components.size() == 3 means that we have an RGB image, so no color transformation is needed.
  1448. } else {
  1449. return Error::from_string_literal("Wrong number of components for CMYK or RGB, aborting.");
  1450. }
  1451. break;
  1452. case ColorTransform::YCbCr:
  1453. ycbcr_to_rgb(context, macroblocks);
  1454. break;
  1455. case ColorTransform::YCCK:
  1456. ycck_to_rgb(context, macroblocks);
  1457. break;
  1458. }
  1459. return {};
  1460. }
  1461. // No App14 segment is present, assuming :
  1462. // - 1 components means grayscale
  1463. // - 3 components means YCbCr
  1464. // - 4 components means CMYK
  1465. if (context.components.size() == 4)
  1466. cmyk_to_rgb(context, macroblocks);
  1467. if (context.components.size() == 3)
  1468. ycbcr_to_rgb(context, macroblocks);
  1469. if (context.components.size() == 1) {
  1470. // With Cb and Cr being equal to zero, this function assign the Y
  1471. // value (luminosity) to R, G and B. Providing a proper conversion
  1472. // from grayscale to RGB.
  1473. ycbcr_to_rgb(context, macroblocks);
  1474. }
  1475. return {};
  1476. }
  1477. static ErrorOr<void> compose_bitmap(JPEGLoadingContext& context, Vector<Macroblock> const& macroblocks)
  1478. {
  1479. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height }));
  1480. for (u32 y = context.frame.height - 1; y < context.frame.height; y--) {
  1481. const u32 block_row = y / 8;
  1482. const u32 pixel_row = y % 8;
  1483. for (u32 x = 0; x < context.frame.width; x++) {
  1484. const u32 block_column = x / 8;
  1485. auto& block = macroblocks[block_row * context.mblock_meta.hpadded_count + block_column];
  1486. const u32 pixel_column = x % 8;
  1487. const u32 pixel_index = pixel_row * 8 + pixel_column;
  1488. const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] };
  1489. context.bitmap->set_pixel(x, y, color);
  1490. }
  1491. }
  1492. return {};
  1493. }
  1494. static bool is_app_marker(Marker const marker)
  1495. {
  1496. return marker >= JPEG_APPN0 && marker <= JPEG_APPN15;
  1497. }
  1498. static bool is_miscellaneous_or_table_marker(Marker const marker)
  1499. {
  1500. // B.2.4 - Table-specification and miscellaneous marker segment syntax
  1501. // See also B.6 - Summary: Figure B.17 – Flow of marker segment
  1502. bool const is_misc = marker == JPEG_COM || marker == JPEG_DRI || is_app_marker(marker);
  1503. bool const is_table = marker == JPEG_DQT || marker == JPEG_DAC || marker == JPEG_DHT;
  1504. return is_misc || is_table;
  1505. }
  1506. static ErrorOr<void> handle_miscellaneous_or_table(JPEGStream& stream, JPEGLoadingContext& context, Marker const marker)
  1507. {
  1508. if (is_app_marker(marker)) {
  1509. TRY(read_app_marker(stream, context, marker - JPEG_APPN0));
  1510. return {};
  1511. }
  1512. switch (marker) {
  1513. case JPEG_COM:
  1514. case JPEG_DAC:
  1515. dbgln_if(JPEG_DEBUG, "TODO: implement marker \"{:x}\"", marker);
  1516. if (auto result = skip_segment(stream); result.is_error()) {
  1517. dbgln_if(JPEG_DEBUG, "Error skipping marker: {:x}!", marker);
  1518. return result.release_error();
  1519. }
  1520. break;
  1521. case JPEG_DHT:
  1522. TRY(read_huffman_table(stream, context));
  1523. break;
  1524. case JPEG_DQT:
  1525. TRY(read_quantization_table(stream, context));
  1526. break;
  1527. case JPEG_DRI:
  1528. TRY(read_restart_interval(stream, context));
  1529. break;
  1530. default:
  1531. dbgln("Unexpected marker: {:x}", marker);
  1532. VERIFY_NOT_REACHED();
  1533. }
  1534. return {};
  1535. }
  1536. static ErrorOr<void> parse_header(JPEGStream& stream, JPEGLoadingContext& context)
  1537. {
  1538. auto marker = TRY(read_marker_at_cursor(stream));
  1539. if (marker != JPEG_SOI) {
  1540. dbgln_if(JPEG_DEBUG, "SOI not found: {:x}!", marker);
  1541. return Error::from_string_literal("SOI not found");
  1542. }
  1543. for (;;) {
  1544. marker = TRY(read_marker_at_cursor(stream));
  1545. if (is_miscellaneous_or_table_marker(marker)) {
  1546. TRY(handle_miscellaneous_or_table(stream, context, marker));
  1547. continue;
  1548. }
  1549. // Set frame type if the marker marks a new frame.
  1550. if (is_frame_marker(marker))
  1551. context.frame.type = static_cast<StartOfFrame::FrameType>(marker & 0xF);
  1552. switch (marker) {
  1553. case JPEG_RST0:
  1554. case JPEG_RST1:
  1555. case JPEG_RST2:
  1556. case JPEG_RST3:
  1557. case JPEG_RST4:
  1558. case JPEG_RST5:
  1559. case JPEG_RST6:
  1560. case JPEG_RST7:
  1561. case JPEG_SOI:
  1562. case JPEG_EOI:
  1563. dbgln_if(JPEG_DEBUG, "Unexpected marker {:x}!", marker);
  1564. return Error::from_string_literal("Unexpected marker");
  1565. case JPEG_SOF0:
  1566. case JPEG_SOF1:
  1567. case JPEG_SOF2:
  1568. TRY(read_start_of_frame(stream, context));
  1569. context.state = JPEGLoadingContext::FrameDecoded;
  1570. return {};
  1571. default:
  1572. if (auto result = skip_segment(stream); result.is_error()) {
  1573. dbgln_if(JPEG_DEBUG, "Error skipping marker: {:x}!", marker);
  1574. return result.release_error();
  1575. }
  1576. break;
  1577. }
  1578. }
  1579. VERIFY_NOT_REACHED();
  1580. }
  1581. static ErrorOr<void> decode_header(JPEGLoadingContext& context)
  1582. {
  1583. if (context.state < JPEGLoadingContext::State::HeaderDecoded) {
  1584. if (auto result = parse_header(context.stream, context); result.is_error()) {
  1585. context.state = JPEGLoadingContext::State::Error;
  1586. return result.release_error();
  1587. }
  1588. if constexpr (JPEG_DEBUG) {
  1589. dbgln("Image width: {}", context.frame.width);
  1590. dbgln("Image height: {}", context.frame.height);
  1591. dbgln("Macroblocks in a row: {}", context.mblock_meta.hpadded_count);
  1592. dbgln("Macroblocks in a column: {}", context.mblock_meta.vpadded_count);
  1593. dbgln("Macroblock meta padded total: {}", context.mblock_meta.padded_total);
  1594. }
  1595. context.state = JPEGLoadingContext::State::HeaderDecoded;
  1596. }
  1597. return {};
  1598. }
  1599. static ErrorOr<Vector<Macroblock>> construct_macroblocks(JPEGLoadingContext& context)
  1600. {
  1601. // B.6 - Summary
  1602. // See: Figure B.16 – Flow of compressed data syntax
  1603. // This function handles the "Multi-scan" loop.
  1604. Vector<Macroblock> macroblocks;
  1605. TRY(macroblocks.try_resize(context.mblock_meta.padded_total));
  1606. Marker marker = TRY(read_marker_at_cursor(context.stream));
  1607. while (true) {
  1608. if (is_miscellaneous_or_table_marker(marker)) {
  1609. TRY(handle_miscellaneous_or_table(context.stream, context, marker));
  1610. } else if (marker == JPEG_SOS) {
  1611. TRY(read_start_of_scan(context.stream, context));
  1612. TRY(decode_huffman_stream(context, macroblocks));
  1613. } else if (marker == JPEG_EOI) {
  1614. return macroblocks;
  1615. } else {
  1616. dbgln_if(JPEG_DEBUG, "Unexpected marker {:x}!", marker);
  1617. return Error::from_string_literal("Unexpected marker");
  1618. }
  1619. marker = TRY(read_marker_at_cursor(context.stream));
  1620. }
  1621. }
  1622. static ErrorOr<void> decode_jpeg(JPEGLoadingContext& context)
  1623. {
  1624. TRY(decode_header(context));
  1625. auto macroblocks = TRY(construct_macroblocks(context));
  1626. TRY(dequantize(context, macroblocks));
  1627. inverse_dct(context, macroblocks);
  1628. TRY(handle_color_transform(context, macroblocks));
  1629. TRY(compose_bitmap(context, macroblocks));
  1630. return {};
  1631. }
  1632. JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  1633. {
  1634. m_context = JPEGLoadingContext::create(move(stream)).release_value_but_fixme_should_propagate_errors();
  1635. }
  1636. JPEGImageDecoderPlugin::~JPEGImageDecoderPlugin() = default;
  1637. IntSize JPEGImageDecoderPlugin::size()
  1638. {
  1639. if (m_context->state == JPEGLoadingContext::State::Error)
  1640. return {};
  1641. if (m_context->state >= JPEGLoadingContext::State::FrameDecoded)
  1642. return { m_context->frame.width, m_context->frame.height };
  1643. return {};
  1644. }
  1645. void JPEGImageDecoderPlugin::set_volatile()
  1646. {
  1647. if (m_context->bitmap)
  1648. m_context->bitmap->set_volatile();
  1649. }
  1650. bool JPEGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1651. {
  1652. if (!m_context->bitmap)
  1653. return false;
  1654. return m_context->bitmap->set_nonvolatile(was_purged);
  1655. }
  1656. ErrorOr<void> JPEGImageDecoderPlugin::initialize()
  1657. {
  1658. return {};
  1659. }
  1660. bool JPEGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1661. {
  1662. return data.size() > 3
  1663. && data.data()[0] == 0xFF
  1664. && data.data()[1] == 0xD8
  1665. && data.data()[2] == 0xFF;
  1666. }
  1667. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGImageDecoderPlugin::create(ReadonlyBytes data)
  1668. {
  1669. auto stream = TRY(try_make<FixedMemoryStream>(data));
  1670. return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(move(stream)));
  1671. }
  1672. bool JPEGImageDecoderPlugin::is_animated()
  1673. {
  1674. return false;
  1675. }
  1676. size_t JPEGImageDecoderPlugin::loop_count()
  1677. {
  1678. return 0;
  1679. }
  1680. size_t JPEGImageDecoderPlugin::frame_count()
  1681. {
  1682. return 1;
  1683. }
  1684. size_t JPEGImageDecoderPlugin::first_animated_frame_index()
  1685. {
  1686. return 0;
  1687. }
  1688. ErrorOr<ImageFrameDescriptor> JPEGImageDecoderPlugin::frame(size_t index)
  1689. {
  1690. if (index > 0)
  1691. return Error::from_string_literal("JPEGImageDecoderPlugin: Invalid frame index");
  1692. if (m_context->state == JPEGLoadingContext::State::Error)
  1693. return Error::from_string_literal("JPEGImageDecoderPlugin: Decoding failed");
  1694. if (m_context->state < JPEGLoadingContext::State::BitmapDecoded) {
  1695. if (auto result = decode_jpeg(*m_context); result.is_error()) {
  1696. m_context->state = JPEGLoadingContext::State::Error;
  1697. return result.release_error();
  1698. }
  1699. m_context->state = JPEGLoadingContext::State::BitmapDecoded;
  1700. }
  1701. return ImageFrameDescriptor { m_context->bitmap, 0 };
  1702. }
  1703. ErrorOr<Optional<ReadonlyBytes>> JPEGImageDecoderPlugin::icc_data()
  1704. {
  1705. TRY(decode_header(*m_context));
  1706. if (m_context->icc_data.has_value())
  1707. return *m_context->icc_data;
  1708. return OptionalNone {};
  1709. }
  1710. }