From 62e13bfaa703bb1b19a3c170ca13a7cfe544c97b Mon Sep 17 00:00:00 2001 From: Alan Third Date: Tue, 9 Mar 2021 18:05:10 +0000 Subject: [PATCH] Enable selectable image smoothing (bug#38394) * lisp/doc-view.el (doc-view-insert-image): Always use smoothing in docview. * lisp/image-mode.el (image-transform-smoothing): New variable. (image-mode-map): Add smoothing binding. (image-transform-properties): Apply smoothing when requested. (image-transform-set-smoothing): New function. (image-transform-reset): Reset smoothing. * src/image.c (image_set_transform): Use new :transform-smoothing attribute. (syms_of_image): Add :transform-smoothing attribute. --- lisp/doc-view.el | 2 ++ lisp/image-mode.el | 20 ++++++++++++++++++-- src/image.c | 16 +++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lisp/doc-view.el b/lisp/doc-view.el index f6fcfae453..cef09009d9 100644 --- a/lisp/doc-view.el +++ b/lisp/doc-view.el @@ -1439,6 +1439,8 @@ doc-view-insert-image (apply #'create-image file doc-view--image-type nil args) (unless (member :width args) (setq args `(,@args :width ,doc-view-image-width))) + (unless (member :transform-smoothing args) + (setq args `(,@args :transform-smoothing t))) (apply #'create-image file doc-view--image-type nil args)))) (slice (doc-view-current-slice)) (img-width (and image (car (image-size image)))) diff --git a/lisp/image-mode.el b/lisp/image-mode.el index 7384abf3b2..ba3f104f1e 100644 --- a/lisp/image-mode.el +++ b/lisp/image-mode.el @@ -95,6 +95,9 @@ image-transform-scale (defvar-local image-transform-rotation 0.0 "Rotation angle for the image in the current Image mode buffer.") +(defvar-local image-transform-smoothing nil + "Whether to use transform smoothing.") + (defvar image-transform-right-angle-fudge 0.0001 "Snap distance to a multiple of a right angle. There's no deep theory behind the default value, it should just @@ -457,6 +460,7 @@ image-mode-map (define-key map "sb" 'image-transform-fit-both) (define-key map "ss" 'image-transform-set-scale) (define-key map "sr" 'image-transform-set-rotation) + (define-key map "sm" 'image-transform-set-smoothing) (define-key map "so" 'image-transform-original) (define-key map "s0" 'image-transform-reset) @@ -523,6 +527,8 @@ image-mode-map :help "Rotate the image"] ["Set Rotation..." image-transform-set-rotation :help "Set rotation angle of the image"] + ["Set Smoothing..." image-transform-set-smoothing + :help "Toggle smoothing"] ["Original Size" image-transform-original :help "Reset image to actual size"] ["Reset to Default Size" image-transform-reset @@ -1474,7 +1480,10 @@ image-transform-properties ,@(when (cdr resized) (list :height (cdr resized))) ,@(unless (= 0.0 image-transform-rotation) - (list :rotation image-transform-rotation)))))) + (list :rotation image-transform-rotation)) + ,@(when image-transform-smoothing + (list :transform-smoothing + (string= image-transform-smoothing "smooth"))))))) (defun image-transform-set-scale (scale) "Prompt for a number, and resize the current image by that amount." @@ -1507,6 +1516,12 @@ image-transform-set-rotation (setq image-transform-rotation (float (mod rotation 360))) (image-toggle-display-image)) +(defun image-transform-set-smoothing (smoothing) + (interactive (list (completing-read "Smoothing: " + '(none smooth) nil t))) + (setq image-transform-smoothing smoothing) + (image-toggle-display-image)) + (defun image-transform-original () "Display the current image with the original (actual) size and rotation." (interactive) @@ -1519,7 +1534,8 @@ image-transform-reset (interactive) (setq image-transform-resize image-auto-resize image-transform-rotation 0.0 - image-transform-scale 1) + image-transform-scale 1 + image-transform-smoothing nil) (image-toggle-display-image)) (provide 'image-mode) diff --git a/src/image.c b/src/image.c index 8137dbea8d..95ae573354 100644 --- a/src/image.c +++ b/src/image.c @@ -2230,7 +2230,12 @@ image_set_transform (struct frame *f, struct image *img) operations to use a blended filter, to avoid aliasing and the like. TODO: implement for Windows. */ - bool scale_down = (width < img->width) || (height < img->height); + bool smoothing; + Lisp_Object s = image_spec_value (img->spec, QCtransform_smoothing, NULL); + if (!s) + smoothing = (width < img->width) || (height < img->height); + else + smoothing = !NILP (s); # endif /* Perform scale transformation. */ @@ -2344,13 +2349,13 @@ image_set_transform (struct frame *f, struct image *img) /* Under NS the transform is applied to the drawing surface at drawing time, so store it for later. */ ns_image_set_transform (img->pixmap, matrix); - ns_image_set_smoothing (img->pixmap, scale_down); + ns_image_set_smoothing (img->pixmap, smoothing); # elif defined USE_CAIRO cairo_matrix_t cr_matrix = {matrix[0][0], matrix[0][1], matrix[1][0], 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_pattern_set_filter (pattern, smoothing ? CAIRO_FILTER_BEST : CAIRO_FILTER_NEAREST); /* Dummy solid color pattern just to record pattern matrix. */ img->cr_data = pattern; @@ -2369,13 +2374,13 @@ image_set_transform (struct frame *f, struct image *img) XDoubleToFixed (matrix[2][2])}}}; XRenderSetPictureFilter (FRAME_X_DISPLAY (f), img->picture, - scale_down ? FilterBest : FilterNearest, 0, 0); + smoothing ? FilterBest : FilterNearest, 0, 0); XRenderSetPictureTransform (FRAME_X_DISPLAY (f), img->picture, &tmat); if (img->mask_picture) { XRenderSetPictureFilter (FRAME_X_DISPLAY (f), img->mask_picture, - scale_down ? FilterBest : FilterNearest, 0, 0); + smoothing ? FilterBest : FilterNearest, 0, 0); XRenderSetPictureTransform (FRAME_X_DISPLAY (f), img->mask_picture, &tmat); } @@ -10693,6 +10698,7 @@ syms_of_image (void) DEFSYM (QCrotation, ":rotation"); DEFSYM (QCmatrix, ":matrix"); DEFSYM (QCscale, ":scale"); + DEFSYM (QCtransform_smoothing, ":transform-smoothing"); DEFSYM (QCcolor_adjustment, ":color-adjustment"); DEFSYM (QCmask, ":mask"); -- 2.29.2