Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / lisp / image.el
blob6c15a7d0b96836d42421351727e4519d3085a7e6
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: multimedia
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;;; Code:
29 (defgroup image ()
30 "Image support."
31 :group 'multimedia)
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]*<"
51 comment-re "*"
52 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
53 "[Ss][Vv][Gg]"))
54 . svg)
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
64 '(("\\.png\\'" . png)
65 ("\\.gif\\'" . gif)
66 ("\\.jpe?g\\'" . jpeg)
67 ("\\.bmp\\'" . bmp)
68 ("\\.xpm\\'" . xpm)
69 ("\\.pbm\\'" . pbm)
70 ("\\.xbm\\'" . xbm)
71 ("\\.ps\\'" . postscript)
72 ("\\.tiff?\\'" . tiff)
73 ("\\.svgz?\\'" . svg)
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
83 '((pbm . t)
84 (xbm . nil)
85 (bmp . maybe)
86 (gif . maybe)
87 (png . maybe)
88 (xpm . nil)
89 (jpeg . maybe)
90 (tiff . maybe)
91 (svg . maybe)
92 (postscript . nil))
93 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
94 \(See `image-type-auto-detected-p').
96 AUTODETECT can be
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
135 `load-path'.
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
143 `image-load-path':
145 ;; Shush compiler.
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)
151 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.
157 (let ((img image)
158 (dir (or
159 ;; Images in image-load-path.
160 (image-search-load-path image)
161 ;; Images in load-path.
162 (locate-library image)))
163 parent)
164 ;; Since the image might be in a nested directory (for
165 ;; example, mail/attach.pbm), adjust `image-directory'
166 ;; accordingly.
167 (when dir
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'.
178 (cond
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.
192 (setq
193 ;; Go down 2 levels.
194 d2ei (file-name-as-directory
195 (expand-file-name
196 (concat (file-name-directory library-name) "../../etc/images")))
197 ;; Go down 1 level.
198 d1ei (file-name-as-directory
199 (expand-file-name
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))
208 (no-error
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))
225 (catch 'jfif
226 (let ((len (length data)) (i 2))
227 (while (< i len)
228 (when (/= (aref data i) #xff)
229 (throw 'jfif nil))
230 (setq i (1+ i))
231 (when (>= (+ i 2) len)
232 (throw 'jfif nil))
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"
238 (throw 'jfif
239 (string-match-p "JFIF\\|Exif"
240 (substring data i (min (+ i nbytes) len)))))
241 (setq i (+ i 1 nbytes))))))))
244 ;;;###autoload
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
248 be determined."
249 (let ((types image-type-header-regexps)
250 type)
251 (while types
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
260 types nil)
261 (setq types (cdr types)))))
262 type))
265 ;;;###autoload
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
269 be determined."
270 (let ((types image-type-header-regexps)
271 type
272 (opoint (point)))
273 (goto-char (point-min))
274 (while types
275 (let ((regexp (car (car types)))
276 (image-type (cdr (car types)))
277 data)
278 (if (or (and (symbolp image-type)
279 (looking-at-p regexp))
280 (and (consp image-type)
281 (funcall (car image-type)
282 (or data
283 (setq data
284 (buffer-substring
285 (point-min)
286 (min (point-max)
287 (+ (point-min) 256))))))
288 (setq image-type (cdr image-type))))
289 (setq type image-type
290 types nil)
291 (setq types (cdr types)))))
292 (goto-char opoint)
293 (and type
294 (memq type image-types)
295 type)))
298 ;;;###autoload
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
302 be determined."
303 (unless (or (file-readable-p file)
304 (file-name-absolute-p file))
305 (setq file (image-search-load-path file)))
306 (and file
307 (file-readable-p file)
308 (with-temp-buffer
309 (set-buffer-multibyte nil)
310 (insert-file-contents-literally file nil 0 256)
311 (image-type-from-buffer))))
314 ;;;###autoload
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
318 be determined."
319 (let (type first)
320 (catch 'found
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)))
324 (throw 'found type)
325 ;; If nothing seems to be supported, return first type that matched.
326 (or first (setq first type))))))))
328 ;;;###autoload
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))
339 (unless type
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))
347 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
355 'image-library-alist
356 'dynamic-library-alist "24.1")
358 ;;;###autoload
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)))
366 ;;;###autoload
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
375 must be available."
376 (let* ((type (image-type-from-buffer))
377 (auto (and type (cdr (assq type image-type-auto-detectable)))))
378 (and auto
379 (or (eq auto t) (image-type-available-p type)))))
382 ;;;###autoload
383 (defun create-image (file-or-data &optional type data-p &rest props)
384 "Create an image.
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)
404 props)))
407 ;;;###autoload
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)
432 overlay)))
435 ;;;###autoload
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))
457 (if 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)))
467 (insert string)
468 (add-text-properties start (point)
469 `(display ,(if slice
470 (list (cons 'slice slice) image)
471 image) rear-nonsticky (display)))))
474 ;;;###autoload
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))
490 (if 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))))
501 (while (< y 1.0)
502 (while (< x 1.0)
503 (let ((start (point)))
504 (insert string)
505 (add-text-properties start (point)
506 `(display ,(list (list 'slice x y dx dy) image)
507 rear-nonsticky (display)))
508 (setq x (+ x dx))))
509 (setq x 0.0
510 y (+ y dy))
511 (insert (propertize "\n" 'line-height t)))))
515 ;;;###autoload
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."
520 (unless buffer
521 (setq buffer (current-buffer)))
522 (let ((overlays (overlays-in start end)))
523 (while overlays
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)
530 (unless path
531 (setq path image-load-path))
532 (let (element found filename)
533 (while (and (not found) (consp path))
534 (setq element (car path))
535 (cond
536 ((stringp element)
537 (setq found
538 (file-readable-p
539 (setq filename (expand-file-name file element)))))
540 ((and (symbolp element) (boundp element))
541 (setq element (symbol-value element))
542 (cond
543 ((stringp element)
544 (setq found
545 (file-readable-p
546 (setq filename (expand-file-name file element)))))
547 ((consp element)
548 (if (setq filename (image-search-load-path file element))
549 (setq found t))))))
550 (setq path (cdr path)))
551 (if found filename)))
553 ;;;###autoload
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
567 satisfied.
569 The image is looked for in `image-load-path'.
571 Image files should not be larger than specified by `max-image-size'."
572 (let (image)
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))
578 found)
579 (when (image-type-available-p type)
580 (cond ((stringp file)
581 (if (setq found (image-search-load-path file))
582 (setq image
583 (cons 'image (plist-put (copy-sequence spec)
584 :file found)))))
585 ((not (null data))
586 (setq image (cons 'image spec)))))
587 (setq specs (cdr specs))))
588 image))
591 ;;;###autoload
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
605 define SYMBOL.
607 Example:
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")
642 ;; "Destructively"?
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))
652 timer)
653 (when animation
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)
658 0 limit))))
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)
664 (while tail
665 (setq timer (car tail)
666 tail (cdr tail))
667 (if (and (eq (timer--function timer) 'image-animate-timeout)
668 (eq (car-safe (timer--args timer)) image))
669 (setq tail nil)
670 (setq timer nil)))
671 timer))
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."
684 (unless nocheck
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
699 (if multiply
700 (* value (image-animate-get-speed image))
701 value)))
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
706 ;; argument?
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))
722 (time (float-time))
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)
727 (/ 1 (abs speed)))
728 time (- (float-time)))
729 image-minimum-frame-delay))
730 done)
731 (setq n (if (< speed 0)
732 (1- n)
733 (1+ n)))
734 (if limit
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))
739 (if (numberp limit)
740 (setq done (>= time-elapsed limit)))
741 (unless done
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)
758 (delq nil
759 (mapcar
760 (lambda (type)
761 (unless (memq type imagemagick-types-inhibit)
762 (if (eq imagemagick-enabled-types t) type
763 (catch 'found
764 (dolist (enable imagemagick-enabled-types nil)
765 (if (cond ((symbolp enable) (eq enable type))
766 ((stringp enable)
767 (string-match enable
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'.")
777 ;;;###autoload
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)
794 auto-mode-alist)))
795 (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
796 image-type-file-name-regexps))))
797 (if (not re)
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))
801 (if ama-elt
802 (setcar ama-elt re)
803 (push (cons re 'image-mode) auto-mode-alist))
804 (if itfnr-elt
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
825 has no effect."
826 :type '(choice (const :tag "Support all ImageMagick types" nil)
827 (const :tag "Disable all ImageMagick types" t)
828 (repeat symbol))
829 :initialize 'custom-initialize-default
830 :set (lambda (symbol value)
831 (set-default symbol value)
832 (imagemagick-register-types))
833 :version "24.3"
834 :group 'image)
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
850 matching types.
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
861 has no effect."
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))
871 :version "24.3"
872 :group 'image)
874 (imagemagick-register-types)
876 (provide 'image)
878 ;;; image.el ends here