| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 17:53:54 -04:00
										 |  |  | #include <AK/BitStream.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-16 13:43:08 +01:00
										 |  |  | #include <AK/Debug.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-30 09:57:02 +01:00
										 |  |  | #include <AK/Endian.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>
 | 
					
						
							| 
									
										
										
										
											2023-01-25 20:19:05 +01:00
										 |  |  | #include <AK/MemoryStream.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  | #include <AK/Try.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-04 16:04:40 -04:00
										 |  |  | #include <LibCompress/LZWDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:58:06 -04:00
										 |  |  | #include <LibGfx/ImageFormats/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 }; | 
					
						
							| 
									
										
										
										
											2024-05-08 09:38:13 -04:00
										 |  |  |     ByteBuffer lzw_encoded_bytes; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 16:02:06 -04:00
										 |  |  |     IntRect rect() const | 
					
						
							| 
									
										
										
										
											2020-08-30 16:49:59 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         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 { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     GIFLoadingContext(FixedMemoryStream stream) | 
					
						
							|  |  |  |         : stream(move(stream)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  |     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 }; | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     FixedMemoryStream stream; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  |     LogicalScreen logical_screen {}; | 
					
						
							|  |  |  |     u8 background_color_index { 0 }; | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |     Vector<NonnullOwnPtr<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-02-10 01:00:18 +01:00
										 |  |  | static ErrorOr<GIFFormat> decode_gif_header(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-03-01 15:27:35 +01:00
										 |  |  |     TRY(stream.read_until_filled(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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2023-11-04 16:02:06 -04:00
										 |  |  |     size_t const 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; | 
					
						
							| 
									
										
										
										
											2023-01-20 20:06:05 +01:00
										 |  |  |         context.frame_buffer = TRY(Bitmap::create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height })); | 
					
						
							|  |  |  |         context.prev_frame_buffer = TRY(Bitmap::create(BitmapFormat::BGRA8888, { context.logical_screen.width, context.logical_screen.height })); | 
					
						
							| 
									
										
										
										
											2022-12-22 08:00:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01: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); | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |         } else if (i > 0 && image->disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |             && 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.
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |             clear_rect(*context.frame_buffer, context.images[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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |         auto decoded_stream = TRY(Compress::LZWDecoder<LittleEndianInputBitStream>::decode_all(image->lzw_encoded_bytes, image->lzw_min_code_size)); | 
					
						
							| 
									
										
										
										
											2020-08-31 13:22:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |         if (!image->width) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |         for (auto const& color : decoded_stream.bytes()) { | 
					
						
							|  |  |  |             auto c = color_map[color]; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |             int x = pixel_index % image->width + image->x; | 
					
						
							|  |  |  |             int y = row + image->y; | 
					
						
							| 
									
										
										
										
											2020-05-08 12:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |             if (context.frame_buffer->rect().contains(x, y) && (!image->transparent || color != image->transparency_index)) { | 
					
						
							|  |  |  |                 context.frame_buffer->set_pixel(x, y, c); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-17 21:48:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |             ++pixel_index; | 
					
						
							|  |  |  |             if (pixel_index % image->width == 0) { | 
					
						
							|  |  |  |                 if (image->interlaced) { | 
					
						
							|  |  |  |                     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 { | 
					
						
							|  |  |  |                             row += INTERLACE_ROW_STRIDES[interlace_pass]; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2023-11-27 22:50:38 -05:00
										 |  |  |                 } else { | 
					
						
							|  |  |  |                     ++row; | 
					
						
							| 
									
										
										
										
											2020-08-29 16:05:24 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:50:01 -04:00
										 |  |  | static ErrorOr<void> load_header_and_logical_screen(GIFLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     if (TRY(context.stream.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-07-14 17:45:19 -04:00
										 |  |  |     TRY(decode_gif_header(context.stream)); | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     context.logical_screen.width = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |     context.logical_screen.height = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:17:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 21:19:53 +01:00
										 |  |  |     auto packed_fields = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     context.background_color_index = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |     [[maybe_unused]] auto pixel_aspect_ratio = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-04 21:19:53 +01:00
										 |  |  |     // Global Color Table; if the flag is set, the Global Color Table will
 | 
					
						
							|  |  |  |     // immediately follow the Logical Screen Descriptor.
 | 
					
						
							|  |  |  |     bool global_color_table_flag = packed_fields & 0x80; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (global_color_table_flag) { | 
					
						
							|  |  |  |         u8 bits_per_pixel = (packed_fields & 7) + 1; | 
					
						
							| 
									
										
										
										
											2024-01-05 11:08:12 +01:00
										 |  |  |         size_t color_map_entry_count = 1 << bits_per_pixel; | 
					
						
							| 
									
										
										
										
											2024-01-04 21:19:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 11:08:12 +01:00
										 |  |  |         for (size_t i = 0; i < color_map_entry_count; ++i) { | 
					
						
							| 
									
										
										
										
											2024-01-04 21:19:53 +01:00
										 |  |  |             u8 r = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |             u8 g = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |             u8 b = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |             context.logical_screen.color_map[i] = { r, g, b }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:50:01 -04:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-28 07:01:52 +02:00
										 |  |  |     NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>(); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |         u8 sentinel = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-11-13 11:49:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 20:11:25 -05:00
										 |  |  |         if (sentinel == '!') { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |             u8 extension_type = TRY(context.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-07-14 17:45:19 -04:00
										 |  |  |                 sub_block_length = TRY(context.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)); | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |                 TRY(context.stream.read_until_filled(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-07-14 17:45:19 -04:00
										 |  |  |             image->x = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image->y = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image->width = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							|  |  |  |             image->height = TRY(context.stream.read_value<LittleEndian<u16>>()); | 
					
						
							| 
									
										
										
										
											2020-09-19 19:18:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |             auto packed_fields = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |             image->use_global_color_map = !(packed_fields & 0x80); | 
					
						
							|  |  |  |             image->interlaced = (packed_fields & 0x40) != 0; | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-11 21:13:40 -04:00
										 |  |  |             dbgln_if(GIF_DEBUG, "Image descriptor: x={}, y={}, width={}, height={}, use_global_color_map={}, local_map_size_exponent={}, interlaced={}", image->x, image->y, image->width, image->height, image->use_global_color_map, (packed_fields & 7) + 1, image->interlaced); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01: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-07-14 17:45:19 -04:00
										 |  |  |                     u8 r = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |                     u8 g = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  |                     u8 b = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |                     image->color_map[i] = { r, g, b }; | 
					
						
							| 
									
										
										
										
											2020-08-20 19:41:52 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |             image->lzw_min_code_size = TRY(context.stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for (;;) { | 
					
						
							| 
									
										
										
										
											2024-05-08 09:27:32 -04:00
										 |  |  |                 auto const lzw_encoded_bytes_expected = TRY(context.stream.read_value<u8>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Block terminator
 | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |                 if (lzw_encoded_bytes_expected == 0) | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 09:38:13 -04:00
										 |  |  |                 auto const lzw_subblock = TRY(image->lzw_encoded_bytes.get_bytes_for_writing(lzw_encoded_bytes_expected)); | 
					
						
							|  |  |  |                 TRY(context.stream.read_until_filled(lzw_subblock)); | 
					
						
							| 
									
										
										
										
											2019-11-23 16:48:28 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  | GIFImageDecoderPlugin::GIFImageDecoderPlugin(FixedMemoryStream stream) | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     m_context = make<GIFLoadingContext>(move(stream)); | 
					
						
							| 
									
										
										
										
											2020-04-25 13:29:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 18:02:50 +00:00
										 |  |  | bool GIFImageDecoderPlugin::sniff(ReadonlyBytes data) | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-30 11:05:43 +01:00
										 |  |  |     FixedMemoryStream stream { data }; | 
					
						
							|  |  |  |     return !decode_gif_header(stream).is_error(); | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-14 17:45:19 -04:00
										 |  |  |     FixedMemoryStream stream { data }; | 
					
						
							| 
									
										
										
										
											2023-07-14 18:01:43 -04:00
										 |  |  |     auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GIFImageDecoderPlugin(move(stream)))); | 
					
						
							|  |  |  |     TRY(load_header_and_logical_screen(*plugin->m_context)); | 
					
						
							|  |  |  |     return plugin; | 
					
						
							| 
									
										
										
										
											2023-01-20 10:13:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-07 20:41:22 -06:00
										 |  |  | size_t GIFImageDecoderPlugin::first_animated_frame_index() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 22:20:06 +01:00
										 |  |  | ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index, Optional<IntSize>) | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							| 
									
										
										
										
											2023-03-06 17:16:25 +01:00
										 |  |  |     frame.duration = m_context->images[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
										 |  |  | } |