JPEGLoader.cpp 71 KB

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