1 ;;; image.el --- image API
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
6 ;; Keywords: multimedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 (defalias 'image-refresh
'image-flush
)
35 (defconst image-type-header-regexps
36 `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm
)
37 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm
)
38 ("\\`GIF8[79]a" . gif
)
39 ("\\`\x89PNG\r\n\x1a\n" . png
)
40 ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
41 #define \\1_height [0-9]+\n\\(\
42 #define \\1_x_hot [0-9]+\n\
43 #define \\1_y_hot [0-9]+\n\\)?\
44 static \\(unsigned \\)?char \\1_bits" . xbm
)
45 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff
)
46 ("\\`[\t\n\r ]*%!PS" . postscript
)
47 ("\\`\xff\xd8" . jpeg
) ; used to be (image-jpeg-p . jpeg)
48 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
49 (comment-re (concat "\\(?:!--" incomment-re
"*-->[ \t\r\n]*<\\)")))
50 (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
52 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re
"*\\)?"
56 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
57 When the first bytes of an image file match REGEXP, it is assumed to
58 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
59 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
60 with one argument, a string containing the image data. If PREDICATE returns
61 a non-nil value, TYPE is the image's type.")
63 (defvar image-type-file-name-regexps
66 ("\\.jpe?g\\'" . jpeg
)
71 ("\\.ps\\'" . postscript
)
72 ("\\.tiff?\\'" . tiff
)
75 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
76 When the name of an image file match REGEXP, it is assumed to
77 be of image type IMAGE-TYPE.")
79 ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
80 ;; of content autodetection. Their contents are just C code, so it is
81 ;; easy to generate false matches.
82 (defvar image-type-auto-detectable
93 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
94 \(See `image-type-auto-detected-p').
97 - t always auto-detect.
98 - nil never auto-detect.
99 - maybe auto-detect only if the image type is available
100 (see `image-type-available-p').")
102 (defvar image-format-suffixes
103 '((image/x-icon
"ico"))
104 "Alist of MIME Content-Type headers to file name suffixes.
105 This is used as a hint by the ImageMagick library when detecting
106 image types. If `create-image' is called with a :format
107 matching found in this alist, the ImageMagick library will be
108 told that the data would have this suffix if saved to a file.")
110 (defcustom image-load-path
111 (list (file-name-as-directory (expand-file-name "images" data-directory
))
112 'data-directory
'load-path
)
113 "List of locations in which to search for image files.
114 If an element is a string, it defines a directory to search.
115 If an element is a variable symbol whose value is a string, that
116 value defines a directory to search.
117 If an element is a variable symbol whose value is a list, the
118 value is used as a list of directories to search."
119 :type
'(repeat (choice directory variable
))
120 :initialize
'custom-initialize-delay
)
123 (defun image-load-path-for-library (library image
&optional path no-error
)
124 "Return a suitable search path for images used by LIBRARY.
126 It searches for IMAGE in `image-load-path' (excluding
127 \"`data-directory'/images\") and `load-path', followed by a path
128 suitable for LIBRARY, which includes \"../../etc/images\" and
129 \"../etc/images\" relative to the library file itself, and then
130 in \"`data-directory'/images\".
132 Then this function returns a list of directories which contains
133 first the directory in which IMAGE was found, followed by the
134 value of `load-path'. If PATH is given, it is used instead of
137 If NO-ERROR is non-nil and a suitable path can't be found, don't
138 signal an error. Instead, return a list of directories as before,
139 except that nil appears in place of the image directory.
141 Here is an example that uses a common idiom to provide
142 compatibility with versions of Emacs that lack the variable
146 (defvar image-load-path)
148 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
149 (image-load-path (cons (car load-path)
150 (when (boundp 'image-load-path)
152 (mh-tool-bar-folder-buttons-init))"
153 (unless library
(error "No library specified"))
154 (unless image
(error "No image specified"))
155 (let (image-directory image-directory-load-path
)
156 ;; Check for images in image-load-path or load-path.
159 ;; Images in image-load-path.
160 (image-search-load-path image
)
161 ;; Images in load-path.
162 (locate-library image
)))
164 ;; Since the image might be in a nested directory (for
165 ;; example, mail/attach.pbm), adjust `image-directory'
168 (setq dir
(file-name-directory dir
))
169 (while (setq parent
(file-name-directory img
))
170 (setq img
(directory-file-name parent
)
171 dir
(expand-file-name "../" dir
))))
172 (setq image-directory-load-path dir
))
174 ;; If `image-directory-load-path' isn't Emacs's image directory,
175 ;; it's probably a user preference, so use it. Then use a
176 ;; relative setting if possible; otherwise, use
177 ;; `image-directory-load-path'.
179 ;; User-modified image-load-path?
180 ((and image-directory-load-path
181 (not (equal image-directory-load-path
182 (file-name-as-directory
183 (expand-file-name "images" data-directory
)))))
184 (setq image-directory image-directory-load-path
))
185 ;; Try relative setting.
186 ((let (library-name d1ei d2ei
)
187 ;; First, find library in the load-path.
188 (setq library-name
(locate-library library
))
189 (if (not library-name
)
190 (error "Cannot find library %s in load-path" library
))
191 ;; And then set image-directory relative to that.
194 d2ei
(file-name-as-directory
196 (concat (file-name-directory library-name
) "../../etc/images")))
198 d1ei
(file-name-as-directory
200 (concat (file-name-directory library-name
) "../etc/images"))))
201 (setq image-directory
202 ;; Set it to nil if image is not found.
203 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
204 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
205 ;; Use Emacs's image directory.
206 (image-directory-load-path
207 (setq image-directory image-directory-load-path
))
209 (message "Could not find image %s for library %s" image library
))
211 (error "Could not find image %s for library %s" image library
)))
213 ;; Return an augmented `path' or `load-path'.
214 (nconc (list image-directory
)
215 (delete image-directory
(copy-sequence (or path load-path
))))))
218 ;; Used to be in image-type-header-regexps, but now not used anywhere
219 ;; (since 2009-08-28).
220 (defun image-jpeg-p (data)
221 "Value is non-nil if DATA, a string, consists of JFIF image data.
222 We accept the tag Exif because that is the same format."
223 (setq data
(ignore-errors (string-to-unibyte data
)))
224 (when (and data
(string-match-p "\\`\xff\xd8" data
))
226 (let ((len (length data
)) (i 2))
228 (when (/= (aref data i
) #xff
)
231 (when (>= (+ i
2) len
)
233 (let ((nbytes (+ (lsh (aref data
(+ i
1)) 8)
234 (aref data
(+ i
2))))
235 (code (aref data i
)))
236 (when (and (>= code
#xe0
) (<= code
#xef
))
237 ;; APP0 LEN1 LEN2 "JFIF\0"
239 (string-match-p "JFIF\\|Exif"
240 (substring data i
(min (+ i nbytes
) len
)))))
241 (setq i
(+ i
1 nbytes
))))))))
245 (defun image-type-from-data (data)
246 "Determine the image type from image data DATA.
247 Value is a symbol specifying the image type or nil if type cannot
249 (let ((types image-type-header-regexps
)
252 (let ((regexp (car (car types
)))
253 (image-type (cdr (car types
))))
254 (if (or (and (symbolp image-type
)
255 (string-match-p regexp data
))
256 (and (consp image-type
)
257 (funcall (car image-type
) data
)
258 (setq image-type
(cdr image-type
))))
259 (setq type image-type
261 (setq types
(cdr types
)))))
266 (defun image-type-from-buffer ()
267 "Determine the image type from data in the current buffer.
268 Value is a symbol specifying the image type or nil if type cannot
270 (let ((types image-type-header-regexps
)
273 (goto-char (point-min))
275 (let ((regexp (car (car types
)))
276 (image-type (cdr (car types
)))
278 (if (or (and (symbolp image-type
)
279 (looking-at-p regexp
))
280 (and (consp image-type
)
281 (funcall (car image-type
)
287 (+ (point-min) 256))))))
288 (setq image-type
(cdr image-type
))))
289 (setq type image-type
291 (setq types
(cdr types
)))))
294 (memq type image-types
)
299 (defun image-type-from-file-header (file)
300 "Determine the type of image file FILE from its first few bytes.
301 Value is a symbol specifying the image type, or nil if type cannot
303 (unless (or (file-readable-p file
)
304 (file-name-absolute-p file
))
305 (setq file
(image-search-load-path file
)))
307 (file-readable-p file
)
309 (set-buffer-multibyte nil
)
310 (insert-file-contents-literally file nil
0 256)
311 (image-type-from-buffer))))
315 (defun image-type-from-file-name (file)
316 "Determine the type of image file FILE from its name.
317 Value is a symbol specifying the image type, or nil if type cannot
321 (dolist (elem image-type-file-name-regexps first
)
322 (when (string-match-p (car elem
) file
)
323 (if (image-type-available-p (setq type
(cdr elem
)))
325 ;; If nothing seems to be supported, return first type that matched.
326 (or first
(setq first type
))))))))
329 (defun image-type (source &optional type data-p
)
330 "Determine and return image type.
331 SOURCE is an image file name or image data.
332 Optional TYPE is a symbol describing the image type. If TYPE is omitted
333 or nil, try to determine the image type from its first few bytes
334 of image data. If that doesn't work, and SOURCE is a file name,
335 use its file extension as image type.
336 Optional DATA-P non-nil means SOURCE is a string containing image data."
337 (when (and (not data-p
) (not (stringp source
)))
338 (error "Invalid image file name `%s'" source
))
340 (setq type
(if data-p
341 (image-type-from-data source
)
342 (or (image-type-from-file-header source
)
343 (image-type-from-file-name source
))))
344 (or type
(error "Cannot determine image type")))
345 (or (memq type
(and (boundp 'image-types
) image-types
))
346 (error "Invalid image type `%s'" type
))
350 (if (fboundp 'image-metadata
) ; eg not --without-x
351 (define-obsolete-function-alias 'image-extension-data
352 'image-metadata
' "24.1"))
354 (define-obsolete-variable-alias
356 'dynamic-library-alist
"24.1")
359 (defun image-type-available-p (type)
360 "Return non-nil if image type TYPE is available.
361 Image types are symbols like `xbm' or `jpeg'."
362 (and (fboundp 'init-image-library
)
363 (init-image-library type
)))
367 (defun image-type-auto-detected-p ()
368 "Return t if the current buffer contains an auto-detectable image.
369 This function is intended to be used from `magic-fallback-mode-alist'.
371 The buffer is considered to contain an auto-detectable image if
372 its beginning matches an image type in `image-type-header-regexps',
373 and that image type is present in `image-type-auto-detectable' with a
374 non-nil value. If that value is non-nil, but not t, then the image type
376 (let* ((type (image-type-from-buffer))
377 (auto (and type
(cdr (assq type image-type-auto-detectable
)))))
379 (or (eq auto t
) (image-type-available-p type
)))))
383 (defun create-image (file-or-data &optional type data-p
&rest props
)
385 FILE-OR-DATA is an image file name or image data.
386 Optional TYPE is a symbol describing the image type. If TYPE is omitted
387 or nil, try to determine the image type from its first few bytes
388 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
389 use its file extension as image type.
390 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
391 Optional PROPS are additional image attributes to assign to the image,
392 like, e.g. `:mask MASK'.
393 Value is the image created, or nil if images of type TYPE are not supported.
395 Images should not be larger than specified by `max-image-size'.
397 Image file names that are not absolute are searched for in the
398 \"images\" sub-directory of `data-directory' and
399 `x-bitmap-file-path' (in that order)."
400 ;; It is x_find_image_file in image.c that sets the search path.
401 (setq type
(image-type file-or-data type data-p
))
402 (when (image-type-available-p type
)
403 (append (list 'image
:type type
(if data-p
:data
:file
) file-or-data
)
408 (defun put-image (image pos
&optional string area
)
409 "Put image IMAGE in front of POS in the current buffer.
410 IMAGE must be an image created with `create-image' or `defimage'.
411 IMAGE is displayed by putting an overlay into the current buffer with a
412 `before-string' STRING that has a `display' property whose value is the
413 image. STRING is defaulted if you omit it.
414 The overlay created will have the `put-image' property set to t.
415 POS may be an integer or marker.
416 AREA is where to display the image. AREA nil or omitted means
417 display it in the text area, a value of `left-margin' means
418 display it in the left marginal area, a value of `right-margin'
419 means display it in the right marginal area."
420 (unless string
(setq string
"x"))
421 (let ((buffer (current-buffer)))
422 (unless (eq (car-safe image
) 'image
)
423 (error "Not an image: %s" image
))
424 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
425 (error "Invalid area %s" area
))
426 (setq string
(copy-sequence string
))
427 (let ((overlay (make-overlay pos pos buffer
))
428 (prop (if (null area
) image
(list (list 'margin area
) image
))))
429 (put-text-property 0 (length string
) 'display prop string
)
430 (overlay-put overlay
'put-image t
)
431 (overlay-put overlay
'before-string string
)
436 (defun insert-image (image &optional string area slice
)
437 "Insert IMAGE into current buffer at point.
438 IMAGE is displayed by inserting STRING into the current buffer
439 with a `display' property whose value is the image. STRING
440 defaults to a single space if you omit it.
441 AREA is where to display the image. AREA nil or omitted means
442 display it in the text area, a value of `left-margin' means
443 display it in the left marginal area, a value of `right-margin'
444 means display it in the right marginal area.
445 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
446 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
447 specifying the X and Y positions and WIDTH and HEIGHT of image area
448 to insert. A float value 0.0 - 1.0 means relative to the width or
449 height of the image; integer values are taken as pixel values."
450 ;; Use a space as least likely to cause trouble when it's a hidden
451 ;; character in the buffer.
452 (unless string
(setq string
" "))
453 (unless (eq (car-safe image
) 'image
)
454 (error "Not an image: %s" image
))
455 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
456 (error "Invalid area %s" area
))
458 (setq image
(list (list 'margin area
) image
))
459 ;; Cons up a new spec equal but not eq to `image' so that
460 ;; inserting it twice in a row (adjacently) displays two copies of
461 ;; the image. Don't try to avoid this by looking at the display
462 ;; properties on either side so that we DTRT more often with
463 ;; cut-and-paste. (Yanking killed image text next to another copy
464 ;; of it loses anyway.)
465 (setq image
(cons 'image
(cdr image
))))
466 (let ((start (point)))
468 (add-text-properties start
(point)
470 (list (cons 'slice slice
) image
)
471 image
) rear-nonsticky
(display)))))
475 (defun insert-sliced-image (image &optional string area rows cols
)
476 "Insert IMAGE into current buffer at point.
477 IMAGE is displayed by inserting STRING into the current buffer
478 with a `display' property whose value is the image. The default
479 STRING is a single space.
480 AREA is where to display the image. AREA nil or omitted means
481 display it in the text area, a value of `left-margin' means
482 display it in the left marginal area, a value of `right-margin'
483 means display it in the right marginal area.
484 The image is automatically split into ROWS x COLS slices."
485 (unless string
(setq string
" "))
486 (unless (eq (car-safe image
) 'image
)
487 (error "Not an image: %s" image
))
488 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
489 (error "Invalid area %s" area
))
491 (setq image
(list (list 'margin area
) image
))
492 ;; Cons up a new spec equal but not eq to `image' so that
493 ;; inserting it twice in a row (adjacently) displays two copies of
494 ;; the image. Don't try to avoid this by looking at the display
495 ;; properties on either side so that we DTRT more often with
496 ;; cut-and-paste. (Yanking killed image text next to another copy
497 ;; of it loses anyway.)
498 (setq image
(cons 'image
(cdr image
))))
499 (let ((x 0.0) (dx (/ 1.0001 (or cols
1)))
500 (y 0.0) (dy (/ 1.0001 (or rows
1))))
503 (let ((start (point)))
505 (add-text-properties start
(point)
506 `(display ,(list (list 'slice x y dx dy
) image
)
507 rear-nonsticky
(display)))
511 (insert (propertize "\n" 'line-height t
)))))
516 (defun remove-images (start end
&optional buffer
)
517 "Remove images between START and END in BUFFER.
518 Remove only images that were put in BUFFER with calls to `put-image'.
519 BUFFER nil or omitted means use the current buffer."
521 (setq buffer
(current-buffer)))
522 (let ((overlays (overlays-in start end
)))
524 (let ((overlay (car overlays
)))
525 (when (overlay-get overlay
'put-image
)
526 (delete-overlay overlay
)))
527 (setq overlays
(cdr overlays
)))))
529 (defun image-search-load-path (file &optional path
)
531 (setq path image-load-path
))
532 (let (element found filename
)
533 (while (and (not found
) (consp path
))
534 (setq element
(car path
))
539 (setq filename
(expand-file-name file element
)))))
540 ((and (symbolp element
) (boundp element
))
541 (setq element
(symbol-value element
))
546 (setq filename
(expand-file-name file element
)))))
548 (if (setq filename
(image-search-load-path file element
))
550 (setq path
(cdr path
)))
551 (if found filename
)))
554 (defun find-image (specs)
555 "Find an image, choosing one of a list of image specifications.
557 SPECS is a list of image specifications.
559 Each image specification in SPECS is a property list. The contents of
560 a specification are image type dependent. All specifications must at
561 least contain the properties `:type TYPE' and either `:file FILE' or
562 `:data DATA', where TYPE is a symbol specifying the image type,
563 e.g. `xbm', FILE is the file to load the image from, and DATA is a
564 string containing the actual image data. The specification whose TYPE
565 is supported, and FILE exists, is used to construct the image
566 specification to be returned. Return nil if no specification is
569 The image is looked for in `image-load-path'.
571 Image files should not be larger than specified by `max-image-size'."
573 (while (and specs
(null image
))
574 (let* ((spec (car specs
))
575 (type (plist-get spec
:type
))
576 (data (plist-get spec
:data
))
577 (file (plist-get spec
:file
))
579 (when (image-type-available-p type
)
580 (cond ((stringp file
)
581 (if (setq found
(image-search-load-path file
))
583 (cons 'image
(plist-put (copy-sequence spec
)
586 (setq image
(cons 'image spec
)))))
587 (setq specs
(cdr specs
))))
592 (defmacro defimage
(symbol specs
&optional doc
)
593 "Define SYMBOL as an image.
595 SPECS is a list of image specifications. DOC is an optional
596 documentation string.
598 Each image specification in SPECS is a property list. The contents of
599 a specification are image type dependent. All specifications must at
600 least contain the properties `:type TYPE' and either `:file FILE' or
601 `:data DATA', where TYPE is a symbol specifying the image type,
602 e.g. `xbm', FILE is the file to load the image from, and DATA is a
603 string containing the actual image data. The first image
604 specification whose TYPE is supported, and FILE exists, is used to
609 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
610 (:type xbm :file \"~/test1.xbm\")))"
611 (declare (doc-string 3))
612 `(defvar ,symbol
(find-image ',specs
) ,doc
))
615 ;;; Animated image API
617 (defvar image-default-frame-delay
0.1
618 "Default interval in seconds between frames of a multi-frame image.
619 Only used if the image does not specify a value.")
621 (defun image-multi-frame-p (image)
622 "Return non-nil if IMAGE contains more than one frame.
623 The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
624 the number of frames (or sub-images) in the image and DELAY is the delay
625 in seconds that the image specifies between each frame. DELAY may be nil,
626 in which case you might want to use `image-default-frame-delay'."
627 (let* ((metadata (image-metadata image
))
628 (images (plist-get metadata
'count
))
629 (delay (plist-get metadata
'delay
)))
630 (when (and images
(> images
1))
631 (if (or (not (numberp delay
)) (< delay
0))
632 (setq delay image-default-frame-delay
))
633 (cons images delay
))))
635 (defun image-animated-p (image)
636 "Like `image-multi-frame-p', but returns nil if no delay is specified."
637 (let ((multi (image-multi-frame-p image
)))
638 (and (cdr multi
) multi
)))
640 (make-obsolete 'image-animated-p
'image-multi-frame-p
"24.4")
643 (defun image-animate (image &optional index limit
)
644 "Start animating IMAGE.
645 Animation occurs by destructively altering the IMAGE spec list.
647 With optional INDEX, begin animating from that animation frame.
648 LIMIT specifies how long to animate the image. If omitted or
649 nil, play the animation until the end. If t, loop forever. If a
650 number, play until that number of seconds has elapsed."
651 (let ((animation (image-multi-frame-p image
))
654 (if (setq timer
(image-animate-timer image
))
655 (cancel-timer timer
))
656 (run-with-timer 0.2 nil
'image-animate-timeout
657 image
(or index
0) (car animation
)
660 (defun image-animate-timer (image)
661 "Return the animation timer for image IMAGE."
662 ;; See cancel-function-timers
663 (let ((tail timer-list
) timer
)
665 (setq timer
(car tail
)
667 (if (and (eq (timer--function timer
) 'image-animate-timeout
)
668 (eq (car-safe (timer--args timer
)) image
))
673 (defconst image-minimum-frame-delay
0.01
674 "Minimum interval in seconds between frames of an animated image.")
676 (defun image-current-frame (image)
677 "The current frame number of IMAGE, indexed from 0."
678 (or (plist-get (cdr image
) :index
) 0))
680 (defun image-show-frame (image n
&optional nocheck
)
681 "Show frame N of IMAGE.
682 Frames are indexed from 0. Optional argument NOCHECK non-nil means
683 do not check N is within the range of frames present in the image."
685 (if (< n
0) (setq n
0)
686 (setq n
(min n
(1- (car (image-multi-frame-p image
)))))))
687 (plist-put (cdr image
) :index n
)
688 (force-window-update))
690 (defun image-animate-get-speed (image)
691 "Return the speed factor for animating IMAGE."
692 (or (plist-get (cdr image
) :speed
) 1))
694 (defun image-animate-set-speed (image value
&optional multiply
)
695 "Set the speed factor for animating IMAGE to VALUE.
696 With optional argument MULTIPLY non-nil, treat VALUE as a
697 multiplication factor for the current value."
698 (plist-put (cdr image
) :speed
700 (* value
(image-animate-get-speed image
))
703 ;; FIXME? The delay may not be the same for different sub-images,
704 ;; hence we need to call image-multi-frame-p to return it.
705 ;; But it also returns count, so why do we bother passing that as an
707 (defun image-animate-timeout (image n count time-elapsed limit
)
708 "Display animation frame N of IMAGE.
709 N=0 refers to the initial animation frame.
710 COUNT is the total number of frames in the animation.
711 TIME-ELAPSED is the total time that has elapsed since
712 `image-animate-start' was called.
713 LIMIT determines when to stop. If t, loop forever. If nil, stop
714 after displaying the last animation frame. Otherwise, stop
715 after LIMIT seconds have elapsed.
716 The minimum delay between successive frames is `image-minimum-frame-delay'.
718 If the image has a non-nil :speed property, it acts as a multiplier
719 for the animation speed. A negative value means to animate in reverse."
720 (image-show-frame image n t
)
721 (let* ((speed (image-animate-get-speed image
))
723 (animation (image-multi-frame-p image
))
724 ;; Subtract off the time we took to load the image from the
725 ;; stated delay time.
726 (delay (max (+ (* (or (cdr animation
) image-default-frame-delay
)
728 time
(- (float-time)))
729 image-minimum-frame-delay
))
731 (setq n
(if (< speed
0)
735 (cond ((>= n count
) (setq n
0))
736 ((< n
0) (setq n
(1- count
))))
737 (and (or (>= n count
) (< n
0)) (setq done t
)))
738 (setq time-elapsed
(+ delay time-elapsed
))
740 (setq done
(>= time-elapsed limit
)))
742 (run-with-timer delay nil
'image-animate-timeout
743 image n count time-elapsed limit
))))
746 (defvar imagemagick-types-inhibit
)
747 (defvar imagemagick-enabled-types
)
749 (defun imagemagick-filter-types ()
750 "Return a list of the ImageMagick types to be treated as images, or nil.
751 This is the result of `imagemagick-types', including only elements
752 that match `imagemagick-enabled-types' and do not match
753 `imagemagick-types-inhibit'."
754 (when (fboundp 'imagemagick-types
)
755 (cond ((null imagemagick-enabled-types
) nil
)
756 ((eq imagemagick-types-inhibit t
) nil
)
761 (unless (memq type imagemagick-types-inhibit
)
762 (if (eq imagemagick-enabled-types t
) type
764 (dolist (enable imagemagick-enabled-types nil
)
765 (if (cond ((symbolp enable
) (eq enable type
))
768 (symbol-name type
))))
769 (throw 'found type
)))))))
770 (imagemagick-types)))))))
772 (defvar imagemagick--file-regexp nil
773 "File extension regexp for ImageMagick files, if any.
774 This is the extension installed into `auto-mode-alist' and
775 `image-type-file-name-regexps' by `imagemagick-register-types'.")
778 (defun imagemagick-register-types ()
779 "Register file types that can be handled by ImageMagick.
780 This function is called at startup, after loading the init file.
781 It registers the ImageMagick types returned by `imagemagick-filter-types'.
783 Registered image types are added to `auto-mode-alist', so that
784 Emacs visits them in Image mode. They are also added to
785 `image-type-file-name-regexps', so that the `image-type' function
786 recognizes these files as having image type `imagemagick'.
788 If Emacs is compiled without ImageMagick support, this does nothing."
789 (when (fboundp 'imagemagick-types
)
790 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type
)))
791 (imagemagick-filter-types)))
792 (re (if types
(concat "\\." (regexp-opt types
) "\\'")))
793 (ama-elt (car (member (cons imagemagick--file-regexp
'image-mode
)
795 (itfnr-elt (car (member (cons imagemagick--file-regexp
'imagemagick
)
796 image-type-file-name-regexps
))))
798 (setq auto-mode-alist
(delete ama-elt auto-mode-alist
)
799 image-type-file-name-regexps
800 (delete itfnr-elt image-type-file-name-regexps
))
803 (push (cons re
'image-mode
) auto-mode-alist
))
805 (setcar itfnr-elt re
)
806 ;; Append to `image-type-file-name-regexps', so that we
807 ;; preferentially use specialized image libraries.
808 (add-to-list 'image-type-file-name-regexps
809 (cons re
'imagemagick
) t
)))
810 (setq imagemagick--file-regexp re
))))
812 (defcustom imagemagick-types-inhibit
813 '(C HTML HTM INFO M TXT PDF
)
814 "List of ImageMagick types that should never be treated as images.
815 This should be a list of symbols, each of which should be one of
816 the ImageMagick types listed by `imagemagick-types'. The listed
817 image types are not registered by `imagemagick-register-types'.
819 If the value is t, inhibit the use of ImageMagick for images.
821 If you change this without using customize, you must call
822 `imagemagick-register-types' afterwards.
824 If Emacs is compiled without ImageMagick support, this variable
826 :type
'(choice (const :tag
"Support all ImageMagick types" nil
)
827 (const :tag
"Disable all ImageMagick types" t
)
829 :initialize
'custom-initialize-default
830 :set
(lambda (symbol value
)
831 (set-default symbol value
)
832 (imagemagick-register-types))
836 (defcustom imagemagick-enabled-types
837 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
838 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
839 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
840 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
841 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
842 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
843 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
844 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
845 XWD YCbCr YCbCrA YUV
)
846 "List of ImageMagick types to treat as images.
847 Each list element should be a string or symbol, representing one
848 of the image types returned by `imagemagick-types'. If the
849 element is a string, it is handled as a regexp that enables all
852 The value of `imagemagick-enabled-types' may also be t, meaning
853 to enable all types that ImageMagick supports.
855 The variable `imagemagick-types-inhibit' overrides this variable.
857 If you change this without using customize, you must call
858 `imagemagick-register-types' afterwards.
860 If Emacs is compiled without ImageMagick support, this variable
862 :type
'(choice (const :tag
"Support all ImageMagick types" t
)
863 (const :tag
"Disable all ImageMagick types" nil
)
864 (repeat :tag
"List of types"
865 (choice (symbol :tag
"type")
866 (regexp :tag
"regexp"))))
867 :initialize
'custom-initialize-default
868 :set
(lambda (symbol value
)
869 (set-default symbol value
)
870 (imagemagick-register-types))
874 (imagemagick-register-types)
878 ;;; image.el ends here