JPEGLoader.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Endian.h>
  8. #include <AK/Error.h>
  9. #include <AK/FixedArray.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/Math.h>
  12. #include <AK/MemoryStream.h>
  13. #include <AK/String.h>
  14. #include <AK/Try.h>
  15. #include <AK/Vector.h>
  16. #include <LibGfx/JPEGLoader.h>
  17. #define JPEG_INVALID 0X0000
  18. // 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_APPNA 0XFFEA
  30. #define JPEG_APPNB 0XFFEB
  31. #define JPEG_APPNC 0XFFEC
  32. #define JPEG_APPND 0XFFED
  33. #define JPEG_APPNE 0xFFEE
  34. #define JPEG_APPNF 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. // See the note on Figure B.4 - Scan header syntax
  179. bool are_components_interleaved() const
  180. {
  181. return components.size() != 1;
  182. }
  183. };
  184. struct JPEGLoadingContext {
  185. enum State {
  186. NotDecoded = 0,
  187. Error,
  188. FrameDecoded,
  189. HeaderDecoded,
  190. BitmapDecoded
  191. };
  192. State state { State::NotDecoded };
  193. u8 const* data { nullptr };
  194. size_t data_size { 0 };
  195. u32 luma_table[64] = { 0 };
  196. u32 chroma_table[64] = { 0 };
  197. StartOfFrame frame;
  198. u8 hsample_factor { 0 };
  199. u8 vsample_factor { 0 };
  200. Scan current_scan;
  201. Vector<Component, 3> components;
  202. RefPtr<Gfx::Bitmap> bitmap;
  203. u16 dc_restart_interval { 0 };
  204. HashMap<u8, HuffmanTableSpec> dc_tables;
  205. HashMap<u8, HuffmanTableSpec> ac_tables;
  206. i32 previous_dc_values[3] = { 0 };
  207. MacroblockMeta mblock_meta;
  208. OwnPtr<FixedMemoryStream> stream;
  209. Optional<ICCMultiChunkState> icc_multi_chunk_state;
  210. Optional<ByteBuffer> icc_data;
  211. };
  212. static void generate_huffman_codes(HuffmanTableSpec& table)
  213. {
  214. unsigned code = 0;
  215. for (auto number_of_codes : table.code_counts) {
  216. for (int i = 0; i < number_of_codes; i++)
  217. table.codes.append(code++);
  218. code <<= 1;
  219. }
  220. }
  221. static ErrorOr<size_t> read_huffman_bits(HuffmanStreamState& hstream, size_t count = 1)
  222. {
  223. if (count > (8 * sizeof(size_t))) {
  224. dbgln_if(JPEG_DEBUG, "Can't read {} bits at once!", count);
  225. return Error::from_string_literal("Reading too much huffman bits at once");
  226. }
  227. size_t value = 0;
  228. while (count--) {
  229. if (hstream.byte_offset >= hstream.stream.size()) {
  230. dbgln_if(JPEG_DEBUG, "Huffman stream exhausted. This could be an error!");
  231. return Error::from_string_literal("Huffman stream exhausted.");
  232. }
  233. u8 current_byte = hstream.stream[hstream.byte_offset];
  234. u8 current_bit = 1u & (u32)(current_byte >> (7 - hstream.bit_offset)); // MSB first.
  235. hstream.bit_offset++;
  236. value = (value << 1) | (size_t)current_bit;
  237. if (hstream.bit_offset == 8) {
  238. hstream.byte_offset++;
  239. hstream.bit_offset = 0;
  240. }
  241. }
  242. return value;
  243. }
  244. static ErrorOr<u8> get_next_symbol(HuffmanStreamState& hstream, HuffmanTableSpec const& table)
  245. {
  246. unsigned code = 0;
  247. size_t code_cursor = 0;
  248. for (int i = 0; i < 16; i++) { // Codes can't be longer than 16 bits.
  249. auto result = TRY(read_huffman_bits(hstream));
  250. code = (code << 1) | (i32)result;
  251. for (int j = 0; j < table.code_counts[i]; j++) {
  252. if (code == table.codes[code_cursor])
  253. return table.symbols[code_cursor];
  254. code_cursor++;
  255. }
  256. }
  257. dbgln_if(JPEG_DEBUG, "If you're seeing this...the jpeg decoder needs to support more kinds of JPEGs!");
  258. return Error::from_string_literal("This kind of JPEG is not yet supported by the decoder");
  259. }
  260. static inline i32* get_component(Macroblock& block, unsigned component)
  261. {
  262. switch (component) {
  263. case 0:
  264. return block.y;
  265. case 1:
  266. return block.cb;
  267. default:
  268. return block.cr;
  269. }
  270. }
  271. static ErrorOr<void> add_dc(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
  272. {
  273. auto& dc_table = context.dc_tables.find(scan_component.dc_destination_id)->value;
  274. auto& scan = context.current_scan;
  275. // For DC coefficients, symbol encodes the length of the coefficient.
  276. auto dc_length = TRY(get_next_symbol(scan.huffman_stream, dc_table));
  277. if (dc_length > 11) {
  278. dbgln_if(JPEG_DEBUG, "DC coefficient too long: {}!", dc_length);
  279. return Error::from_string_literal("DC coefficient too long");
  280. }
  281. // DC coefficients are encoded as the difference between previous and current DC values.
  282. i32 dc_diff = TRY(read_huffman_bits(scan.huffman_stream, dc_length));
  283. // If MSB in diff is 0, the difference is -ve. Otherwise +ve.
  284. if (dc_length != 0 && dc_diff < (1 << (dc_length - 1)))
  285. dc_diff -= (1 << dc_length) - 1;
  286. auto* select_component = get_component(macroblock, scan_component.component.index);
  287. auto& previous_dc = context.previous_dc_values[scan_component.component.index];
  288. select_component[0] = previous_dc += dc_diff;
  289. return {};
  290. }
  291. static ErrorOr<void> add_ac(JPEGLoadingContext& context, Macroblock& macroblock, ScanComponent const& scan_component)
  292. {
  293. auto& ac_table = context.ac_tables.find(scan_component.ac_destination_id)->value;
  294. auto* select_component = get_component(macroblock, scan_component.component.index);
  295. auto& scan = context.current_scan;
  296. // Compute the AC coefficients.
  297. // 0th coefficient is the dc, which is already handled
  298. auto first_coefficient = max(1, scan.spectral_selection_start);
  299. for (int j = first_coefficient; j <= scan.spectral_selection_end;) {
  300. // AC symbols encode 2 pieces of information, the high 4 bits represent
  301. // number of zeroes to be stuffed before reading the coefficient. Low 4
  302. // bits represent the magnitude of the coefficient.
  303. auto ac_symbol = TRY(get_next_symbol(scan.huffman_stream, ac_table));
  304. if (ac_symbol == 0)
  305. break;
  306. // ac_symbol = JPEG_ZRL means we need to skip 16 zeroes.
  307. u8 run_length = ac_symbol == JPEG_ZRL ? 16 : ac_symbol >> 4;
  308. j += run_length;
  309. if (j > scan.spectral_selection_end) {
  310. dbgln_if(JPEG_DEBUG, "Run-length exceeded boundaries. Cursor: {}, Skipping: {}!", j, run_length);
  311. return Error::from_string_literal("Run-length exceeded boundaries");
  312. }
  313. u8 coeff_length = ac_symbol & 0x0F;
  314. if (coeff_length > 10) {
  315. dbgln_if(JPEG_DEBUG, "AC coefficient too long: {}!", coeff_length);
  316. return Error::from_string_literal("AC coefficient too long");
  317. }
  318. if (coeff_length != 0) {
  319. i32 ac_coefficient = TRY(read_huffman_bits(scan.huffman_stream, coeff_length));
  320. if (ac_coefficient < (1 << (coeff_length - 1)))
  321. ac_coefficient -= (1 << coeff_length) - 1;
  322. select_component[zigzag_map[j++]] = ac_coefficient;
  323. }
  324. }
  325. return {};
  326. }
  327. /**
  328. * Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
  329. * Depending on the sampling factors, we may not see triples of y, cb, cr in that
  330. * order. If sample factors differ from one, we'll read more than one block of y-
  331. * coefficients before we get to read a cb-cr block.
  332. * In the function below, `hcursor` and `vcursor` denote the location of the block
  333. * we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
  334. * that iterate over the vertical and horizontal subsampling factors, respectively.
  335. * When we finish one iteration of the innermost loop, we'll have the coefficients
  336. * of one of the components of block at position `mb_index`. When the outermost loop
  337. * finishes first iteration, we'll have all the luminance coefficients for all the
  338. * macroblocks that share the chrominance data. Next two iterations (assuming that
  339. * we are dealing with three components) will fill up the blocks with chroma data.
  340. */
  341. static ErrorOr<void> build_macroblocks(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks, u32 hcursor, u32 vcursor)
  342. {
  343. for (auto const& scan_component : context.current_scan.components) {
  344. if (scan_component.dc_destination_id >= context.dc_tables.size())
  345. return Error::from_string_literal("DC destination ID is greater than number of DC tables");
  346. if (scan_component.ac_destination_id >= context.ac_tables.size())
  347. return Error::from_string_literal("AC destination ID is greater than number of AC tables");
  348. for (u8 vfactor_i = 0; vfactor_i < scan_component.component.vsample_factor; vfactor_i++) {
  349. for (u8 hfactor_i = 0; hfactor_i < scan_component.component.hsample_factor; hfactor_i++) {
  350. // A.2.3 - Interleaved order
  351. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  352. if (!context.current_scan.are_components_interleaved())
  353. mb_index = vcursor * context.mblock_meta.hpadded_count + (hfactor_i + (hcursor * scan_component.component.vsample_factor) + (vfactor_i * scan_component.component.hsample_factor));
  354. Macroblock& block = macroblocks[mb_index];
  355. if (context.current_scan.spectral_selection_start == 0)
  356. TRY(add_dc(context, block, scan_component));
  357. TRY(add_ac(context, block, scan_component));
  358. }
  359. }
  360. }
  361. return {};
  362. }
  363. static bool is_dct_based(StartOfFrame::FrameType frame_type)
  364. {
  365. return frame_type == StartOfFrame::FrameType::Baseline_DCT
  366. || frame_type == StartOfFrame::FrameType::Extended_Sequential_DCT
  367. || frame_type == StartOfFrame::FrameType::Progressive_DCT
  368. || frame_type == StartOfFrame::FrameType::Differential_Sequential_DCT
  369. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT
  370. || frame_type == StartOfFrame::FrameType::Progressive_DCT_Arithmetic
  371. || frame_type == StartOfFrame::FrameType::Differential_Sequential_DCT_Arithmetic
  372. || frame_type == StartOfFrame::FrameType::Differential_Progressive_DCT_Arithmetic;
  373. }
  374. static void reset_decoder(JPEGLoadingContext& context)
  375. {
  376. // E.2.4 Control procedure for decoding a restart interval
  377. if (is_dct_based(context.frame.type)) {
  378. context.previous_dc_values[0] = 0;
  379. context.previous_dc_values[1] = 0;
  380. context.previous_dc_values[2] = 0;
  381. return;
  382. }
  383. VERIFY_NOT_REACHED();
  384. }
  385. static ErrorOr<void> decode_huffman_stream(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks)
  386. {
  387. // Compute huffman codes for DC and AC tables.
  388. for (auto it = context.dc_tables.begin(); it != context.dc_tables.end(); ++it)
  389. generate_huffman_codes(it->value);
  390. for (auto it = context.ac_tables.begin(); it != context.ac_tables.end(); ++it)
  391. generate_huffman_codes(it->value);
  392. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  393. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  394. u32 i = vcursor * context.mblock_meta.hpadded_count + hcursor;
  395. auto& huffman_stream = context.current_scan.huffman_stream;
  396. if (context.dc_restart_interval > 0) {
  397. if (i != 0 && i % (context.dc_restart_interval * context.vsample_factor * context.hsample_factor) == 0) {
  398. reset_decoder(context);
  399. // Restart markers are stored in byte boundaries. Advance the huffman stream cursor to
  400. // the 0th bit of the next byte.
  401. if (huffman_stream.byte_offset < huffman_stream.stream.size()) {
  402. if (huffman_stream.bit_offset > 0) {
  403. huffman_stream.bit_offset = 0;
  404. huffman_stream.byte_offset++;
  405. }
  406. // Skip the restart marker (RSTn).
  407. huffman_stream.byte_offset++;
  408. }
  409. }
  410. }
  411. if (auto result = build_macroblocks(context, macroblocks, hcursor, vcursor); result.is_error()) {
  412. if constexpr (JPEG_DEBUG) {
  413. dbgln("Failed to build Macroblock {}: {}", i, result.error());
  414. dbgln("Huffman stream byte offset {}", huffman_stream.byte_offset);
  415. dbgln("Huffman stream bit offset {}", huffman_stream.bit_offset);
  416. }
  417. return result.release_error();
  418. }
  419. }
  420. }
  421. return {};
  422. }
  423. static inline ErrorOr<void> ensure_bounds_okay(const size_t cursor, const size_t delta, const size_t bound)
  424. {
  425. if (Checked<size_t>::addition_would_overflow(delta, cursor))
  426. return Error::from_string_literal("Bounds are not ok: addition would overflow");
  427. if (delta + cursor >= bound)
  428. return Error::from_string_literal("Bounds are not ok");
  429. return {};
  430. }
  431. static bool is_frame_marker(Marker const marker)
  432. {
  433. // B.1.1.3 - Marker assignments
  434. bool const is_sof_marker = marker >= JPEG_SOF0 && marker <= JPEG_SOF15;
  435. // Start of frame markers are valid for JPEG_SOF0 to JPEG_SOF15 except number 4, 8 (reserved) and 12.
  436. bool const is_defined_marker = marker != JPEG_DHT && marker != 0xFFC8 && marker != JPEG_DAC;
  437. return is_sof_marker && is_defined_marker;
  438. }
  439. static inline bool is_supported_marker(Marker const marker)
  440. {
  441. if (marker >= JPEG_APPN0 && marker <= JPEG_APPNF) {
  442. if (marker != JPEG_APPN0)
  443. dbgln_if(JPEG_DEBUG, "{:#04x} not supported yet. The decoder may fail!", marker);
  444. return true;
  445. }
  446. if (marker >= JPEG_RESERVED1 && marker <= JPEG_RESERVEDD)
  447. return true;
  448. if (marker >= JPEG_RST0 && marker <= JPEG_RST7)
  449. return true;
  450. switch (marker) {
  451. case JPEG_COM:
  452. case JPEG_DHP:
  453. case JPEG_EXP:
  454. case JPEG_DHT:
  455. case JPEG_DQT:
  456. case JPEG_DRI:
  457. case JPEG_EOI:
  458. case JPEG_SOF0:
  459. case JPEG_SOI:
  460. case JPEG_SOS:
  461. return true;
  462. }
  463. if (is_frame_marker(marker))
  464. dbgln_if(JPEG_DEBUG, "Decoding this frame-type (SOF{}) is not currently supported. Decoder will fail!", marker & 0xf);
  465. return false;
  466. }
  467. static inline ErrorOr<Marker> read_marker_at_cursor(Stream& stream)
  468. {
  469. u16 marker = TRY(stream.read_value<BigEndian<u16>>());
  470. if (is_supported_marker(marker))
  471. return marker;
  472. if (marker != 0xFFFF)
  473. return JPEG_INVALID;
  474. u8 next;
  475. do {
  476. next = TRY(stream.read_value<u8>());
  477. if (next == 0x00)
  478. return JPEG_INVALID;
  479. } while (next == 0xFF);
  480. marker = 0xFF00 | (u16)next;
  481. return is_supported_marker(marker) ? marker : JPEG_INVALID;
  482. }
  483. static ErrorOr<void> read_start_of_scan(AK::SeekableStream& stream, JPEGLoadingContext& context)
  484. {
  485. // B.2.3 - Scan header syntax
  486. if (context.state < JPEGLoadingContext::State::FrameDecoded) {
  487. dbgln_if(JPEG_DEBUG, "{}: SOS found before reading a SOF!", TRY(stream.tell()));
  488. return Error::from_string_literal("SOS found before reading a SOF");
  489. }
  490. u16 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  491. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  492. u8 const component_count = TRY(stream.read_value<u8>());
  493. Scan current_scan;
  494. current_scan.huffman_stream.stream.ensure_capacity(50 * KiB);
  495. Optional<u8> last_read;
  496. u8 component_read = 0;
  497. for (auto& component : context.components) {
  498. // See the Csj paragraph:
  499. // [...] the ordering in the scan header shall follow the ordering in the frame header.
  500. if (component_read == component_count)
  501. break;
  502. if (!last_read.has_value())
  503. last_read = TRY(stream.read_value<u8>());
  504. if (component.id != *last_read)
  505. continue;
  506. u8 table_ids = TRY(stream.read_value<u8>());
  507. current_scan.components.empend(component, static_cast<u8>(table_ids >> 4), static_cast<u8>(table_ids & 0x0F));
  508. component_read++;
  509. last_read.clear();
  510. }
  511. current_scan.spectral_selection_start = TRY(stream.read_value<u8>());
  512. current_scan.spectral_selection_end = TRY(stream.read_value<u8>());
  513. current_scan.successive_approximation = TRY(stream.read_value<u8>());
  514. dbgln_if(JPEG_DEBUG, "Start of Selection: {}, End of Selection: {}, Successive Approximation: {}",
  515. current_scan.spectral_selection_start,
  516. current_scan.spectral_selection_end,
  517. current_scan.successive_approximation);
  518. // The three values should be fixed for baseline JPEGs utilizing sequential DCT.
  519. if (current_scan.spectral_selection_start != 0 || current_scan.spectral_selection_end != 63 || current_scan.successive_approximation != 0) {
  520. dbgln_if(JPEG_DEBUG, "{}: ERROR! Start of Selection: {}, End of Selection: {}, Successive Approximation: {}!",
  521. TRY(stream.tell()),
  522. current_scan.spectral_selection_start,
  523. current_scan.spectral_selection_end,
  524. current_scan.successive_approximation);
  525. return Error::from_string_literal("Spectral selection is not [0,63] or successive approximation is not null");
  526. }
  527. context.current_scan = move(current_scan);
  528. return {};
  529. }
  530. static ErrorOr<void> read_restart_interval(AK::SeekableStream& stream, JPEGLoadingContext& context)
  531. {
  532. // B.2.4.4 - Restart interval definition syntax
  533. u16 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  534. if (bytes_to_read != 2) {
  535. dbgln_if(JPEG_DEBUG, "{}: Malformed DRI marker found!", TRY(stream.tell()));
  536. return Error::from_string_literal("Malformed DRI marker found");
  537. }
  538. context.dc_restart_interval = TRY(stream.read_value<BigEndian<u16>>());
  539. return {};
  540. }
  541. static ErrorOr<void> read_huffman_table(AK::SeekableStream& stream, JPEGLoadingContext& context)
  542. {
  543. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  544. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  545. bytes_to_read -= 2;
  546. while (bytes_to_read > 0) {
  547. HuffmanTableSpec table;
  548. u8 table_info = TRY(stream.read_value<u8>());
  549. u8 table_type = table_info >> 4;
  550. u8 table_destination_id = table_info & 0x0F;
  551. if (table_type > 1) {
  552. dbgln_if(JPEG_DEBUG, "{}: Unrecognized huffman table: {}!", TRY(stream.tell()), table_type);
  553. return Error::from_string_literal("Unrecognized huffman table");
  554. }
  555. if (table_destination_id > 1) {
  556. dbgln_if(JPEG_DEBUG, "{}: Invalid huffman table destination id: {}!", TRY(stream.tell()), table_destination_id);
  557. return Error::from_string_literal("Invalid huffman table destination id");
  558. }
  559. table.type = table_type;
  560. table.destination_id = table_destination_id;
  561. u32 total_codes = 0;
  562. // Read code counts. At each index K, the value represents the number of K+1 bit codes in this header.
  563. for (int i = 0; i < 16; i++) {
  564. u8 count = TRY(stream.read_value<u8>());
  565. total_codes += count;
  566. table.code_counts[i] = count;
  567. }
  568. table.codes.ensure_capacity(total_codes);
  569. // Read symbols. Read X bytes, where X is the sum of the counts of codes read in the previous step.
  570. for (u32 i = 0; i < total_codes; i++) {
  571. u8 symbol = TRY(stream.read_value<u8>());
  572. table.symbols.append(symbol);
  573. }
  574. auto& huffman_table = table.type == 0 ? context.dc_tables : context.ac_tables;
  575. huffman_table.set(table.destination_id, table);
  576. VERIFY(huffman_table.size() <= 2);
  577. bytes_to_read -= 1 + 16 + total_codes;
  578. }
  579. if (bytes_to_read != 0) {
  580. dbgln_if(JPEG_DEBUG, "{}: Extra bytes detected in huffman header!", TRY(stream.tell()));
  581. return Error::from_string_literal("Extra bytes detected in huffman header");
  582. }
  583. return {};
  584. }
  585. static ErrorOr<void> read_icc_profile(SeekableStream& stream, JPEGLoadingContext& context, int bytes_to_read)
  586. {
  587. if (bytes_to_read <= 2)
  588. return Error::from_string_literal("icc marker too small");
  589. auto chunk_sequence_number = TRY(stream.read_value<u8>()); // 1-based
  590. auto number_of_chunks = TRY(stream.read_value<u8>());
  591. bytes_to_read -= 2;
  592. if (!context.icc_multi_chunk_state.has_value())
  593. context.icc_multi_chunk_state.emplace(ICCMultiChunkState { 0, TRY(FixedArray<ByteBuffer>::create(number_of_chunks)) });
  594. auto& chunk_state = context.icc_multi_chunk_state;
  595. if (chunk_state->seen_number_of_icc_chunks >= number_of_chunks)
  596. return Error::from_string_literal("Too many ICC chunks");
  597. if (chunk_state->chunks.size() != number_of_chunks)
  598. return Error::from_string_literal("Inconsistent number of total ICC chunks");
  599. if (chunk_sequence_number == 0)
  600. return Error::from_string_literal("ICC chunk sequence number not 1 based");
  601. u8 index = chunk_sequence_number - 1;
  602. if (index >= chunk_state->chunks.size())
  603. return Error::from_string_literal("ICC chunk sequence number larger than number of chunks");
  604. if (!chunk_state->chunks[index].is_empty())
  605. return Error::from_string_literal("Duplicate ICC chunk at sequence number");
  606. chunk_state->chunks[index] = TRY(ByteBuffer::create_zeroed(bytes_to_read));
  607. TRY(stream.read_entire_buffer(chunk_state->chunks[index]));
  608. chunk_state->seen_number_of_icc_chunks++;
  609. if (chunk_state->seen_number_of_icc_chunks != chunk_state->chunks.size())
  610. return {};
  611. if (number_of_chunks == 1) {
  612. context.icc_data = move(chunk_state->chunks[0]);
  613. return {};
  614. }
  615. size_t total_size = 0;
  616. for (auto const& chunk : chunk_state->chunks)
  617. total_size += chunk.size();
  618. auto icc_bytes = TRY(ByteBuffer::create_zeroed(total_size));
  619. size_t start = 0;
  620. for (auto const& chunk : chunk_state->chunks) {
  621. memcpy(icc_bytes.data() + start, chunk.data(), chunk.size());
  622. start += chunk.size();
  623. }
  624. context.icc_data = move(icc_bytes);
  625. return {};
  626. }
  627. static ErrorOr<void> read_app_marker(SeekableStream& stream, JPEGLoadingContext& context, int app_marker_number)
  628. {
  629. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  630. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  631. if (bytes_to_read <= 2)
  632. return Error::from_string_literal("app marker size too small");
  633. bytes_to_read -= 2;
  634. StringBuilder builder;
  635. for (;;) {
  636. if (bytes_to_read == 0)
  637. return Error::from_string_literal("app marker size too small for identifier");
  638. auto c = TRY(stream.read_value<char>());
  639. bytes_to_read--;
  640. if (c == '\0')
  641. break;
  642. TRY(builder.try_append(c));
  643. }
  644. auto app_id = TRY(builder.to_string());
  645. if (app_marker_number == 2 && app_id == "ICC_PROFILE"sv)
  646. return read_icc_profile(stream, context, bytes_to_read);
  647. return stream.discard(bytes_to_read);
  648. }
  649. static inline bool validate_luma_and_modify_context(Component const& luma, JPEGLoadingContext& context)
  650. {
  651. if ((luma.hsample_factor == 1 || luma.hsample_factor == 2) && (luma.vsample_factor == 1 || luma.vsample_factor == 2)) {
  652. context.mblock_meta.hpadded_count += luma.hsample_factor == 1 ? 0 : context.mblock_meta.hcount % 2;
  653. context.mblock_meta.vpadded_count += luma.vsample_factor == 1 ? 0 : context.mblock_meta.vcount % 2;
  654. context.mblock_meta.padded_total = context.mblock_meta.hpadded_count * context.mblock_meta.vpadded_count;
  655. // For easy reference to relevant sample factors.
  656. context.hsample_factor = luma.hsample_factor;
  657. context.vsample_factor = luma.vsample_factor;
  658. if constexpr (JPEG_DEBUG) {
  659. dbgln("Horizontal Subsampling Factor: {}", luma.hsample_factor);
  660. dbgln("Vertical Subsampling Factor: {}", luma.vsample_factor);
  661. }
  662. return true;
  663. }
  664. return false;
  665. }
  666. static inline void set_macroblock_metadata(JPEGLoadingContext& context)
  667. {
  668. context.mblock_meta.hcount = (context.frame.width + 7) / 8;
  669. context.mblock_meta.vcount = (context.frame.height + 7) / 8;
  670. context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
  671. context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
  672. context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
  673. }
  674. static ErrorOr<void> read_start_of_frame(AK::SeekableStream& stream, JPEGLoadingContext& context)
  675. {
  676. if (context.state == JPEGLoadingContext::FrameDecoded) {
  677. dbgln_if(JPEG_DEBUG, "{}: SOF repeated!", TRY(stream.tell()));
  678. return Error::from_string_literal("SOF repeated");
  679. }
  680. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>());
  681. bytes_to_read -= 2;
  682. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  683. context.frame.precision = TRY(stream.read_value<u8>());
  684. if (context.frame.precision != 8) {
  685. dbgln_if(JPEG_DEBUG, "{}: SOF precision != 8!", TRY(stream.tell()));
  686. return Error::from_string_literal("SOF precision != 8");
  687. }
  688. context.frame.height = TRY(stream.read_value<BigEndian<u16>>());
  689. context.frame.width = TRY(stream.read_value<BigEndian<u16>>());
  690. if (!context.frame.width || !context.frame.height) {
  691. dbgln_if(JPEG_DEBUG, "{}: ERROR! Image height: {}, Image width: {}!", TRY(stream.tell()), context.frame.height, context.frame.width);
  692. return Error::from_string_literal("Image frame height of width null");
  693. }
  694. if (context.frame.width > maximum_width_for_decoded_images || context.frame.height > maximum_height_for_decoded_images) {
  695. dbgln("This JPEG is too large for comfort: {}x{}", context.frame.width, context.frame.height);
  696. return Error::from_string_literal("JPEG too large for comfort");
  697. }
  698. set_macroblock_metadata(context);
  699. auto component_count = TRY(stream.read_value<u8>());
  700. if (component_count != 1 && component_count != 3) {
  701. dbgln_if(JPEG_DEBUG, "{}: Unsupported number of components in SOF: {}!", TRY(stream.tell()), component_count);
  702. return Error::from_string_literal("Unsupported number of components in SOF");
  703. }
  704. for (u8 i = 0; i < component_count; i++) {
  705. Component component;
  706. component.id = TRY(stream.read_value<u8>());
  707. component.index = i;
  708. u8 subsample_factors = TRY(stream.read_value<u8>());
  709. component.hsample_factor = subsample_factors >> 4;
  710. component.vsample_factor = subsample_factors & 0x0F;
  711. if (i == 0) {
  712. // By convention, downsampling is applied only on chroma components. So we should
  713. // hope to see the maximum sampling factor in the luma component.
  714. if (!validate_luma_and_modify_context(component, context)) {
  715. dbgln_if(JPEG_DEBUG, "{}: Unsupported luma subsampling factors: horizontal: {}, vertical: {}",
  716. TRY(stream.tell()),
  717. component.hsample_factor,
  718. component.vsample_factor);
  719. return Error::from_string_literal("Unsupported luma subsampling factors");
  720. }
  721. } else {
  722. if (component.hsample_factor != 1 || component.vsample_factor != 1) {
  723. dbgln_if(JPEG_DEBUG, "{}: Unsupported chroma subsampling factors: horizontal: {}, vertical: {}",
  724. TRY(stream.tell()),
  725. component.hsample_factor,
  726. component.vsample_factor);
  727. return Error::from_string_literal("Unsupported chroma subsampling factors");
  728. }
  729. }
  730. component.qtable_id = TRY(stream.read_value<u8>());
  731. if (component.qtable_id > 1) {
  732. dbgln_if(JPEG_DEBUG, "{}: Unsupported quantization table id: {}!", TRY(stream.tell()), component.qtable_id);
  733. return Error::from_string_literal("Unsupported quantization table id");
  734. }
  735. context.components.append(move(component));
  736. }
  737. return {};
  738. }
  739. static ErrorOr<void> read_quantization_table(AK::SeekableStream& stream, JPEGLoadingContext& context)
  740. {
  741. i32 bytes_to_read = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  742. TRY(ensure_bounds_okay(TRY(stream.tell()), bytes_to_read, context.data_size));
  743. while (bytes_to_read > 0) {
  744. u8 info_byte = TRY(stream.read_value<u8>());
  745. u8 element_unit_hint = info_byte >> 4;
  746. if (element_unit_hint > 1) {
  747. dbgln_if(JPEG_DEBUG, "{}: Unsupported unit hint in quantization table: {}!", TRY(stream.tell()), element_unit_hint);
  748. return Error::from_string_literal("Unsupported unit hint in quantization table");
  749. }
  750. u8 table_id = info_byte & 0x0F;
  751. if (table_id > 1) {
  752. dbgln_if(JPEG_DEBUG, "{}: Unsupported quantization table id: {}!", TRY(stream.tell()), table_id);
  753. return Error::from_string_literal("Unsupported quantization table id");
  754. }
  755. u32* table = table_id == 0 ? context.luma_table : context.chroma_table;
  756. for (int i = 0; i < 64; i++) {
  757. if (element_unit_hint == 0) {
  758. u8 tmp = TRY(stream.read_value<u8>());
  759. table[zigzag_map[i]] = tmp;
  760. } else {
  761. table[zigzag_map[i]] = TRY(stream.read_value<BigEndian<u16>>());
  762. }
  763. }
  764. bytes_to_read -= 1 + (element_unit_hint == 0 ? 64 : 128);
  765. }
  766. if (bytes_to_read != 0) {
  767. dbgln_if(JPEG_DEBUG, "{}: Invalid length for one or more quantization tables!", TRY(stream.tell()));
  768. return Error::from_string_literal("Invalid length for one or more quantization tables");
  769. }
  770. return {};
  771. }
  772. static ErrorOr<void> skip_segment(Stream& stream)
  773. {
  774. u16 bytes_to_skip = TRY(stream.read_value<BigEndian<u16>>()) - 2;
  775. TRY(stream.discard(bytes_to_skip));
  776. return {};
  777. }
  778. static void dequantize(JPEGLoadingContext& context, Vector<Macroblock>& macroblocks)
  779. {
  780. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  781. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  782. for (u32 i = 0; i < context.components.size(); i++) {
  783. auto& component = context.components[i];
  784. u32 const* table = component.qtable_id == 0 ? context.luma_table : context.chroma_table;
  785. for (u32 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  786. for (u32 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  787. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  788. Macroblock& block = macroblocks[mb_index];
  789. int* block_component = get_component(block, i);
  790. for (u32 k = 0; k < 64; k++)
  791. block_component[k] *= table[k];
  792. }
  793. }
  794. }
  795. }
  796. }
  797. }
  798. static void inverse_dct(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  799. {
  800. static float const m0 = 2.0f * AK::cos(1.0f / 16.0f * 2.0f * AK::Pi<float>);
  801. static float const m1 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  802. static float const m3 = 2.0f * AK::cos(2.0f / 16.0f * 2.0f * AK::Pi<float>);
  803. static float const m5 = 2.0f * AK::cos(3.0f / 16.0f * 2.0f * AK::Pi<float>);
  804. static float const m2 = m0 - m5;
  805. static float const m4 = m0 + m5;
  806. static float const s0 = AK::cos(0.0f / 16.0f * AK::Pi<float>) * AK::rsqrt(8.0f);
  807. static float const s1 = AK::cos(1.0f / 16.0f * AK::Pi<float>) / 2.0f;
  808. static float const s2 = AK::cos(2.0f / 16.0f * AK::Pi<float>) / 2.0f;
  809. static float const s3 = AK::cos(3.0f / 16.0f * AK::Pi<float>) / 2.0f;
  810. static float const s4 = AK::cos(4.0f / 16.0f * AK::Pi<float>) / 2.0f;
  811. static float const s5 = AK::cos(5.0f / 16.0f * AK::Pi<float>) / 2.0f;
  812. static float const s6 = AK::cos(6.0f / 16.0f * AK::Pi<float>) / 2.0f;
  813. static float const s7 = AK::cos(7.0f / 16.0f * AK::Pi<float>) / 2.0f;
  814. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  815. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  816. for (u32 component_i = 0; component_i < context.components.size(); component_i++) {
  817. auto& component = context.components[component_i];
  818. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  819. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  820. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  821. Macroblock& block = macroblocks[mb_index];
  822. i32* block_component = get_component(block, component_i);
  823. for (u32 k = 0; k < 8; ++k) {
  824. float const g0 = block_component[0 * 8 + k] * s0;
  825. float const g1 = block_component[4 * 8 + k] * s4;
  826. float const g2 = block_component[2 * 8 + k] * s2;
  827. float const g3 = block_component[6 * 8 + k] * s6;
  828. float const g4 = block_component[5 * 8 + k] * s5;
  829. float const g5 = block_component[1 * 8 + k] * s1;
  830. float const g6 = block_component[7 * 8 + k] * s7;
  831. float const g7 = block_component[3 * 8 + k] * s3;
  832. float const f0 = g0;
  833. float const f1 = g1;
  834. float const f2 = g2;
  835. float const f3 = g3;
  836. float const f4 = g4 - g7;
  837. float const f5 = g5 + g6;
  838. float const f6 = g5 - g6;
  839. float const f7 = g4 + g7;
  840. float const e0 = f0;
  841. float const e1 = f1;
  842. float const e2 = f2 - f3;
  843. float const e3 = f2 + f3;
  844. float const e4 = f4;
  845. float const e5 = f5 - f7;
  846. float const e6 = f6;
  847. float const e7 = f5 + f7;
  848. float const e8 = f4 + f6;
  849. float const d0 = e0;
  850. float const d1 = e1;
  851. float const d2 = e2 * m1;
  852. float const d3 = e3;
  853. float const d4 = e4 * m2;
  854. float const d5 = e5 * m3;
  855. float const d6 = e6 * m4;
  856. float const d7 = e7;
  857. float const d8 = e8 * m5;
  858. float const c0 = d0 + d1;
  859. float const c1 = d0 - d1;
  860. float const c2 = d2 - d3;
  861. float const c3 = d3;
  862. float const c4 = d4 + d8;
  863. float const c5 = d5 + d7;
  864. float const c6 = d6 - d8;
  865. float const c7 = d7;
  866. float const c8 = c5 - c6;
  867. float const b0 = c0 + c3;
  868. float const b1 = c1 + c2;
  869. float const b2 = c1 - c2;
  870. float const b3 = c0 - c3;
  871. float const b4 = c4 - c8;
  872. float const b5 = c8;
  873. float const b6 = c6 - c7;
  874. float const b7 = c7;
  875. block_component[0 * 8 + k] = b0 + b7;
  876. block_component[1 * 8 + k] = b1 + b6;
  877. block_component[2 * 8 + k] = b2 + b5;
  878. block_component[3 * 8 + k] = b3 + b4;
  879. block_component[4 * 8 + k] = b3 - b4;
  880. block_component[5 * 8 + k] = b2 - b5;
  881. block_component[6 * 8 + k] = b1 - b6;
  882. block_component[7 * 8 + k] = b0 - b7;
  883. }
  884. for (u32 l = 0; l < 8; ++l) {
  885. float const g0 = block_component[l * 8 + 0] * s0;
  886. float const g1 = block_component[l * 8 + 4] * s4;
  887. float const g2 = block_component[l * 8 + 2] * s2;
  888. float const g3 = block_component[l * 8 + 6] * s6;
  889. float const g4 = block_component[l * 8 + 5] * s5;
  890. float const g5 = block_component[l * 8 + 1] * s1;
  891. float const g6 = block_component[l * 8 + 7] * s7;
  892. float const g7 = block_component[l * 8 + 3] * s3;
  893. float const f0 = g0;
  894. float const f1 = g1;
  895. float const f2 = g2;
  896. float const f3 = g3;
  897. float const f4 = g4 - g7;
  898. float const f5 = g5 + g6;
  899. float const f6 = g5 - g6;
  900. float const f7 = g4 + g7;
  901. float const e0 = f0;
  902. float const e1 = f1;
  903. float const e2 = f2 - f3;
  904. float const e3 = f2 + f3;
  905. float const e4 = f4;
  906. float const e5 = f5 - f7;
  907. float const e6 = f6;
  908. float const e7 = f5 + f7;
  909. float const e8 = f4 + f6;
  910. float const d0 = e0;
  911. float const d1 = e1;
  912. float const d2 = e2 * m1;
  913. float const d3 = e3;
  914. float const d4 = e4 * m2;
  915. float const d5 = e5 * m3;
  916. float const d6 = e6 * m4;
  917. float const d7 = e7;
  918. float const d8 = e8 * m5;
  919. float const c0 = d0 + d1;
  920. float const c1 = d0 - d1;
  921. float const c2 = d2 - d3;
  922. float const c3 = d3;
  923. float const c4 = d4 + d8;
  924. float const c5 = d5 + d7;
  925. float const c6 = d6 - d8;
  926. float const c7 = d7;
  927. float const c8 = c5 - c6;
  928. float const b0 = c0 + c3;
  929. float const b1 = c1 + c2;
  930. float const b2 = c1 - c2;
  931. float const b3 = c0 - c3;
  932. float const b4 = c4 - c8;
  933. float const b5 = c8;
  934. float const b6 = c6 - c7;
  935. float const b7 = c7;
  936. block_component[l * 8 + 0] = b0 + b7;
  937. block_component[l * 8 + 1] = b1 + b6;
  938. block_component[l * 8 + 2] = b2 + b5;
  939. block_component[l * 8 + 3] = b3 + b4;
  940. block_component[l * 8 + 4] = b3 - b4;
  941. block_component[l * 8 + 5] = b2 - b5;
  942. block_component[l * 8 + 6] = b1 - b6;
  943. block_component[l * 8 + 7] = b0 - b7;
  944. }
  945. }
  946. }
  947. }
  948. }
  949. }
  950. }
  951. static void ycbcr_to_rgb(JPEGLoadingContext const& context, Vector<Macroblock>& macroblocks)
  952. {
  953. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  954. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  955. const u32 chroma_block_index = vcursor * context.mblock_meta.hpadded_count + hcursor;
  956. Macroblock const& chroma = macroblocks[chroma_block_index];
  957. // Overflows are intentional.
  958. for (u8 vfactor_i = context.vsample_factor - 1; vfactor_i < context.vsample_factor; --vfactor_i) {
  959. for (u8 hfactor_i = context.hsample_factor - 1; hfactor_i < context.hsample_factor; --hfactor_i) {
  960. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  961. i32* y = macroblocks[mb_index].y;
  962. i32* cb = macroblocks[mb_index].cb;
  963. i32* cr = macroblocks[mb_index].cr;
  964. for (u8 i = 7; i < 8; --i) {
  965. for (u8 j = 7; j < 8; --j) {
  966. const u8 pixel = i * 8 + j;
  967. const u32 chroma_pxrow = (i / context.vsample_factor) + 4 * vfactor_i;
  968. const u32 chroma_pxcol = (j / context.hsample_factor) + 4 * hfactor_i;
  969. const u32 chroma_pixel = chroma_pxrow * 8 + chroma_pxcol;
  970. int r = y[pixel] + 1.402f * chroma.cr[chroma_pixel] + 128;
  971. int g = y[pixel] - 0.344f * chroma.cb[chroma_pixel] - 0.714f * chroma.cr[chroma_pixel] + 128;
  972. int b = y[pixel] + 1.772f * chroma.cb[chroma_pixel] + 128;
  973. y[pixel] = r < 0 ? 0 : (r > 255 ? 255 : r);
  974. cb[pixel] = g < 0 ? 0 : (g > 255 ? 255 : g);
  975. cr[pixel] = b < 0 ? 0 : (b > 255 ? 255 : b);
  976. }
  977. }
  978. }
  979. }
  980. }
  981. }
  982. }
  983. static ErrorOr<void> compose_bitmap(JPEGLoadingContext& context, Vector<Macroblock> const& macroblocks)
  984. {
  985. context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height }));
  986. for (u32 y = context.frame.height - 1; y < context.frame.height; y--) {
  987. const u32 block_row = y / 8;
  988. const u32 pixel_row = y % 8;
  989. for (u32 x = 0; x < context.frame.width; x++) {
  990. const u32 block_column = x / 8;
  991. auto& block = macroblocks[block_row * context.mblock_meta.hpadded_count + block_column];
  992. const u32 pixel_column = x % 8;
  993. const u32 pixel_index = pixel_row * 8 + pixel_column;
  994. const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] };
  995. context.bitmap->set_pixel(x, y, color);
  996. }
  997. }
  998. return {};
  999. }
  1000. static bool is_app_marker(Marker const marker)
  1001. {
  1002. return marker >= JPEG_APPN0 && marker <= JPEG_APPNF;
  1003. }
  1004. static bool is_miscellaneous_or_table_marker(Marker const marker)
  1005. {
  1006. // B.6 - Summary
  1007. // See: Figure B.17 – Flow of marker segment
  1008. bool const is_misc = marker == JPEG_COM || marker == JPEG_DRI || is_app_marker(marker);
  1009. bool const is_table = marker == JPEG_DQT || marker == JPEG_DAC || marker == JPEG_DHT;
  1010. return is_misc || is_table;
  1011. }
  1012. static ErrorOr<void> handle_miscellaneous_or_table(AK::SeekableStream& stream, JPEGLoadingContext& context, Marker const marker)
  1013. {
  1014. if (is_app_marker(marker)) {
  1015. TRY(read_app_marker(stream, context, marker - JPEG_APPN0));
  1016. return {};
  1017. }
  1018. switch (marker) {
  1019. case JPEG_COM:
  1020. case JPEG_DAC:
  1021. dbgln_if(JPEG_DEBUG, "TODO: implement marker \"{:x}\"", marker);
  1022. if (auto result = skip_segment(stream); result.is_error()) {
  1023. dbgln_if(JPEG_DEBUG, "{}: Error skipping marker: {:x}!", TRY(stream.tell()), marker);
  1024. return result.release_error();
  1025. }
  1026. break;
  1027. case JPEG_DHT:
  1028. TRY(read_huffman_table(stream, context));
  1029. break;
  1030. case JPEG_DQT:
  1031. TRY(read_quantization_table(stream, context));
  1032. break;
  1033. case JPEG_DRI:
  1034. TRY(read_restart_interval(stream, context));
  1035. break;
  1036. default:
  1037. dbgln("Unexpected marker: {:x}", marker);
  1038. VERIFY_NOT_REACHED();
  1039. }
  1040. return {};
  1041. }
  1042. static ErrorOr<void> parse_header(AK::SeekableStream& stream, JPEGLoadingContext& context)
  1043. {
  1044. auto marker = TRY(read_marker_at_cursor(stream));
  1045. if (marker != JPEG_SOI) {
  1046. dbgln_if(JPEG_DEBUG, "{}: SOI not found: {:x}!", TRY(stream.tell()), marker);
  1047. return Error::from_string_literal("SOI not found");
  1048. }
  1049. for (;;) {
  1050. marker = TRY(read_marker_at_cursor(stream));
  1051. if (is_miscellaneous_or_table_marker(marker)) {
  1052. TRY(handle_miscellaneous_or_table(stream, context, marker));
  1053. continue;
  1054. }
  1055. // Set frame type if the marker marks a new frame.
  1056. if (is_frame_marker(marker))
  1057. context.frame.type = static_cast<StartOfFrame::FrameType>(marker & 0xF);
  1058. switch (marker) {
  1059. case JPEG_INVALID:
  1060. case JPEG_RST0:
  1061. case JPEG_RST1:
  1062. case JPEG_RST2:
  1063. case JPEG_RST3:
  1064. case JPEG_RST4:
  1065. case JPEG_RST5:
  1066. case JPEG_RST6:
  1067. case JPEG_RST7:
  1068. case JPEG_SOI:
  1069. case JPEG_EOI:
  1070. dbgln_if(JPEG_DEBUG, "{}: Unexpected marker {:x}!", TRY(stream.tell()), marker);
  1071. return Error::from_string_literal("Unexpected marker");
  1072. case JPEG_SOF0:
  1073. TRY(read_start_of_frame(stream, context));
  1074. context.state = JPEGLoadingContext::FrameDecoded;
  1075. return {};
  1076. default:
  1077. if (auto result = skip_segment(stream); result.is_error()) {
  1078. dbgln_if(JPEG_DEBUG, "{}: Error skipping marker: {:x}!", TRY(stream.tell()), marker);
  1079. return result.release_error();
  1080. }
  1081. break;
  1082. }
  1083. }
  1084. VERIFY_NOT_REACHED();
  1085. }
  1086. static ErrorOr<void> scan_huffman_stream(AK::SeekableStream& stream, HuffmanStreamState& huffman_stream)
  1087. {
  1088. u8 last_byte;
  1089. u8 current_byte = TRY(stream.read_value<u8>());
  1090. for (;;) {
  1091. last_byte = current_byte;
  1092. current_byte = TRY(stream.read_value<u8>());
  1093. if (last_byte == 0xFF) {
  1094. if (current_byte == 0xFF)
  1095. continue;
  1096. if (current_byte == 0x00) {
  1097. current_byte = TRY(stream.read_value<u8>());
  1098. huffman_stream.stream.append(last_byte);
  1099. continue;
  1100. }
  1101. Marker marker = 0xFF00 | current_byte;
  1102. if (marker >= JPEG_RST0 && marker <= JPEG_RST7) {
  1103. huffman_stream.stream.append(marker);
  1104. current_byte = TRY(stream.read_value<u8>());
  1105. continue;
  1106. }
  1107. // Rollback the marker we just read
  1108. TRY(stream.seek(-2, AK::SeekMode::FromCurrentPosition));
  1109. return {};
  1110. } else {
  1111. huffman_stream.stream.append(last_byte);
  1112. }
  1113. }
  1114. VERIFY_NOT_REACHED();
  1115. }
  1116. static ErrorOr<void> decode_header(JPEGLoadingContext& context)
  1117. {
  1118. if (context.state < JPEGLoadingContext::State::HeaderDecoded) {
  1119. context.stream = TRY(try_make<FixedMemoryStream>(ReadonlyBytes { context.data, context.data_size }));
  1120. if (auto result = parse_header(*context.stream, context); result.is_error()) {
  1121. context.state = JPEGLoadingContext::State::Error;
  1122. return result.release_error();
  1123. }
  1124. if constexpr (JPEG_DEBUG) {
  1125. dbgln("Image width: {}", context.frame.width);
  1126. dbgln("Image height: {}", context.frame.height);
  1127. dbgln("Macroblocks in a row: {}", context.mblock_meta.hpadded_count);
  1128. dbgln("Macroblocks in a column: {}", context.mblock_meta.vpadded_count);
  1129. dbgln("Macroblock meta padded total: {}", context.mblock_meta.padded_total);
  1130. }
  1131. context.state = JPEGLoadingContext::State::HeaderDecoded;
  1132. }
  1133. return {};
  1134. }
  1135. static ErrorOr<Vector<Macroblock>> construct_macroblocks(JPEGLoadingContext& context)
  1136. {
  1137. // B.6 - Summary
  1138. // See: Figure B.16 – Flow of compressed data syntax
  1139. // This function handles the "Multi-scan" loop.
  1140. Vector<Macroblock> macroblocks;
  1141. TRY(macroblocks.try_resize(context.mblock_meta.padded_total));
  1142. Marker marker = TRY(read_marker_at_cursor(*context.stream));
  1143. while (true) {
  1144. if (is_miscellaneous_or_table_marker(marker)) {
  1145. TRY(handle_miscellaneous_or_table(*context.stream, context, marker));
  1146. } else if (marker == JPEG_SOS) {
  1147. TRY(read_start_of_scan(*context.stream, context));
  1148. TRY(scan_huffman_stream(*context.stream, context.current_scan.huffman_stream));
  1149. TRY(decode_huffman_stream(context, macroblocks));
  1150. } else if (marker == JPEG_EOI) {
  1151. return macroblocks;
  1152. } else {
  1153. dbgln_if(JPEG_DEBUG, "{}: Unexpected marker {:x}!", TRY(context.stream->tell()), marker);
  1154. return Error::from_string_literal("Unexpected marker");
  1155. }
  1156. marker = TRY(read_marker_at_cursor(*context.stream));
  1157. }
  1158. }
  1159. static ErrorOr<void> decode_jpeg(JPEGLoadingContext& context)
  1160. {
  1161. TRY(decode_header(context));
  1162. auto macroblocks = TRY(construct_macroblocks(context));
  1163. dequantize(context, macroblocks);
  1164. inverse_dct(context, macroblocks);
  1165. ycbcr_to_rgb(context, macroblocks);
  1166. TRY(compose_bitmap(context, macroblocks));
  1167. context.stream.clear();
  1168. return {};
  1169. }
  1170. JPEGImageDecoderPlugin::JPEGImageDecoderPlugin(u8 const* data, size_t size)
  1171. {
  1172. m_context = make<JPEGLoadingContext>();
  1173. m_context->data = data;
  1174. m_context->data_size = size;
  1175. }
  1176. JPEGImageDecoderPlugin::~JPEGImageDecoderPlugin() = default;
  1177. IntSize JPEGImageDecoderPlugin::size()
  1178. {
  1179. if (m_context->state == JPEGLoadingContext::State::Error)
  1180. return {};
  1181. if (m_context->state >= JPEGLoadingContext::State::FrameDecoded)
  1182. return { m_context->frame.width, m_context->frame.height };
  1183. return {};
  1184. }
  1185. void JPEGImageDecoderPlugin::set_volatile()
  1186. {
  1187. if (m_context->bitmap)
  1188. m_context->bitmap->set_volatile();
  1189. }
  1190. bool JPEGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1191. {
  1192. if (!m_context->bitmap)
  1193. return false;
  1194. return m_context->bitmap->set_nonvolatile(was_purged);
  1195. }
  1196. bool JPEGImageDecoderPlugin::initialize()
  1197. {
  1198. return true;
  1199. }
  1200. bool JPEGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1201. {
  1202. return data.size() > 3
  1203. && data.data()[0] == 0xFF
  1204. && data.data()[1] == 0xD8
  1205. && data.data()[2] == 0xFF;
  1206. }
  1207. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> JPEGImageDecoderPlugin::create(ReadonlyBytes data)
  1208. {
  1209. return adopt_nonnull_own_or_enomem(new (nothrow) JPEGImageDecoderPlugin(data.data(), data.size()));
  1210. }
  1211. bool JPEGImageDecoderPlugin::is_animated()
  1212. {
  1213. return false;
  1214. }
  1215. size_t JPEGImageDecoderPlugin::loop_count()
  1216. {
  1217. return 0;
  1218. }
  1219. size_t JPEGImageDecoderPlugin::frame_count()
  1220. {
  1221. return 1;
  1222. }
  1223. ErrorOr<ImageFrameDescriptor> JPEGImageDecoderPlugin::frame(size_t index)
  1224. {
  1225. if (index > 0)
  1226. return Error::from_string_literal("JPEGImageDecoderPlugin: Invalid frame index");
  1227. if (m_context->state == JPEGLoadingContext::State::Error)
  1228. return Error::from_string_literal("JPEGImageDecoderPlugin: Decoding failed");
  1229. if (m_context->state < JPEGLoadingContext::State::BitmapDecoded) {
  1230. if (auto result = decode_jpeg(*m_context); result.is_error()) {
  1231. m_context->state = JPEGLoadingContext::State::Error;
  1232. return result.release_error();
  1233. }
  1234. m_context->state = JPEGLoadingContext::State::BitmapDecoded;
  1235. }
  1236. return ImageFrameDescriptor { m_context->bitmap, 0 };
  1237. }
  1238. ErrorOr<Optional<ReadonlyBytes>> JPEGImageDecoderPlugin::icc_data()
  1239. {
  1240. TRY(decode_header(*m_context));
  1241. if (m_context->icc_data.has_value())
  1242. return *m_context->icc_data;
  1243. return OptionalNone {};
  1244. }
  1245. }