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
)
38 \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[[:space:]]\\)+\
39 \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[0-9]\\)+\
41 ("\\`GIF8[79]a" . gif
)
42 ("\\`\x89PNG\r\n\x1a\n" . png
)
43 ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
44 #define \\1_height [0-9]+\n\\(\
45 #define \\1_x_hot [0-9]+\n\
46 #define \\1_y_hot [0-9]+\n\\)?\
47 static \\(unsigned \\)?char \\1_bits" . xbm
)
48 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff
)
49 ("\\`[\t\n\r ]*%!PS" . postscript
)
50 ("\\`\xff\xd8" . jpeg
) ; used to be (image-jpeg-p . jpeg)
51 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
52 (comment-re (concat "\\(?:!--" incomment-re
"*-->[ \t\r\n]*<\\)")))
53 (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
55 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re
"*\\)?"
59 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
60 When the first bytes of an image file match REGEXP, it is assumed to
61 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
62 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
63 with one argument, a string containing the image data. If PREDICATE returns
64 a non-nil value, TYPE is the image's type.")
66 (defvar image-type-file-name-regexps
69 ("\\.jpe?g\\'" . jpeg
)
74 ("\\.ps\\'" . postscript
)
75 ("\\.tiff?\\'" . tiff
)
78 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
79 When the name of an image file match REGEXP, it is assumed to
80 be of image type IMAGE-TYPE.")
82 ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
83 ;; of content autodetection. Their contents are just C code, so it is
84 ;; easy to generate false matches.
85 (defvar image-type-auto-detectable
96 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
97 \(See `image-type-auto-detected-p').
100 - t always auto-detect.
101 - nil never auto-detect.
102 - maybe auto-detect only if the image type is available
103 (see `image-type-available-p').")
105 (defvar image-format-suffixes
106 '((image/x-icon
"ico"))
107 "Alist of MIME Content-Type headers to file name suffixes.
108 This is used as a hint by the ImageMagick library when detecting
109 image types. If `create-image' is called with a :format
110 matching found in this alist, the ImageMagick library will be
111 told that the data would have this suffix if saved to a file.")
113 (defcustom image-load-path
114 (list (file-name-as-directory (expand-file-name "images" data-directory
))
115 'data-directory
'load-path
)
116 "List of locations in which to search for image files.
117 If an element is a string, it defines a directory to search.
118 If an element is a variable symbol whose value is a string, that
119 value defines a directory to search.
120 If an element is a variable symbol whose value is a list, the
121 value is used as a list of directories to search.
123 Subdirectories are not automatically included in the search."
124 :type
'(repeat (choice directory variable
))
125 :initialize
'custom-initialize-delay
)
128 (defun image-load-path-for-library (library image
&optional path no-error
)
129 "Return a suitable search path for images used by LIBRARY.
131 It searches for IMAGE in `image-load-path' (excluding
132 \"`data-directory'/images\") and `load-path', followed by a path
133 suitable for LIBRARY, which includes \"../../etc/images\" and
134 \"../etc/images\" relative to the library file itself, and then
135 in \"`data-directory'/images\".
137 Then this function returns a list of directories which contains
138 first the directory in which IMAGE was found, followed by the
139 value of `load-path'. If PATH is given, it is used instead of
142 If NO-ERROR is non-nil and a suitable path can't be found, don't
143 signal an error. Instead, return a list of directories as before,
144 except that nil appears in place of the image directory.
146 Here is an example that uses a common idiom to provide
147 compatibility with versions of Emacs that lack the variable
151 (defvar image-load-path)
153 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
154 (image-load-path (cons (car load-path)
155 (when (boundp 'image-load-path)
157 (mh-tool-bar-folder-buttons-init))"
158 (unless library
(error "No library specified"))
159 (unless image
(error "No image specified"))
160 (let (image-directory image-directory-load-path
)
161 ;; Check for images in image-load-path or load-path.
164 ;; Images in image-load-path.
165 (image-search-load-path image
)
166 ;; Images in load-path.
167 (locate-library image
)))
169 ;; Since the image might be in a nested directory (for
170 ;; example, mail/attach.pbm), adjust `image-directory'
173 (setq dir
(file-name-directory dir
))
174 (while (setq parent
(file-name-directory img
))
175 (setq img
(directory-file-name parent
)
176 dir
(expand-file-name "../" dir
))))
177 (setq image-directory-load-path dir
))
179 ;; If `image-directory-load-path' isn't Emacs's image directory,
180 ;; it's probably a user preference, so use it. Then use a
181 ;; relative setting if possible; otherwise, use
182 ;; `image-directory-load-path'.
184 ;; User-modified image-load-path?
185 ((and image-directory-load-path
186 (not (equal image-directory-load-path
187 (file-name-as-directory
188 (expand-file-name "images" data-directory
)))))
189 (setq image-directory image-directory-load-path
))
190 ;; Try relative setting.
191 ((let (library-name d1ei d2ei
)
192 ;; First, find library in the load-path.
193 (setq library-name
(locate-library library
))
194 (if (not library-name
)
195 (error "Cannot find library %s in load-path" library
))
196 ;; And then set image-directory relative to that.
199 d2ei
(file-name-as-directory
201 (concat (file-name-directory library-name
) "../../etc/images")))
203 d1ei
(file-name-as-directory
205 (concat (file-name-directory library-name
) "../etc/images"))))
206 (setq image-directory
207 ;; Set it to nil if image is not found.
208 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
209 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
210 ;; Use Emacs's image directory.
211 (image-directory-load-path
212 (setq image-directory image-directory-load-path
))
214 (message "Could not find image %s for library %s" image library
))
216 (error "Could not find image %s for library %s" image library
)))
218 ;; Return an augmented `path' or `load-path'.
219 (nconc (list image-directory
)
220 (delete image-directory
(copy-sequence (or path load-path
))))))
223 ;; Used to be in image-type-header-regexps, but now not used anywhere
224 ;; (since 2009-08-28).
225 (defun image-jpeg-p (data)
226 "Value is non-nil if DATA, a string, consists of JFIF image data.
227 We accept the tag Exif because that is the same format."
228 (setq data
(ignore-errors (string-to-unibyte data
)))
229 (when (and data
(string-match-p "\\`\xff\xd8" data
))
231 (let ((len (length data
)) (i 2))
233 (when (/= (aref data i
) #xff
)
236 (when (>= (+ i
2) len
)
238 (let ((nbytes (+ (lsh (aref data
(+ i
1)) 8)
239 (aref data
(+ i
2))))
240 (code (aref data i
)))
241 (when (and (>= code
#xe0
) (<= code
#xef
))
242 ;; APP0 LEN1 LEN2 "JFIF\0"
244 (string-match-p "JFIF\\|Exif"
245 (substring data i
(min (+ i nbytes
) len
)))))
246 (setq i
(+ i
1 nbytes
))))))))
250 (defun image-type-from-data (data)
251 "Determine the image type from image data DATA.
252 Value is a symbol specifying the image type or nil if type cannot
254 (let ((types image-type-header-regexps
)
257 (let ((regexp (car (car types
)))
258 (image-type (cdr (car types
))))
259 (if (or (and (symbolp image-type
)
260 (string-match-p regexp data
))
261 (and (consp image-type
)
262 (funcall (car image-type
) data
)
263 (setq image-type
(cdr image-type
))))
264 (setq type image-type
266 (setq types
(cdr types
)))))
271 (defun image-type-from-buffer ()
272 "Determine the image type from data in the current buffer.
273 Value is a symbol specifying the image type or nil if type cannot
275 (let ((types image-type-header-regexps
)
278 (goto-char (point-min))
280 (let ((regexp (car (car types
)))
281 (image-type (cdr (car types
)))
283 (if (or (and (symbolp image-type
)
284 (looking-at-p regexp
))
285 (and (consp image-type
)
286 (funcall (car image-type
)
292 (+ (point-min) 256))))))
293 (setq image-type
(cdr image-type
))))
294 (setq type image-type
296 (setq types
(cdr types
)))))
299 (boundp 'image-types
)
300 (memq type image-types
)
305 (defun image-type-from-file-header (file)
306 "Determine the type of image file FILE from its first few bytes.
307 Value is a symbol specifying the image type, or nil if type cannot
309 (unless (or (file-readable-p file
)
310 (file-name-absolute-p file
))
311 (setq file
(image-search-load-path file
)))
313 (file-readable-p file
)
315 (set-buffer-multibyte nil
)
316 (insert-file-contents-literally file nil
0 256)
317 (image-type-from-buffer))))
321 (defun image-type-from-file-name (file)
322 "Determine the type of image file FILE from its name.
323 Value is a symbol specifying the image type, or nil if type cannot
327 (dolist (elem image-type-file-name-regexps first
)
328 (when (string-match-p (car elem
) file
)
329 (if (image-type-available-p (setq type
(cdr elem
)))
331 ;; If nothing seems to be supported, return first type that matched.
332 (or first
(setq first type
))))))))
335 (defun image-type (source &optional type data-p
)
336 "Determine and return image type.
337 SOURCE is an image file name or image data.
338 Optional TYPE is a symbol describing the image type. If TYPE is omitted
339 or nil, try to determine the image type from its first few bytes
340 of image data. If that doesn't work, and SOURCE is a file name,
341 use its file extension as image type.
342 Optional DATA-P non-nil means SOURCE is a string containing image data."
343 (when (and (not data-p
) (not (stringp source
)))
344 (error "Invalid image file name `%s'" source
))
346 (setq type
(if data-p
347 (image-type-from-data source
)
348 (or (image-type-from-file-header source
)
349 (image-type-from-file-name source
))))
350 (or type
(error "Cannot determine image type")))
351 (or (memq type
(and (boundp 'image-types
) image-types
))
352 (error "Invalid image type `%s'" type
))
356 (if (fboundp 'image-metadata
) ; eg not --without-x
357 (define-obsolete-function-alias 'image-extension-data
358 'image-metadata
' "24.1"))
360 (define-obsolete-variable-alias
362 'dynamic-library-alist
"24.1")
365 (defun image-type-available-p (type)
366 "Return non-nil if image type TYPE is available.
367 Image types are symbols like `xbm' or `jpeg'."
368 (and (fboundp 'init-image-library
)
369 (init-image-library type
)))
373 (defun image-type-auto-detected-p ()
374 "Return t if the current buffer contains an auto-detectable image.
375 This function is intended to be used from `magic-fallback-mode-alist'.
377 The buffer is considered to contain an auto-detectable image if
378 its beginning matches an image type in `image-type-header-regexps',
379 and that image type is present in `image-type-auto-detectable' with a
380 non-nil value. If that value is non-nil, but not t, then the image type
382 (let* ((type (image-type-from-buffer))
383 (auto (and type
(cdr (assq type image-type-auto-detectable
)))))
385 (or (eq auto t
) (image-type-available-p type
)))))
389 (defun create-image (file-or-data &optional type data-p
&rest props
)
391 FILE-OR-DATA is an image file name or image data.
392 Optional TYPE is a symbol describing the image type. If TYPE is omitted
393 or nil, try to determine the image type from its first few bytes
394 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
395 use its file extension as image type.
396 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
397 Optional PROPS are additional image attributes to assign to the image,
398 like, e.g. `:mask MASK'.
399 Value is the image created, or nil if images of type TYPE are not supported.
401 Images should not be larger than specified by `max-image-size'.
403 Image file names that are not absolute are searched for in the
404 \"images\" sub-directory of `data-directory' and
405 `x-bitmap-file-path' (in that order)."
406 ;; It is x_find_image_file in image.c that sets the search path.
407 (setq type
(image-type file-or-data type data-p
))
408 (when (image-type-available-p type
)
409 (append (list 'image
:type type
(if data-p
:data
:file
) file-or-data
)
414 (defun put-image (image pos
&optional string area
)
415 "Put image IMAGE in front of POS in the current buffer.
416 IMAGE must be an image created with `create-image' or `defimage'.
417 IMAGE is displayed by putting an overlay into the current buffer with a
418 `before-string' STRING that has a `display' property whose value is the
419 image. STRING is defaulted if you omit it.
420 The overlay created will have the `put-image' property set to t.
421 POS may be an integer or marker.
422 AREA is where to display the image. AREA nil or omitted means
423 display it in the text area, a value of `left-margin' means
424 display it in the left marginal area, a value of `right-margin'
425 means display it in the right marginal area."
426 (unless string
(setq string
"x"))
427 (let ((buffer (current-buffer)))
428 (unless (eq (car-safe image
) 'image
)
429 (error "Not an image: %s" image
))
430 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
431 (error "Invalid area %s" area
))
432 (setq string
(copy-sequence string
))
433 (let ((overlay (make-overlay pos pos buffer
))
434 (prop (if (null area
) image
(list (list 'margin area
) image
))))
435 (put-text-property 0 (length string
) 'display prop string
)
436 (overlay-put overlay
'put-image t
)
437 (overlay-put overlay
'before-string string
)
442 (defun insert-image (image &optional string area slice
)
443 "Insert IMAGE into current buffer at point.
444 IMAGE is displayed by inserting STRING into the current buffer
445 with a `display' property whose value is the image. STRING
446 defaults to a single space if you omit it.
447 AREA is where to display the image. AREA nil or omitted means
448 display it in the text area, a value of `left-margin' means
449 display it in the left marginal area, a value of `right-margin'
450 means display it in the right marginal area.
451 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
452 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
453 specifying the X and Y positions and WIDTH and HEIGHT of image area
454 to insert. A float value 0.0 - 1.0 means relative to the width or
455 height of the image; integer values are taken as pixel values."
456 ;; Use a space as least likely to cause trouble when it's a hidden
457 ;; character in the buffer.
458 (unless string
(setq string
" "))
459 (unless (eq (car-safe image
) 'image
)
460 (error "Not an image: %s" image
))
461 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
462 (error "Invalid area %s" area
))
464 (setq image
(list (list 'margin area
) image
))
465 ;; Cons up a new spec equal but not eq to `image' so that
466 ;; inserting it twice in a row (adjacently) displays two copies of
467 ;; the image. Don't try to avoid this by looking at the display
468 ;; properties on either side so that we DTRT more often with
469 ;; cut-and-paste. (Yanking killed image text next to another copy
470 ;; of it loses anyway.)
471 (setq image
(cons 'image
(cdr image
))))
472 (let ((start (point)))
474 (add-text-properties start
(point)
476 (list (cons 'slice slice
) image
)
477 image
) rear-nonsticky
(display)))))
481 (defun insert-sliced-image (image &optional string area rows cols
)
482 "Insert IMAGE into current buffer at point.
483 IMAGE is displayed by inserting STRING into the current buffer
484 with a `display' property whose value is the image. The default
485 STRING is a single space.
486 AREA is where to display the image. AREA nil or omitted means
487 display it in the text area, a value of `left-margin' means
488 display it in the left marginal area, a value of `right-margin'
489 means display it in the right marginal area.
490 The image is automatically split into ROWS x COLS slices."
491 (unless string
(setq string
" "))
492 (unless (eq (car-safe image
) 'image
)
493 (error "Not an image: %s" image
))
494 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
495 (error "Invalid area %s" area
))
497 (setq image
(list (list 'margin area
) image
))
498 ;; Cons up a new spec equal but not eq to `image' so that
499 ;; inserting it twice in a row (adjacently) displays two copies of
500 ;; the image. Don't try to avoid this by looking at the display
501 ;; properties on either side so that we DTRT more often with
502 ;; cut-and-paste. (Yanking killed image text next to another copy
503 ;; of it loses anyway.)
504 (setq image
(cons 'image
(cdr image
))))
505 (let ((x 0.0) (dx (/ 1.0001 (or cols
1)))
506 (y 0.0) (dy (/ 1.0001 (or rows
1))))
509 (let ((start (point)))
511 (add-text-properties start
(point)
512 `(display ,(list (list 'slice x y dx dy
) image
)
513 rear-nonsticky
(display)))
517 (insert (propertize "\n" 'line-height t
)))))
522 (defun remove-images (start end
&optional buffer
)
523 "Remove images between START and END in BUFFER.
524 Remove only images that were put in BUFFER with calls to `put-image'.
525 BUFFER nil or omitted means use the current buffer."
527 (setq buffer
(current-buffer)))
528 (let ((overlays (overlays-in start end
)))
530 (let ((overlay (car overlays
)))
531 (when (overlay-get overlay
'put-image
)
532 (delete-overlay overlay
)))
533 (setq overlays
(cdr overlays
)))))
535 (defun image-search-load-path (file &optional path
)
537 (setq path image-load-path
))
538 (let (element found filename
)
539 (while (and (not found
) (consp path
))
540 (setq element
(car path
))
545 (setq filename
(expand-file-name file element
)))))
546 ((and (symbolp element
) (boundp element
))
547 (setq element
(symbol-value element
))
552 (setq filename
(expand-file-name file element
)))))
554 (if (setq filename
(image-search-load-path file element
))
556 (setq path
(cdr path
)))
557 (if found filename
)))
560 (defun find-image (specs)
561 "Find an image, choosing one of a list of image specifications.
563 SPECS is a list of image specifications.
565 Each image specification in SPECS is a property list. The contents of
566 a specification are image type dependent. All specifications must at
567 least contain the properties `:type TYPE' and either `:file FILE' or
568 `:data DATA', where TYPE is a symbol specifying the image type,
569 e.g. `xbm', FILE is the file to load the image from, and DATA is a
570 string containing the actual image data. The specification whose TYPE
571 is supported, and FILE exists, is used to construct the image
572 specification to be returned. Return nil if no specification is
575 The image is looked for in `image-load-path'.
577 Image files should not be larger than specified by `max-image-size'."
579 (while (and specs
(null image
))
580 (let* ((spec (car specs
))
581 (type (plist-get spec
:type
))
582 (data (plist-get spec
:data
))
583 (file (plist-get spec
:file
))
585 (when (image-type-available-p type
)
586 (cond ((stringp file
)
587 (if (setq found
(image-search-load-path file
))
589 (cons 'image
(plist-put (copy-sequence spec
)
592 (setq image
(cons 'image spec
)))))
593 (setq specs
(cdr specs
))))
598 (defmacro defimage
(symbol specs
&optional doc
)
599 "Define SYMBOL as an image, and return SYMBOL.
601 SPECS is a list of image specifications. DOC is an optional
602 documentation string.
604 Each image specification in SPECS is a property list. The contents of
605 a specification are image type dependent. All specifications must at
606 least contain the properties `:type TYPE' and either `:file FILE' or
607 `:data DATA', where TYPE is a symbol specifying the image type,
608 e.g. `xbm', FILE is the file to load the image from, and DATA is a
609 string containing the actual image data. The first image
610 specification whose TYPE is supported, and FILE exists, is used to
615 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
616 (:type xbm :file \"~/test1.xbm\")))"
617 (declare (doc-string 3))
618 `(defvar ,symbol
(find-image ',specs
) ,doc
))
621 ;;; Animated image API
623 (defvar image-default-frame-delay
0.1
624 "Default interval in seconds between frames of a multi-frame image.
625 Only used if the image does not specify a value.")
627 (defun image-multi-frame-p (image)
628 "Return non-nil if IMAGE contains more than one frame.
629 The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
630 the number of frames (or sub-images) in the image and DELAY is the delay
631 in seconds that the image specifies between each frame. DELAY may be nil,
632 in which case you might want to use `image-default-frame-delay'."
633 (when (fboundp 'image-metadata
)
634 (let* ((metadata (image-metadata image
))
635 (images (plist-get metadata
'count
))
636 (delay (plist-get metadata
'delay
)))
637 (when (and images
(> images
1))
638 (if (or (not (numberp delay
)) (< delay
0))
639 (setq delay image-default-frame-delay
))
640 (cons images delay
)))))
642 (defun image-animated-p (image)
643 "Like `image-multi-frame-p', but returns nil if no delay is specified."
644 (let ((multi (image-multi-frame-p image
)))
645 (and (cdr multi
) multi
)))
647 (make-obsolete 'image-animated-p
'image-multi-frame-p
"24.4")
650 (defun image-animate (image &optional index limit
)
651 "Start animating IMAGE.
652 Animation occurs by destructively altering the IMAGE spec list.
654 With optional INDEX, begin animating from that animation frame.
655 LIMIT specifies how long to animate the image. If omitted or
656 nil, play the animation until the end. If t, loop forever. If a
657 number, play until that number of seconds has elapsed."
658 (let ((animation (image-multi-frame-p image
))
661 (if (setq timer
(image-animate-timer image
))
662 (cancel-timer timer
))
663 (run-with-timer 0.2 nil
'image-animate-timeout
664 image
(or index
0) (car animation
)
667 (defun image-animate-timer (image)
668 "Return the animation timer for image IMAGE."
669 ;; See cancel-function-timers
670 (let ((tail timer-list
) timer
)
672 (setq timer
(car tail
)
674 (if (and (eq (timer--function timer
) 'image-animate-timeout
)
675 (eq (car-safe (timer--args timer
)) image
))
680 (defconst image-minimum-frame-delay
0.01
681 "Minimum interval in seconds between frames of an animated image.")
683 (defun image-current-frame (image)
684 "The current frame number of IMAGE, indexed from 0."
685 (or (plist-get (cdr image
) :index
) 0))
687 (defun image-show-frame (image n
&optional nocheck
)
688 "Show frame N of IMAGE.
689 Frames are indexed from 0. Optional argument NOCHECK non-nil means
690 do not check N is within the range of frames present in the image."
692 (if (< n
0) (setq n
0)
693 (setq n
(min n
(1- (car (image-multi-frame-p image
)))))))
694 (plist-put (cdr image
) :index n
)
695 (force-window-update))
697 (defun image-animate-get-speed (image)
698 "Return the speed factor for animating IMAGE."
699 (or (plist-get (cdr image
) :speed
) 1))
701 (defun image-animate-set-speed (image value
&optional multiply
)
702 "Set the speed factor for animating IMAGE to VALUE.
703 With optional argument MULTIPLY non-nil, treat VALUE as a
704 multiplication factor for the current value."
705 (plist-put (cdr image
) :speed
707 (* value
(image-animate-get-speed image
))
710 ;; FIXME? The delay may not be the same for different sub-images,
711 ;; hence we need to call image-multi-frame-p to return it.
712 ;; But it also returns count, so why do we bother passing that as an
714 (defun image-animate-timeout (image n count time-elapsed limit
)
715 "Display animation frame N of IMAGE.
716 N=0 refers to the initial animation frame.
717 COUNT is the total number of frames in the animation.
718 TIME-ELAPSED is the total time that has elapsed since
719 `image-animate-start' was called.
720 LIMIT determines when to stop. If t, loop forever. If nil, stop
721 after displaying the last animation frame. Otherwise, stop
722 after LIMIT seconds have elapsed.
723 The minimum delay between successive frames is `image-minimum-frame-delay'.
725 If the image has a non-nil :speed property, it acts as a multiplier
726 for the animation speed. A negative value means to animate in reverse."
727 (image-show-frame image n t
)
728 (let* ((speed (image-animate-get-speed image
))
730 (animation (image-multi-frame-p image
))
731 ;; Subtract off the time we took to load the image from the
732 ;; stated delay time.
733 (delay (max (+ (* (or (cdr animation
) image-default-frame-delay
)
735 time
(- (float-time)))
736 image-minimum-frame-delay
))
738 (setq n
(if (< speed
0)
742 (cond ((>= n count
) (setq n
0))
743 ((< n
0) (setq n
(1- count
))))
744 (and (or (>= n count
) (< n
0)) (setq done t
)))
745 (setq time-elapsed
(+ delay time-elapsed
))
747 (setq done
(>= time-elapsed limit
)))
749 (run-with-timer delay nil
'image-animate-timeout
750 image n count time-elapsed limit
))))
753 (defvar imagemagick-types-inhibit
)
754 (defvar imagemagick-enabled-types
)
756 (defun imagemagick-filter-types ()
757 "Return a list of the ImageMagick types to be treated as images, or nil.
758 This is the result of `imagemagick-types', including only elements
759 that match `imagemagick-enabled-types' and do not match
760 `imagemagick-types-inhibit'."
761 (when (fboundp 'imagemagick-types
)
762 (cond ((null imagemagick-enabled-types
) nil
)
763 ((eq imagemagick-types-inhibit t
) nil
)
768 (unless (memq type imagemagick-types-inhibit
)
769 (if (eq imagemagick-enabled-types t
) type
771 (dolist (enable imagemagick-enabled-types nil
)
772 (if (cond ((symbolp enable
) (eq enable type
))
775 (symbol-name type
))))
776 (throw 'found type
)))))))
777 (imagemagick-types)))))))
779 (defvar imagemagick--file-regexp nil
780 "File extension regexp for ImageMagick files, if any.
781 This is the extension installed into `auto-mode-alist' and
782 `image-type-file-name-regexps' by `imagemagick-register-types'.")
785 (defun imagemagick-register-types ()
786 "Register file types that can be handled by ImageMagick.
787 This function is called at startup, after loading the init file.
788 It registers the ImageMagick types returned by `imagemagick-filter-types'.
790 Registered image types are added to `auto-mode-alist', so that
791 Emacs visits them in Image mode. They are also added to
792 `image-type-file-name-regexps', so that the `image-type' function
793 recognizes these files as having image type `imagemagick'.
795 If Emacs is compiled without ImageMagick support, this does nothing."
796 (when (fboundp 'imagemagick-types
)
797 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type
)))
798 (imagemagick-filter-types)))
799 (re (if types
(concat "\\." (regexp-opt types
) "\\'")))
800 (ama-elt (car (member (cons imagemagick--file-regexp
'image-mode
)
802 (itfnr-elt (car (member (cons imagemagick--file-regexp
'imagemagick
)
803 image-type-file-name-regexps
))))
805 (setq auto-mode-alist
(delete ama-elt auto-mode-alist
)
806 image-type-file-name-regexps
807 (delete itfnr-elt image-type-file-name-regexps
))
810 (push (cons re
'image-mode
) auto-mode-alist
))
812 (setcar itfnr-elt re
)
813 ;; Append to `image-type-file-name-regexps', so that we
814 ;; preferentially use specialized image libraries.
815 (add-to-list 'image-type-file-name-regexps
816 (cons re
'imagemagick
) t
)))
817 (setq imagemagick--file-regexp re
))))
819 (defcustom imagemagick-types-inhibit
820 '(C HTML HTM INFO M TXT PDF
)
821 "List of ImageMagick types that should never be treated as images.
822 This should be a list of symbols, each of which should be one of
823 the ImageMagick types listed by `imagemagick-types'. The listed
824 image types are not registered by `imagemagick-register-types'.
826 If the value is t, inhibit the use of ImageMagick for images.
828 If you change this without using customize, you must call
829 `imagemagick-register-types' afterwards.
831 If Emacs is compiled without ImageMagick support, this variable
833 :type
'(choice (const :tag
"Support all ImageMagick types" nil
)
834 (const :tag
"Disable all ImageMagick types" t
)
836 :initialize
'custom-initialize-default
837 :set
(lambda (symbol value
)
838 (set-default symbol value
)
839 (imagemagick-register-types))
843 (defcustom imagemagick-enabled-types
844 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
845 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
846 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
847 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
848 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
849 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
850 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
851 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
852 XWD YCbCr YCbCrA YUV
)
853 "List of ImageMagick types to treat as images.
854 Each list element should be a string or symbol, representing one
855 of the image types returned by `imagemagick-types'. If the
856 element is a string, it is handled as a regexp that enables all
859 The value of `imagemagick-enabled-types' may also be t, meaning
860 to enable all types that ImageMagick supports.
862 The variable `imagemagick-types-inhibit' overrides this variable.
864 If you change this without using customize, you must call
865 `imagemagick-register-types' afterwards.
867 If Emacs is compiled without ImageMagick support, this variable
869 :type
'(choice (const :tag
"Support all ImageMagick types" t
)
870 (const :tag
"Disable all ImageMagick types" nil
)
871 (repeat :tag
"List of types"
872 (choice (symbol :tag
"type")
873 (regexp :tag
"regexp"))))
874 :initialize
'custom-initialize-default
875 :set
(lambda (symbol value
)
876 (set-default symbol value
)
877 (imagemagick-register-types))
881 (imagemagick-register-types)
885 ;;; image.el ends here