PNGLoader.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. ErrorOr<void> PNGImageDecoderPlugin::initialize()
  1154. {
  1155. if (decode_png_header(*m_context))
  1156. return {};
  1157. return Error::from_string_literal("bad image header");
  1158. }
  1159. bool PNGImageDecoderPlugin::sniff(ReadonlyBytes data)
  1160. {
  1161. PNGLoadingContext context;
  1162. context.data = context.data_current_ptr = data.data();
  1163. context.data_size = data.size();
  1164. return decode_png_header(context);
  1165. }
  1166. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(ReadonlyBytes data)
  1167. {
  1168. return adopt_nonnull_own_or_enomem(new (nothrow) PNGImageDecoderPlugin(data.data(), data.size()));
  1169. }
  1170. bool PNGImageDecoderPlugin::is_animated()
  1171. {
  1172. if (!ensure_image_data_chunk_was_decoded())
  1173. return false;
  1174. return m_context->has_seen_actl_chunk_before_idat;
  1175. }
  1176. size_t PNGImageDecoderPlugin::loop_count()
  1177. {
  1178. if (!ensure_image_data_chunk_was_decoded())
  1179. return 0;
  1180. return m_context->animation_loop_count;
  1181. }
  1182. size_t PNGImageDecoderPlugin::frame_count()
  1183. {
  1184. if (!ensure_image_data_chunk_was_decoded())
  1185. return 0;
  1186. if (!m_context->has_seen_actl_chunk_before_idat)
  1187. return 1;
  1188. auto total_frames = m_context->animation_frame_count;
  1189. if (!m_context->is_first_idat_part_of_animation)
  1190. total_frames++;
  1191. return total_frames;
  1192. }
  1193. size_t PNGImageDecoderPlugin::first_animated_frame_index()
  1194. {
  1195. if (!ensure_image_data_chunk_was_decoded())
  1196. return 0;
  1197. if (!m_context->has_seen_actl_chunk_before_idat)
  1198. return 0;
  1199. return m_context->is_first_idat_part_of_animation ? 0 : 1;
  1200. }
  1201. static ErrorOr<RefPtr<Bitmap>> render_animation_frame(AnimationFrame const& prev_animation_frame, AnimationFrame& animation_frame, Bitmap const& decoded_frame_bitmap)
  1202. {
  1203. auto rendered_bitmap = TRY(prev_animation_frame.bitmap->clone());
  1204. Painter painter(rendered_bitmap);
  1205. static constexpr Color transparent_black = { 0, 0, 0, 0 };
  1206. auto frame_rect = animation_frame.rect();
  1207. switch (prev_animation_frame.fcTL.dispose_op) {
  1208. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_NONE:
  1209. break;
  1210. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_BACKGROUND:
  1211. painter.clear_rect(rendered_bitmap->rect(), transparent_black);
  1212. break;
  1213. case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_PREVIOUS: {
  1214. painter.blit(frame_rect.location(), *prev_animation_frame.bitmap, frame_rect, 1.0f, false);
  1215. break;
  1216. }
  1217. }
  1218. switch (animation_frame.fcTL.blend_op) {
  1219. case fcTL_Chunk::BlendOp::APNG_BLEND_OP_SOURCE:
  1220. painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, false);
  1221. break;
  1222. case fcTL_Chunk::BlendOp::APNG_BLEND_OP_OVER:
  1223. painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, true);
  1224. break;
  1225. }
  1226. return rendered_bitmap;
  1227. }
  1228. ErrorOr<ImageFrameDescriptor> PNGImageDecoderPlugin::frame(size_t index)
  1229. {
  1230. if (m_context->state == PNGLoadingContext::State::Error)
  1231. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed");
  1232. if (!ensure_image_data_chunk_was_decoded())
  1233. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk");
  1234. auto set_descriptor_duration = [](ImageFrameDescriptor& descriptor, AnimationFrame const& animation_frame) {
  1235. descriptor.duration = static_cast<int>(animation_frame.duration_ms());
  1236. if (descriptor.duration < 0)
  1237. descriptor.duration = NumericLimits<int>::min();
  1238. };
  1239. auto load_default_image = [&]() -> ErrorOr<void> {
  1240. if (m_context->state < PNGLoadingContext::State::BitmapDecoded) {
  1241. // NOTE: This forces the chunk decoding to happen.
  1242. TRY(decode_png_bitmap(*m_context));
  1243. }
  1244. VERIFY(m_context->bitmap);
  1245. return {};
  1246. };
  1247. if (index == 0) {
  1248. TRY(load_default_image());
  1249. ImageFrameDescriptor descriptor { m_context->bitmap };
  1250. if (m_context->has_seen_actl_chunk_before_idat && m_context->is_first_idat_part_of_animation)
  1251. set_descriptor_duration(descriptor, m_context->animation_frames[0]);
  1252. return descriptor;
  1253. }
  1254. if (!m_context->has_seen_actl_chunk_before_idat)
  1255. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid frame index");
  1256. if (!ensure_animation_frame_was_decoded(index))
  1257. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk");
  1258. if (index >= m_context->animation_frames.size())
  1259. return Error::from_string_literal("PNGImageDecoderPlugin: Invalid animation frame index");
  1260. // We need to assemble each frame up until the one requested,
  1261. // so decode all bitmaps that haven't been decoded yet.
  1262. for (size_t i = m_context->animation_next_frame_to_render; i <= index; i++) {
  1263. if (i == 0) {
  1264. // If the default image hasn't been loaded, load it now
  1265. TRY(load_default_image()); // May modify animation_frames!
  1266. auto& animation_frame = m_context->animation_frames[i];
  1267. animation_frame.bitmap = m_context->bitmap;
  1268. } else {
  1269. auto& animation_frame = m_context->animation_frames[i];
  1270. VERIFY(!animation_frame.bitmap);
  1271. auto decoded_bitmap = TRY(decode_png_animation_frame_bitmap(*m_context, animation_frame));
  1272. auto prev_animation_frame = m_context->animation_frames[i - 1];
  1273. animation_frame.bitmap = TRY(render_animation_frame(prev_animation_frame, animation_frame, *decoded_bitmap));
  1274. }
  1275. m_context->animation_next_frame_to_render = i + 1;
  1276. }
  1277. auto const& animation_frame = m_context->animation_frames[index];
  1278. VERIFY(animation_frame.bitmap);
  1279. ImageFrameDescriptor descriptor { animation_frame.bitmap };
  1280. set_descriptor_duration(descriptor, animation_frame);
  1281. return descriptor;
  1282. }
  1283. ErrorOr<Optional<ReadonlyBytes>> PNGImageDecoderPlugin::icc_data()
  1284. {
  1285. if (!decode_png_chunks(*m_context))
  1286. return Error::from_string_literal("PNGImageDecoderPlugin: Decoding chunks failed");
  1287. if (m_context->embedded_icc_profile.has_value()) {
  1288. if (!m_context->decompressed_icc_profile.has_value()) {
  1289. auto result = Compress::ZlibDecompressor::decompress_all(m_context->embedded_icc_profile->compressed_data);
  1290. if (!result.has_value()) {
  1291. m_context->embedded_icc_profile.clear();
  1292. return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of ICC profile failed");
  1293. }
  1294. m_context->decompressed_icc_profile = move(*result);
  1295. }
  1296. return m_context->decompressed_icc_profile.value();
  1297. }
  1298. // FIXME: Eventually, look at coding_independent_code_points, chromaticities_and_whitepoint, gamma, sRGB_rendering_intent too.
  1299. // The order is:
  1300. // 1. Use coding_independent_code_points if it exists, ignore the rest.
  1301. // 2. Use embedded_icc_profile if it exists, ignore the rest.
  1302. // 3. Use sRGB_rendering_intent if it exists, ignore the rest.
  1303. // 4. Use gamma to adjust gamma and chromaticities_and_whitepoint to adjust color.
  1304. // (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."
  1305. return OptionalNone {};
  1306. }
  1307. }