JPEGLoader.cpp 77 KB

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