JPEGLoader.cpp 77 KB

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