Don't call turn_on_atimers around `connect' (Bug#5723).
[bpt/emacs.git] / lisp / image.el
CommitLineData
7840ced1
GM
1;;; image.el --- image API
2
3e5b7009
GM
3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
634f4a67
PJ
5
6;; Maintainer: FSF
04799cf5 7;; Keywords: multimedia
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
33
4fde92ef 34(defconst image-type-header-regexps
62564fc7 35 `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
6642c904 36 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
3205431d 37 ("\\`GIF8[79]a" . gif)
6642c904 38 ("\\`\x89PNG\r\n\x1a\n" . png)
fa4354c1
AS
39 ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
40#define \\1_height [0-9]+\n\\(\
41#define \\1_x_hot [0-9]+\n\
42#define \\1_y_hot [0-9]+\n\\)?\
43static \\(unsigned \\)?char \\1_bits" . xbm)
6642c904
JB
44 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
45 ("\\`[\t\n\r ]*%!PS" . postscript)
d30a05d1 46 ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg)
62564fc7
JL
47 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
48 (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
49 (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
50 comment-re "*"
51 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
52 "[Ss][Vv][Gg]"))
53 . svg)
54 )
6642c904 55 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
7840ced1 56When the first bytes of an image file match REGEXP, it is assumed to
6642c904
JB
57be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
58IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
59with one argument, a string containing the image data. If PREDICATE returns
60a non-nil value, TYPE is the image's type.")
8a8ef149 61
4fde92ef
KS
62(defconst image-type-file-name-regexps
63 '(("\\.png\\'" . png)
64 ("\\.gif\\'" . gif)
65 ("\\.jpe?g\\'" . jpeg)
66 ("\\.bmp\\'" . bmp)
67 ("\\.xpm\\'" . xpm)
68 ("\\.pbm\\'" . pbm)
69 ("\\.xbm\\'" . xbm)
70 ("\\.ps\\'" . postscript)
62564fc7
JL
71 ("\\.tiff?\\'" . tiff)
72 ("\\.svgz?\\'" . svg)
73 )
4fde92ef
KS
74 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
75When the name of an image file match REGEXP, it is assumed to
76be of image type IMAGE-TYPE.")
77
a9bb04bb
CY
78;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
79;; of content autodetection. Their contents are just C code, so it is
80;; easy to generate false matches.
6642c904
JB
81(defvar image-type-auto-detectable
82 '((pbm . t)
a9bb04bb 83 (xbm . nil)
6642c904
JB
84 (bmp . maybe)
85 (gif . maybe)
86 (png . maybe)
a9bb04bb 87 (xpm . nil)
6642c904
JB
88 (jpeg . maybe)
89 (tiff . maybe)
62564fc7 90 (svg . maybe)
6642c904
JB
91 (postscript . nil))
92 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
93\(See `image-type-auto-detected-p').
94
95AUTODETECT can be
96 - t always auto-detect.
97 - nil never auto-detect.
98 - maybe auto-detect only if the image type is available
99 (see `image-type-available-p').")
4fde92ef 100
45448e64
SM
101(defcustom image-load-path
102 (list (file-name-as-directory (expand-file-name "images" data-directory))
103 'data-directory 'load-path)
5fc5ac38 104 "List of locations in which to search for image files.
93a75651
CY
105If an element is a string, it defines a directory to search.
106If an element is a variable symbol whose value is a string, that
107value defines a directory to search.
108If an element is a variable symbol whose value is a list, the
45448e64
SM
109value is used as a list of directories to search."
110 :type '(repeat (choice directory variable))
111 :initialize 'custom-initialize-delay)
dada660a 112
5248a565 113
20c65d08 114(defun image-load-path-for-library (library image &optional path no-error)
ae77c7ff 115 "Return a suitable search path for images used by LIBRARY.
7c565097 116
40db64d2 117It searches for IMAGE in `image-load-path' (excluding
c0696e1b
BW
118\"`data-directory'/images\") and `load-path', followed by a path
119suitable for LIBRARY, which includes \"../../etc/images\" and
120\"../etc/images\" relative to the library file itself, and then
121in \"`data-directory'/images\".
7c565097 122
5248a565
BW
123Then this function returns a list of directories which contains
124first the directory in which IMAGE was found, followed by the
68ba6c49 125value of `load-path'. If PATH is given, it is used instead of
5248a565 126`load-path'.
20c65d08 127
5248a565 128If NO-ERROR is non-nil and a suitable path can't be found, don't
68ba6c49 129signal an error. Instead, return a list of directories as before,
5248a565 130except that nil appears in place of the image directory.
7c565097
BW
131
132Here is an example that uses a common idiom to provide
133compatibility with versions of Emacs that lack the variable
134`image-load-path':
135
9f036d33
BW
136 ;; Shush compiler.
137 (defvar image-load-path)
5248a565
BW
138
139 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
9f036d33
BW
140 (image-load-path (cons (car load-path)
141 (when (boundp 'image-load-path)
142 image-load-path))))
5248a565 143 (mh-tool-bar-folder-buttons-init))"
7c565097
BW
144 (unless library (error "No library specified"))
145 (unless image (error "No image specified"))
c0696e1b
BW
146 (let (image-directory image-directory-load-path)
147 ;; Check for images in image-load-path or load-path.
148 (let ((img image)
149 (dir (or
150 ;; Images in image-load-path.
151 (image-search-load-path image)
152 ;; Images in load-path.
153 (locate-library image)))
154 parent)
155 ;; Since the image might be in a nested directory (for
156 ;; example, mail/attach.pbm), adjust `image-directory'
157 ;; accordingly.
158 (when dir
159 (setq dir (file-name-directory dir))
160 (while (setq parent (file-name-directory img))
161 (setq img (directory-file-name parent)
162 dir (expand-file-name "../" dir))))
163 (setq image-directory-load-path dir))
164
165 ;; If `image-directory-load-path' isn't Emacs' image directory,
166 ;; it's probably a user preference, so use it. Then use a
167 ;; relative setting if possible; otherwise, use
168 ;; `image-directory-load-path'.
7c565097 169 (cond
c0696e1b
BW
170 ;; User-modified image-load-path?
171 ((and image-directory-load-path
172 (not (equal image-directory-load-path
173 (file-name-as-directory
174 (expand-file-name "images" data-directory)))))
175 (setq image-directory image-directory-load-path))
7c565097
BW
176 ;; Try relative setting.
177 ((let (library-name d1ei d2ei)
178 ;; First, find library in the load-path.
179 (setq library-name (locate-library library))
180 (if (not library-name)
181 (error "Cannot find library %s in load-path" library))
182 ;; And then set image-directory relative to that.
183 (setq
184 ;; Go down 2 levels.
c0696e1b
BW
185 d2ei (file-name-as-directory
186 (expand-file-name
187 (concat (file-name-directory library-name) "../../etc/images")))
7c565097 188 ;; Go down 1 level.
c0696e1b
BW
189 d1ei (file-name-as-directory
190 (expand-file-name
191 (concat (file-name-directory library-name) "../etc/images"))))
7c565097
BW
192 (setq image-directory
193 ;; Set it to nil if image is not found.
194 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
195 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
c0696e1b
BW
196 ;; Use Emacs' image directory.
197 (image-directory-load-path
198 (setq image-directory image-directory-load-path))
20c65d08 199 (no-error
20c65d08 200 (message "Could not find image %s for library %s" image library))
7c565097
BW
201 (t
202 (error "Could not find image %s for library %s" image library)))
203
5248a565
BW
204 ;; Return an augmented `path' or `load-path'.
205 (nconc (list image-directory)
206 (delete image-directory (copy-sequence (or path load-path))))))
207
7c565097 208
8a8ef149 209(defun image-jpeg-p (data)
9b78a6a3
RS
210 "Value is non-nil if DATA, a string, consists of JFIF image data.
211We accept the tag Exif because that is the same format."
24c23999
JB
212 (setq data (ignore-errors (string-to-unibyte data)))
213 (when (and data (string-match-p "\\`\xff\xd8" data))
8a8ef149
GM
214 (catch 'jfif
215 (let ((len (length data)) (i 2))
216 (while (< i len)
217 (when (/= (aref data i) #xff)
218 (throw 'jfif nil))
219 (setq i (1+ i))
220 (when (>= (+ i 2) len)
221 (throw 'jfif nil))
222 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
4a9bf8a4
GM
223 (aref data (+ i 2))))
224 (code (aref data i)))
225 (when (and (>= code #xe0) (<= code #xef))
8a8ef149 226 ;; APP0 LEN1 LEN2 "JFIF\0"
71296446 227 (throw 'jfif
24c23999
JB
228 (string-match-p "JFIF\\|Exif"
229 (substring data i (min (+ i nbytes) len)))))
3bdbead3 230 (setq i (+ i 1 nbytes))))))))
7840ced1
GM
231
232
233;;;###autoload
162dec01
GM
234(defun image-type-from-data (data)
235 "Determine the image type from image data DATA.
236Value is a symbol specifying the image type or nil if type cannot
7840ced1 237be determined."
4fde92ef 238 (let ((types image-type-header-regexps)
7840ced1 239 type)
4fde92ef 240 (while types
7840ced1 241 (let ((regexp (car (car types)))
6642c904 242 (image-type (cdr (car types))))
4fde92ef 243 (if (or (and (symbolp image-type)
24c23999 244 (string-match-p regexp data))
4fde92ef
KS
245 (and (consp image-type)
246 (funcall (car image-type) data)
247 (setq image-type (cdr image-type))))
248 (setq type image-type
249 types nil)
250 (setq types (cdr types)))))
251 type))
252
253
254;;;###autoload
6642c904 255(defun image-type-from-buffer ()
4fde92ef 256 "Determine the image type from data in the current buffer.
6642c904
JB
257Value is a symbol specifying the image type or nil if type cannot
258be determined."
4fde92ef
KS
259 (let ((types image-type-header-regexps)
260 type
261 (opoint (point)))
262 (goto-char (point-min))
263 (while types
264 (let ((regexp (car (car types)))
6642c904 265 (image-type (cdr (car types)))
4fde92ef
KS
266 data)
267 (if (or (and (symbolp image-type)
24c23999 268 (looking-at-p regexp))
4fde92ef
KS
269 (and (consp image-type)
270 (funcall (car image-type)
271 (or data
272 (setq data
273 (buffer-substring
274 (point-min)
275 (min (point-max)
276 (+ (point-min) 256))))))
277 (setq image-type (cdr image-type))))
6642c904 278 (setq type image-type
4fde92ef
KS
279 types nil)
280 (setq types (cdr types)))))
281 (goto-char opoint)
7840ced1
GM
282 type))
283
284
162dec01
GM
285;;;###autoload
286(defun image-type-from-file-header (file)
287 "Determine the type of image file FILE from its first few bytes.
288Value is a symbol specifying the image type, or nil if type cannot
289be determined."
4fde92ef
KS
290 (unless (or (file-readable-p file)
291 (file-name-absolute-p file))
292 (setq file (image-search-load-path file)))
293 (and file
294 (file-readable-p file)
295 (with-temp-buffer
296 (set-buffer-multibyte nil)
297 (insert-file-contents-literally file nil 0 256)
6642c904 298 (image-type-from-buffer))))
4fde92ef
KS
299
300
301;;;###autoload
302(defun image-type-from-file-name (file)
303 "Determine the type of image file FILE from its name.
304Value is a symbol specifying the image type, or nil if type cannot
305be determined."
24c23999 306 (assoc-default file image-type-file-name-regexps 'string-match-p))
162dec01
GM
307
308
7840ced1 309;;;###autoload
7d88e015 310(defun image-type (source &optional type data-p)
e7968373 311 "Determine and return image type.
7d88e015 312SOURCE is an image file name or image data.
7840ced1 313Optional TYPE is a symbol describing the image type. If TYPE is omitted
162dec01 314or nil, try to determine the image type from its first few bytes
7d88e015 315of image data. If that doesn't work, and SOURCE is a file name,
9ea8de1b 316use its file extension as image type.
7d88e015
CY
317Optional DATA-P non-nil means SOURCE is a string containing image data."
318 (when (and (not data-p) (not (stringp source)))
319 (error "Invalid image file name `%s'" source))
162dec01 320 (unless type
7d88e015
CY
321 (setq type (if data-p
322 (image-type-from-data source)
323 (or (image-type-from-file-header source)
324 (image-type-from-file-name source))))
325 (or type (error "Cannot determine image type")))
28681e35 326 (or (memq type (and (boundp 'image-types) image-types))
7d88e015 327 (error "Invalid image type `%s'" type))
e7968373
KS
328 type)
329
6642c904 330
aa360da1
GM
331(defvar image-library-alist)
332
e7968373
KS
333;;;###autoload
334(defun image-type-available-p (type)
335 "Return non-nil if image type TYPE is available.
336Image types are symbols like `xbm' or `jpeg'."
337 (and (fboundp 'init-image-library)
338 (init-image-library type image-library-alist)))
339
340
6642c904
JB
341;;;###autoload
342(defun image-type-auto-detected-p ()
4837b516 343 "Return t if the current buffer contains an auto-detectable image.
7d88e015
CY
344This function is intended to be used from `magic-fallback-mode-alist'.
345
346The buffer is considered to contain an auto-detectable image if
347its beginning matches an image type in `image-type-header-regexps',
1e83e2ca
GM
348and that image type is present in `image-type-auto-detectable' with a
349non-nil value. If that value is non-nil, but not t, then the image type
350must be available."
6642c904
JB
351 (let* ((type (image-type-from-buffer))
352 (auto (and type (cdr (assq type image-type-auto-detectable)))))
d567be22 353 (and auto
7d88e015 354 (or (eq auto t) (image-type-available-p type)))))
6642c904
JB
355
356
e7968373
KS
357;;;###autoload
358(defun create-image (file-or-data &optional type data-p &rest props)
359 "Create an image.
360FILE-OR-DATA is an image file name or image data.
361Optional TYPE is a symbol describing the image type. If TYPE is omitted
362or nil, try to determine the image type from its first few bytes
363of image data. If that doesn't work, and FILE-OR-DATA is a file name,
364use its file extension as image type.
365Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
366Optional PROPS are additional image attributes to assign to the image,
367like, e.g. `:mask MASK'.
368Value is the image created, or nil if images of type TYPE are not supported.
369
2467875c
GM
370Images should not be larger than specified by `max-image-size'.
371
372Image file names that are not absolute are searched for in the
373\"images\" sub-directory of `data-directory' and
374`x-bitmap-file-path' (in that order)."
375 ;; It is x_find_image_file in image.c that sets the search path.
e7968373 376 (setq type (image-type file-or-data type data-p))
7840ced1 377 (when (image-type-available-p type)
162dec01
GM
378 (append (list 'image :type type (if data-p :data :file) file-or-data)
379 props)))
7840ced1
GM
380
381
382;;;###autoload
cedbd8d0 383(defun put-image (image pos &optional string area)
1571b5ff 384 "Put image IMAGE in front of POS in the current buffer.
7840ced1 385IMAGE must be an image created with `create-image' or `defimage'.
46655c94
GM
386IMAGE is displayed by putting an overlay into the current buffer with a
387`before-string' STRING that has a `display' property whose value is the
cedbd8d0 388image. STRING is defaulted if you omit it.
7840ced1 389POS may be an integer or marker.
7840ced1
GM
390AREA is where to display the image. AREA nil or omitted means
391display it in the text area, a value of `left-margin' means
392display it in the left marginal area, a value of `right-margin'
46655c94 393means display it in the right marginal area."
0ad550ba 394 (unless string (setq string "x"))
1571b5ff 395 (let ((buffer (current-buffer)))
cedbd8d0 396 (unless (eq (car-safe image) 'image)
1571b5ff
GM
397 (error "Not an image: %s" image))
398 (unless (or (null area) (memq area '(left-margin right-margin)))
399 (error "Invalid area %s" area))
46655c94 400 (setq string (copy-sequence string))
1571b5ff 401 (let ((overlay (make-overlay pos pos buffer))
46655c94
GM
402 (prop (if (null area) image (list (list 'margin area) image))))
403 (put-text-property 0 (length string) 'display prop string)
1571b5ff
GM
404 (overlay-put overlay 'put-image t)
405 (overlay-put overlay 'before-string string))))
7840ced1
GM
406
407
408;;;###autoload
5af275e0 409(defun insert-image (image &optional string area slice)
7840ced1 410 "Insert IMAGE into current buffer at point.
46655c94 411IMAGE is displayed by inserting STRING into the current buffer
cedbd8d0
DL
412with a `display' property whose value is the image. STRING is
413defaulted if you omit it.
7840ced1
GM
414AREA is where to display the image. AREA nil or omitted means
415display it in the text area, a value of `left-margin' means
416display it in the left marginal area, a value of `right-margin'
5af275e0
KS
417means display it in the right marginal area.
418SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
419means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
420specifying the X and Y positions and WIDTH and HEIGHT of image area
421to insert. A float value 0.0 - 1.0 means relative to the width or
422height of the image; integer values are taken as pixel values."
0ad550ba
DL
423 ;; Use a space as least likely to cause trouble when it's a hidden
424 ;; character in the buffer.
425 (unless string (setq string " "))
cedbd8d0 426 (unless (eq (car-safe image) 'image)
7840ced1
GM
427 (error "Not an image: %s" image))
428 (unless (or (null area) (memq area '(left-margin right-margin)))
429 (error "Invalid area %s" area))
0dc91c57
DL
430 (if area
431 (setq image (list (list 'margin area) image))
432 ;; Cons up a new spec equal but not eq to `image' so that
433 ;; inserting it twice in a row (adjacently) displays two copies of
434 ;; the image. Don't try to avoid this by looking at the display
435 ;; properties on either side so that we DTRT more often with
436 ;; cut-and-paste. (Yanking killed image text next to another copy
437 ;; of it loses anyway.)
438 (setq image (cons 'image (cdr image))))
46655c94
GM
439 (let ((start (point)))
440 (insert string)
441 (add-text-properties start (point)
5af275e0
KS
442 `(display ,(if slice
443 (list (cons 'slice slice) image)
444 image) rear-nonsticky (display)))))
445
446
f22c36d3 447;;;###autoload
5af275e0 448(defun insert-sliced-image (image &optional string area rows cols)
b48bc937
KS
449 "Insert IMAGE into current buffer at point.
450IMAGE is displayed by inserting STRING into the current buffer
451with a `display' property whose value is the image. STRING is
452defaulted if you omit it.
453AREA is where to display the image. AREA nil or omitted means
454display it in the text area, a value of `left-margin' means
455display it in the left marginal area, a value of `right-margin'
456means display it in the right marginal area.
68ba6c49 457The image is automatically split into ROWS x COLS slices."
5af275e0
KS
458 (unless string (setq string " "))
459 (unless (eq (car-safe image) 'image)
460 (error "Not an image: %s" image))
461 (unless (or (null area) (memq area '(left-margin right-margin)))
462 (error "Invalid area %s" area))
463 (if area
464 (setq image (list (list 'margin area) image))
465 ;; Cons up a new spec equal but not eq to `image' so that
466 ;; inserting it twice in a row (adjacently) displays two copies of
467 ;; the image. Don't try to avoid this by looking at the display
468 ;; properties on either side so that we DTRT more often with
469 ;; cut-and-paste. (Yanking killed image text next to another copy
470 ;; of it loses anyway.)
471 (setq image (cons 'image (cdr image))))
472 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
473 (y 0.0) (dy (/ 1.0001 (or rows 1))))
474 (while (< y 1.0)
475 (while (< x 1.0)
476 (let ((start (point)))
477 (insert string)
478 (add-text-properties start (point)
479 `(display ,(list (list 'slice x y dx dy) image)
480 rear-nonsticky (display)))
481 (setq x (+ x dx))))
482 (setq x 0.0
483 y (+ y dy))
82dc617b 484 (insert (propertize "\n" 'line-height t)))))
5af275e0 485
10e2102e 486
7840ced1
GM
487
488;;;###autoload
489(defun remove-images (start end &optional buffer)
490 "Remove images between START and END in BUFFER.
491Remove only images that were put in BUFFER with calls to `put-image'.
492BUFFER nil or omitted means use the current buffer."
493 (unless buffer
494 (setq buffer (current-buffer)))
495 (let ((overlays (overlays-in start end)))
496 (while overlays
497 (let ((overlay (car overlays)))
498 (when (overlay-get overlay 'put-image)
0c898dd9
DL
499 (delete-overlay overlay)))
500 (setq overlays (cdr overlays)))))
7840ced1 501
4fde92ef
KS
502(defun image-search-load-path (file &optional path)
503 (unless path
504 (setq path image-load-path))
505 (let (element found filename)
5fc5ac38 506 (while (and (not found) (consp path))
93a75651 507 (setq element (car path))
5fc5ac38 508 (cond
93a75651 509 ((stringp element)
5fc5ac38
CY
510 (setq found
511 (file-readable-p
4fde92ef 512 (setq filename (expand-file-name file element)))))
93a75651
CY
513 ((and (symbolp element) (boundp element))
514 (setq element (symbol-value element))
515 (cond
516 ((stringp element)
517 (setq found
518 (file-readable-p
4fde92ef 519 (setq filename (expand-file-name file element)))))
93a75651 520 ((consp element)
4fde92ef 521 (if (setq filename (image-search-load-path file element))
93a75651 522 (setq found t))))))
5fc5ac38 523 (setq path (cdr path)))
4fde92ef 524 (if found filename)))
7840ced1
GM
525
526;;;###autoload
349c034d
GM
527(defun find-image (specs)
528 "Find an image, choosing one of a list of image specifications.
7840ced1 529
cedbd8d0 530SPECS is a list of image specifications.
7840ced1
GM
531
532Each image specification in SPECS is a property list. The contents of
533a specification are image type dependent. All specifications must at
bc283707
WP
534least contain the properties `:type TYPE' and either `:file FILE' or
535`:data DATA', where TYPE is a symbol specifying the image type,
536e.g. `xbm', FILE is the file to load the image from, and DATA is a
cedbd8d0
DL
537string containing the actual image data. The specification whose TYPE
538is supported, and FILE exists, is used to construct the image
539specification to be returned. Return nil if no specification is
540satisfied.
541
8646a62e
CY
542The image is looked for in `image-load-path'.
543
544Image files should not be larger than specified by `max-image-size'."
7840ced1
GM
545 (let (image)
546 (while (and specs (null image))
547 (let* ((spec (car specs))
548 (type (plist-get spec :type))
162dec01 549 (data (plist-get spec :data))
7f228379
GM
550 (file (plist-get spec :file))
551 found)
162dec01
GM
552 (when (image-type-available-p type)
553 (cond ((stringp file)
4fde92ef 554 (if (setq found (image-search-load-path file))
5fc5ac38
CY
555 (setq image
556 (cons 'image (plist-put (copy-sequence spec)
557 :file found)))))
f64ae86c 558 ((not (null data))
162dec01
GM
559 (setq image (cons 'image spec)))))
560 (setq specs (cdr specs))))
349c034d
GM
561 image))
562
563
564;;;###autoload
565(defmacro defimage (symbol specs &optional doc)
566 "Define SYMBOL as an image.
567
568SPECS is a list of image specifications. DOC is an optional
569documentation string.
570
571Each image specification in SPECS is a property list. The contents of
572a specification are image type dependent. All specifications must at
573least contain the properties `:type TYPE' and either `:file FILE' or
574`:data DATA', where TYPE is a symbol specifying the image type,
575e.g. `xbm', FILE is the file to load the image from, and DATA is a
576string containing the actual image data. The first image
577specification whose TYPE is supported, and FILE exists, is used to
578define SYMBOL.
579
580Example:
581
582 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
583 (:type xbm :file \"~/test1.xbm\")))"
fd8e7a80 584 (declare (doc-string 3))
349c034d 585 `(defvar ,symbol (find-image ',specs) ,doc))
7840ced1 586
0608aa45
KS
587\f
588;;; Animated image API
7840ced1 589
0608aa45
KS
590(defcustom image-animate-max-time 30
591 "Time in seconds to animate images."
592 :type 'integer
3e5b7009 593 :version "24.1"
0608aa45
KS
594 :group 'image)
595
596(defconst image-animated-types '(gif)
597 "List of supported animated image types.")
598
599;;;###autoload
600(defun create-animated-image (file-or-data &optional type data-p &rest props)
601 "Create an animated image.
602FILE-OR-DATA is an image file name or image data.
603Optional TYPE is a symbol describing the image type. If TYPE is omitted
604or nil, try to determine the image type from its first few bytes
605of image data. If that doesn't work, and FILE-OR-DATA is a file name,
606use its file extension as image type.
607Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
608Optional PROPS are additional image attributes to assign to the image,
609like, e.g. `:mask MASK'.
610Value is the image created, or nil if images of type TYPE are not supported.
611
612Images should not be larger than specified by `max-image-size'."
613 (setq type (image-type file-or-data type data-p))
614 (when (image-type-available-p type)
615 (let* ((animate (memq type image-animated-types))
616 (image
617 (append (list 'image :type type (if data-p :data :file) file-or-data)
618 (if animate '(:index 0 :mask heuristic))
619 props)))
620 (if animate
621 (image-animate-start image))
622 image)))
623
624(defun image-animate-timer (image)
625 "Return the animation timer for image IMAGE."
626 ;; See cancel-function-timers
627 (let ((tail timer-list) timer)
628 (while tail
629 (setq timer (car tail)
630 tail (cdr tail))
631 (if (and (eq (aref timer 5) #'image-animate-timeout)
632 (consp (aref timer 6))
633 (eq (car (aref timer 6)) image))
634 (setq tail nil)
635 (setq timer nil)))
636 timer))
637
638(defun image-animate-start (image &optional max-time)
639 "Start animation of image IMAGE.
640Optional second arg MAX-TIME is number of seconds to animate image,
641or t to animate infinitely."
642 (let ((anim (image-animated-p image))
643 timer tmo)
644 (when anim
645 (if (setq timer (image-animate-timer image))
646 (setcar (nthcdr 3 (aref timer 6)) max-time)
647 (setq tmo (* (cdr anim) 0.01))
648 (setq max-time (or max-time image-animate-max-time))
649 (run-with-timer tmo nil #'image-animate-timeout
650 image 1 (car anim)
651 (if (numberp max-time)
652 (- max-time tmo)
653 max-time))))))
654
655(defun image-animate-stop (image)
656 "Stop animation of image."
657 (let ((timer (image-animate-timer image)))
658 (when timer
659 (cancel-timer timer))))
660
661(defun image-animate-timeout (image ino count time-left)
662 (if (>= ino count)
663 (setq ino 0))
664 (plist-put (cdr image) :index ino)
665 (force-window-update)
666 (let ((anim (image-animated-p image)) tmo)
667 (when anim
668 (setq tmo (* (cdr anim) 0.01))
669 (unless (and (= ino 0) (numberp time-left) (< time-left tmo))
670 (run-with-timer tmo nil #'image-animate-timeout
671 image (1+ ino) count
672 (if (numberp time-left)
673 (- time-left tmo)
674 time-left))))))
675
676(defun image-animated-p (image)
677 "Return non-nil if image is animated.
678Actually, return value is a cons (IMAGES . DELAY) where IMAGES
679is the number of sub-images in the animated image, and DELAY
680is the delay in 100ths of a second until the next sub-image
681shall be displayed."
682 (cond
683 ((eq (plist-get (cdr image) :type) 'gif)
684 (let* ((extdata (image-extension-data image))
685 (images (plist-get extdata 'count))
686 (anim (plist-get extdata #xF9)))
687 (and (integerp images) (> images 1)
688 (stringp anim) (>= (length anim) 4)
689 (cons images (+ (aref anim 1) (* (aref anim 2) 256))))))))
690
691\f
7840ced1
GM
692(provide 'image)
693
fd8e7a80 694;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
e43367a0 695;;; image.el ends here