| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-13 16:09:41 -06:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-16 15:51:56 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-25 15:11:15 +02:00
										 |  |  | #include <AK/Endian.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-11 07:30:37 -05:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-28 15:33:37 +01:00
										 |  |  | #include <LibCompress/Zlib.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:58:06 -04:00
										 |  |  | #include <LibGfx/ImageFormats/PNGLoader.h>
 | 
					
						
							|  |  |  | #include <LibGfx/ImageFormats/PNGShared.h>
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | struct PNG_IHDR { | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     NetworkOrdered<u32> width; | 
					
						
							|  |  |  |     NetworkOrdered<u32> height; | 
					
						
							|  |  |  |     u8 bit_depth { 0 }; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     PNG::ColorType color_type { 0 }; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 compression_method { 0 }; | 
					
						
							|  |  |  |     u8 filter_method { 0 }; | 
					
						
							|  |  |  |     u8 interlace_method { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 01:00:46 -07:00
										 |  |  | static_assert(AssertSize<PNG_IHDR, 13>()); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | struct acTL_Chunk { | 
					
						
							|  |  |  |     NetworkOrdered<u32> num_frames; | 
					
						
							|  |  |  |     NetworkOrdered<u32> num_plays; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | static_assert(AssertSize<acTL_Chunk, 8>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct fcTL_Chunk { | 
					
						
							|  |  |  |     enum class DisposeOp : u8 { | 
					
						
							|  |  |  |         APNG_DISPOSE_OP_NONE = 0, | 
					
						
							|  |  |  |         APNG_DISPOSE_OP_BACKGROUND, | 
					
						
							|  |  |  |         APNG_DISPOSE_OP_PREVIOUS | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     enum class BlendOp : u8 { | 
					
						
							|  |  |  |         APNG_BLEND_OP_SOURCE = 0, | 
					
						
							|  |  |  |         APNG_BLEND_OP_OVER | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     NetworkOrdered<u32> sequence_number; | 
					
						
							|  |  |  |     NetworkOrdered<u32> width; | 
					
						
							|  |  |  |     NetworkOrdered<u32> height; | 
					
						
							|  |  |  |     NetworkOrdered<u32> x_offset; | 
					
						
							|  |  |  |     NetworkOrdered<u32> y_offset; | 
					
						
							|  |  |  |     NetworkOrdered<u16> delay_num; | 
					
						
							|  |  |  |     NetworkOrdered<u16> delay_den; | 
					
						
							|  |  |  |     DisposeOp dispose_op { DisposeOp::APNG_DISPOSE_OP_NONE }; | 
					
						
							|  |  |  |     BlendOp blend_op { BlendOp::APNG_BLEND_OP_SOURCE }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | static_assert(AssertSize<fcTL_Chunk, 26>()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  | struct ChromaticitiesAndWhitepoint { | 
					
						
							|  |  |  |     NetworkOrdered<u32> white_point_x; | 
					
						
							|  |  |  |     NetworkOrdered<u32> white_point_y; | 
					
						
							|  |  |  |     NetworkOrdered<u32> red_x; | 
					
						
							|  |  |  |     NetworkOrdered<u32> red_y; | 
					
						
							|  |  |  |     NetworkOrdered<u32> green_x; | 
					
						
							|  |  |  |     NetworkOrdered<u32> green_y; | 
					
						
							|  |  |  |     NetworkOrdered<u32> blue_x; | 
					
						
							|  |  |  |     NetworkOrdered<u32> blue_y; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | static_assert(AssertSize<ChromaticitiesAndWhitepoint, 32>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct CodingIndependentCodePoints { | 
					
						
							|  |  |  |     u8 color_primaries; | 
					
						
							|  |  |  |     u8 transfer_function; | 
					
						
							|  |  |  |     u8 matrix_coefficients; | 
					
						
							|  |  |  |     u8 video_full_range_flag; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | static_assert(AssertSize<CodingIndependentCodePoints, 4>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct EmbeddedICCProfile { | 
					
						
							|  |  |  |     StringView profile_name; | 
					
						
							|  |  |  |     ReadonlyBytes compressed_data; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | struct Scanline { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     PNG::FilterType filter; | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |     ReadonlyBytes data {}; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 22:44:54 +01:00
										 |  |  | struct [[gnu::packed]] PaletteEntry { | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     u8 r; | 
					
						
							|  |  |  |     u8 g; | 
					
						
							|  |  |  |     u8 b; | 
					
						
							| 
									
										
										
										
											2021-11-06 19:30:59 +01:00
										 |  |  |     // u8 a;
 | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  | template<typename T> | 
					
						
							| 
									
										
										
										
											2020-12-30 22:44:54 +01:00
										 |  |  | struct [[gnu::packed]] Tuple { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  |     T gray; | 
					
						
							|  |  |  |     T a; | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  | template<typename T> | 
					
						
							| 
									
										
										
										
											2020-12-30 22:44:54 +01:00
										 |  |  | struct [[gnu::packed]] Triplet { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  |     T r; | 
					
						
							|  |  |  |     T g; | 
					
						
							|  |  |  |     T b; | 
					
						
							| 
									
										
										
										
											2022-02-14 17:55:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool operator==(Triplet const& other) const = default; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  | template<typename T> | 
					
						
							| 
									
										
										
										
											2022-04-07 20:42:09 +02:00
										 |  |  | struct [[gnu::packed]] Quartet { | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  |     T r; | 
					
						
							|  |  |  |     T g; | 
					
						
							|  |  |  |     T b; | 
					
						
							|  |  |  |     T a; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | enum PngInterlaceMethod { | 
					
						
							|  |  |  |     Null = 0, | 
					
						
							|  |  |  |     Adam7 = 1 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  | enum RenderingIntent { | 
					
						
							|  |  |  |     Perceptual = 0, | 
					
						
							|  |  |  |     RelativeColorimetric = 1, | 
					
						
							|  |  |  |     Saturation = 2, | 
					
						
							|  |  |  |     AbsoluteColorimetric = 3, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | struct AnimationFrame { | 
					
						
							|  |  |  |     fcTL_Chunk const& fcTL; | 
					
						
							|  |  |  |     RefPtr<Bitmap> bitmap; | 
					
						
							|  |  |  |     Vector<u8> compressed_data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     AnimationFrame(fcTL_Chunk const& fcTL) | 
					
						
							|  |  |  |         : fcTL(fcTL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u32 duration_ms() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         u32 num = fcTL.delay_num; | 
					
						
							|  |  |  |         if (num == 0) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         u32 denom = fcTL.delay_den != 0 ? static_cast<u32>(fcTL.delay_den) : 100u; | 
					
						
							|  |  |  |         return (num * 1000) / denom; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IntRect rect() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return { fcTL.x_offset, fcTL.y_offset, fcTL.width, fcTL.height }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | struct PNGLoadingContext { | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     enum State { | 
					
						
							|  |  |  |         NotDecoded = 0, | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         Error, | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |         HeaderDecoded, | 
					
						
							|  |  |  |         SizeDecoded, | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |         ImageDataChunkDecoded, | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |         ChunksDecoded, | 
					
						
							|  |  |  |         BitmapDecoded, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     State state { State::NotDecoded }; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     u8 const* data { nullptr }; | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     u8 const* data_current_ptr { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     size_t data_size { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     int width { -1 }; | 
					
						
							|  |  |  |     int height { -1 }; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 bit_depth { 0 }; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     PNG::ColorType color_type { 0 }; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 compression_method { 0 }; | 
					
						
							|  |  |  |     u8 filter_method { 0 }; | 
					
						
							|  |  |  |     u8 interlace_method { 0 }; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |     u8 channels { 0 }; | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     u32 animation_next_expected_seq { 0 }; | 
					
						
							|  |  |  |     u32 animation_next_frame_to_render { 0 }; | 
					
						
							|  |  |  |     u32 animation_frame_count { 0 }; | 
					
						
							|  |  |  |     u32 animation_loop_count { 0 }; | 
					
						
							|  |  |  |     Optional<u32> last_completed_animation_frame_index; | 
					
						
							|  |  |  |     bool is_first_idat_part_of_animation { false }; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     bool has_seen_zlib_header { false }; | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     bool has_seen_iend { false }; | 
					
						
							|  |  |  |     bool has_seen_idat_chunk { false }; | 
					
						
							|  |  |  |     bool has_seen_actl_chunk_before_idat { false }; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     bool has_alpha() const { return to_underlying(color_type) & 4 || palette_transparency_data.size() > 0; } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     Vector<Scanline> scanlines; | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |     ByteBuffer unfiltered_data; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap> bitmap; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     Vector<u8> compressed_data; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     Vector<PaletteEntry> palette_data; | 
					
						
							|  |  |  |     Vector<u8> palette_transparency_data; | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     Vector<AnimationFrame> animation_frames; | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  |     Optional<ChromaticitiesAndWhitepoint> chromaticities_and_whitepoint; | 
					
						
							|  |  |  |     Optional<CodingIndependentCodePoints> coding_independent_code_points; | 
					
						
							|  |  |  |     Optional<u32> gamma; | 
					
						
							|  |  |  |     Optional<EmbeddedICCProfile> embedded_icc_profile; | 
					
						
							| 
									
										
										
										
											2023-01-27 20:32:47 -05:00
										 |  |  |     Optional<ByteBuffer> decompressed_icc_profile; | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  |     Optional<RenderingIntent> sRGB_rendering_intent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  |     Checked<int> compute_row_size_for_width(int width) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Checked<int> row_size = width; | 
					
						
							|  |  |  |         row_size *= channels; | 
					
						
							|  |  |  |         row_size *= bit_depth; | 
					
						
							|  |  |  |         row_size += 7; | 
					
						
							|  |  |  |         row_size /= 8; | 
					
						
							|  |  |  |         if (row_size.has_overflow()) { | 
					
						
							|  |  |  |             dbgln("PNG too large, integer overflow while computing row size"); | 
					
						
							|  |  |  |             state = State::Error; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return row_size; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PNGLoadingContext create_subimage_context(int width, int height) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PNGLoadingContext subimage_context; | 
					
						
							|  |  |  |         subimage_context.state = State::ChunksDecoded; | 
					
						
							|  |  |  |         subimage_context.width = width; | 
					
						
							|  |  |  |         subimage_context.height = height; | 
					
						
							|  |  |  |         subimage_context.channels = channels; | 
					
						
							|  |  |  |         subimage_context.color_type = color_type; | 
					
						
							|  |  |  |         subimage_context.palette_data = palette_data; | 
					
						
							|  |  |  |         subimage_context.palette_transparency_data = palette_transparency_data; | 
					
						
							|  |  |  |         subimage_context.bit_depth = bit_depth; | 
					
						
							|  |  |  |         subimage_context.filter_method = filter_method; | 
					
						
							|  |  |  |         return subimage_context; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Streamer { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     Streamer(u8 const* data, size_t size) | 
					
						
							| 
									
										
										
										
											2020-06-02 16:55:55 +02:00
										 |  |  |         : m_data_ptr(data) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         , m_size_remaining(size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     bool read(T& value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  |         if (m_size_remaining < sizeof(T)) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         value = *((NetworkOrdered<T> const*)m_data_ptr); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         m_data_ptr += sizeof(T); | 
					
						
							|  |  |  |         m_size_remaining -= sizeof(T); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  |     bool read_bytes(u8* buffer, size_t count) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (m_size_remaining < count) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         memcpy(buffer, m_data_ptr, count); | 
					
						
							|  |  |  |         m_data_ptr += count; | 
					
						
							|  |  |  |         m_size_remaining -= count; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |     bool wrap_bytes(ReadonlyBytes& buffer, size_t count) | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (m_size_remaining < count) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |         buffer = ReadonlyBytes { m_data_ptr, count }; | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |         m_data_ptr += count; | 
					
						
							|  |  |  |         m_size_remaining -= count; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     u8 const* current_data_ptr() const { return m_data_ptr; } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     bool at_end() const { return !m_size_remaining; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     u8 const* m_data_ptr { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  |     size_t m_size_remaining { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | static bool process_chunk(Streamer&, PNGLoadingContext& context); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 22:44:54 +01:00
										 |  |  | union [[gnu::packed]] Pixel { | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     ARGB32 rgba { 0 }; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 v[4]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     struct { | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |         u8 r; | 
					
						
							|  |  |  |         u8 g; | 
					
						
							|  |  |  |         u8 b; | 
					
						
							|  |  |  |         u8 a; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-09-05 01:00:46 -07:00
										 |  |  | static_assert(AssertSize<Pixel, 4>()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  | static void unfilter_scanline(PNG::FilterType filter, Bytes scanline_data, ReadonlyBytes previous_scanlines_data, u8 bytes_per_complete_pixel) | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |     VERIFY(filter != PNG::FilterType::None); | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |     switch (filter) { | 
					
						
							|  |  |  |     case PNG::FilterType::Sub: | 
					
						
							| 
									
										
										
										
											2022-08-18 02:29:36 -07:00
										 |  |  |         // This loop starts at bytes_per_complete_pixel because all bytes before that are
 | 
					
						
							|  |  |  |         // guaranteed to have no valid byte at index (i - bytes_per_complete pixel).
 | 
					
						
							|  |  |  |         // All such invalid byte indexes should be treated as 0, and adding 0 to the current
 | 
					
						
							|  |  |  |         // byte would do nothing, so the first bytes_per_complete_pixel bytes can instead
 | 
					
						
							|  |  |  |         // just be skipped.
 | 
					
						
							|  |  |  |         for (size_t i = bytes_per_complete_pixel; i < scanline_data.size(); ++i) { | 
					
						
							|  |  |  |             u8 left = scanline_data[i - bytes_per_complete_pixel]; | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |             scanline_data[i] += left; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case PNG::FilterType::Up: | 
					
						
							|  |  |  |         for (size_t i = 0; i < scanline_data.size(); ++i) { | 
					
						
							|  |  |  |             u8 above = previous_scanlines_data[i]; | 
					
						
							|  |  |  |             scanline_data[i] += above; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case PNG::FilterType::Average: | 
					
						
							|  |  |  |         for (size_t i = 0; i < scanline_data.size(); ++i) { | 
					
						
							|  |  |  |             u32 left = (i < bytes_per_complete_pixel) ? 0 : scanline_data[i - bytes_per_complete_pixel]; | 
					
						
							|  |  |  |             u32 above = previous_scanlines_data[i]; | 
					
						
							|  |  |  |             u8 average = (left + above) / 2; | 
					
						
							|  |  |  |             scanline_data[i] += average; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case PNG::FilterType::Paeth: | 
					
						
							|  |  |  |         for (size_t i = 0; i < scanline_data.size(); ++i) { | 
					
						
							|  |  |  |             u8 left = (i < bytes_per_complete_pixel) ? 0 : scanline_data[i - bytes_per_complete_pixel]; | 
					
						
							|  |  |  |             u8 above = previous_scanlines_data[i]; | 
					
						
							|  |  |  |             u8 upper_left = (i < bytes_per_complete_pixel) ? 0 : previous_scanlines_data[i - bytes_per_complete_pixel]; | 
					
						
							|  |  |  |             i32 predictor = left + above - upper_left; | 
					
						
							|  |  |  |             u32 predictor_left = abs(predictor - left); | 
					
						
							|  |  |  |             u32 predictor_above = abs(predictor - above); | 
					
						
							|  |  |  |             u32 predictor_upper_left = abs(predictor - upper_left); | 
					
						
							|  |  |  |             u8 nearest; | 
					
						
							|  |  |  |             if (predictor_left <= predictor_above && predictor_left <= predictor_upper_left) { | 
					
						
							|  |  |  |                 nearest = left; | 
					
						
							|  |  |  |             } else if (predictor_above <= predictor_upper_left) { | 
					
						
							|  |  |  |                 nearest = above; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 nearest = upper_left; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |             scanline_data[i] += nearest; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | ALWAYS_INLINE static void unpack_grayscale_without_alpha(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-10-17 00:06:11 +02:00
										 |  |  |         auto* gray_values = reinterpret_cast<T const*>(context.scanlines[y].data.data()); | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |         for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |             auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							|  |  |  |             pixel.r = gray_values[i]; | 
					
						
							|  |  |  |             pixel.g = gray_values[i]; | 
					
						
							|  |  |  |             pixel.b = gray_values[i]; | 
					
						
							|  |  |  |             pixel.a = 0xff; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | ALWAYS_INLINE static void unpack_grayscale_with_alpha(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         auto* tuples = reinterpret_cast<Tuple<T> const*>(context.scanlines[y].data.data()); | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |         for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |             auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							|  |  |  |             pixel.r = tuples[i].gray; | 
					
						
							|  |  |  |             pixel.g = tuples[i].gray; | 
					
						
							|  |  |  |             pixel.b = tuples[i].gray; | 
					
						
							|  |  |  |             pixel.a = tuples[i].a; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							|  |  |  | ALWAYS_INLINE static void unpack_triplets_without_alpha(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         auto* triplets = reinterpret_cast<Triplet<T> const*>(context.scanlines[y].data.data()); | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |         for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |             auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							|  |  |  |             pixel.r = triplets[i].r; | 
					
						
							|  |  |  |             pixel.g = triplets[i].g; | 
					
						
							|  |  |  |             pixel.b = triplets[i].b; | 
					
						
							|  |  |  |             pixel.a = 0xff; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 17:55:35 +01:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | ALWAYS_INLINE static void unpack_triplets_with_transparency_value(PNGLoadingContext& context, Triplet<T> transparency_value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         auto* triplets = reinterpret_cast<Triplet<T> const*>(context.scanlines[y].data.data()); | 
					
						
							| 
									
										
										
										
											2022-02-14 17:55:35 +01:00
										 |  |  |         for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |             auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							|  |  |  |             pixel.r = triplets[i].r; | 
					
						
							|  |  |  |             pixel.g = triplets[i].g; | 
					
						
							|  |  |  |             pixel.b = triplets[i].b; | 
					
						
							|  |  |  |             if (triplets[i] == transparency_value) | 
					
						
							|  |  |  |                 pixel.a = 0x00; | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 pixel.a = 0xff; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  | NEVER_INLINE FLATTEN static ErrorOr<void> unfilter(PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |     // First unfilter the scanlines:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: Instead of creating a separate buffer for the scanlines that need to be
 | 
					
						
							|  |  |  |     //        mutated, the mutation could be done in place (if the data was non-const).
 | 
					
						
							|  |  |  |     size_t bytes_per_scanline = context.scanlines[0].data.size(); | 
					
						
							|  |  |  |     size_t bytes_needed_for_all_unfiltered_scanlines = 0; | 
					
						
							|  |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |         if (context.scanlines[y].filter != PNG::FilterType::None) { | 
					
						
							|  |  |  |             bytes_needed_for_all_unfiltered_scanlines += bytes_per_scanline; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     context.unfiltered_data = TRY(ByteBuffer::create_uninitialized(bytes_needed_for_all_unfiltered_scanlines)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // From section 6.3 of http://www.libpng.org/pub/png/spec/1.2/PNG-Filters.html
 | 
					
						
							|  |  |  |     // "bpp is defined as the number of bytes per complete pixel, rounding up to one.
 | 
					
						
							|  |  |  |     // For example, for color type 2 with a bit depth of 16, bpp is equal to 6
 | 
					
						
							|  |  |  |     // (three samples, two bytes per sample); for color type 0 with a bit depth of 2,
 | 
					
						
							|  |  |  |     // bpp is equal to 1 (rounding up); for color type 4 with a bit depth of 16, bpp
 | 
					
						
							|  |  |  |     // is equal to 4 (two-byte grayscale sample, plus two-byte alpha sample)."
 | 
					
						
							|  |  |  |     u8 bytes_per_complete_pixel = (context.bit_depth + 7) / 8 * context.channels; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u8 dummy_scanline_bytes[bytes_per_scanline]; | 
					
						
							|  |  |  |     memset(dummy_scanline_bytes, 0, sizeof(dummy_scanline_bytes)); | 
					
						
							|  |  |  |     auto previous_scanlines_data = ReadonlyBytes { dummy_scanline_bytes, sizeof(dummy_scanline_bytes) }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int y = 0, data_start = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |         if (context.scanlines[y].filter != PNG::FilterType::None) { | 
					
						
							|  |  |  |             auto scanline_data_slice = context.unfiltered_data.bytes().slice(data_start, bytes_per_scanline); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Copy the current values over and set the scanline's data to the to-be-mutated slice
 | 
					
						
							|  |  |  |             context.scanlines[y].data.copy_to(scanline_data_slice); | 
					
						
							|  |  |  |             context.scanlines[y].data = scanline_data_slice; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             unfilter_scanline(context.scanlines[y].filter, scanline_data_slice, previous_scanlines_data, bytes_per_complete_pixel); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             data_start += bytes_per_scanline; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         previous_scanlines_data = context.scanlines[y].data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Now unpack the scanlines to RGBA:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     switch (context.color_type) { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::Greyscale: | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |         if (context.bit_depth == 8) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |             unpack_grayscale_without_alpha<u8>(context); | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |         } else if (context.bit_depth == 16) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |             unpack_grayscale_without_alpha<u16>(context); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) { | 
					
						
							| 
									
										
										
										
											2020-07-27 19:08:12 +02:00
										 |  |  |             auto bit_depth_squared = context.bit_depth * context.bit_depth; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |             auto pixels_per_byte = 8 / context.bit_depth; | 
					
						
							|  |  |  |             auto mask = (1 << context.bit_depth) - 1; | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |                 auto* gray_values = context.scanlines[y].data.data(); | 
					
						
							| 
									
										
										
										
											2020-06-11 08:48:09 +02:00
										 |  |  |                 for (int x = 0; x < context.width; ++x) { | 
					
						
							|  |  |  |                     auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (x % pixels_per_byte)); | 
					
						
							|  |  |  |                     auto value = (gray_values[x / pixels_per_byte] >> bit_offset) & mask; | 
					
						
							|  |  |  |                     auto& pixel = (Pixel&)context.bitmap->scanline(y)[x]; | 
					
						
							| 
									
										
										
										
											2020-07-27 16:40:12 +02:00
										 |  |  |                     pixel.r = value * (0xff / bit_depth_squared); | 
					
						
							|  |  |  |                     pixel.g = value * (0xff / bit_depth_squared); | 
					
						
							|  |  |  |                     pixel.b = value * (0xff / bit_depth_squared); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                     pixel.a = 0xff; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::GreyscaleWithAlpha: | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |         if (context.bit_depth == 8) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |             unpack_grayscale_with_alpha<u8>(context); | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |         } else if (context.bit_depth == 16) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |             unpack_grayscale_with_alpha<u16>(context); | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::Truecolor: | 
					
						
							| 
									
										
										
										
											2022-02-14 17:55:35 +01:00
										 |  |  |         if (context.palette_transparency_data.size() == 6) { | 
					
						
							|  |  |  |             if (context.bit_depth == 8) { | 
					
						
							|  |  |  |                 unpack_triplets_with_transparency_value<u8>(context, Triplet<u8> { context.palette_transparency_data[0], context.palette_transparency_data[2], context.palette_transparency_data[4] }); | 
					
						
							|  |  |  |             } else if (context.bit_depth == 16) { | 
					
						
							|  |  |  |                 u16 tr = context.palette_transparency_data[0] | context.palette_transparency_data[1] << 8; | 
					
						
							|  |  |  |                 u16 tg = context.palette_transparency_data[2] | context.palette_transparency_data[3] << 8; | 
					
						
							|  |  |  |                 u16 tb = context.palette_transparency_data[4] | context.palette_transparency_data[5] << 8; | 
					
						
							|  |  |  |                 unpack_triplets_with_transparency_value<u16>(context, Triplet<u16> { tr, tg, tb }); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-02-14 17:55:35 +01:00
										 |  |  |             if (context.bit_depth == 8) | 
					
						
							|  |  |  |                 unpack_triplets_without_alpha<u8>(context); | 
					
						
							|  |  |  |             else if (context.bit_depth == 16) | 
					
						
							|  |  |  |                 unpack_triplets_without_alpha<u16>(context); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::TruecolorWithAlpha: | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         if (context.bit_depth == 8) { | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |                 memcpy(context.bitmap->scanline(y), context.scanlines[y].data.data(), context.scanlines[y].data.size()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else if (context.bit_depth == 16) { | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-04-07 20:42:09 +02:00
										 |  |  |                 auto* quartets = reinterpret_cast<Quartet<u16> const*>(context.scanlines[y].data.data()); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |                 for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |                     auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							| 
									
										
										
										
											2022-04-07 20:42:09 +02:00
										 |  |  |                     pixel.r = quartets[i].r & 0xFF; | 
					
						
							|  |  |  |                     pixel.g = quartets[i].g & 0xFF; | 
					
						
							|  |  |  |                     pixel.b = quartets[i].b & 0xFF; | 
					
						
							|  |  |  |                     pixel.a = quartets[i].a & 0xFF; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::IndexedColor: | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         if (context.bit_depth == 8) { | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |                 auto* palette_index = context.scanlines[y].data.data(); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                 for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |                     auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  |                     if (palette_index[i] >= context.palette_data.size()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |                         return Error::from_string_literal("PNGImageDecoderPlugin: Palette index out of range"); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                     auto& color = context.palette_data.at((int)palette_index[i]); | 
					
						
							|  |  |  |                     auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u | 
					
						
							|  |  |  |                         ? context.palette_transparency_data.data()[palette_index[i]] | 
					
						
							|  |  |  |                         : 0xff; | 
					
						
							|  |  |  |                     pixel.r = color.r; | 
					
						
							|  |  |  |                     pixel.g = color.g; | 
					
						
							|  |  |  |                     pixel.b = color.b; | 
					
						
							|  |  |  |                     pixel.a = transparency; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         } else if (context.bit_depth == 1 || context.bit_depth == 2 || context.bit_depth == 4) { | 
					
						
							|  |  |  |             auto pixels_per_byte = 8 / context.bit_depth; | 
					
						
							|  |  |  |             auto mask = (1 << context.bit_depth) - 1; | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2021-04-29 22:23:52 +02:00
										 |  |  |                 auto* palette_indices = context.scanlines[y].data.data(); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                 for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |                     auto bit_offset = (8 - context.bit_depth) - (context.bit_depth * (i % pixels_per_byte)); | 
					
						
							| 
									
										
										
										
											2021-04-29 22:23:52 +02:00
										 |  |  |                     auto palette_index = (palette_indices[i / pixels_per_byte] >> bit_offset) & mask; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                     auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  |                     if ((size_t)palette_index >= context.palette_data.size()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |                         return Error::from_string_literal("PNGImageDecoderPlugin: Palette index out of range"); | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |                     auto& color = context.palette_data.at(palette_index); | 
					
						
							|  |  |  |                     auto transparency = context.palette_transparency_data.size() >= palette_index + 1u | 
					
						
							|  |  |  |                         ? context.palette_transparency_data.data()[palette_index] | 
					
						
							|  |  |  |                         : 0xff; | 
					
						
							|  |  |  |                     pixel.r = color.r; | 
					
						
							|  |  |  |                     pixel.g = color.g; | 
					
						
							|  |  |  |                     pixel.b = color.b; | 
					
						
							|  |  |  |                     pixel.a = transparency; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |     // Swap r and b values:
 | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-08-17 23:13:59 -07:00
										 |  |  |         auto* pixels = (Pixel*)context.bitmap->scanline(y); | 
					
						
							|  |  |  |         for (int i = 0; i < context.bitmap->width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool decode_png_header(PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (context.state >= PNGLoadingContext::HeaderDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:14:19 +02:00
										 |  |  |     if (!context.data || context.data_size < sizeof(PNG::header)) { | 
					
						
							| 
									
										
										
										
											2021-04-07 17:25:22 +03:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Missing PNG header"); | 
					
						
							| 
									
										
										
										
											2020-04-19 17:48:43 +02:00
										 |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:14:19 +02:00
										 |  |  |     if (memcmp(context.data, PNG::header.span().data(), sizeof(PNG::header)) != 0) { | 
					
						
							| 
									
										
										
										
											2021-04-07 17:25:22 +03:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Invalid PNG header"); | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     context.data_current_ptr = context.data + sizeof(PNG::header); | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     context.state = PNGLoadingContext::HeaderDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-21 14:08:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool decode_png_size(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::SizeDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (context.state < PNGLoadingContext::HeaderDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_header(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     size_t data_remaining = context.data_size - (context.data_current_ptr - context.data); | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     Streamer streamer(context.data_current_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end() && !context.has_seen_iend) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         if (!process_chunk(streamer, context)) { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         context.data_current_ptr = streamer.current_data_ptr(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |         if (context.width && context.height) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::SizeDecoded; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static bool decode_png_image_data_chunk(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::ImageDataChunkDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.state < PNGLoadingContext::SizeDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_size(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t data_remaining = context.data_size - (context.data_current_ptr - context.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Streamer streamer(context.data_current_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end() && !context.has_seen_iend) { | 
					
						
							|  |  |  |         if (!process_chunk(streamer, context)) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         context.data_current_ptr = streamer.current_data_ptr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (context.state >= PNGLoadingContext::State::ImageDataChunkDecoded) | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool decode_png_animation_data_chunks(PNGLoadingContext& context, u32 requested_animation_frame_index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::ImageDataChunkDecoded) { | 
					
						
							|  |  |  |         if (context.last_completed_animation_frame_index.has_value()) { | 
					
						
							|  |  |  |             if (requested_animation_frame_index <= context.last_completed_animation_frame_index.value()) | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if (!decode_png_image_data_chunk(context)) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t data_remaining = context.data_size - (context.data_current_ptr - context.data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Streamer streamer(context.data_current_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end() && !context.has_seen_iend) { | 
					
						
							|  |  |  |         if (!process_chunk(streamer, context)) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         context.data_current_ptr = streamer.current_data_ptr(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (context.last_completed_animation_frame_index.has_value()) { | 
					
						
							|  |  |  |             if (requested_animation_frame_index <= context.last_completed_animation_frame_index.value()) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!context.last_completed_animation_frame_index.has_value()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return requested_animation_frame_index <= context.last_completed_animation_frame_index.value(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool decode_png_chunks(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::State::ChunksDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.state < PNGLoadingContext::HeaderDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_header(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     size_t data_remaining = context.data_size - (context.data_current_ptr - context.data); | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context.compressed_data.ensure_capacity(context.data_size); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     Streamer streamer(context.data_current_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end() && !context.has_seen_iend) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         if (!process_chunk(streamer, context)) { | 
					
						
							| 
									
										
										
										
											2021-01-17 00:51:41 +01:00
										 |  |  |             // Ignore failed chunk and just consider chunk decoding being done.
 | 
					
						
							|  |  |  |             // decode_png_bitmap() will check whether we got all required ones anyway.
 | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         context.data_current_ptr = streamer.current_data_ptr(); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::ChunksDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static ErrorOr<void> decode_png_bitmap_simple(PNGLoadingContext& context, ByteBuffer& decompression_buffer) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     Streamer streamer(decompression_buffer.data(), decompression_buffer.size()); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |         PNG::FilterType filter; | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         if (!streamer.read(filter)) { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |         if (to_underlying(filter) > 4) { | 
					
						
							| 
									
										
										
										
											2020-06-11 09:32:17 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter"); | 
					
						
							| 
									
										
										
										
											2020-06-11 09:32:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         context.scanlines.append({ filter }); | 
					
						
							|  |  |  |         auto& scanline_buffer = context.scanlines.last().data; | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  |         auto row_size = context.compute_row_size_for_width(context.width); | 
					
						
							|  |  |  |         if (row_size.has_overflow()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Row size overflow"); | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) { | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 20:06:05 +01:00
										 |  |  |     context.bitmap = TRY(Bitmap::create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height })); | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  |     return unfilter(context); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adam7_height(PNGLoadingContext& context, int pass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (pass) { | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         return (context.height + 7) / 8; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         return (context.height + 7) / 8; | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         return (context.height + 3) / 8; | 
					
						
							|  |  |  |     case 4: | 
					
						
							|  |  |  |         return (context.height + 3) / 4; | 
					
						
							|  |  |  |     case 5: | 
					
						
							|  |  |  |         return (context.height + 1) / 4; | 
					
						
							|  |  |  |     case 6: | 
					
						
							|  |  |  |         return (context.height + 1) / 2; | 
					
						
							|  |  |  |     case 7: | 
					
						
							|  |  |  |         return context.height / 2; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int adam7_width(PNGLoadingContext& context, int pass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (pass) { | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         return (context.width + 7) / 8; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         return (context.width + 3) / 8; | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         return (context.width + 3) / 4; | 
					
						
							|  |  |  |     case 4: | 
					
						
							|  |  |  |         return (context.width + 1) / 4; | 
					
						
							|  |  |  |     case 5: | 
					
						
							|  |  |  |         return (context.width + 1) / 2; | 
					
						
							|  |  |  |     case 6: | 
					
						
							|  |  |  |         return context.width / 2; | 
					
						
							|  |  |  |     case 7: | 
					
						
							|  |  |  |         return context.width; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 10:30:21 +01:00
										 |  |  | // Index 0 unused (non-interlaced case)
 | 
					
						
							|  |  |  | static int adam7_starty[8] = { 0, 0, 0, 4, 0, 2, 0, 1 }; | 
					
						
							|  |  |  | static int adam7_startx[8] = { 0, 0, 4, 0, 2, 0, 1, 0 }; | 
					
						
							|  |  |  | static int adam7_stepy[8] = { 1, 8, 8, 8, 4, 4, 2, 2 }; | 
					
						
							|  |  |  | static int adam7_stepx[8] = { 1, 8, 8, 4, 4, 2, 2, 1 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  | static ErrorOr<void> decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     auto subimage_context = context.create_subimage_context(adam7_width(context, pass), adam7_height(context, pass)); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // For small images, some passes might be empty
 | 
					
						
							|  |  |  |     if (!subimage_context.width || !subimage_context.height) | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int y = 0; y < subimage_context.height; ++y) { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |         PNG::FilterType filter; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         if (!streamer.read(filter)) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |         if (to_underlying(filter) > 4) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Invalid PNG filter"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         subimage_context.scanlines.append({ filter }); | 
					
						
							|  |  |  |         auto& scanline_buffer = subimage_context.scanlines.last().data; | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         auto row_size = context.compute_row_size_for_width(subimage_context.width); | 
					
						
							|  |  |  |         if (row_size.has_overflow()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Row size overflow"); | 
					
						
							| 
									
										
										
										
											2020-12-23 19:04:12 +01:00
										 |  |  |         if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 20:06:05 +01:00
										 |  |  |     subimage_context.bitmap = TRY(Bitmap::create(context.bitmap->format(), { subimage_context.width, subimage_context.height })); | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |     TRY(unfilter(subimage_context)); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Copy the subimage data into the main image according to the pass pattern
 | 
					
						
							|  |  |  |     for (int y = 0, dy = adam7_starty[pass]; y < subimage_context.height && dy < context.height; ++y, dy += adam7_stepy[pass]) { | 
					
						
							|  |  |  |         for (int x = 0, dx = adam7_startx[pass]; x < subimage_context.width && dy < context.width; ++x, dx += adam7_stepx[pass]) { | 
					
						
							|  |  |  |             context.bitmap->set_pixel(dx, dy, subimage_context.bitmap->get_pixel(x, y)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static ErrorOr<void> decode_png_adam7(PNGLoadingContext& context, ByteBuffer& decompression_buffer) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     Streamer streamer(decompression_buffer.data(), decompression_buffer.size()); | 
					
						
							| 
									
										
										
										
											2023-01-20 20:06:05 +01:00
										 |  |  |     context.bitmap = TRY(Bitmap::create(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height })); | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |     for (int pass = 1; pass <= 7; ++pass) | 
					
						
							|  |  |  |         TRY(decode_adam7_pass(context, streamer, pass)); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  | static ErrorOr<void> decode_png_bitmap(PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     if (context.state < PNGLoadingContext::State::ChunksDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_chunks(context)) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::State::BitmapDecoded) | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 14:34:54 -05:00
										 |  |  |     if (context.width == -1 || context.height == -1) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see an IHDR chunk."); | 
					
						
							| 
									
										
										
										
											2020-11-29 14:34:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty."); | 
					
						
							| 
									
										
										
										
											2020-11-13 11:37:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-26 15:51:51 +01:00
										 |  |  |     auto result = Compress::ZlibDecompressor::decompress_all(context.compressed_data.span()); | 
					
						
							| 
									
										
										
										
											2021-03-03 23:54:07 +02:00
										 |  |  |     if (!result.has_value()) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decompression failed"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     auto& decompression_buffer = result.value(); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     context.compressed_data.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.scanlines.ensure_capacity(context.height); | 
					
						
							|  |  |  |     switch (context.interlace_method) { | 
					
						
							|  |  |  |     case PngInterlaceMethod::Null: | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |         TRY(decode_png_bitmap_simple(context, decompression_buffer)); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case PngInterlaceMethod::Adam7: | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |         TRY(decode_png_adam7(context, decompression_buffer)); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-03-16 19:45:54 +02:00
										 |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Invalid interlace method"); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::BitmapDecoded; | 
					
						
							| 
									
										
										
										
											2021-11-20 15:59:23 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static ErrorOr<RefPtr<Bitmap>> decode_png_animation_frame_bitmap(PNGLoadingContext& context, AnimationFrame& animation_frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.color_type == PNG::ColorType::IndexedColor && context.palette_data.is_empty()) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Didn't see a PLTE chunk for a palletized image, or it was empty."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VERIFY(!animation_frame.bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto frame_rect = animation_frame.rect(); | 
					
						
							|  |  |  |     auto frame_context = context.create_subimage_context(frame_rect.width(), frame_rect.height()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto result = Compress::ZlibDecompressor::decompress_all(animation_frame.compressed_data.span()); | 
					
						
							|  |  |  |     if (!result.has_value()) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of animation frame failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto& decompression_buffer = result.value(); | 
					
						
							|  |  |  |     frame_context.compressed_data.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     frame_context.scanlines.ensure_capacity(frame_context.height); | 
					
						
							|  |  |  |     switch (context.interlace_method) { | 
					
						
							|  |  |  |     case PngInterlaceMethod::Null: | 
					
						
							|  |  |  |         TRY(decode_png_bitmap_simple(frame_context, decompression_buffer)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case PngInterlaceMethod::Adam7: | 
					
						
							|  |  |  |         TRY(decode_png_adam7(frame_context, decompression_buffer)); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Invalid interlace method"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.state = PNGLoadingContext::State::BitmapDecoded; | 
					
						
							|  |  |  |     return move(frame_context.bitmap); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 15:44:54 +01:00
										 |  |  | static bool is_valid_compression_method(u8 compression_method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return compression_method == 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool is_valid_filter_method(u8 filter_method) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-07 17:23:36 +03:00
										 |  |  |     return filter_method == 0; | 
					
						
							| 
									
										
										
										
											2020-12-23 15:44:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  | static bool process_IHDR(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-22 14:41:11 +02:00
										 |  |  |     if (data.size() < (int)sizeof(PNG_IHDR)) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |     auto& ihdr = *(const PNG_IHDR*)data.data(); | 
					
						
							| 
									
										
										
										
											2020-11-13 11:37:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-25 00:19:06 +01:00
										 |  |  |     if (ihdr.width > maximum_width_for_decoded_images || ihdr.height > maximum_height_for_decoded_images) { | 
					
						
							|  |  |  |         dbgln("This PNG is too large for comfort: {}x{}", (u32)ihdr.width, (u32)ihdr.height); | 
					
						
							| 
									
										
										
										
											2020-11-13 11:37:10 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 15:44:54 +01:00
										 |  |  |     if (!is_valid_compression_method(ihdr.compression_method)) { | 
					
						
							|  |  |  |         dbgln("PNG has invalid compression method {}", ihdr.compression_method); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!is_valid_filter_method(ihdr.filter_method)) { | 
					
						
							|  |  |  |         dbgln("PNG has invalid filter method {}", ihdr.filter_method); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     context.width = ihdr.width; | 
					
						
							|  |  |  |     context.height = ihdr.height; | 
					
						
							|  |  |  |     context.bit_depth = ihdr.bit_depth; | 
					
						
							|  |  |  |     context.color_type = ihdr.color_type; | 
					
						
							|  |  |  |     context.compression_method = ihdr.compression_method; | 
					
						
							|  |  |  |     context.filter_method = ihdr.filter_method; | 
					
						
							|  |  |  |     context.interlace_method = ihdr.interlace_method; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |     dbgln_if(PNG_DEBUG, "PNG: {}x{} ({} bpp)", context.width, context.height, context.bit_depth); | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     dbgln_if(PNG_DEBUG, "     Color type: {}", to_underlying(context.color_type)); | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |     dbgln_if(PNG_DEBUG, "Compress Method: {}", context.compression_method); | 
					
						
							|  |  |  |     dbgln_if(PNG_DEBUG, "  Filter Method: {}", context.filter_method); | 
					
						
							|  |  |  |     dbgln_if(PNG_DEBUG, " Interlace type: {}", context.interlace_method); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     if (context.interlace_method != PngInterlaceMethod::Null && context.interlace_method != PngInterlaceMethod::Adam7) { | 
					
						
							| 
									
										
										
										
											2021-04-07 17:25:22 +03:00
										 |  |  |         dbgln_if(PNG_DEBUG, "PNGLoader::process_IHDR: unknown interlace method: {}", context.interlace_method); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     switch (context.color_type) { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::Greyscale: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8 && context.bit_depth != 16) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         context.channels = 1; | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::GreyscaleWithAlpha: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         if (context.bit_depth != 8 && context.bit_depth != 16) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         context.channels = 2; | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::Truecolor: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         if (context.bit_depth != 8 && context.bit_depth != 16) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         context.channels = 3; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::IndexedColor: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         if (context.bit_depth != 1 && context.bit_depth != 2 && context.bit_depth != 4 && context.bit_depth != 8) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         context.channels = 1; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::TruecolorWithAlpha: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         if (context.bit_depth != 8 && context.bit_depth != 16) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |         context.channels = 4; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:37:15 -05:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  | static bool process_IDAT(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |     context.compressed_data.append(data.data(), data.size()); | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (context.state < PNGLoadingContext::State::ImageDataChunkDecoded) | 
					
						
							|  |  |  |         context.state = PNGLoadingContext::State::ImageDataChunkDecoded; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  | static bool process_PLTE(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     context.palette_data.append((PaletteEntry const*)data.data(), data.size() / 3); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  | static bool process_tRNS(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (context.color_type) { | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     case PNG::ColorType::Greyscale: | 
					
						
							|  |  |  |     case PNG::ColorType::Truecolor: | 
					
						
							|  |  |  |     case PNG::ColorType::IndexedColor: | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |         context.palette_transparency_data.append(data.data(), data.size()); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  | static bool process_cHRM(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#11cHRM
 | 
					
						
							|  |  |  |     if (data.size() != 32) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     context.chromaticities_and_whitepoint = *bit_cast<ChromaticitiesAndWhitepoint* const>(data.data()); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_cICP(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#cICP-chunk
 | 
					
						
							|  |  |  |     if (data.size() != 4) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     context.coding_independent_code_points = *bit_cast<CodingIndependentCodePoints* const>(data.data()); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_iCCP(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#11iCCP
 | 
					
						
							|  |  |  |     size_t profile_name_length_max = min(80u, data.size()); | 
					
						
							|  |  |  |     size_t profile_name_length = strnlen((char const*)data.data(), profile_name_length_max); | 
					
						
							|  |  |  |     if (profile_name_length == 0 || profile_name_length == profile_name_length_max) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (data.size() < profile_name_length + 2) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u8 compression_method = data[profile_name_length + 1]; | 
					
						
							|  |  |  |     if (compression_method != 0) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.embedded_icc_profile = EmbeddedICCProfile { { data.data(), profile_name_length }, data.slice(profile_name_length + 2) }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_gAMA(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#11gAMA
 | 
					
						
							|  |  |  |     if (data.size() != 4) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u32 gamma = *bit_cast<NetworkOrdered<u32> const*>(data.data()); | 
					
						
							|  |  |  |     if (gamma & 0x8000'0000) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     context.gamma = gamma; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_sRGB(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#srgb-standard-colour-space
 | 
					
						
							|  |  |  |     if (data.size() != 1) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u8 rendering_intent = data[0]; | 
					
						
							|  |  |  |     if (rendering_intent > 3) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.sRGB_rendering_intent = (RenderingIntent)rendering_intent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static bool process_acTL(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#acTL-chunk
 | 
					
						
							|  |  |  |     if (context.has_seen_idat_chunk) | 
					
						
							|  |  |  |         return true; // Ignore if we encounter it after the first idat
 | 
					
						
							|  |  |  |     if (data.size() != sizeof(acTL_Chunk)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto const& acTL = *bit_cast<acTL_Chunk* const>(data.data()); | 
					
						
							|  |  |  |     context.animation_frame_count = acTL.num_frames; | 
					
						
							|  |  |  |     context.animation_loop_count = acTL.num_plays; | 
					
						
							|  |  |  |     context.has_seen_actl_chunk_before_idat = true; | 
					
						
							|  |  |  |     context.animation_frames.ensure_capacity(context.animation_frame_count); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_fcTL(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#fcTL-chunk
 | 
					
						
							|  |  |  |     if (!context.has_seen_actl_chunk_before_idat) | 
					
						
							|  |  |  |         return true; // Ignore if it's not a valid animated png
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (data.size() != sizeof(fcTL_Chunk)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto const& fcTL = *bit_cast<fcTL_Chunk* const>(data.data()); | 
					
						
							|  |  |  |     if (fcTL.sequence_number != context.animation_next_expected_seq) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.animation_next_expected_seq++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fcTL.width == 0 || fcTL.height == 0) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Checked<int> left { static_cast<int>(fcTL.x_offset) }; | 
					
						
							|  |  |  |     Checked<int> top { static_cast<int>(fcTL.y_offset) }; | 
					
						
							|  |  |  |     Checked<int> width { static_cast<int>(fcTL.width) }; | 
					
						
							|  |  |  |     Checked<int> height { static_cast<int>(fcTL.height) }; | 
					
						
							|  |  |  |     auto right = left + width; | 
					
						
							|  |  |  |     auto bottom = top + height; | 
					
						
							|  |  |  |     if (left < 0 || width <= 0 || right.has_overflow() || right > context.width) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     if (top < 0 || height <= 0 || bottom.has_overflow() || bottom > context.height) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_first_animation_frame = context.animation_frames.is_empty(); | 
					
						
							|  |  |  |     if (!is_first_animation_frame) | 
					
						
							|  |  |  |         context.last_completed_animation_frame_index = context.animation_frames.size() - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.animation_frames.append({ fcTL }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!context.has_seen_idat_chunk && is_first_animation_frame) | 
					
						
							|  |  |  |         context.is_first_idat_part_of_animation = true; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_fdAT(ReadonlyBytes data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#fdAT-chunk
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (data.size() <= 4) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     u32 sequence_number = *bit_cast<NetworkOrdered<u32> const*>(data.data()); | 
					
						
							|  |  |  |     if (sequence_number != context.animation_next_expected_seq) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     context.animation_next_expected_seq++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.animation_frames.is_empty()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     auto& current_animation_frame = context.animation_frames[context.animation_frames.size() - 1]; | 
					
						
							|  |  |  |     auto compressed_data = data.slice(4); | 
					
						
							|  |  |  |     current_animation_frame.compressed_data.append(compressed_data.data(), compressed_data.size()); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_IEND(ReadonlyBytes, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://www.w3.org/TR/png/#11IEND
 | 
					
						
							|  |  |  |     if (context.has_seen_actl_chunk_before_idat) | 
					
						
							|  |  |  |         context.last_completed_animation_frame_index = context.animation_frames.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.has_seen_iend = true; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | static bool process_chunk(Streamer& streamer, PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u32 chunk_size; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     if (!streamer.read(chunk_size)) { | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Bail at chunk_size"); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 chunk_type[5]; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     chunk_type[4] = '\0'; | 
					
						
							|  |  |  |     if (!streamer.read_bytes(chunk_type, 4)) { | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Bail at chunk_type"); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-19 12:00:17 +01:00
										 |  |  |     ReadonlyBytes chunk_data; | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |     if (!streamer.wrap_bytes(chunk_data, chunk_size)) { | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Bail at chunk_data"); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u32 chunk_crc; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     if (!streamer.read(chunk_crc)) { | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |         dbgln_if(PNG_DEBUG, "Bail at chunk_crc"); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-31 15:04:23 +01:00
										 |  |  |     dbgln_if(PNG_DEBUG, "Chunk type: '{}', size: {}, crc: {:x}", chunk_type, chunk_size, chunk_crc); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "IHDR")) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         return process_IHDR(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "IDAT")) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return process_IDAT(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "PLTE")) | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         return process_PLTE(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2023-01-27 14:38:08 -05:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "cHRM")) | 
					
						
							|  |  |  |         return process_cHRM(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "cICP")) | 
					
						
							|  |  |  |         return process_cICP(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "iCCP")) | 
					
						
							|  |  |  |         return process_iCCP(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "gAMA")) | 
					
						
							|  |  |  |         return process_gAMA(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "sRGB")) | 
					
						
							|  |  |  |         return process_sRGB(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "tRNS")) | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         return process_tRNS(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!strcmp((char const*)chunk_type, "acTL")) | 
					
						
							|  |  |  |         return process_acTL(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "fcTL")) | 
					
						
							|  |  |  |         return process_fcTL(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "fdAT")) | 
					
						
							|  |  |  |         return process_fdAT(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((char const*)chunk_type, "IEND")) | 
					
						
							|  |  |  |         return process_IEND(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | PNGImageDecoderPlugin::PNGImageDecoderPlugin(u8 const* data, size_t size) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_context = make<PNGLoadingContext>(); | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     m_context->data = m_context->data_current_ptr = data; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     m_context->data_size = size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:26:37 -06:00
										 |  |  | PNGImageDecoderPlugin::~PNGImageDecoderPlugin() = default; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | bool PNGImageDecoderPlugin::ensure_image_data_chunk_was_decoded() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_context->state == PNGLoadingContext::State::Error) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->state < PNGLoadingContext::State::ImageDataChunkDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_image_data_chunk(*m_context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool PNGImageDecoderPlugin::ensure_animation_frame_was_decoded(u32 animation_frame_index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_context->state == PNGLoadingContext::State::Error) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->state < PNGLoadingContext::State::ImageDataChunkDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_image_data_chunk(*m_context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->last_completed_animation_frame_index.has_value()) { | 
					
						
							|  |  |  |         if (m_context->last_completed_animation_frame_index.value() >= animation_frame_index) | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return decode_png_animation_data_chunks(*m_context, animation_frame_index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | IntSize PNGImageDecoderPlugin::size() | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |     if (m_context->state == PNGLoadingContext::State::Error) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (m_context->state < PNGLoadingContext::State::SizeDecoded) { | 
					
						
							|  |  |  |         bool success = decode_png_size(*m_context); | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         if (!success) | 
					
						
							|  |  |  |             return {}; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { m_context->width, m_context->height }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  | void PNGImageDecoderPlugin::set_volatile() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_context->bitmap) | 
					
						
							|  |  |  |         m_context->bitmap->set_volatile(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  | bool PNGImageDecoderPlugin::set_nonvolatile(bool& was_purged) | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_context->bitmap) | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  |     return m_context->bitmap->set_nonvolatile(was_purged); | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 19:27:07 +02:00
										 |  |  | ErrorOr<void> PNGImageDecoderPlugin::initialize() | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-07 19:27:07 +02:00
										 |  |  |     if (decode_png_header(*m_context)) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     return Error::from_string_literal("bad image header"); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 18:02:50 +00:00
										 |  |  | bool PNGImageDecoderPlugin::sniff(ReadonlyBytes data) | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     PNGLoadingContext context; | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     context.data = context.data_current_ptr = data.data(); | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  |     context.data_size = data.size(); | 
					
						
							|  |  |  |     return decode_png_header(context); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> PNGImageDecoderPlugin::create(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return adopt_nonnull_own_or_enomem(new (nothrow) PNGImageDecoderPlugin(data.data(), data.size())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | bool PNGImageDecoderPlugin::is_animated() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!ensure_image_data_chunk_was_decoded()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return m_context->has_seen_actl_chunk_before_idat; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t PNGImageDecoderPlugin::loop_count() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!ensure_image_data_chunk_was_decoded()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return m_context->animation_loop_count; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t PNGImageDecoderPlugin::frame_count() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!ensure_image_data_chunk_was_decoded()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_context->has_seen_actl_chunk_before_idat) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto total_frames = m_context->animation_frame_count; | 
					
						
							|  |  |  |     if (!m_context->is_first_idat_part_of_animation) | 
					
						
							|  |  |  |         total_frames++; | 
					
						
							|  |  |  |     return total_frames; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-07 20:41:22 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | size_t PNGImageDecoderPlugin::first_animated_frame_index() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!ensure_image_data_chunk_was_decoded()) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     if (!m_context->has_seen_actl_chunk_before_idat) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     return m_context->is_first_idat_part_of_animation ? 0 : 1; | 
					
						
							| 
									
										
										
										
											2023-04-07 20:41:22 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | static ErrorOr<RefPtr<Bitmap>> render_animation_frame(AnimationFrame const& prev_animation_frame, AnimationFrame& animation_frame, Bitmap const& decoded_frame_bitmap) | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     auto rendered_bitmap = TRY(prev_animation_frame.bitmap->clone()); | 
					
						
							|  |  |  |     Painter painter(rendered_bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static constexpr Color transparent_black = { 0, 0, 0, 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto frame_rect = animation_frame.rect(); | 
					
						
							|  |  |  |     switch (prev_animation_frame.fcTL.dispose_op) { | 
					
						
							|  |  |  |     case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_NONE: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_BACKGROUND: | 
					
						
							|  |  |  |         painter.clear_rect(rendered_bitmap->rect(), transparent_black); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case fcTL_Chunk::DisposeOp::APNG_DISPOSE_OP_PREVIOUS: { | 
					
						
							|  |  |  |         painter.blit(frame_rect.location(), *prev_animation_frame.bitmap, frame_rect, 1.0f, false); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     switch (animation_frame.fcTL.blend_op) { | 
					
						
							|  |  |  |     case fcTL_Chunk::BlendOp::APNG_BLEND_OP_SOURCE: | 
					
						
							|  |  |  |         painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, false); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case fcTL_Chunk::BlendOp::APNG_BLEND_OP_OVER: | 
					
						
							|  |  |  |         painter.blit(frame_rect.location(), decoded_frame_bitmap, decoded_frame_bitmap.rect(), 1.0f, true); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rendered_bitmap; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-18 13:47:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  | ErrorOr<ImageFrameDescriptor> PNGImageDecoderPlugin::frame(size_t index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-18 13:47:29 +01:00
										 |  |  |     if (m_context->state == PNGLoadingContext::State::Error) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2021-11-18 13:47:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     if (!ensure_image_data_chunk_was_decoded()) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto set_descriptor_duration = [](ImageFrameDescriptor& descriptor, AnimationFrame const& animation_frame) { | 
					
						
							|  |  |  |         descriptor.duration = static_cast<int>(animation_frame.duration_ms()); | 
					
						
							|  |  |  |         if (descriptor.duration < 0) | 
					
						
							|  |  |  |             descriptor.duration = NumericLimits<int>::min(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     auto load_default_image = [&]() -> ErrorOr<void> { | 
					
						
							|  |  |  |         if (m_context->state < PNGLoadingContext::State::BitmapDecoded) { | 
					
						
							|  |  |  |             // NOTE: This forces the chunk decoding to happen.
 | 
					
						
							|  |  |  |             TRY(decode_png_bitmap(*m_context)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         VERIFY(m_context->bitmap); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (index == 0) { | 
					
						
							|  |  |  |         TRY(load_default_image()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ImageFrameDescriptor descriptor { m_context->bitmap }; | 
					
						
							|  |  |  |         if (m_context->has_seen_actl_chunk_before_idat && m_context->is_first_idat_part_of_animation) | 
					
						
							|  |  |  |             set_descriptor_duration(descriptor, m_context->animation_frames[0]); | 
					
						
							|  |  |  |         return descriptor; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_context->has_seen_actl_chunk_before_idat) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Invalid frame index"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!ensure_animation_frame_was_decoded(index)) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decoding image data chunk"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (index >= m_context->animation_frames.size()) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Invalid animation frame index"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // We need to assemble each frame up until the one requested,
 | 
					
						
							|  |  |  |     // so decode all bitmaps that haven't been decoded yet.
 | 
					
						
							|  |  |  |     for (size_t i = m_context->animation_next_frame_to_render; i <= index; i++) { | 
					
						
							|  |  |  |         if (i == 0) { | 
					
						
							|  |  |  |             // If the default image hasn't been loaded, load it now
 | 
					
						
							|  |  |  |             TRY(load_default_image()); // May modify animation_frames!
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             auto& animation_frame = m_context->animation_frames[i]; | 
					
						
							|  |  |  |             animation_frame.bitmap = m_context->bitmap; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             auto& animation_frame = m_context->animation_frames[i]; | 
					
						
							|  |  |  |             VERIFY(!animation_frame.bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             auto decoded_bitmap = TRY(decode_png_animation_frame_bitmap(*m_context, animation_frame)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             auto prev_animation_frame = m_context->animation_frames[i - 1]; | 
					
						
							|  |  |  |             animation_frame.bitmap = TRY(render_animation_frame(prev_animation_frame, animation_frame, *decoded_bitmap)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         m_context->animation_next_frame_to_render = i + 1; | 
					
						
							| 
									
										
										
										
											2021-11-18 13:47:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-08 23:40:55 -06:00
										 |  |  |     auto const& animation_frame = m_context->animation_frames[index]; | 
					
						
							|  |  |  |     VERIFY(animation_frame.bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ImageFrameDescriptor descriptor { animation_frame.bitmap }; | 
					
						
							|  |  |  |     set_descriptor_duration(descriptor, animation_frame); | 
					
						
							|  |  |  |     return descriptor; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 07:23:59 -05:00
										 |  |  | ErrorOr<Optional<ReadonlyBytes>> PNGImageDecoderPlugin::icc_data() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-27 20:32:47 -05:00
										 |  |  |     if (!decode_png_chunks(*m_context)) | 
					
						
							|  |  |  |         return Error::from_string_literal("PNGImageDecoderPlugin: Decoding chunks failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->embedded_icc_profile.has_value()) { | 
					
						
							|  |  |  |         if (!m_context->decompressed_icc_profile.has_value()) { | 
					
						
							|  |  |  |             auto result = Compress::ZlibDecompressor::decompress_all(m_context->embedded_icc_profile->compressed_data); | 
					
						
							|  |  |  |             if (!result.has_value()) { | 
					
						
							|  |  |  |                 m_context->embedded_icc_profile.clear(); | 
					
						
							|  |  |  |                 return Error::from_string_literal("PNGImageDecoderPlugin: Decompression of ICC profile failed"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             m_context->decompressed_icc_profile = move(*result); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return m_context->decompressed_icc_profile.value(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: Eventually, look at coding_independent_code_points, chromaticities_and_whitepoint, gamma, sRGB_rendering_intent too.
 | 
					
						
							|  |  |  |     // The order is:
 | 
					
						
							|  |  |  |     // 1. Use coding_independent_code_points if it exists, ignore the rest.
 | 
					
						
							|  |  |  |     // 2. Use embedded_icc_profile if it exists, ignore the rest.
 | 
					
						
							|  |  |  |     // 3. Use sRGB_rendering_intent if it exists, ignore the rest.
 | 
					
						
							|  |  |  |     // 4. Use gamma to adjust gamma and chromaticities_and_whitepoint to adjust color.
 | 
					
						
							|  |  |  |     // (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."
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 07:23:59 -05:00
										 |  |  |     return OptionalNone {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | } |