| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 21:41:10 +01:00
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-30 13:46:03 +02:00
										 |  |  | #include <AK/FileSystemPath.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <AK/MappedFile.h>
 | 
					
						
							|  |  |  | #include <AK/NetworkOrdered.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-23 23:43:37 +01:00
										 |  |  | #include <LibCore/puff.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/PNGLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <serenity.h>
 | 
					
						
							|  |  |  | #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-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 }; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |     ByteBuffer data {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct [[gnu::packed]] PaletteEntry | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u8 r; | 
					
						
							|  |  |  |     u8 g; | 
					
						
							|  |  |  |     u8 b; | 
					
						
							|  |  |  |     //u8 a;
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct [[gnu::packed]] Triplet | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u8 r; | 
					
						
							|  |  |  |     u8 g; | 
					
						
							|  |  |  |     u8 b; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct [[gnu::packed]] Triplet16 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u16 r; | 
					
						
							|  |  |  |     u16 g; | 
					
						
							|  |  |  |     u16 b; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct [[gnu::packed]] Quad16 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u16 r; | 
					
						
							|  |  |  |     u16 g; | 
					
						
							|  |  |  |     u16 b; | 
					
						
							|  |  |  |     u16 a; | 
					
						
							| 
									
										
										
										
											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 }; | 
					
						
							|  |  |  |     u8 bytes_per_pixel { 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; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u8* decompression_buffer { nullptr }; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     int decompression_buffer_size { 0 }; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Streamer { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     Streamer(const u8* data, int size) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         : m_original_data(data) | 
					
						
							|  |  |  |         , m_original_size(size) | 
					
						
							|  |  |  |         , m_data_ptr(data) | 
					
						
							|  |  |  |         , m_size_remaining(size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     bool read(T& value) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-06-22 14:41:11 +02:00
										 |  |  |         if (m_size_remaining < (int)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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     bool read_bytes(u8* buffer, int 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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |     bool wrap_bytes(ByteBuffer& buffer, int count) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_size_remaining < count) | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-04-23 13:00:53 +02:00
										 |  |  |         buffer = ByteBuffer::wrap(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: | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     const u8* m_original_data; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     int m_original_size; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     const u8* m_data_ptr; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     int m_size_remaining; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | static RefPtr<Gfx::Bitmap> load_png_impl(const u8*, int); | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool process_chunk(Streamer&, PNGLoadingContext& context, bool decode_size_only); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | RefPtr<Gfx::Bitmap> load_png(const StringView& path) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-03 14:15:35 +02:00
										 |  |  |     MappedFile mapped_file(path); | 
					
						
							|  |  |  |     if (!mapped_file.is_valid()) | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |     auto bitmap = load_png_impl((const u8*)mapped_file.data(), mapped_file.size()); | 
					
						
							| 
									
										
										
										
											2019-04-30 13:46:03 +02:00
										 |  |  |     if (bitmap) | 
					
						
							| 
									
										
										
										
											2020-02-06 12:07:05 +01:00
										 |  |  |         bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PNG: %s", bitmap->width(), bitmap->height(), canonicalized_path(path).characters())); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2020-02-06 12:07:05 +01:00
										 |  |  |         bitmap->set_mmap_name(String::format("Gfx::Bitmap [%dx%d] - Decoded PNG: <memory>", bitmap->width(), bitmap->height())); | 
					
						
							| 
									
										
										
										
											2019-09-16 00:32:16 +10:00
										 |  |  |     return bitmap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  | [[gnu::always_inline]] static inline 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02: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-02-06 11:56:38 +01:00
										 |  |  | [[gnu::always_inline]] static inline 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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  | [[gnu::noinline]] static void unfilter(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     // First unpack the scanlines to RGBA:
 | 
					
						
							|  |  |  |     switch (context.color_type) { | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         if (context.bit_depth == 8) { | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |                 auto* triplets = (Triplet*)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; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else if (context.bit_depth == 16) { | 
					
						
							|  |  |  |             for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |                 auto* triplets = (Triplet16*)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 & 0xFF; | 
					
						
							|  |  |  |                     pixel.g = triplets[i].g & 0xFF; | 
					
						
							|  |  |  |                     pixel.b = triplets[i].b & 0xFF; | 
					
						
							|  |  |  |                     pixel.a = 0xff; | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         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-01-21 15:40:14 +01:00
										 |  |  |                 auto* triplets = (Quad16*)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 { | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:55 +02:00
										 |  |  |             ASSERT_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         for (int y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |             auto* palette_index = (u8*)context.scanlines[y].data.data(); | 
					
						
							|  |  |  |             for (int i = 0; i < context.width; ++i) { | 
					
						
							|  |  |  |                 auto& pixel = (Pixel&)context.bitmap->scanline(y)[i]; | 
					
						
							|  |  |  |                 auto& color = context.palette_data.at((int)palette_index[i]); | 
					
						
							| 
									
										
										
										
											2020-02-25 14:49:47 +01:00
										 |  |  |                 auto transparency = context.palette_transparency_data.size() >= palette_index[i] + 1u | 
					
						
							|  |  |  |                     ? context.palette_transparency_data.data()[palette_index[i]] | 
					
						
							|  |  |  |                     : 0xff; | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |                 pixel.r = color.r; | 
					
						
							|  |  |  |                 pixel.g = color.g; | 
					
						
							|  |  |  |                 pixel.b = color.b; | 
					
						
							|  |  |  |                 pixel.a = transparency; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 16:55:31 +01:00
										 |  |  |     auto dummy_scanline = ByteBuffer::create_zeroed(context.width * sizeof(RGBA32)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<true, 0>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-22 00:19:53 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<false, 0>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<true, 1>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<false, 1>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 2) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<true, 2>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<false, 2>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 3) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<true, 3>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<false, 3>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (filter == 4) { | 
					
						
							|  |  |  |             if (context.has_alpha()) | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<true, 4>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             else | 
					
						
							| 
									
										
										
										
											2019-09-30 08:57:01 +02:00
										 |  |  |                 unfilter_impl<false, 4>(*context.bitmap, y, dummy_scanline.data()); | 
					
						
							| 
									
										
										
										
											2019-03-21 16:40:40 +01:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (memcmp(context.data, png_header, sizeof(png_header)) != 0) { | 
					
						
							|  |  |  |         dbg() << "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()) { | 
					
						
							|  |  |  |         if (!process_chunk(streamer, context, true)) { | 
					
						
							| 
									
										
										
										
											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()) { | 
					
						
							|  |  |  |         if (!process_chunk(streamer, context, false)) { | 
					
						
							| 
									
										
										
										
											2019-10-19 17:43:47 +02:00
										 |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::ChunksDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool decode_png_bitmap(PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (context.state < PNGLoadingContext::State::ChunksDecoded) { | 
					
						
							|  |  |  |         if (!decode_png_chunks(context)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (context.state >= PNGLoadingContext::State::BitmapDecoded) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 15:40:14 +01:00
										 |  |  |     unsigned long srclen = context.compressed_data.size() - 6; | 
					
						
							|  |  |  |     unsigned long destlen = context.decompression_buffer_size; | 
					
						
							|  |  |  |     int ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen); | 
					
						
							|  |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     context.compressed_data.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.scanlines.ensure_capacity(context.height); | 
					
						
							|  |  |  |     Streamer streamer(context.decompression_buffer, context.decompression_buffer_size); | 
					
						
							|  |  |  |     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-01-21 15:40:14 +01:00
										 |  |  |         context.scanlines.append({ filter }); | 
					
						
							|  |  |  |         auto& scanline_buffer = context.scanlines.last().data; | 
					
						
							|  |  |  |         if (!streamer.wrap_bytes(scanline_buffer, context.width * context.bytes_per_pixel)) { | 
					
						
							|  |  |  |             context.state = PNGLoadingContext::State::Error; | 
					
						
							|  |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |     context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::RGBA32 : BitmapFormat::RGB32, { context.width, context.height }); | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-21 16:19:11 +01:00
										 |  |  |     unfilter(context); | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     munmap(context.decompression_buffer, context.decompression_buffer_size); | 
					
						
							|  |  |  |     context.decompression_buffer = nullptr; | 
					
						
							|  |  |  |     context.decompression_buffer_size = 0; | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 21:48:08 +02:00
										 |  |  |     context.state = PNGLoadingContext::State::BitmapDecoded; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | static RefPtr<Gfx::Bitmap> load_png_impl(const u8* data, int 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool process_IHDR(const ByteBuffer& data, PNGLoadingContext& context, bool decode_size_only = false) | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | #ifdef 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); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: Implement Adam7 deinterlacing
 | 
					
						
							|  |  |  |     if (context.interlace_method != 0) { | 
					
						
							|  |  |  |         dbgprintf("PNGLoader::process_IHDR: Interlaced PNGs not currently supported.\n"); | 
					
						
							|  |  |  |         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.
 | 
					
						
							|  |  |  |     case 4: // Each pixel is a grayscale sample, followed by an alpha sample.
 | 
					
						
							|  |  |  |         // FIXME: Implement grayscale PNG support.
 | 
					
						
							|  |  |  |         dbgprintf("PNGLoader::process_IHDR: Unsupported grayscale format.\n"); | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |     case 2: | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         context.bytes_per_pixel = 3 * (ihdr.bit_depth / 8); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 3: // Each pixel is a palette index; a PLTE chunk must appear.
 | 
					
						
							|  |  |  |         // FIXME: Implement support for 1/2/4 bit palette based images.
 | 
					
						
							|  |  |  |         if (ihdr.bit_depth != 8) { | 
					
						
							|  |  |  |             dbgprintf("PNGLoader::process_IHDR: Unsupported index-based format (%d bpp).\n", context.bit_depth); | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         context.bytes_per_pixel = 1; | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case 6: | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  |         context.bytes_per_pixel = 4 * (ihdr.bit_depth / 8); | 
					
						
							| 
									
										
										
										
											2019-03-21 05:24:42 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |     if (!decode_size_only) { | 
					
						
							|  |  |  |         context.decompression_buffer_size = (context.width * context.height * context.bytes_per_pixel + context.height); | 
					
						
							|  |  |  |         context.decompression_buffer = (u8*)mmap_with_name(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "PNG decompression buffer"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_IDAT(const ByteBuffer& data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 02:55:28 -05:00
										 |  |  | static bool process_PLTE(const ByteBuffer& data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool process_tRNS(const ByteBuffer& data, PNGLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  | static bool process_chunk(Streamer& streamer, PNGLoadingContext& context, bool decode_size_only) | 
					
						
							| 
									
										
										
										
											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)) { | 
					
						
							|  |  |  |         printf("Bail at chunk_size\n"); | 
					
						
							|  |  |  |         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)) { | 
					
						
							|  |  |  |         printf("Bail at chunk_type\n"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-21 13:58:40 +01:00
										 |  |  |     ByteBuffer chunk_data; | 
					
						
							|  |  |  |     if (!streamer.wrap_bytes(chunk_data, chunk_size)) { | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |         printf("Bail at chunk_data\n"); | 
					
						
							|  |  |  |         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)) { | 
					
						
							|  |  |  |         printf("Bail at chunk_crc\n"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-15 23:47:42 +02:00
										 |  |  | #ifdef PNG_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  |     printf("Chunk type: '%s', size: %u, crc: %x\n", chunk_type, chunk_size, chunk_crc); | 
					
						
							| 
									
										
										
										
											2019-04-15 23:47:42 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-21 03:57:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!strcmp((const char*)chunk_type, "IHDR")) | 
					
						
							| 
									
										
										
										
											2019-10-16 20:01:11 +02:00
										 |  |  |         return process_IHDR(chunk_data, context, decode_size_only); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 19:56:49 +02:00
										 |  |  | Size 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT(m_context->bitmap); | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							|  |  |  | } |