[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Image transformations

From: Lars Ingebrigtsen
Subject: Re: Image transformations
Date: Thu, 25 Jul 2019 21:40:12 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Nothing further was done here?  So just to recap for new viewers who
have recently joined us:

Currently, anybody who wants to insert an image in Emacs has to say the
equivalent of

(create-image data
              (if (or (and (fboundp 'image-transforms-p)
                      (not (fboundp 'imagemagick-types)))

to be somewhat compatible over current Emacses (because we almost always
want to scale images).  That's rather a mouthful, so Alan's suggestion
was to just do this in `create-image', but better.

I took that idea and worked it a bit more so that we never use
imagemagick unless we absolutely need to, to satisfy the user's wishes.

Does the following look OK to everybody?

diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index 3c91092906..d9ac2e0411 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -5757,10 +5757,14 @@ Defining Images
 a string containing the image data; @var{data-p} should be @code{nil}
 for the former case, non-@code{nil} for the latter case.
-The optional argument @var{type} is a symbol specifying the image type.
-If @var{type} is omitted or @code{nil}, @code{create-image} tries to
-determine the image type from the file's first few bytes, or else
-from the file's name.
+The optional argument @var{type} is a symbol specifying the image
+type.  If @var{type} is omitted or @code{nil}, @code{create-image}
+tries to determine the image type from the file's first few bytes, or
+else from the file's name.  If @var{props} specify an image transform
+(for instance, @samp{:scale}, @samp{:max-height} or @samp{rotate}),
+and @var{type} is @code{nil}, and Emacs doesn't have the requisite
+native support for that transform, and Emacs is built with ImageMagick
+support, then @var{type} will default to @var{imagemagick} instead.
 The remaining arguments, @var{props}, specify additional image
 properties---for example,
diff --git a/etc/NEWS b/etc/NEWS
index f47cf071bb..1ce7bba786 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2344,6 +2344,12 @@ The new function 'image-transforms-p' can be used to 
test whether any
 given frame supports these capabilities.
+** If `create-image' is called with a nil TYPE parameter, and a transform
+is specified (:scale, :max-height, :rotate, etc), and Emacs doesn't
+support this natively, and Emacs is built with ImageMagick support,
+then TYPE will default to `imagemagick'.
 ** '(locale-info 'paper)' now returns the paper size on systems that support 
 This is currently supported on GNUish hosts and on modern versions of
diff --git a/lisp/image.el b/lisp/image.el
index c3e28655c3..379fda8a51 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -421,6 +421,11 @@ create-image
 of image data.  If that doesn't work, and FILE-OR-DATA is a file name,
 use its file extension as image type.
+If PROPS contain image transforms (like :max-height, :scale,
+:rotate, etc), and we don't support these transforms natively in
+Emacs, and TYPE is nil, and we have ImageMagick support in Emacs,
+then TYPE will default to `imagemagick'.
 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
 Optional PROPS are additional image attributes to assign to the image,
@@ -436,13 +441,35 @@ create-image
 Image file names that are not absolute are searched for in the
 \"images\" sub-directory of `data-directory' and
 `x-bitmap-file-path' (in that order)."
+  (unless (plist-get props :scale)
+    (let ((scale (image-compute-scaling-factor image-scaling-factor)))
+      (unless (= scale 1)
+        (setq props (append (list :scale scale) props)))))
+  ;; Default to ImageMagick if a transform is requested, and we do not
+  ;; have the appropriate native transform, and the type isn't
+  ;; specified.
+  (if (and (not type)
+           (fboundp 'imagemagick-types)
+           ;; All these props require scaling.
+           (or (and (or (plist-get props :scale)
+                        (plist-get props :width)
+                        (plist-get props :height)
+                        (plist-get props :max-width)
+                        (plist-get props :max-height))
+                    (not (memq 'scale (image-transforms-p))))
+               ;; We can have rotation by 90/180/270 degrees supported
+               ;; natively...
+               (and (plist-get props :rotation)
+                    (if (zerop (mod (plist-get props :rotation) 90))
+                        (not (memq 'rotate90 (image-transforms-p)))
+                      t))
+               ;; We don't currently have native cropping.
+               (plist-get props :crop)))
+      (setq type 'imagemagick))
   ;; It is x_find_image_file in image.c that sets the search path.
   (setq type (image-type file-or-data type data-p))
   (when (image-type-available-p type)
     (append (list 'image :type type (if data-p :data :file) file-or-data)
-            (and (not (plist-get props :scale))
-                 (list :scale
-                       (image-compute-scaling-factor image-scaling-factor)))
 (defun image--set-property (image property value)

(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no

reply via email to

[Prev in Thread] Current Thread [Next in Thread]