Subject: bug#38394: Fwd: Use different image filtering when zooming in vs zooming out

From: Alan Third <[email-masked]>
Date: Sun, 2 Aug 2020 21:35:15 +0100

On Sun, Aug 02, 2020 at 07:52:25PM +0200, Lars Ingebrigtsen wrote:
> Alan Third <[email-masked]> writes:
> 
> > It might be worth smoothing on scaling down, but not on scaling up.
> > That way if you zoom in you get exact pixels, but zooming out you
> > don’t get aliasing effects.
> >
> > I don’t know if that would add any other problems...
> 
> You apparently didn't apply this patch?

There didn't appear to be much enthusiasm so I just left it in case
someone requested it in the future.

> > * src/image.c (image_set_transform [HAVE_XRENDER]): Use different filter
> > when zooming in vs zooming out.
> 
> I think it makes sense...  although I'm not sure of what the effects are
> if you just increase the image size by, say, a couple percent.

I've attached an updated version of this patch and a screenshot
showing the result with a couple of levels of scaling.

Annoyingly we now use Cairo by default which is unaffected by this
change, so only plain X users will see this behaviour.
-- 
Alan Third
>From bdd090bf8cc29bd5427d69f10bf0738a74596ba7 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 2 Aug 2020 20:43:56 +0100
Subject: [PATCH] Don't smooth images when scaling up (bug#38394)

* src/image.c (image_set_transform [HAVE_XRENDER]): Use different filter
when scaling up vs scaling down.
---
 src/image.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/image.c b/src/image.c
index e7e0a93313..ce4c34df7b 100644
--- a/src/image.c
+++ b/src/image.c
@@ -2114,6 +2114,15 @@ image_set_transform (struct frame *f, struct image *img)
   double rotation = 0.0;
   compute_image_rotation (img, &rotation);
 
+# if !defined USE_CAIRO && defined HAVE_XRENDER
+  /* We want scale up operations to use a nearest neighbour filter to
+     show real pixels instead of munging them, but scale down
+     operations to use a blended filter, to avoid aliasing and the like.
+
+     TODO: implement for Cairo, NS and Windows.  */
+  bool scale_down = (width < img->width) || (height < img->height);
+# endif
+
   /* Perform scale transformation.  */
 
   matrix3x3 matrix
@@ -2246,14 +2255,14 @@ image_set_transform (struct frame *f, struct image *img)
              XDoubleToFixed (matrix[1][2]),
              XDoubleToFixed (matrix[2][2])}}};
 
-      XRenderSetPictureFilter (FRAME_X_DISPLAY (f), img->picture, FilterBest,
-			       0, 0);
+      XRenderSetPictureFilter (FRAME_X_DISPLAY (f), img->picture,
+                               scale_down ? FilterBest : FilterNearest, 0, 0);
       XRenderSetPictureTransform (FRAME_X_DISPLAY (f), img->picture, &tmat);
 
       if (img->mask_picture)
         {
           XRenderSetPictureFilter (FRAME_X_DISPLAY (f), img->mask_picture,
-                                   FilterBest, 0, 0);
+                                   scale_down ? FilterBest : FilterNearest, 0, 0);
           XRenderSetPictureTransform (FRAME_X_DISPLAY (f), img->mask_picture,
                                       &tmat);
         }
-- 
2.26.1