From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#69602: 29.1; Image :map should adjust with :scale and :rotation Date: Thu, 07 Mar 2024 05:53:11 -0800 Message-ID: <871q8lk2o7.fsf@breatheoutbreathe.in> References: <87msramv72.fsf@breatheoutbreathe.in> <86jzmejzfd.fsf@gnu.org> <87a5namqyz.fsf@breatheoutbreathe.in> <86cys63281.fsf@gnu.org> <87wmqelakx.fsf@breatheoutbreathe.in> <86bk7q2xye.fsf@gnu.org> Reply-To: Joseph Turner Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28400"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 69602@debbugs.gnu.org, stephen.berman@gmx.net, juri@linkov.net To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Mar 08 01:07:55 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1riNmE-0007Be-M5 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 08 Mar 2024 01:07:55 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1riNm0-00006V-I3; Thu, 07 Mar 2024 19:07:41 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1riNlr-0008Vm-EQ for bug-gnu-emacs@gnu.org; Thu, 07 Mar 2024 19:07:32 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1riNlr-0008Qr-6W for bug-gnu-emacs@gnu.org; Thu, 07 Mar 2024 19:07:31 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1riNmL-0002G4-VI for bug-gnu-emacs@gnu.org; Thu, 07 Mar 2024 19:08:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Joseph Turner Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 08 Mar 2024 00:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69602 X-GNU-PR-Package: emacs Original-Received: via spool by 69602-submit@debbugs.gnu.org id=B69602.17098564578650 (code B ref 69602); Fri, 08 Mar 2024 00:08:01 +0000 Original-Received: (at 69602) by debbugs.gnu.org; 8 Mar 2024 00:07:37 +0000 Original-Received: from localhost ([127.0.0.1]:55504 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1riNlu-0002FQ-N3 for submit@debbugs.gnu.org; Thu, 07 Mar 2024 19:07:37 -0500 Original-Received: from out-174.mta1.migadu.com ([95.215.58.174]:59037) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1riNlp-0002FA-QG for 69602@debbugs.gnu.org; Thu, 07 Mar 2024 19:07:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=breatheoutbreathe.in; s=key1; t=1709856382; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/ad6zV8Ml+xgtMYcTNgDrjwzy/KGK55dz3wkPPkD6o8=; b=bykiTefWlsVsJog37PiX54LIqTgoxS+B/MFbx3da/veLV3tGH1Q3JebWmTAqQfycgac8ef Uqpqb3R5lcz9+GNFJ01bCy3XF99PaztP4W4+ppZm9zZZld0n/UoYXwDuVwCSsJMRNLXP0Q tT4ZBe6rDveGAz/X4VDS4yTU9VefB9s= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <86bk7q2xye.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:281195 Archived-At: --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable Eli Zaretskii writes: >> From: Joseph Turner >> Cc: 69602@debbugs.gnu.org, stephen.berman@gmx.net, juri@linkov.net >> Date: Thu, 07 Mar 2024 00:08:57 -0800 >> >> Eli Zaretskii writes: > I hoped :map allows its value to be a form that is evaluated when the > image is being processed, in which case that form could call > image-compute-scaling-factor when it produces the coordinates. Thanks! Would this require a change in C? > If that doesn't work, then... > >> When creating an image, we set its :map property according to the return >> value of `image-compute-scaling-factor'. Once the image is inserted into >> the buffer, the user may run `image-increase-size' or `image-rotate', >> which changes how the image is displayed but not its :map. >> >> Now, we need to rerun `image-compute-scaling-factor' and recompute :map. What I said here is wrong. `image-compute-scaling-factor' is not useful for recomputing :map, but `image--current-scaling' is. >> However, there is no hook which runs after the user runs those commands, >> so AFAICT there's no way for our code to know when to recompute :map. > > ...AFAIU, when an image is rescaled, we call > image-transform-properties to produce the updated image properties. There are two ways to rescale an image, `image-transform-properties' (defined in image-mode.el; works only on file-backed images in image-mode) and `image--change-size' (defined in image.el; works on any image object in any mode). For now, I'd like to focus on improving `image.el'. > So I guess you'd like that function to recompute the coordinates in > :map according to the transform? > > IOW, I don't understand why you think the problem can only be solved > in C: AFAIK almost all of the machinery that performs image transforms > is implemented in Lisp, and each time an image is rescaled, we > basically re-process the image descriptor anew. The attached patch adds two hooks in `image.el' which allow packages to recompute an image's map after it's rescaled or rotated. The following demonstrates `image-after-change-size-hooks': (progn (defun image--scale-map (map factor) "Scale MAP by FACTOR, destructively modifying it." (when (and factor (/=3D 1 factor)) (pcase-dolist (`(,`(,type . ,coords) ,_id ,_plist) map) (pcase-exhaustive type ('rect (setf (caar coords) (round (* (caar coords) factor))) (setf (cdar coords) (round (* (cdar coords) factor))) (setf (cadr coords) (round (* (cadr coords) factor))) (setf (cddr coords) (round (* (cddr coords) factor)))) ('circle (setf (caar coords) (round (* (caar coords) factor))) (setf (cdar coords) (round (* (cdar coords) factor))) (setf (cdr coords) (round (* (cdr coords) factor)))) ('poly (dotimes (i (length coords)) (aset coords i (round (* (aref coords i) factor)))))))) map) (defun image-rescale-image-map () "Recalculate and set :map property of image at point. Assumes that image has an :unscaled-map property." (when-let* ((image (image--get-imagemagick-and-warn)) (unscaled-image (image--image-without-parameters image)) (unscaled-map (image-property image :unscaled-map)) (scale (image--current-scaling image unscaled-image))) (setf (image-property image :map) (image--scale-map (copy-tree unscaled-map t) scale)))) (with-current-buffer (get-buffer-create "*image-properties-test*") (let* ((svg-string "\n\n\n\n\n\norggraphview\n\n\n\na\n\n\nHover me!\n\n\n\n\n\n") (scale 0.75) ; Adjust initial image scale (unscaled-map '(((circle (85 . 85) . 80) "1" (help-echo "Surpris= e!")))) (map (image--scale-map (copy-tree unscaled-map t) scale)) (image (create-image svg-string 'svg t :scale scale :map map :unscaled-map unscaled-map)= )) (add-hook 'image-after-change-size-hooks #'image-rescale-image-map ni= l t) (erase-buffer) (insert-image image) (goto-char (point-min)) (pop-to-buffer (current-buffer))))) After applying the attached patch, evaluate the above form, press "i +" and "i -" repeatedly to see the image and its map scale together. Thanks! Joseph --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-image-after-change-size-hooks-image-after-rotate.patch >From ace6449374eb92f820388c5d111daaa2dbd89835 Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Thu, 7 Mar 2024 15:24:49 -0800 Subject: [PATCH] Add image-after-change-size-hooks, image-after-rotate-hooks With these hooks, image properties, notably :map, may now be recalculated and set when an image is rotated or rescaled. See #69602. * lisp/image.el (image-after-change-size-hooks): Add hooks variable. (image--change-size): Run hooks at end of function. (image-after-rotate-hooks): Add hooks variable. (image-rotate): Run hooks at end of function. --- lisp/image.el | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lisp/image.el b/lisp/image.el index d2f8868b60e..053fb436cd5 100644 --- a/lisp/image.el +++ b/lisp/image.el @@ -1286,7 +1286,11 @@ image--change-size (new-image (image--image-without-parameters image)) (scale (image--current-scaling image new-image))) (setcdr image (cdr new-image)) - (plist-put (cdr image) :scale (* scale factor)))) + (plist-put (cdr image) :scale (* scale factor)) + (run-hooks 'image-after-change-size-hooks))) + +(defvar image-after-change-size-hooks nil + "Hooks run after image is rescaled.") (advice-add #'image--change-size :after #'image--change-size-scale-map) @@ -1355,9 +1359,13 @@ image-rotate ;; We don't want to exceed 360 degrees rotation, ;; because it's not seen as valid in Exif data. 360)))) + (run-hooks 'image-after-rotate-hooks) (set-transient-map image--repeat-map nil nil "Use %k for further adjustments")) +(defvar image-after-rotate-hooks nil + "Hooks run after image is rotated.") + (defun image-save () "Save the image under point. This writes the original image data to a file. Rotating or -- 2.41.0 --=-=-=--