From 7d93f7fc992abdf4d04e89698b8d8613ba31f8c6 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 4 May 2024 20:19:30 -0400 Subject: [PATCH] LibGfx/WebPWriter: Correctly fill in alpha_is_used hint --- .../Libraries/LibGfx/ImageFormats/WebPWriter.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp index f70a397e3f6..721c2012dd9 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/WebPWriter.cpp @@ -64,6 +64,15 @@ static ErrorOr write_VP8L_header(Stream& stream, unsigned width, unsigned return {}; } +static bool are_all_pixels_opaque(Bitmap const& bitmap) +{ + for (ARGB32 pixel : bitmap) { + if ((pixel >> 24) != 0xff) + return false; + } + return true; +} + static ErrorOr write_VP8L_image_data(Stream& stream, Bitmap const& bitmap) { LittleEndianOutputBitStream bit_stream { MaybeOwned(stream) }; @@ -249,10 +258,12 @@ static ErrorOr align_to_two(AllocatingMemoryStream& stream) ErrorOr WebPWriter::encode(Stream& stream, Bitmap const& bitmap, Options const& options) { + bool alpha_is_used_hint = !are_all_pixels_opaque(bitmap); + // The chunk headers need to know their size, so we either need a SeekableStream or need to buffer the data. We're doing the latter. // FIXME: The whole writing-and-reading-into-buffer over-and-over is awkward and inefficient. AllocatingMemoryStream vp8l_header_stream; - TRY(write_VP8L_header(vp8l_header_stream, bitmap.width(), bitmap.height(), true)); + TRY(write_VP8L_header(vp8l_header_stream, bitmap.width(), bitmap.height(), alpha_is_used_hint)); auto vp8l_header_bytes = TRY(vp8l_header_stream.read_until_eof()); AllocatingMemoryStream vp8l_data_stream;