From 5a6b995444694db2379753de604042393913c3b0 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Thu, 16 Feb 2023 22:22:21 +0000 Subject: [PATCH] LibGfx: Use premultiplied alpha when scaling images This commit replaces usages of `Color::interpolate()` with `Color::mixed_with()` in image scaling functions. The latter uses premultiplied alpha, which results in more visually pleasing edges when images are scaled against a transparent background. These changes affect the SmoothPixels and BilinearBlend scaling modes of `Painter::draw_scaled_bitmap()` as well as the `Bitmap::scaled()` function. Fixes #17153. --- Userland/Libraries/LibGfx/Bitmap.cpp | 10 +++++----- Userland/Libraries/LibGfx/Painter.cpp | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 8f5c4c7c95..0964109c73 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -403,9 +403,9 @@ ErrorOr> Bitmap::scaled(float sx, float sy) const auto c = get_pixel(i, j + 1); auto d = get_pixel(i + 1, j + 1); - auto e = a.interpolate(b, u); - auto f = c.interpolate(d, u); - auto color = e.interpolate(f, v); + auto e = a.mixed_with(b, u); + auto f = c.mixed_with(d, u); + auto color = e.mixed_with(f, v); new_bitmap->set_pixel(x, y, color); } } @@ -421,7 +421,7 @@ ErrorOr> Bitmap::scaled(float sx, float sy) const auto a = get_pixel(i, old_bottom_y); auto b = get_pixel(i + 1, old_bottom_y); - auto color = a.interpolate(b, u); + auto color = a.mixed_with(b, u); new_bitmap->set_pixel(x, new_bottom_y, color); } @@ -437,7 +437,7 @@ ErrorOr> Bitmap::scaled(float sx, float sy) const auto c = get_pixel(old_right_x, j); auto d = get_pixel(old_right_x, j + 1); - auto color = c.interpolate(d, v); + auto color = c.mixed_with(d, v); new_bitmap->set_pixel(new_right_x, y, color); } diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 3f3ca806a6..1bac87f5f3 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -1230,10 +1230,10 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con auto bottom_left = get_pixel(source, scaled_x0, scaled_y1); auto bottom_right = get_pixel(source, scaled_x1, scaled_y1); - auto top = top_left.interpolate(top_right, x_ratio); - auto bottom = bottom_left.interpolate(bottom_right, x_ratio); + auto top = top_left.mixed_with(top_right, x_ratio); + auto bottom = bottom_left.mixed_with(bottom_right, x_ratio); - src_pixel = top.interpolate(bottom, y_ratio); + src_pixel = top.mixed_with(bottom, y_ratio); } else if constexpr (scaling_mode == Painter::ScalingMode::SmoothPixels) { auto scaled_x1 = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right()); auto scaled_x0 = clamp(scaled_x1 - 1, clipped_src_rect.left(), clipped_src_rect.right()); @@ -1251,10 +1251,10 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con auto bottom_left = get_pixel(source, scaled_x0, scaled_y1); auto bottom_right = get_pixel(source, scaled_x1, scaled_y1); - auto top = top_left.interpolate(top_right, scaled_x_ratio); - auto bottom = bottom_left.interpolate(bottom_right, scaled_x_ratio); + auto top = top_left.mixed_with(top_right, scaled_x_ratio); + auto bottom = bottom_left.mixed_with(bottom_right, scaled_x_ratio); - src_pixel = top.interpolate(bottom, scaled_y_ratio); + src_pixel = top.mixed_with(bottom, scaled_y_ratio); } else { auto scaled_x = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right()); auto scaled_y = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom());