* lisp/image.el (image-load-path): Doc fix.
[emacs.git] / lisp / image.el
blob6ce5b82ec48a8d36f2d2149a01ea5121df6a7dad
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.
120 Subdirectories are not automatically included in the search."
121 :type '(repeat (choice directory variable))
122 :initialize 'custom-initialize-delay)
125 (defun image-load-path-for-library (library image &optional path no-error)
126 "Return a suitable search path for images used by LIBRARY.
128 It searches for IMAGE in `image-load-path' (excluding
129 \"`data-directory'/images\") and `load-path', followed by a path
130 suitable for LIBRARY, which includes \"../../etc/images\" and
131 \"../etc/images\" relative to the library file itself, and then
132 in \"`data-directory'/images\".
134 Then this function returns a list of directories which contains
135 first the directory in which IMAGE was found, followed by the
136 value of `load-path'. If PATH is given, it is used instead of
137 `load-path'.
139 If NO-ERROR is non-nil and a suitable path can't be found, don't
140 signal an error. Instead, return a list of directories as before,
141 except that nil appears in place of the image directory.
143 Here is an example that uses a common idiom to provide
144 compatibility with versions of Emacs that lack the variable
145 `image-load-path':
147 ;; Shush compiler.
148 (defvar image-load-path)
150 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
151 (image-load-path (cons (car load-path)
152 (when (boundp 'image-load-path)
153 image-load-path))))
154 (mh-tool-bar-folder-buttons-init))"
155 (unless library (error "No library specified"))
156 (unless image (error "No image specified"))
157 (let (image-directory image-directory-load-path)
158 ;; Check for images in image-load-path or load-path.
159 (let ((img image)
160 (dir (or
161 ;; Images in image-load-path.
162 (image-search-load-path image)
163 ;; Images in load-path.
164 (locate-library image)))
165 parent)
166 ;; Since the image might be in a nested directory (for
167 ;; example, mail/attach.pbm), adjust `image-directory'
168 ;; accordingly.
169 (when dir
170 (setq dir (file-name-directory dir))
171 (while (setq parent (file-name-directory img))
172 (setq img (directory-file-name parent)
173 dir (expand-file-name "../" dir))))
174 (setq image-directory-load-path dir))
176 ;; If `image-directory-load-path' isn't Emacs's image directory,
177 ;; it's probably a user preference, so use it. Then use a
178 ;; relative setting if possible; otherwise, use
179 ;; `image-directory-load-path'.
180 (cond
181 ;; User-modified image-load-path?
182 ((and image-directory-load-path
183 (not (equal image-directory-load-path
184 (file-name-as-directory
185 (expand-file-name "images" data-directory)))))
186 (setq image-directory image-directory-load-path))
187 ;; Try relative setting.
188 ((let (library-name d1ei d2ei)
189 ;; First, find library in the load-path.
190 (setq library-name (locate-library library))
191 (if (not library-name)
192 (error "Cannot find library %s in load-path" library))
193 ;; And then set image-directory relative to that.
194 (setq
195 ;; Go down 2 levels.
196 d2ei (file-name-as-directory
197 (expand-file-name
198 (concat (file-name-directory library-name) "../../etc/images")))
199 ;; Go down 1 level.
200 d1ei (file-name-as-directory
201 (expand-file-name
202 (concat (file-name-directory library-name) "../etc/images"))))
203 (setq image-directory
204 ;; Set it to nil if image is not found.
205 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
206 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
207 ;; Use Emacs's image directory.
208 (image-directory-load-path
209 (setq image-directory image-directory-load-path))
210 (no-error
211 (message "Could not find image %s for library %s" image library))
213 (error "Could not find image %s for library %s" image library)))
215 ;; Return an augmented `path' or `load-path'.
216 (nconc (list image-directory)
217 (delete image-directory (copy-sequence (or path load-path))))))
220 ;; Used to be in image-type-header-regexps, but now not used anywhere
221 ;; (since 2009-08-28).
222 (defun image-jpeg-p (data)
223 "Value is non-nil if DATA, a string, consists of JFIF image data.
224 We accept the tag Exif because that is the same format."
225 (setq data (ignore-errors (string-to-unibyte data)))
226 (when (and data (string-match-p "\\`\xff\xd8" data))
227 (catch 'jfif
228 (let ((len (length data)) (i 2))
229 (while (< i len)
230 (when (/= (aref data i) #xff)
231 (throw 'jfif nil))
232 (setq i (1+ i))
233 (when (>= (+ i 2) len)
234 (throw 'jfif nil))
235 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
236 (aref data (+ i 2))))
237 (code (aref data i)))
238 (when (and (>= code #xe0) (<= code #xef))
239 ;; APP0 LEN1 LEN2 "JFIF\0"
240 (throw 'jfif
241 (string-match-p "JFIF\\|Exif"
242 (substring data i (min (+ i nbytes) len)))))
243 (setq i (+ i 1 nbytes))))))))
246 ;;;###autoload
247 (defun image-type-from-data (data)
248 "Determine the image type from image data DATA.
249 Value is a symbol specifying the image type or nil if type cannot
250 be determined."
251 (let ((types image-type-header-regexps)
252 type)
253 (while types
254 (let ((regexp (car (car types)))
255 (image-type (cdr (car types))))
256 (if (or (and (symbolp image-type)
257 (string-match-p regexp data))
258 (and (consp image-type)
259 (funcall (car image-type) data)
260 (setq image-type (cdr image-type))))
261 (setq type image-type
262 types nil)
263 (setq types (cdr types)))))
264 type))
267 ;;;###autoload
268 (defun image-type-from-buffer ()
269 "Determine the image type from data in the current buffer.
270 Value is a symbol specifying the image type or nil if type cannot
271 be determined."
272 (let ((types image-type-header-regexps)
273 type
274 (opoint (point)))
275 (goto-char (point-min))
276 (while types
277 (let ((regexp (car (car types)))
278 (image-type (cdr (car types)))
279 data)
280 (if (or (and (symbolp image-type)
281 (looking-at-p regexp))
282 (and (consp image-type)
283 (funcall (car image-type)
284 (or data
285 (setq data
286 (buffer-substring
287 (point-min)
288 (min (point-max)
289 (+ (point-min) 256))))))
290 (setq image-type (cdr image-type))))
291 (setq type image-type
292 types nil)
293 (setq types (cdr types)))))
294 (goto-char opoint)
295 (and type
296 (boundp 'image-types)
297 (memq type image-types)
298 type)))
301 ;;;###autoload
302 (defun image-type-from-file-header (file)
303 "Determine the type of image file FILE from its first few bytes.
304 Value is a symbol specifying the image type, or nil if type cannot
305 be determined."
306 (unless (or (file-readable-p file)
307 (file-name-absolute-p file))
308 (setq file (image-search-load-path file)))
309 (and file
310 (file-readable-p file)
311 (with-temp-buffer
312 (set-buffer-multibyte nil)
313 (insert-file-contents-literally file nil 0 256)
314 (image-type-from-buffer))))
317 ;;;###autoload
318 (defun image-type-from-file-name (file)
319 "Determine the type of image file FILE from its name.
320 Value is a symbol specifying the image type, or nil if type cannot
321 be determined."
322 (let (type first)
323 (catch 'found
324 (dolist (elem image-type-file-name-regexps first)
325 (when (string-match-p (car elem) file)
326 (if (image-type-available-p (setq type (cdr elem)))
327 (throw 'found type)
328 ;; If nothing seems to be supported, return first type that matched.
329 (or first (setq first type))))))))
331 ;;;###autoload
332 (defun image-type (source &optional type data-p)
333 "Determine and return image type.
334 SOURCE is an image file name or image data.
335 Optional TYPE is a symbol describing the image type. If TYPE is omitted
336 or nil, try to determine the image type from its first few bytes
337 of image data. If that doesn't work, and SOURCE is a file name,
338 use its file extension as image type.
339 Optional DATA-P non-nil means SOURCE is a string containing image data."
340 (when (and (not data-p) (not (stringp source)))
341 (error "Invalid image file name `%s'" source))
342 (unless type
343 (setq type (if data-p
344 (image-type-from-data source)
345 (or (image-type-from-file-header source)
346 (image-type-from-file-name source))))
347 (or type (error "Cannot determine image type")))
348 (or (memq type (and (boundp 'image-types) image-types))
349 (error "Invalid image type `%s'" type))
350 type)
353 (if (fboundp 'image-metadata) ; eg not --without-x
354 (define-obsolete-function-alias 'image-extension-data
355 'image-metadata' "24.1"))
357 (define-obsolete-variable-alias
358 'image-library-alist
359 'dynamic-library-alist "24.1")
361 ;;;###autoload
362 (defun image-type-available-p (type)
363 "Return non-nil if image type TYPE is available.
364 Image types are symbols like `xbm' or `jpeg'."
365 (and (fboundp 'init-image-library)
366 (init-image-library type)))
369 ;;;###autoload
370 (defun image-type-auto-detected-p ()
371 "Return t if the current buffer contains an auto-detectable image.
372 This function is intended to be used from `magic-fallback-mode-alist'.
374 The buffer is considered to contain an auto-detectable image if
375 its beginning matches an image type in `image-type-header-regexps',
376 and that image type is present in `image-type-auto-detectable' with a
377 non-nil value. If that value is non-nil, but not t, then the image type
378 must be available."
379 (let* ((type (image-type-from-buffer))
380 (auto (and type (cdr (assq type image-type-auto-detectable)))))
381 (and auto
382 (or (eq auto t) (image-type-available-p type)))))
385 ;;;###autoload
386 (defun create-image (file-or-data &optional type data-p &rest props)
387 "Create an image.
388 FILE-OR-DATA is an image file name or image data.
389 Optional TYPE is a symbol describing the image type. If TYPE is omitted
390 or nil, try to determine the image type from its first few bytes
391 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
392 use its file extension as image type.
393 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
394 Optional PROPS are additional image attributes to assign to the image,
395 like, e.g. `:mask MASK'.
396 Value is the image created, or nil if images of type TYPE are not supported.
398 Images should not be larger than specified by `max-image-size'.
400 Image file names that are not absolute are searched for in the
401 \"images\" sub-directory of `data-directory' and
402 `x-bitmap-file-path' (in that order)."
403 ;; It is x_find_image_file in image.c that sets the search path.
404 (setq type (image-type file-or-data type data-p))
405 (when (image-type-available-p type)
406 (append (list 'image :type type (if data-p :data :file) file-or-data)
407 props)))
410 ;;;###autoload
411 (defun put-image (image pos &optional string area)
412 "Put image IMAGE in front of POS in the current buffer.
413 IMAGE must be an image created with `create-image' or `defimage'.
414 IMAGE is displayed by putting an overlay into the current buffer with a
415 `before-string' STRING that has a `display' property whose value is the
416 image. STRING is defaulted if you omit it.
417 The overlay created will have the `put-image' property set to t.
418 POS may be an integer or marker.
419 AREA is where to display the image. AREA nil or omitted means
420 display it in the text area, a value of `left-margin' means
421 display it in the left marginal area, a value of `right-margin'
422 means display it in the right marginal area."
423 (unless string (setq string "x"))
424 (let ((buffer (current-buffer)))
425 (unless (eq (car-safe image) 'image)
426 (error "Not an image: %s" image))
427 (unless (or (null area) (memq area '(left-margin right-margin)))
428 (error "Invalid area %s" area))
429 (setq string (copy-sequence string))
430 (let ((overlay (make-overlay pos pos buffer))
431 (prop (if (null area) image (list (list 'margin area) image))))
432 (put-text-property 0 (length string) 'display prop string)
433 (overlay-put overlay 'put-image t)
434 (overlay-put overlay 'before-string string)
435 overlay)))
438 ;;;###autoload
439 (defun insert-image (image &optional string area slice)
440 "Insert IMAGE into current buffer at point.
441 IMAGE is displayed by inserting STRING into the current buffer
442 with a `display' property whose value is the image. STRING
443 defaults to a single space if you omit it.
444 AREA is where to display the image. AREA nil or omitted means
445 display it in the text area, a value of `left-margin' means
446 display it in the left marginal area, a value of `right-margin'
447 means display it in the right marginal area.
448 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
449 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
450 specifying the X and Y positions and WIDTH and HEIGHT of image area
451 to insert. A float value 0.0 - 1.0 means relative to the width or
452 height of the image; integer values are taken as pixel values."
453 ;; Use a space as least likely to cause trouble when it's a hidden
454 ;; character in the buffer.
455 (unless string (setq string " "))
456 (unless (eq (car-safe image) 'image)
457 (error "Not an image: %s" image))
458 (unless (or (null area) (memq area '(left-margin right-margin)))
459 (error "Invalid area %s" area))
460 (if area
461 (setq image (list (list 'margin area) image))
462 ;; Cons up a new spec equal but not eq to `image' so that
463 ;; inserting it twice in a row (adjacently) displays two copies of
464 ;; the image. Don't try to avoid this by looking at the display
465 ;; properties on either side so that we DTRT more often with
466 ;; cut-and-paste. (Yanking killed image text next to another copy
467 ;; of it loses anyway.)
468 (setq image (cons 'image (cdr image))))
469 (let ((start (point)))
470 (insert string)
471 (add-text-properties start (point)
472 `(display ,(if slice
473 (list (cons 'slice slice) image)
474 image) rear-nonsticky (display)))))
477 ;;;###autoload
478 (defun insert-sliced-image (image &optional string area rows cols)
479 "Insert IMAGE into current buffer at point.
480 IMAGE is displayed by inserting STRING into the current buffer
481 with a `display' property whose value is the image. The default
482 STRING is a single space.
483 AREA is where to display the image. AREA nil or omitted means
484 display it in the text area, a value of `left-margin' means
485 display it in the left marginal area, a value of `right-margin'
486 means display it in the right marginal area.
487 The image is automatically split into ROWS x COLS slices."
488 (unless string (setq string " "))
489 (unless (eq (car-safe image) 'image)
490 (error "Not an image: %s" image))
491 (unless (or (null area) (memq area '(left-margin right-margin)))
492 (error "Invalid area %s" area))
493 (if area
494 (setq image (list (list 'margin area) image))
495 ;; Cons up a new spec equal but not eq to `image' so that
496 ;; inserting it twice in a row (adjacently) displays two copies of
497 ;; the image. Don't try to avoid this by looking at the display
498 ;; properties on either side so that we DTRT more often with
499 ;; cut-and-paste. (Yanking killed image text next to another copy
500 ;; of it loses anyway.)
501 (setq image (cons 'image (cdr image))))
502 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
503 (y 0.0) (dy (/ 1.0001 (or rows 1))))
504 (while (< y 1.0)
505 (while (< x 1.0)
506 (let ((start (point)))
507 (insert string)
508 (add-text-properties start (point)
509 `(display ,(list (list 'slice x y dx dy) image)
510 rear-nonsticky (display)))
511 (setq x (+ x dx))))
512 (setq x 0.0
513 y (+ y dy))
514 (insert (propertize "\n" 'line-height t)))))
518 ;;;###autoload
519 (defun remove-images (start end &optional buffer)
520 "Remove images between START and END in BUFFER.
521 Remove only images that were put in BUFFER with calls to `put-image'.
522 BUFFER nil or omitted means use the current buffer."
523 (unless buffer
524 (setq buffer (current-buffer)))
525 (let ((overlays (overlays-in start end)))
526 (while overlays
527 (let ((overlay (car overlays)))
528 (when (overlay-get overlay 'put-image)
529 (delete-overlay overlay)))
530 (setq overlays (cdr overlays)))))
532 (defun image-search-load-path (file &optional path)
533 (unless path
534 (setq path image-load-path))
535 (let (element found filename)
536 (while (and (not found) (consp path))
537 (setq element (car path))
538 (cond
539 ((stringp element)
540 (setq found
541 (file-readable-p
542 (setq filename (expand-file-name file element)))))
543 ((and (symbolp element) (boundp element))
544 (setq element (symbol-value element))
545 (cond
546 ((stringp element)
547 (setq found
548 (file-readable-p
549 (setq filename (expand-file-name file element)))))
550 ((consp element)
551 (if (setq filename (image-search-load-path file element))
552 (setq found t))))))
553 (setq path (cdr path)))
554 (if found filename)))
556 ;;;###autoload
557 (defun find-image (specs)
558 "Find an image, choosing one of a list of image specifications.
560 SPECS is a list of image specifications.
562 Each image specification in SPECS is a property list. The contents of
563 a specification are image type dependent. All specifications must at
564 least contain the properties `:type TYPE' and either `:file FILE' or
565 `:data DATA', where TYPE is a symbol specifying the image type,
566 e.g. `xbm', FILE is the file to load the image from, and DATA is a
567 string containing the actual image data. The specification whose TYPE
568 is supported, and FILE exists, is used to construct the image
569 specification to be returned. Return nil if no specification is
570 satisfied.
572 The image is looked for in `image-load-path'.
574 Image files should not be larger than specified by `max-image-size'."
575 (let (image)
576 (while (and specs (null image))
577 (let* ((spec (car specs))
578 (type (plist-get spec :type))
579 (data (plist-get spec :data))
580 (file (plist-get spec :file))
581 found)
582 (when (image-type-available-p type)
583 (cond ((stringp file)
584 (if (setq found (image-search-load-path file))
585 (setq image
586 (cons 'image (plist-put (copy-sequence spec)
587 :file found)))))
588 ((not (null data))
589 (setq image (cons 'image spec)))))
590 (setq specs (cdr specs))))
591 image))
594 ;;;###autoload
595 (defmacro defimage (symbol specs &optional doc)
596 "Define SYMBOL as an image, and return SYMBOL.
598 SPECS is a list of image specifications. DOC is an optional
599 documentation string.
601 Each image specification in SPECS is a property list. The contents of
602 a specification are image type dependent. All specifications must at
603 least contain the properties `:type TYPE' and either `:file FILE' or
604 `:data DATA', where TYPE is a symbol specifying the image type,
605 e.g. `xbm', FILE is the file to load the image from, and DATA is a
606 string containing the actual image data. The first image
607 specification whose TYPE is supported, and FILE exists, is used to
608 define SYMBOL.
610 Example:
612 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
613 (:type xbm :file \"~/test1.xbm\")))"
614 (declare (doc-string 3))
615 `(defvar ,symbol (find-image ',specs) ,doc))
618 ;;; Animated image API
620 (defvar image-default-frame-delay 0.1
621 "Default interval in seconds between frames of a multi-frame image.
622 Only used if the image does not specify a value.")
624 (defun image-multi-frame-p (image)
625 "Return non-nil if IMAGE contains more than one frame.
626 The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
627 the number of frames (or sub-images) in the image and DELAY is the delay
628 in seconds that the image specifies between each frame. DELAY may be nil,
629 in which case you might want to use `image-default-frame-delay'."
630 (when (fboundp 'image-metadata)
631 (let* ((metadata (image-metadata image))
632 (images (plist-get metadata 'count))
633 (delay (plist-get metadata 'delay)))
634 (when (and images (> images 1))
635 (if (or (not (numberp delay)) (< delay 0))
636 (setq delay image-default-frame-delay))
637 (cons images delay)))))
639 (defun image-animated-p (image)
640 "Like `image-multi-frame-p', but returns nil if no delay is specified."
641 (let ((multi (image-multi-frame-p image)))
642 (and (cdr multi) multi)))
644 (make-obsolete 'image-animated-p 'image-multi-frame-p "24.4")
646 ;; "Destructively"?
647 (defun image-animate (image &optional index limit)
648 "Start animating IMAGE.
649 Animation occurs by destructively altering the IMAGE spec list.
651 With optional INDEX, begin animating from that animation frame.
652 LIMIT specifies how long to animate the image. If omitted or
653 nil, play the animation until the end. If t, loop forever. If a
654 number, play until that number of seconds has elapsed."
655 (let ((animation (image-multi-frame-p image))
656 timer)
657 (when animation
658 (if (setq timer (image-animate-timer image))
659 (cancel-timer timer))
660 (run-with-timer 0.2 nil 'image-animate-timeout
661 image (or index 0) (car animation)
662 0 limit))))
664 (defun image-animate-timer (image)
665 "Return the animation timer for image IMAGE."
666 ;; See cancel-function-timers
667 (let ((tail timer-list) timer)
668 (while tail
669 (setq timer (car tail)
670 tail (cdr tail))
671 (if (and (eq (timer--function timer) 'image-animate-timeout)
672 (eq (car-safe (timer--args timer)) image))
673 (setq tail nil)
674 (setq timer nil)))
675 timer))
677 (defconst image-minimum-frame-delay 0.01
678 "Minimum interval in seconds between frames of an animated image.")
680 (defun image-current-frame (image)
681 "The current frame number of IMAGE, indexed from 0."
682 (or (plist-get (cdr image) :index) 0))
684 (defun image-show-frame (image n &optional nocheck)
685 "Show frame N of IMAGE.
686 Frames are indexed from 0. Optional argument NOCHECK non-nil means
687 do not check N is within the range of frames present in the image."
688 (unless nocheck
689 (if (< n 0) (setq n 0)
690 (setq n (min n (1- (car (image-multi-frame-p image)))))))
691 (plist-put (cdr image) :index n)
692 (force-window-update))
694 (defun image-animate-get-speed (image)
695 "Return the speed factor for animating IMAGE."
696 (or (plist-get (cdr image) :speed) 1))
698 (defun image-animate-set-speed (image value &optional multiply)
699 "Set the speed factor for animating IMAGE to VALUE.
700 With optional argument MULTIPLY non-nil, treat VALUE as a
701 multiplication factor for the current value."
702 (plist-put (cdr image) :speed
703 (if multiply
704 (* value (image-animate-get-speed image))
705 value)))
707 ;; FIXME? The delay may not be the same for different sub-images,
708 ;; hence we need to call image-multi-frame-p to return it.
709 ;; But it also returns count, so why do we bother passing that as an
710 ;; argument?
711 (defun image-animate-timeout (image n count time-elapsed limit)
712 "Display animation frame N of IMAGE.
713 N=0 refers to the initial animation frame.
714 COUNT is the total number of frames in the animation.
715 TIME-ELAPSED is the total time that has elapsed since
716 `image-animate-start' was called.
717 LIMIT determines when to stop. If t, loop forever. If nil, stop
718 after displaying the last animation frame. Otherwise, stop
719 after LIMIT seconds have elapsed.
720 The minimum delay between successive frames is `image-minimum-frame-delay'.
722 If the image has a non-nil :speed property, it acts as a multiplier
723 for the animation speed. A negative value means to animate in reverse."
724 (image-show-frame image n t)
725 (let* ((speed (image-animate-get-speed image))
726 (time (float-time))
727 (animation (image-multi-frame-p image))
728 ;; Subtract off the time we took to load the image from the
729 ;; stated delay time.
730 (delay (max (+ (* (or (cdr animation) image-default-frame-delay)
731 (/ 1 (abs speed)))
732 time (- (float-time)))
733 image-minimum-frame-delay))
734 done)
735 (setq n (if (< speed 0)
736 (1- n)
737 (1+ n)))
738 (if limit
739 (cond ((>= n count) (setq n 0))
740 ((< n 0) (setq n (1- count))))
741 (and (or (>= n count) (< n 0)) (setq done t)))
742 (setq time-elapsed (+ delay time-elapsed))
743 (if (numberp limit)
744 (setq done (>= time-elapsed limit)))
745 (unless done
746 (run-with-timer delay nil 'image-animate-timeout
747 image n count time-elapsed limit))))
750 (defvar imagemagick-types-inhibit)
751 (defvar imagemagick-enabled-types)
753 (defun imagemagick-filter-types ()
754 "Return a list of the ImageMagick types to be treated as images, or nil.
755 This is the result of `imagemagick-types', including only elements
756 that match `imagemagick-enabled-types' and do not match
757 `imagemagick-types-inhibit'."
758 (when (fboundp 'imagemagick-types)
759 (cond ((null imagemagick-enabled-types) nil)
760 ((eq imagemagick-types-inhibit t) nil)
762 (delq nil
763 (mapcar
764 (lambda (type)
765 (unless (memq type imagemagick-types-inhibit)
766 (if (eq imagemagick-enabled-types t) type
767 (catch 'found
768 (dolist (enable imagemagick-enabled-types nil)
769 (if (cond ((symbolp enable) (eq enable type))
770 ((stringp enable)
771 (string-match enable
772 (symbol-name type))))
773 (throw 'found type)))))))
774 (imagemagick-types)))))))
776 (defvar imagemagick--file-regexp nil
777 "File extension regexp for ImageMagick files, if any.
778 This is the extension installed into `auto-mode-alist' and
779 `image-type-file-name-regexps' by `imagemagick-register-types'.")
781 ;;;###autoload
782 (defun imagemagick-register-types ()
783 "Register file types that can be handled by ImageMagick.
784 This function is called at startup, after loading the init file.
785 It registers the ImageMagick types returned by `imagemagick-filter-types'.
787 Registered image types are added to `auto-mode-alist', so that
788 Emacs visits them in Image mode. They are also added to
789 `image-type-file-name-regexps', so that the `image-type' function
790 recognizes these files as having image type `imagemagick'.
792 If Emacs is compiled without ImageMagick support, this does nothing."
793 (when (fboundp 'imagemagick-types)
794 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type)))
795 (imagemagick-filter-types)))
796 (re (if types (concat "\\." (regexp-opt types) "\\'")))
797 (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
798 auto-mode-alist)))
799 (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
800 image-type-file-name-regexps))))
801 (if (not re)
802 (setq auto-mode-alist (delete ama-elt auto-mode-alist)
803 image-type-file-name-regexps
804 (delete itfnr-elt image-type-file-name-regexps))
805 (if ama-elt
806 (setcar ama-elt re)
807 (push (cons re 'image-mode) auto-mode-alist))
808 (if itfnr-elt
809 (setcar itfnr-elt re)
810 ;; Append to `image-type-file-name-regexps', so that we
811 ;; preferentially use specialized image libraries.
812 (add-to-list 'image-type-file-name-regexps
813 (cons re 'imagemagick) t)))
814 (setq imagemagick--file-regexp re))))
816 (defcustom imagemagick-types-inhibit
817 '(C HTML HTM INFO M TXT PDF)
818 "List of ImageMagick types that should never be treated as images.
819 This should be a list of symbols, each of which should be one of
820 the ImageMagick types listed by `imagemagick-types'. The listed
821 image types are not registered by `imagemagick-register-types'.
823 If the value is t, inhibit the use of ImageMagick for images.
825 If you change this without using customize, you must call
826 `imagemagick-register-types' afterwards.
828 If Emacs is compiled without ImageMagick support, this variable
829 has no effect."
830 :type '(choice (const :tag "Support all ImageMagick types" nil)
831 (const :tag "Disable all ImageMagick types" t)
832 (repeat symbol))
833 :initialize 'custom-initialize-default
834 :set (lambda (symbol value)
835 (set-default symbol value)
836 (imagemagick-register-types))
837 :version "24.3"
838 :group 'image)
840 (defcustom imagemagick-enabled-types
841 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
842 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
843 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
844 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
845 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
846 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
847 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
848 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
849 XWD YCbCr YCbCrA YUV)
850 "List of ImageMagick types to treat as images.
851 Each list element should be a string or symbol, representing one
852 of the image types returned by `imagemagick-types'. If the
853 element is a string, it is handled as a regexp that enables all
854 matching types.
856 The value of `imagemagick-enabled-types' may also be t, meaning
857 to enable all types that ImageMagick supports.
859 The variable `imagemagick-types-inhibit' overrides this variable.
861 If you change this without using customize, you must call
862 `imagemagick-register-types' afterwards.
864 If Emacs is compiled without ImageMagick support, this variable
865 has no effect."
866 :type '(choice (const :tag "Support all ImageMagick types" t)
867 (const :tag "Disable all ImageMagick types" nil)
868 (repeat :tag "List of types"
869 (choice (symbol :tag "type")
870 (regexp :tag "regexp"))))
871 :initialize 'custom-initialize-default
872 :set (lambda (symbol value)
873 (set-default symbol value)
874 (imagemagick-register-types))
875 :version "24.3"
876 :group 'image)
878 (imagemagick-register-types)
880 (provide 'image)
882 ;;; image.el ends here