PNGLoader.cpp 54 KB

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