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