JPEGLoader.cpp 71 KB

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