Merge from emacs-24; up to 2012-12-10T20:27:33Z!eggert@cs.ucla.edu
[emacs.git] / lisp / image.el
blob73b25f6da67564413b20a9e1b8dd275b6682aeaa
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998-2013 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's 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's 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 ;; Used to be in image-type-header-regexps, but now not used anywhere
211 ;; (since 2009-08-28).
212 (defun image-jpeg-p (data)
213 "Value is non-nil if DATA, a string, consists of JFIF image data.
214 We accept the tag Exif because that is the same format."
215 (setq data (ignore-errors (string-to-unibyte data)))
216 (when (and data (string-match-p "\\`\xff\xd8" data))
217 (catch 'jfif
218 (let ((len (length data)) (i 2))
219 (while (< i len)
220 (when (/= (aref data i) #xff)
221 (throw 'jfif nil))
222 (setq i (1+ i))
223 (when (>= (+ i 2) len)
224 (throw 'jfif nil))
225 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
226 (aref data (+ i 2))))
227 (code (aref data i)))
228 (when (and (>= code #xe0) (<= code #xef))
229 ;; APP0 LEN1 LEN2 "JFIF\0"
230 (throw 'jfif
231 (string-match-p "JFIF\\|Exif"
232 (substring data i (min (+ i nbytes) len)))))
233 (setq i (+ i 1 nbytes))))))))
236 ;;;###autoload
237 (defun image-type-from-data (data)
238 "Determine the image type from image data DATA.
239 Value is a symbol specifying the image type or nil if type cannot
240 be determined."
241 (let ((types image-type-header-regexps)
242 type)
243 (while types
244 (let ((regexp (car (car types)))
245 (image-type (cdr (car types))))
246 (if (or (and (symbolp image-type)
247 (string-match-p regexp data))
248 (and (consp image-type)
249 (funcall (car image-type) data)
250 (setq image-type (cdr image-type))))
251 (setq type image-type
252 types nil)
253 (setq types (cdr types)))))
254 type))
257 ;;;###autoload
258 (defun image-type-from-buffer ()
259 "Determine the image type from data in the current buffer.
260 Value is a symbol specifying the image type or nil if type cannot
261 be determined."
262 (let ((types image-type-header-regexps)
263 type
264 (opoint (point)))
265 (goto-char (point-min))
266 (while types
267 (let ((regexp (car (car types)))
268 (image-type (cdr (car types)))
269 data)
270 (if (or (and (symbolp image-type)
271 (looking-at-p regexp))
272 (and (consp image-type)
273 (funcall (car image-type)
274 (or data
275 (setq data
276 (buffer-substring
277 (point-min)
278 (min (point-max)
279 (+ (point-min) 256))))))
280 (setq image-type (cdr image-type))))
281 (setq type image-type
282 types nil)
283 (setq types (cdr types)))))
284 (goto-char opoint)
285 (and type
286 (memq type image-types)
287 type)))
290 ;;;###autoload
291 (defun image-type-from-file-header (file)
292 "Determine the type of image file FILE from its first few bytes.
293 Value is a symbol specifying the image type, or nil if type cannot
294 be determined."
295 (unless (or (file-readable-p file)
296 (file-name-absolute-p file))
297 (setq file (image-search-load-path file)))
298 (and file
299 (file-readable-p file)
300 (with-temp-buffer
301 (set-buffer-multibyte nil)
302 (insert-file-contents-literally file nil 0 256)
303 (image-type-from-buffer))))
306 ;;;###autoload
307 (defun image-type-from-file-name (file)
308 "Determine the type of image file FILE from its name.
309 Value is a symbol specifying the image type, or nil if type cannot
310 be determined."
311 (let (type first)
312 (catch 'found
313 (dolist (elem image-type-file-name-regexps first)
314 (when (string-match-p (car elem) file)
315 (if (image-type-available-p (setq type (cdr elem)))
316 (throw 'found type)
317 ;; If nothing seems to be supported, return first type that matched.
318 (or first (setq first type))))))))
320 ;;;###autoload
321 (defun image-type (source &optional type data-p)
322 "Determine and return image type.
323 SOURCE is an image file name or image data.
324 Optional TYPE is a symbol describing the image type. If TYPE is omitted
325 or nil, try to determine the image type from its first few bytes
326 of image data. If that doesn't work, and SOURCE is a file name,
327 use its file extension as image type.
328 Optional DATA-P non-nil means SOURCE is a string containing image data."
329 (when (and (not data-p) (not (stringp source)))
330 (error "Invalid image file name `%s'" source))
331 (unless type
332 (setq type (if data-p
333 (image-type-from-data source)
334 (or (image-type-from-file-header source)
335 (image-type-from-file-name source))))
336 (or type (error "Cannot determine image type")))
337 (or (memq type (and (boundp 'image-types) image-types))
338 (error "Invalid image type `%s'" type))
339 type)
342 (if (fboundp 'image-metadata) ; eg not --without-x
343 (define-obsolete-function-alias 'image-extension-data
344 'image-metadata' "24.1"))
346 (define-obsolete-variable-alias
347 'image-library-alist
348 'dynamic-library-alist "24.1")
350 ;;;###autoload
351 (defun image-type-available-p (type)
352 "Return non-nil if image type TYPE is available.
353 Image types are symbols like `xbm' or `jpeg'."
354 (and (fboundp 'init-image-library)
355 (init-image-library type)))
358 ;;;###autoload
359 (defun image-type-auto-detected-p ()
360 "Return t if the current buffer contains an auto-detectable image.
361 This function is intended to be used from `magic-fallback-mode-alist'.
363 The buffer is considered to contain an auto-detectable image if
364 its beginning matches an image type in `image-type-header-regexps',
365 and that image type is present in `image-type-auto-detectable' with a
366 non-nil value. If that value is non-nil, but not t, then the image type
367 must be available."
368 (let* ((type (image-type-from-buffer))
369 (auto (and type (cdr (assq type image-type-auto-detectable)))))
370 (and auto
371 (or (eq auto t) (image-type-available-p type)))))
374 ;;;###autoload
375 (defun create-image (file-or-data &optional type data-p &rest props)
376 "Create an image.
377 FILE-OR-DATA is an image file name or image data.
378 Optional TYPE is a symbol describing the image type. If TYPE is omitted
379 or nil, try to determine the image type from its first few bytes
380 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
381 use its file extension as image type.
382 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
383 Optional PROPS are additional image attributes to assign to the image,
384 like, e.g. `:mask MASK'.
385 Value is the image created, or nil if images of type TYPE are not supported.
387 Images should not be larger than specified by `max-image-size'.
389 Image file names that are not absolute are searched for in the
390 \"images\" sub-directory of `data-directory' and
391 `x-bitmap-file-path' (in that order)."
392 ;; It is x_find_image_file in image.c that sets the search path.
393 (setq type (image-type file-or-data type data-p))
394 (when (image-type-available-p type)
395 (append (list 'image :type type (if data-p :data :file) file-or-data)
396 props)))
399 ;;;###autoload
400 (defun put-image (image pos &optional string area)
401 "Put image IMAGE in front of POS in the current buffer.
402 IMAGE must be an image created with `create-image' or `defimage'.
403 IMAGE is displayed by putting an overlay into the current buffer with a
404 `before-string' STRING that has a `display' property whose value is the
405 image. STRING is defaulted if you omit it.
406 The overlay created will have the `put-image' property set to t.
407 POS may be an integer or marker.
408 AREA is where to display the image. AREA nil or omitted means
409 display it in the text area, a value of `left-margin' means
410 display it in the left marginal area, a value of `right-margin'
411 means display it in the right marginal area."
412 (unless string (setq string "x"))
413 (let ((buffer (current-buffer)))
414 (unless (eq (car-safe image) 'image)
415 (error "Not an image: %s" image))
416 (unless (or (null area) (memq area '(left-margin right-margin)))
417 (error "Invalid area %s" area))
418 (setq string (copy-sequence string))
419 (let ((overlay (make-overlay pos pos buffer))
420 (prop (if (null area) image (list (list 'margin area) image))))
421 (put-text-property 0 (length string) 'display prop string)
422 (overlay-put overlay 'put-image t)
423 (overlay-put overlay 'before-string string)
424 overlay)))
427 ;;;###autoload
428 (defun insert-image (image &optional string area slice)
429 "Insert IMAGE into current buffer at point.
430 IMAGE is displayed by inserting STRING into the current buffer
431 with a `display' property whose value is the image. STRING
432 defaults to a single space if you omit it.
433 AREA is where to display the image. AREA nil or omitted means
434 display it in the text area, a value of `left-margin' means
435 display it in the left marginal area, a value of `right-margin'
436 means display it in the right marginal area.
437 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
438 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
439 specifying the X and Y positions and WIDTH and HEIGHT of image area
440 to insert. A float value 0.0 - 1.0 means relative to the width or
441 height of the image; integer values are taken as pixel values."
442 ;; Use a space as least likely to cause trouble when it's a hidden
443 ;; character in the buffer.
444 (unless string (setq string " "))
445 (unless (eq (car-safe image) 'image)
446 (error "Not an image: %s" image))
447 (unless (or (null area) (memq area '(left-margin right-margin)))
448 (error "Invalid area %s" area))
449 (if area
450 (setq image (list (list 'margin area) image))
451 ;; Cons up a new spec equal but not eq to `image' so that
452 ;; inserting it twice in a row (adjacently) displays two copies of
453 ;; the image. Don't try to avoid this by looking at the display
454 ;; properties on either side so that we DTRT more often with
455 ;; cut-and-paste. (Yanking killed image text next to another copy
456 ;; of it loses anyway.)
457 (setq image (cons 'image (cdr image))))
458 (let ((start (point)))
459 (insert string)
460 (add-text-properties start (point)
461 `(display ,(if slice
462 (list (cons 'slice slice) image)
463 image) rear-nonsticky (display)))))
466 ;;;###autoload
467 (defun insert-sliced-image (image &optional string area rows cols)
468 "Insert IMAGE into current buffer at point.
469 IMAGE is displayed by inserting STRING into the current buffer
470 with a `display' property whose value is the image. The default
471 STRING is a single space.
472 AREA is where to display the image. AREA nil or omitted means
473 display it in the text area, a value of `left-margin' means
474 display it in the left marginal area, a value of `right-margin'
475 means display it in the right marginal area.
476 The image is automatically split into ROWS x COLS slices."
477 (unless string (setq string " "))
478 (unless (eq (car-safe image) 'image)
479 (error "Not an image: %s" image))
480 (unless (or (null area) (memq area '(left-margin right-margin)))
481 (error "Invalid area %s" area))
482 (if area
483 (setq image (list (list 'margin area) image))
484 ;; Cons up a new spec equal but not eq to `image' so that
485 ;; inserting it twice in a row (adjacently) displays two copies of
486 ;; the image. Don't try to avoid this by looking at the display
487 ;; properties on either side so that we DTRT more often with
488 ;; cut-and-paste. (Yanking killed image text next to another copy
489 ;; of it loses anyway.)
490 (setq image (cons 'image (cdr image))))
491 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
492 (y 0.0) (dy (/ 1.0001 (or rows 1))))
493 (while (< y 1.0)
494 (while (< x 1.0)
495 (let ((start (point)))
496 (insert string)
497 (add-text-properties start (point)
498 `(display ,(list (list 'slice x y dx dy) image)
499 rear-nonsticky (display)))
500 (setq x (+ x dx))))
501 (setq x 0.0
502 y (+ y dy))
503 (insert (propertize "\n" 'line-height t)))))
507 ;;;###autoload
508 (defun remove-images (start end &optional buffer)
509 "Remove images between START and END in BUFFER.
510 Remove only images that were put in BUFFER with calls to `put-image'.
511 BUFFER nil or omitted means use the current buffer."
512 (unless buffer
513 (setq buffer (current-buffer)))
514 (let ((overlays (overlays-in start end)))
515 (while overlays
516 (let ((overlay (car overlays)))
517 (when (overlay-get overlay 'put-image)
518 (delete-overlay overlay)))
519 (setq overlays (cdr overlays)))))
521 (defun image-search-load-path (file &optional path)
522 (unless path
523 (setq path image-load-path))
524 (let (element found filename)
525 (while (and (not found) (consp path))
526 (setq element (car path))
527 (cond
528 ((stringp element)
529 (setq found
530 (file-readable-p
531 (setq filename (expand-file-name file element)))))
532 ((and (symbolp element) (boundp element))
533 (setq element (symbol-value element))
534 (cond
535 ((stringp element)
536 (setq found
537 (file-readable-p
538 (setq filename (expand-file-name file element)))))
539 ((consp element)
540 (if (setq filename (image-search-load-path file element))
541 (setq found t))))))
542 (setq path (cdr path)))
543 (if found filename)))
545 ;;;###autoload
546 (defun find-image (specs)
547 "Find an image, choosing one of a list of image specifications.
549 SPECS is a list of image specifications.
551 Each image specification in SPECS is a property list. The contents of
552 a specification are image type dependent. All specifications must at
553 least contain the properties `:type TYPE' and either `:file FILE' or
554 `:data DATA', where TYPE is a symbol specifying the image type,
555 e.g. `xbm', FILE is the file to load the image from, and DATA is a
556 string containing the actual image data. The specification whose TYPE
557 is supported, and FILE exists, is used to construct the image
558 specification to be returned. Return nil if no specification is
559 satisfied.
561 The image is looked for in `image-load-path'.
563 Image files should not be larger than specified by `max-image-size'."
564 (let (image)
565 (while (and specs (null image))
566 (let* ((spec (car specs))
567 (type (plist-get spec :type))
568 (data (plist-get spec :data))
569 (file (plist-get spec :file))
570 found)
571 (when (image-type-available-p type)
572 (cond ((stringp file)
573 (if (setq found (image-search-load-path file))
574 (setq image
575 (cons 'image (plist-put (copy-sequence spec)
576 :file found)))))
577 ((not (null data))
578 (setq image (cons 'image spec)))))
579 (setq specs (cdr specs))))
580 image))
583 ;;;###autoload
584 (defmacro defimage (symbol specs &optional doc)
585 "Define SYMBOL as an image.
587 SPECS is a list of image specifications. DOC is an optional
588 documentation string.
590 Each image specification in SPECS is a property list. The contents of
591 a specification are image type dependent. All specifications must at
592 least contain the properties `:type TYPE' and either `:file FILE' or
593 `:data DATA', where TYPE is a symbol specifying the image type,
594 e.g. `xbm', FILE is the file to load the image from, and DATA is a
595 string containing the actual image data. The first image
596 specification whose TYPE is supported, and FILE exists, is used to
597 define SYMBOL.
599 Example:
601 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
602 (:type xbm :file \"~/test1.xbm\")))"
603 (declare (doc-string 3))
604 `(defvar ,symbol (find-image ',specs) ,doc))
607 ;;; Animated image API
609 (defconst image-animated-types '(gif)
610 "List of supported animated image types.")
612 (defun image-animated-p (image)
613 "Return non-nil if IMAGE can be animated.
614 To be capable of being animated, an image must be of a type
615 listed in `image-animated-types', and contain more than one
616 sub-image, with a specified animation delay. The actual return
617 value is a cons (NIMAGES . DELAY), where NIMAGES is the number
618 of sub-images in the animated image and DELAY is the delay in
619 seconds until the next sub-image should be displayed."
620 (cond
621 ((memq (plist-get (cdr image) :type) image-animated-types)
622 (let* ((metadata (image-metadata image))
623 (images (plist-get metadata 'count))
624 (delay (plist-get metadata 'delay)))
625 (when (and images (> images 1) (numberp delay))
626 (if (< delay 0) (setq delay 0.1))
627 (cons images delay))))))
629 ;; "Destructively"?
630 (defun image-animate (image &optional index limit)
631 "Start animating IMAGE.
632 Animation occurs by destructively altering the IMAGE spec list.
634 With optional INDEX, begin animating from that animation frame.
635 LIMIT specifies how long to animate the image. If omitted or
636 nil, play the animation until the end. If t, loop forever. If a
637 number, play until that number of seconds has elapsed."
638 (let ((animation (image-animated-p image))
639 timer)
640 (when animation
641 (if (setq timer (image-animate-timer image))
642 (cancel-timer timer))
643 (run-with-timer 0.2 nil 'image-animate-timeout
644 image (or index 0) (car animation)
645 0 limit))))
647 (defun image-animate-timer (image)
648 "Return the animation timer for image IMAGE."
649 ;; See cancel-function-timers
650 (let ((tail timer-list) timer)
651 (while tail
652 (setq timer (car tail)
653 tail (cdr tail))
654 (if (and (eq (timer--function timer) 'image-animate-timeout)
655 (eq (car-safe (timer--args timer)) image))
656 (setq tail nil)
657 (setq timer nil)))
658 timer))
660 ;; FIXME? The delay may not be the same for different sub-images,
661 ;; hence we need to call image-animated-p to return it.
662 ;; But it also returns count, so why do we bother passing that as an
663 ;; argument?
664 (defun image-animate-timeout (image n count time-elapsed limit)
665 "Display animation frame N of IMAGE.
666 N=0 refers to the initial animation frame.
667 COUNT is the total number of frames in the animation.
668 TIME-ELAPSED is the total time that has elapsed since
669 `image-animate-start' was called.
670 LIMIT determines when to stop. If t, loop forever. If nil, stop
671 after displaying the last animation frame. Otherwise, stop
672 after LIMIT seconds have elapsed.
673 The minimum delay between successive frames is 0.01s."
674 (plist-put (cdr image) :index n)
675 (force-window-update)
676 (setq n (1+ n))
677 (let* ((time (float-time))
678 (animation (image-animated-p image))
679 ;; Subtract off the time we took to load the image from the
680 ;; stated delay time.
681 (delay (max (+ (cdr animation) time (- (float-time)))
682 0.01))
683 done)
684 (if (>= n count)
685 (if limit
686 (setq n 0)
687 (setq done t)))
688 (setq time-elapsed (+ delay time-elapsed))
689 (if (numberp limit)
690 (setq done (>= time-elapsed limit)))
691 (unless done
692 (run-with-timer delay nil 'image-animate-timeout
693 image n count time-elapsed limit))))
696 (defvar imagemagick-types-inhibit)
697 (defvar imagemagick-enabled-types)
699 (defun imagemagick-filter-types ()
700 "Return a list of the ImageMagick types to be treated as images, or nil.
701 This is the result of `imagemagick-types', including only elements
702 that match `imagemagick-enabled-types' and do not match
703 `imagemagick-types-inhibit'."
704 (when (fboundp 'imagemagick-types)
705 (cond ((null imagemagick-enabled-types) nil)
706 ((eq imagemagick-types-inhibit t) nil)
708 (delq nil
709 (mapcar
710 (lambda (type)
711 (unless (memq type imagemagick-types-inhibit)
712 (if (eq imagemagick-enabled-types t) type
713 (catch 'found
714 (dolist (enable imagemagick-enabled-types nil)
715 (if (cond ((symbolp enable) (eq enable type))
716 ((stringp enable)
717 (string-match enable
718 (symbol-name type))))
719 (throw 'found type)))))))
720 (imagemagick-types)))))))
722 (defvar imagemagick--file-regexp nil
723 "File extension regexp for ImageMagick files, if any.
724 This is the extension installed into `auto-mode-alist' and
725 `image-type-file-name-regexps' by `imagemagick-register-types'.")
727 ;;;###autoload
728 (defun imagemagick-register-types ()
729 "Register file types that can be handled by ImageMagick.
730 This function is called at startup, after loading the init file.
731 It registers the ImageMagick types returned by `imagemagick-filter-types'.
733 Registered image types are added to `auto-mode-alist', so that
734 Emacs visits them in Image mode. They are also added to
735 `image-type-file-name-regexps', so that the `image-type' function
736 recognizes these files as having image type `imagemagick'.
738 If Emacs is compiled without ImageMagick support, this does nothing."
739 (when (fboundp 'imagemagick-types)
740 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type)))
741 (imagemagick-filter-types)))
742 (re (if types (concat "\\." (regexp-opt types) "\\'")))
743 (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
744 auto-mode-alist)))
745 (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
746 image-type-file-name-regexps))))
747 (if (not re)
748 (setq auto-mode-alist (delete ama-elt auto-mode-alist)
749 image-type-file-name-regexps
750 (delete itfnr-elt image-type-file-name-regexps))
751 (if ama-elt
752 (setcar ama-elt re)
753 (push (cons re 'image-mode) auto-mode-alist))
754 (if itfnr-elt
755 (setcar itfnr-elt re)
756 ;; Append to `image-type-file-name-regexps', so that we
757 ;; preferentially use specialized image libraries.
758 (add-to-list 'image-type-file-name-regexps
759 (cons re 'imagemagick) t)))
760 (setq imagemagick--file-regexp re))))
762 (defcustom imagemagick-types-inhibit
763 '(C HTML HTM INFO M TXT PDF)
764 "List of ImageMagick types that should never be treated as images.
765 This should be a list of symbols, each of which should be one of
766 the ImageMagick types listed by `imagemagick-types'. The listed
767 image types are not registered by `imagemagick-register-types'.
769 If the value is t, inhibit the use of ImageMagick for images.
771 If you change this without using customize, you must call
772 `imagemagick-register-types' afterwards.
774 If Emacs is compiled without ImageMagick support, this variable
775 has no effect."
776 :type '(choice (const :tag "Support all ImageMagick types" nil)
777 (const :tag "Disable all ImageMagick types" t)
778 (repeat symbol))
779 :initialize 'custom-initialize-default
780 :set (lambda (symbol value)
781 (set-default symbol value)
782 (imagemagick-register-types))
783 :version "24.3"
784 :group 'image)
786 (defcustom imagemagick-enabled-types
787 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
788 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
789 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
790 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
791 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
792 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
793 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
794 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
795 XWD YCbCr YCbCrA YUV)
796 "List of ImageMagick types to treat as images.
797 Each list element should be a string or symbol, representing one
798 of the image types returned by `imagemagick-types'. If the
799 element is a string, it is handled as a regexp that enables all
800 matching types.
802 The value of `imagemagick-enabled-types' may also be t, meaning
803 to enable all types that ImageMagick supports.
805 The variable `imagemagick-types-inhibit' overrides this variable.
807 If you change this without using customize, you must call
808 `imagemagick-register-types' afterwards.
810 If Emacs is compiled without ImageMagick support, this variable
811 has no effect."
812 :type '(choice (const :tag "Support all ImageMagick types" t)
813 (const :tag "Disable all ImageMagick types" nil)
814 (repeat :tag "List of types"
815 (choice (symbol :tag "type")
816 (regexp :tag "regexp"))))
817 :initialize 'custom-initialize-default
818 :set (lambda (symbol value)
819 (set-default symbol value)
820 (imagemagick-register-types))
821 :version "24.3"
822 :group 'image)
824 (imagemagick-register-types)
826 (provide 'image)
828 ;;; image.el ends here