From 33acd4e7479ca9ae3b0e93151c3016f11fde2b77 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 14 May 2024 19:04:24 -0400 Subject: [PATCH] LibGfx/WebPWriter: Extract another align_to_two() helper --- .../LibGfx/ImageFormats/WebPWriter.cpp | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp index e9bcc25a15a..75a91d6e810 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp @@ -198,6 +198,16 @@ static ErrorOr compress_VP8L_image_data(Bitmap const& bitmap) return vp8l_data_stream.read_until_eof(); } +// FIXME: Consider using LibRIFF for RIFF writing details. (It currently has no writing support.) +static ErrorOr align_to_two(Stream& stream, size_t number_of_bytes_written) +{ + // https://developers.google.com/speed/webp/docs/riff_container + // "If Chunk Size is odd, a single padding byte -- which MUST be 0 to conform with RIFF -- is added." + if (number_of_bytes_written % 2 != 0) + TRY(stream.write_value(0)); + return {}; +} + static u8 vp8x_flags_from_header(VP8XHeader const& header) { u8 flags = 0; @@ -279,11 +289,7 @@ static ErrorOr write_VP8X_chunk(Stream& stream, VP8XHeader const& header) // FIXME: Consider using LibRIFF for RIFF writing details. (It currently has no writing support.) static ErrorOr align_to_two(AllocatingMemoryStream& stream) { - // https://developers.google.com/speed/webp/docs/riff_container - // "If Chunk Size is odd, a single padding byte -- which MUST be 0 to conform with RIFF -- is added." - if (stream.used_buffer_size() % 2 != 0) - TRY(stream.write_value(0)); - return {}; + return align_to_two(stream, stream.used_buffer_size()); } ErrorOr WebPWriter::encode(Stream& stream, Bitmap const& bitmap, Options const& options) @@ -352,11 +358,7 @@ private: static ErrorOr align_to_two(SeekableStream& stream) { - // https://developers.google.com/speed/webp/docs/riff_container - // "If Chunk Size is odd, a single padding byte -- which MUST be 0 to conform with RIFF -- is added." - if (TRY(stream.tell()) % 2 != 0) - TRY(stream.write_value(0)); - return {}; + return align_to_two(stream, TRY(stream.tell())); } static ErrorOr write_ANMF_chunk(Stream& stream, ANMFChunk const& chunk)