JPEGLoader.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. // FIXME: This is what we used to do for grayscale,
  1113. // we should at least document it and maybe change it.
  1114. ycbcr_to_rgb(context, macroblocks);
  1115. }
  1116. return {};
  1117. }
  1118. static ErrorOr<void> compose_bitmap(JPEGLoadingContext& context, Vector<Macroblock> const& macroblocks)
  1119. {
  1120. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height }));
  1121. for (u32 y = context.frame.height - 1; y < context.frame.height; y--) {
  1122. const u32 block_row = y / 8;
  1123. const u32 pixel_row = y % 8;
  1124. for (u32 x = 0; x < context.frame.width; x++) {
  1125. const u32 block_column = x / 8;
  1126. auto& block = macroblocks[block_row * context.mblock_meta.hpadded_count + block_column];
  1127. const u32 pixel_column = x % 8;
  1128. const u32 pixel_index = pixel_row * 8 + pixel_column;
  1129. const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] };
  1130. context.bitmap->set_pixel(x, y, color);
  1131. }
  1132. }
  1133. return {};
  1134. }
  1135. static bool is_app_marker(Marker const marker)
  1136. {
  1137. return marker >= JPEG_APPN0 && marker <= JPEG_APPN15;
  1138. }
  1139. static bool is_miscellaneous_or_table_marker(Marker const marker)
  1140. {
  1141. // B.2.4 - Table-specification and miscellaneous marker segment syntax
  1142. // See also B.6 - Summary: Figure B.17 – Flow of marker segment
  1143. bool const is_misc = marker == JPEG_COM || marker == JPEG_DRI || is_app_marker(marker);
  1144. bool const is_table = marker == JPEG_DQT || marker == JPEG_DAC || marker == JPEG_DHT;
  1145. return is_misc || is_table;
  1146. }
  1147. static ErrorOr<void> handle_miscellaneous_or_table(AK::SeekableStream& stream, JPEGLoadingContext& context, Marker const marker)
  1148. {
  1149. if (is_app_marker(marker)) {
  1150. TRY(read_app_marker(stream, context, marker - JPEG_APPN0));
  1151. return {};
  1152. }
  1153. switch (marker) {
  1154. case JPEG_COM:
  1155. case JPEG_DAC:
  1156. dbgln_if(JPEG_DEBUG, "TODO: implement marker \"{:x}\"", marker);
  1157. if (auto result = skip_segment(stream); result.is_error()) {
  1158. dbgln_if(JPEG_DEBUG, "{}: Error skipping marker: {:x}!", TRY(stream.tell()), marker);
  1159. return result.release_error();
  1160. }
  1161. break;
  1162. case JPEG_DHT:
  1163. TRY(read_huffman_table(stream, context));
  1164. break;
  1165. case JPEG_DQT:
  1166. TRY(read_quantization_table(stream, context));
  1167. break;
  1168. case JPEG_DRI:
  1169. TRY(read_restart_interval(stream, context));
  1170. break;
  1171. default:
  1172. dbgln("Unexpected marker: {:x}", marker);
  1173. VERIFY_NOT_REACHED();
  1174. }
  1175. return {};
  1176. }
  1177. static ErrorOr<void> parse_header(AK::SeekableStream& stream, JPEGLoadingContext& context)
  1178. {
  1179. auto marker = TRY(read_marker_at_cursor(stream));
  1180. if (marker != JPEG_SOI) {
  1181. dbgln_if(JPEG_DEBUG, "{}: SOI not found: {:x}!", TRY(stream.tell()), marker);
  1182. return Error::from_string_literal("SOI not found");
  1183. }
  1184. for (;;) {
  1185. marker = TRY(read_marker_at_cursor(stream));
  1186. if (is_miscellaneous_or_table_marker(marker)) {
  1187. TRY(handle_miscellaneous_or_table(stream, context, marker));
  1188. continue;
  1189. }
  1190. // Set frame type if the marker marks a new frame.
  1191. if (is_frame_marker(marker))
  1192. context.frame.type = static_cast<StartOfFrame::FrameType>(marker & 0xF);
  1193. switch (marker) {
  1194. case JPEG_INVALID:
  1195. case JPEG_RST0:
  1196. case JPEG_RST1:
  1197. case JPEG_RST2:
  1198. case JPEG_RST3:
  1199. case JPEG_RST4:
  1200. case JPEG_RST5:
  1201. case JPEG_RST6:
  1202. case JPEG_RST7:
  1203. case JPEG_SOI:
  1204. case JPEG_EOI:
  1205. dbgln_if(JPEG_DEBUG, "{}: Unexpected marker {:x}!", TRY(stream.tell()), marker);
  1206. return Error::from_string_literal("Unexpected marker");
  1207. case JPEG_SOF0:
  1208. case JPEG_SOF2:
  1209. TRY(read_start_of_frame(stream, context));
  1210. context.state = JPEGLoadingContext::FrameDecoded;
  1211. return {};
  1212. default:
  1213. if (auto result = skip_segment(stream); result.is_error()) {
  1214. dbgln_if(JPEG_DEBUG, "{}: Error skipping marker: {:x}!", TRY(stream.tell()), marker);
  1215. return result.release_error();
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. VERIFY_NOT_REACHED();
  1221. }
  1222. static ErrorOr<void> scan_huffman_stream(AK::SeekableStream& stream, HuffmanStreamState& huffman_stream)
  1223. {
  1224. u8 last_byte;
  1225. u8 current_byte = TRY(stream.read_value<u8>());
  1226. for (;;) {
  1227. last_byte = current_byte;
  1228. current_byte = TRY(stream.read_value<u8>());
  1229. if (last_byte == 0xFF) {
  1230. if (current_byte == 0xFF)
  1231. continue;
  1232. if (current_byte == 0x00) {
  1233. current_byte = TRY(stream.read_value<u8>());
  1234. huffman_stream.stream.append(last_byte);
  1235. continue;
  1236. }
  1237. Marker marker = 0xFF00 | current_byte;
  1238. if (marker >= JPEG_RST0 && marker <= JPEG_RST7) {
  1239. huffman_stream.stream.append(marker);
  1240. current_byte = TRY(stream.read_value<u8>());
  1241. continue;
  1242. }
  1243. // Rollback the marker we just read
  1244. TRY(stream.seek(-2, AK::SeekMode::FromCurrentPosition));
  1245. return {};
  1246. } else {
  1247. huffman_stream.stream.append(last_byte);
  1248. }
  1249. }
  1250. VERIFY_NOT_REACHED();
  1251. }
  1252. static ErrorOr<void> decode_header(JPEGLoadingContext& context)
  1253. {
  1254. if (context.state < JPEGLoadingContext::State::HeaderDecoded) {
  1255. if (auto result = parse_header(*context.stream, context); result.is_error()) {
  1256. context.state = JPEGLoadingContext::State::Error;
  1257. return result.release_error();
  1258. }
  1259. if constexpr (JPEG_DEBUG) {
  1260. dbgln("Image width: {}", context.frame.width);
  1261. dbgln("Image height: {}", context.frame.height);
  1262. dbgln("Macroblocks in a row: {}", context.mblock_meta.hpadded_count);
  1263. dbgln("Macroblocks in a column: {}", context.mblock_meta.vpadded_count);
  1264. dbgln("Macroblock meta padded total: {}", context.mblock_meta.padded_total);
  1265. }
  1266. context.state = JPEGLoadingContext::State::HeaderDecoded;
  1267. }
  1268. return {};
  1269. }
  1270. static ErrorOr<Vector<Macroblock>> construct_macroblocks(JPEGLoadingContext& context)
  1271. {
  1272. // B.6 - Summary
  1273. // See: Figure B.16 – Flow of compressed data syntax
  1274. // This function handles the "Multi-scan" loop.
  1275. Vector<Macroblock> macroblocks;
  1276. TRY(macroblocks.try_resize(context.mblock_meta.padded_total));
  1277. Marker marker = TRY(read_marker_at_cursor(*context.stream));
  1278. while (true) {
  1279. if (is_miscellaneous_or_table_marker(marker)) {
  1280. TRY(handle_miscellaneous_or_table(*context.stream, context, marker));
  1281. } else if (marker == JPEG_SOS) {
  1282. TRY(read_start_of_scan(*context.stream, context));
  1283. TRY(scan_huffman_stream(*context.stream, context.current_scan.huffman_stream));
  1284. TRY(decode_huffman_stream(context, macroblocks));
  1285. } else if (marker == JPEG_EOI) {
  1286. return macroblocks;
  1287. } else {
  1288. dbgln_if(JPEG_DEBUG, "{}: Unexpected marker {:x}!", TRY(context.stream->tell()), marker);
  1289. return Error::from_string_literal("Unexpected marker");
  1290. }
  1291. marker = TRY(read_marker_at_cursor(*context.stream));
  1292. }
  1293. }
  1294. static ErrorOr<void> decode_jpeg(JPEGLoadingContext& context)
  1295. {
  1296. TRY(decode_header(context));
  1297. auto macroblocks = TRY(construct_macroblocks(context));
  1298. dequantize(context, macroblocks);
  1299. inverse_dct(context, macroblocks);
  1300. TRY(handle_color_transform(context, macroblocks));
  1301. TRY(compose_bitmap(context, macroblocks));
  1302. context.stream.clear();
  1303. return {};
  1304. }
  1305. JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(NonnullOwnPtr<FixedMemoryStream> stream)
  1306. {
  1307. m_context = make<JPEGLoadingContext>();
  1308. m_context->stream = move(stream);
  1309. }
  1310. JPEGImageDecoderPlugin::~JPEGImageDecoderPlugin() = default;
  1311. IntSize JPEGImageDecoderPlugin::size()
  1312. {
  1313. if (m_context->state == JPEGLoadingContext::State::Error)
  1314. return {};
  1315. if (m_context->state >= JPEGLoadingContext::State::FrameDecoded)
  1316. return { m_context->frame.width, m_context->frame.height };
  1317. return {};
  1318. }
  1319. void JPEGImageDecoderPlugin::set_volatile()
  1320. {
  1321. if (m_context->bitmap)
  1322. m_context->bitmap->set_volatile();
  1323. }
  1324. bool JPEGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1325. {
  1326. if (!m_context->bitmap)
  1327. return false;
  1328. return m_context->bitmap->set_nonvolatile(was_purged);
  1329. }
  1330. bool JPEGImageDecoderPlugin::initialize()
  1331. {
  1332. return true;
  1333. }
  1334. bool JPEGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1335. {
  1336. return data.size() > 3
  1337. && data.data()[0] == 0xFF
  1338. && data.data()[1] == 0xD8
  1339. && data.data()[2] == 0xFF;
  1340. }
  1341. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGImageDecoderPlugin::create(ReadonlyBytes data)
  1342. {
  1343. auto stream = TRY(try_make<FixedMemoryStream>(data));
  1344. return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(move(stream)));
  1345. }
  1346. bool JPEGImageDecoderPlugin::is_animated()
  1347. {
  1348. return false;
  1349. }
  1350. size_t JPEGImageDecoderPlugin::loop_count()
  1351. {
  1352. return 0;
  1353. }
  1354. size_t JPEGImageDecoderPlugin::frame_count()
  1355. {
  1356. return 1;
  1357. }
  1358. ErrorOr<ImageFrameDescriptor> JPEGImageDecoderPlugin::frame(size_t index)
  1359. {
  1360. if (index > 0)
  1361. return Error::from_string_literal("JPEGImageDecoderPlugin: Invalid frame index");
  1362. if (m_context->state == JPEGLoadingContext::State::Error)
  1363. return Error::from_string_literal("JPEGImageDecoderPlugin: Decoding failed");
  1364. if (m_context->state < JPEGLoadingContext::State::BitmapDecoded) {
  1365. if (auto result = decode_jpeg(*m_context); result.is_error()) {
  1366. m_context->state = JPEGLoadingContext::State::Error;
  1367. return result.release_error();
  1368. }
  1369. m_context->state = JPEGLoadingContext::State::BitmapDecoded;
  1370. }
  1371. return ImageFrameDescriptor { m_context->bitmap, 0 };
  1372. }
  1373. ErrorOr<Optional<ReadonlyBytes>> JPEGImageDecoderPlugin::icc_data()
  1374. {
  1375. TRY(decode_header(*m_context));
  1376. if (m_context->icc_data.has_value())
  1377. return *m_context->icc_data;
  1378. return OptionalNone {};
  1379. }
  1380. }