2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2022-03-13 22:09:41 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-19 17:18:21 +00:00
|
|
|
#include <AK/Array.h>
|
2023-11-04 21:53:54 +00:00
|
|
|
#include <AK/BitStream.h>
|
2021-01-16 12:43:08 +00:00
|
|
|
#include <AK/Debug.h>
|
2023-01-30 08:57:02 +00:00
|
|
|
#include <AK/Endian.h>
|
2022-12-22 07:00:39 +00:00
|
|
|
#include <AK/Error.h>
|
2022-01-29 15:51:02 +00:00
|
|
|
#include <AK/IntegralMath.h>
|
2021-01-29 21:29:27 +00:00
|
|
|
#include <AK/Memory.h>
|
2023-01-25 19:19:05 +00:00
|
|
|
#include <AK/MemoryStream.h>
|
2022-12-22 07:00:39 +00:00
|
|
|
#include <AK/Try.h>
|
2024-04-26 15:32:02 +00:00
|
|
|
#include <LibCompress/Lzw.h>
|
2023-03-21 18:58:06 +00:00
|
|
|
#include <LibGfx/ImageFormats/GIFLoader.h>
|
2024-09-18 16:58:20 +00:00
|
|
|
#include <LibGfx/Painter.h>
|
2019-11-23 15:48:28 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
2019-11-23 15:48:28 +00:00
|
|
|
|
2021-05-21 09:30:21 +00:00
|
|
|
// Row strides and offsets for each interlace pass.
|
2022-03-13 22:09:41 +00: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 09:30:21 +00:00
|
|
|
|
2021-05-28 05:01:52 +00:00
|
|
|
struct GIFImageDescriptor {
|
2020-08-20 17:41:29 +00:00
|
|
|
u16 x { 0 };
|
|
|
|
u16 y { 0 };
|
|
|
|
u16 width { 0 };
|
|
|
|
u16 height { 0 };
|
|
|
|
bool use_global_color_map { true };
|
2020-08-29 15:05:24 +00:00
|
|
|
bool interlaced { false };
|
2020-08-30 07:47:56 +00:00
|
|
|
Color color_map[256];
|
2020-08-20 17:41:29 +00:00
|
|
|
u8 lzw_min_code_size { 0 };
|
2024-05-08 13:38:13 +00:00
|
|
|
ByteBuffer lzw_encoded_bytes;
|
2020-05-08 11:03:20 +00: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 15:49:59 +00:00
|
|
|
|
2023-11-04 20:02:06 +00:00
|
|
|
IntRect rect() const
|
2020-08-30 15:49:59 +00:00
|
|
|
{
|
|
|
|
return { this->x, this->y, this->width, this->height };
|
|
|
|
}
|
2020-05-08 11:03:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct LogicalScreen {
|
|
|
|
u16 width;
|
|
|
|
u16 height;
|
2020-08-30 07:47:56 +00:00
|
|
|
Color color_map[256];
|
2020-05-08 11:03:20 +00:00
|
|
|
};
|
2020-04-25 12:17:06 +00:00
|
|
|
|
|
|
|
struct GIFLoadingContext {
|
2023-07-14 21:45:19 +00:00
|
|
|
GIFLoadingContext(FixedMemoryStream stream)
|
|
|
|
: stream(move(stream))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:17:06 +00:00
|
|
|
enum State {
|
|
|
|
NotDecoded = 0,
|
2020-05-08 11:03:20 +00:00
|
|
|
FrameDescriptorsLoaded,
|
2020-08-29 16:48:17 +00:00
|
|
|
FrameComplete,
|
2020-04-25 12:17:06 +00: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 21:45:19 +00:00
|
|
|
|
|
|
|
FixedMemoryStream stream;
|
|
|
|
|
2020-05-08 11:03:20 +00:00
|
|
|
LogicalScreen logical_screen {};
|
|
|
|
u8 background_color_index { 0 };
|
2023-03-06 16:16:25 +00:00
|
|
|
Vector<NonnullOwnPtr<GIFImageDescriptor>> images {};
|
2020-05-08 11:03:20 +00:00
|
|
|
size_t loops { 1 };
|
2020-08-29 16:48:17 +00:00
|
|
|
RefPtr<Gfx::Bitmap> frame_buffer;
|
|
|
|
size_t current_frame { 0 };
|
2020-08-31 12:22:38 +00:00
|
|
|
RefPtr<Gfx::Bitmap> prev_frame_buffer;
|
2020-04-25 12:17:06 +00:00
|
|
|
};
|
2020-02-06 10:56:38 +00:00
|
|
|
|
2019-11-23 15:48:28 +00:00
|
|
|
enum class GIFFormat {
|
|
|
|
GIF87a,
|
|
|
|
GIF89a,
|
|
|
|
};
|
|
|
|
|
2023-02-10 00:00:18 +00:00
|
|
|
static ErrorOr<GIFFormat> decode_gif_header(Stream& stream)
|
2020-04-25 12:45:41 +00:00
|
|
|
{
|
2021-11-20 15:18:28 +00:00
|
|
|
static auto valid_header_87 = "GIF87a"sv;
|
|
|
|
static auto valid_header_89 = "GIF89a"sv;
|
2020-04-25 12:45:41 +00:00
|
|
|
|
2020-09-19 17:18:21 +00:00
|
|
|
Array<u8, 6> header;
|
2023-03-01 14:27:35 +00:00
|
|
|
TRY(stream.read_until_filled(header));
|
2020-04-25 15:03:40 +00:00
|
|
|
|
2021-11-20 15:18:28 +00:00
|
|
|
if (header.span() == valid_header_87.bytes())
|
2020-04-25 12:45:41 +00:00
|
|
|
return GIFFormat::GIF87a;
|
2021-11-20 15:18:28 +00:00
|
|
|
if (header.span() == valid_header_89.bytes())
|
2020-04-25 12:45:41 +00:00
|
|
|
return GIFFormat::GIF89a;
|
|
|
|
|
2022-12-23 12:36:19 +00:00
|
|
|
return Error::from_string_literal("GIF header unknown");
|
2020-04-25 12:45:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static void copy_frame_buffer(Bitmap& dest, Bitmap const& src)
|
2020-08-31 12:22:38 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(dest.size_in_bytes() == src.size_in_bytes());
|
2020-08-31 12:22:38 +00:00
|
|
|
memcpy(dest.scanline(0), src.scanline(0), dest.size_in_bytes());
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static void clear_rect(Bitmap& bitmap, IntRect const& rect, Color color)
|
2021-01-29 21:29:27 +00:00
|
|
|
{
|
2021-10-21 21:38:13 +00:00
|
|
|
auto intersection_rect = rect.intersected(bitmap.rect());
|
|
|
|
if (intersection_rect.is_empty())
|
2021-01-29 21:29:27 +00:00
|
|
|
return;
|
|
|
|
|
2022-03-04 21:05:20 +00:00
|
|
|
ARGB32* dst = bitmap.scanline(intersection_rect.top()) + intersection_rect.left();
|
2023-11-04 20:02:06 +00:00
|
|
|
size_t const dst_skip = bitmap.pitch() / sizeof(ARGB32);
|
2021-01-29 21:29:27 +00:00
|
|
|
|
2021-10-21 21:38:13 +00:00
|
|
|
for (int i = intersection_rect.height() - 1; i >= 0; --i) {
|
|
|
|
fast_u32_fill(dst, color.value(), intersection_rect.width());
|
2021-01-29 21:29:27 +00:00
|
|
|
dst += dst_skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 07:00:39 +00:00
|
|
|
static ErrorOr<void> decode_frame(GIFLoadingContext& context, size_t frame_index)
|
2020-05-08 11:03:20 +00:00
|
|
|
{
|
|
|
|
if (frame_index >= context.images.size()) {
|
2022-12-22 07:00:39 +00:00
|
|
|
return Error::from_string_literal("frame_index size too high");
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 16:48:17 +00:00
|
|
|
if (context.state >= GIFLoadingContext::State::FrameComplete && frame_index == context.current_frame) {
|
2022-12-22 07:00:39 +00:00
|
|
|
return {};
|
2020-08-29 16:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t start_frame = context.current_frame + 1;
|
|
|
|
if (context.state < GIFLoadingContext::State::FrameComplete) {
|
|
|
|
start_frame = 0;
|
2023-01-20 19:06:05 +00: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 07:00:39 +00:00
|
|
|
|
2020-08-29 16:48:17 +00:00
|
|
|
} else if (frame_index < context.current_frame) {
|
|
|
|
start_frame = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = start_frame; i <= frame_index; ++i) {
|
2020-05-08 11:03:20 +00:00
|
|
|
auto& image = context.images.at(i);
|
|
|
|
|
2023-03-06 16:16:25 +00:00
|
|
|
auto const previous_image_disposal_method = i > 0 ? context.images.at(i - 1)->disposal_method : GIFImageDescriptor::DisposalMethod::None;
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2020-08-29 14:04:41 +00:00
|
|
|
if (i == 0) {
|
2024-09-18 16:58:20 +00:00
|
|
|
auto painter = Gfx::Painter::create(*context.frame_buffer);
|
|
|
|
painter->clear_rect(context.frame_buffer->rect().to_type<float>(), Color::Transparent);
|
2023-03-06 16:16:25 +00:00
|
|
|
} else if (i > 0 && image->disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious
|
2021-05-28 05:01:52 +00:00
|
|
|
&& previous_image_disposal_method != GIFImageDescriptor::DisposalMethod::RestorePrevious) {
|
2020-08-31 12:22:38 +00: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 14:04:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:01:52 +00:00
|
|
|
if (previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestoreBackground) {
|
2020-08-31 12:22:38 +00: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 16:16:25 +00:00
|
|
|
clear_rect(*context.frame_buffer, context.images[i - 1]->rect(), Color::Transparent);
|
2021-05-28 05:01:52 +00:00
|
|
|
} else if (i > 0 && previous_image_disposal_method == GIFImageDescriptor::DisposalMethod::RestorePrevious) {
|
2020-08-31 12:22:38 +00: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 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:16:25 +00:00
|
|
|
if (image->lzw_min_code_size > 8)
|
2022-12-22 07:00:39 +00:00
|
|
|
return Error::from_string_literal("LZW minimum code size is greater than 8");
|
2021-05-30 18:03:32 +00:00
|
|
|
|
2024-05-13 14:28:01 +00:00
|
|
|
auto decoded_stream = TRY(Compress::LzwDecompressor<LittleEndianInputBitStream>::decompress_all(image->lzw_encoded_bytes, image->lzw_min_code_size));
|
2020-08-31 12:22:38 +00:00
|
|
|
|
2023-03-06 16:16:25 +00:00
|
|
|
auto const& color_map = image->use_global_color_map ? context.logical_screen.color_map : image->color_map;
|
2020-08-31 12:22:38 +00:00
|
|
|
|
2020-05-08 11:03:20 +00:00
|
|
|
int pixel_index = 0;
|
2020-08-29 15:05:24 +00:00
|
|
|
int row = 0;
|
|
|
|
int interlace_pass = 0;
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2023-11-28 03:50:38 +00:00
|
|
|
if (!image->width)
|
|
|
|
continue;
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2023-11-28 03:50:38 +00:00
|
|
|
for (auto const& color : decoded_stream.bytes()) {
|
|
|
|
auto c = color_map[color];
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2023-11-28 03:50:38 +00:00
|
|
|
int x = pixel_index % image->width + image->x;
|
|
|
|
int y = row + image->y;
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2023-11-28 03:50:38 +00: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 18:48:15 +00:00
|
|
|
|
2023-11-28 03:50:38 +00: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 15:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 03:50:38 +00:00
|
|
|
} else {
|
|
|
|
++row;
|
2020-08-29 15:05:24 +00:00
|
|
|
}
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:01:44 +00:00
|
|
|
context.current_frame = i;
|
|
|
|
context.state = GIFLoadingContext::State::FrameComplete;
|
|
|
|
}
|
2020-08-29 16:48:17 +00:00
|
|
|
|
2022-12-22 07:00:39 +00:00
|
|
|
return {};
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:50:01 +00:00
|
|
|
static ErrorOr<void> load_header_and_logical_screen(GIFLoadingContext& context)
|
2019-11-23 15:48:28 +00:00
|
|
|
{
|
2023-07-14 21:45:19 +00:00
|
|
|
if (TRY(context.stream.size()) < 32)
|
2022-12-23 12:36:19 +00:00
|
|
|
return Error::from_string_literal("Size too short for GIF frame descriptors");
|
2019-11-23 15:48:28 +00:00
|
|
|
|
2023-07-14 21:45:19 +00:00
|
|
|
TRY(decode_gif_header(context.stream));
|
2020-09-19 17:18:21 +00:00
|
|
|
|
2023-07-14 21:45:19 +00: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 12:17:06 +00:00
|
|
|
|
2024-01-04 20:19:53 +00:00
|
|
|
auto packed_fields = TRY(context.stream.read_value<u8>());
|
2023-07-14 21:45:19 +00: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 15:48:28 +00:00
|
|
|
|
2024-01-04 20:19:53 +00: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 10:08:12 +00:00
|
|
|
size_t color_map_entry_count = 1 << bits_per_pixel;
|
2024-01-04 20:19:53 +00:00
|
|
|
|
2024-01-05 10:08:12 +00:00
|
|
|
for (size_t i = 0; i < color_map_entry_count; ++i) {
|
2024-01-04 20:19:53 +00: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 15:48:28 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:50:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
|
|
|
|
{
|
2021-05-28 05:01:52 +00:00
|
|
|
NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>();
|
2019-11-23 15:48:28 +00:00
|
|
|
for (;;) {
|
2023-07-14 21:45:19 +00:00
|
|
|
u8 sentinel = TRY(context.stream.read_value<u8>());
|
2020-11-13 10:49:33 +00:00
|
|
|
|
2020-11-20 01:11:25 +00:00
|
|
|
if (sentinel == '!') {
|
2023-07-14 21:45:19 +00:00
|
|
|
u8 extension_type = TRY(context.stream.read_value<u8>());
|
2019-11-23 15:48:28 +00:00
|
|
|
|
|
|
|
u8 sub_block_length = 0;
|
|
|
|
|
2020-05-08 11:03:20 +00:00
|
|
|
Vector<u8> sub_block {};
|
2019-11-23 15:48:28 +00:00
|
|
|
for (;;) {
|
2023-07-14 21:45:19 +00:00
|
|
|
sub_block_length = TRY(context.stream.read_value<u8>());
|
2019-11-23 15:48:28 +00:00
|
|
|
if (sub_block_length == 0)
|
|
|
|
break;
|
|
|
|
|
2023-01-22 10:58:55 +00:00
|
|
|
TRY(sub_block.try_resize(sub_block.size() + sub_block_length));
|
2023-07-14 21:45:19 +00:00
|
|
|
TRY(context.stream.read_until_filled(sub_block.span().slice_from_end(sub_block_length)));
|
2019-11-23 15:48:28 +00:00
|
|
|
}
|
2020-05-08 11:03:20 +00:00
|
|
|
|
|
|
|
if (extension_type == 0xF9) {
|
|
|
|
if (sub_block.size() != 4) {
|
2021-05-01 19:10:08 +00:00
|
|
|
dbgln_if(GIF_DEBUG, "Unexpected graphic control size");
|
2020-05-08 11:03:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 disposal_method = (sub_block[0] & 0x1C) >> 2;
|
2021-05-28 05:01:52 +00:00
|
|
|
current_image->disposal_method = (GIFImageDescriptor::DisposalMethod)disposal_method;
|
2020-05-08 11:03:20 +00: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 10:37:17 +00:00
|
|
|
u16 duration = sub_block[1] + ((u16)sub_block[2] << 8);
|
2020-05-08 11:03:20 +00:00
|
|
|
current_image->duration = duration;
|
|
|
|
|
|
|
|
current_image->transparency_index = sub_block[3];
|
2024-05-21 20:20:38 +00:00
|
|
|
|
|
|
|
dbgln_if(GIF_DEBUG, "Graphic control: disposal_method={}, user_input={}, transparent={}, duration={}", (int)current_image->disposal_method, current_image->user_input, current_image->transparent, current_image->duration);
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (extension_type == 0xFF) {
|
|
|
|
if (sub_block.size() != 14) {
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(GIF_DEBUG, "Unexpected application extension size: {}", sub_block.size());
|
2020-05-08 11:03:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub_block[11] != 1) {
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(GIF_DEBUG, "Unexpected application extension format");
|
2020-05-08 11:03:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 loops = sub_block[12] + (sub_block[13] << 8);
|
|
|
|
context.loops = loops;
|
2024-05-21 20:20:38 +00:00
|
|
|
|
|
|
|
dbgln_if(GIF_DEBUG, "Application extension: loops={}", context.loops);
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 15:48:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-20 01:11:25 +00:00
|
|
|
if (sentinel == ',') {
|
2020-05-08 11:03:20 +00:00
|
|
|
context.images.append(move(current_image));
|
|
|
|
auto& image = context.images.last();
|
|
|
|
|
2023-07-14 21:45:19 +00: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 17:18:21 +00:00
|
|
|
|
2023-07-14 21:45:19 +00:00
|
|
|
auto packed_fields = TRY(context.stream.read_value<u8>());
|
2020-08-20 17:41:52 +00:00
|
|
|
|
2023-03-06 16:16:25 +00:00
|
|
|
image->use_global_color_map = !(packed_fields & 0x80);
|
|
|
|
image->interlaced = (packed_fields & 0x40) != 0;
|
2020-08-20 17:41:52 +00:00
|
|
|
|
2024-05-12 01:13:40 +00: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 16:16:25 +00:00
|
|
|
if (!image->use_global_color_map) {
|
2021-07-17 16:29:28 +00:00
|
|
|
size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1);
|
2020-08-20 17:41:52 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < local_color_table_size; ++i) {
|
2023-07-14 21:45:19 +00: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 16:16:25 +00:00
|
|
|
image->color_map[i] = { r, g, b };
|
2020-08-20 17:41:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:45:19 +00:00
|
|
|
image->lzw_min_code_size = TRY(context.stream.read_value<u8>());
|
2019-11-23 15:48:28 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2024-05-08 13:27:32 +00:00
|
|
|
auto const lzw_encoded_bytes_expected = TRY(context.stream.read_value<u8>());
|
|
|
|
|
|
|
|
// Block terminator
|
2019-11-23 15:48:28 +00:00
|
|
|
if (lzw_encoded_bytes_expected == 0)
|
|
|
|
break;
|
|
|
|
|
2024-05-08 13:38:13 +00: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 15:48:28 +00:00
|
|
|
}
|
2020-05-08 11:03:20 +00:00
|
|
|
|
2021-05-28 05:01:52 +00:00
|
|
|
current_image = make<GIFImageDescriptor>();
|
2019-11-23 15:48:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-20 01:11:25 +00:00
|
|
|
if (sentinel == ';') {
|
2019-11-23 15:48:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-23 12:36:19 +00:00
|
|
|
return Error::from_string_literal("Unexpected sentinel");
|
2019-11-23 15:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 11:03:20 +00:00
|
|
|
context.state = GIFLoadingContext::State::FrameDescriptorsLoaded;
|
2022-12-23 12:36:19 +00:00
|
|
|
return {};
|
2020-04-25 12:17:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:45:19 +00:00
|
|
|
GIFImageDecoderPlugin::GIFImageDecoderPlugin(FixedMemoryStream stream)
|
2020-04-25 12:29:12 +00:00
|
|
|
{
|
2023-07-14 21:45:19 +00:00
|
|
|
m_context = make<GIFLoadingContext>(move(stream));
|
2020-04-25 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 19:26:37 +00:00
|
|
|
GIFImageDecoderPlugin::~GIFImageDecoderPlugin() = default;
|
2020-04-25 12:29:12 +00:00
|
|
|
|
2020-06-10 08:57:59 +00:00
|
|
|
IntSize GIFImageDecoderPlugin::size()
|
2020-04-25 12:29:12 +00:00
|
|
|
{
|
2020-05-08 11:03:20 +00:00
|
|
|
return { m_context->logical_screen.width, m_context->logical_screen.height };
|
2020-04-25 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:02:50 +00:00
|
|
|
bool GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
|
2023-01-20 08:13:14 +00:00
|
|
|
{
|
2023-01-30 10:05:43 +00:00
|
|
|
FixedMemoryStream stream { data };
|
|
|
|
return !decode_gif_header(stream).is_error();
|
2023-01-20 08:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data)
|
|
|
|
{
|
2023-07-14 21:45:19 +00:00
|
|
|
FixedMemoryStream stream { data };
|
2023-07-14 22:01:43 +00: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 08:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 16:12:54 +00: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 11:03:20 +00:00
|
|
|
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
|
2022-12-23 12:36:19 +00: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 11:03:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_context->images.size() > 1;
|
2020-05-03 16:12:54 +00: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 11:03:20 +00:00
|
|
|
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
|
2022-12-23 12:36:19 +00: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 11:03:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_context->loops;
|
2020-05-03 16:12:54 +00: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 11:03:20 +00:00
|
|
|
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
|
2022-12-23 12:36:19 +00: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 11:03:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_context->images.size();
|
2020-05-03 16:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:41:22 +00:00
|
|
|
size_t GIFImageDecoderPlugin::first_animated_frame_index()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-02 21:20:06 +00:00
|
|
|
ErrorOr<ImageFrameDescriptor> GIFImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
|
2020-05-03 16:12:54 +00: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 16:12:54 +00:00
|
|
|
}
|
2020-05-08 11:03:20 +00:00
|
|
|
|
|
|
|
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
|
2022-12-23 12:36:19 +00: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 12:36:19 +00:00
|
|
|
return result.release_error();
|
2020-05-08 11:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 07:00:39 +00: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 11:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageFrameDescriptor frame {};
|
2021-11-20 13:29:33 +00:00
|
|
|
frame.image = TRY(m_context->frame_buffer->clone());
|
2023-03-06 16:16:25 +00:00
|
|
|
frame.duration = m_context->images[index]->duration * 10;
|
2020-05-08 11:03:20 +00:00
|
|
|
|
|
|
|
if (frame.duration <= 10) {
|
|
|
|
frame.duration = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
2020-05-03 16:12:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
}
|