PNGLoader.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/Vector.h>
  10. #include <LibCompress/Zlib.h>
  11. #include <LibGfx/ImageFormats/PNGLoader.h>
  12. #include <LibGfx/ImageFormats/PNGShared.h>
  13. #include <LibGfx/Painter.h>
  14. #include <string.h>
  15. namespace Gfx {
  16. struct PNG_IHDR {
  17. NetworkOrdered<u32> width;
  18. NetworkOrdered<u32> height;
  19. u8 bit_depth { 0 };
  20. PNG::ColorType color_type { 0 };
  21. u8 compression_method { 0 };
  22. u8 filter_method { 0 };
  23. u8 interlace_method { 0 };
  24. };
  25. static_assert(AssertSize<PNG_IHDR, 13>());
  26. struct acTL_Chunk {
  27. NetworkOrdered<u32> num_frames;
  28. NetworkOrdered<u32> num_plays;
  29. };
  30. static_assert(AssertSize<acTL_Chunk, 8>());
  31. struct fcTL_Chunk {
  32. enum class DisposeOp : u8 {
  33. APNG_DISPOSE_OP_NONE = 0,
  34. APNG_DISPOSE_OP_BACKGROUND,
  35. APNG_DISPOSE_OP_PREVIOUS
  36. };
  37. enum class BlendOp : u8 {
  38. APNG_BLEND_OP_SOURCE = 0,
  39. APNG_BLEND_OP_OVER
  40. };
  41. NetworkOrdered<u32> sequence_number;
  42. NetworkOrdered<u32> width;
  43. NetworkOrdered<u32> height;
  44. NetworkOrdered<u32> x_offset;
  45. NetworkOrdered<u32> y_offset;
  46. NetworkOrdered<u16> delay_num;
  47. NetworkOrdered<u16> delay_den;
  48. DisposeOp dispose_op { DisposeOp::APNG_DISPOSE_OP_NONE };
  49. BlendOp blend_op { BlendOp::APNG_BLEND_OP_SOURCE };
  50. };
  51. static_assert(AssertSize<fcTL_Chunk, 26>());
  52. struct ChromaticitiesAndWhitepoint {
  53. NetworkOrdered<u32> white_point_x;
  54. NetworkOrdered<u32> white_point_y;
  55. NetworkOrdered<u32> red_x;
  56. NetworkOrdered<u32> red_y;
  57. NetworkOrdered<u32> green_x;
  58. NetworkOrdered<u32> green_y;
  59. NetworkOrdered<u32> blue_x;
  60. NetworkOrdered<u32> blue_y;
  61. };
  62. static_assert(AssertSize<ChromaticitiesAndWhitepoint, 32>());
  63. struct CodingIndependentCodePoints {
  64. u8 color_primaries;
  65. u8 transfer_function;
  66. u8 matrix_coefficients;
  67. u8 video_full_range_flag;
  68. };
  69. static_assert(AssertSize<CodingIndependentCodePoints, 4>());
  70. struct EmbeddedICCProfile {
  71. StringView profile_name;
  72. ReadonlyBytes compressed_data;
  73. };
  74. struct Scanline {
  75. PNG::FilterType filter;
  76. ReadonlyBytes data {};
  77. };
  78. struct [[gnu::packed]] PaletteEntry {
  79. u8 r;
  80. u8 g;
  81. u8 b;
  82. // u8 a;
  83. };
  84. template<typename T>
  85. struct [[gnu::packed]] Tuple {
  86. T gray;
  87. T a;
  88. };
  89. template<typename T>
  90. struct [[gnu::packed]] Triplet {
  91. T r;
  92. T g;
  93. T b;
  94. bool operator==(Triplet const& other) const = default;
  95. };
  96. template<typename T>
  97. struct [[gnu::packed]] Quartet {
  98. T r;
  99. T g;
  100. T b;
  101. T a;
  102. };
  103. enum PngInterlaceMethod {
  104. Null = 0,
  105. Adam7 = 1
  106. };
  107. enum RenderingIntent {
  108. Perceptual = 0,
  109. RelativeColorimetric = 1,
  110. Saturation = 2,
  111. AbsoluteColorimetric = 3,
  112. };
  113. struct AnimationFrame {
  114. fcTL_Chunk const& fcTL;
  115. RefPtr<Bitmap> bitmap;
  116. Vector<u8> compressed_data;
  117. AnimationFrame(fcTL_Chunk const& fcTL)
  118. : fcTL(fcTL)
  119. {
  120. }
  121. u32 duration_ms() const
  122. {
  123. u32 num = fcTL.delay_num;
  124. if (num == 0)
  125. return 1;
  126. u32 denom = fcTL.delay_den != 0 ? static_cast<u32>(fcTL.delay_den) : 100u;
  127. return (num * 1000) / denom;
  128. }
  129. IntRect rect() const
  130. {
  131. return { fcTL.x_offset, fcTL.y_offset, fcTL.width, fcTL.height };
  132. }
  133. };
  134. struct PNGLoadingContext {
  135. enum State {
  136. NotDecoded = 0,
  137. Error,
  138. HeaderDecoded,
  139. SizeDecoded,
  140. ImageDataChunkDecoded,
  141. ChunksDecoded,
  142. BitmapDecoded,
  143. };
  144. State state { State::NotDecoded };
  145. u8 const* data { nullptr };
  146. u8 const* data_current_ptr { nullptr };
  147. size_t data_size { 0 };
  148. int width { -1 };
  149. int height { -1 };
  150. u8 bit_depth { 0 };
  151. PNG::ColorType color_type { 0 };
  152. u8 compression_method { 0 };
  153. u8 filter_method { 0 };
  154. u8 interlace_method { 0 };
  155. u8 channels { 0 };
  156. u32 animation_next_expected_seq { 0 };
  157. u32 animation_next_frame_to_render { 0 };
  158. u32 animation_frame_count { 0 };
  159. u32 animation_loop_count { 0 };
  160. Optional<u32> last_completed_animation_frame_index;
  161. bool is_first_idat_part_of_animation { false };
  162. bool has_seen_zlib_header { false };
  163. bool has_seen_iend { false };
  164. bool has_seen_idat_chunk { false };
  165. bool has_seen_actl_chunk_before_idat { false };
  166. bool has_alpha() const { return to_underlying(color_type) & 4 || palette_transparency_data.size() > 0; }
  167. Vector<Scanline> scanlines;
  168. ByteBuffer unfiltered_data;
  169. RefPtr<Gfx::Bitmap> bitmap;
  170. Vector<u8> compressed_data;
  171. Vector<PaletteEntry> palette_data;
  172. Vector<u8> palette_transparency_data;
  173. Vector<AnimationFrame> animation_frames;
  174. Optional<ChromaticitiesAndWhitepoint> chromaticities_and_whitepoint;
  175. Optional<CodingIndependentCodePoints> coding_independent_code_points;
  176. Optional<u32> gamma;
  177. Optional<EmbeddedICCProfile> embedded_icc_profile;
  178. Optional<ByteBuffer> decompressed_icc_profile;
  179. Optional<RenderingIntent> sRGB_rendering_intent;
  180. Checked<int> compute_row_size_for_width(int width)
  181. {
  182. Checked<int> row_size = width;
  183. row_size *= channels;
  184. row_size *= bit_depth;
  185. row_size += 7;
  186. row_size /= 8;
  187. if (row_size.has_overflow()) {
  188. dbgln("PNG too large, integer overflow while computing row size");
  189. state = State::Error;
  190. }
  191. return row_size;
  192. }
  193. PNGLoadingContext create_subimage_context(int width, int height)
  194. {
  195. PNGLoadingContext subimage_context;
  196. subimage_context.state = State::ChunksDecoded;
  197. subimage_context.width = width;
  198. subimage_context.height = height;
  199. subimage_context.channels = channels;
  200. subimage_context.color_type = color_type;
  201. subimage_context.palette_data = palette_data;
  202. subimage_context.palette_transparency_data = palette_transparency_data;
  203. subimage_context.bit_depth = bit_depth;
  204. subimage_context.filter_method = filter_method;
  205. return subimage_context;
  206. }
  207. };
  208. class Streamer {
  209. public:
  210. Streamer(u8 const* data, size_t size)
  211. : m_data_ptr(data)
  212. , m_size_remaining(size)
  213. {
  214. }
  215. template<typename T>
  216. bool read(T& value)
  217. {
  218. if (m_size_remaining < sizeof(T))
  219. return false;
  220. value = *((NetworkOrdered<T> const*)m_data_ptr);
  221. m_data_ptr += sizeof(T);
  222. m_size_remaining -= sizeof(T);
  223. return true;
  224. }
  225. bool read_bytes(u8* buffer, size_t count)
  226. {
  227. if (m_size_remaining < count)
  228. return false;
  229. memcpy(buffer, m_data_ptr, count);
  230. m_data_ptr += count;
  231. m_size_remaining -= count;
  232. return true;
  233. }
  234. bool wrap_bytes(ReadonlyBytes& buffer, size_t count)
  235. {
  236. if (m_size_remaining < count)
  237. return false;
  238. buffer = ReadonlyBytes { m_data_ptr, count };
  239. m_data_ptr += count;
  240. m_size_remaining -= count;
  241. return true;
  242. }
  243. u8 const* current_data_ptr() const { return m_data_ptr; }
  244. bool at_end() const { return !m_size_remaining; }
  245. private:
  246. u8 const* m_data_ptr { nullptr };
  247. size_t m_size_remaining { 0 };
  248. };
  249. static bool process_chunk(Streamer&, PNGLoadingContext& context);
  250. union [[gnu::packed]] Pixel {
  251. ARGB32 rgba { 0 };
  252. u8 v[4];
  253. struct {
  254. u8 r;
  255. u8 g;
  256. u8 b;
  257. u8 a;
  258. };
  259. };
  260. static_assert(AssertSize<Pixel, 4>());
  261. static void unfilter_scanline(PNG::FilterType filter, Bytes scanline_data, ReadonlyBytes previous_scanlines_data, u8 bytes_per_complete_pixel)
  262. {
  263. VERIFY(filter != PNG::FilterType::None);
  264. switch (filter) {
  265. case PNG::FilterType::Sub:
  266. // This loop starts at bytes_per_complete_pixel because all bytes before that are
  267. // guaranteed to have no valid byte at index (i - bytes_per_complete pixel).
  268. // All such invalid byte indexes should be treated as 0, and adding 0 to the current
  269. // byte would do nothing, so the first bytes_per_complete_pixel bytes can instead
  270. // just be skipped.
  271. for (size_t i = bytes_per_complete_pixel; i < scanline_data.size(); ++i) {
  272. u8 left = scanline_data[i - bytes_per_complete_pixel];
  273. scanline_data[i] += left;
  274. }
  275. break;
  276. case PNG::FilterType::Up:
  277. for (size_t i = 0; i < scanline_data.size(); ++i) {
  278. u8 above = previous_scanlines_data[i];
  279. scanline_data[i] += above;
  280. }
  281. break;
  282. case PNG::FilterType::Average:
  283. for (size_t i = 0; i < scanline_data.size(); ++i) {
  284. u32 left = (i < bytes_per_complete_pixel) ? 0 : scanline_data[i - bytes_per_complete_pixel];
  285. u32 above = previous_scanlines_data[i];
  286. u8 average = (left + above) / 2;
  287. scanline_data[i] += average;
  288. }
  289. break;
  290. case PNG::FilterType::Paeth:
  291. for (size_t i = 0; i < scanline_data.size(); ++i) {
  292. u8 left = (i < bytes_per_complete_pixel) ? 0 : scanline_data[i - bytes_per_complete_pixel];
  293. u8 above = previous_scanlines_data[i];
  294. u8 upper_left = (i < bytes_per_complete_pixel) ? 0 : previous_scanlines_data[i - bytes_per_complete_pixel];
  295. i32 predictor = left + above - upper_left;
  296. u32 predictor_left = abs(predictor - left);
  297. u32 predictor_above = abs(predictor - above);
  298. u32 predictor_upper_left = abs(predictor - upper_left);
  299. u8 nearest;
  300. if (predictor_left <= predictor_above && predictor_left <= predictor_upper_left) {
  301. nearest = left;
  302. } else if (predictor_above <= predictor_upper_left) {
  303. nearest = above;
  304. } else {
  305. nearest = upper_left;
  306. }
  307. scanline_data[i] += nearest;
  308. }
  309. break;
  310. default:
  311. VERIFY_NOT_REACHED();
  312. }
  313. }
  314. template<typename T>
  315. ALWAYS_INLINE static void unpack_grayscale_without_alpha(PNGLoadingContext& context)
  316. {
  317. for (int y = 0; y < context.height; ++y) {
  318. auto* gray_values = reinterpret_cast<T const*>(context.scanlines[y].data.data());
  319. for (int i = 0; i < context.width; ++i) {
  320. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  321. pixel.r = gray_values[i];
  322. pixel.g = gray_values[i];
  323. pixel.b = gray_values[i];
  324. pixel.a = 0xff;
  325. }
  326. }
  327. }
  328. template<typename T>
  329. ALWAYS_INLINE static void unpack_grayscale_with_alpha(PNGLoadingContext& context)
  330. {
  331. for (int y = 0; y < context.height; ++y) {
  332. auto* tuples = reinterpret_cast<Tuple<T> const*>(context.scanlines[y].data.data());
  333. for (int i = 0; i < context.width; ++i) {
  334. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  335. pixel.r = tuples[i].gray;
  336. pixel.g = tuples[i].gray;
  337. pixel.b = tuples[i].gray;
  338. pixel.a = tuples[i].a;
  339. }
  340. }
  341. }
  342. template<typename T>
  343. ALWAYS_INLINE static void unpack_triplets_without_alpha(PNGLoadingContext& context)
  344. {
  345. for (int y = 0; y < context.height; ++y) {
  346. auto* triplets = reinterpret_cast<Triplet<T> const*>(context.scanlines[y].data.data());
  347. for (int i = 0; i < context.width; ++i) {
  348. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  349. pixel.r = triplets[i].r;
  350. pixel.g = triplets[i].g;
  351. pixel.b = triplets[i].b;
  352. pixel.a = 0xff;
  353. }
  354. }
  355. }
  356. template<typename T>
  357. ALWAYS_INLINE static void unpack_triplets_with_transparency_value(PNGLoadingContext& context, Triplet<T> transparency_value)
  358. {
  359. for (int y = 0; y < context.height; ++y) {
  360. auto* triplets = reinterpret_cast<Triplet<T> const*>(context.scanlines[y].data.data());
  361. for (int i = 0; i < context.width; ++i) {
  362. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  363. pixel.r = triplets[i].r;
  364. pixel.g = triplets[i].g;
  365. pixel.b = triplets[i].b;
  366. if (triplets[i] == transparency_value)
  367. pixel.a = 0x00;
  368. else
  369. pixel.a = 0xff;
  370. }
  371. }
  372. }
  373. NEVER_INLINE FLATTEN static ErrorOr<void> unfilter(PNGLoadingContext& context)
  374. {
  375. // First unfilter the scanlines:
  376. // FIXME: Instead of creating a separate buffer for the scanlines that need to be
  377. // mutated, the mutation could be done in place (if the data was non-const).
  378. size_t bytes_per_scanline = context.scanlines[0].data.size();
  379. size_t bytes_needed_for_all_unfiltered_scanlines = 0;
  380. for (int y = 0; y < context.height; ++y) {
  381. if (context.scanlines[y].filter != PNG::FilterType::None) {
  382. bytes_needed_for_all_unfiltered_scanlines += bytes_per_scanline;
  383. }
  384. }
  385. context.unfiltered_data = TRY(ByteBuffer::create_uninitialized(bytes_needed_for_all_unfiltered_scanlines));
  386. // From section 6.3 of http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
  387. // "bpp is defined as the number of bytes per complete pixel, rounding up to one.
  388. // For example, for color type 2 with a bit depth of 16, bpp is equal to 6
  389. // (three samples, two bytes per sample); for color type 0 with a bit depth of 2,
  390. // bpp is equal to 1 (rounding up); for color type 4 with a bit depth of 16, bpp
  391. // is equal to 4 (two-byte grayscale sample, plus two-byte alpha sample)."
  392. u8 bytes_per_complete_pixel = (context.bit_depth + 7) / 8 * context.channels;
  393. u8 dummy_scanline_bytes[bytes_per_scanline];
  394. memset(dummy_scanline_bytes, 0, sizeof(dummy_scanline_bytes));
  395. auto previous_scanlines_data = ReadonlyBytes { dummy_scanline_bytes, sizeof(dummy_scanline_bytes) };
  396. for (int y = 0, data_start = 0; y < context.height; ++y) {
  397. if (context.scanlines[y].filter != PNG::FilterType::None) {
  398. auto scanline_data_slice = context.unfiltered_data.bytes().slice(data_start, bytes_per_scanline);
  399. // Copy the current values over and set the scanline's data to the to-be-mutated slice
  400. context.scanlines[y].data.copy_to(scanline_data_slice);
  401. context.scanlines[y].data = scanline_data_slice;
  402. unfilter_scanline(context.scanlines[y].filter, scanline_data_slice, previous_scanlines_data, bytes_per_complete_pixel);
  403. data_start += bytes_per_scanline;
  404. }
  405. previous_scanlines_data = context.scanlines[y].data;
  406. }
  407. // Now unpack the scanlines to RGBA:
  408. switch (context.color_type) {
  409. case PNG::ColorType::Greyscale:
  410. if (context.bit_depth == 8) {
  411. unpack_grayscale_without_alpha<u8>(context);
  412. } else if (context.bit_depth == 16) {
  413. unpack_grayscale_without_alpha<u16>(context);
  414. } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
  415. auto bit_depth_squared = context.bit_depth * context.bit_depth;
  416. auto pixels_per_byte = 8 / context.bit_depth;
  417. auto mask = (1 << context.bit_depth) - 1;
  418. for (int y = 0; y < context.height; ++y) {
  419. auto* gray_values = context.scanlines[y].data.data();
  420. for (int x = 0; x < context.width; ++x) {
  421. auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (x % pixels_per_byte));
  422. auto value = (gray_values[x / pixels_per_byte] >> bit_offset) & mask;
  423. auto& pixel = (Pixel&)context.bitmap->scanline(y)[x];
  424. pixel.r = value * (0xff / bit_depth_squared);
  425. pixel.g = value * (0xff / bit_depth_squared);
  426. pixel.b = value * (0xff / bit_depth_squared);
  427. pixel.a = 0xff;
  428. }
  429. }
  430. } else {
  431. VERIFY_NOT_REACHED();
  432. }
  433. break;
  434. case PNG::ColorType::GreyscaleWithAlpha:
  435. if (context.bit_depth == 8) {
  436. unpack_grayscale_with_alpha<u8>(context);
  437. } else if (context.bit_depth == 16) {
  438. unpack_grayscale_with_alpha<u16>(context);
  439. } else {
  440. VERIFY_NOT_REACHED();
  441. }
  442. break;
  443. case PNG::ColorType::Truecolor:
  444. if (context.palette_transparency_data.size() == 6) {
  445. if (context.bit_depth == 8) {
  446. unpack_triplets_with_transparency_value<u8>(context, Triplet<u8> { context.palette_transparency_data[0], context.palette_transparency_data[2], context.palette_transparency_data[4] });
  447. } else if (context.bit_depth == 16) {
  448. u16 tr = context.palette_transparency_data[0] | context.palette_transparency_data[1] << 8;
  449. u16 tg = context.palette_transparency_data[2] | context.palette_transparency_data[3] << 8;
  450. u16 tb = context.palette_transparency_data[4] | context.palette_transparency_data[5] << 8;
  451. unpack_triplets_with_transparency_value<u16>(context, Triplet<u16> { tr, tg, tb });
  452. } else {
  453. VERIFY_NOT_REACHED();
  454. }
  455. } else {
  456. if (context.bit_depth == 8)
  457. unpack_triplets_without_alpha<u8>(context);
  458. else if (context.bit_depth == 16)
  459. unpack_triplets_without_alpha<u16>(context);
  460. else
  461. VERIFY_NOT_REACHED();
  462. }
  463. break;
  464. case PNG::ColorType::TruecolorWithAlpha:
  465. if (context.bit_depth == 8) {
  466. for (int y = 0; y < context.height; ++y) {
  467. memcpy(context.bitmap->scanline(y), context.scanlines[y].data.data(), context.scanlines[y].data.size());
  468. }
  469. } else if (context.bit_depth == 16) {
  470. for (int y = 0; y < context.height; ++y) {
  471. auto* quartets = reinterpret_cast<Quartet<u16> const*>(context.scanlines[y].data.data());
  472. for (int i = 0; i < context.width; ++i) {
  473. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  474. pixel.r = quartets[i].r & 0xFF;
  475. pixel.g = quartets[i].g & 0xFF;
  476. pixel.b = quartets[i].b & 0xFF;
  477. pixel.a = quartets[i].a & 0xFF;
  478. }
  479. }
  480. } else {
  481. VERIFY_NOT_REACHED();
  482. }
  483. break;
  484. case PNG::ColorType::IndexedColor:
  485. if (context.bit_depth == 8) {
  486. for (int y = 0; y < context.height; ++y) {
  487. auto* palette_index = context.scanlines[y].data.data();
  488. for (int i = 0; i < context.width; ++i) {
  489. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  490. if (palette_index[i] >= context.palette_data.size())
  491. return Error::from_string_literal("PNGImageDecoderPlugin: Palette index out of range");
  492. auto& color = context.palette_data.at((int)palette_index[i]);
  493. auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u
  494. ? context.palette_transparency_data.data()[palette_index[i]]
  495. : 0xff;
  496. pixel.r = color.r;
  497. pixel.g = color.g;
  498. pixel.b = color.b;
  499. pixel.a = transparency;
  500. }
  501. }
  502. } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) {
  503. auto pixels_per_byte = 8 / context.bit_depth;
  504. auto mask = (1 << context.bit_depth) - 1;
  505. for (int y = 0; y < context.height; ++y) {
  506. auto* palette_indices = context.scanlines[y].data.data();
  507. for (int i = 0; i < context.width; ++i) {
  508. auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (i % pixels_per_byte));
  509. auto palette_index = (palette_indices[i / pixels_per_byte] >> bit_offset) & mask;
  510. auto& pixel = (Pixel&)context.bitmap->scanline(y)[i];
  511. if ((size_t)palette_index >= context.palette_data.size())
  512. return Error::from_string_literal("PNGImageDecoderPlugin: Palette index out of range");
  513. auto& color = context.palette_data.at(palette_index);
  514. auto transparency = context.palette_transparency_data.size() >= palette_index + 1u
  515. ? context.palette_transparency_data.data()[palette_index]
  516. : 0xff;
  517. pixel.r = color.r;
  518. pixel.g = color.g;
  519. pixel.b = color.b;
  520. pixel.a = transparency;
  521. }
  522. }
  523. } else {
  524. VERIFY_NOT_REACHED();
  525. }
  526. break;
  527. default:
  528. VERIFY_NOT_REACHED();
  529. break;
  530. }
  531. // Swap r and b values:
  532. for (int y = 0; y < context.height; ++y) {
  533. auto* pixels = (Pixel*)context.bitmap->scanline(y);
  534. for (int i = 0; i < context.bitmap->width(); ++i) {
  535. auto& x = pixels[i];
  536. swap(x.r, x.b);
  537. }
  538. }
  539. return {};
  540. }
  541. static bool decode_png_header(PNGLoadingContext& context)
  542. {
  543. if (context.state >= PNGLoadingContext::HeaderDecoded)
  544. return true;
  545. if (!context.data || context.data_size < sizeof(PNG::header)) {
  546. dbgln_if(PNG_DEBUG, "Missing PNG header");
  547. context.state = PNGLoadingContext::State::Error;
  548. return false;
  549. }
  550. if (memcmp(context.data, PNG::header.span().data(), sizeof(PNG::header)) != 0) {
  551. dbgln_if(PNG_DEBUG, "Invalid PNG header");
  552. context.state = PNGLoadingContext::State::Error;
  553. return false;
  554. }
  555. context.data_current_ptr = context.data + sizeof(PNG::header);
  556. context.state = PNGLoadingContext::HeaderDecoded;
  557. return true;
  558. }
  559. static bool decode_png_size(PNGLoadingContext& context)
  560. {
  561. if (context.state >= PNGLoadingContext::SizeDecoded)
  562. return true;
  563. if (context.state < PNGLoadingContext::HeaderDecoded) {
  564. if (!decode_png_header(context))
  565. return false;
  566. }
  567. size_t data_remaining = context.data_size - (context.data_current_ptr - context.data);
  568. Streamer streamer(context.data_current_ptr, data_remaining);
  569. while (!streamer.at_end() && !context.has_seen_iend) {
  570. if (!process_chunk(streamer, context)) {
  571. context.state = PNGLoadingContext::State::Error;
  572. return false;
  573. }
  574. context.data_current_ptr = streamer.current_data_ptr();
  575. if (context.width && context.height) {
  576. context.state = PNGLoadingContext::State::SizeDecoded;
  577. return true;
  578. }
  579. }
  580. return false;
  581. }
  582. static bool decode_png_image_data_chunk(PNGLoadingContext& context)
  583. {
  584. if (context.state >= PNGLoadingContext::ImageDataChunkDecoded)
  585. return true;
  586. if (context.state < PNGLoadingContext::SizeDecoded) {
  587. if (!decode_png_size(context))
  588. return false;
  589. }
  590. size_t data_remaining = context.data_size - (context.data_current_ptr - context.data);
  591. Streamer streamer(context.data_current_ptr, data_remaining);
  592. while (!streamer.at_end() && !context.has_seen_iend) {
  593. if (!process_chunk(streamer, context)) {
  594. context.state = PNGLoadingContext::State::Error;
  595. return false;
  596. }
  597. context.data_current_ptr = streamer.current_data_ptr();
  598. if (context.state >= PNGLoadingContext::State::ImageDataChunkDecoded)
  599. return true;
  600. }
  601. return false;
  602. }
  603. static bool decode_png_animation_data_chunks(PNGLoadingContext& context, u32 requested_animation_frame_index)
  604. {
  605. if (context.state >= PNGLoadingContext::ImageDataChunkDecoded) {
  606. if (context.last_completed_animation_frame_index.has_value()) {
  607. if (requested_animation_frame_index <= context.last_completed_animation_frame_index.value())
  608. return true;
  609. }
  610. } else if (!decode_png_image_data_chunk(context)) {
  611. return false;
  612. }
  613. size_t data_remaining = context.data_size - (context.data_current_ptr - context.data);
  614. Streamer streamer(context.data_current_ptr, data_remaining);
  615. while (!streamer.at_end() && !context.has_seen_iend) {
  616. if (!process_chunk(streamer, context)) {
  617. context.state = PNGLoadingContext::State::Error;
  618. return false;
  619. }
  620. context.data_current_ptr = streamer.current_data_ptr();
  621. if (context.last_completed_animation_frame_index.has_value()) {
  622. if (requested_animation_frame_index <= context.last_completed_animation_frame_index.value())
  623. break;
  624. }
  625. }
  626. if (!context.last_completed_animation_frame_index.has_value())
  627. return false;
  628. return requested_animation_frame_index <= context.last_completed_animation_frame_index.value();
  629. }
  630. static bool decode_png_chunks(PNGLoadingContext& context)
  631. {
  632. if (context.state >= PNGLoadingContext::State::ChunksDecoded)
  633. return true;
  634. if (context.state < PNGLoadingContext::HeaderDecoded) {
  635. if (!decode_png_header(context))
  636. return false;
  637. }
  638. size_t data_remaining = context.data_size - (context.data_current_ptr - context.data);
  639. context.compressed_data.ensure_capacity(context.data_size);
  640. Streamer streamer(context.data_current_ptr, data_remaining);
  641. while (!streamer.at_end() && !context.has_seen_iend) {
  642. if (!process_chunk(streamer, context)) {
  643. // Ignore failed chunk and just consider chunk decoding being done.
  644. // decode_png_bitmap() will check whether we got all required ones anyway.
  645. break;
  646. }
  647. context.data_current_ptr = streamer.current_data_ptr();
  648. }
  649. context.state = PNGLoadingContext::State::ChunksDecoded;
  650. return true;
  651. }
  652. static ErrorOr<void> decode_png_bitmap_simple(PNGLoadingContext& context, ByteBuffer& decompression_buffer)
  653. {
  654. Streamer streamer(decompression_buffer.data(), decompression_buffer.size());
  655. for (int y = 0; y < context.height; ++y) {
  656. PNG::FilterType filter;
  657. if (!streamer.read(filter)) {
  658. context.state = PNGLoadingContext::State::Error;
  659. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  660. }
  661. if (to_underlying(filter) > 4) {
  662. context.state = PNGLoadingContext::State::Error;
  663. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
  664. }
  665. context.scanlines.append({ filter });
  666. auto& scanline_buffer = context.scanlines.last().data;
  667. auto row_size = context.compute_row_size_for_width(context.width);
  668. if (row_size.has_overflow())
  669. return Error::from_string_literal("PNGImageDecoderPlugin: Row size overflow");
  670. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  671. context.state = PNGLoadingContext::State::Error;
  672. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  673. }
  674. }
  675. context.bitmap = TRY(Bitmap::create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height }));
  676. return unfilter(context);
  677. }
  678. static int adam7_height(PNGLoadingContext& context, int pass)
  679. {
  680. switch (pass) {
  681. case 1:
  682. return (context.height + 7) / 8;
  683. case 2:
  684. return (context.height + 7) / 8;
  685. case 3:
  686. return (context.height + 3) / 8;
  687. case 4:
  688. return (context.height + 3) / 4;
  689. case 5:
  690. return (context.height + 1) / 4;
  691. case 6:
  692. return (context.height + 1) / 2;
  693. case 7:
  694. return context.height / 2;
  695. default:
  696. VERIFY_NOT_REACHED();
  697. }
  698. }
  699. static int adam7_width(PNGLoadingContext& context, int pass)
  700. {
  701. switch (pass) {
  702. case 1:
  703. return (context.width + 7) / 8;
  704. case 2:
  705. return (context.width + 3) / 8;
  706. case 3:
  707. return (context.width + 3) / 4;
  708. case 4:
  709. return (context.width + 1) / 4;
  710. case 5:
  711. return (context.width + 1) / 2;
  712. case 6:
  713. return context.width / 2;
  714. case 7:
  715. return context.width;
  716. default:
  717. VERIFY_NOT_REACHED();
  718. }
  719. }
  720. // Index 0 unused (non-interlaced case)
  721. static int adam7_starty[8] = { 0, 0, 0, 4, 0, 2, 0, 1 };
  722. static int adam7_startx[8] = { 0, 0, 4, 0, 2, 0, 1, 0 };
  723. static int adam7_stepy[8] = { 1, 8, 8, 8, 4, 4, 2, 2 };
  724. static int adam7_stepx[8] = { 1, 8, 8, 4, 4, 2, 2, 1 };
  725. static ErrorOr<void> decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass)
  726. {
  727. auto subimage_context = context.create_subimage_context(adam7_width(context, pass), adam7_height(context, pass));
  728. // For small images, some passes might be empty
  729. if (!subimage_context.width || !subimage_context.height)
  730. return {};
  731. for (int y = 0; y < subimage_context.height; ++y) {
  732. PNG::FilterType filter;
  733. if (!streamer.read(filter)) {
  734. context.state = PNGLoadingContext::State::Error;
  735. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  736. }
  737. if (to_underlying(filter) > 4) {
  738. context.state = PNGLoadingContext::State::Error;
  739. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter");
  740. }
  741. subimage_context.scanlines.append({ filter });
  742. auto& scanline_buffer = subimage_context.scanlines.last().data;
  743. auto row_size = context.compute_row_size_for_width(subimage_context.width);
  744. if (row_size.has_overflow())
  745. return Error::from_string_literal("PNGImageDecoderPlugin: Row size overflow");
  746. if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) {
  747. context.state = PNGLoadingContext::State::Error;
  748. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  749. }
  750. }
  751. subimage_context.bitmap = TRY(Bitmap::create(context.bitmap->format(), { subimage_context.width, subimage_context.height }));
  752. TRY(unfilter(subimage_context));
  753. // Copy the subimage data into the main image according to the pass pattern
  754. for (int y = 0, dy = adam7_starty[pass]; y < subimage_context.height && dy < context.height; ++y, dy += adam7_stepy[pass]) {
  755. for (int x = 0, dx = adam7_startx[pass]; x < subimage_context.width && dy < context.width; ++x, dx += adam7_stepx[pass]) {
  756. context.bitmap->set_pixel(dx, dy, subimage_context.bitmap->get_pixel(x, y));
  757. }
  758. }
  759. return {};
  760. }
  761. static ErrorOr<void> decode_png_adam7(PNGLoadingContext& context, ByteBuffer& decompression_buffer)
  762. {
  763. Streamer streamer(decompression_buffer.data(), decompression_buffer.size());
  764. context.bitmap = TRY(Bitmap::create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height }));
  765. for (int pass = 1; pass <= 7; ++pass)
  766. TRY(decode_adam7_pass(context, streamer, pass));
  767. return {};
  768. }
  769. static ErrorOr<void> decode_png_bitmap(PNGLoadingContext& context)
  770. {
  771. if (context.state < PNGLoadingContext::State::ChunksDecoded) {
  772. if (!decode_png_chunks(context))
  773. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  774. }
  775. if (context.state >= PNGLoadingContext::State::BitmapDecoded)
  776. return {};
  777. if (context.width == -1 || context.height == -1)
  778. return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see an IHDR chunk.");
  779. if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty())
  780. return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty.");
  781. auto result = Compress::ZlibDecompressor::decompress_all(context.compressed_data.span());
  782. if (!result.has_value()) {
  783. context.state = PNGLoadingContext::State::Error;
  784. return Error::from_string_literal("PNGImageDecoderPlugin: Decompression failed");
  785. }
  786. auto& decompression_buffer = result.value();
  787. context.compressed_data.clear();
  788. context.scanlines.ensure_capacity(context.height);
  789. switch (context.interlace_method) {
  790. case PngInterlaceMethod::Null:
  791. TRY(decode_png_bitmap_simple(context, decompression_buffer));
  792. break;
  793. case PngInterlaceMethod::Adam7:
  794. TRY(decode_png_adam7(context, decompression_buffer));
  795. break;
  796. default:
  797. context.state = PNGLoadingContext::State::Error;
  798. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid interlace method");
  799. }
  800. context.state = PNGLoadingContext::State::BitmapDecoded;
  801. return {};
  802. }
  803. static ErrorOr<RefPtr<Bitmap>> decode_png_animation_frame_bitmap(PNGLoadingContext& context, AnimationFrame& animation_frame)
  804. {
  805. if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty())
  806. return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty.");
  807. VERIFY(!animation_frame.bitmap);
  808. auto frame_rect = animation_frame.rect();
  809. auto frame_context = context.create_subimage_context(frame_rect.width(), frame_rect.height());
  810. auto result = Compress::ZlibDecompressor::decompress_all(animation_frame.compressed_data.span());
  811. if (!result.has_value())
  812. return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of animation frame failed");
  813. auto& decompression_buffer = result.value();
  814. frame_context.compressed_data.clear();
  815. frame_context.scanlines.ensure_capacity(frame_context.height);
  816. switch (context.interlace_method) {
  817. case PngInterlaceMethod::Null:
  818. TRY(decode_png_bitmap_simple(frame_context, decompression_buffer));
  819. break;
  820. case PngInterlaceMethod::Adam7:
  821. TRY(decode_png_adam7(frame_context, decompression_buffer));
  822. break;
  823. default:
  824. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid interlace method");
  825. }
  826. context.state = PNGLoadingContext::State::BitmapDecoded;
  827. return move(frame_context.bitmap);
  828. }
  829. static bool is_valid_compression_method(u8 compression_method)
  830. {
  831. return compression_method == 0;
  832. }
  833. static bool is_valid_filter_method(u8 filter_method)
  834. {
  835. return filter_method == 0;
  836. }
  837. static bool process_IHDR(ReadonlyBytes data, PNGLoadingContext& context)
  838. {
  839. if (data.size() < (int)sizeof(PNG_IHDR))
  840. return false;
  841. auto& ihdr = *(const PNG_IHDR*)data.data();
  842. if (ihdr.width > maximum_width_for_decoded_images || ihdr.height > maximum_height_for_decoded_images) {
  843. dbgln("This PNG is too large for comfort: {}x{}", (u32)ihdr.width, (u32)ihdr.height);
  844. return false;
  845. }
  846. if (!is_valid_compression_method(ihdr.compression_method)) {
  847. dbgln("PNG has invalid compression method {}", ihdr.compression_method);
  848. return false;
  849. }
  850. if (!is_valid_filter_method(ihdr.filter_method)) {
  851. dbgln("PNG has invalid filter method {}", ihdr.filter_method);
  852. return false;
  853. }
  854. context.width = ihdr.width;
  855. context.height = ihdr.height;
  856. context.bit_depth = ihdr.bit_depth;
  857. context.color_type = ihdr.color_type;
  858. context.compression_method = ihdr.compression_method;
  859. context.filter_method = ihdr.filter_method;
  860. context.interlace_method = ihdr.interlace_method;
  861. dbgln_if(PNG_DEBUG, "PNG: {}x{} ({} bpp)", context.width, context.height, context.bit_depth);
  862. dbgln_if(PNG_DEBUG, " Color type: {}", to_underlying(context.color_type));
  863. dbgln_if(PNG_DEBUG, "Compress Method: {}", context.compression_method);
  864. dbgln_if(PNG_DEBUG, " Filter Method: {}", context.filter_method);
  865. dbgln_if(PNG_DEBUG, " Interlace type: {}", context.interlace_method);
  866. if (context.interlace_method != PngInterlaceMethod::Null && context.interlace_method != PngInterlaceMethod::Adam7) {
  867. dbgln_if(PNG_DEBUG, "PNGLoader::process_IHDR: unknown interlace method: {}", context.interlace_method);
  868. return false;
  869. }
  870. switch (context.color_type) {
  871. case PNG::ColorType::Greyscale:
  872. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8 && context.bit_depth != 16)
  873. return false;
  874. context.channels = 1;
  875. break;
  876. case PNG::ColorType::GreyscaleWithAlpha:
  877. if (context.bit_depth != 8 && context.bit_depth != 16)
  878. return false;
  879. context.channels = 2;
  880. break;
  881. case PNG::ColorType::Truecolor:
  882. if (context.bit_depth != 8 && context.bit_depth != 16)
  883. return false;
  884. context.channels = 3;
  885. break;
  886. case PNG::ColorType::IndexedColor:
  887. if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8)
  888. return false;
  889. context.channels = 1;
  890. break;
  891. case PNG::ColorType::TruecolorWithAlpha:
  892. if (context.bit_depth != 8 && context.bit_depth != 16)
  893. return false;
  894. context.channels = 4;
  895. break;
  896. default:
  897. return false;
  898. }
  899. return true;
  900. }
  901. static bool process_IDAT(ReadonlyBytes data, PNGLoadingContext& context)
  902. {
  903. context.compressed_data.append(data.data(), data.size());
  904. if (context.state < PNGLoadingContext::State::ImageDataChunkDecoded)
  905. context.state = PNGLoadingContext::State::ImageDataChunkDecoded;
  906. return true;
  907. }
  908. static bool process_PLTE(ReadonlyBytes data, PNGLoadingContext& context)
  909. {
  910. context.palette_data.append((PaletteEntry const*)data.data(), data.size() / 3);
  911. return true;
  912. }
  913. static bool process_tRNS(ReadonlyBytes data, PNGLoadingContext& context)
  914. {
  915. switch (context.color_type) {
  916. case PNG::ColorType::Greyscale:
  917. case PNG::ColorType::Truecolor:
  918. case PNG::ColorType::IndexedColor:
  919. context.palette_transparency_data.append(data.data(), data.size());
  920. break;
  921. default:
  922. break;
  923. }
  924. return true;
  925. }
  926. static bool process_cHRM(ReadonlyBytes data, PNGLoadingContext& context)
  927. {
  928. // https://www.w3.org/TR/png/#11cHRM
  929. if (data.size() != 32)
  930. return false;
  931. context.chromaticities_and_whitepoint = *bit_cast<ChromaticitiesAndWhitepoint* const>(data.data());
  932. return true;
  933. }
  934. static bool process_cICP(ReadonlyBytes data, PNGLoadingContext& context)
  935. {
  936. // https://www.w3.org/TR/png/#cICP-chunk
  937. if (data.size() != 4)
  938. return false;
  939. context.coding_independent_code_points = *bit_cast<CodingIndependentCodePoints* const>(data.data());
  940. return true;
  941. }
  942. static bool process_iCCP(ReadonlyBytes data, PNGLoadingContext& context)
  943. {
  944. // https://www.w3.org/TR/png/#11iCCP
  945. size_t profile_name_length_max = min(80u, data.size());
  946. size_t profile_name_length = strnlen((char const*)data.data(), profile_name_length_max);
  947. if (profile_name_length == 0 || profile_name_length == profile_name_length_max)
  948. return false;
  949. if (data.size() < profile_name_length + 2)
  950. return false;
  951. u8 compression_method = data[profile_name_length + 1];
  952. if (compression_method != 0)
  953. return false;
  954. context.embedded_icc_profile = EmbeddedICCProfile { { data.data(), profile_name_length }, data.slice(profile_name_length + 2) };
  955. return true;
  956. }
  957. static bool process_gAMA(ReadonlyBytes data, PNGLoadingContext& context)
  958. {
  959. // https://www.w3.org/TR/png/#11gAMA
  960. if (data.size() != 4)
  961. return false;
  962. u32 gamma = *bit_cast<NetworkOrdered<u32> const*>(data.data());
  963. if (gamma & 0x8000'0000)
  964. return false;
  965. context.gamma = gamma;
  966. return true;
  967. }
  968. static bool process_sRGB(ReadonlyBytes data, PNGLoadingContext& context)
  969. {
  970. // https://www.w3.org/TR/png/#srgb-standard-colour-space
  971. if (data.size() != 1)
  972. return false;
  973. u8 rendering_intent = data[0];
  974. if (rendering_intent > 3)
  975. return false;
  976. context.sRGB_rendering_intent = (RenderingIntent)rendering_intent;
  977. return true;
  978. }
  979. static bool process_acTL(ReadonlyBytes data, PNGLoadingContext& context)
  980. {
  981. // https://www.w3.org/TR/png/#acTL-chunk
  982. if (context.has_seen_idat_chunk)
  983. return true; // Ignore if we encounter it after the first idat
  984. if (data.size() != sizeof(acTL_Chunk))
  985. return false;
  986. auto const& acTL = *bit_cast<acTL_Chunk* const>(data.data());
  987. context.animation_frame_count = acTL.num_frames;
  988. context.animation_loop_count = acTL.num_plays;
  989. context.has_seen_actl_chunk_before_idat = true;
  990. context.animation_frames.ensure_capacity(context.animation_frame_count);
  991. return true;
  992. }
  993. static bool process_fcTL(ReadonlyBytes data, PNGLoadingContext& context)
  994. {
  995. // https://www.w3.org/TR/png/#fcTL-chunk
  996. if (!context.has_seen_actl_chunk_before_idat)
  997. return true; // Ignore if it's not a valid animated png
  998. if (data.size() != sizeof(fcTL_Chunk))
  999. return false;
  1000. auto const& fcTL = *bit_cast<fcTL_Chunk* const>(data.data());
  1001. if (fcTL.sequence_number != context.animation_next_expected_seq)
  1002. return false;
  1003. context.animation_next_expected_seq++;
  1004. if (fcTL.width == 0 || fcTL.height == 0)
  1005. return false;
  1006. Checked<int> left { static_cast<int>(fcTL.x_offset) };
  1007. Checked<int> top { static_cast<int>(fcTL.y_offset) };
  1008. Checked<int> width { static_cast<int>(fcTL.width) };
  1009. Checked<int> height { static_cast<int>(fcTL.height) };
  1010. auto right = left + width;
  1011. auto bottom = top + height;
  1012. if (left < 0 || width <= 0 || right.has_overflow() || right > context.width)
  1013. return false;
  1014. if (top < 0 || height <= 0 || bottom.has_overflow() || bottom > context.height)
  1015. return false;
  1016. bool is_first_animation_frame = context.animation_frames.is_empty();
  1017. if (!is_first_animation_frame)
  1018. context.last_completed_animation_frame_index = context.animation_frames.size() - 1;
  1019. context.animation_frames.append({ fcTL });
  1020. if (!context.has_seen_idat_chunk && is_first_animation_frame)
  1021. context.is_first_idat_part_of_animation = true;
  1022. return true;
  1023. }
  1024. static bool process_fdAT(ReadonlyBytes data, PNGLoadingContext& context)
  1025. {
  1026. // https://www.w3.org/TR/png/#fdAT-chunk
  1027. if (data.size() <= 4)
  1028. return false;
  1029. u32 sequence_number = *bit_cast<NetworkOrdered<u32> const*>(data.data());
  1030. if (sequence_number != context.animation_next_expected_seq)
  1031. return false;
  1032. context.animation_next_expected_seq++;
  1033. if (context.animation_frames.is_empty())
  1034. return false;
  1035. auto& current_animation_frame = context.animation_frames[context.animation_frames.size() - 1];
  1036. auto compressed_data = data.slice(4);
  1037. current_animation_frame.compressed_data.append(compressed_data.data(), compressed_data.size());
  1038. return true;
  1039. }
  1040. static bool process_IEND(ReadonlyBytes, PNGLoadingContext& context)
  1041. {
  1042. // https://www.w3.org/TR/png/#11IEND
  1043. if (context.has_seen_actl_chunk_before_idat)
  1044. context.last_completed_animation_frame_index = context.animation_frames.size();
  1045. context.has_seen_iend = true;
  1046. return true;
  1047. }
  1048. static bool process_chunk(Streamer& streamer, PNGLoadingContext& context)
  1049. {
  1050. u32 chunk_size;
  1051. if (!streamer.read(chunk_size)) {
  1052. dbgln_if(PNG_DEBUG, "Bail at chunk_size");
  1053. return false;
  1054. }
  1055. u8 chunk_type[5];
  1056. chunk_type[4] = '\0';
  1057. if (!streamer.read_bytes(chunk_type, 4)) {
  1058. dbgln_if(PNG_DEBUG, "Bail at chunk_type");
  1059. return false;
  1060. }
  1061. ReadonlyBytes chunk_data;
  1062. if (!streamer.wrap_bytes(chunk_data, chunk_size)) {
  1063. dbgln_if(PNG_DEBUG, "Bail at chunk_data");
  1064. return false;
  1065. }
  1066. u32 chunk_crc;
  1067. if (!streamer.read(chunk_crc)) {
  1068. dbgln_if(PNG_DEBUG, "Bail at chunk_crc");
  1069. return false;
  1070. }
  1071. dbgln_if(PNG_DEBUG, "Chunk type: '{}', size: {}, crc: {:x}", chunk_type, chunk_size, chunk_crc);
  1072. if (!strcmp((char const*)chunk_type, "IHDR"))
  1073. return process_IHDR(chunk_data, context);
  1074. if (!strcmp((char const*)chunk_type, "IDAT"))
  1075. return process_IDAT(chunk_data, context);
  1076. if (!strcmp((char const*)chunk_type, "PLTE"))
  1077. return process_PLTE(chunk_data, context);
  1078. if (!strcmp((char const*)chunk_type, "cHRM"))
  1079. return process_cHRM(chunk_data, context);
  1080. if (!strcmp((char const*)chunk_type, "cICP"))
  1081. return process_cICP(chunk_data, context);
  1082. if (!strcmp((char const*)chunk_type, "iCCP"))
  1083. return process_iCCP(chunk_data, context);
  1084. if (!strcmp((char const*)chunk_type, "gAMA"))
  1085. return process_gAMA(chunk_data, context);
  1086. if (!strcmp((char const*)chunk_type, "sRGB"))
  1087. return process_sRGB(chunk_data, context);
  1088. if (!strcmp((char const*)chunk_type, "tRNS"))
  1089. return process_tRNS(chunk_data, context);
  1090. if (!strcmp((char const*)chunk_type, "acTL"))
  1091. return process_acTL(chunk_data, context);
  1092. if (!strcmp((char const*)chunk_type, "fcTL"))
  1093. return process_fcTL(chunk_data, context);
  1094. if (!strcmp((char const*)chunk_type, "fdAT"))
  1095. return process_fdAT(chunk_data, context);
  1096. if (!strcmp((char const*)chunk_type, "IEND"))
  1097. return process_IEND(chunk_data, context);
  1098. return true;
  1099. }
  1100. PNGImageDecoderPlugin::PNGImageDecoderPlugin(u8 const* data, size_t size)
  1101. {
  1102. m_context = make<PNGLoadingContext>();
  1103. m_context->data = m_context->data_current_ptr = data;
  1104. m_context->data_size = size;
  1105. }
  1106. PNGImageDecoderPlugin::~PNGImageDecoderPlugin() = default;
  1107. bool PNGImageDecoderPlugin::ensure_image_data_chunk_was_decoded()
  1108. {
  1109. if (m_context->state == PNGLoadingContext::State::Error)
  1110. return false;
  1111. if (m_context->state < PNGLoadingContext::State::ImageDataChunkDecoded) {
  1112. if (!decode_png_image_data_chunk(*m_context))
  1113. return false;
  1114. }
  1115. return true;
  1116. }
  1117. bool PNGImageDecoderPlugin::ensure_animation_frame_was_decoded(u32 animation_frame_index)
  1118. {
  1119. if (m_context->state == PNGLoadingContext::State::Error)
  1120. return false;
  1121. if (m_context->state < PNGLoadingContext::State::ImageDataChunkDecoded) {
  1122. if (!decode_png_image_data_chunk(*m_context))
  1123. return false;
  1124. }
  1125. if (m_context->last_completed_animation_frame_index.has_value()) {
  1126. if (m_context->last_completed_animation_frame_index.value() >= animation_frame_index)
  1127. return true;
  1128. }
  1129. return decode_png_animation_data_chunks(*m_context, animation_frame_index);
  1130. }
  1131. IntSize PNGImageDecoderPlugin::size()
  1132. {
  1133. if (m_context->state == PNGLoadingContext::State::Error)
  1134. return {};
  1135. if (m_context->state < PNGLoadingContext::State::SizeDecoded) {
  1136. bool success = decode_png_size(*m_context);
  1137. if (!success)
  1138. return {};
  1139. }
  1140. return { m_context->width, m_context->height };
  1141. }
  1142. void PNGImageDecoderPlugin::set_volatile()
  1143. {
  1144. if (m_context->bitmap)
  1145. m_context->bitmap->set_volatile();
  1146. }
  1147. bool PNGImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  1148. {
  1149. if (!m_context->bitmap)
  1150. return false;
  1151. return m_context->bitmap->set_nonvolatile(was_purged);
  1152. }
  1153. bool PNGImageDecoderPlugin::initialize()
  1154. {
  1155. return decode_png_header(*m_context);
  1156. }
  1157. bool PNGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1158. {
  1159. PNGLoadingContext context;
  1160. context.data = context.data_current_ptr = data.data();
  1161. context.data_size = data.size();
  1162. return decode_png_header(context);
  1163. }
  1164. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(ReadonlyBytes data)
  1165. {
  1166. return adopt_nonnull_own_or_enomem(new (nothrow) PNGImageDecoderPlugin(data.data(), data.size()));
  1167. }
  1168. bool PNGImageDecoderPlugin::is_animated()
  1169. {
  1170. if (!ensure_image_data_chunk_was_decoded())
  1171. return false;
  1172. return m_context->has_seen_actl_chunk_before_idat;
  1173. }
  1174. size_t PNGImageDecoderPlugin::loop_count()
  1175. {
  1176. if (!ensure_image_data_chunk_was_decoded())
  1177. return 0;
  1178. return m_context->animation_loop_count;
  1179. }
  1180. size_t PNGImageDecoderPlugin::frame_count()
  1181. {
  1182. if (!ensure_image_data_chunk_was_decoded())
  1183. return 0;
  1184. if (!m_context->has_seen_actl_chunk_before_idat)
  1185. return 1;
  1186. auto total_frames = m_context->animation_frame_count;
  1187. if (!m_context->is_first_idat_part_of_animation)
  1188. total_frames++;
  1189. return total_frames;
  1190. }
  1191. size_t PNGImageDecoderPlugin::first_animated_frame_index()
  1192. {
  1193. if (!ensure_image_data_chunk_was_decoded())
  1194. return 0;
  1195. if (!m_context->has_seen_actl_chunk_before_idat)
  1196. return 0;
  1197. return m_context->is_first_idat_part_of_animation ? 0 : 1;
  1198. }
  1199. static ErrorOr<RefPtr<Bitmap>> render_animation_frame(AnimationFrame const& prev_animation_frame, AnimationFrame& animation_frame, Bitmap const& decoded_frame_bitmap)
  1200. {
  1201. auto rendered_bitmap = TRY(prev_animation_frame.bitmap->clone());
  1202. Painter painter(rendered_bitmap);
  1203. static constexpr Color transparent_black = { 0, 0, 0, 0 };
  1204. auto frame_rect = animation_frame.rect();
  1205. switch (prev_animation_frame.fcTL.dispose_op) {
  1206. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_NONE:
  1207. break;
  1208. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_BACKGROUND:
  1209. painter.clear_rect(rendered_bitmap->rect(), transparent_black);
  1210. break;
  1211. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_PREVIOUS: {
  1212. painter.blit(frame_rect.location(), *prev_animation_frame.bitmap, frame_rect, 1.0f, false);
  1213. break;
  1214. }
  1215. }
  1216. switch (animation_frame.fcTL.blend_op) {
  1217. case fcTL_Chunk::BlendOp::APNG_BLEND_OP_SOURCE:
  1218. painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, false);
  1219. break;
  1220. case fcTL_Chunk::BlendOp::APNG_BLEND_OP_OVER:
  1221. painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, true);
  1222. break;
  1223. }
  1224. return rendered_bitmap;
  1225. }
  1226. ErrorOr<ImageFrameDescriptor> PNGImageDecoderPlugin::frame(size_t index)
  1227. {
  1228. if (m_context->state == PNGLoadingContext::State::Error)
  1229. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  1230. if (!ensure_image_data_chunk_was_decoded())
  1231. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk");
  1232. auto set_descriptor_duration = [](ImageFrameDescriptor& descriptor, AnimationFrame const& animation_frame) {
  1233. descriptor.duration = static_cast<int>(animation_frame.duration_ms());
  1234. if (descriptor.duration < 0)
  1235. descriptor.duration = NumericLimits<int>::min();
  1236. };
  1237. auto load_default_image = [&]() -> ErrorOr<void> {
  1238. if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
  1239. // NOTE: This forces the chunk decoding to happen.
  1240. TRY(decode_png_bitmap(*m_context));
  1241. }
  1242. VERIFY(m_context->bitmap);
  1243. return {};
  1244. };
  1245. if (index == 0) {
  1246. TRY(load_default_image());
  1247. ImageFrameDescriptor descriptor { m_context->bitmap };
  1248. if (m_context->has_seen_actl_chunk_before_idat && m_context->is_first_idat_part_of_animation)
  1249. set_descriptor_duration(descriptor, m_context->animation_frames[0]);
  1250. return descriptor;
  1251. }
  1252. if (!m_context->has_seen_actl_chunk_before_idat)
  1253. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid frame index");
  1254. if (!ensure_animation_frame_was_decoded(index))
  1255. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk");
  1256. if (index >= m_context->animation_frames.size())
  1257. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid animation frame index");
  1258. // We need to assemble each frame up until the one requested,
  1259. // so decode all bitmaps that haven't been decoded yet.
  1260. for (size_t i = m_context->animation_next_frame_to_render; i <= index; i++) {
  1261. if (i == 0) {
  1262. // If the default image hasn't been loaded, load it now
  1263. TRY(load_default_image()); // May modify animation_frames!
  1264. auto& animation_frame = m_context->animation_frames[i];
  1265. animation_frame.bitmap = m_context->bitmap;
  1266. } else {
  1267. auto& animation_frame = m_context->animation_frames[i];
  1268. VERIFY(!animation_frame.bitmap);
  1269. auto decoded_bitmap = TRY(decode_png_animation_frame_bitmap(*m_context, animation_frame));
  1270. auto prev_animation_frame = m_context->animation_frames[i - 1];
  1271. animation_frame.bitmap = TRY(render_animation_frame(prev_animation_frame, animation_frame, *decoded_bitmap));
  1272. }
  1273. m_context->animation_next_frame_to_render = i + 1;
  1274. }
  1275. auto const& animation_frame = m_context->animation_frames[index];
  1276. VERIFY(animation_frame.bitmap);
  1277. ImageFrameDescriptor descriptor { animation_frame.bitmap };
  1278. set_descriptor_duration(descriptor, animation_frame);
  1279. return descriptor;
  1280. }
  1281. ErrorOr<Optional<ReadonlyBytes>> PNGImageDecoderPlugin::icc_data()
  1282. {
  1283. if (!decode_png_chunks(*m_context))
  1284. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding chunks failed");
  1285. if (m_context->embedded_icc_profile.has_value()) {
  1286. if (!m_context->decompressed_icc_profile.has_value()) {
  1287. auto result = Compress::ZlibDecompressor::decompress_all(m_context->embedded_icc_profile->compressed_data);
  1288. if (!result.has_value()) {
  1289. m_context->embedded_icc_profile.clear();
  1290. return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of ICC profile failed");
  1291. }
  1292. m_context->decompressed_icc_profile = move(*result);
  1293. }
  1294. return m_context->decompressed_icc_profile.value();
  1295. }
  1296. // FIXME: Eventually, look at coding_independent_code_points, chromaticities_and_whitepoint, gamma, sRGB_rendering_intent too.
  1297. // The order is:
  1298. // 1. Use coding_independent_code_points if it exists, ignore the rest.
  1299. // 2. Use embedded_icc_profile if it exists, ignore the rest.
  1300. // 3. Use sRGB_rendering_intent if it exists, ignore the rest.
  1301. // 4. Use gamma to adjust gamma and chromaticities_and_whitepoint to adjust color.
  1302. // (Order between 2 and 3 isn't fully clear, but "It is recommended that the sRGB and iCCP chunks do not appear simultaneously in a PNG datastream."
  1303. return OptionalNone {};
  1304. }
  1305. }