From 27cc9e7386e097d140e0e138849064116a9472a5 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 8 May 2024 17:47:21 -0400 Subject: [PATCH] LibGfx/WebPWriter: Write VP8X chunk header and bytes at once For VP8L it arguably makes sense to separate the two, but for fixed-size chunk like VP8X it doesn't. No behavior change. --- Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp index 08b4f593e41..082a016ed42 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp @@ -200,7 +200,7 @@ struct VP8XHeader { }; // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format -static ErrorOr write_VP8X_header(Stream& stream, VP8XHeader const& header) +static ErrorOr write_VP8X_chunk(Stream& stream, VP8XHeader const& header) { if (header.width > (1 << 24) || header.height > (1 << 24)) return Error::from_string_literal("WebP dimensions too large for VP8X chunk"); @@ -213,6 +213,8 @@ static ErrorOr write_VP8X_header(Stream& stream, VP8XHeader const& header) if (product >= (1ull << 32)) return Error::from_string_literal("WebP dimensions too large for VP8X chunk"); + TRY(write_chunk_header(stream, "VP8X"sv, 10)); + LittleEndianOutputBitStream bit_stream { MaybeOwned(stream) }; // Don't use bit_stream.write_bits() to write individual flags here: @@ -311,13 +313,8 @@ ErrorOr WebPWriter::encode(Stream& stream, Bitmap const& bitmap, Options c TRY(align_to_two(iccp_chunk_stream)); iccp_chunk_bytes = TRY(iccp_chunk_stream.read_until_eof()); - AllocatingMemoryStream vp8x_header_stream; - TRY(write_VP8X_header(vp8x_header_stream, { .has_icc = true, .has_alpha = alpha_is_used_hint, .width = (u32)bitmap.width(), .height = (u32)bitmap.height() })); - auto vp8x_header_bytes = TRY(vp8x_header_stream.read_until_eof()); - AllocatingMemoryStream vp8x_chunk_stream; - TRY(write_chunk_header(vp8x_chunk_stream, "VP8X"sv, vp8x_header_bytes.size())); - TRY(vp8x_chunk_stream.write_until_depleted(vp8x_header_bytes)); + TRY(write_VP8X_chunk(vp8x_chunk_stream, { .has_icc = true, .has_alpha = alpha_is_used_hint, .width = (u32)bitmap.width(), .height = (u32)bitmap.height() })); VERIFY(vp8x_chunk_stream.used_buffer_size() % 2 == 0); vp8x_chunk_bytes = TRY(vp8x_chunk_stream.read_until_eof()); }