Skip to content

Truly independent web browser

LibWeb: Paint a 1x1 background image with its alpha applied once

sideshowbarker26a111e06763
diff --git a/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp b/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp
index 3189cf3c481..6477bc9010c 100644
--- a/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp
+++ b/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp
@@ -276,7 +276,18 @@ Optional<Gfx::Color> BitmapDecodedImageData::color_if_single_pixel_bitmap() cons
auto frame = ensure_frame();
if (!frame.has_value())
return {};
- return frame->bitmap_sharing_pixels()->get_pixel(0, 0);
+ auto bitmap = frame->bitmap_sharing_pixels();
+ auto color = bitmap->get_pixel(0, 0);
+ // NB: The color fills the background like any other color, which applies its alpha, so the pixel of a
+ // premultiplied bitmap is unpremultiplied first.
+ if (bitmap->alpha_type() != Gfx::AlphaType::Premultiplied || color.alpha() == 255)
+ return color;
+ if (color.alpha() == 0)
+ return Gfx::Color(Gfx::Color::Transparent);
+ auto unpremultiplied = [alpha = color.alpha()](u8 channel) {
+ return static_cast<u8>(min(255, (channel * 255 + alpha / 2) / alpha));
+ };
+ return Gfx::Color(unpremultiplied(color.red()), unpremultiplied(color.green()), unpremultiplied(color.blue()), color.alpha());
}
Optional<Painting::ImagePaint> BitmapDecodedImageData::image_paint(Painting::ImagePaintRequest const&) const
diff --git a/Tests/LibWeb/Ref/expected/css-background-single-pixel-image-alpha-ref.html b/Tests/LibWeb/Ref/expected/css-background-single-pixel-image-alpha-ref.html
new file mode 100644
index 00000000000..b733f365b38
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css-background-single-pixel-image-alpha-ref.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ background: black;
+ }
+ div {
+ float: left;
+ width: 50px;
+ height: 50px;
+ margin-right: 10px;
+ }
+</style>
+<div style="background: rgba(255, 255, 255, 0.7608)"></div>
+<div style="background: rgba(255, 0, 0, 0.5020)"></div>
+<div style="background: rgba(10, 200, 90, 0.2353)"></div>
+<div style="background: rgba(0, 0, 255, 0.0000)"></div>
diff --git a/Tests/LibWeb/Ref/input/css-background-single-pixel-image-alpha.html b/Tests/LibWeb/Ref/input/css-background-single-pixel-image-alpha.html
new file mode 100644
index 00000000000..db42973dbc0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css-background-single-pixel-image-alpha.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/css-background-single-pixel-image-alpha-ref.html" />
+<meta name="fuzzy" content="maxDifference=0-2;totalPixels=0-10000" />
+<style>
+ body {
+ margin: 0;
+ background: black;
+ }
+ div {
+ float: left;
+ width: 50px;
+ height: 50px;
+ margin-right: 10px;
+ }
+</style>
+<!-- A 1x1 background image with partial alpha paints like a color with that alpha, once. -->
+<div style="background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4////IQAJvgPAMjuJNwAAAABJRU5ErkJggg==)"></div>
+<div style="background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DQAAAEgQGALFXOsAAAAABJRU5ErkJggg==)"></div>
+<div style="background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGPgOhFlAwADdQFpDO9UOwAAAABJRU5ErkJggg==)"></div>
+<div style="background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYPjPAAACAwEAKOj8bQAAAABJRU5ErkJggg==)"></div>