JPEGLoader.cpp 77 KB

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