mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:57:35 +00:00
LibGfx: Implement SmoothPixels scaling mode
If you wanted to upscale an image, you had two options: - use Nearest Neighbor: it's probably a good choice. The image stays sharp.. unless you aren't using integer scales. - use Bilinear blending, but this on the other hand, doesn't handle upscaling well. It just blurs everything. But what if we could take the best of both of them and make the image sharp on integers and just blur it a little when needed? Well, there's Smooth Pixels! This mode is similar to the Bilinear Blend, with the main difference is that the blend ratio is multiplied by the current scale, so the blur on corners can be only 1px wide. From my testing this mode doesn't handles downscaling as good as the Bilinear blending though.
This commit is contained in:
parent
c409881b5f
commit
3d7838c5fb
2 changed files with 26 additions and 1 deletions
|
@ -1100,7 +1100,7 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con
|
|||
if (clipped_src_rect.is_empty())
|
||||
return;
|
||||
|
||||
if constexpr (scaling_mode == Painter::ScalingMode::NearestNeighbor) {
|
||||
if constexpr (scaling_mode == Painter::ScalingMode::NearestNeighbor || scaling_mode == Painter::ScalingMode::SmoothPixels) {
|
||||
if (dst_rect == clipped_rect && int_src_rect == src_rect && !(dst_rect.width() % int_src_rect.width()) && !(dst_rect.height() % int_src_rect.height())) {
|
||||
int hfactor = dst_rect.width() / int_src_rect.width();
|
||||
int vfactor = dst_rect.height() / int_src_rect.height();
|
||||
|
@ -1155,6 +1155,27 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con
|
|||
auto bottom = bottom_left.interpolate(bottom_right, x_ratio);
|
||||
|
||||
src_pixel = top.interpolate(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());
|
||||
auto scaled_y1 = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom());
|
||||
auto scaled_y0 = clamp(scaled_y1 - 1, clipped_src_rect.top(), clipped_src_rect.bottom());
|
||||
|
||||
float x_ratio = (desired_x & fractional_mask) / (float)shift;
|
||||
float y_ratio = (desired_y & fractional_mask) / (float)shift;
|
||||
|
||||
float scaled_x_ratio = clamp(x_ratio * dst_rect.width() / (float)src_rect.width(), 0.0f, 1.0f);
|
||||
float scaled_y_ratio = clamp(y_ratio * dst_rect.height() / (float)src_rect.height(), 0.0f, 1.0f);
|
||||
|
||||
auto top_left = get_pixel(source, scaled_x0, scaled_y0);
|
||||
auto top_right = get_pixel(source, scaled_x1, scaled_y0);
|
||||
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);
|
||||
|
||||
src_pixel = top.interpolate(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());
|
||||
|
@ -1179,6 +1200,9 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con
|
|||
case Painter::ScalingMode::NearestNeighbor:
|
||||
do_draw_scaled_bitmap<has_alpha_channel, Painter::ScalingMode::NearestNeighbor>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
|
||||
break;
|
||||
case Painter::ScalingMode::SmoothPixels:
|
||||
do_draw_scaled_bitmap<has_alpha_channel, Painter::ScalingMode::SmoothPixels>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
|
||||
break;
|
||||
case Painter::ScalingMode::BilinearBlend:
|
||||
do_draw_scaled_bitmap<has_alpha_channel, Painter::ScalingMode::BilinearBlend>(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity);
|
||||
break;
|
||||
|
|
|
@ -36,6 +36,7 @@ public:
|
|||
|
||||
enum class ScalingMode {
|
||||
NearestNeighbor,
|
||||
SmoothPixels,
|
||||
BilinearBlend,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue