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