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

From: Alan Third <[email-masked]>
Date: Mon, 3 Aug 2020 10:10:06 +0100

On Mon, Aug 03, 2020 at 08:10:28AM +0200, Lars Ingebrigtsen wrote:
> Alan Third <[email-masked]> writes:
> 
> > Annoyingly we now use Cairo by default which is unaffected by this
> > change, so only plain X users will see this behaviour.
> 
> Oh, I didn't know that...  so this would make the plain X users (a very
> small minority now, I guess) have different "embiggening" scaling than
> all the rest?  There's no way to make Cairo not do the ultra-smoothing
> that it does now? 

It should be possible, I just don't know how. I've never done any
Cairo work before...

Turned out to be pretty simple. Patch attached.
-- 
Alan Third
>From 6d04d11b8fbcd14bdd1f0c173169033c1bc0d422 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 2 Aug 2020 20:43:56 +0100
Subject: [PATCH v2] 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 | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/image.c b/src/image.c
index e7e0a93313..a6b24e6652 100644
--- a/src/image.c
+++ b/src/image.c
@@ -259,6 +259,8 @@ cr_put_image_to_cr_data (struct image *img)
 	  cairo_matrix_t matrix;
 	  cairo_pattern_get_matrix (img->cr_data, &matrix);
 	  cairo_pattern_set_matrix (pattern, &matrix);
+          cairo_pattern_set_filter
+            (pattern, cairo_pattern_get_filter (img->cr_data));
 	  cairo_pattern_destroy (img->cr_data);
 	}
       cairo_surface_destroy (surface);
@@ -2114,6 +2116,15 @@ image_set_transform (struct frame *f, struct image *img)
   double rotation = 0.0;
   compute_image_rotation (img, &rotation);
 
+# if 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 NS and Windows.  */
+  bool scale_down = (width < img->width) || (height < img->height);
+# endif
+
   /* Perform scale transformation.  */
 
   matrix3x3 matrix
@@ -2230,6 +2241,8 @@ image_set_transform (struct frame *f, struct image *img)
 			      matrix[1][1], matrix[2][0], matrix[2][1]};
   cairo_pattern_t *pattern = cairo_pattern_create_rgb (0, 0, 0);
   cairo_pattern_set_matrix (pattern, &cr_matrix);
+  cairo_pattern_set_filter (pattern, scale_down
+                            ? CAIRO_FILTER_BEST : CAIRO_FILTER_NEAREST);
   /* Dummy solid color pattern just to record pattern matrix.  */
   img->cr_data = pattern;
 # elif defined (HAVE_XRENDER)
@@ -2246,14 +2259,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