JPEGLoader.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Endian.h>
  8. #include <AK/Error.h>
  9. #include <AK/FixedArray.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/Math.h>
  12. #include <AK/MemoryStream.h>
  13. #include <AK/String.h>
  14. #include <AK/Try.h>
  15. #include <AK/Vector.h>
  16. #include <LibGfx/JPEGLoader.h>
  17. #define JPEG_INVALID 0X0000
  18. #define JPEG_APPN0 0XFFE0
  19. #define JPEG_APPN1 0XFFE1
  20. #define JPEG_APPN2 0XFFE2
  21. #define JPEG_APPN3 0XFFE3
  22. #define JPEG_APPN4 0XFFE4
  23. #define JPEG_APPN5 0XFFE5
  24. #define JPEG_APPN6 0XFFE6
  25. #define JPEG_APPN7 0XFFE7
  26. #define JPEG_APPN8 0XFFE8
  27. #define JPEG_APPN9 0XFFE9
  28. #define JPEG_APPNA 0XFFEA
  29. #define JPEG_APPNB 0XFFEB
  30. #define JPEG_APPNC 0XFFEC
  31. #define JPEG_APPND 0XFFED
  32. #define JPEG_APPNE 0xFFEE
  33. #define JPEG_APPNF 0xFFEF
  34. #define JPEG_RESERVED1 0xFFF1
  35. #define JPEG_RESERVED2 0xFFF2
  36. #define JPEG_RESERVED3 0xFFF3
  37. #define JPEG_RESERVED4 0xFFF4
  38. #define JPEG_RESERVED5 0xFFF5
  39. #define JPEG_RESERVED6 0xFFF6
  40. #define JPEG_RESERVED7 0xFFF7
  41. #define JPEG_RESERVED8 0xFFF8
  42. #define JPEG_RESERVED9 0xFFF9
  43. #define JPEG_RESERVEDA 0xFFFA
  44. #define JPEG_RESERVEDB 0xFFFB
  45. #define JPEG_RESERVEDC 0xFFFC
  46. #define JPEG_RESERVEDD 0xFFFD
  47. #define JPEG_RST0 0xFFD0
  48. #define JPEG_RST1 0xFFD1
  49. #define JPEG_RST2 0xFFD2
  50. #define JPEG_RST3 0xFFD3
  51. #define JPEG_RST4 0xFFD4
  52. #define JPEG_RST5 0xFFD5
  53. #define JPEG_RST6 0xFFD6
  54. #define JPEG_RST7 0xFFD7
  55. #define JPEG_DHP 0xFFDE
  56. #define JPEG_EXP 0xFFDF
  57. #define JPEG_DHT 0XFFC4
  58. #define JPEG_DQT 0XFFDB
  59. #define JPEG_EOI 0xFFD9
  60. #define JPEG_RST 0XFFDD
  61. #define JPEG_SOF0 0XFFC0
  62. #define JPEG_SOF2 0xFFC2
  63. #define JPEG_SOI 0XFFD8
  64. #define JPEG_SOS 0XFFDA
  65. #define JPEG_COM 0xFFFE
  66. namespace Gfx {
  67. constexpr static u8 zigzag_map[64] {
  68. 0, 1, 8, 16, 9, 2, 3, 10,
  69. 17, 24, 32, 25, 18, 11, 4, 5,
  70. 12, 19, 26, 33, 40, 48, 41, 34,
  71. 27, 20, 13, 6, 7, 14, 21, 28,
  72. 35, 42, 49, 56, 57, 50, 43, 36,
  73. 29, 22, 15, 23, 30, 37, 44, 51,
  74. 58, 59, 52, 45, 38, 31, 39, 46,
  75. 53, 60, 61, 54, 47, 55, 62, 63
  76. };
  77. using Marker = u16;
  78. /**
  79. * MCU means group of data units that are coded together. A data unit is an 8x8
  80. * block of component data. In interleaved scans, number of non-interleaved data
  81. * units of a component C is Ch * Cv, where Ch and Cv represent the horizontal &
  82. * vertical subsampling factors of the component, respectively. A MacroBlock is
  83. * an 8x8 block of RGB values before encoding, and 8x8 block of YCbCr values when
  84. * we're done decoding the huffman stream.
  85. */
  86. struct Macroblock {
  87. union {
  88. i32 y[64] = { 0 };
  89. i32 r[64];
  90. };
  91. union {
  92. i32 cb[64] = { 0 };
  93. i32 g[64];
  94. };
  95. union {
  96. i32 cr[64] = { 0 };
  97. i32 b[64];
  98. };
  99. };
  100. struct MacroblockMeta {
  101. u32 total { 0 };
  102. u32 padded_total { 0 };
  103. u32 hcount { 0 };
  104. u32 vcount { 0 };
  105. u32 hpadded_count { 0 };
  106. u32 vpadded_count { 0 };
  107. };
  108. struct ComponentSpec {
  109. u8 id { 0 };
  110. u8 hsample_factor { 1 }; // Horizontal sampling factor.
  111. u8 vsample_factor { 1 }; // Vertical sampling factor.
  112. u8 ac_destination_id { 0 };
  113. u8 dc_destination_id { 0 };
  114. u8 qtable_id { 0 }; // Quantization table id.
  115. };
  116. struct StartOfFrame {
  117. // Of these, only the first 3 are in mainstream use, and refers to SOF0-2.
  118. enum class FrameType {
  119. Baseline_DCT = 0,
  120. Extended_Sequential_DCT = 1,
  121. Progressive_DCT = 2,
  122. Sequential_Lossless = 3,
  123. Differential_Sequential_DCT = 5,
  124. Differential_Progressive_DCT = 6,
  125. Differential_Sequential_Lossless = 7,
  126. Extended_Sequential_DCT_Arithmetic = 9,
  127. Progressive_DCT_Arithmetic = 10,
  128. Sequential_Lossless_Arithmetic = 11,
  129. Differential_Sequential_DCT_Arithmetic = 13,
  130. Differential_Progressive_DCT_Arithmetic = 14,
  131. Differential_Sequential_Lossless_Arithmetic = 15,
  132. };
  133. FrameType type { FrameType::Baseline_DCT };
  134. u8 precision { 0 };
  135. u16 height { 0 };
  136. u16 width { 0 };
  137. };
  138. struct HuffmanTableSpec {
  139. u8 type { 0 };
  140. u8 destination_id { 0 };
  141. u8 code_counts[16] = { 0 };
  142. Vector<u8> symbols;
  143. Vector<u16> codes;
  144. };
  145. struct HuffmanStreamState {
  146. Vector<u8> stream;
  147. u8 bit_offset { 0 };
  148. size_t byte_offset { 0 };
  149. };
  150. struct ICCMultiChunkState {
  151. u8 seen_number_of_icc_chunks { 0 };
  152. FixedArray<ByteBuffer> chunks;
  153. };
  154. struct JPEGLoadingContext {
  155. enum State {
  156. NotDecoded = 0,
  157. Error,
  158. FrameDecoded,
  159. HeaderDecoded,
  160. BitmapDecoded
  161. };
  162. State state { State::NotDecoded };
  163. u8 const* data { nullptr };
  164. size_t data_size { 0 };
  165. u32 luma_table[64] = { 0 };
  166. u32 chroma_table[64] = { 0 };
  167. StartOfFrame frame;
  168. u8 hsample_factor { 0 };
  169. u8 vsample_factor { 0 };
  170. u8 component_count { 0 };
  171. Vector<ComponentSpec, 3> components;
  172. RefPtr<Gfx::Bitmap> bitmap;
  173. u16 dc_reset_interval { 0 };
  174. HashMap<u8, HuffmanTableSpec> dc_tables;
  175. HashMap<u8, HuffmanTableSpec> ac_tables;
  176. HuffmanStreamState huffman_stream;
  177. i32 previous_dc_values[3] = { 0 };
  178. MacroblockMeta mblock_meta;
  179. OwnPtr<FixedMemoryStream> stream;
  180. Optional<ICCMultiChunkState> icc_multi_chunk_state;
  181. Optional<ByteBuffer> icc_data;
  182. };
  183. static void generate_huffman_codes(HuffmanTableSpec& table)
  184. {
  185. unsigned code = 0;
  186. for (auto number_of_codes : table.code_counts) {
  187. for (int i = 0; i < number_of_codes; i++)
  188. table.codes.append(code++);
  189. code <<= 1;
  190. }
  191. }
  192. static ErrorOr<size_t> read_huffman_bits(HuffmanStreamState& hstream, size_t count = 1)
  193. {
  194. if (count > (8 * sizeof(size_t))) {
  195. dbgln_if(JPEG_DEBUG, "Can't read {} bits at once!", count);
  196. return Error::from_string_literal("Reading too much huffman bits at once");
  197. }
  198. size_t value = 0;
  199. while (count--) {
  200. if (hstream.byte_offset >= hstream.stream.size()) {
  201. dbgln_if(JPEG_DEBUG, "Huffman stream exhausted. This could be an error!");
  202. return Error::from_string_literal("Huffman stream exhausted.");
  203. }
  204. u8 current_byte = hstream.stream[hstream.byte_offset];
  205. u8 current_bit = 1u & (u32)(current_byte >> (7 - hstream.bit_offset)); // MSB first.
  206. hstream.bit_offset++;
  207. value = (value << 1) | (size_t)current_bit;
  208. if (hstream.bit_offset == 8) {
  209. hstream.byte_offset++;
  210. hstream.bit_offset = 0;
  211. }
  212. }
  213. return value;
  214. }
  215. static ErrorOr<u8> get_next_symbol(HuffmanStreamState& hstream, HuffmanTableSpec const& table)
  216. {
  217. unsigned code = 0;
  218. size_t code_cursor = 0;
  219. for (int i = 0; i < 16; i++) { // Codes can't be longer than 16 bits.
  220. auto result = TRY(read_huffman_bits(hstream));
  221. code = (code << 1) | (i32)result;
  222. for (int j = 0; j < table.code_counts[i]; j++) {
  223. if (code == table.codes[code_cursor])
  224. return table.symbols[code_cursor];
  225. code_cursor++;
  226. }
  227. }
  228. dbgln_if(JPEG_DEBUG, "If you're seeing this...the jpeg decoder needs to support more kinds of JPEGs!");
  229. return Error::from_string_literal("This kind of JPEG is not yet supported by the decoder");
  230. }
  231. static inline i32* get_component(Macroblock& block, unsigned component)
  232. {
  233. switch (component) {
  234. case 0:
  235. return block.y;
  236. case 1:
  237. return block.cb;
  238. default:
  239. return block.cr;
  240. }
  241. }
  242. /**
  243. * Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
  244. * Depending on the sampling factors, we may not see triples of y, cb, cr in that
  245. * order. If sample factors differ from one, we'll read more than one block of y-
  246. * coefficients before we get to read a cb-cr block.
  247. * In the function below, `hcursor` and `vcursor` denote the location of the block
  248. * we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
  249. * that iterate over the vertical and horizontal subsampling factors, respectively.
  250. * When we finish one iteration of the innermost loop, we'll have the coefficients
  251. * of one of the components of block at position `mb_index`. When the outermost loop
  252. * finishes first iteration, we'll have all the luminance coefficients for all the
  253. * macroblocks that share the chrominance data. Next two iterations (assuming that
  254. * we are dealing with three components) will fill up the blocks with chroma data.
  255. */
  256. static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
  257. {
  258. for (unsigned component_i = 0; component_i < context.component_count; component_i++) {
  259. auto& component = context.components[component_i];
  260. if (component.dc_destination_id >= context.dc_tables.size())
  261. return Error::from_string_literal("DC destination ID is greater than number of DC tables");
  262. if (component.ac_destination_id >= context.ac_tables.size())
  263. return Error::from_string_literal("AC destination ID is greater than number of AC tables");
  264. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  265. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  266. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  267. Macroblock& block = macroblocks[mb_index];
  268. auto& dc_table = context.dc_tables.find(component.dc_destination_id)->value;
  269. auto& ac_table = context.ac_tables.find(component.ac_destination_id)->value;
  270. // For DC coefficients, symbol encodes the length of the coefficient.
  271. auto dc_length = TRY(get_next_symbol(context.huffman_stream, dc_table));
  272. if (dc_length > 11) {
  273. dbgln_if(JPEG_DEBUG, "DC coefficient too long: {}!", dc_length);
  274. return Error::from_string_literal("DC coefficient too long");
  275. }
  276. // DC coefficients are encoded as the difference between previous and current DC values.
  277. i32 dc_diff = TRY(read_huffman_bits(context.huffman_stream, dc_length));
  278. // If MSB in diff is 0, the difference is -ve. Otherwise +ve.
  279. if (dc_length != 0 && dc_diff < (1 << (dc_length - 1)))
  280. dc_diff -= (1 << dc_length) - 1;
  281. auto select_component = get_component(block, component_i);
  282. auto& previous_dc = context.previous_dc_values[component_i];
  283. select_component[0] = previous_dc += dc_diff;
  284. // Compute the AC coefficients.
  285. for (int j = 1; j < 64;) {
  286. // AC symbols encode 2 pieces of information, the high 4 bits represent
  287. // number of zeroes to be stuffed before reading the coefficient. Low 4
  288. // bits represent the magnitude of the coefficient.
  289. auto ac_symbol = TRY(get_next_symbol(context.huffman_stream, ac_table));
  290. if (ac_symbol == 0)
  291. break;
  292. // ac_symbol = 0xF0 means we need to skip 16 zeroes.
  293. u8 run_length = ac_symbol == 0xF0 ? 16 : ac_symbol >> 4;
  294. j += run_length;
  295. if (j >= 64) {
  296. dbgln_if(JPEG_DEBUG, "Run-length exceeded boundaries. Cursor: {}, Skipping: {}!", j, run_length);
  297. return Error::from_string_literal("Run-length exceeded boundaries");
  298. }
  299. u8 coeff_length = ac_symbol & 0x0F;
  300. if (coeff_length > 10) {
  301. dbgln_if(JPEG_DEBUG, "AC coefficient too long: {}!", coeff_length);
  302. return Error::from_string_literal("AC coefficient too long");
  303. }
  304. if (coeff_length != 0) {
  305. i32 ac_coefficient = TRY(read_huffman_bits(context.huffman_stream, coeff_length));
  306. if (ac_coefficient < (1 << (coeff_length - 1)))
  307. ac_coefficient -= (1 << coeff_length) - 1;
  308. select_component[zigzag_map[j++]] = ac_coefficient;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. return {};
  315. }
  316. static ErrorOr<Vector<Macroblock>> decode_huffman_stream(JPEGLoadingContext& context)
  317. {
  318. Vector<Macroblock> macroblocks;
  319. macroblocks.resize(context.mblock_meta.padded_total);
  320. if constexpr (JPEG_DEBUG) {
  321. dbgln("Image width: {}", context.frame.width);
  322. dbgln("Image height: {}", context.frame.height);
  323. dbgln("Macroblocks in a row: {}", context.mblock_meta.hpadded_count);
  324. dbgln("Macroblocks in a column: {}", context.mblock_meta.vpadded_count);
  325. dbgln("Macroblock meta padded total: {}", context.mblock_meta.padded_total);
  326. }
  327. // Compute huffman codes for DC and AC tables.
  328. for (auto it = context.dc_tables.begin(); it != context.dc_tables.end(); ++it)
  329. generate_huffman_codes(it->value);
  330. for (auto it = context.ac_tables.begin(); it != context.ac_tables.end(); ++it)
  331. generate_huffman_codes(it->value);
  332. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  333. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  334. u32 i = vcursor * context.mblock_meta.hpadded_count + hcursor;
  335. if (context.dc_reset_interval > 0) {
  336. if (i != 0 && i % (context.dc_reset_interval * context.vsample_factor * context.hsample_factor) == 0) {
  337. context.previous_dc_values[0] = 0;
  338. context.previous_dc_values[1] = 0;
  339. context.previous_dc_values[2] = 0;
  340. // Restart markers are stored in byte boundaries. Advance the huffman stream cursor to
  341. // the 0th bit of the next byte.
  342. if (context.huffman_stream.byte_offset < context.huffman_stream.stream.size()) {
  343. if (context.huffman_stream.bit_offset > 0) {
  344. context.huffman_stream.bit_offset = 0;
  345. context.huffman_stream.byte_offset++;
  346. }
  347. // Skip the restart marker (RSTn).
  348. context.huffman_stream.byte_offset++;
  349. }
  350. }
  351. }
  352. if (auto result = build_macroblocks(context, macroblocks, hcursor, vcursor); result.is_error()) {
  353. if constexpr (JPEG_DEBUG) {
  354. dbgln("Failed to build Macroblock {}", i);
  355. dbgln("Huffman stream byte offset {}", context.huffman_stream.byte_offset);
  356. dbgln("Huffman stream bit offset {}", context.huffman_stream.bit_offset);
  357. }
  358. return result.release_error();
  359. }
  360. }
  361. }
  362. return macroblocks;
  363. }
  364. static inline ErrorOr<void> ensure_bounds_okay(const size_t cursor, const size_t delta, const size_t bound)
  365. {
  366. if (Checked<size_t>::addition_would_overflow(delta, cursor))
  367. return Error::from_string_literal("Bounds are not ok: addition would overflow");
  368. if (delta + cursor >= bound)
  369. return Error::from_string_literal("Bounds are not ok");
  370. return {};
  371. }
  372. static inline bool is_valid_marker(const Marker marker)
  373. {
  374. if (marker >= JPEG_APPN0 && marker <= JPEG_APPNF) {
  375. if (marker != JPEG_APPN0)
  376. dbgln_if(JPEG_DEBUG, "{:#04x} not supported yet. The decoder may fail!", marker);
  377. return true;
  378. }
  379. if (marker >= JPEG_RESERVED1 && marker <= JPEG_RESERVEDD)
  380. return true;
  381. if (marker >= JPEG_RST0 && marker <= JPEG_RST7)
  382. return true;
  383. switch (marker) {
  384. case JPEG_COM:
  385. case JPEG_DHP:
  386. case JPEG_EXP:
  387. case JPEG_DHT:
  388. case JPEG_DQT:
  389. case JPEG_RST:
  390. case JPEG_SOF0:
  391. case JPEG_SOI:
  392. case JPEG_SOS:
  393. return true;
  394. }
  395. if (marker >= 0xFFC0 && marker <= 0xFFCF) {
  396. if (marker != 0xFFC4 && marker != 0xFFC8 && marker != 0xFFCC) {
  397. dbgln_if(JPEG_DEBUG, "Decoding this frame-type (SOF{}) is not currently supported. Decoder will fail!", marker & 0xf);
  398. return false;
  399. }
  400. }
  401. return false;
  402. }
  403. static inline ErrorOr<Marker> read_marker_at_cursor(Stream& stream)
  404. {
  405. u16 marker = TRY(stream.read_value<BigEndian<u16>>());
  406. if (is_valid_marker(marker))
  407. return marker;
  408. if (marker != 0xFFFF)
  409. return JPEG_INVALID;
  410. u8 next;
  411. do {
  412. next = TRY(stream.read_value<u8>());
  413. if (next == 0x00)
  414. return JPEG_INVALID;
  415. } while (next == 0xFF);
  416. marker = 0xFF00 | (u16)next;
  417. return is_valid_marker(marker) ? marker : JPEG_INVALID;
  418. }
  419. static ErrorOr<void> read_start_of_scan(AK::SeekableStream& stream, JPEGLoadingContext& context)
  420. {
  421. if (context.state < JPEGLoadingContext::State::FrameDecoded) {
  422. dbgln_if(JPEG_DEBUG, "{}: SOS found before reading a SOF!", TRY(stream.tell()));
  423. return Error::from_string_literal("SOS found before reading a SOF");
  424. }
  425. u16 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  426. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  427. u8 component_count = TRY(stream.read_value<u8>());
  428. if (component_count != context.component_count) {
  429. dbgln_if(JPEG_DEBUG, "{}: Unsupported number of components: {}!", TRY(stream.tell()), component_count);
  430. return Error::from_string_literal("Unsupported number of components");
  431. }
  432. for (int i = 0; i < component_count; i++) {
  433. u8 component_id = TRY(stream.read_value<u8>());
  434. auto& component = context.components[i];
  435. if (component.id != component_id) {
  436. dbgln("JPEG decode failed (component.id != component_id)");
  437. return Error::from_string_literal("JPEG decode failed (component.id != component_id)");
  438. }
  439. u8 table_ids = TRY(stream.read_value<u8>());
  440. component.dc_destination_id = table_ids >> 4;
  441. component.ac_destination_id = table_ids & 0x0F;
  442. if (context.dc_tables.size() != context.ac_tables.size()) {
  443. dbgln_if(JPEG_DEBUG, "{}: DC & AC table count mismatch!", TRY(stream.tell()));
  444. return Error::from_string_literal("DC & AC table count mismatch");
  445. }
  446. if (!context.dc_tables.contains(component.dc_destination_id)) {
  447. dbgln_if(JPEG_DEBUG, "DC table (id: {}) does not exist!", component.dc_destination_id);
  448. return Error::from_string_literal("DC table does not exist");
  449. }
  450. if (!context.ac_tables.contains(component.ac_destination_id)) {
  451. dbgln_if(JPEG_DEBUG, "AC table (id: {}) does not exist!", component.ac_destination_id);
  452. return Error::from_string_literal("AC table does not exist");
  453. }
  454. }
  455. u8 spectral_selection_start = TRY(stream.read_value<u8>());
  456. u8 spectral_selection_end = TRY(stream.read_value<u8>());
  457. u8 successive_approximation = TRY(stream.read_value<u8>());
  458. // The three values should be fixed for baseline JPEGs utilizing sequential DCT.
  459. if (spectral_selection_start != 0 || spectral_selection_end != 63 || successive_approximation != 0) {
  460. dbgln_if(JPEG_DEBUG, "{}: ERROR! Start of Selection: {}, End of Selection: {}, Successive Approximation: {}!",
  461. TRY(stream.tell()),
  462. spectral_selection_start,
  463. spectral_selection_end,
  464. successive_approximation);
  465. return Error::from_string_literal("Spectral selection is not [0,63] or successive approximation is not null");
  466. }
  467. return {};
  468. }
  469. static ErrorOr<void> read_reset_marker(AK::SeekableStream& stream, JPEGLoadingContext& context)
  470. {
  471. u16 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  472. if (bytes_to_read != 2) {
  473. dbgln_if(JPEG_DEBUG, "{}: Malformed reset marker found!", TRY(stream.tell()));
  474. return Error::from_string_literal("Malformed reset marker found");
  475. }
  476. context.dc_reset_interval = TRY(stream.read_value<BigEndian<u16>>());
  477. return {};
  478. }
  479. static ErrorOr<void> read_huffman_table(AK::SeekableStream& stream, JPEGLoadingContext& context)
  480. {
  481. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  482. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  483. bytes_to_read -= 2;
  484. while (bytes_to_read > 0) {
  485. HuffmanTableSpec table;
  486. u8 table_info = TRY(stream.read_value<u8>());
  487. u8 table_type = table_info >> 4;
  488. u8 table_destination_id = table_info & 0x0F;
  489. if (table_type > 1) {
  490. dbgln_if(JPEG_DEBUG, "{}: Unrecognized huffman table: {}!", TRY(stream.tell()), table_type);
  491. return Error::from_string_literal("Unrecognized huffman table");
  492. }
  493. if (table_destination_id > 1) {
  494. dbgln_if(JPEG_DEBUG, "{}: Invalid huffman table destination id: {}!", TRY(stream.tell()), table_destination_id);
  495. return Error::from_string_literal("Invalid huffman table destination id");
  496. }
  497. table.type = table_type;
  498. table.destination_id = table_destination_id;
  499. u32 total_codes = 0;
  500. // Read code counts. At each index K, the value represents the number of K+1 bit codes in this header.
  501. for (int i = 0; i < 16; i++) {
  502. u8 count = TRY(stream.read_value<u8>());
  503. total_codes += count;
  504. table.code_counts[i] = count;
  505. }
  506. table.codes.ensure_capacity(total_codes);
  507. // Read symbols. Read X bytes, where X is the sum of the counts of codes read in the previous step.
  508. for (u32 i = 0; i < total_codes; i++) {
  509. u8 symbol = TRY(stream.read_value<u8>());
  510. table.symbols.append(symbol);
  511. }
  512. auto& huffman_table = table.type == 0 ? context.dc_tables : context.ac_tables;
  513. huffman_table.set(table.destination_id, table);
  514. VERIFY(huffman_table.size() <= 2);
  515. bytes_to_read -= 1 + 16 + total_codes;
  516. }
  517. if (bytes_to_read != 0) {
  518. dbgln_if(JPEG_DEBUG, "{}: Extra bytes detected in huffman header!", TRY(stream.tell()));
  519. return Error::from_string_literal("Extra bytes detected in huffman header");
  520. }
  521. return {};
  522. }
  523. static ErrorOr<void> read_icc_profile(SeekableStream& stream, JPEGLoadingContext& context, int bytes_to_read)
  524. {
  525. if (bytes_to_read <= 2)
  526. return Error::from_string_literal("icc marker too small");
  527. auto chunk_sequence_number = TRY(stream.read_value<u8>()); // 1-based
  528. auto number_of_chunks = TRY(stream.read_value<u8>());
  529. bytes_to_read -= 2;
  530. if (!context.icc_multi_chunk_state.has_value())
  531. context.icc_multi_chunk_state.emplace(ICCMultiChunkState { 0, TRY(FixedArray<ByteBuffer>::create(number_of_chunks)) });
  532. auto& chunk_state = context.icc_multi_chunk_state;
  533. if (chunk_state->seen_number_of_icc_chunks >= number_of_chunks)
  534. return Error::from_string_literal("Too many ICC chunks");
  535. if (chunk_state->chunks.size() != number_of_chunks)
  536. return Error::from_string_literal("Inconsistent number of total ICC chunks");
  537. if (chunk_sequence_number == 0)
  538. return Error::from_string_literal("ICC chunk sequence number not 1 based");
  539. u8 index = chunk_sequence_number - 1;
  540. if (index >= chunk_state->chunks.size())
  541. return Error::from_string_literal("ICC chunk sequence number larger than number of chunks");
  542. if (!chunk_state->chunks[index].is_empty())
  543. return Error::from_string_literal("Duplicate ICC chunk at sequence number");
  544. chunk_state->chunks[index] = TRY(ByteBuffer::create_zeroed(bytes_to_read));
  545. TRY(stream.read_entire_buffer(chunk_state->chunks[index]));
  546. chunk_state->seen_number_of_icc_chunks++;
  547. if (chunk_state->seen_number_of_icc_chunks != chunk_state->chunks.size())
  548. return {};
  549. if (number_of_chunks == 1) {
  550. context.icc_data = move(chunk_state->chunks[0]);
  551. return {};
  552. }
  553. size_t total_size = 0;
  554. for (auto const& chunk : chunk_state->chunks)
  555. total_size += chunk.size();
  556. auto icc_bytes = TRY(ByteBuffer::create_zeroed(total_size));
  557. size_t start = 0;
  558. for (auto const& chunk : chunk_state->chunks) {
  559. memcpy(icc_bytes.data() + start, chunk.data(), chunk.size());
  560. start += chunk.size();
  561. }
  562. context.icc_data = move(icc_bytes);
  563. return {};
  564. }
  565. static ErrorOr<void> read_app_marker(SeekableStream& stream, JPEGLoadingContext& context, int app_marker_number)
  566. {
  567. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  568. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  569. if (bytes_to_read <= 2)
  570. return Error::from_string_literal("app marker size too small");
  571. bytes_to_read -= 2;
  572. StringBuilder builder;
  573. for (;;) {
  574. if (bytes_to_read == 0)
  575. return Error::from_string_literal("app marker size too small for identifier");
  576. auto c = TRY(stream.read_value<char>());
  577. bytes_to_read--;
  578. if (c == '\0')
  579. break;
  580. TRY(builder.try_append(c));
  581. }
  582. auto app_id = TRY(builder.to_string());
  583. if (app_marker_number == 2 && app_id == "ICC_PROFILE"sv)
  584. return read_icc_profile(stream, context, bytes_to_read);
  585. return stream.discard(bytes_to_read);
  586. }
  587. static inline bool validate_luma_and_modify_context(ComponentSpec const& luma, JPEGLoadingContext& context)
  588. {
  589. if ((luma.hsample_factor == 1 || luma.hsample_factor == 2) && (luma.vsample_factor == 1 || luma.vsample_factor == 2)) {
  590. context.mblock_meta.hpadded_count += luma.hsample_factor == 1 ? 0 : context.mblock_meta.hcount % 2;
  591. context.mblock_meta.vpadded_count += luma.vsample_factor == 1 ? 0 : context.mblock_meta.vcount % 2;
  592. context.mblock_meta.padded_total = context.mblock_meta.hpadded_count * context.mblock_meta.vpadded_count;
  593. // For easy reference to relevant sample factors.
  594. context.hsample_factor = luma.hsample_factor;
  595. context.vsample_factor = luma.vsample_factor;
  596. if constexpr (JPEG_DEBUG) {
  597. dbgln("Horizontal Subsampling Factor: {}", luma.hsample_factor);
  598. dbgln("Vertical Subsampling Factor: {}", luma.vsample_factor);
  599. }
  600. return true;
  601. }
  602. return false;
  603. }
  604. static inline void set_macroblock_metadata(JPEGLoadingContext& context)
  605. {
  606. context.mblock_meta.hcount = (context.frame.width + 7) / 8;
  607. context.mblock_meta.vcount = (context.frame.height + 7) / 8;
  608. context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
  609. context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
  610. context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
  611. }
  612. static ErrorOr<void> read_start_of_frame(AK::SeekableStream& stream, JPEGLoadingContext& context)
  613. {
  614. if (context.state == JPEGLoadingContext::FrameDecoded) {
  615. dbgln_if(JPEG_DEBUG, "{}: SOF repeated!", TRY(stream.tell()));
  616. return Error::from_string_literal("SOF repeated");
  617. }
  618. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  619. bytes_to_read -= 2;
  620. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  621. context.frame.precision = TRY(stream.read_value<u8>());
  622. if (context.frame.precision != 8) {
  623. dbgln_if(JPEG_DEBUG, "{}: SOF precision != 8!", TRY(stream.tell()));
  624. return Error::from_string_literal("SOF precision != 8");
  625. }
  626. context.frame.height = TRY(stream.read_value<BigEndian<u16>>());
  627. context.frame.width = TRY(stream.read_value<BigEndian<u16>>());
  628. if (!context.frame.width || !context.frame.height) {
  629. dbgln_if(JPEG_DEBUG, "{}: ERROR! Image height: {}, Image width: {}!", TRY(stream.tell()), context.frame.height, context.frame.width);
  630. return Error::from_string_literal("Image frame height of width null");
  631. }
  632. if (context.frame.width > maximum_width_for_decoded_images || context.frame.height > maximum_height_for_decoded_images) {
  633. dbgln("This JPEG is too large for comfort: {}x{}", context.frame.width, context.frame.height);
  634. return Error::from_string_literal("JPEG too large for comfort");
  635. }
  636. set_macroblock_metadata(context);
  637. context.component_count = TRY(stream.read_value<u8>());
  638. if (context.component_count != 1 && context.component_count != 3) {
  639. dbgln_if(JPEG_DEBUG, "{}: Unsupported number of components in SOF: {}!", TRY(stream.tell()), context.component_count);
  640. return Error::from_string_literal("Unsupported number of components in SOF");
  641. }
  642. for (u8 i = 0; i < context.component_count; i++) {
  643. ComponentSpec component;
  644. component.id = TRY(stream.read_value<u8>());
  645. u8 subsample_factors = TRY(stream.read_value<u8>());
  646. component.hsample_factor = subsample_factors >> 4;
  647. component.vsample_factor = subsample_factors & 0x0F;
  648. if (i == 0) {
  649. // If there is only a single component, i.e. grayscale, the macroblocks will not be interleaved, even if
  650. // the horizontal or vertical sample factor is larger than 1.
  651. if (context.component_count == 1) {
  652. component.hsample_factor = 1;
  653. component.vsample_factor = 1;
  654. }
  655. // By convention, downsampling is applied only on chroma components. So we should
  656. // hope to see the maximum sampling factor in the luma component.
  657. if (!validate_luma_and_modify_context(component, context)) {
  658. dbgln_if(JPEG_DEBUG, "{}: Unsupported luma subsampling factors: horizontal: {}, vertical: {}",
  659. TRY(stream.tell()),
  660. component.hsample_factor,
  661. component.vsample_factor);
  662. return Error::from_string_literal("Unsupported luma subsampling factors");
  663. }
  664. } else {
  665. if (component.hsample_factor != 1 || component.vsample_factor != 1) {
  666. dbgln_if(JPEG_DEBUG, "{}: Unsupported chroma subsampling factors: horizontal: {}, vertical: {}",
  667. TRY(stream.tell()),
  668. component.hsample_factor,
  669. component.vsample_factor);
  670. return Error::from_string_literal("Unsupported chroma subsampling factors");
  671. }
  672. }
  673. component.qtable_id = TRY(stream.read_value<u8>());
  674. if (component.qtable_id > 1) {
  675. dbgln_if(JPEG_DEBUG, "{}: Unsupported quantization table id: {}!", TRY(stream.tell()), component.qtable_id);
  676. return Error::from_string_literal("Unsupported quantization table id");
  677. }
  678. context.components.append(move(component));
  679. }
  680. return {};
  681. }
  682. static ErrorOr<void> read_quantization_table(AK::SeekableStream& stream, JPEGLoadingContext& context)
  683. {
  684. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  685. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  686. while (bytes_to_read > 0) {
  687. u8 info_byte = TRY(stream.read_value<u8>());
  688. u8 element_unit_hint = info_byte >> 4;
  689. if (element_unit_hint > 1) {
  690. dbgln_if(JPEG_DEBUG, "{}: Unsupported unit hint in quantization table: {}!", TRY(stream.tell()), element_unit_hint);
  691. return Error::from_string_literal("Unsupported unit hint in quantization table");
  692. }
  693. u8 table_id = info_byte & 0x0F;
  694. if (table_id > 1) {
  695. dbgln_if(JPEG_DEBUG, "{}: Unsupported quantization table id: {}!", TRY(stream.tell()), table_id);
  696. return Error::from_string_literal("Unsupported quantization table id");
  697. }
  698. u32* table = table_id == 0 ? context.luma_table : context.chroma_table;
  699. for (int i = 0; i < 64; i++) {
  700. if (element_unit_hint == 0) {
  701. u8 tmp = TRY(stream.read_value<u8>());
  702. table[zigzag_map[i]] = tmp;
  703. } else {
  704. table[zigzag_map[i]] = TRY(stream.read_value<BigEndian<u16>>());
  705. }
  706. }
  707. bytes_to_read -= 1 + (element_unit_hint == 0 ? 64 : 128);
  708. }
  709. if (bytes_to_read != 0) {
  710. dbgln_if(JPEG_DEBUG, "{}: Invalid length for one or more quantization tables!", TRY(stream.tell()));
  711. return Error::from_string_literal("Invalid length for one or more quantization tables");
  712. }
  713. return {};
  714. }
  715. static ErrorOr<void> skip_marker_with_length(Stream& stream)
  716. {
  717. u16 bytes_to_skip = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  718. TRY(stream.discard(bytes_to_skip));
  719. return {};
  720. }
  721. static void dequantize(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks)
  722. {
  723. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  724. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  725. for (u32 i = 0; i < context.component_count; i++) {
  726. auto& component = context.components[i];
  727. u32 const* table = component.qtable_id == 0 ? context.luma_table : context.chroma_table;
  728. for (u32 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  729. for (u32 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  730. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  731. Macroblock& block = macroblocks[mb_index];
  732. int* block_component = get_component(block, i);
  733. for (u32 k = 0; k < 64; k++)
  734. block_component[k] *= table[k];
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. static void inverse_dct(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  742. {
  743. static float const m0 = 2.0f * AK::cos(1.0f / 16.0f * 2.0f * AK::Pi<float>);
  744. static float const m1 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  745. static float const m3 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  746. static float const m5 = 2.0f * AK::cos(3.0f / 16.0f * 2.0f * AK::Pi<float>);
  747. static float const m2 = m0 - m5;
  748. static float const m4 = m0 + m5;
  749. static float const s0 = AK::cos(0.0f / 16.0f * AK::Pi<float>) * AK::rsqrt(8.0f);
  750. static float const s1 = AK::cos(1.0f / 16.0f * AK::Pi<float>) / 2.0f;
  751. static float const s2 = AK::cos(2.0f / 16.0f * AK::Pi<float>) / 2.0f;
  752. static float const s3 = AK::cos(3.0f / 16.0f * AK::Pi<float>) / 2.0f;
  753. static float const s4 = AK::cos(4.0f / 16.0f * AK::Pi<float>) / 2.0f;
  754. static float const s5 = AK::cos(5.0f / 16.0f * AK::Pi<float>) / 2.0f;
  755. static float const s6 = AK::cos(6.0f / 16.0f * AK::Pi<float>) / 2.0f;
  756. static float const s7 = AK::cos(7.0f / 16.0f * AK::Pi<float>) / 2.0f;
  757. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  758. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  759. for (u32 component_i = 0; component_i < context.component_count; component_i++) {
  760. auto& component = context.components[component_i];
  761. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  762. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  763. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  764. Macroblock& block = macroblocks[mb_index];
  765. i32* block_component = get_component(block, component_i);
  766. for (u32 k = 0; k < 8; ++k) {
  767. float const g0 = block_component[0 * 8 + k] * s0;
  768. float const g1 = block_component[4 * 8 + k] * s4;
  769. float const g2 = block_component[2 * 8 + k] * s2;
  770. float const g3 = block_component[6 * 8 + k] * s6;
  771. float const g4 = block_component[5 * 8 + k] * s5;
  772. float const g5 = block_component[1 * 8 + k] * s1;
  773. float const g6 = block_component[7 * 8 + k] * s7;
  774. float const g7 = block_component[3 * 8 + k] * s3;
  775. float const f0 = g0;
  776. float const f1 = g1;
  777. float const f2 = g2;
  778. float const f3 = g3;
  779. float const f4 = g4 - g7;
  780. float const f5 = g5 + g6;
  781. float const f6 = g5 - g6;
  782. float const f7 = g4 + g7;
  783. float const e0 = f0;
  784. float const e1 = f1;
  785. float const e2 = f2 - f3;
  786. float const e3 = f2 + f3;
  787. float const e4 = f4;
  788. float const e5 = f5 - f7;
  789. float const e6 = f6;
  790. float const e7 = f5 + f7;
  791. float const e8 = f4 + f6;
  792. float const d0 = e0;
  793. float const d1 = e1;
  794. float const d2 = e2 * m1;
  795. float const d3 = e3;
  796. float const d4 = e4 * m2;
  797. float const d5 = e5 * m3;
  798. float const d6 = e6 * m4;
  799. float const d7 = e7;
  800. float const d8 = e8 * m5;
  801. float const c0 = d0 + d1;
  802. float const c1 = d0 - d1;
  803. float const c2 = d2 - d3;
  804. float const c3 = d3;
  805. float const c4 = d4 + d8;
  806. float const c5 = d5 + d7;
  807. float const c6 = d6 - d8;
  808. float const c7 = d7;
  809. float const c8 = c5 - c6;
  810. float const b0 = c0 + c3;
  811. float const b1 = c1 + c2;
  812. float const b2 = c1 - c2;
  813. float const b3 = c0 - c3;
  814. float const b4 = c4 - c8;
  815. float const b5 = c8;
  816. float const b6 = c6 - c7;
  817. float const b7 = c7;
  818. block_component[0 * 8 + k] = b0 + b7;
  819. block_component[1 * 8 + k] = b1 + b6;
  820. block_component[2 * 8 + k] = b2 + b5;
  821. block_component[3 * 8 + k] = b3 + b4;
  822. block_component[4 * 8 + k] = b3 - b4;
  823. block_component[5 * 8 + k] = b2 - b5;
  824. block_component[6 * 8 + k] = b1 - b6;
  825. block_component[7 * 8 + k] = b0 - b7;
  826. }
  827. for (u32 l = 0; l < 8; ++l) {
  828. float const g0 = block_component[l * 8 + 0] * s0;
  829. float const g1 = block_component[l * 8 + 4] * s4;
  830. float const g2 = block_component[l * 8 + 2] * s2;
  831. float const g3 = block_component[l * 8 + 6] * s6;
  832. float const g4 = block_component[l * 8 + 5] * s5;
  833. float const g5 = block_component[l * 8 + 1] * s1;
  834. float const g6 = block_component[l * 8 + 7] * s7;
  835. float const g7 = block_component[l * 8 + 3] * s3;
  836. float const f0 = g0;
  837. float const f1 = g1;
  838. float const f2 = g2;
  839. float const f3 = g3;
  840. float const f4 = g4 - g7;
  841. float const f5 = g5 + g6;
  842. float const f6 = g5 - g6;
  843. float const f7 = g4 + g7;
  844. float const e0 = f0;
  845. float const e1 = f1;
  846. float const e2 = f2 - f3;
  847. float const e3 = f2 + f3;
  848. float const e4 = f4;
  849. float const e5 = f5 - f7;
  850. float const e6 = f6;
  851. float const e7 = f5 + f7;
  852. float const e8 = f4 + f6;
  853. float const d0 = e0;
  854. float const d1 = e1;
  855. float const d2 = e2 * m1;
  856. float const d3 = e3;
  857. float const d4 = e4 * m2;
  858. float const d5 = e5 * m3;
  859. float const d6 = e6 * m4;
  860. float const d7 = e7;
  861. float const d8 = e8 * m5;
  862. float const c0 = d0 + d1;
  863. float const c1 = d0 - d1;
  864. float const c2 = d2 - d3;
  865. float const c3 = d3;
  866. float const c4 = d4 + d8;
  867. float const c5 = d5 + d7;
  868. float const c6 = d6 - d8;
  869. float const c7 = d7;
  870. float const c8 = c5 - c6;
  871. float const b0 = c0 + c3;
  872. float const b1 = c1 + c2;
  873. float const b2 = c1 - c2;
  874. float const b3 = c0 - c3;
  875. float const b4 = c4 - c8;
  876. float const b5 = c8;
  877. float const b6 = c6 - c7;
  878. float const b7 = c7;
  879. block_component[l * 8 + 0] = b0 + b7;
  880. block_component[l * 8 + 1] = b1 + b6;
  881. block_component[l * 8 + 2] = b2 + b5;
  882. block_component[l * 8 + 3] = b3 + b4;
  883. block_component[l * 8 + 4] = b3 - b4;
  884. block_component[l * 8 + 5] = b2 - b5;
  885. block_component[l * 8 + 6] = b1 - b6;
  886. block_component[l * 8 + 7] = b0 - b7;
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. }
  894. static void ycbcr_to_rgb(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  895. {
  896. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  897. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  898. const u32 chroma_block_index = vcursor * context.mblock_meta.hpadded_count + hcursor;
  899. Macroblock const& chroma = macroblocks[chroma_block_index];
  900. // Overflows are intentional.
  901. for (u8 vfactor_i = context.vsample_factor - 1; vfactor_i < context.vsample_factor; --vfactor_i) {
  902. for (u8 hfactor_i = context.hsample_factor - 1; hfactor_i < context.hsample_factor; --hfactor_i) {
  903. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  904. i32* y = macroblocks[mb_index].y;
  905. i32* cb = macroblocks[mb_index].cb;
  906. i32* cr = macroblocks[mb_index].cr;
  907. for (u8 i = 7; i < 8; --i) {
  908. for (u8 j = 7; j < 8; --j) {
  909. const u8 pixel = i * 8 + j;
  910. const u32 chroma_pxrow = (i / context.vsample_factor) + 4 * vfactor_i;
  911. const u32 chroma_pxcol = (j / context.hsample_factor) + 4 * hfactor_i;
  912. const u32 chroma_pixel = chroma_pxrow * 8 + chroma_pxcol;
  913. int r = y[pixel] + 1.402f * chroma.cr[chroma_pixel] + 128;
  914. int g = y[pixel] - 0.344f * chroma.cb[chroma_pixel] - 0.714f * chroma.cr[chroma_pixel] + 128;
  915. int b = y[pixel] + 1.772f * chroma.cb[chroma_pixel] + 128;
  916. y[pixel] = r < 0 ? 0 : (r > 255 ? 255 : r);
  917. cb[pixel] = g < 0 ? 0 : (g > 255 ? 255 : g);
  918. cr[pixel] = b < 0 ? 0 : (b > 255 ? 255 : b);
  919. }
  920. }
  921. }
  922. }
  923. }
  924. }
  925. }
  926. static ErrorOr<void> compose_bitmap(JPEGLoadingContext& context, Vector<Macroblock> const& macroblocks)
  927. {
  928. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height }));
  929. for (u32 y = context.frame.height - 1; y < context.frame.height; y--) {
  930. const u32 block_row = y / 8;
  931. const u32 pixel_row = y % 8;
  932. for (u32 x = 0; x < context.frame.width; x++) {
  933. const u32 block_column = x / 8;
  934. auto& block = macroblocks[block_row * context.mblock_meta.hpadded_count + block_column];
  935. const u32 pixel_column = x % 8;
  936. const u32 pixel_index = pixel_row * 8 + pixel_column;
  937. const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] };
  938. context.bitmap->set_pixel(x, y, color);
  939. }
  940. }
  941. return {};
  942. }
  943. static ErrorOr<void> parse_header(AK::SeekableStream& stream, JPEGLoadingContext& context)
  944. {
  945. auto marker = TRY(read_marker_at_cursor(stream));
  946. if (marker != JPEG_SOI) {
  947. dbgln_if(JPEG_DEBUG, "{}: SOI not found: {:x}!", TRY(stream.tell()), marker);
  948. return Error::from_string_literal("SOI not found");
  949. }
  950. for (;;) {
  951. marker = TRY(read_marker_at_cursor(stream));
  952. // Set frame type if the marker marks a new frame.
  953. if (marker >= 0xFFC0 && marker <= 0xFFCF) {
  954. // Ignore interleaved markers.
  955. if (marker != 0xFFC4 && marker != 0xFFC8 && marker != 0xFFCC) {
  956. context.frame.type = static_cast<StartOfFrame::FrameType>(marker & 0xF);
  957. }
  958. }
  959. switch (marker) {
  960. case JPEG_INVALID:
  961. case JPEG_RST0:
  962. case JPEG_RST1:
  963. case JPEG_RST2:
  964. case JPEG_RST3:
  965. case JPEG_RST4:
  966. case JPEG_RST5:
  967. case JPEG_RST6:
  968. case JPEG_RST7:
  969. case JPEG_SOI:
  970. case JPEG_EOI:
  971. dbgln_if(JPEG_DEBUG, "{}: Unexpected marker {:x}!", TRY(stream.tell()), marker);
  972. return Error::from_string_literal("Unexpected marker");
  973. case JPEG_APPN0:
  974. case JPEG_APPN1:
  975. case JPEG_APPN2:
  976. case JPEG_APPN3:
  977. case JPEG_APPN4:
  978. case JPEG_APPN5:
  979. case JPEG_APPN6:
  980. case JPEG_APPN7:
  981. case JPEG_APPN8:
  982. case JPEG_APPN9:
  983. case JPEG_APPNA:
  984. case JPEG_APPNB:
  985. case JPEG_APPNC:
  986. case JPEG_APPND:
  987. case JPEG_APPNE:
  988. case JPEG_APPNF:
  989. TRY(read_app_marker(stream, context, marker - JPEG_APPN0));
  990. break;
  991. case JPEG_SOF0:
  992. TRY(read_start_of_frame(stream, context));
  993. context.state = JPEGLoadingContext::FrameDecoded;
  994. break;
  995. case JPEG_DQT:
  996. TRY(read_quantization_table(stream, context));
  997. break;
  998. case JPEG_RST:
  999. TRY(read_reset_marker(stream, context));
  1000. break;
  1001. case JPEG_DHT:
  1002. TRY(read_huffman_table(stream, context));
  1003. break;
  1004. case JPEG_SOS:
  1005. return read_start_of_scan(stream, context);
  1006. default:
  1007. if (auto result = skip_marker_with_length(stream); result.is_error()) {
  1008. dbgln_if(JPEG_DEBUG, "{}: Error skipping marker: {:x}!", TRY(stream.tell()), marker);
  1009. return result.release_error();
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. VERIFY_NOT_REACHED();
  1015. }
  1016. static ErrorOr<void> scan_huffman_stream(AK::SeekableStream& stream, JPEGLoadingContext& context)
  1017. {
  1018. u8 last_byte;
  1019. u8 current_byte = TRY(stream.read_value<u8>());
  1020. for (;;) {
  1021. last_byte = current_byte;
  1022. current_byte = TRY(stream.read_value<u8>());
  1023. if (last_byte == 0xFF) {
  1024. if (current_byte == 0xFF)
  1025. continue;
  1026. if (current_byte == 0x00) {
  1027. current_byte = TRY(stream.read_value<u8>());
  1028. context.huffman_stream.stream.append(last_byte);
  1029. continue;
  1030. }
  1031. Marker marker = 0xFF00 | current_byte;
  1032. if (marker == JPEG_EOI)
  1033. return {};
  1034. if (marker >= JPEG_RST0 && marker <= JPEG_RST7) {
  1035. context.huffman_stream.stream.append(marker);
  1036. current_byte = TRY(stream.read_value<u8>());
  1037. continue;
  1038. }
  1039. dbgln_if(JPEG_DEBUG, "{}: Invalid marker: {:x}!", TRY(stream.tell()), marker);
  1040. return Error::from_string_literal("Invalid marker");
  1041. } else {
  1042. context.huffman_stream.stream.append(last_byte);
  1043. }
  1044. }
  1045. VERIFY_NOT_REACHED();
  1046. }
  1047. static ErrorOr<void> decode_header(JPEGLoadingContext& context)
  1048. {
  1049. if (context.state < JPEGLoadingContext::State::HeaderDecoded) {
  1050. context.stream = TRY(try_make<FixedMemoryStream>(ReadonlyBytes { context.data, context.data_size }));
  1051. if (auto result = parse_header(*context.stream, context); result.is_error()) {
  1052. context.state = JPEGLoadingContext::State::Error;
  1053. return result.release_error();
  1054. }
  1055. context.state = JPEGLoadingContext::State::HeaderDecoded;
  1056. }
  1057. return {};
  1058. }
  1059. static ErrorOr<void> decode_jpeg(JPEGLoadingContext& context)
  1060. {
  1061. TRY(decode_header(context));
  1062. TRY(scan_huffman_stream(*context.stream, context));
  1063. auto macroblocks = TRY(decode_huffman_stream(context));
  1064. dequantize(context, macroblocks);
  1065. inverse_dct(context, macroblocks);
  1066. ycbcr_to_rgb(context, macroblocks);
  1067. TRY(compose_bitmap(context, macroblocks));
  1068. context.stream.clear();
  1069. return {};
  1070. }
  1071. JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(u8 const* data, size_t size)
  1072. {
  1073. m_context = make<JPEGLoadingContext>();
  1074. m_context->data = data;
  1075. m_context->data_size = size;
  1076. m_context->huffman_stream.stream.ensure_capacity(50 * KiB);
  1077. }
  1078. JPEGImageDecoderPlugin::~JPEGImageDecoderPlugin() = default;
  1079. IntSize JPEGImageDecoderPlugin::size()
  1080. {
  1081. if (m_context->state == JPEGLoadingContext::State::Error)
  1082. return {};
  1083. if (m_context->state >= JPEGLoadingContext::State::FrameDecoded)
  1084. return { m_context->frame.width, m_context->frame.height };
  1085. return {};
  1086. }
  1087. void JPEGImageDecoderPlugin::set_volatile()
  1088. {
  1089. if (m_context->bitmap)
  1090. m_context->bitmap->set_volatile();
  1091. }
  1092. bool JPEGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1093. {
  1094. if (!m_context->bitmap)
  1095. return false;
  1096. return m_context->bitmap->set_nonvolatile(was_purged);
  1097. }
  1098. bool JPEGImageDecoderPlugin::initialize()
  1099. {
  1100. return true;
  1101. }
  1102. ErrorOr<bool> JPEGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1103. {
  1104. return data.size() > 3
  1105. && data.data()[0] == 0xFF
  1106. && data.data()[1] == 0xD8
  1107. && data.data()[2] == 0xFF;
  1108. }
  1109. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGImageDecoderPlugin::create(ReadonlyBytes data)
  1110. {
  1111. return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(data.data(), data.size()));
  1112. }
  1113. bool JPEGImageDecoderPlugin::is_animated()
  1114. {
  1115. return false;
  1116. }
  1117. size_t JPEGImageDecoderPlugin::loop_count()
  1118. {
  1119. return 0;
  1120. }
  1121. size_t JPEGImageDecoderPlugin::frame_count()
  1122. {
  1123. return 1;
  1124. }
  1125. ErrorOr<ImageFrameDescriptor> JPEGImageDecoderPlugin::frame(size_t index)
  1126. {
  1127. if (index > 0)
  1128. return Error::from_string_literal("JPEGImageDecoderPlugin: Invalid frame index");
  1129. if (m_context->state == JPEGLoadingContext::State::Error)
  1130. return Error::from_string_literal("JPEGImageDecoderPlugin: Decoding failed");
  1131. if (m_context->state < JPEGLoadingContext::State::BitmapDecoded) {
  1132. if (auto result = decode_jpeg(*m_context); result.is_error()) {
  1133. m_context->state = JPEGLoadingContext::State::Error;
  1134. return result.release_error();
  1135. }
  1136. m_context->state = JPEGLoadingContext::State::BitmapDecoded;
  1137. }
  1138. return ImageFrameDescriptor { m_context->bitmap, 0 };
  1139. }
  1140. ErrorOr<Optional<ReadonlyBytes>> JPEGImageDecoderPlugin::icc_data()
  1141. {
  1142. TRY(decode_header(*m_context));
  1143. if (m_context->icc_data.has_value())
  1144. return *m_context->icc_data;
  1145. return OptionalNone {};
  1146. }
  1147. }