Allow specifying the intermediate format

* lisp/image/image-converter.el (image-convert-to-format): New
user format (bug#54494).
(image-convert, image-converter--convert-magick)
(image-converter--convert): Use it.
This commit is contained in:
Lars Ingebrigtsen 2022-03-21 17:51:21 +01:00
parent ede8ad507d
commit 61d34c6a50
2 changed files with 29 additions and 9 deletions

View file

@ -461,6 +461,7 @@ must be available."
(and auto
(or (eq auto t) (image-type-available-p type)))))
(defvar image-convert-to-format)
;;;###autoload
(defun create-image (file-or-data &optional type data-p &rest props)
@ -498,7 +499,7 @@ Image file names that are not absolute are searched for in the
(when (eq type 'image-convert)
(require 'image-converter)
(setq file-or-data (image-convert file-or-data data-format)
type 'png
type (intern image-convert-to-format)
data-p t)))
(when (image-type-available-p type)
(let ((image

View file

@ -46,6 +46,16 @@ formats that are to be supported: Only the suffixes that map to
:type 'symbol
:version "27.1")
(defcustom image-convert-to-format "png"
"The image format to convert to.
This should be a string like \"png\" or \"ppm\" or some
other (preferrably lossless) format that Emacs understands
natively. The converter chosen has to support the format, and if
not, conversion will fail."
:group 'image
:version "29.1"
:type 'string)
(defvar image-converter-regexp nil
"A regexp that matches the file name suffixes that can be converted.")
@ -85,7 +95,10 @@ is a string, it should be a MIME format string like
'image-convert))
(defun image-convert (image &optional image-format)
"Convert IMAGE file to the PNG format.
"Convert IMAGE file to an image format Emacs understands.
This will usually be \"png\", but this is controlled by the
`image-convert-to-format' user option.
IMAGE can either be a file name or image data.
To pass in image data, IMAGE should a string containing the image
@ -96,8 +109,8 @@ like \"image/webp\". For instance:
IMAGE can also be an image object as returned by `create-image'.
This function converts the image to PNG, and the converted image
data is returned as a string."
This function converts the image the preferred format, and the
converted image data is returned as a string."
;; Find an installed image converter.
(unless image-converter
(image-converter--find-converter))
@ -120,7 +133,9 @@ data is returned as a string."
(if (listp image)
;; Return an image object that's the same as we were passed,
;; but ignore the :type value.
(apply #'create-image (buffer-string) 'png t
(apply #'create-image (buffer-string)
(intern image-convert-to-format)
t
(cl-loop for (key val) on (cdr image) by #'cddr
unless (eq key :type)
append (list key val)))
@ -239,12 +254,15 @@ Only suffixes that map to `image-mode' are returned."
(list (format "%s:-"
(image-converter--mime-type
image-format))
"png:-"))))
(concat image-convert-to-format
":-")))))
;; SOURCE is a file name.
(apply #'call-process (car command)
nil t nil
(append (cdr command)
(list (expand-file-name source) "png:-")))))
(list (expand-file-name source)
(concat image-convert-to-format
":-"))))))
;; If the command failed, hopefully the buffer contains the
;; error message.
(buffer-string))))
@ -262,14 +280,15 @@ Only suffixes that map to `image-mode' are returned."
(append
(cdr command)
(list "-i" "-"
"-c:v" "png"
"-c:v" image-convert-to-format
"-f" "image2pipe" "-"))))
(apply #'call-process
(car command)
nil '(t nil) nil
(append (cdr command)
(list "-i" (expand-file-name source)
"-c:v" "png" "-f" "image2pipe"
"-c:v" image-convert-to-format
"-f" "image2pipe"
"-")))))
"ffmpeg error when converting")))