* image.el (image-animated-p): Tweak definition.
[bpt/emacs.git] / lisp / image.el
CommitLineData
7840ced1
GM
1;;; image.el --- image API
2
ab422c4d 3;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
634f4a67
PJ
4
5;; Maintainer: FSF
04799cf5 6;; Keywords: multimedia
bd78fa1d 7;; Package: emacs
7840ced1
GM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
7840ced1 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
7840ced1
GM
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7840ced1
GM
23
24;;; Commentary:
25
26;;; Code:
27
77f6105c
MB
28
29(defgroup image ()
30 "Image support."
31 :group 'multimedia)
32
110683ad 33(defalias 'image-refresh 'image-flush)
77f6105c 34
4fde92ef 35(defconst image-type-header-regexps
62564fc7 36 `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
6642c904 37 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
3205431d 38 ("\\`GIF8[79]a" . gif)
6642c904 39 ("\\`\x89PNG\r\n\x1a\n" . png)
fa4354c1
AS
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\\)?\
44static \\(unsigned \\)?char \\1_bits" . xbm)
6642c904
JB
45 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
46 ("\\`[\t\n\r ]*%!PS" . postscript)
d30a05d1 47 ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg)
62564fc7
JL
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)
55 )
6642c904 56 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
7840ced1 57When the first bytes of an image file match REGEXP, it is assumed to
6642c904
JB
58be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
59IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
60with one argument, a string containing the image data. If PREDICATE returns
61a non-nil value, TYPE is the image's type.")
8a8ef149 62
5fd62452 63(defvar image-type-file-name-regexps
4fde92ef
KS
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)
62564fc7
JL
72 ("\\.tiff?\\'" . tiff)
73 ("\\.svgz?\\'" . svg)
74 )
4fde92ef
KS
75 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
76When the name of an image file match REGEXP, it is assumed to
77be of image type IMAGE-TYPE.")
78
a9bb04bb
CY
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.
6642c904
JB
82(defvar image-type-auto-detectable
83 '((pbm . t)
a9bb04bb 84 (xbm . nil)
6642c904
JB
85 (bmp . maybe)
86 (gif . maybe)
87 (png . maybe)
a9bb04bb 88 (xpm . nil)
6642c904
JB
89 (jpeg . maybe)
90 (tiff . maybe)
62564fc7 91 (svg . maybe)
6642c904
JB
92 (postscript . nil))
93 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
94\(See `image-type-auto-detected-p').
95
96AUTODETECT 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').")
4fde92ef 101
45448e64
SM
102(defcustom image-load-path
103 (list (file-name-as-directory (expand-file-name "images" data-directory))
104 'data-directory 'load-path)
5fc5ac38 105 "List of locations in which to search for image files.
93a75651
CY
106If an element is a string, it defines a directory to search.
107If an element is a variable symbol whose value is a string, that
108value defines a directory to search.
109If an element is a variable symbol whose value is a list, the
45448e64
SM
110value is used as a list of directories to search."
111 :type '(repeat (choice directory variable))
112 :initialize 'custom-initialize-delay)
dada660a 113
5248a565 114
20c65d08 115(defun image-load-path-for-library (library image &optional path no-error)
ae77c7ff 116 "Return a suitable search path for images used by LIBRARY.
7c565097 117
40db64d2 118It searches for IMAGE in `image-load-path' (excluding
c0696e1b
BW
119\"`data-directory'/images\") and `load-path', followed by a path
120suitable for LIBRARY, which includes \"../../etc/images\" and
121\"../etc/images\" relative to the library file itself, and then
122in \"`data-directory'/images\".
7c565097 123
5248a565
BW
124Then this function returns a list of directories which contains
125first the directory in which IMAGE was found, followed by the
68ba6c49 126value of `load-path'. If PATH is given, it is used instead of
5248a565 127`load-path'.
20c65d08 128
5248a565 129If NO-ERROR is non-nil and a suitable path can't be found, don't
68ba6c49 130signal an error. Instead, return a list of directories as before,
5248a565 131except that nil appears in place of the image directory.
7c565097
BW
132
133Here is an example that uses a common idiom to provide
134compatibility with versions of Emacs that lack the variable
135`image-load-path':
136
9f036d33
BW
137 ;; Shush compiler.
138 (defvar image-load-path)
5248a565
BW
139
140 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
9f036d33
BW
141 (image-load-path (cons (car load-path)
142 (when (boundp 'image-load-path)
143 image-load-path))))
5248a565 144 (mh-tool-bar-folder-buttons-init))"
7c565097
BW
145 (unless library (error "No library specified"))
146 (unless image (error "No image specified"))
c0696e1b
BW
147 (let (image-directory image-directory-load-path)
148 ;; Check for images in image-load-path or load-path.
149 (let ((img image)
150 (dir (or
151 ;; Images in image-load-path.
152 (image-search-load-path image)
153 ;; Images in load-path.
154 (locate-library image)))
155 parent)
156 ;; Since the image might be in a nested directory (for
157 ;; example, mail/attach.pbm), adjust `image-directory'
158 ;; accordingly.
159 (when dir
160 (setq dir (file-name-directory dir))
161 (while (setq parent (file-name-directory img))
162 (setq img (directory-file-name parent)
163 dir (expand-file-name "../" dir))))
164 (setq image-directory-load-path dir))
165
44e97401 166 ;; If `image-directory-load-path' isn't Emacs's image directory,
c0696e1b
BW
167 ;; it's probably a user preference, so use it. Then use a
168 ;; relative setting if possible; otherwise, use
169 ;; `image-directory-load-path'.
7c565097 170 (cond
c0696e1b
BW
171 ;; User-modified image-load-path?
172 ((and image-directory-load-path
173 (not (equal image-directory-load-path
174 (file-name-as-directory
175 (expand-file-name "images" data-directory)))))
176 (setq image-directory image-directory-load-path))
7c565097
BW
177 ;; Try relative setting.
178 ((let (library-name d1ei d2ei)
179 ;; First, find library in the load-path.
180 (setq library-name (locate-library library))
181 (if (not library-name)
182 (error "Cannot find library %s in load-path" library))
183 ;; And then set image-directory relative to that.
184 (setq
185 ;; Go down 2 levels.
c0696e1b
BW
186 d2ei (file-name-as-directory
187 (expand-file-name
188 (concat (file-name-directory library-name) "../../etc/images")))
7c565097 189 ;; Go down 1 level.
c0696e1b
BW
190 d1ei (file-name-as-directory
191 (expand-file-name
192 (concat (file-name-directory library-name) "../etc/images"))))
7c565097
BW
193 (setq image-directory
194 ;; Set it to nil if image is not found.
195 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
196 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
44e97401 197 ;; Use Emacs's image directory.
c0696e1b
BW
198 (image-directory-load-path
199 (setq image-directory image-directory-load-path))
20c65d08 200 (no-error
20c65d08 201 (message "Could not find image %s for library %s" image library))
7c565097
BW
202 (t
203 (error "Could not find image %s for library %s" image library)))
204
5248a565
BW
205 ;; Return an augmented `path' or `load-path'.
206 (nconc (list image-directory)
207 (delete image-directory (copy-sequence (or path load-path))))))
208
7c565097 209
b4ac6e8c
GM
210;; Used to be in image-type-header-regexps, but now not used anywhere
211;; (since 2009-08-28).
8a8ef149 212(defun image-jpeg-p (data)
9b78a6a3
RS
213 "Value is non-nil if DATA, a string, consists of JFIF image data.
214We accept the tag Exif because that is the same format."
24c23999
JB
215 (setq data (ignore-errors (string-to-unibyte data)))
216 (when (and data (string-match-p "\\`\xff\xd8" data))
8a8ef149
GM
217 (catch 'jfif
218 (let ((len (length data)) (i 2))
219 (while (< i len)
220 (when (/= (aref data i) #xff)
221 (throw 'jfif nil))
222 (setq i (1+ i))
223 (when (>= (+ i 2) len)
224 (throw 'jfif nil))
225 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
4a9bf8a4
GM
226 (aref data (+ i 2))))
227 (code (aref data i)))
228 (when (and (>= code #xe0) (<= code #xef))
8a8ef149 229 ;; APP0 LEN1 LEN2 "JFIF\0"
71296446 230 (throw 'jfif
24c23999
JB
231 (string-match-p "JFIF\\|Exif"
232 (substring data i (min (+ i nbytes) len)))))
3bdbead3 233 (setq i (+ i 1 nbytes))))))))
7840ced1
GM
234
235
236;;;###autoload
162dec01
GM
237(defun image-type-from-data (data)
238 "Determine the image type from image data DATA.
239Value is a symbol specifying the image type or nil if type cannot
7840ced1 240be determined."
4fde92ef 241 (let ((types image-type-header-regexps)
7840ced1 242 type)
4fde92ef 243 (while types
7840ced1 244 (let ((regexp (car (car types)))
6642c904 245 (image-type (cdr (car types))))
4fde92ef 246 (if (or (and (symbolp image-type)
24c23999 247 (string-match-p regexp data))
4fde92ef
KS
248 (and (consp image-type)
249 (funcall (car image-type) data)
250 (setq image-type (cdr image-type))))
251 (setq type image-type
252 types nil)
253 (setq types (cdr types)))))
254 type))
255
256
257;;;###autoload
6642c904 258(defun image-type-from-buffer ()
4fde92ef 259 "Determine the image type from data in the current buffer.
6642c904
JB
260Value is a symbol specifying the image type or nil if type cannot
261be determined."
4fde92ef
KS
262 (let ((types image-type-header-regexps)
263 type
264 (opoint (point)))
265 (goto-char (point-min))
266 (while types
267 (let ((regexp (car (car types)))
6642c904 268 (image-type (cdr (car types)))
4fde92ef
KS
269 data)
270 (if (or (and (symbolp image-type)
24c23999 271 (looking-at-p regexp))
4fde92ef
KS
272 (and (consp image-type)
273 (funcall (car image-type)
274 (or data
275 (setq data
276 (buffer-substring
277 (point-min)
278 (min (point-max)
279 (+ (point-min) 256))))))
280 (setq image-type (cdr image-type))))
6642c904 281 (setq type image-type
4fde92ef
KS
282 types nil)
283 (setq types (cdr types)))))
284 (goto-char opoint)
dce04f7f
CY
285 (and type
286 (memq type image-types)
287 type)))
7840ced1
GM
288
289
162dec01
GM
290;;;###autoload
291(defun image-type-from-file-header (file)
292 "Determine the type of image file FILE from its first few bytes.
293Value is a symbol specifying the image type, or nil if type cannot
294be determined."
4fde92ef
KS
295 (unless (or (file-readable-p file)
296 (file-name-absolute-p file))
297 (setq file (image-search-load-path file)))
298 (and file
299 (file-readable-p file)
300 (with-temp-buffer
301 (set-buffer-multibyte nil)
302 (insert-file-contents-literally file nil 0 256)
6642c904 303 (image-type-from-buffer))))
4fde92ef
KS
304
305
306;;;###autoload
307(defun image-type-from-file-name (file)
308 "Determine the type of image file FILE from its name.
309Value is a symbol specifying the image type, or nil if type cannot
310be determined."
ea1d4aac 311 (let (type first)
11fef14a
GM
312 (catch 'found
313 (dolist (elem image-type-file-name-regexps first)
314 (when (string-match-p (car elem) file)
315 (if (image-type-available-p (setq type (cdr elem)))
316 (throw 'found type)
317 ;; If nothing seems to be supported, return first type that matched.
318 (or first (setq first type))))))))
162dec01 319
7840ced1 320;;;###autoload
7d88e015 321(defun image-type (source &optional type data-p)
e7968373 322 "Determine and return image type.
7d88e015 323SOURCE is an image file name or image data.
7840ced1 324Optional TYPE is a symbol describing the image type. If TYPE is omitted
162dec01 325or nil, try to determine the image type from its first few bytes
7d88e015 326of image data. If that doesn't work, and SOURCE is a file name,
9ea8de1b 327use its file extension as image type.
7d88e015
CY
328Optional DATA-P non-nil means SOURCE is a string containing image data."
329 (when (and (not data-p) (not (stringp source)))
330 (error "Invalid image file name `%s'" source))
162dec01 331 (unless type
7d88e015
CY
332 (setq type (if data-p
333 (image-type-from-data source)
334 (or (image-type-from-file-header source)
335 (image-type-from-file-name source))))
336 (or type (error "Cannot determine image type")))
28681e35 337 (or (memq type (and (boundp 'image-types) image-types))
7d88e015 338 (error "Invalid image type `%s'" type))
e7968373
KS
339 type)
340
6642c904 341
82ff1d13
GM
342(if (fboundp 'image-metadata) ; eg not --without-x
343 (define-obsolete-function-alias 'image-extension-data
344 'image-metadata' "24.1"))
345
2e288d54
JB
346(define-obsolete-variable-alias
347 'image-library-alist
348 'dynamic-library-alist "24.1")
aa360da1 349
e7968373
KS
350;;;###autoload
351(defun image-type-available-p (type)
352 "Return non-nil if image type TYPE is available.
353Image types are symbols like `xbm' or `jpeg'."
354 (and (fboundp 'init-image-library)
d07ff9db 355 (init-image-library type)))
e7968373
KS
356
357
6642c904
JB
358;;;###autoload
359(defun image-type-auto-detected-p ()
4837b516 360 "Return t if the current buffer contains an auto-detectable image.
7d88e015
CY
361This function is intended to be used from `magic-fallback-mode-alist'.
362
363The buffer is considered to contain an auto-detectable image if
364its beginning matches an image type in `image-type-header-regexps',
1e83e2ca
GM
365and that image type is present in `image-type-auto-detectable' with a
366non-nil value. If that value is non-nil, but not t, then the image type
367must be available."
6642c904
JB
368 (let* ((type (image-type-from-buffer))
369 (auto (and type (cdr (assq type image-type-auto-detectable)))))
d567be22 370 (and auto
7d88e015 371 (or (eq auto t) (image-type-available-p type)))))
6642c904
JB
372
373
e7968373
KS
374;;;###autoload
375(defun create-image (file-or-data &optional type data-p &rest props)
376 "Create an image.
377FILE-OR-DATA is an image file name or image data.
378Optional TYPE is a symbol describing the image type. If TYPE is omitted
379or nil, try to determine the image type from its first few bytes
380of image data. If that doesn't work, and FILE-OR-DATA is a file name,
381use its file extension as image type.
382Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
383Optional PROPS are additional image attributes to assign to the image,
384like, e.g. `:mask MASK'.
385Value is the image created, or nil if images of type TYPE are not supported.
386
2467875c
GM
387Images should not be larger than specified by `max-image-size'.
388
389Image file names that are not absolute are searched for in the
390\"images\" sub-directory of `data-directory' and
391`x-bitmap-file-path' (in that order)."
392 ;; It is x_find_image_file in image.c that sets the search path.
e7968373 393 (setq type (image-type file-or-data type data-p))
7840ced1 394 (when (image-type-available-p type)
162dec01
GM
395 (append (list 'image :type type (if data-p :data :file) file-or-data)
396 props)))
7840ced1
GM
397
398
399;;;###autoload
cedbd8d0 400(defun put-image (image pos &optional string area)
1571b5ff 401 "Put image IMAGE in front of POS in the current buffer.
7840ced1 402IMAGE must be an image created with `create-image' or `defimage'.
46655c94
GM
403IMAGE is displayed by putting an overlay into the current buffer with a
404`before-string' STRING that has a `display' property whose value is the
cedbd8d0 405image. STRING is defaulted if you omit it.
12dc863d 406The overlay created will have the `put-image' property set to t.
7840ced1 407POS may be an integer or marker.
7840ced1
GM
408AREA is where to display the image. AREA nil or omitted means
409display it in the text area, a value of `left-margin' means
410display it in the left marginal area, a value of `right-margin'
46655c94 411means display it in the right marginal area."
0ad550ba 412 (unless string (setq string "x"))
1571b5ff 413 (let ((buffer (current-buffer)))
cedbd8d0 414 (unless (eq (car-safe image) 'image)
1571b5ff
GM
415 (error "Not an image: %s" image))
416 (unless (or (null area) (memq area '(left-margin right-margin)))
417 (error "Invalid area %s" area))
46655c94 418 (setq string (copy-sequence string))
1571b5ff 419 (let ((overlay (make-overlay pos pos buffer))
46655c94
GM
420 (prop (if (null area) image (list (list 'margin area) image))))
421 (put-text-property 0 (length string) 'display prop string)
1571b5ff 422 (overlay-put overlay 'put-image t)
bc72b5d9
LMI
423 (overlay-put overlay 'before-string string)
424 overlay)))
7840ced1
GM
425
426
427;;;###autoload
5af275e0 428(defun insert-image (image &optional string area slice)
7840ced1 429 "Insert IMAGE into current buffer at point.
46655c94 430IMAGE is displayed by inserting STRING into the current buffer
0f0a88b9 431with a `display' property whose value is the image. STRING
913c8291 432defaults to a single space if you omit it.
7840ced1
GM
433AREA is where to display the image. AREA nil or omitted means
434display it in the text area, a value of `left-margin' means
435display it in the left marginal area, a value of `right-margin'
5af275e0
KS
436means display it in the right marginal area.
437SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
438means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
439specifying the X and Y positions and WIDTH and HEIGHT of image area
440to insert. A float value 0.0 - 1.0 means relative to the width or
441height of the image; integer values are taken as pixel values."
0ad550ba
DL
442 ;; Use a space as least likely to cause trouble when it's a hidden
443 ;; character in the buffer.
444 (unless string (setq string " "))
cedbd8d0 445 (unless (eq (car-safe image) 'image)
7840ced1
GM
446 (error "Not an image: %s" image))
447 (unless (or (null area) (memq area '(left-margin right-margin)))
448 (error "Invalid area %s" area))
0dc91c57
DL
449 (if area
450 (setq image (list (list 'margin area) image))
451 ;; Cons up a new spec equal but not eq to `image' so that
452 ;; inserting it twice in a row (adjacently) displays two copies of
453 ;; the image. Don't try to avoid this by looking at the display
454 ;; properties on either side so that we DTRT more often with
455 ;; cut-and-paste. (Yanking killed image text next to another copy
456 ;; of it loses anyway.)
457 (setq image (cons 'image (cdr image))))
46655c94
GM
458 (let ((start (point)))
459 (insert string)
460 (add-text-properties start (point)
5af275e0
KS
461 `(display ,(if slice
462 (list (cons 'slice slice) image)
463 image) rear-nonsticky (display)))))
464
465
f22c36d3 466;;;###autoload
5af275e0 467(defun insert-sliced-image (image &optional string area rows cols)
b48bc937
KS
468 "Insert IMAGE into current buffer at point.
469IMAGE is displayed by inserting STRING into the current buffer
913c8291
GM
470with a `display' property whose value is the image. The default
471STRING is a single space.
b48bc937
KS
472AREA is where to display the image. AREA nil or omitted means
473display it in the text area, a value of `left-margin' means
474display it in the left marginal area, a value of `right-margin'
475means display it in the right marginal area.
68ba6c49 476The image is automatically split into ROWS x COLS slices."
5af275e0
KS
477 (unless string (setq string " "))
478 (unless (eq (car-safe image) 'image)
479 (error "Not an image: %s" image))
480 (unless (or (null area) (memq area '(left-margin right-margin)))
481 (error "Invalid area %s" area))
482 (if area
483 (setq image (list (list 'margin area) image))
484 ;; Cons up a new spec equal but not eq to `image' so that
485 ;; inserting it twice in a row (adjacently) displays two copies of
486 ;; the image. Don't try to avoid this by looking at the display
487 ;; properties on either side so that we DTRT more often with
488 ;; cut-and-paste. (Yanking killed image text next to another copy
489 ;; of it loses anyway.)
490 (setq image (cons 'image (cdr image))))
491 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
492 (y 0.0) (dy (/ 1.0001 (or rows 1))))
493 (while (< y 1.0)
494 (while (< x 1.0)
495 (let ((start (point)))
496 (insert string)
497 (add-text-properties start (point)
498 `(display ,(list (list 'slice x y dx dy) image)
499 rear-nonsticky (display)))
500 (setq x (+ x dx))))
501 (setq x 0.0
502 y (+ y dy))
82dc617b 503 (insert (propertize "\n" 'line-height t)))))
5af275e0 504
10e2102e 505
7840ced1
GM
506
507;;;###autoload
508(defun remove-images (start end &optional buffer)
509 "Remove images between START and END in BUFFER.
510Remove only images that were put in BUFFER with calls to `put-image'.
511BUFFER nil or omitted means use the current buffer."
512 (unless buffer
513 (setq buffer (current-buffer)))
514 (let ((overlays (overlays-in start end)))
515 (while overlays
516 (let ((overlay (car overlays)))
517 (when (overlay-get overlay 'put-image)
0c898dd9
DL
518 (delete-overlay overlay)))
519 (setq overlays (cdr overlays)))))
7840ced1 520
4fde92ef
KS
521(defun image-search-load-path (file &optional path)
522 (unless path
523 (setq path image-load-path))
524 (let (element found filename)
5fc5ac38 525 (while (and (not found) (consp path))
93a75651 526 (setq element (car path))
5fc5ac38 527 (cond
93a75651 528 ((stringp element)
5fc5ac38
CY
529 (setq found
530 (file-readable-p
4fde92ef 531 (setq filename (expand-file-name file element)))))
93a75651
CY
532 ((and (symbolp element) (boundp element))
533 (setq element (symbol-value element))
534 (cond
535 ((stringp element)
536 (setq found
537 (file-readable-p
4fde92ef 538 (setq filename (expand-file-name file element)))))
93a75651 539 ((consp element)
4fde92ef 540 (if (setq filename (image-search-load-path file element))
93a75651 541 (setq found t))))))
5fc5ac38 542 (setq path (cdr path)))
4fde92ef 543 (if found filename)))
7840ced1
GM
544
545;;;###autoload
349c034d
GM
546(defun find-image (specs)
547 "Find an image, choosing one of a list of image specifications.
7840ced1 548
cedbd8d0 549SPECS is a list of image specifications.
7840ced1
GM
550
551Each image specification in SPECS is a property list. The contents of
552a specification are image type dependent. All specifications must at
bc283707
WP
553least contain the properties `:type TYPE' and either `:file FILE' or
554`:data DATA', where TYPE is a symbol specifying the image type,
555e.g. `xbm', FILE is the file to load the image from, and DATA is a
cedbd8d0
DL
556string containing the actual image data. The specification whose TYPE
557is supported, and FILE exists, is used to construct the image
558specification to be returned. Return nil if no specification is
559satisfied.
560
8646a62e
CY
561The image is looked for in `image-load-path'.
562
563Image files should not be larger than specified by `max-image-size'."
7840ced1
GM
564 (let (image)
565 (while (and specs (null image))
566 (let* ((spec (car specs))
567 (type (plist-get spec :type))
162dec01 568 (data (plist-get spec :data))
7f228379
GM
569 (file (plist-get spec :file))
570 found)
162dec01
GM
571 (when (image-type-available-p type)
572 (cond ((stringp file)
4fde92ef 573 (if (setq found (image-search-load-path file))
5fc5ac38
CY
574 (setq image
575 (cons 'image (plist-put (copy-sequence spec)
576 :file found)))))
f64ae86c 577 ((not (null data))
162dec01
GM
578 (setq image (cons 'image spec)))))
579 (setq specs (cdr specs))))
349c034d
GM
580 image))
581
582
583;;;###autoload
584(defmacro defimage (symbol specs &optional doc)
585 "Define SYMBOL as an image.
586
587SPECS is a list of image specifications. DOC is an optional
588documentation string.
589
590Each image specification in SPECS is a property list. The contents of
591a specification are image type dependent. All specifications must at
592least contain the properties `:type TYPE' and either `:file FILE' or
593`:data DATA', where TYPE is a symbol specifying the image type,
594e.g. `xbm', FILE is the file to load the image from, and DATA is a
595string containing the actual image data. The first image
596specification whose TYPE is supported, and FILE exists, is used to
597define SYMBOL.
598
599Example:
600
601 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
602 (:type xbm :file \"~/test1.xbm\")))"
fd8e7a80 603 (declare (doc-string 3))
349c034d 604 `(defvar ,symbol (find-image ',specs) ,doc))
7840ced1 605
0608aa45
KS
606\f
607;;; Animated image API
7840ced1 608
99e619b6
GM
609(defvar image-default-frame-delay 0.1
610 "Default interval in seconds between frames of a multi-frame image.
611Only used if the image does not specify a value.")
612
ed8d7fca
GM
613(defun image-multi-frame-p (image)
614 "Return non-nil if IMAGE contains more than one frame.
615The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
616the number of frames (or sub-images) in the image and DELAY is the delay
617in seconds that the image specifies between each frame. DELAY may be nil,
618in which case you might want to use `image-default-frame-delay'."
619 (let* ((metadata (image-metadata image))
620 (images (plist-get metadata 'count))
621 (delay (plist-get metadata 'delay)))
622 (when (and images (> images 1))
623 (if (or (not (numberp delay)) (< delay 0))
624 (setq delay image-default-frame-delay))
625 (cons images delay))))
626
dd8620de
GM
627(defun image-animated-p (image)
628 "Like `image-multi-frame-p', but returns nil if no delay is specified."
629 (let ((multi (image-multi-frame-p image)))
630 (and (cdr multi) multi)))
631
632(make-obsolete 'image-animated-p 'image-multi-frame-p "24.4")
0608aa45 633
eea14f31 634;; "Destructively"?
18af70d0
CY
635(defun image-animate (image &optional index limit)
636 "Start animating IMAGE.
637Animation occurs by destructively altering the IMAGE spec list.
638
639With optional INDEX, begin animating from that animation frame.
640LIMIT specifies how long to animate the image. If omitted or
641nil, play the animation until the end. If t, loop forever. If a
642number, play until that number of seconds has elapsed."
ed8d7fca 643 (let ((animation (image-multi-frame-p image))
1100a63c
CY
644 timer)
645 (when animation
18af70d0
CY
646 (if (setq timer (image-animate-timer image))
647 (cancel-timer timer))
1100a63c
CY
648 (run-with-timer 0.2 nil 'image-animate-timeout
649 image (or index 0) (car animation)
650 0 limit))))
0608aa45
KS
651
652(defun image-animate-timer (image)
653 "Return the animation timer for image IMAGE."
654 ;; See cancel-function-timers
655 (let ((tail timer-list) timer)
656 (while tail
657 (setq timer (car tail)
658 tail (cdr tail))
72eac303
PE
659 (if (and (eq (timer--function timer) 'image-animate-timeout)
660 (eq (car-safe (timer--args timer)) image))
0608aa45
KS
661 (setq tail nil)
662 (setq timer nil)))
663 timer))
664
99e619b6
GM
665(defconst image-minimum-frame-delay 0.01
666 "Minimum interval in seconds between frames of an animated image.")
667
dc504515
GM
668(defun image-current-frame (image)
669 "The current frame number of IMAGE, indexed from 0."
670 (or (plist-get (cdr image) :index) 0))
bb9dfee1 671
dc504515 672(defun image-show-frame (image n &optional nocheck)
c0211c4e
GM
673 "Show frame N of IMAGE.
674Frames are indexed from 0. Optional argument NOCHECK non-nil means
675do not check N is within the range of frames present in the image."
676 (unless nocheck
677 (if (< n 0) (setq n 0)
ed8d7fca 678 (setq n (min n (1- (car (image-multi-frame-p image)))))))
c0211c4e 679 (plist-put (cdr image) :index n)
c0211c4e
GM
680 (force-window-update))
681
eea14f31 682;; FIXME? The delay may not be the same for different sub-images,
ed8d7fca 683;; hence we need to call image-multi-frame-p to return it.
eea14f31
GM
684;; But it also returns count, so why do we bother passing that as an
685;; argument?
1100a63c 686(defun image-animate-timeout (image n count time-elapsed limit)
e8cbec34
CY
687 "Display animation frame N of IMAGE.
688N=0 refers to the initial animation frame.
689COUNT is the total number of frames in the animation.
e8cbec34
CY
690TIME-ELAPSED is the total time that has elapsed since
691`image-animate-start' was called.
18af70d0 692LIMIT determines when to stop. If t, loop forever. If nil, stop
e8cbec34 693 after displaying the last animation frame. Otherwise, stop
eea14f31 694 after LIMIT seconds have elapsed.
99e619b6 695The minimum delay between successive frames is `image-minimum-frame-delay'."
dc504515 696 (image-show-frame image n t)
1100a63c
CY
697 (setq n (1+ n))
698 (let* ((time (float-time))
ed8d7fca 699 (animation (image-multi-frame-p image))
1100a63c
CY
700 ;; Subtract off the time we took to load the image from the
701 ;; stated delay time.
ed8d7fca
GM
702 (delay (max (+ (or (cdr animation) image-default-frame-delay)
703 time (- (float-time)))
99e619b6 704 image-minimum-frame-delay))
1100a63c 705 done)
e8cbec34 706 (if (>= n count)
18af70d0 707 (if limit
e8cbec34
CY
708 (setq n 0)
709 (setq done t)))
710 (setq time-elapsed (+ delay time-elapsed))
18af70d0
CY
711 (if (numberp limit)
712 (setq done (>= time-elapsed limit)))
e8cbec34
CY
713 (unless done
714 (run-with-timer delay nil 'image-animate-timeout
1100a63c 715 image n count time-elapsed limit))))
0608aa45
KS
716
717\f
60b5f187 718(defvar imagemagick-types-inhibit)
95f520b5 719(defvar imagemagick-enabled-types)
60b5f187
GM
720
721(defun imagemagick-filter-types ()
722 "Return a list of the ImageMagick types to be treated as images, or nil.
723This is the result of `imagemagick-types', including only elements
95f520b5 724that match `imagemagick-enabled-types' and do not match
60b5f187
GM
725`imagemagick-types-inhibit'."
726 (when (fboundp 'imagemagick-types)
95f520b5 727 (cond ((null imagemagick-enabled-types) nil)
60b5f187 728 ((eq imagemagick-types-inhibit t) nil)
60b5f187
GM
729 (t
730 (delq nil
731 (mapcar
732 (lambda (type)
733 (unless (memq type imagemagick-types-inhibit)
95f520b5 734 (if (eq imagemagick-enabled-types t) type
43f5c7ae 735 (catch 'found
95f520b5 736 (dolist (enable imagemagick-enabled-types nil)
43f5c7ae
GM
737 (if (cond ((symbolp enable) (eq enable type))
738 ((stringp enable)
739 (string-match enable
740 (symbol-name type))))
741 (throw 'found type)))))))
60b5f187
GM
742 (imagemagick-types)))))))
743
c505aaeb
CY
744(defvar imagemagick--file-regexp nil
745 "File extension regexp for ImageMagick files, if any.
746This is the extension installed into `auto-mode-alist' and
747`image-type-file-name-regexps' by `imagemagick-register-types'.")
3de25479
JV
748
749;;;###autoload
ce07fa9a 750(defun imagemagick-register-types ()
d66c4c7c 751 "Register file types that can be handled by ImageMagick.
c505aaeb 752This function is called at startup, after loading the init file.
60b5f187 753It registers the ImageMagick types returned by `imagemagick-filter-types'.
5319014e
CY
754
755Registered image types are added to `auto-mode-alist', so that
756Emacs visits them in Image mode. They are also added to
757`image-type-file-name-regexps', so that the `image-type' function
758recognizes these files as having image type `imagemagick'.
d66c4c7c 759
32d72c2f 760If Emacs is compiled without ImageMagick support, this does nothing."
d66c4c7c 761 (when (fboundp 'imagemagick-types)
60b5f187
GM
762 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type)))
763 (imagemagick-filter-types)))
764 (re (if types (concat "\\." (regexp-opt types) "\\'")))
32d72c2f
GM
765 (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
766 auto-mode-alist)))
767 (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
768 image-type-file-name-regexps))))
769 (if (not re)
770 (setq auto-mode-alist (delete ama-elt auto-mode-alist)
771 image-type-file-name-regexps
772 (delete itfnr-elt image-type-file-name-regexps))
773 (if ama-elt
774 (setcar ama-elt re)
775 (push (cons re 'image-mode) auto-mode-alist))
776 (if itfnr-elt
777 (setcar itfnr-elt re)
72834e10
CY
778 ;; Append to `image-type-file-name-regexps', so that we
779 ;; preferentially use specialized image libraries.
780 (add-to-list 'image-type-file-name-regexps
781 (cons re 'imagemagick) t)))
c505aaeb
CY
782 (setq imagemagick--file-regexp re))))
783
041df390
CY
784(defcustom imagemagick-types-inhibit
785 '(C HTML HTM INFO M TXT PDF)
95f520b5 786 "List of ImageMagick types that should never be treated as images.
c505aaeb 787This should be a list of symbols, each of which should be one of
95f520b5 788the ImageMagick types listed by `imagemagick-types'. The listed
c505aaeb
CY
789image types are not registered by `imagemagick-register-types'.
790
791If the value is t, inhibit the use of ImageMagick for images.
792
87eb79c2
GM
793If you change this without using customize, you must call
794`imagemagick-register-types' afterwards.
795
c505aaeb
CY
796If Emacs is compiled without ImageMagick support, this variable
797has no effect."
798 :type '(choice (const :tag "Support all ImageMagick types" nil)
799 (const :tag "Disable all ImageMagick types" t)
800 (repeat symbol))
32d72c2f 801 :initialize 'custom-initialize-default
c505aaeb
CY
802 :set (lambda (symbol value)
803 (set-default symbol value)
804 (imagemagick-register-types))
2a1e2476 805 :version "24.3"
c505aaeb 806 :group 'image)
3de25479 807
95f520b5 808(defcustom imagemagick-enabled-types
47b36b94 809 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
041df390
CY
810 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
811 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
812 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
813 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
814 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
815 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
816 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
817 XWD YCbCr YCbCrA YUV)
32d72c2f 818 "List of ImageMagick types to treat as images.
95f520b5
CY
819Each list element should be a string or symbol, representing one
820of the image types returned by `imagemagick-types'. If the
821element is a string, it is handled as a regexp that enables all
822matching types.
32d72c2f 823
95f520b5
CY
824The value of `imagemagick-enabled-types' may also be t, meaning
825to enable all types that ImageMagick supports.
32d72c2f
GM
826
827The variable `imagemagick-types-inhibit' overrides this variable.
828
eda82a31 829If you change this without using customize, you must call
32d72c2f
GM
830`imagemagick-register-types' afterwards.
831
832If Emacs is compiled without ImageMagick support, this variable
833has no effect."
834 :type '(choice (const :tag "Support all ImageMagick types" t)
835 (const :tag "Disable all ImageMagick types" nil)
836 (repeat :tag "List of types"
837 (choice (symbol :tag "type")
838 (regexp :tag "regexp"))))
839 :initialize 'custom-initialize-default
840 :set (lambda (symbol value)
841 (set-default symbol value)
842 (imagemagick-register-types))
2a1e2476 843 :version "24.3"
32d72c2f
GM
844 :group 'image)
845
846(imagemagick-register-types)
847
7840ced1
GM
848(provide 'image)
849
e43367a0 850;;; image.el ends here