diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp index 1deac270b1d..d58797273ff 100644 --- a/Tests/LibGfx/TestImageDecoder.cpp +++ b/Tests/LibGfx/TestImageDecoder.cpp @@ -927,6 +927,19 @@ TEST_CASE(test_webp_extended_lossless_animated) } } +TEST_CASE(test_webp_unpremultiplied_alpha) +{ + auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("webp/semi-transparent-pixel.webp"sv))); + EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); + auto plugin_decoder = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(file->bytes())); + + auto frame = TRY_OR_FAIL(expect_single_frame_of_size(*plugin_decoder, { 1, 1 })); + + // Webp decodes with unpremultiplied color data, so {R,G,B} can be >A (unlike with premultiplied colors). + EXPECT_EQ(frame.image->alpha_type(), Gfx::AlphaType::Unpremultiplied); + EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color(255, 255, 255, 128)); +} + TEST_CASE(test_tvg) { auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tvg/yak.tvg"sv))); diff --git a/Tests/LibGfx/test-inputs/webp/semi-transparent-pixel.webp b/Tests/LibGfx/test-inputs/webp/semi-transparent-pixel.webp new file mode 100644 index 00000000000..e84966543c0 Binary files /dev/null and b/Tests/LibGfx/test-inputs/webp/semi-transparent-pixel.webp differ diff --git a/Userland/Libraries/LibGfx/ImageFormats/WebPLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/WebPLoader.cpp index 9efa648fdec..6630267626a 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/WebPLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/WebPLoader.cpp @@ -139,7 +139,7 @@ static ErrorOr decode_webp_image(WebPLoadingContext& context) return Error::from_string_literal("Failed to decode animated frame"); auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888; - auto bitmap = TRY(Bitmap::create(bitmap_format, context.size)); + auto bitmap = TRY(Bitmap::create(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size)); memcpy(bitmap->scanline_u8(0), frame_data, context.size.width() * context.size.height() * 4); @@ -150,7 +150,7 @@ static ErrorOr decode_webp_image(WebPLoadingContext& context) } } else { auto bitmap_format = context.has_alpha ? BitmapFormat::BGRA8888 : BitmapFormat::BGRx8888; - auto bitmap = TRY(Bitmap::create(bitmap_format, context.size)); + auto bitmap = TRY(Bitmap::create(bitmap_format, Gfx::AlphaType::Unpremultiplied, context.size)); auto image_data = WebPDecodeBGRAInto(context.data.data(), context.data.size(), bitmap->scanline_u8(0), bitmap->data_size(), bitmap->pitch()); if (image_data == nullptr)