From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Third Newsgroups: gmane.emacs.devel Subject: Re: Image transformation filter for upscaled images Date: Tue, 9 Mar 2021 18:12:34 +0000 Message-ID: References: <871rcpo3nm.fsf@gnus.org> <87v9a1l4d7.fsf@gnus.org> <87zgzclb5w.fsf@gnus.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="0zbkJx6TEnFFOX3w" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31666"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Evgeny Zajcev , Stefan Kangas , emacs-devel To: Lars Ingebrigtsen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Mar 09 20:56:06 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lJiSb-00087Y-Q7 for ged-emacs-devel@m.gmane-mx.org; Tue, 09 Mar 2021 20:56:05 +0100 Original-Received: from localhost ([::1]:52262 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJiSa-0008IN-QS for ged-emacs-devel@m.gmane-mx.org; Tue, 09 Mar 2021 14:56:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45278) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJgqc-0007Ht-Fd for emacs-devel@gnu.org; Tue, 09 Mar 2021 13:12:46 -0500 Original-Received: from outbound.soverin.net ([2a01:4f8:fff0:2d:8::218]:51667) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJgqY-0003Zi-H6 for emacs-devel@gnu.org; Tue, 09 Mar 2021 13:12:45 -0500 Original-Received: from smtp.soverin.net (unknown [10.10.3.24]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by outbound.soverin.net (Postfix) with ESMTPS id CA221600E9; Tue, 9 Mar 2021 18:12:37 +0000 (UTC) Original-Received: from smtp.soverin.net (smtp.soverin.net [159.69.232.138]) by soverin.net DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=idiocy.org; s=soverin; t=1615313557; bh=OBPOFIKMmE+dqBG2R7zzDzlIIm9yXpxbane9XYwi5Wc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=SNXoaW84oI+uEQf27QfciQ0nEBw4l6HMdO+ADfhstE/LPwJSqvggYmfnnC9rXhGyo mNc68yhL3oU3t7gnpiREe1WALLr1ea/BZca78CGqYon08aCrz3lk2ESyxA4XWUBO5L hkKmd1Bwjt0nFWPW5Qjj5OGok5+Hqg2XFB1/VkgjHPXr433BDv234dR45dYIj28fem TZbXjdvdFJnH8f8CJLcfWjQ3lvBHDNNHIL+o9J9lbwXodN+1q9HybCu+zz4noKwSvT KSY9Ir5nMGqokzpGaENvVra2kTquXYvEbgvgV/1Yd3Dxt/vin2iuVydJOUqOtMxg0f NsA9+i5M2lK0Q== Original-Received: by breton.holly.idiocy.org (Postfix, from userid 501) id 7521F202ADFAC3; Tue, 9 Mar 2021 18:12:34 +0000 (GMT) Mail-Followup-To: Alan Third , Lars Ingebrigtsen , Evgeny Zajcev , Stefan Kangas , emacs-devel Content-Disposition: inline In-Reply-To: <87zgzclb5w.fsf@gnus.org> Received-SPF: pass client-ip=2a01:4f8:fff0:2d:8::218; envelope-from=alan@idiocy.org; helo=outbound.soverin.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:266252 Archived-At: --0zbkJx6TEnFFOX3w Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Mar 09, 2021 at 02:16:43PM +0100, Lars Ingebrigtsen wrote: > Evgeny Zajcev writes: > > > I vote for "best" everywhere by default, making doc-view shine out-of-box > > doc-view can call create-image with :filter 'best, so what's best for > doc-view is pretty much irrelevant as to what the default should be. > > The current default works pretty well for most images (just not on > text), so I think it makes sense to not change the default. Attached is a patch that should allow us to set smoothing using ":transform-smoothing t", or nil to disable it and use nearest neighbour. Also sm in image mode lets you set it directly, and doc-view will always use it. I'm not sure about the term "smoothing", but it seemed better than filter or similar. I dunno. Anyway, it needs a news entry and documentation written, but I'll do that later. -- Alan Third --0zbkJx6TEnFFOX3w Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-Enable-selectable-image-smoothing-bug-38394.patch" >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 --0zbkJx6TEnFFOX3w--