JPEGLoader.cpp 77 KB

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