JPEGLoader.cpp 75 KB

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