| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Pierre Hoffmeister | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  |  * Copyright (c) 2021, Aziz Berkay Yesilyurt <abyesilyurt@gmail.com> | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | #include <AK/Concepts.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  | #include <LibCompress/Zlib.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | #include <LibCrypto/Checksum/CRC32.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							|  |  |  | #include <LibGfx/PNGWriter.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PNGChunk { | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  |     using data_length_type = u32; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |     explicit PNGChunk(String); | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     auto const& data() const { return m_data; }; | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |     String const& type() const { return m_type; }; | 
					
						
							| 
									
										
										
										
											2021-07-11 18:50:45 +02:00
										 |  |  |     void reserve(size_t bytes) { m_data.ensure_capacity(bytes); } | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     void add_as_big_endian(T); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     void add_as_little_endian(T); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     void add_u8(u8); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:54:18 +02:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     void add(T*, size_t); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 19:00:32 +02:00
										 |  |  |     void store_type(); | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  |     void store_data_length(); | 
					
						
							|  |  |  |     u32 crc(); | 
					
						
							| 
									
										
										
										
											2021-07-11 19:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     requires(IsUnsigned<T>) void add(T); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ByteBuffer m_data; | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     String m_type; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  | PNGChunk::PNGChunk(String type) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     : m_type(move(type)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  |     add<data_length_type>(0); | 
					
						
							| 
									
										
										
										
											2021-07-11 19:00:32 +02:00
										 |  |  |     store_type(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PNGChunk::store_type() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-06 03:28:46 +04:30
										 |  |  |     m_data.append(type().bytes()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  | void PNGChunk::store_data_length() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-08-01 07:24:10 +02:00
										 |  |  |     auto data_length = BigEndian<u32>(m_data.size() - sizeof(data_length_type) - m_type.length()); | 
					
						
							| 
									
										
										
										
											2021-08-01 07:23:54 +02:00
										 |  |  |     __builtin_memcpy(m_data.offset_pointer(0), &data_length, sizeof(u32)); | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | u32 PNGChunk::crc() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     u32 crc = Crypto::Checksum::CRC32({ m_data.offset_pointer(sizeof(data_length_type)), m_data.size() - sizeof(data_length_type) }).digest(); | 
					
						
							|  |  |  |     return crc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | requires(IsUnsigned<T>) void PNGChunk::add(T data) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     m_data.append(&data, sizeof(T)); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:54:18 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | void PNGChunk::add(T* data, size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_data.append(data, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | void PNGChunk::add_as_little_endian(T data) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     auto data_out = AK::convert_between_host_and_little_endian(data); | 
					
						
							|  |  |  |     add(data_out); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | void PNGChunk::add_as_big_endian(T data) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     auto data_out = AK::convert_between_host_and_big_endian(data); | 
					
						
							|  |  |  |     add(data_out); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  | void PNGChunk::add_u8(u8 data) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     add(data); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  | void PNGWriter::add_chunk(PNGChunk& png_chunk) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-11 21:33:37 +02:00
										 |  |  |     png_chunk.store_data_length(); | 
					
						
							|  |  |  |     u32 crc = png_chunk.crc(); | 
					
						
							|  |  |  |     png_chunk.add_as_big_endian(crc); | 
					
						
							|  |  |  |     m_data.append(png_chunk.data().data(), png_chunk.data().size()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PNGWriter::add_png_header() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 }; | 
					
						
							|  |  |  |     m_data.append(png_header, sizeof(png_header)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  | void PNGWriter::add_IHDR_chunk(u32 width, u32 height, u8 bit_depth, PNG::ColorType color_type, u8 compression_method, u8 filter_method, u8 interlace_method) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     PNGChunk png_chunk { "IHDR" }; | 
					
						
							| 
									
										
										
										
											2021-07-11 18:39:17 +02:00
										 |  |  |     png_chunk.add_as_big_endian(width); | 
					
						
							|  |  |  |     png_chunk.add_as_big_endian(height); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     png_chunk.add_u8(bit_depth); | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     png_chunk.add_u8(to_underlying(color_type)); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     png_chunk.add_u8(compression_method); | 
					
						
							|  |  |  |     png_chunk.add_u8(filter_method); | 
					
						
							|  |  |  |     png_chunk.add_u8(interlace_method); | 
					
						
							|  |  |  |     add_chunk(png_chunk); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PNGWriter::add_IEND_chunk() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PNGChunk png_chunk { "IEND" }; | 
					
						
							|  |  |  |     add_chunk(png_chunk); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  | void PNGWriter::add_IDAT_chunk(Gfx::Bitmap const& bitmap) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     PNGChunk png_chunk { "IDAT" }; | 
					
						
							| 
									
										
										
										
											2021-07-11 18:50:45 +02:00
										 |  |  |     png_chunk.reserve(bitmap.size_in_bytes()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  |     ByteBuffer uncompressed_block_data; | 
					
						
							|  |  |  |     uncompressed_block_data.ensure_capacity(bitmap.size_in_bytes() + bitmap.height()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |     for (int y = 0; y < bitmap.height(); ++y) { | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  |         uncompressed_block_data.append(0); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |         for (int x = 0; x < bitmap.width(); ++x) { | 
					
						
							|  |  |  |             auto pixel = bitmap.get_pixel(x, y); | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  |             uncompressed_block_data.append(pixel.red()); | 
					
						
							|  |  |  |             uncompressed_block_data.append(pixel.green()); | 
					
						
							|  |  |  |             uncompressed_block_data.append(pixel.blue()); | 
					
						
							|  |  |  |             uncompressed_block_data.append(pixel.alpha()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  |     auto maybe_zlib_buffer = Compress::ZlibCompressor::compress_all(uncompressed_block_data); | 
					
						
							|  |  |  |     if (!maybe_zlib_buffer.has_value()) { | 
					
						
							|  |  |  |         // FIXME: Handle errors.
 | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto zlib_buffer = maybe_zlib_buffer.release_value(); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 09:27:39 +02:00
										 |  |  |     png_chunk.add(zlib_buffer.data(), zlib_buffer.size()); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  |     add_chunk(png_chunk); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  | ByteBuffer PNGWriter::encode(Gfx::Bitmap const& bitmap) | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |     PNGWriter writer; | 
					
						
							|  |  |  |     writer.add_png_header(); | 
					
						
							| 
									
										
										
										
											2022-07-10 00:08:32 +02:00
										 |  |  |     writer.add_IHDR_chunk(bitmap.width(), bitmap.height(), 8, PNG::ColorType::TruecolorWithAlpha, 0, 0, 0); | 
					
						
							| 
									
										
										
										
											2021-04-19 23:43:04 +02:00
										 |  |  |     writer.add_IDAT_chunk(bitmap); | 
					
						
							|  |  |  |     writer.add_IEND_chunk(); | 
					
						
							| 
									
										
										
										
											2021-09-06 03:29:52 +04:30
										 |  |  |     // FIXME: Handle OOM failure.
 | 
					
						
							| 
									
										
										
										
											2022-01-20 17:47:39 +00:00
										 |  |  |     return ByteBuffer::copy(writer.m_data).release_value_but_fixme_should_propagate_errors(); | 
					
						
							| 
									
										
										
										
											2021-01-22 11:46:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |