JPEGLoader.cpp 62 KB

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