| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-13 16:09:41 -06:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | #include <AK/Array.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-16 13:43:08 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  | #include <AK/Error.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-29 16:51:02 +01:00
										 |  |  | #include <AK/IntegralMath.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  | #include <AK/Memory.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | #include <AK/NonnullOwnPtrVector.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  | #include <AK/Try.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  | #include <LibCore/MemoryStream.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 12:04:00 +01:00
										 |  |  | #include <LibGfx/GIFLoader.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-08 12:05:14 +01:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | namespace Gfx { | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 10:30:21 +01:00
										 |  |  | // Row strides and offsets for each interlace pass.
 | 
					
						
							| 
									
										
										
										
											2022-03-13 16:09:41 -06:00
										 |  |  | static constexpr Array<int, 4> INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 }; | 
					
						
							|  |  |  | static constexpr Array<int, 4> INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 }; | 
					
						
							| 
									
										
										
										
											2021-05-21 10:30:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  | struct GIFImageDescriptor { | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:29 +02:00
										 |  |  |     u16 x { 0 }; | 
					
						
							|  |  |  |     u16 y { 0 }; | 
					
						
							|  |  |  |     u16 width { 0 }; | 
					
						
							|  |  |  |     u16 height { 0 }; | 
					
						
							|  |  |  |     bool use_global_color_map { true }; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |     bool interlaced { false }; | 
					
						
							| 
									
										
										
										
											2020-08-30 08:47:56 +01:00
										 |  |  |     Color color_map[256]; | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:29 +02:00
										 |  |  |     u8 lzw_min_code_size { 0 }; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     Vector<u8> lzw_encoded_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Fields from optional graphic control extension block
 | 
					
						
							|  |  |  |     enum DisposalMethod : u8 { | 
					
						
							|  |  |  |         None = 0, | 
					
						
							|  |  |  |         InPlace = 1, | 
					
						
							|  |  |  |         RestoreBackground = 2, | 
					
						
							|  |  |  |         RestorePrevious = 3, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     DisposalMethod disposal_method { None }; | 
					
						
							|  |  |  |     u8 transparency_index { 0 }; | 
					
						
							|  |  |  |     u16 duration { 0 }; | 
					
						
							|  |  |  |     bool transparent { false }; | 
					
						
							|  |  |  |     bool user_input { false }; | 
					
						
							| 
									
										
										
										
											2020-08-30 16:49:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const IntRect rect() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return { this->x, this->y, this->width, this->height }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct LogicalScreen { | 
					
						
							|  |  |  |     u16 width; | 
					
						
							|  |  |  |     u16 height; | 
					
						
							| 
									
										
										
										
											2020-08-30 08:47:56 +01:00
										 |  |  |     Color color_map[256]; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct GIFLoadingContext { | 
					
						
							|  |  |  |     enum State { | 
					
						
							|  |  |  |         NotDecoded = 0, | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         FrameDescriptorsLoaded, | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |         FrameComplete, | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  |     State state { NotDecoded }; | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     enum ErrorState { | 
					
						
							|  |  |  |         NoError = 0, | 
					
						
							|  |  |  |         FailedToDecodeAllFrames, | 
					
						
							|  |  |  |         FailedToDecodeAnyFrame, | 
					
						
							|  |  |  |         FailedToLoadFrameDescriptors, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ErrorState error_state { NoError }; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     u8 const* data { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  |     size_t data_size { 0 }; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     LogicalScreen logical_screen {}; | 
					
						
							|  |  |  |     u8 background_color_index { 0 }; | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |     NonnullOwnPtrVector<GIFImageDescriptor> images {}; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     size_t loops { 1 }; | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap> frame_buffer; | 
					
						
							|  |  |  |     size_t current_frame { 0 }; | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap> prev_frame_buffer; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | enum class GIFFormat { | 
					
						
							|  |  |  |     GIF87a, | 
					
						
							|  |  |  |     GIF89a, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  | static ErrorOr<GIFFormat> decode_gif_header(Core::Stream::Stream& stream) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:45:41 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-20 16:18:28 +01:00
										 |  |  |     static auto valid_header_87 = "GIF87a"sv; | 
					
						
							|  |  |  |     static auto valid_header_89 = "GIF89a"sv; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:45:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  |     Array<u8, 6> header; | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     TRY(stream.read_entire_buffer(header)); | 
					
						
							| 
									
										
										
										
											2020-04-25 17:03:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 16:18:28 +01:00
										 |  |  |     if (header.span() == valid_header_87.bytes()) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:45:41 +01:00
										 |  |  |         return GIFFormat::GIF87a; | 
					
						
							| 
									
										
										
										
											2021-11-20 16:18:28 +01:00
										 |  |  |     if (header.span() == valid_header_89.bytes()) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:45:41 +01:00
										 |  |  |         return GIFFormat::GIF89a; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |     return Error::from_string_literal("GIF header unknown"); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:45:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  | class LZWDecoder { | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  | private: | 
					
						
							|  |  |  |     static constexpr int max_code_size = 12; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     explicit LZWDecoder(Vector<u8> const& lzw_bytes, u8 min_code_size) | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         : m_lzw_bytes(lzw_bytes) | 
					
						
							|  |  |  |         , m_code_size(min_code_size) | 
					
						
							|  |  |  |         , m_original_code_size(min_code_size) | 
					
						
							| 
									
										
										
										
											2021-07-17 18:29:28 +02:00
										 |  |  |         , m_table_capacity(AK::exp2<u32>(min_code_size)) | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         init_code_table(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     u16 add_control_code() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         const u16 control_code = m_code_table.size(); | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |         m_code_table.append(Vector<u8> {}); | 
					
						
							|  |  |  |         m_original_code_table.append(Vector<u8> {}); | 
					
						
							|  |  |  |         if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |             ++m_code_size; | 
					
						
							|  |  |  |             ++m_original_code_size; | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |             m_table_capacity *= 2; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         return control_code; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void reset() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_code_table.clear(); | 
					
						
							| 
									
										
										
										
											2021-06-12 13:24:45 +02:00
										 |  |  |         m_code_table.extend(m_original_code_table); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         m_code_size = m_original_code_size; | 
					
						
							| 
									
										
										
										
											2021-07-17 18:29:28 +02:00
										 |  |  |         m_table_capacity = AK::exp2<u32>(m_code_size); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         m_output.clear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |     ErrorOr<u16> next_code() | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         size_t current_byte_index = m_current_bit_index / 8; | 
					
						
							|  |  |  |         if (current_byte_index >= m_lzw_bytes.size()) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |             return Error::from_string_literal("LZWDecoder tries to read ouf of bounds"); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 14:40:30 +01:00
										 |  |  |         // Extract the code bits using a 32-bit mask to cover the possibility that if
 | 
					
						
							|  |  |  |         // the current code size > 9 bits then the code can span 3 bytes.
 | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         u8 current_bit_offset = m_current_bit_index % 8; | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |         u32 mask = (u32)(m_table_capacity - 1) << current_bit_offset; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 17:30:09 +01:00
										 |  |  |         // Make a padded copy of the final bytes in the data to ensure we don't read past the end.
 | 
					
						
							|  |  |  |         if (current_byte_index + sizeof(mask) > m_lzw_bytes.size()) { | 
					
						
							|  |  |  |             u8 padded_last_bytes[sizeof(mask)] = { 0 }; | 
					
						
							|  |  |  |             for (int i = 0; current_byte_index + i < m_lzw_bytes.size(); ++i) { | 
					
						
							|  |  |  |                 padded_last_bytes[i] = m_lzw_bytes[current_byte_index + i]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |             u32 const* addr = (u32 const*)&padded_last_bytes; | 
					
						
							| 
									
										
										
										
											2020-04-25 17:30:09 +01:00
										 |  |  |             m_current_code = (*addr & mask) >> current_bit_offset; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-05-24 08:23:35 -06:00
										 |  |  |             u32 tmp_word; | 
					
						
							|  |  |  |             memcpy(&tmp_word, &m_lzw_bytes.at(current_byte_index), sizeof(u32)); | 
					
						
							|  |  |  |             m_current_code = (tmp_word & mask) >> current_bit_offset; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (m_current_code > m_code_table.size()) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |             dbgln_if(GIF_DEBUG, "Corrupted LZW stream, invalid code: {} at bit index {}, code table size: {}", | 
					
						
							| 
									
										
										
										
											2021-01-16 13:43:08 +01:00
										 |  |  |                 m_current_code, | 
					
						
							|  |  |  |                 m_current_bit_index, | 
					
						
							|  |  |  |                 m_code_table.size()); | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |             return Error::from_string_literal("Corrupted LZW stream, invalid code"); | 
					
						
							| 
									
										
										
										
											2020-12-28 12:31:09 +00:00
										 |  |  |         } else if (m_current_code == m_code_table.size() && m_output.is_empty()) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |             dbgln_if(GIF_DEBUG, "Corrupted LZW stream, valid new code but output buffer is empty: {} at bit index {}, code table size: {}", | 
					
						
							| 
									
										
										
										
											2021-01-16 13:43:08 +01:00
										 |  |  |                 m_current_code, | 
					
						
							|  |  |  |                 m_current_bit_index, | 
					
						
							|  |  |  |                 m_code_table.size()); | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |             return Error::from_string_literal("Corrupted LZW stream, valid new code but output buffer is empty"); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_current_bit_index += m_code_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return m_current_code; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |     Vector<u8>& get_output() | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(m_current_code <= m_code_table.size()); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         if (m_current_code < m_code_table.size()) { | 
					
						
							|  |  |  |             Vector<u8> new_entry = m_output; | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |             m_output = m_code_table.at(m_current_code); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |             new_entry.append(m_output[0]); | 
					
						
							|  |  |  |             extend_code_table(new_entry); | 
					
						
							|  |  |  |         } else if (m_current_code == m_code_table.size()) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(!m_output.is_empty()); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |             m_output.append(m_output[0]); | 
					
						
							|  |  |  |             extend_code_table(m_output); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return m_output; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     void init_code_table() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-05-30 19:31:32 +02:00
										 |  |  |         m_code_table.ensure_capacity(m_table_capacity); | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |         for (u16 i = 0; i < m_table_capacity; ++i) { | 
					
						
							| 
									
										
										
										
											2021-05-30 19:31:32 +02:00
										 |  |  |             m_code_table.unchecked_append({ (u8)i }); | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         m_original_code_table = m_code_table; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void extend_code_table(Vector<u8> const& entry) | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (entry.size() > 1 && m_code_table.size() < 4096) { | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |             m_code_table.append(entry); | 
					
						
							|  |  |  |             if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) { | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |                 ++m_code_size; | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |                 m_table_capacity *= 2; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     Vector<u8> const& m_lzw_bytes; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int m_current_bit_index { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |     Vector<Vector<u8>> m_code_table {}; | 
					
						
							|  |  |  |     Vector<Vector<u8>> m_original_code_table {}; | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     u8 m_code_size { 0 }; | 
					
						
							|  |  |  |     u8 m_original_code_size { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 19:06:14 +01:00
										 |  |  |     u32 m_table_capacity { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 12:59:11 +01:00
										 |  |  |     u16 m_current_code { 0 }; | 
					
						
							|  |  |  |     Vector<u8> m_output {}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static void copy_frame_buffer(Bitmap& dest, Bitmap const& src) | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(dest.size_in_bytes() == src.size_in_bytes()); | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |     memcpy(dest.scanline(0), src.scanline(0), dest.size_in_bytes()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static void clear_rect(Bitmap& bitmap, IntRect const& rect, Color color) | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-21 23:38:13 +02:00
										 |  |  |     auto intersection_rect = rect.intersected(bitmap.rect()); | 
					
						
							|  |  |  |     if (intersection_rect.is_empty()) | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-04 22:05:20 +01:00
										 |  |  |     ARGB32* dst = bitmap.scanline(intersection_rect.top()) + intersection_rect.left(); | 
					
						
							|  |  |  |     const size_t dst_skip = bitmap.pitch() / sizeof(ARGB32); | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 23:38:13 +02:00
										 |  |  |     for (int i = intersection_rect.height() - 1; i >= 0; --i) { | 
					
						
							|  |  |  |         fast_u32_fill(dst, color.value(), intersection_rect.width()); | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  |         dst += dst_skip; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  | static ErrorOr<void> decode_frame(GIFLoadingContext& context, size_t frame_index) | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (frame_index >= context.images.size()) { | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |         return Error::from_string_literal("frame_index size too high"); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |     if (context.state >= GIFLoadingContext::State::FrameComplete && frame_index == context.current_frame) { | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t start_frame = context.current_frame + 1; | 
					
						
							|  |  |  |     if (context.state < GIFLoadingContext::State::FrameComplete) { | 
					
						
							|  |  |  |         start_frame = 0; | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |         context.frame_buffer = TRY(Bitmap::try_create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height })); | 
					
						
							|  |  |  |         context.prev_frame_buffer = TRY(Bitmap::try_create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |     } else if (frame_index < context.current_frame) { | 
					
						
							|  |  |  |         start_frame = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (size_t i = start_frame; i <= frame_index; ++i) { | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         auto& image = context.images.at(i); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         auto const previous_image_disposal_method = i > 0 ? context.images.at(i - 1).disposal_method : GIFImageDescriptor::DisposalMethod::None; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-29 15:04:41 +01:00
										 |  |  |         if (i == 0) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |             context.frame_buffer->fill(Color::Transparent); | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |         } else if (i > 0 && image.disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious | 
					
						
							|  |  |  |             && previous_image_disposal_method != GIFImageDescriptor::DisposalMethod::RestorePrevious) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |             // This marks the start of a run of frames that once disposed should be restored to the
 | 
					
						
							|  |  |  |             // previous underlying image contents. Therefore we make a copy of the current frame
 | 
					
						
							|  |  |  |             // buffer so that it can be restored later.
 | 
					
						
							|  |  |  |             copy_frame_buffer(*context.prev_frame_buffer, *context.frame_buffer); | 
					
						
							| 
									
										
										
										
											2020-08-29 15:04:41 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |         if (previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestoreBackground) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |             // Note: RestoreBackground could be interpreted either as restoring the underlying
 | 
					
						
							|  |  |  |             // background of the entire image (e.g. container element's background-color), or the
 | 
					
						
							|  |  |  |             // background color of the GIF itself. It appears that all major browsers and most other
 | 
					
						
							|  |  |  |             // GIF decoders adhere to the former interpretation, therefore we will do the same by
 | 
					
						
							|  |  |  |             // clearing the entire frame buffer to transparent.
 | 
					
						
							| 
									
										
										
										
											2021-01-29 22:29:27 +01:00
										 |  |  |             clear_rect(*context.frame_buffer, context.images.at(i - 1).rect(), Color::Transparent); | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |         } else if (i > 0 && previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious) { | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |             // Previous frame indicated that once disposed, it should be restored to *its* previous
 | 
					
						
							|  |  |  |             // underlying image contents, therefore we restore the saved previous frame buffer.
 | 
					
						
							|  |  |  |             copy_frame_buffer(*context.frame_buffer, *context.prev_frame_buffer); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 20:03:32 +02:00
										 |  |  |         if (image.lzw_min_code_size > 8) | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |             return Error::from_string_literal("LZW minimum code size is greater than 8"); | 
					
						
							| 
									
										
										
										
											2021-05-30 20:03:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  |         LZWDecoder decoder(image.lzw_encoded_bytes, image.lzw_min_code_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add GIF-specific control codes
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         int const clear_code = decoder.add_control_code(); | 
					
						
							|  |  |  |         int const end_of_information_code = decoder.add_control_code(); | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |         auto const& color_map = image.use_global_color_map ? context.logical_screen.color_map : image.color_map; | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         int pixel_index = 0; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |         int row = 0; | 
					
						
							|  |  |  |         int interlace_pass = 0; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         while (true) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |             ErrorOr<u16> code = decoder.next_code(); | 
					
						
							|  |  |  |             if (code.is_error()) { | 
					
						
							| 
									
										
										
										
											2021-05-01 21:10:08 +02:00
										 |  |  |                 dbgln_if(GIF_DEBUG, "Unexpectedly reached end of gif frame data"); | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |                 return code.release_error(); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (code.value() == clear_code) { | 
					
						
							|  |  |  |                 decoder.reset(); | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-11-29 14:01:16 -05:00
										 |  |  |             if (code.value() == end_of_information_code) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             if (!image.width) | 
					
						
							|  |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             auto colors = decoder.get_output(); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |             for (auto const& color : colors) { | 
					
						
							| 
									
										
										
										
											2020-08-30 08:47:56 +01:00
										 |  |  |                 auto c = color_map[color]; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 21:48:15 +03:00
										 |  |  |                 int x = pixel_index % image.width + image.x; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |                 int y = row + image.y; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-01 15:26:26 +00:00
										 |  |  |                 if (context.frame_buffer->rect().contains(x, y) && (!image.transparent || color != image.transparency_index)) { | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |                     context.frame_buffer->set_pixel(x, y, c); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-06-17 21:48:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                 ++pixel_index; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |                 if (pixel_index % image.width == 0) { | 
					
						
							|  |  |  |                     if (image.interlaced) { | 
					
						
							| 
									
										
										
										
											2021-02-26 22:31:07 +00:00
										 |  |  |                         if (interlace_pass < 4) { | 
					
						
							|  |  |  |                             if (row + INTERLACE_ROW_STRIDES[interlace_pass] >= image.height) { | 
					
						
							|  |  |  |                                 ++interlace_pass; | 
					
						
							|  |  |  |                                 if (interlace_pass < 4) | 
					
						
							|  |  |  |                                     row = INTERLACE_ROW_OFFSETS[interlace_pass]; | 
					
						
							|  |  |  |                             } else { | 
					
						
							| 
									
										
										
										
											2021-02-07 11:18:55 +01:00
										 |  |  |                                 row += INTERLACE_ROW_STRIDES[interlace_pass]; | 
					
						
							| 
									
										
										
										
											2021-02-26 22:31:07 +00:00
										 |  |  |                             } | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         ++row; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |         context.current_frame = i; | 
					
						
							|  |  |  |         context.state = GIFLoadingContext::State::FrameComplete; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  | static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  |     if (context.data_size < 32) | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         return Error::from_string_literal("Size too short for GIF frame descriptors"); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     auto stream = TRY(Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { context.data, context.data_size })); | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     TRY(decode_gif_header(*stream)); | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     context.logical_screen.width = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |     context.logical_screen.height = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-25 00:19:06 +01:00
										 |  |  |     if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) { | 
					
						
							|  |  |  |         dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height); | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         return Error::from_string_literal("This GIF is too large for comfort"); | 
					
						
							| 
									
										
										
										
											2020-12-25 00:19:06 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     auto gcm_info = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |     context.background_color_index = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |     [[maybe_unused]] auto pixel_aspect_ratio = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 17:43:01 +00:00
										 |  |  |     u8 bits_per_pixel = (gcm_info & 7) + 1; | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     int color_map_entry_count = 1; | 
					
						
							|  |  |  |     for (int i = 0; i < bits_per_pixel; ++i) | 
					
						
							|  |  |  |         color_map_entry_count *= 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < color_map_entry_count; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |         u8 r = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |         u8 g = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |         u8 b = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-08-30 08:47:56 +01:00
										 |  |  |         context.logical_screen.color_map[i] = { r, g, b }; | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |     NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>(); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |         u8 sentinel = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-11-13 11:49:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 20:11:25 -05:00
										 |  |  |         if (sentinel == '!') { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |             u8 extension_type = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             u8 sub_block_length = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             Vector<u8> sub_block {}; | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             for (;;) { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |                 sub_block_length = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |                 if (sub_block_length == 0) | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |                 TRY(sub_block.try_resize(sub_block.size() + sub_block_length)); | 
					
						
							|  |  |  |                 TRY(stream->read_entire_buffer(sub_block.span().slice_from_end(sub_block_length))); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (extension_type == 0xF9) { | 
					
						
							|  |  |  |                 if (sub_block.size() != 4) { | 
					
						
							| 
									
										
										
										
											2021-05-01 21:10:08 +02:00
										 |  |  |                     dbgln_if(GIF_DEBUG, "Unexpected graphic control size"); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 u8 disposal_method = (sub_block[0] & 0x1C) >> 2; | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |                 current_image->disposal_method = (GIFImageDescriptor::DisposalMethod)disposal_method; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 u8 user_input = (sub_block[0] & 0x2) >> 1; | 
					
						
							|  |  |  |                 current_image->user_input = user_input == 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 u8 transparent = sub_block[0] & 1; | 
					
						
							|  |  |  |                 current_image->transparent = transparent == 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 12:37:17 +02:00
										 |  |  |                 u16 duration = sub_block[1] + ((u16)sub_block[2] << 8); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                 current_image->duration = duration; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 current_image->transparency_index = sub_block[3]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (extension_type == 0xFF) { | 
					
						
							|  |  |  |                 if (sub_block.size() != 14) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |                     dbgln_if(GIF_DEBUG, "Unexpected application extension size: {}", sub_block.size()); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (sub_block[11] != 1) { | 
					
						
							| 
									
										
										
										
											2021-02-07 15:33:24 +03:30
										 |  |  |                     dbgln_if(GIF_DEBUG, "Unexpected application extension format"); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 u16 loops = sub_block[12] + (sub_block[13] << 8); | 
					
						
							|  |  |  |                 context.loops = loops; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 20:11:25 -05:00
										 |  |  |         if (sentinel == ',') { | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             context.images.append(move(current_image)); | 
					
						
							|  |  |  |             auto& image = context.images.last(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |             image.x = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image.y = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image.width = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image.height = TRY(stream->read_value<LittleEndian<u16>>()); | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |             auto packed_fields = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             image.use_global_color_map = !(packed_fields & 0x80); | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |             image.interlaced = (packed_fields & 0x40) != 0; | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!image.use_global_color_map) { | 
					
						
							| 
									
										
										
										
											2021-07-17 18:29:28 +02:00
										 |  |  |                 size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1); | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 for (size_t i = 0; i < local_color_table_size; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |                     u8 r = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |                     u8 g = TRY(stream->read_value<u8>()); | 
					
						
							|  |  |  |                     u8 b = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-08-30 08:47:56 +01:00
										 |  |  |                     image.color_map[i] = { r, g, b }; | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |             image.lzw_min_code_size = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             u8 lzw_encoded_bytes_expected = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (;;) { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |                 lzw_encoded_bytes_expected = TRY(stream->read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |                 if (lzw_encoded_bytes_expected == 0) | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  |                 Array<u8, 256> buffer; | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |                 TRY(stream->read_entire_buffer(buffer.span().trim(lzw_encoded_bytes_expected))); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { | 
					
						
							|  |  |  |                     image.lzw_encoded_bytes.append(buffer[i]); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |             current_image = make<GIFImageDescriptor>(); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 20:11:25 -05:00
										 |  |  |         if (sentinel == ';') { | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         return Error::from_string_literal("Unexpected sentinel"); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     context.state = GIFLoadingContext::State::FrameDescriptorsLoaded; | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | GIFImageDecoderPlugin::GIFImageDecoderPlugin(u8 const* data, size_t size) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_context = make<GIFLoadingContext>(); | 
					
						
							|  |  |  |     m_context->data = data; | 
					
						
							|  |  |  |     m_context->data_size = size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:26:37 -06:00
										 |  |  | GIFImageDecoderPlugin::~GIFImageDecoderPlugin() = default; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | IntSize GIFImageDecoderPlugin::size() | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     if (m_context->error_state == GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors) { | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         if (load_gif_frame_descriptors(*m_context).is_error()) { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  |             return {}; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     return { m_context->logical_screen.width, m_context->logical_screen.height }; | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GIFImageDecoderPlugin::set_volatile() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-29 17:48:17 +01:00
										 |  |  |     if (m_context->frame_buffer) { | 
					
						
							|  |  |  |         m_context->frame_buffer->set_volatile(); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  | bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-24 22:49:48 +02:00
										 |  |  |     if (!m_context->frame_buffer) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return m_context->frame_buffer->set_nonvolatile(was_purged); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | bool GIFImageDecoderPlugin::initialize() | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     auto stream_or_error = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size }); | 
					
						
							|  |  |  |     if (stream_or_error.is_error()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return !decode_gif_header(*stream_or_error.value()).is_error(); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:51:00 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-22 11:58:55 +01:00
										 |  |  |     auto stream = TRY(Core::Stream::FixedMemoryStream::construct(data)); | 
					
						
							|  |  |  |     return !decode_gif_header(*stream).is_error(); | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return adopt_nonnull_own_or_enomem(new (nothrow) GIFImageDecoderPlugin(data.data(), data.size())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | bool GIFImageDecoderPlugin::is_animated() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         if (load_gif_frame_descriptors(*m_context).is_error()) { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_context->images.size() > 1; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t GIFImageDecoderPlugin::loop_count() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         if (load_gif_frame_descriptors(*m_context).is_error()) { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_context->loops; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t GIFImageDecoderPlugin::frame_count() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         if (load_gif_frame_descriptors(*m_context).is_error()) { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_context->images.size(); | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 14:29:33 +01:00
										 |  |  | ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index) | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |     if (m_context->error_state >= GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame) { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("GIFImageDecoderPlugin: Decoding failed"); | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) { | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |         if (auto result = load_gif_frame_descriptors(*m_context); result.is_error()) { | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors; | 
					
						
							| 
									
										
										
										
											2022-12-23 13:36:19 +01:00
										 |  |  |             return result.release_error(); | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  |     if (m_context->error_state == GIFLoadingContext::ErrorState::NoError) { | 
					
						
							|  |  |  |         if (auto result = decode_frame(*m_context, index); result.is_error()) { | 
					
						
							|  |  |  |             if (m_context->state < GIFLoadingContext::State::FrameComplete) { | 
					
						
							|  |  |  |                 m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame; | 
					
						
							|  |  |  |                 return result.release_error(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (auto result = decode_frame(*m_context, 0); result.is_error()) { | 
					
						
							|  |  |  |                 m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame; | 
					
						
							|  |  |  |                 return result.release_error(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames; | 
					
						
							| 
									
										
										
										
											2020-11-08 17:01:44 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ImageFrameDescriptor frame {}; | 
					
						
							| 
									
										
										
										
											2021-11-20 14:29:33 +01:00
										 |  |  |     frame.image = TRY(m_context->frame_buffer->clone()); | 
					
						
							|  |  |  |     frame.duration = m_context->images.at(index).duration * 10; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (frame.duration <= 10) { | 
					
						
							|  |  |  |         frame.duration = 100; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return frame; | 
					
						
							| 
									
										
										
										
											2020-05-03 17:12:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | } |