From 5aaa39e64efb10afae4c3be63ad161c8decd63ac Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sun, 22 Jan 2023 11:58:55 +0100 Subject: [PATCH] LibGfx: Port GIFLoader to Core::Stream --- Userland/Libraries/LibGfx/GIFLoader.cpp | 120 +++++++----------------- 1 file changed, 35 insertions(+), 85 deletions(-) diff --git a/Userland/Libraries/LibGfx/GIFLoader.cpp b/Userland/Libraries/LibGfx/GIFLoader.cpp index f491cdd451..2c18e22fad 100644 --- a/Userland/Libraries/LibGfx/GIFLoader.cpp +++ b/Userland/Libraries/LibGfx/GIFLoader.cpp @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -88,15 +88,13 @@ enum class GIFFormat { GIF89a, }; -static ErrorOr decode_gif_header(InputMemoryStream& stream) +static ErrorOr decode_gif_header(Core::Stream::Stream& stream) { static auto valid_header_87 = "GIF87a"sv; static auto valid_header_89 = "GIF89a"sv; Array header; - stream >> header; - - TRY(stream.try_handle_any_error()); + TRY(stream.read_entire_buffer(header)); if (header.span() == valid_header_87.bytes()) return GIFFormat::GIF87a; @@ -382,36 +380,21 @@ static ErrorOr load_gif_frame_descriptors(GIFLoadingContext& context) if (context.data_size < 32) 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 value; - - stream >> value; - context.logical_screen.width = value; - - stream >> value; - context.logical_screen.height = value; - - TRY(stream.try_handle_any_error()); + context.logical_screen.width = TRY(stream->read_value>()); + context.logical_screen.height = TRY(stream->read_value>()); 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); return Error::from_string_literal("This GIF is too large for comfort"); } - u8 gcm_info = 0; - stream >> gcm_info; - - 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()); + auto gcm_info = TRY(stream->read_value()); + context.background_color_index = TRY(stream->read_value()); + [[maybe_unused]] auto pixel_aspect_ratio = TRY(stream->read_value()); u8 bits_per_pixel = (gcm_info & 7) + 1; int color_map_entry_count = 1; @@ -419,45 +402,29 @@ static ErrorOr load_gif_frame_descriptors(GIFLoadingContext& context) color_map_entry_count *= 2; for (int i = 0; i < color_map_entry_count; ++i) { - u8 r = 0; - u8 g = 0; - u8 b = 0; - stream >> r >> g >> b; + u8 r = TRY(stream->read_value()); + u8 g = TRY(stream->read_value()); + u8 b = TRY(stream->read_value()); context.logical_screen.color_map[i] = { r, g, b }; } - TRY(stream.try_handle_any_error()); - NonnullOwnPtr current_image = make(); for (;;) { - u8 sentinel = 0; - stream >> sentinel; - - TRY(stream.try_handle_any_error()); + u8 sentinel = TRY(stream->read_value()); if (sentinel == '!') { - u8 extension_type = 0; - stream >> extension_type; - TRY(stream.try_handle_any_error()); + u8 extension_type = TRY(stream->read_value()); u8 sub_block_length = 0; Vector sub_block {}; for (;;) { - stream >> sub_block_length; - - TRY(stream.try_handle_any_error()); - + sub_block_length = TRY(stream->read_value()); if (sub_block_length == 0) break; - u8 dummy = 0; - for (u16 i = 0; i < sub_block_length; ++i) { - stream >> dummy; - sub_block.append(dummy); - } - - TRY(stream.try_handle_any_error()); + TRY(sub_block.try_resize(sub_block.size() + sub_block_length)); + TRY(stream->read_entire_buffer(sub_block.span().slice_from_end(sub_block_length))); } if (extension_type == 0xF9) { @@ -503,24 +470,12 @@ static ErrorOr load_gif_frame_descriptors(GIFLoadingContext& context) context.images.append(move(current_image)); auto& image = context.images.last(); - LittleEndian tmp; + image.x = TRY(stream->read_value>()); + image.y = TRY(stream->read_value>()); + image.width = TRY(stream->read_value>()); + image.height = TRY(stream->read_value>()); - u8 packed_fields { 0 }; - - 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()); + auto packed_fields = TRY(stream->read_value()); image.use_global_color_map = !(packed_fields & 0x80); image.interlaced = (packed_fields & 0x40) != 0; @@ -529,31 +484,24 @@ static ErrorOr load_gif_frame_descriptors(GIFLoadingContext& context) size_t local_color_table_size = AK::exp2((packed_fields & 7) + 1); for (size_t i = 0; i < local_color_table_size; ++i) { - u8 r = 0; - u8 g = 0; - u8 b = 0; - stream >> r >> g >> b; + u8 r = TRY(stream->read_value()); + u8 g = TRY(stream->read_value()); + u8 b = TRY(stream->read_value()); image.color_map[i] = { r, g, b }; } } - stream >> image.lzw_min_code_size; - TRY(stream.try_handle_any_error()); + image.lzw_min_code_size = TRY(stream->read_value()); u8 lzw_encoded_bytes_expected = 0; for (;;) { - stream >> lzw_encoded_bytes_expected; - - TRY(stream.try_handle_any_error()); - + lzw_encoded_bytes_expected = TRY(stream->read_value()); if (lzw_encoded_bytes_expected == 0) break; Array buffer; - stream >> buffer.span().trim(lzw_encoded_bytes_expected); - - TRY(stream.try_handle_any_error()); + TRY(stream->read_entire_buffer(buffer.span().trim(lzw_encoded_bytes_expected))); for (int i = 0; i < lzw_encoded_bytes_expected; ++i) { image.lzw_encoded_bytes.append(buffer[i]); @@ -616,14 +564,16 @@ bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged) bool GIFImageDecoderPlugin::initialize() { - InputMemoryStream stream { { m_context->data, m_context->data_size } }; - return !decode_gif_header(stream).is_error(); + auto stream_or_error = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size }); + if (stream_or_error.is_error()) + return false; + return !decode_gif_header(*stream_or_error.value()).is_error(); } ErrorOr GIFImageDecoderPlugin::sniff(ReadonlyBytes data) { - InputMemoryStream stream { { data.data(), data.size() } }; - return !decode_gif_header(stream).is_error(); + auto stream = TRY(Core::Stream::FixedMemoryStream::construct(data)); + return !decode_gif_header(*stream).is_error(); } ErrorOr> GIFImageDecoderPlugin::create(ReadonlyBytes data)