| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2020-05-26 14:52:44 +03:00
										 |  |  | #include <AK/LexicalPath.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <AK/MappedFile.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-28 15:33:37 +01:00
										 |  |  | #include <LibCompress/Zlib.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/PNGLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-12 10:44:45 +02:00
										 |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | #include <sys/mman.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 14:34:53 -04:00
										 |  |  | #ifdef __serenity__
 | 
					
						
							| 
									
										
										
										
											2021-03-03 23:54:07 +02:00
										 |  |  | #    include <LibCompress/Deflate.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-23 14:34:53 -04:00
										 |  |  | #    include <serenity.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 }; | 
					
						
							|  |  |  |     u8 color_type { 0 }; | 
					
						
							|  |  |  |     u8 compression_method { 0 }; | 
					
						
							|  |  |  |     u8 filter_method { 0 }; | 
					
						
							|  |  |  |     u8 interlace_method { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static_assert(sizeof(PNG_IHDR) == 13); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Scanline { | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8 filter { 0 }; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     //u8 a;
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											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]] Quad { | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |         ChunksDecoded, | 
					
						
							|  |  |  |         BitmapDecoded, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     State state { State::NotDecoded }; | 
					
						
							|  |  |  |     const u8* data { nullptr }; | 
					
						
							|  |  |  |     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 }; | 
					
						
							|  |  |  |     u8 color_type { 0 }; | 
					
						
							|  |  |  |     u8 compression_method { 0 }; | 
					
						
							|  |  |  |     u8 filter_method { 0 }; | 
					
						
							|  |  |  |     u8 interlace_method { 0 }; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |     u8 channels { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     bool has_seen_zlib_header { false }; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     Vector<Scanline> scanlines; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap> bitmap; | 
					
						
							| 
									
										
										
										
											2021-05-15 15:01:38 +02:00
										 |  |  |     ByteBuffer* decompression_buffer { nullptr }; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Streamer { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  |     Streamer(const u8* 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; | 
					
						
							| 
									
										
										
										
											2019-06-22 14:41:11 +02:00
										 |  |  |         value = *((const NetworkOrdered<T>*)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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     bool at_end() const { return !m_size_remaining; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  |     const u8* m_data_ptr { nullptr }; | 
					
						
							|  |  |  |     size_t m_size_remaining { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  | static RefPtr<Gfx::Bitmap> load_png_impl(const u8*, size_t); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | static bool process_chunk(Streamer&, PNGLoadingContext& context); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 23:40:49 +02:00
										 |  |  | RefPtr<Gfx::Bitmap> load_png(String const& path) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-10 15:55:54 +01:00
										 |  |  |     auto file_or_error = MappedFile::map(path); | 
					
						
							|  |  |  |     if (file_or_error.is_error()) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2021-01-10 15:55:54 +01:00
										 |  |  |     auto bitmap = load_png_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); | 
					
						
							| 
									
										
										
										
											2019-04-30 13:46:03 +02:00
										 |  |  |     if (bitmap) | 
					
						
							| 
									
										
										
										
											2021-01-03 15:26:47 +01:00
										 |  |  |         bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); | 
					
						
							| 
									
										
										
										
											2019-04-30 13:46:03 +02:00
										 |  |  |     return bitmap; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | RefPtr<Gfx::Bitmap> load_png_from_memory(const u8* data, size_t length) | 
					
						
							| 
									
										
										
										
											2019-09-16 00:32:16 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     auto bitmap = load_png_impl(data, length); | 
					
						
							|  |  |  |     if (bitmap) | 
					
						
							| 
									
										
										
										
											2021-01-03 15:26:47 +01:00
										 |  |  |         bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: <memory>", bitmap->size())); | 
					
						
							| 
									
										
										
										
											2019-09-16 00:32:16 +10:00
										 |  |  |     return bitmap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 11:43:25 +02:00
										 |  |  | ALWAYS_INLINE static u8 paeth_predictor(int a, int b, int c) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int p = a + b - c; | 
					
						
							|  |  |  |     int pa = abs(p - a); | 
					
						
							|  |  |  |     int pb = abs(p - b); | 
					
						
							|  |  |  |     int pc = abs(p - c); | 
					
						
							|  |  |  |     if (pa <= pb && pa <= pc) | 
					
						
							|  |  |  |         return a; | 
					
						
							|  |  |  |     if (pb <= pc) | 
					
						
							|  |  |  |         return b; | 
					
						
							|  |  |  |     return c; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 22:44:54 +01:00
										 |  |  | union [[gnu::packed]] Pixel { | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     RGBA32 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
										 |  |  |     }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | static_assert(sizeof(Pixel) == 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  | template<bool has_alpha, u8 filter_type> | 
					
						
							| 
									
										
										
										
											2020-04-30 11:43:25 +02:00
										 |  |  | ALWAYS_INLINE static void unfilter_impl(Gfx::Bitmap& bitmap, int y, const void* dummy_scanline_data) | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |     auto* dummy_scanline = (const Pixel*)dummy_scanline_data; | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |     if constexpr (filter_type == 0) { | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         auto* pixels = (Pixel*)bitmap.scanline(y); | 
					
						
							|  |  |  |         for (int i = 0; i < bitmap.width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if constexpr (filter_type == 1) { | 
					
						
							|  |  |  |         auto* pixels = (Pixel*)bitmap.scanline(y); | 
					
						
							|  |  |  |         swap(pixels[0].r, pixels[0].b); | 
					
						
							|  |  |  |         for (int i = 1; i < bitmap.width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							|  |  |  |             auto& a = (const Pixel&)pixels[i - 1]; | 
					
						
							|  |  |  |             x.v[0] += a.v[0]; | 
					
						
							|  |  |  |             x.v[1] += a.v[1]; | 
					
						
							|  |  |  |             x.v[2] += a.v[2]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             if constexpr (has_alpha) | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |                 x.v[3] += a.v[3]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if constexpr (filter_type == 2) { | 
					
						
							|  |  |  |         auto* pixels = (Pixel*)bitmap.scanline(y); | 
					
						
							| 
									
										
										
										
											2020-06-11 08:48:09 +02:00
										 |  |  |         auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (const Pixel*)bitmap.scanline(y - 1); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         for (int i = 0; i < bitmap.width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             const Pixel& b = pixels_y_minus_1[i]; | 
					
						
							|  |  |  |             x.v[0] += b.v[0]; | 
					
						
							|  |  |  |             x.v[1] += b.v[1]; | 
					
						
							|  |  |  |             x.v[2] += b.v[2]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             if constexpr (has_alpha) | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |                 x.v[3] += b.v[3]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if constexpr (filter_type == 3) { | 
					
						
							|  |  |  |         auto* pixels = (Pixel*)bitmap.scanline(y); | 
					
						
							| 
									
										
										
										
											2020-06-11 08:48:09 +02:00
										 |  |  |         auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (const Pixel*)bitmap.scanline(y - 1); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         for (int i = 0; i < bitmap.width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							|  |  |  |             Pixel a; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  |             if (i != 0) | 
					
						
							|  |  |  |                 a = pixels[i - 1]; | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             const Pixel& b = pixels_y_minus_1[i]; | 
					
						
							|  |  |  |             x.v[0] = x.v[0] + ((a.v[0] + b.v[0]) / 2); | 
					
						
							|  |  |  |             x.v[1] = x.v[1] + ((a.v[1] + b.v[1]) / 2); | 
					
						
							|  |  |  |             x.v[2] = x.v[2] + ((a.v[2] + b.v[2]) / 2); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             if constexpr (has_alpha) | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |                 x.v[3] = x.v[3] + ((a.v[3] + b.v[3]) / 2); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if constexpr (filter_type == 4) { | 
					
						
							|  |  |  |         auto* pixels = (Pixel*)bitmap.scanline(y); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |         auto* pixels_y_minus_1 = y == 0 ? dummy_scanline : (Pixel*)bitmap.scanline(y - 1); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         for (int i = 0; i < bitmap.width(); ++i) { | 
					
						
							|  |  |  |             auto& x = pixels[i]; | 
					
						
							|  |  |  |             swap(x.r, x.b); | 
					
						
							|  |  |  |             Pixel a; | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             const Pixel& b = pixels_y_minus_1[i]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             Pixel c; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |             if (i != 0) { | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |                 a = pixels[i - 1]; | 
					
						
							|  |  |  |                 c = pixels_y_minus_1[i - 1]; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             x.v[0] += paeth_predictor(a.v[0], b.v[0], c.v[0]); | 
					
						
							|  |  |  |             x.v[1] += paeth_predictor(a.v[1], b.v[1], c.v[1]); | 
					
						
							|  |  |  |             x.v[2] += paeth_predictor(a.v[2], b.v[2], c.v[2]); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             if constexpr (has_alpha) | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |                 x.v[3] += paeth_predictor(a.v[3], b.v[3], c.v[3]); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |         auto* gray_values = reinterpret_cast<const T*>(context.scanlines[y].data.data()); | 
					
						
							|  |  |  |         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) { | 
					
						
							|  |  |  |         auto* tuples = reinterpret_cast<const Tuple<T>*>(context.scanlines[y].data.data()); | 
					
						
							|  |  |  |         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) { | 
					
						
							|  |  |  |         auto* triplets = reinterpret_cast<const Triplet<T>*>(context.scanlines[y].data.data()); | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  | NEVER_INLINE FLATTEN static bool unfilter(PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     // First unpack the scanlines to RGBA:
 | 
					
						
							|  |  |  |     switch (context.color_type) { | 
					
						
							| 
									
										
										
										
											2020-04-26 18:30:11 +02:00
										 |  |  |     case 0: | 
					
						
							|  |  |  |         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; | 
					
						
							| 
									
										
										
										
											2020-04-26 20:02:30 +02:00
										 |  |  |     case 4: | 
					
						
							|  |  |  |         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; | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     case 2: | 
					
						
							|  |  |  |         if (context.bit_depth == 8) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |             unpack_triplets_without_alpha<u8>(context); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else if (context.bit_depth == 16) { | 
					
						
							| 
									
										
										
										
											2020-06-13 12:51:54 -04:00
										 |  |  |             unpack_triplets_without_alpha<u16>(context); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 6: | 
					
						
							|  |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:34:56 +02:00
										 |  |  |                 auto* triplets = reinterpret_cast<const Quad<u16>*>(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]; | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |                     pixel.r = triplets[i].r & 0xFF; | 
					
						
							|  |  |  |                     pixel.g = triplets[i].g & 0xFF; | 
					
						
							|  |  |  |                     pixel.b = triplets[i].b & 0xFF; | 
					
						
							|  |  |  |                     pixel.a = triplets[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; | 
					
						
							|  |  |  |     case 3: | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							|  |  |  |                         return false; | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							|  |  |  |                         return false; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |     u8 dummy_scanline[context.width * sizeof(RGBA32)]; | 
					
						
							| 
									
										
										
										
											2021-04-07 17:21:38 +03:00
										 |  |  |     memset(dummy_scanline, 0, sizeof(dummy_scanline)); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |         auto filter = context.scanlines[y].filter; | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |         if (filter == 0) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<true, 0>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<false, 0>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |         if (filter == 1) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<true, 1>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<false, 1>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 2) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<true, 2>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<false, 2>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 3) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<true, 3>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<false, 3>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 4) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<true, 4>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2020-12-23 18:06:25 +01:00
										 |  |  |                 unfilter_impl<false, 4>(*context.bitmap, y, dummy_scanline); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-19 17:48:43 +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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (memcmp(context.data, png_header, 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const u8* data_ptr = context.data + sizeof(png_header); | 
					
						
							|  |  |  |     size_t data_remaining = context.data_size - sizeof(png_header); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Streamer streamer(data_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end()) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (context.width && context.height) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::SizeDecoded; | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const u8* data_ptr = context.data + sizeof(png_header); | 
					
						
							|  |  |  |     int data_remaining = context.data_size - sizeof(png_header); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.compressed_data.ensure_capacity(context.data_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Streamer streamer(data_ptr, data_remaining); | 
					
						
							|  |  |  |     while (!streamer.at_end()) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::ChunksDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | static bool decode_png_bitmap_simple(PNGLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-15 15:01:38 +02:00
										 |  |  |     Streamer streamer(context.decompression_buffer->data(), context.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) { | 
					
						
							|  |  |  |         u8 filter; | 
					
						
							|  |  |  |         if (!streamer.read(filter)) { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-11 09:32:17 +02:00
										 |  |  |         if (filter > 4) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |             dbgln_if(PNG_DEBUG, "Invalid PNG filter: {}", filter); | 
					
						
							| 
									
										
										
										
											2020-06-11 09:32:17 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) { | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |     context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height }); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 15:22:41 +01:00
										 |  |  |     if (!context.bitmap) { | 
					
						
							|  |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PNGLoadingContext subimage_context; | 
					
						
							|  |  |  |     subimage_context.width = adam7_width(context, pass); | 
					
						
							|  |  |  |     subimage_context.height = adam7_height(context, pass); | 
					
						
							|  |  |  |     subimage_context.channels = context.channels; | 
					
						
							|  |  |  |     subimage_context.color_type = context.color_type; | 
					
						
							|  |  |  |     subimage_context.palette_data = context.palette_data; | 
					
						
							|  |  |  |     subimage_context.palette_transparency_data = context.palette_transparency_data; | 
					
						
							|  |  |  |     subimage_context.bit_depth = context.bit_depth; | 
					
						
							|  |  |  |     subimage_context.filter_method = context.filter_method; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // For small images, some passes might be empty
 | 
					
						
							|  |  |  |     if (!subimage_context.width || !subimage_context.height) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subimage_context.scanlines.clear_with_capacity(); | 
					
						
							|  |  |  |     for (int y = 0; y < subimage_context.height; ++y) { | 
					
						
							|  |  |  |         u8 filter; | 
					
						
							|  |  |  |         if (!streamer.read(filter)) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (filter > 4) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |             dbgln_if(PNG_DEBUG, "Invalid PNG filter: {}", filter); | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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()) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         if (!streamer.wrap_bytes(scanline_buffer, row_size.value())) { | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     subimage_context.bitmap = Bitmap::create(context.bitmap->format(), { subimage_context.width, subimage_context.height }); | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  |     if (!unfilter(subimage_context)) { | 
					
						
							|  |  |  |         subimage_context.bitmap = nullptr; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool decode_png_adam7(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-15 15:01:38 +02:00
										 |  |  |     Streamer streamer(context.decompression_buffer->data(), context.decompression_buffer->size()); | 
					
						
							| 
									
										
										
										
											2021-03-16 11:48:42 +01:00
										 |  |  |     context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888, { context.width, context.height }); | 
					
						
							| 
									
										
										
										
											2020-12-20 16:04:29 +01:00
										 |  |  |     if (!context.bitmap) | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int pass = 1; pass <= 7; ++pass) { | 
					
						
							|  |  |  |         if (!decode_adam7_pass(context, streamer, pass)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool decode_png_bitmap(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (context.state < PNGLoadingContext::State::ChunksDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_chunks(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::State::BitmapDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 14:34:54 -05:00
										 |  |  |     if (context.width == -1 || context.height == -1) | 
					
						
							|  |  |  |         return false; // Didn't see an IHDR chunk.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-06 01:32:59 +01:00
										 |  |  |     if (context.color_type == 3 && context.palette_data.is_empty()) | 
					
						
							|  |  |  |         return false; // Didn't see a PLTE chunk for a palettized image, or it was empty.
 | 
					
						
							| 
									
										
										
										
											2020-11-13 11:37:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 15:33:37 +01:00
										 |  |  |     auto result = Compress::Zlib::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; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-15 15:01:38 +02:00
										 |  |  |     context.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: | 
					
						
							|  |  |  |         if (!decode_png_bitmap_simple(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case PngInterlaceMethod::Adam7: | 
					
						
							|  |  |  |         if (!decode_png_adam7(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-03-16 19:45:54 +02:00
										 |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-15 15:01:38 +02:00
										 |  |  |     context.decompression_buffer = nullptr; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::BitmapDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 18:59:58 +02:00
										 |  |  | static RefPtr<Gfx::Bitmap> load_png_impl(const u8* data, size_t data_size) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     PNGLoadingContext context; | 
					
						
							|  |  |  |     context.data = data; | 
					
						
							|  |  |  |     context.data_size = data_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!decode_png_chunks(context)) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!decode_png_bitmap(context)) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return 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-04-07 17:25:22 +03:00
										 |  |  |     if constexpr (PNG_DEBUG) { | 
					
						
							|  |  |  |         printf("PNG: %dx%d (%d bpp)\n", context.width, context.height, context.bit_depth); | 
					
						
							|  |  |  |         printf("     Color type: %d\n", context.color_type); | 
					
						
							|  |  |  |         printf("Compress Method: %d\n", context.compression_method); | 
					
						
							|  |  |  |         printf("  Filter Method: %d\n", context.filter_method); | 
					
						
							|  |  |  |         printf(" Interlace type: %d\n", 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) { | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     case 0: // Each pixel is a grayscale sample.
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |     case 2: // Each pixel is an RGB sample
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     case 3: // Each pixel is a palette index; a PLTE chunk must appear.
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2020-05-01 13:24:23 +02:00
										 |  |  |     case 6: // Each pixel is an RGB sample, followed by an alpha sample.
 | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |     context.palette_data.append((const PaletteEntry*)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) { | 
					
						
							|  |  |  |     case 3: | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     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-04-07 17:25:22 +03:00
										 |  |  |         if constexpr (PNG_DEBUG) | 
					
						
							|  |  |  |             printf("Bail at chunk_size\n"); | 
					
						
							| 
									
										
										
										
											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-04-07 17:25:22 +03:00
										 |  |  |         if constexpr (PNG_DEBUG) | 
					
						
							|  |  |  |             printf("Bail at chunk_type\n"); | 
					
						
							| 
									
										
										
										
											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-04-07 17:25:22 +03:00
										 |  |  |         if constexpr (PNG_DEBUG) | 
					
						
							|  |  |  |             printf("Bail at chunk_data\n"); | 
					
						
							| 
									
										
										
										
											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-04-07 17:25:22 +03:00
										 |  |  |         if constexpr (PNG_DEBUG) | 
					
						
							|  |  |  |             printf("Bail at chunk_crc\n"); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-07 17:25:22 +03:00
										 |  |  |     if constexpr (PNG_DEBUG) | 
					
						
							|  |  |  |         printf("Chunk type: '%s', size: %u, crc: %x\n", chunk_type, chunk_size, chunk_crc); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!strcmp((const char*)chunk_type, "IHDR")) | 
					
						
							| 
									
										
										
										
											2020-06-13 13:17:43 -04:00
										 |  |  |         return process_IHDR(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     if (!strcmp((const char*)chunk_type, "IDAT")) | 
					
						
							|  |  |  |         return process_IDAT(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     if (!strcmp((const char*)chunk_type, "PLTE")) | 
					
						
							|  |  |  |         return process_PLTE(chunk_data, context); | 
					
						
							|  |  |  |     if (!strcmp((const char*)chunk_type, "tRNS")) | 
					
						
							|  |  |  |         return process_tRNS(chunk_data, context); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  | PNGImageDecoderPlugin::PNGImageDecoderPlugin(const u8* data, size_t size) | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_context = make<PNGLoadingContext>(); | 
					
						
							|  |  |  |     m_context->data = data; | 
					
						
							|  |  |  |     m_context->data_size = size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  | PNGImageDecoderPlugin::~PNGImageDecoderPlugin() | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | RefPtr<Gfx::Bitmap> PNGImageDecoderPlugin::bitmap() | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |     if (m_context->state == PNGLoadingContext::State::Error) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (m_context->state < PNGLoadingContext::State::BitmapDecoded) { | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |         // NOTE: This forces the chunk decoding to happen.
 | 
					
						
							|  |  |  |         bool success = decode_png_bitmap(*m_context); | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |         if (!success) | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_context->bitmap); | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     return m_context->bitmap; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-18 20:50:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void PNGImageDecoderPlugin::set_volatile() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_context->bitmap) | 
					
						
							|  |  |  |         m_context->bitmap->set_volatile(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool PNGImageDecoderPlugin::set_nonvolatile() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_context->bitmap) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return m_context->bitmap->set_nonvolatile(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  | bool PNGImageDecoderPlugin::sniff() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return decode_png_header(*m_context); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | bool PNGImageDecoderPlugin::is_animated() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t PNGImageDecoderPlugin::loop_count() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t PNGImageDecoderPlugin::frame_count() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ImageFrameDescriptor PNGImageDecoderPlugin::frame(size_t i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (i > 0) { | 
					
						
							|  |  |  |         return { bitmap(), 0 }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | } |