1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:57:45 +00:00

LibGfx: Port GIFLoader to Core::Stream

This commit is contained in:
Karol Kosek 2023-01-22 11:58:55 +01:00 committed by Andreas Kling
parent 86b1b49d9a
commit 5aaa39e64e

View file

@ -10,9 +10,9 @@
#include <AK/Error.h> #include <AK/Error.h>
#include <AK/IntegralMath.h> #include <AK/IntegralMath.h>
#include <AK/Memory.h> #include <AK/Memory.h>
#include <AK/MemoryStream.h>
#include <AK/NonnullOwnPtrVector.h> #include <AK/NonnullOwnPtrVector.h>
#include <AK/Try.h> #include <AK/Try.h>
#include <LibCore/MemoryStream.h>
#include <LibGfx/GIFLoader.h> #include <LibGfx/GIFLoader.h>
#include <string.h> #include <string.h>
@ -88,15 +88,13 @@ enum class GIFFormat {
GIF89a, GIF89a,
}; };
static ErrorOr<GIFFormat> decode_gif_header(InputMemoryStream& stream) static ErrorOr<GIFFormat> decode_gif_header(Core::Stream::Stream& stream)
{ {
static auto valid_header_87 = "GIF87a"sv; static auto valid_header_87 = "GIF87a"sv;
static auto valid_header_89 = "GIF89a"sv; static auto valid_header_89 = "GIF89a"sv;
Array<u8, 6> header; Array<u8, 6> header;
stream >> header; TRY(stream.read_entire_buffer(header));
TRY(stream.try_handle_any_error());
if (header.span() == valid_header_87.bytes()) if (header.span() == valid_header_87.bytes())
return GIFFormat::GIF87a; return GIFFormat::GIF87a;
@ -382,36 +380,21 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
if (context.data_size < 32) if (context.data_size < 32)
return Error::from_string_literal("Size too short for GIF frame descriptors"); return Error::from_string_literal("Size too short for GIF frame descriptors");
InputMemoryStream stream { { context.data, context.data_size } }; auto stream = TRY(Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { context.data, context.data_size }));
TRY(decode_gif_header(stream)); TRY(decode_gif_header(*stream));
LittleEndian<u16> value; context.logical_screen.width = TRY(stream->read_value<LittleEndian<u16>>());
context.logical_screen.height = TRY(stream->read_value<LittleEndian<u16>>());
stream >> value;
context.logical_screen.width = value;
stream >> value;
context.logical_screen.height = value;
TRY(stream.try_handle_any_error());
if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) { 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); dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height);
return Error::from_string_literal("This GIF is too large for comfort"); return Error::from_string_literal("This GIF is too large for comfort");
} }
u8 gcm_info = 0; auto gcm_info = TRY(stream->read_value<u8>());
stream >> gcm_info; context.background_color_index = TRY(stream->read_value<u8>());
[[maybe_unused]] auto pixel_aspect_ratio = TRY(stream->read_value<u8>());
TRY(stream.try_handle_any_error());
stream >> context.background_color_index;
TRY(stream.try_handle_any_error());
u8 pixel_aspect_ratio = 0;
stream >> pixel_aspect_ratio;
TRY(stream.try_handle_any_error());
u8 bits_per_pixel = (gcm_info & 7) + 1; u8 bits_per_pixel = (gcm_info & 7) + 1;
int color_map_entry_count = 1; int color_map_entry_count = 1;
@ -419,45 +402,29 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
color_map_entry_count *= 2; color_map_entry_count *= 2;
for (int i = 0; i < color_map_entry_count; ++i) { for (int i = 0; i < color_map_entry_count; ++i) {
u8 r = 0; u8 r = TRY(stream->read_value<u8>());
u8 g = 0; u8 g = TRY(stream->read_value<u8>());
u8 b = 0; u8 b = TRY(stream->read_value<u8>());
stream >> r >> g >> b;
context.logical_screen.color_map[i] = { r, g, b }; context.logical_screen.color_map[i] = { r, g, b };
} }
TRY(stream.try_handle_any_error());
NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>(); NonnullOwnPtr<GIFImageDescriptor> current_image = make<GIFImageDescriptor>();
for (;;) { for (;;) {
u8 sentinel = 0; u8 sentinel = TRY(stream->read_value<u8>());
stream >> sentinel;
TRY(stream.try_handle_any_error());
if (sentinel == '!') { if (sentinel == '!') {
u8 extension_type = 0; u8 extension_type = TRY(stream->read_value<u8>());
stream >> extension_type;
TRY(stream.try_handle_any_error());
u8 sub_block_length = 0; u8 sub_block_length = 0;
Vector<u8> sub_block {}; Vector<u8> sub_block {};
for (;;) { for (;;) {
stream >> sub_block_length; sub_block_length = TRY(stream->read_value<u8>());
TRY(stream.try_handle_any_error());
if (sub_block_length == 0) if (sub_block_length == 0)
break; break;
u8 dummy = 0; TRY(sub_block.try_resize(sub_block.size() + sub_block_length));
for (u16 i = 0; i < sub_block_length; ++i) { TRY(stream->read_entire_buffer(sub_block.span().slice_from_end(sub_block_length)));
stream >> dummy;
sub_block.append(dummy);
}
TRY(stream.try_handle_any_error());
} }
if (extension_type == 0xF9) { if (extension_type == 0xF9) {
@ -503,24 +470,12 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
context.images.append(move(current_image)); context.images.append(move(current_image));
auto& image = context.images.last(); auto& image = context.images.last();
LittleEndian<u16> tmp; 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>>());
u8 packed_fields { 0 }; auto packed_fields = TRY(stream->read_value<u8>());
stream >> tmp;
image.x = tmp;
stream >> tmp;
image.y = tmp;
stream >> tmp;
image.width = tmp;
stream >> tmp;
image.height = tmp;
stream >> packed_fields;
TRY(stream.try_handle_any_error());
image.use_global_color_map = !(packed_fields & 0x80); image.use_global_color_map = !(packed_fields & 0x80);
image.interlaced = (packed_fields & 0x40) != 0; image.interlaced = (packed_fields & 0x40) != 0;
@ -529,31 +484,24 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1); size_t local_color_table_size = AK::exp2<size_t>((packed_fields & 7) + 1);
for (size_t i = 0; i < local_color_table_size; ++i) { for (size_t i = 0; i < local_color_table_size; ++i) {
u8 r = 0; u8 r = TRY(stream->read_value<u8>());
u8 g = 0; u8 g = TRY(stream->read_value<u8>());
u8 b = 0; u8 b = TRY(stream->read_value<u8>());
stream >> r >> g >> b;
image.color_map[i] = { r, g, b }; image.color_map[i] = { r, g, b };
} }
} }
stream >> image.lzw_min_code_size; image.lzw_min_code_size = TRY(stream->read_value<u8>());
TRY(stream.try_handle_any_error());
u8 lzw_encoded_bytes_expected = 0; u8 lzw_encoded_bytes_expected = 0;
for (;;) { for (;;) {
stream >> lzw_encoded_bytes_expected; lzw_encoded_bytes_expected = TRY(stream->read_value<u8>());
TRY(stream.try_handle_any_error());
if (lzw_encoded_bytes_expected == 0) if (lzw_encoded_bytes_expected == 0)
break; break;
Array<u8, 256> buffer; Array<u8, 256> buffer;
stream >> buffer.span().trim(lzw_encoded_bytes_expected); TRY(stream->read_entire_buffer(buffer.span().trim(lzw_encoded_bytes_expected)));
TRY(stream.try_handle_any_error());
for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
image.lzw_encoded_bytes.append(buffer[i]); image.lzw_encoded_bytes.append(buffer[i]);
@ -616,14 +564,16 @@ bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool GIFImageDecoderPlugin::initialize() bool GIFImageDecoderPlugin::initialize()
{ {
InputMemoryStream stream { { m_context->data, m_context->data_size } }; auto stream_or_error = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size });
return !decode_gif_header(stream).is_error(); if (stream_or_error.is_error())
return false;
return !decode_gif_header(*stream_or_error.value()).is_error();
} }
ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data) ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
{ {
InputMemoryStream stream { { data.data(), data.size() } }; auto stream = TRY(Core::Stream::FixedMemoryStream::construct(data));
return !decode_gif_header(stream).is_error(); return !decode_gif_header(*stream).is_error();
} }
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data) ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> GIFImageDecoderPlugin::create(ReadonlyBytes data)