* lisp/url/url-vars.el (url-automatic-caching): Remove unused variable.
[emacs.git] / lisp / image.el
blob2ca2971b4aa891224ba79efcb0dca491f54580b0
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: multimedia
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;;; Code:
30 (defgroup image ()
31 "Image support."
32 :group 'multimedia)
34 (defalias 'image-refresh 'image-flush)
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)
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.")
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)
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.")
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').
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').")
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)
116 (defun image-load-path-for-library (library image &optional path no-error)
117 "Return a suitable search path for images used by LIBRARY.
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\".
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'.
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.
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':
138 ;; Shush compiler.
139 (defvar image-load-path)
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))
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))
204 (error "Could not find image %s for library %s" image library)))
206 ;; Return an augmented `path' or `load-path'.
207 (nconc (list image-directory)
208 (delete image-directory (copy-sequence (or path load-path))))))
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))))))))
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))
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))
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))))
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))
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)
333 (defvar image-library-alist)
335 ;;;###autoload
336 (defun image-type-available-p (type)
337 "Return non-nil if image type TYPE is available.
338 Image types are symbols like `xbm' or `jpeg'."
339 (and (fboundp 'init-image-library)
340 (init-image-library type image-library-alist)))
343 ;;;###autoload
344 (defun image-type-auto-detected-p ()
345 "Return t if the current buffer contains an auto-detectable image.
346 This function is intended to be used from `magic-fallback-mode-alist'.
348 The buffer is considered to contain an auto-detectable image if
349 its beginning matches an image type in `image-type-header-regexps',
350 and that image type is present in `image-type-auto-detectable' with a
351 non-nil value. If that value is non-nil, but not t, then the image type
352 must be available."
353 (let* ((type (image-type-from-buffer))
354 (auto (and type (cdr (assq type image-type-auto-detectable)))))
355 (and auto
356 (or (eq auto t) (image-type-available-p type)))))
359 ;;;###autoload
360 (defun create-image (file-or-data &optional type data-p &rest props)
361 "Create an image.
362 FILE-OR-DATA is an image file name or image data.
363 Optional TYPE is a symbol describing the image type. If TYPE is omitted
364 or nil, try to determine the image type from its first few bytes
365 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
366 use its file extension as image type.
367 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
368 Optional PROPS are additional image attributes to assign to the image,
369 like, e.g. `:mask MASK'.
370 Value is the image created, or nil if images of type TYPE are not supported.
372 Images should not be larger than specified by `max-image-size'.
374 Image file names that are not absolute are searched for in the
375 \"images\" sub-directory of `data-directory' and
376 `x-bitmap-file-path' (in that order)."
377 ;; It is x_find_image_file in image.c that sets the search path.
378 (setq type (image-type file-or-data type data-p))
379 (when (image-type-available-p type)
380 (append (list 'image :type type (if data-p :data :file) file-or-data)
381 props)))
384 ;;;###autoload
385 (defun put-image (image pos &optional string area)
386 "Put image IMAGE in front of POS in the current buffer.
387 IMAGE must be an image created with `create-image' or `defimage'.
388 IMAGE is displayed by putting an overlay into the current buffer with a
389 `before-string' STRING that has a `display' property whose value is the
390 image. STRING is defaulted if you omit it.
391 POS may be an integer or marker.
392 AREA is where to display the image. AREA nil or omitted means
393 display it in the text area, a value of `left-margin' means
394 display it in the left marginal area, a value of `right-margin'
395 means display it in the right marginal area."
396 (unless string (setq string "x"))
397 (let ((buffer (current-buffer)))
398 (unless (eq (car-safe image) 'image)
399 (error "Not an image: %s" image))
400 (unless (or (null area) (memq area '(left-margin right-margin)))
401 (error "Invalid area %s" area))
402 (setq string (copy-sequence string))
403 (let ((overlay (make-overlay pos pos buffer))
404 (prop (if (null area) image (list (list 'margin area) image))))
405 (put-text-property 0 (length string) 'display prop string)
406 (overlay-put overlay 'put-image t)
407 (overlay-put overlay 'before-string string))))
410 ;;;###autoload
411 (defun insert-image (image &optional string area slice)
412 "Insert IMAGE into current buffer at point.
413 IMAGE is displayed by inserting STRING into the current buffer
414 with a `display' property whose value is the image. STRING is
415 defaulted if you omit it.
416 AREA is where to display the image. AREA nil or omitted means
417 display it in the text area, a value of `left-margin' means
418 display it in the left marginal area, a value of `right-margin'
419 means display it in the right marginal area.
420 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
421 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
422 specifying the X and Y positions and WIDTH and HEIGHT of image area
423 to insert. A float value 0.0 - 1.0 means relative to the width or
424 height of the image; integer values are taken as pixel values."
425 ;; Use a space as least likely to cause trouble when it's a hidden
426 ;; character in the buffer.
427 (unless string (setq string " "))
428 (unless (eq (car-safe image) 'image)
429 (error "Not an image: %s" image))
430 (unless (or (null area) (memq area '(left-margin right-margin)))
431 (error "Invalid area %s" area))
432 (if area
433 (setq image (list (list 'margin area) image))
434 ;; Cons up a new spec equal but not eq to `image' so that
435 ;; inserting it twice in a row (adjacently) displays two copies of
436 ;; the image. Don't try to avoid this by looking at the display
437 ;; properties on either side so that we DTRT more often with
438 ;; cut-and-paste. (Yanking killed image text next to another copy
439 ;; of it loses anyway.)
440 (setq image (cons 'image (cdr image))))
441 (let ((start (point)))
442 (insert string)
443 (add-text-properties start (point)
444 `(display ,(if slice
445 (list (cons 'slice slice) image)
446 image) rear-nonsticky (display)))))
449 ;;;###autoload
450 (defun insert-sliced-image (image &optional string area rows cols)
451 "Insert IMAGE into current buffer at point.
452 IMAGE is displayed by inserting STRING into the current buffer
453 with a `display' property whose value is the image. STRING is
454 defaulted if you omit it.
455 AREA is where to display the image. AREA nil or omitted means
456 display it in the text area, a value of `left-margin' means
457 display it in the left marginal area, a value of `right-margin'
458 means display it in the right marginal area.
459 The image is automatically split into ROWS x COLS slices."
460 (unless string (setq string " "))
461 (unless (eq (car-safe image) 'image)
462 (error "Not an image: %s" image))
463 (unless (or (null area) (memq area '(left-margin right-margin)))
464 (error "Invalid area %s" area))
465 (if area
466 (setq image (list (list 'margin area) image))
467 ;; Cons up a new spec equal but not eq to `image' so that
468 ;; inserting it twice in a row (adjacently) displays two copies of
469 ;; the image. Don't try to avoid this by looking at the display
470 ;; properties on either side so that we DTRT more often with
471 ;; cut-and-paste. (Yanking killed image text next to another copy
472 ;; of it loses anyway.)
473 (setq image (cons 'image (cdr image))))
474 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
475 (y 0.0) (dy (/ 1.0001 (or rows 1))))
476 (while (< y 1.0)
477 (while (< x 1.0)
478 (let ((start (point)))
479 (insert string)
480 (add-text-properties start (point)
481 `(display ,(list (list 'slice x y dx dy) image)
482 rear-nonsticky (display)))
483 (setq x (+ x dx))))
484 (setq x 0.0
485 y (+ y dy))
486 (insert (propertize "\n" 'line-height t)))))
490 ;;;###autoload
491 (defun remove-images (start end &optional buffer)
492 "Remove images between START and END in BUFFER.
493 Remove only images that were put in BUFFER with calls to `put-image'.
494 BUFFER nil or omitted means use the current buffer."
495 (unless buffer
496 (setq buffer (current-buffer)))
497 (let ((overlays (overlays-in start end)))
498 (while overlays
499 (let ((overlay (car overlays)))
500 (when (overlay-get overlay 'put-image)
501 (delete-overlay overlay)))
502 (setq overlays (cdr overlays)))))
504 (defun image-search-load-path (file &optional path)
505 (unless path
506 (setq path image-load-path))
507 (let (element found filename)
508 (while (and (not found) (consp path))
509 (setq element (car path))
510 (cond
511 ((stringp element)
512 (setq found
513 (file-readable-p
514 (setq filename (expand-file-name file element)))))
515 ((and (symbolp element) (boundp element))
516 (setq element (symbol-value element))
517 (cond
518 ((stringp element)
519 (setq found
520 (file-readable-p
521 (setq filename (expand-file-name file element)))))
522 ((consp element)
523 (if (setq filename (image-search-load-path file element))
524 (setq found t))))))
525 (setq path (cdr path)))
526 (if found filename)))
528 ;;;###autoload
529 (defun find-image (specs)
530 "Find an image, choosing one of a list of image specifications.
532 SPECS is a list of image specifications.
534 Each image specification in SPECS is a property list. The contents of
535 a specification are image type dependent. All specifications must at
536 least contain the properties `:type TYPE' and either `:file FILE' or
537 `:data DATA', where TYPE is a symbol specifying the image type,
538 e.g. `xbm', FILE is the file to load the image from, and DATA is a
539 string containing the actual image data. The specification whose TYPE
540 is supported, and FILE exists, is used to construct the image
541 specification to be returned. Return nil if no specification is
542 satisfied.
544 The image is looked for in `image-load-path'.
546 Image files should not be larger than specified by `max-image-size'."
547 (let (image)
548 (while (and specs (null image))
549 (let* ((spec (car specs))
550 (type (plist-get spec :type))
551 (data (plist-get spec :data))
552 (file (plist-get spec :file))
553 found)
554 (when (image-type-available-p type)
555 (cond ((stringp file)
556 (if (setq found (image-search-load-path file))
557 (setq image
558 (cons 'image (plist-put (copy-sequence spec)
559 :file found)))))
560 ((not (null data))
561 (setq image (cons 'image spec)))))
562 (setq specs (cdr specs))))
563 image))
566 ;;;###autoload
567 (defmacro defimage (symbol specs &optional doc)
568 "Define SYMBOL as an image.
570 SPECS is a list of image specifications. DOC is an optional
571 documentation string.
573 Each image specification in SPECS is a property list. The contents of
574 a specification are image type dependent. All specifications must at
575 least contain the properties `:type TYPE' and either `:file FILE' or
576 `:data DATA', where TYPE is a symbol specifying the image type,
577 e.g. `xbm', FILE is the file to load the image from, and DATA is a
578 string containing the actual image data. The first image
579 specification whose TYPE is supported, and FILE exists, is used to
580 define SYMBOL.
582 Example:
584 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
585 (:type xbm :file \"~/test1.xbm\")))"
586 (declare (doc-string 3))
587 `(defvar ,symbol (find-image ',specs) ,doc))
590 ;;; Animated image API
592 (defcustom image-animate-max-time 30
593 "Time in seconds to animate images."
594 :type 'integer
595 :version "24.1"
596 :group 'image)
598 (defconst image-animated-types '(gif)
599 "List of supported animated image types.")
601 ;;;###autoload
602 (defun create-animated-image (file-or-data &optional type data-p &rest props)
603 "Create an animated image.
604 FILE-OR-DATA is an image file name or image data.
605 Optional TYPE is a symbol describing the image type. If TYPE is omitted
606 or nil, try to determine the image type from its first few bytes
607 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
608 use its file extension as image type.
609 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
610 Optional PROPS are additional image attributes to assign to the image,
611 like, e.g. `:mask MASK'.
612 Value is the image created, or nil if images of type TYPE are not supported.
614 Images should not be larger than specified by `max-image-size'."
615 (setq type (image-type file-or-data type data-p))
616 (when (image-type-available-p type)
617 (let* ((animate (memq type image-animated-types))
618 (image
619 (append (list 'image :type type (if data-p :data :file) file-or-data)
620 (if animate '(:index 0))
621 props)))
622 (if animate
623 (image-animate-start image))
624 image)))
626 (defun image-animate-timer (image)
627 "Return the animation timer for image IMAGE."
628 ;; See cancel-function-timers
629 (let ((tail timer-list) timer)
630 (while tail
631 (setq timer (car tail)
632 tail (cdr tail))
633 (if (and (eq (aref timer 5) #'image-animate-timeout)
634 (consp (aref timer 6))
635 (eq (car (aref timer 6)) image))
636 (setq tail nil)
637 (setq timer nil)))
638 timer))
640 (defun image-animate-start (image &optional max-time)
641 "Start animation of image IMAGE.
642 Optional second arg MAX-TIME is number of seconds to animate image,
643 or t to animate infinitely."
644 (let ((anim (image-animated-p image))
645 timer tmo)
646 (when anim
647 (if (setq timer (image-animate-timer image))
648 (setcar (nthcdr 3 (aref timer 6)) max-time)
649 (setq tmo (* (cdr anim) 0.01))
650 (setq max-time (or max-time image-animate-max-time))
651 (run-with-timer tmo nil #'image-animate-timeout
652 image 1 (car anim)
653 (if (numberp max-time)
654 (- max-time tmo)
655 max-time))))))
657 (defun image-animate-stop (image)
658 "Stop animation of image."
659 (let ((timer (image-animate-timer image)))
660 (when timer
661 (cancel-timer timer))))
663 (defun image-animate-timeout (image ino count time-left)
664 (if (>= ino count)
665 (setq ino 0))
666 (plist-put (cdr image) :index ino)
667 (force-window-update)
668 (let ((anim (image-animated-p image)) tmo)
669 (when anim
670 (setq tmo (* (cdr anim) 0.01))
671 (unless (and (= ino 0) (numberp time-left) (< time-left tmo))
672 (run-with-timer tmo nil #'image-animate-timeout
673 image (1+ ino) count
674 (if (numberp time-left)
675 (- time-left tmo)
676 time-left))))))
678 (defun image-animated-p (image)
679 "Return non-nil if image is animated.
680 Actually, return value is a cons (IMAGES . DELAY) where IMAGES
681 is the number of sub-images in the animated image, and DELAY
682 is the delay in 100ths of a second until the next sub-image
683 shall be displayed."
684 (cond
685 ((eq (plist-get (cdr image) :type) 'gif)
686 (let* ((metadata (image-metadata image))
687 (images (plist-get metadata 'count))
688 (extdata (plist-get metadata 'extension-data))
689 (anim (plist-get extdata #xF9))
690 (tmo (and (integerp images) (> images 1)
691 (stringp anim) (>= (length anim) 4)
692 (+ (aref anim 1) (* (aref anim 2) 256)))))
693 (when tmo
694 (if (eq tmo 0) (setq tmo 10))
695 (cons images tmo))))))
698 (defcustom imagemagick-types-inhibit
699 '(C HTML HTM TXT PDF)
700 ;; FIXME what are the possible options?
701 ;; Are these actually file-name extensions?
702 ;; Why are these upper-case when eg image-types is lower-case?
703 "Types the ImageMagick loader should not try to handle."
704 :type '(choice (const :tag "Let ImageMagick handle all the types it can" nil)
705 (repeat symbol))
706 :version "24.1"
707 :group 'image)
709 ;;;###autoload
710 (defun imagemagick-register-types ()
711 "Register the file types that ImageMagick is able to handle."
712 (let ((im-types (imagemagick-types)))
713 (dolist (im-inhibit imagemagick-types-inhibit)
714 (setq im-types (remove im-inhibit im-types)))
715 (dolist (im-type im-types)
716 (let ((extension (downcase (symbol-name im-type))))
717 (push
718 (cons (concat "\\." extension "\\'") 'image-mode)
719 auto-mode-alist)
720 (push
721 (cons (concat "\\." extension "\\'") 'imagemagick)
722 image-type-file-name-regexps)))))
726 (provide 'image)
728 ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
729 ;;; image.el ends here