JPGLoader.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Bitmap.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/LexicalPath.h>
  30. #include <AK/MappedFile.h>
  31. #include <AK/MemoryStream.h>
  32. #include <AK/String.h>
  33. #include <AK/Vector.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <LibGfx/JPGLoader.h>
  36. #include <math.h>
  37. //#define JPG_DEBUG
  38. #define JPG_INVALID 0X0000
  39. #define JPG_APPN0 0XFFE0
  40. #define JPG_APPN1 0XFFE1
  41. #define JPG_APPN2 0XFFE2
  42. #define JPG_APPN3 0XFFE3
  43. #define JPG_APPN4 0XFFE4
  44. #define JPG_APPN5 0XFFE5
  45. #define JPG_APPN6 0XFFE6
  46. #define JPG_APPN7 0XFFE7
  47. #define JPG_APPN8 0XFFE8
  48. #define JPG_APPN9 0XFFE9
  49. #define JPG_APPNA 0XFFEA
  50. #define JPG_APPNB 0XFFEB
  51. #define JPG_APPNC 0XFFEC
  52. #define JPG_APPND 0XFFED
  53. #define JPG_APPNE 0xFFEE
  54. #define JPG_APPNF 0xFFEF
  55. #define JPG_RESERVED1 0xFFF1
  56. #define JPG_RESERVED2 0xFFF2
  57. #define JPG_RESERVED3 0xFFF3
  58. #define JPG_RESERVED4 0xFFF4
  59. #define JPG_RESERVED5 0xFFF5
  60. #define JPG_RESERVED6 0xFFF6
  61. #define JPG_RESERVED7 0xFFF7
  62. #define JPG_RESERVED8 0xFFF8
  63. #define JPG_RESERVED9 0xFFF9
  64. #define JPG_RESERVEDA 0xFFFA
  65. #define JPG_RESERVEDB 0xFFFB
  66. #define JPG_RESERVEDC 0xFFFC
  67. #define JPG_RESERVEDD 0xFFFD
  68. #define JPG_RST0 0xFFD0
  69. #define JPG_RST1 0xFFD1
  70. #define JPG_RST2 0xFFD2
  71. #define JPG_RST3 0xFFD3
  72. #define JPG_RST4 0xFFD4
  73. #define JPG_RST5 0xFFD5
  74. #define JPG_RST6 0xFFD6
  75. #define JPG_RST7 0xFFD7
  76. #define JPG_DHP 0xFFDE
  77. #define JPG_EXP 0xFFDF
  78. #define JPG_DHT 0XFFC4
  79. #define JPG_DQT 0XFFDB
  80. #define JPG_EOI 0xFFD9
  81. #define JPG_RST 0XFFDD
  82. #define JPG_SOF0 0XFFC0
  83. #define JPG_SOF2 0xFFC2
  84. #define JPG_SOI 0XFFD8
  85. #define JPG_SOS 0XFFDA
  86. #define JPG_COM 0xFFFE
  87. namespace Gfx {
  88. constexpr static u8 zigzag_map[64] {
  89. 0, 1, 8, 16, 9, 2, 3, 10,
  90. 17, 24, 32, 25, 18, 11, 4, 5,
  91. 12, 19, 26, 33, 40, 48, 41, 34,
  92. 27, 20, 13, 6, 7, 14, 21, 28,
  93. 35, 42, 49, 56, 57, 50, 43, 36,
  94. 29, 22, 15, 23, 30, 37, 44, 51,
  95. 58, 59, 52, 45, 38, 31, 39, 46,
  96. 53, 60, 61, 54, 47, 55, 62, 63
  97. };
  98. using Marker = u16;
  99. /**
  100. * MCU means group of data units that are coded together. A data unit is an 8x8
  101. * block of component data. In interleaved scans, number of non-interleaved data
  102. * units of a component C is Ch * Cv, where Ch and Cv represent the horizontal &
  103. * vertical subsampling factors of the component, respectively. A MacroBlock is
  104. * an 8x8 block of RGB values before encoding, and 8x8 block of YCbCr values when
  105. * we're done decoding the huffman stream.
  106. */
  107. struct Macroblock {
  108. union {
  109. i32 y[64] = { 0 };
  110. i32 r[64];
  111. };
  112. union {
  113. i32 cb[64] = { 0 };
  114. i32 g[64];
  115. };
  116. union {
  117. i32 cr[64] = { 0 };
  118. i32 b[64];
  119. };
  120. };
  121. struct MacroblockMeta {
  122. u32 total { 0 };
  123. u32 padded_total { 0 };
  124. u32 hcount { 0 };
  125. u32 vcount { 0 };
  126. u32 hpadded_count { 0 };
  127. u32 vpadded_count { 0 };
  128. };
  129. struct ComponentSpec {
  130. u8 serial_id { 255 }; // In the interval [0, 3).
  131. u8 id { 0 };
  132. u8 hsample_factor { 1 }; // Horizontal sampling factor.
  133. u8 vsample_factor { 1 }; // Vertical sampling factor.
  134. u8 ac_destination_id { 0 };
  135. u8 dc_destination_id { 0 };
  136. u8 qtable_id { 0 }; // Quantization table id.
  137. };
  138. struct StartOfFrame {
  139. // Of these, only the first 3 are in mainstream use, and refers to SOF0-2.
  140. enum class FrameType {
  141. Baseline_DCT = 0,
  142. Extended_Sequential_DCT = 1,
  143. Progressive_DCT = 2,
  144. Sequential_Lossless = 3,
  145. Differential_Sequential_DCT = 5,
  146. Differential_Progressive_DCT = 6,
  147. Differential_Sequential_Lossless = 7,
  148. Extended_Sequential_DCT_Arithmetic = 9,
  149. Progressive_DCT_Arithmetic = 10,
  150. Sequential_Lossless_Arithmetic = 11,
  151. Differential_Sequential_DCT_Arithmetic = 13,
  152. Differential_Progressive_DCT_Arithmetic = 14,
  153. Differential_Sequential_Lossless_Arithmetic = 15,
  154. };
  155. FrameType type { FrameType::Baseline_DCT };
  156. u8 precision { 0 };
  157. u16 height { 0 };
  158. u16 width { 0 };
  159. };
  160. struct HuffmanTableSpec {
  161. u8 type { 0 };
  162. u8 destination_id { 0 };
  163. u8 code_counts[16] = { 0 };
  164. Vector<u8> symbols;
  165. Vector<u16> codes;
  166. };
  167. struct HuffmanStreamState {
  168. Vector<u8> stream;
  169. u8 bit_offset { 0 };
  170. size_t byte_offset { 0 };
  171. };
  172. struct JPGLoadingContext {
  173. enum State {
  174. NotDecoded = 0,
  175. Error,
  176. FrameDecoded,
  177. BitmapDecoded
  178. };
  179. State state { State::NotDecoded };
  180. const u8* data { nullptr };
  181. size_t data_size { 0 };
  182. u32 luma_table[64] = { 0 };
  183. u32 chroma_table[64] = { 0 };
  184. StartOfFrame frame;
  185. u8 hsample_factor { 0 };
  186. u8 vsample_factor { 0 };
  187. u8 component_count { 0 };
  188. HashMap<u8, ComponentSpec> components;
  189. RefPtr<Gfx::Bitmap> bitmap;
  190. u16 dc_reset_interval { 0 };
  191. HashMap<u8, HuffmanTableSpec> dc_tables;
  192. HashMap<u8, HuffmanTableSpec> ac_tables;
  193. HuffmanStreamState huffman_stream;
  194. i32 previous_dc_values[3] = { 0 };
  195. MacroblockMeta mblock_meta;
  196. };
  197. static void generate_huffman_codes(HuffmanTableSpec& table)
  198. {
  199. unsigned code = 0;
  200. for (auto number_of_codes : table.code_counts) {
  201. for (int i = 0; i < number_of_codes; i++)
  202. table.codes.append(code++);
  203. code <<= 1;
  204. }
  205. }
  206. static Optional<size_t> read_huffman_bits(HuffmanStreamState& hstream, size_t count = 1)
  207. {
  208. if (count > (8 * sizeof(size_t))) {
  209. #ifdef JPG_DEBUG
  210. dbg() << String::format("Can't read %i bits at once!", count);
  211. #endif
  212. return {};
  213. }
  214. size_t value = 0;
  215. while (count--) {
  216. if (hstream.byte_offset >= hstream.stream.size()) {
  217. #ifdef JPG_DEBUG
  218. dbg() << String::format("Huffman stream exhausted. This could be an error!");
  219. #endif
  220. return {};
  221. }
  222. u8 current_byte = hstream.stream[hstream.byte_offset];
  223. u8 current_bit = 1u & (u32)(current_byte >> (7 - hstream.bit_offset)); // MSB first.
  224. hstream.bit_offset++;
  225. value = (value << 1) | (size_t)current_bit;
  226. if (hstream.bit_offset == 8) {
  227. hstream.byte_offset++;
  228. hstream.bit_offset = 0;
  229. }
  230. }
  231. return value;
  232. }
  233. static Optional<u8> get_next_symbol(HuffmanStreamState& hstream, const HuffmanTableSpec& table)
  234. {
  235. unsigned code = 0;
  236. size_t code_cursor = 0;
  237. for (int i = 0; i < 16; i++) { // Codes can't be longer than 16 bits.
  238. auto result = read_huffman_bits(hstream);
  239. if (!result.has_value())
  240. return {};
  241. code = (code << 1) | (i32)result.release_value();
  242. for (int j = 0; j < table.code_counts[i]; j++) {
  243. if (code == table.codes[code_cursor])
  244. return table.symbols[code_cursor];
  245. code_cursor++;
  246. }
  247. }
  248. #ifdef JPG_DEBUG
  249. dbg() << "If you're seeing this...the jpeg decoder needs to support more kinds of JPEGs!";
  250. #endif
  251. return {};
  252. }
  253. /**
  254. * Build the macroblocks possible by reading single (MCU) subsampled pair of CbCr.
  255. * Depending on the sampling factors, we may not see triples of y, cb, cr in that
  256. * order. If sample factors differ from one, we'll read more than one block of y-
  257. * coefficients before we get to read a cb-cr block.
  258. * In the function below, `hcursor` and `vcursor` denote the location of the block
  259. * we're building in the macroblock matrix. `vfactor_i` and `hfactor_i` are cursors
  260. * that iterate over the vertical and horizontal subsampling factors, respectively.
  261. * When we finish one iteration of the innermost loop, we'll have the coefficients
  262. * of one of the components of block at position `mb_index`. When the outermost loop
  263. * finishes first iteration, we'll have all the luminance coefficients for all the
  264. * macroblocks that share the chrominance data. Next two iterations (assuming that
  265. * we are dealing with three components) will fill up the blocks with chroma data.
  266. */
  267. static bool build_macroblocks(JPGLoadingContext& context, Vector<Macroblock>& macroblocks, u8 hcursor, u8 vcursor)
  268. {
  269. for (auto it = context.components.begin(); it != context.components.end(); ++it) {
  270. ComponentSpec& component = it->value;
  271. if (component.dc_destination_id >= context.dc_tables.size())
  272. return false;
  273. if (component.ac_destination_id >= context.ac_tables.size())
  274. return false;
  275. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  276. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  277. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  278. Macroblock& block = macroblocks[mb_index];
  279. auto& dc_table = context.dc_tables.find(component.dc_destination_id)->value;
  280. auto& ac_table = context.ac_tables.find(component.ac_destination_id)->value;
  281. auto symbol_or_error = get_next_symbol(context.huffman_stream, dc_table);
  282. if (!symbol_or_error.has_value())
  283. return false;
  284. // For DC coefficients, symbol encodes the length of the coefficient.
  285. auto dc_length = symbol_or_error.release_value();
  286. if (dc_length > 11) {
  287. #ifdef JPG_DEBUG
  288. dbg() << String::format("DC coefficient too long: %i!", dc_length);
  289. #endif
  290. return false;
  291. }
  292. auto coeff_or_error = read_huffman_bits(context.huffman_stream, dc_length);
  293. if (!coeff_or_error.has_value())
  294. return false;
  295. // DC coefficients are encoded as the difference between previous and current DC values.
  296. i32 dc_diff = coeff_or_error.release_value();
  297. // If MSB in diff is 0, the difference is -ve. Otherwise +ve.
  298. if (dc_length != 0 && dc_diff < (1 << (dc_length - 1)))
  299. dc_diff -= (1 << dc_length) - 1;
  300. i32* select_component = component.serial_id == 0 ? block.y : (component.serial_id == 1 ? block.cb : block.cr);
  301. auto& previous_dc = context.previous_dc_values[component.serial_id];
  302. select_component[0] = previous_dc += dc_diff;
  303. // Compute the AC coefficients.
  304. for (int j = 1; j < 64;) {
  305. symbol_or_error = get_next_symbol(context.huffman_stream, ac_table);
  306. if (!symbol_or_error.has_value())
  307. return false;
  308. // AC symbols encode 2 pieces of information, the high 4 bits represent
  309. // number of zeroes to be stuffed before reading the coefficient. Low 4
  310. // bits represent the magnitude of the coefficient.
  311. auto ac_symbol = symbol_or_error.release_value();
  312. if (ac_symbol == 0)
  313. break;
  314. // ac_symbol = 0xF0 means we need to skip 16 zeroes.
  315. u8 run_length = ac_symbol == 0xF0 ? 16 : ac_symbol >> 4;
  316. j += run_length;
  317. if (j >= 64) {
  318. #ifdef JPG_DEBUG
  319. dbg() << String::format("Run-length exceeded boundaries. Cursor: %i, Skipping: %i!", j, run_length);
  320. #endif
  321. return false;
  322. }
  323. u8 coeff_length = ac_symbol & 0x0F;
  324. if (coeff_length > 10) {
  325. #ifdef JPG_DEBUG
  326. dbg() << String::format("AC coefficient too long: %i!", coeff_length);
  327. #endif
  328. return false;
  329. }
  330. if (coeff_length != 0) {
  331. coeff_or_error = read_huffman_bits(context.huffman_stream, coeff_length);
  332. if (!coeff_or_error.has_value())
  333. return false;
  334. i32 ac_coefficient = coeff_or_error.release_value();
  335. if (ac_coefficient < (1 << (coeff_length - 1)))
  336. ac_coefficient -= (1 << coeff_length) - 1;
  337. select_component[zigzag_map[j++]] = ac_coefficient;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. return true;
  344. }
  345. static Optional<Vector<Macroblock>> decode_huffman_stream(JPGLoadingContext& context)
  346. {
  347. Vector<Macroblock> macroblocks;
  348. macroblocks.resize(context.mblock_meta.padded_total);
  349. #ifdef JPG_DEBUG
  350. dbg() << "Image width: " << context.frame.width;
  351. dbg() << "Image height: " << context.frame.height;
  352. dbg() << "Macroblocks in a row: " << context.mblock_meta.hpadded_count;
  353. dbg() << "Macroblocks in a column: " << context.mblock_meta.vpadded_count;
  354. dbg() << "Macroblock meta padded total: " << context.mblock_meta.padded_total;
  355. #endif
  356. // Compute huffman codes for DC and AC tables.
  357. for (auto it = context.dc_tables.begin(); it != context.dc_tables.end(); ++it)
  358. generate_huffman_codes(it->value);
  359. for (auto it = context.ac_tables.begin(); it != context.ac_tables.end(); ++it)
  360. generate_huffman_codes(it->value);
  361. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  362. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  363. u32 i = vcursor * context.mblock_meta.hpadded_count + hcursor;
  364. if (context.dc_reset_interval > 0) {
  365. if (i % context.dc_reset_interval == 0) {
  366. context.previous_dc_values[0] = 0;
  367. context.previous_dc_values[1] = 0;
  368. context.previous_dc_values[2] = 0;
  369. // Restart markers are stored in byte boundaries. Advance the huffman stream cursor to
  370. // the 0th bit of the next byte.
  371. if (context.huffman_stream.byte_offset < context.huffman_stream.stream.size()) {
  372. if (context.huffman_stream.bit_offset > 0) {
  373. context.huffman_stream.bit_offset = 0;
  374. context.huffman_stream.byte_offset++;
  375. }
  376. // Skip the restart marker (RSTn).
  377. context.huffman_stream.byte_offset++;
  378. }
  379. }
  380. }
  381. if (!build_macroblocks(context, macroblocks, hcursor, vcursor)) {
  382. #ifdef JPG_DEBUG
  383. dbg() << "Failed to build Macroblock " << i;
  384. dbg() << "Huffman stream byte offset " << context.huffman_stream.byte_offset;
  385. dbg() << "Huffman stream bit offset " << context.huffman_stream.bit_offset;
  386. #endif
  387. return {};
  388. }
  389. }
  390. }
  391. return macroblocks;
  392. }
  393. static inline bool bounds_okay(const size_t cursor, const size_t delta, const size_t bound)
  394. {
  395. return (delta + cursor) < bound;
  396. }
  397. static inline bool is_valid_marker(const Marker marker)
  398. {
  399. if (marker >= JPG_APPN0 && marker <= JPG_APPNF) {
  400. #ifdef JPG_DEBUG
  401. if (marker != JPG_APPN0)
  402. dbg() << String::format("%04x not supported yet. The decoder may fail!", marker);
  403. #endif
  404. return true;
  405. }
  406. if (marker >= JPG_RESERVED1 && marker <= JPG_RESERVEDD)
  407. return true;
  408. if (marker >= JPG_RST0 && marker <= JPG_RST7)
  409. return true;
  410. switch (marker) {
  411. case JPG_COM:
  412. case JPG_DHP:
  413. case JPG_EXP:
  414. case JPG_DHT:
  415. case JPG_DQT:
  416. case JPG_RST:
  417. case JPG_SOF0:
  418. case JPG_SOI:
  419. case JPG_SOS:
  420. return true;
  421. }
  422. if (marker >= 0xFFC0 && marker <= 0xFFCF) {
  423. if (marker != 0xFFC4 && marker != 0xFFC8 && marker != 0xFFCC) {
  424. #ifdef JPG_DEBUG
  425. dbg() << "Decoding this frame-type (SOF" << (marker & 0xf) << ") is not currently supported. Decoder will fail!";
  426. #endif
  427. return false;
  428. }
  429. }
  430. return false;
  431. }
  432. static inline u16 read_be_word(InputMemoryStream& stream)
  433. {
  434. BigEndian<u16> tmp;
  435. stream >> tmp;
  436. return tmp;
  437. }
  438. static inline Marker read_marker_at_cursor(InputMemoryStream& stream)
  439. {
  440. u16 marker = read_be_word(stream);
  441. if (stream.handle_any_error())
  442. return JPG_INVALID;
  443. if (is_valid_marker(marker))
  444. return marker;
  445. if (marker != 0xFFFF)
  446. return JPG_INVALID;
  447. u8 next;
  448. do {
  449. stream >> next;
  450. if (stream.handle_any_error() || next == 0x00)
  451. return JPG_INVALID;
  452. } while (next == 0xFF);
  453. marker = 0xFF00 | (u16)next;
  454. return is_valid_marker(marker) ? marker : JPG_INVALID;
  455. }
  456. static bool read_start_of_scan(InputMemoryStream& stream, JPGLoadingContext& context)
  457. {
  458. if (context.state < JPGLoadingContext::State::FrameDecoded) {
  459. #ifdef JPG_DEBUG
  460. dbg() << stream.offset() << ": SOS found before reading a SOF!";
  461. #endif
  462. return false;
  463. }
  464. u16 bytes_to_read = read_be_word(stream);
  465. if (stream.handle_any_error())
  466. return false;
  467. bytes_to_read -= 2;
  468. if (!bounds_okay(stream.offset(), bytes_to_read, context.data_size))
  469. return false;
  470. u8 component_count = 0;
  471. stream >> component_count;
  472. if (stream.handle_any_error())
  473. return false;
  474. if (component_count != context.component_count) {
  475. #ifdef JPG_DEBUG
  476. dbg() << stream.offset()
  477. << String::format(": Unsupported number of components: %i!", component_count);
  478. #endif
  479. return false;
  480. }
  481. for (int i = 0; i < component_count; i++) {
  482. ComponentSpec* component = nullptr;
  483. u8 component_id = 0;
  484. stream >> component_id;
  485. if (stream.handle_any_error())
  486. return false;
  487. auto it = context.components.find(component_id);
  488. if (it != context.components.end()) {
  489. component = &it->value;
  490. ASSERT(i == component->serial_id);
  491. } else {
  492. #ifdef JPG_DEBUG
  493. dbg() << stream.offset() << String::format(": Unsupported component id: %i!", component_id);
  494. #endif
  495. return false;
  496. }
  497. u8 table_ids = 0;
  498. stream >> table_ids;
  499. if (stream.handle_any_error())
  500. return false;
  501. component->dc_destination_id = table_ids >> 4;
  502. component->ac_destination_id = table_ids & 0x0F;
  503. if (context.dc_tables.size() != context.ac_tables.size()) {
  504. #ifdef JPG_DEBUG
  505. dbg() << stream.offset() << ": DC & AC table count mismatch!";
  506. #endif
  507. return false;
  508. }
  509. if (!context.dc_tables.contains(component->dc_destination_id)) {
  510. #ifdef JPG_DEBUG
  511. dbgln("DC table (id: {}) does not exist!", component->dc_destination_id);
  512. #endif
  513. return false;
  514. }
  515. if (!context.ac_tables.contains(component->ac_destination_id)) {
  516. #ifdef JPG_DEBUG
  517. dbgln("AC table (id: {}) does not exist!", component->ac_destination_id);
  518. #endif
  519. return false;
  520. }
  521. }
  522. u8 spectral_selection_start = 0;
  523. stream >> spectral_selection_start;
  524. if (stream.handle_any_error())
  525. return false;
  526. u8 spectral_selection_end = 0;
  527. stream >> spectral_selection_end;
  528. if (stream.handle_any_error())
  529. return false;
  530. u8 successive_approximation = 0;
  531. stream >> successive_approximation;
  532. if (stream.handle_any_error())
  533. return false;
  534. // The three values should be fixed for baseline JPEGs utilizing sequential DCT.
  535. if (spectral_selection_start != 0 || spectral_selection_end != 63 || successive_approximation != 0) {
  536. #ifdef JPG_DEBUG
  537. dbg() << stream.offset() << ": ERROR! Start of Selection: " << spectral_selection_start
  538. << ", End of Selection: " << spectral_selection_end
  539. << ", Successive Approximation: " << successive_approximation << "!";
  540. #endif
  541. return false;
  542. }
  543. return true;
  544. }
  545. static bool read_reset_marker(InputMemoryStream& stream, JPGLoadingContext& context)
  546. {
  547. u16 bytes_to_read = read_be_word(stream);
  548. if (stream.handle_any_error())
  549. return false;
  550. bytes_to_read -= 2;
  551. if (bytes_to_read != 2) {
  552. #ifdef JPG_DEBUG
  553. dbg() << stream.offset() << ": Malformed reset marker found!";
  554. #endif
  555. return false;
  556. }
  557. context.dc_reset_interval = read_be_word(stream);
  558. if (stream.handle_any_error())
  559. return false;
  560. return true;
  561. }
  562. static bool read_huffman_table(InputMemoryStream& stream, JPGLoadingContext& context)
  563. {
  564. i32 bytes_to_read = read_be_word(stream);
  565. if (stream.handle_any_error())
  566. return false;
  567. if (!bounds_okay(stream.offset(), bytes_to_read, context.data_size))
  568. return false;
  569. bytes_to_read -= 2;
  570. while (bytes_to_read > 0) {
  571. HuffmanTableSpec table;
  572. u8 table_info = 0;
  573. stream >> table_info;
  574. if (stream.handle_any_error())
  575. return false;
  576. u8 table_type = table_info >> 4;
  577. u8 table_destination_id = table_info & 0x0F;
  578. if (table_type > 1) {
  579. #ifdef JPG_DEBUG
  580. dbg() << stream.offset() << String::format(": Unrecognized huffman table: %i!", table_type);
  581. #endif
  582. return false;
  583. }
  584. if (table_destination_id > 1) {
  585. #ifdef JPG_DEBUG
  586. dbg() << stream.offset()
  587. << String::format(": Invalid huffman table destination id: %i!", table_destination_id);
  588. #endif
  589. return false;
  590. }
  591. table.type = table_type;
  592. table.destination_id = table_destination_id;
  593. u32 total_codes = 0;
  594. // Read code counts. At each index K, the value represents the number of K+1 bit codes in this header.
  595. for (int i = 0; i < 16; i++) {
  596. u8 count = 0;
  597. stream >> count;
  598. if (stream.handle_any_error())
  599. return false;
  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 = 0;
  607. stream >> symbol;
  608. if (stream.handle_any_error())
  609. return false;
  610. table.symbols.append(symbol);
  611. }
  612. if (stream.handle_any_error())
  613. return false;
  614. auto& huffman_table = table.type == 0 ? context.dc_tables : context.ac_tables;
  615. huffman_table.set(table.destination_id, table);
  616. ASSERT(huffman_table.size() <= 2);
  617. bytes_to_read -= 1 + 16 + total_codes;
  618. }
  619. if (bytes_to_read != 0) {
  620. #ifdef JPG_DEBUG
  621. dbg() << stream.offset() << ": Extra bytes detected in huffman header!";
  622. #endif
  623. return false;
  624. }
  625. return true;
  626. }
  627. static inline bool validate_luma_and_modify_context(const ComponentSpec& luma, JPGLoadingContext& context)
  628. {
  629. if ((luma.hsample_factor == 1 || luma.hsample_factor == 2) && (luma.vsample_factor == 1 || luma.vsample_factor == 2)) {
  630. context.mblock_meta.hpadded_count += luma.hsample_factor == 1 ? 0 : context.mblock_meta.hcount % 2;
  631. context.mblock_meta.vpadded_count += luma.vsample_factor == 1 ? 0 : context.mblock_meta.vcount % 2;
  632. context.mblock_meta.padded_total = context.mblock_meta.hpadded_count * context.mblock_meta.vpadded_count;
  633. // For easy reference to relevant sample factors.
  634. context.hsample_factor = luma.hsample_factor;
  635. context.vsample_factor = luma.vsample_factor;
  636. #ifdef JPG_DEBUG
  637. dbg() << String::format("Horizontal Subsampling Factor: %i", luma.hsample_factor);
  638. dbg() << String::format("Vertical Subsampling Factor: %i", luma.vsample_factor);
  639. #endif
  640. return true;
  641. }
  642. return false;
  643. }
  644. static inline void set_macroblock_metadata(JPGLoadingContext& context)
  645. {
  646. context.mblock_meta.hcount = (context.frame.width + 7) / 8;
  647. context.mblock_meta.vcount = (context.frame.height + 7) / 8;
  648. context.mblock_meta.hpadded_count = context.mblock_meta.hcount;
  649. context.mblock_meta.vpadded_count = context.mblock_meta.vcount;
  650. context.mblock_meta.total = context.mblock_meta.hcount * context.mblock_meta.vcount;
  651. }
  652. static bool read_start_of_frame(InputMemoryStream& stream, JPGLoadingContext& context)
  653. {
  654. if (context.state == JPGLoadingContext::FrameDecoded) {
  655. #ifdef JPG_DEBUG
  656. dbg() << stream.offset() << ": SOF repeated!";
  657. #endif
  658. return false;
  659. }
  660. i32 bytes_to_read = read_be_word(stream);
  661. if (stream.handle_any_error())
  662. return false;
  663. bytes_to_read -= 2;
  664. if (!bounds_okay(stream.offset(), bytes_to_read, context.data_size))
  665. return false;
  666. stream >> context.frame.precision;
  667. if (stream.handle_any_error())
  668. return false;
  669. if (context.frame.precision != 8) {
  670. #ifdef JPG_DEBUG
  671. dbg() << stream.offset() << ": SOF precision != 8!";
  672. #endif
  673. return false;
  674. }
  675. context.frame.height = read_be_word(stream);
  676. if (stream.handle_any_error())
  677. return false;
  678. context.frame.width = read_be_word(stream);
  679. if (stream.handle_any_error())
  680. return false;
  681. if (!context.frame.width || !context.frame.height) {
  682. #ifdef JPG_DEBUG
  683. dbg() << stream.offset() << ": ERROR! Image height: " << context.frame.height << ", Image width: "
  684. << context.frame.width << "!";
  685. #endif
  686. return false;
  687. }
  688. set_macroblock_metadata(context);
  689. stream >> context.component_count;
  690. if (stream.handle_any_error())
  691. return false;
  692. if (context.component_count != 1 && context.component_count != 3) {
  693. #ifdef JPG_DEBUG
  694. dbg() << stream.offset() << ": Unsupported number of components in SOF: "
  695. << context.component_count << "!";
  696. #endif
  697. return false;
  698. }
  699. for (u8 i = 0; i < context.component_count; i++) {
  700. ComponentSpec component;
  701. component.serial_id = i;
  702. stream >> component.id;
  703. if (stream.handle_any_error())
  704. return false;
  705. u8 subsample_factors = 0;
  706. stream >> subsample_factors;
  707. if (stream.handle_any_error())
  708. return false;
  709. component.hsample_factor = subsample_factors >> 4;
  710. component.vsample_factor = subsample_factors & 0x0F;
  711. if (component.serial_id == 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. #ifdef JPG_DEBUG
  716. dbg() << stream.offset() << ": Unsupported luma subsampling factors: "
  717. << "horizontal: " << component.hsample_factor << ", vertical: " << component.vsample_factor;
  718. #endif
  719. return false;
  720. }
  721. } else {
  722. if (component.hsample_factor != 1 || component.vsample_factor != 1) {
  723. #ifdef JPG_DEBUG
  724. dbg() << stream.offset() << ": Unsupported chroma subsampling factors: "
  725. << "horizontal: " << component.hsample_factor << ", vertical: " << component.vsample_factor;
  726. #endif
  727. return false;
  728. }
  729. }
  730. stream >> component.qtable_id;
  731. if (stream.handle_any_error())
  732. return false;
  733. if (component.qtable_id > 1) {
  734. #ifdef JPG_DEBUG
  735. dbg() << stream.offset() << ": Unsupported quantization table id: "
  736. << component.qtable_id << "!";
  737. #endif
  738. return false;
  739. }
  740. context.components.set(component.id, component);
  741. }
  742. return true;
  743. }
  744. static bool read_quantization_table(InputMemoryStream& stream, JPGLoadingContext& context)
  745. {
  746. i32 bytes_to_read = read_be_word(stream);
  747. if (stream.handle_any_error())
  748. return false;
  749. bytes_to_read -= 2;
  750. if (!bounds_okay(stream.offset(), bytes_to_read, context.data_size))
  751. return false;
  752. while (bytes_to_read > 0) {
  753. u8 info_byte = 0;
  754. stream >> info_byte;
  755. if (stream.handle_any_error())
  756. return false;
  757. u8 element_unit_hint = info_byte >> 4;
  758. if (element_unit_hint > 1) {
  759. #ifdef JPG_DEBUG
  760. dbg() << stream.offset()
  761. << String::format(": Unsupported unit hint in quantization table: %i!", element_unit_hint);
  762. #endif
  763. return false;
  764. }
  765. u8 table_id = info_byte & 0x0F;
  766. if (table_id > 1) {
  767. #ifdef JPG_DEBUG
  768. dbg() << stream.offset() << String::format(": Unsupported quantization table id: %i!", table_id);
  769. #endif
  770. return false;
  771. }
  772. u32* table = table_id == 0 ? context.luma_table : context.chroma_table;
  773. for (int i = 0; i < 64; i++) {
  774. if (element_unit_hint == 0) {
  775. u8 tmp = 0;
  776. stream >> tmp;
  777. if (stream.handle_any_error())
  778. return false;
  779. table[zigzag_map[i]] = tmp;
  780. } else {
  781. table[zigzag_map[i]] = read_be_word(stream);
  782. if (stream.handle_any_error())
  783. return false;
  784. }
  785. }
  786. if (stream.handle_any_error())
  787. return false;
  788. bytes_to_read -= 1 + (element_unit_hint == 0 ? 64 : 128);
  789. }
  790. if (bytes_to_read != 0) {
  791. #ifdef JPG_DEBUG
  792. dbg() << stream.offset() << ": Invalid length for one or more quantization tables!";
  793. #endif
  794. return false;
  795. }
  796. return true;
  797. }
  798. static bool skip_marker_with_length(InputMemoryStream& stream)
  799. {
  800. u16 bytes_to_skip = read_be_word(stream);
  801. bytes_to_skip -= 2;
  802. if (stream.handle_any_error())
  803. return false;
  804. stream.discard_or_error(bytes_to_skip);
  805. return !stream.handle_any_error();
  806. }
  807. static void dequantize(JPGLoadingContext& context, Vector<Macroblock>& macroblocks)
  808. {
  809. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  810. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  811. for (auto it = context.components.begin(); it != context.components.end(); ++it) {
  812. auto& component = it->value;
  813. const u32* table = component.qtable_id == 0 ? context.luma_table : context.chroma_table;
  814. for (u32 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  815. for (u32 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  816. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  817. Macroblock& block = macroblocks[mb_index];
  818. int* block_component = component.serial_id == 0 ? block.y : (component.serial_id == 1 ? block.cb : block.cr);
  819. for (u32 k = 0; k < 64; k++)
  820. block_component[k] *= table[k];
  821. }
  822. }
  823. }
  824. }
  825. }
  826. }
  827. static void inverse_dct(const JPGLoadingContext& context, Vector<Macroblock>& macroblocks)
  828. {
  829. static const float m0 = 2.0 * cos(1.0 / 16.0 * 2.0 * M_PI);
  830. static const float m1 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
  831. static const float m3 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
  832. static const float m5 = 2.0 * cos(3.0 / 16.0 * 2.0 * M_PI);
  833. static const float m2 = m0 - m5;
  834. static const float m4 = m0 + m5;
  835. static const float s0 = cos(0.0 / 16.0 * M_PI) / sqrt(8);
  836. static const float s1 = cos(1.0 / 16.0 * M_PI) / 2.0;
  837. static const float s2 = cos(2.0 / 16.0 * M_PI) / 2.0;
  838. static const float s3 = cos(3.0 / 16.0 * M_PI) / 2.0;
  839. static const float s4 = cos(4.0 / 16.0 * M_PI) / 2.0;
  840. static const float s5 = cos(5.0 / 16.0 * M_PI) / 2.0;
  841. static const float s6 = cos(6.0 / 16.0 * M_PI) / 2.0;
  842. static const float s7 = cos(7.0 / 16.0 * M_PI) / 2.0;
  843. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  844. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  845. for (auto it = context.components.begin(); it != context.components.end(); ++it) {
  846. auto& component = it->value;
  847. for (u8 vfactor_i = 0; vfactor_i < component.vsample_factor; vfactor_i++) {
  848. for (u8 hfactor_i = 0; hfactor_i < component.hsample_factor; hfactor_i++) {
  849. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hfactor_i + hcursor);
  850. Macroblock& block = macroblocks[mb_index];
  851. i32* block_component = component.serial_id == 0 ? block.y : (component.serial_id == 1 ? block.cb : block.cr);
  852. for (u32 k = 0; k < 8; ++k) {
  853. const float g0 = block_component[0 * 8 + k] * s0;
  854. const float g1 = block_component[4 * 8 + k] * s4;
  855. const float g2 = block_component[2 * 8 + k] * s2;
  856. const float g3 = block_component[6 * 8 + k] * s6;
  857. const float g4 = block_component[5 * 8 + k] * s5;
  858. const float g5 = block_component[1 * 8 + k] * s1;
  859. const float g6 = block_component[7 * 8 + k] * s7;
  860. const float g7 = block_component[3 * 8 + k] * s3;
  861. const float f0 = g0;
  862. const float f1 = g1;
  863. const float f2 = g2;
  864. const float f3 = g3;
  865. const float f4 = g4 - g7;
  866. const float f5 = g5 + g6;
  867. const float f6 = g5 - g6;
  868. const float f7 = g4 + g7;
  869. const float e0 = f0;
  870. const float e1 = f1;
  871. const float e2 = f2 - f3;
  872. const float e3 = f2 + f3;
  873. const float e4 = f4;
  874. const float e5 = f5 - f7;
  875. const float e6 = f6;
  876. const float e7 = f5 + f7;
  877. const float e8 = f4 + f6;
  878. const float d0 = e0;
  879. const float d1 = e1;
  880. const float d2 = e2 * m1;
  881. const float d3 = e3;
  882. const float d4 = e4 * m2;
  883. const float d5 = e5 * m3;
  884. const float d6 = e6 * m4;
  885. const float d7 = e7;
  886. const float d8 = e8 * m5;
  887. const float c0 = d0 + d1;
  888. const float c1 = d0 - d1;
  889. const float c2 = d2 - d3;
  890. const float c3 = d3;
  891. const float c4 = d4 + d8;
  892. const float c5 = d5 + d7;
  893. const float c6 = d6 - d8;
  894. const float c7 = d7;
  895. const float c8 = c5 - c6;
  896. const float b0 = c0 + c3;
  897. const float b1 = c1 + c2;
  898. const float b2 = c1 - c2;
  899. const float b3 = c0 - c3;
  900. const float b4 = c4 - c8;
  901. const float b5 = c8;
  902. const float b6 = c6 - c7;
  903. const float b7 = c7;
  904. block_component[0 * 8 + k] = b0 + b7;
  905. block_component[1 * 8 + k] = b1 + b6;
  906. block_component[2 * 8 + k] = b2 + b5;
  907. block_component[3 * 8 + k] = b3 + b4;
  908. block_component[4 * 8 + k] = b3 - b4;
  909. block_component[5 * 8 + k] = b2 - b5;
  910. block_component[6 * 8 + k] = b1 - b6;
  911. block_component[7 * 8 + k] = b0 - b7;
  912. }
  913. for (u32 l = 0; l < 8; ++l) {
  914. const float g0 = block_component[l * 8 + 0] * s0;
  915. const float g1 = block_component[l * 8 + 4] * s4;
  916. const float g2 = block_component[l * 8 + 2] * s2;
  917. const float g3 = block_component[l * 8 + 6] * s6;
  918. const float g4 = block_component[l * 8 + 5] * s5;
  919. const float g5 = block_component[l * 8 + 1] * s1;
  920. const float g6 = block_component[l * 8 + 7] * s7;
  921. const float g7 = block_component[l * 8 + 3] * s3;
  922. const float f0 = g0;
  923. const float f1 = g1;
  924. const float f2 = g2;
  925. const float f3 = g3;
  926. const float f4 = g4 - g7;
  927. const float f5 = g5 + g6;
  928. const float f6 = g5 - g6;
  929. const float f7 = g4 + g7;
  930. const float e0 = f0;
  931. const float e1 = f1;
  932. const float e2 = f2 - f3;
  933. const float e3 = f2 + f3;
  934. const float e4 = f4;
  935. const float e5 = f5 - f7;
  936. const float e6 = f6;
  937. const float e7 = f5 + f7;
  938. const float e8 = f4 + f6;
  939. const float d0 = e0;
  940. const float d1 = e1;
  941. const float d2 = e2 * m1;
  942. const float d3 = e3;
  943. const float d4 = e4 * m2;
  944. const float d5 = e5 * m3;
  945. const float d6 = e6 * m4;
  946. const float d7 = e7;
  947. const float d8 = e8 * m5;
  948. const float c0 = d0 + d1;
  949. const float c1 = d0 - d1;
  950. const float c2 = d2 - d3;
  951. const float c3 = d3;
  952. const float c4 = d4 + d8;
  953. const float c5 = d5 + d7;
  954. const float c6 = d6 - d8;
  955. const float c7 = d7;
  956. const float c8 = c5 - c6;
  957. const float b0 = c0 + c3;
  958. const float b1 = c1 + c2;
  959. const float b2 = c1 - c2;
  960. const float b3 = c0 - c3;
  961. const float b4 = c4 - c8;
  962. const float b5 = c8;
  963. const float b6 = c6 - c7;
  964. const float b7 = c7;
  965. block_component[l * 8 + 0] = b0 + b7;
  966. block_component[l * 8 + 1] = b1 + b6;
  967. block_component[l * 8 + 2] = b2 + b5;
  968. block_component[l * 8 + 3] = b3 + b4;
  969. block_component[l * 8 + 4] = b3 - b4;
  970. block_component[l * 8 + 5] = b2 - b5;
  971. block_component[l * 8 + 6] = b1 - b6;
  972. block_component[l * 8 + 7] = b0 - b7;
  973. }
  974. }
  975. }
  976. }
  977. }
  978. }
  979. }
  980. static void ycbcr_to_rgb(const JPGLoadingContext& context, Vector<Macroblock>& macroblocks)
  981. {
  982. for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
  983. for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
  984. const u32 chroma_block_index = vcursor * context.mblock_meta.hpadded_count + hcursor;
  985. const Macroblock& chroma = macroblocks[chroma_block_index];
  986. // Overflows are intentional.
  987. for (u8 vfactor_i = context.vsample_factor - 1; vfactor_i < context.vsample_factor; --vfactor_i) {
  988. for (u8 hfactor_i = context.hsample_factor - 1; hfactor_i < context.hsample_factor; --hfactor_i) {
  989. u32 mb_index = (vcursor + vfactor_i) * context.mblock_meta.hpadded_count + (hcursor + hfactor_i);
  990. i32* y = macroblocks[mb_index].y;
  991. i32* cb = macroblocks[mb_index].cb;
  992. i32* cr = macroblocks[mb_index].cr;
  993. for (u8 i = 7; i < 8; --i) {
  994. for (u8 j = 7; j < 8; --j) {
  995. const u8 pixel = i * 8 + j;
  996. const u32 chroma_pxrow = (i / context.vsample_factor) + 4 * vfactor_i;
  997. const u32 chroma_pxcol = (j / context.hsample_factor) + 4 * hfactor_i;
  998. const u32 chroma_pixel = chroma_pxrow * 8 + chroma_pxcol;
  999. int r = y[pixel] + 1.402f * chroma.cr[chroma_pixel] + 128;
  1000. int g = y[pixel] - 0.344f * chroma.cb[chroma_pixel] - 0.714f * chroma.cr[chroma_pixel] + 128;
  1001. int b = y[pixel] + 1.772f * chroma.cb[chroma_pixel] + 128;
  1002. y[pixel] = r < 0 ? 0 : (r > 255 ? 255 : r);
  1003. cb[pixel] = g < 0 ? 0 : (g > 255 ? 255 : g);
  1004. cr[pixel] = b < 0 ? 0 : (b > 255 ? 255 : b);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. static void compose_bitmap(JPGLoadingContext& context, const Vector<Macroblock>& macroblocks)
  1013. {
  1014. context.bitmap = Bitmap::create_purgeable(BitmapFormat::RGB32, { context.frame.width, context.frame.height });
  1015. for (u32 y = context.frame.height - 1; y < context.frame.height; y--) {
  1016. const u32 block_row = y / 8;
  1017. const u32 pixel_row = y % 8;
  1018. for (u32 x = 0; x < context.frame.width; x++) {
  1019. const u32 block_column = x / 8;
  1020. auto& block = macroblocks[block_row * context.mblock_meta.hpadded_count + block_column];
  1021. const u32 pixel_column = x % 8;
  1022. const u32 pixel_index = pixel_row * 8 + pixel_column;
  1023. const Color color { (u8)block.y[pixel_index], (u8)block.cb[pixel_index], (u8)block.cr[pixel_index] };
  1024. context.bitmap->set_pixel(x, y, color);
  1025. }
  1026. }
  1027. }
  1028. static bool parse_header(InputMemoryStream& stream, JPGLoadingContext& context)
  1029. {
  1030. auto marker = read_marker_at_cursor(stream);
  1031. if (stream.handle_any_error())
  1032. return false;
  1033. if (marker != JPG_SOI) {
  1034. #ifdef JPG_DEBUG
  1035. dbg() << stream.offset() << String::format(": SOI not found: %x!", marker);
  1036. #endif
  1037. return false;
  1038. }
  1039. for (;;) {
  1040. marker = read_marker_at_cursor(stream);
  1041. if (stream.handle_any_error())
  1042. return false;
  1043. // Set frame type if the marker marks a new frame.
  1044. if (marker >= 0xFFC0 && marker <= 0xFFCF) {
  1045. // Ignore interleaved markers.
  1046. if (marker != 0xFFC4 && marker != 0xFFC8 && marker != 0xFFCC) {
  1047. context.frame.type = static_cast<StartOfFrame::FrameType>(marker & 0xF);
  1048. }
  1049. }
  1050. switch (marker) {
  1051. case JPG_INVALID:
  1052. case JPG_RST0:
  1053. case JPG_RST1:
  1054. case JPG_RST2:
  1055. case JPG_RST3:
  1056. case JPG_RST4:
  1057. case JPG_RST5:
  1058. case JPG_RST6:
  1059. case JPG_RST7:
  1060. case JPG_SOI:
  1061. case JPG_EOI:
  1062. #ifdef JPG_DEBUG
  1063. dbg() << stream.offset() << String::format(": Unexpected marker %x!", marker);
  1064. #endif
  1065. return false;
  1066. case JPG_SOF0:
  1067. if (!read_start_of_frame(stream, context))
  1068. return false;
  1069. context.state = JPGLoadingContext::FrameDecoded;
  1070. break;
  1071. case JPG_DQT:
  1072. if (!read_quantization_table(stream, context))
  1073. return false;
  1074. break;
  1075. case JPG_RST:
  1076. if (!read_reset_marker(stream, context))
  1077. return false;
  1078. break;
  1079. case JPG_DHT:
  1080. if (!read_huffman_table(stream, context))
  1081. return false;
  1082. break;
  1083. case JPG_SOS:
  1084. return read_start_of_scan(stream, context);
  1085. default:
  1086. if (!skip_marker_with_length(stream)) {
  1087. #ifdef JPG_DEBUG
  1088. dbg() << stream.offset() << String::format(": Error skipping marker: %x!", marker);
  1089. #endif
  1090. return false;
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. ASSERT_NOT_REACHED();
  1096. }
  1097. static bool scan_huffman_stream(InputMemoryStream& stream, JPGLoadingContext& context)
  1098. {
  1099. u8 last_byte;
  1100. u8 current_byte = 0;
  1101. stream >> current_byte;
  1102. if (stream.handle_any_error())
  1103. return false;
  1104. for (;;) {
  1105. last_byte = current_byte;
  1106. stream >> current_byte;
  1107. if (stream.handle_any_error()) {
  1108. #ifdef JPG_DEBUG
  1109. dbg() << stream.offset() << ": EOI not found!";
  1110. #endif
  1111. return false;
  1112. }
  1113. if (last_byte == 0xFF) {
  1114. if (current_byte == 0xFF)
  1115. continue;
  1116. if (current_byte == 0x00) {
  1117. stream >> current_byte;
  1118. if (stream.handle_any_error())
  1119. return false;
  1120. context.huffman_stream.stream.append(last_byte);
  1121. continue;
  1122. }
  1123. Marker marker = 0xFF00 | current_byte;
  1124. if (marker == JPG_EOI)
  1125. return true;
  1126. if (marker >= JPG_RST0 && marker <= JPG_RST7) {
  1127. context.huffman_stream.stream.append(marker);
  1128. stream >> current_byte;
  1129. if (stream.handle_any_error())
  1130. return false;
  1131. continue;
  1132. }
  1133. #ifdef JPG_DEBUG
  1134. dbg() << stream.offset() << String::format(": Invalid marker: %x!", marker);
  1135. #endif
  1136. return false;
  1137. } else {
  1138. context.huffman_stream.stream.append(last_byte);
  1139. }
  1140. }
  1141. ASSERT_NOT_REACHED();
  1142. }
  1143. static bool decode_jpg(JPGLoadingContext& context)
  1144. {
  1145. InputMemoryStream stream { { context.data, context.data_size } };
  1146. if (!parse_header(stream, context))
  1147. return false;
  1148. if (!scan_huffman_stream(stream, context))
  1149. return false;
  1150. auto result = decode_huffman_stream(context);
  1151. if (!result.has_value()) {
  1152. #ifdef JPG_DEBUG
  1153. dbg() << stream.offset() << ": Failed to decode Macroblocks!";
  1154. #endif
  1155. return false;
  1156. }
  1157. auto macroblocks = result.release_value();
  1158. dequantize(context, macroblocks);
  1159. inverse_dct(context, macroblocks);
  1160. ycbcr_to_rgb(context, macroblocks);
  1161. compose_bitmap(context, macroblocks);
  1162. return true;
  1163. }
  1164. static RefPtr<Gfx::Bitmap> load_jpg_impl(const u8* data, size_t data_size)
  1165. {
  1166. JPGLoadingContext context;
  1167. context.data = data;
  1168. context.data_size = data_size;
  1169. if (!decode_jpg(context))
  1170. return nullptr;
  1171. return context.bitmap;
  1172. }
  1173. RefPtr<Gfx::Bitmap> load_jpg(const StringView& path)
  1174. {
  1175. MappedFile mapped_file(path);
  1176. if (!mapped_file.is_valid()) {
  1177. return nullptr;
  1178. }
  1179. auto bitmap = load_jpg_impl((const u8*)mapped_file.data(), mapped_file.size());
  1180. if (bitmap)
  1181. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded JPG: %s", bitmap->width(), bitmap->height(), LexicalPath::canonicalized_path(path).characters()));
  1182. return bitmap;
  1183. }
  1184. RefPtr<Gfx::Bitmap> load_jpg_from_memory(const u8* data, size_t length)
  1185. {
  1186. auto bitmap = load_jpg_impl(data, length);
  1187. if (bitmap)
  1188. bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded jpg: <memory>", bitmap->width(), bitmap->height()));
  1189. return bitmap;
  1190. }
  1191. JPGImageDecoderPlugin::JPGImageDecoderPlugin(const u8* data, size_t size)
  1192. {
  1193. m_context = make<JPGLoadingContext>();
  1194. m_context->data = data;
  1195. m_context->data_size = size;
  1196. m_context->huffman_stream.stream.ensure_capacity(50 * KiB);
  1197. }
  1198. JPGImageDecoderPlugin::~JPGImageDecoderPlugin()
  1199. {
  1200. }
  1201. IntSize JPGImageDecoderPlugin::size()
  1202. {
  1203. if (m_context->state == JPGLoadingContext::State::Error)
  1204. return {};
  1205. if (m_context->state >= JPGLoadingContext::State::FrameDecoded)
  1206. return { m_context->frame.width, m_context->frame.height };
  1207. return {};
  1208. }
  1209. RefPtr<Gfx::Bitmap> JPGImageDecoderPlugin::bitmap()
  1210. {
  1211. if (m_context->state == JPGLoadingContext::State::Error)
  1212. return nullptr;
  1213. if (m_context->state < JPGLoadingContext::State::BitmapDecoded) {
  1214. if (!decode_jpg(*m_context)) {
  1215. m_context->state = JPGLoadingContext::State::Error;
  1216. return nullptr;
  1217. }
  1218. m_context->state = JPGLoadingContext::State::BitmapDecoded;
  1219. }
  1220. return m_context->bitmap;
  1221. }
  1222. void JPGImageDecoderPlugin::set_volatile()
  1223. {
  1224. if (m_context->bitmap)
  1225. m_context->bitmap->set_volatile();
  1226. }
  1227. bool JPGImageDecoderPlugin::set_nonvolatile()
  1228. {
  1229. if (!m_context->bitmap)
  1230. return false;
  1231. return m_context->bitmap->set_nonvolatile();
  1232. }
  1233. bool JPGImageDecoderPlugin::sniff()
  1234. {
  1235. return m_context->data_size > 3
  1236. && m_context->data[0] == 0xFF
  1237. && m_context->data[1] == 0xD8
  1238. && m_context->data[2] == 0xFF;
  1239. }
  1240. bool JPGImageDecoderPlugin::is_animated()
  1241. {
  1242. return false;
  1243. }
  1244. size_t JPGImageDecoderPlugin::loop_count()
  1245. {
  1246. return 0;
  1247. }
  1248. size_t JPGImageDecoderPlugin::frame_count()
  1249. {
  1250. return 1;
  1251. }
  1252. ImageFrameDescriptor JPGImageDecoderPlugin::frame(size_t i)
  1253. {
  1254. if (i > 0) {
  1255. return { bitmap(), 0 };
  1256. }
  1257. return {};
  1258. }
  1259. }