* lisp/progmodes/subword.el (subword-capitalize): Fix Stefan's mess.
[emacs.git] / lisp / image.el
blob91cc3addb067e9601b92dee29c41269c6e99ff1d
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 (defvar image-format-suffixes
103 '((image/x-icon "ico"))
104 "Alist of MIME Content-Type headers to file name suffixes.
105 This is used as a hint by the ImageMagick library when detecting
106 image types. If `create-image' is called with a :format
107 matching found in this alist, the ImageMagick library will be
108 told that the data would have this suffix if saved to a file.")
110 (defcustom image-load-path
111 (list (file-name-as-directory (expand-file-name "images" data-directory))
112 'data-directory 'load-path)
113 "List of locations in which to search for image files.
114 If an element is a string, it defines a directory to search.
115 If an element is a variable symbol whose value is a string, that
116 value defines a directory to search.
117 If an element is a variable symbol whose value is a list, the
118 value is used as a list of directories to search."
119 :type '(repeat (choice directory variable))
120 :initialize 'custom-initialize-delay)
123 (defun image-load-path-for-library (library image &optional path no-error)
124 "Return a suitable search path for images used by LIBRARY.
126 It searches for IMAGE in `image-load-path' (excluding
127 \"`data-directory'/images\") and `load-path', followed by a path
128 suitable for LIBRARY, which includes \"../../etc/images\" and
129 \"../etc/images\" relative to the library file itself, and then
130 in \"`data-directory'/images\".
132 Then this function returns a list of directories which contains
133 first the directory in which IMAGE was found, followed by the
134 value of `load-path'. If PATH is given, it is used instead of
135 `load-path'.
137 If NO-ERROR is non-nil and a suitable path can't be found, don't
138 signal an error. Instead, return a list of directories as before,
139 except that nil appears in place of the image directory.
141 Here is an example that uses a common idiom to provide
142 compatibility with versions of Emacs that lack the variable
143 `image-load-path':
145 ;; Shush compiler.
146 (defvar image-load-path)
148 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
149 (image-load-path (cons (car load-path)
150 (when (boundp 'image-load-path)
151 image-load-path))))
152 (mh-tool-bar-folder-buttons-init))"
153 (unless library (error "No library specified"))
154 (unless image (error "No image specified"))
155 (let (image-directory image-directory-load-path)
156 ;; Check for images in image-load-path or load-path.
157 (let ((img image)
158 (dir (or
159 ;; Images in image-load-path.
160 (image-search-load-path image)
161 ;; Images in load-path.
162 (locate-library image)))
163 parent)
164 ;; Since the image might be in a nested directory (for
165 ;; example, mail/attach.pbm), adjust `image-directory'
166 ;; accordingly.
167 (when dir
168 (setq dir (file-name-directory dir))
169 (while (setq parent (file-name-directory img))
170 (setq img (directory-file-name parent)
171 dir (expand-file-name "../" dir))))
172 (setq image-directory-load-path dir))
174 ;; If `image-directory-load-path' isn't Emacs's image directory,
175 ;; it's probably a user preference, so use it. Then use a
176 ;; relative setting if possible; otherwise, use
177 ;; `image-directory-load-path'.
178 (cond
179 ;; User-modified image-load-path?
180 ((and image-directory-load-path
181 (not (equal image-directory-load-path
182 (file-name-as-directory
183 (expand-file-name "images" data-directory)))))
184 (setq image-directory image-directory-load-path))
185 ;; Try relative setting.
186 ((let (library-name d1ei d2ei)
187 ;; First, find library in the load-path.
188 (setq library-name (locate-library library))
189 (if (not library-name)
190 (error "Cannot find library %s in load-path" library))
191 ;; And then set image-directory relative to that.
192 (setq
193 ;; Go down 2 levels.
194 d2ei (file-name-as-directory
195 (expand-file-name
196 (concat (file-name-directory library-name) "../../etc/images")))
197 ;; Go down 1 level.
198 d1ei (file-name-as-directory
199 (expand-file-name
200 (concat (file-name-directory library-name) "../etc/images"))))
201 (setq image-directory
202 ;; Set it to nil if image is not found.
203 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
204 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
205 ;; Use Emacs's image directory.
206 (image-directory-load-path
207 (setq image-directory image-directory-load-path))
208 (no-error
209 (message "Could not find image %s for library %s" image library))
211 (error "Could not find image %s for library %s" image library)))
213 ;; Return an augmented `path' or `load-path'.
214 (nconc (list image-directory)
215 (delete image-directory (copy-sequence (or path load-path))))))
218 ;; Used to be in image-type-header-regexps, but now not used anywhere
219 ;; (since 2009-08-28).
220 (defun image-jpeg-p (data)
221 "Value is non-nil if DATA, a string, consists of JFIF image data.
222 We accept the tag Exif because that is the same format."
223 (setq data (ignore-errors (string-to-unibyte data)))
224 (when (and data (string-match-p "\\`\xff\xd8" data))
225 (catch 'jfif
226 (let ((len (length data)) (i 2))
227 (while (< i len)
228 (when (/= (aref data i) #xff)
229 (throw 'jfif nil))
230 (setq i (1+ i))
231 (when (>= (+ i 2) len)
232 (throw 'jfif nil))
233 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
234 (aref data (+ i 2))))
235 (code (aref data i)))
236 (when (and (>= code #xe0) (<= code #xef))
237 ;; APP0 LEN1 LEN2 "JFIF\0"
238 (throw 'jfif
239 (string-match-p "JFIF\\|Exif"
240 (substring data i (min (+ i nbytes) len)))))
241 (setq i (+ i 1 nbytes))))))))
244 ;;;###autoload
245 (defun image-type-from-data (data)
246 "Determine the image type from image data DATA.
247 Value is a symbol specifying the image type or nil if type cannot
248 be determined."
249 (let ((types image-type-header-regexps)
250 type)
251 (while types
252 (let ((regexp (car (car types)))
253 (image-type (cdr (car types))))
254 (if (or (and (symbolp image-type)
255 (string-match-p regexp data))
256 (and (consp image-type)
257 (funcall (car image-type) data)
258 (setq image-type (cdr image-type))))
259 (setq type image-type
260 types nil)
261 (setq types (cdr types)))))
262 type))
265 ;;;###autoload
266 (defun image-type-from-buffer ()
267 "Determine the image type from data in the current buffer.
268 Value is a symbol specifying the image type or nil if type cannot
269 be determined."
270 (let ((types image-type-header-regexps)
271 type
272 (opoint (point)))
273 (goto-char (point-min))
274 (while types
275 (let ((regexp (car (car types)))
276 (image-type (cdr (car types)))
277 data)
278 (if (or (and (symbolp image-type)
279 (looking-at-p regexp))
280 (and (consp image-type)
281 (funcall (car image-type)
282 (or data
283 (setq data
284 (buffer-substring
285 (point-min)
286 (min (point-max)
287 (+ (point-min) 256))))))
288 (setq image-type (cdr image-type))))
289 (setq type image-type
290 types nil)
291 (setq types (cdr types)))))
292 (goto-char opoint)
293 (and type
294 (boundp 'image-types)
295 (memq type image-types)
296 type)))
299 ;;;###autoload
300 (defun image-type-from-file-header (file)
301 "Determine the type of image file FILE from its first few bytes.
302 Value is a symbol specifying the image type, or nil if type cannot
303 be determined."
304 (unless (or (file-readable-p file)
305 (file-name-absolute-p file))
306 (setq file (image-search-load-path file)))
307 (and file
308 (file-readable-p file)
309 (with-temp-buffer
310 (set-buffer-multibyte nil)
311 (insert-file-contents-literally file nil 0 256)
312 (image-type-from-buffer))))
315 ;;;###autoload
316 (defun image-type-from-file-name (file)
317 "Determine the type of image file FILE from its name.
318 Value is a symbol specifying the image type, or nil if type cannot
319 be determined."
320 (let (type first)
321 (catch 'found
322 (dolist (elem image-type-file-name-regexps first)
323 (when (string-match-p (car elem) file)
324 (if (image-type-available-p (setq type (cdr elem)))
325 (throw 'found type)
326 ;; If nothing seems to be supported, return first type that matched.
327 (or first (setq first type))))))))
329 ;;;###autoload
330 (defun image-type (source &optional type data-p)
331 "Determine and return image type.
332 SOURCE is an image file name or image data.
333 Optional TYPE is a symbol describing the image type. If TYPE is omitted
334 or nil, try to determine the image type from its first few bytes
335 of image data. If that doesn't work, and SOURCE is a file name,
336 use its file extension as image type.
337 Optional DATA-P non-nil means SOURCE is a string containing image data."
338 (when (and (not data-p) (not (stringp source)))
339 (error "Invalid image file name `%s'" source))
340 (unless type
341 (setq type (if data-p
342 (image-type-from-data source)
343 (or (image-type-from-file-header source)
344 (image-type-from-file-name source))))
345 (or type (error "Cannot determine image type")))
346 (or (memq type (and (boundp 'image-types) image-types))
347 (error "Invalid image type `%s'" type))
348 type)
351 (if (fboundp 'image-metadata) ; eg not --without-x
352 (define-obsolete-function-alias 'image-extension-data
353 'image-metadata' "24.1"))
355 (define-obsolete-variable-alias
356 'image-library-alist
357 'dynamic-library-alist "24.1")
359 ;;;###autoload
360 (defun image-type-available-p (type)
361 "Return non-nil if image type TYPE is available.
362 Image types are symbols like `xbm' or `jpeg'."
363 (and (fboundp 'init-image-library)
364 (init-image-library type)))
367 ;;;###autoload
368 (defun image-type-auto-detected-p ()
369 "Return t if the current buffer contains an auto-detectable image.
370 This function is intended to be used from `magic-fallback-mode-alist'.
372 The buffer is considered to contain an auto-detectable image if
373 its beginning matches an image type in `image-type-header-regexps',
374 and that image type is present in `image-type-auto-detectable' with a
375 non-nil value. If that value is non-nil, but not t, then the image type
376 must be available."
377 (let* ((type (image-type-from-buffer))
378 (auto (and type (cdr (assq type image-type-auto-detectable)))))
379 (and auto
380 (or (eq auto t) (image-type-available-p type)))))
383 ;;;###autoload
384 (defun create-image (file-or-data &optional type data-p &rest props)
385 "Create an image.
386 FILE-OR-DATA is an image file name or image data.
387 Optional TYPE is a symbol describing the image type. If TYPE is omitted
388 or nil, try to determine the image type from its first few bytes
389 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
390 use its file extension as image type.
391 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
392 Optional PROPS are additional image attributes to assign to the image,
393 like, e.g. `:mask MASK'.
394 Value is the image created, or nil if images of type TYPE are not supported.
396 Images should not be larger than specified by `max-image-size'.
398 Image file names that are not absolute are searched for in the
399 \"images\" sub-directory of `data-directory' and
400 `x-bitmap-file-path' (in that order)."
401 ;; It is x_find_image_file in image.c that sets the search path.
402 (setq type (image-type file-or-data type data-p))
403 (when (image-type-available-p type)
404 (append (list 'image :type type (if data-p :data :file) file-or-data)
405 props)))
408 ;;;###autoload
409 (defun put-image (image pos &optional string area)
410 "Put image IMAGE in front of POS in the current buffer.
411 IMAGE must be an image created with `create-image' or `defimage'.
412 IMAGE is displayed by putting an overlay into the current buffer with a
413 `before-string' STRING that has a `display' property whose value is the
414 image. STRING is defaulted if you omit it.
415 The overlay created will have the `put-image' property set to t.
416 POS may be an integer or marker.
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 (unless string (setq string "x"))
422 (let ((buffer (current-buffer)))
423 (unless (eq (car-safe image) 'image)
424 (error "Not an image: %s" image))
425 (unless (or (null area) (memq area '(left-margin right-margin)))
426 (error "Invalid area %s" area))
427 (setq string (copy-sequence string))
428 (let ((overlay (make-overlay pos pos buffer))
429 (prop (if (null area) image (list (list 'margin area) image))))
430 (put-text-property 0 (length string) 'display prop string)
431 (overlay-put overlay 'put-image t)
432 (overlay-put overlay 'before-string string)
433 overlay)))
436 ;;;###autoload
437 (defun insert-image (image &optional string area slice)
438 "Insert IMAGE into current buffer at point.
439 IMAGE is displayed by inserting STRING into the current buffer
440 with a `display' property whose value is the image. STRING
441 defaults to a single space if you omit it.
442 AREA is where to display the image. AREA nil or omitted means
443 display it in the text area, a value of `left-margin' means
444 display it in the left marginal area, a value of `right-margin'
445 means display it in the right marginal area.
446 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
447 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
448 specifying the X and Y positions and WIDTH and HEIGHT of image area
449 to insert. A float value 0.0 - 1.0 means relative to the width or
450 height of the image; integer values are taken as pixel values."
451 ;; Use a space as least likely to cause trouble when it's a hidden
452 ;; character in the buffer.
453 (unless string (setq string " "))
454 (unless (eq (car-safe image) 'image)
455 (error "Not an image: %s" image))
456 (unless (or (null area) (memq area '(left-margin right-margin)))
457 (error "Invalid area %s" area))
458 (if area
459 (setq image (list (list 'margin area) image))
460 ;; Cons up a new spec equal but not eq to `image' so that
461 ;; inserting it twice in a row (adjacently) displays two copies of
462 ;; the image. Don't try to avoid this by looking at the display
463 ;; properties on either side so that we DTRT more often with
464 ;; cut-and-paste. (Yanking killed image text next to another copy
465 ;; of it loses anyway.)
466 (setq image (cons 'image (cdr image))))
467 (let ((start (point)))
468 (insert string)
469 (add-text-properties start (point)
470 `(display ,(if slice
471 (list (cons 'slice slice) image)
472 image) rear-nonsticky (display)))))
475 ;;;###autoload
476 (defun insert-sliced-image (image &optional string area rows cols)
477 "Insert IMAGE into current buffer at point.
478 IMAGE is displayed by inserting STRING into the current buffer
479 with a `display' property whose value is the image. The default
480 STRING is a single space.
481 AREA is where to display the image. AREA nil or omitted means
482 display it in the text area, a value of `left-margin' means
483 display it in the left marginal area, a value of `right-margin'
484 means display it in the right marginal area.
485 The image is automatically split into ROWS x COLS slices."
486 (unless string (setq string " "))
487 (unless (eq (car-safe image) 'image)
488 (error "Not an image: %s" image))
489 (unless (or (null area) (memq area '(left-margin right-margin)))
490 (error "Invalid area %s" area))
491 (if area
492 (setq image (list (list 'margin area) image))
493 ;; Cons up a new spec equal but not eq to `image' so that
494 ;; inserting it twice in a row (adjacently) displays two copies of
495 ;; the image. Don't try to avoid this by looking at the display
496 ;; properties on either side so that we DTRT more often with
497 ;; cut-and-paste. (Yanking killed image text next to another copy
498 ;; of it loses anyway.)
499 (setq image (cons 'image (cdr image))))
500 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
501 (y 0.0) (dy (/ 1.0001 (or rows 1))))
502 (while (< y 1.0)
503 (while (< x 1.0)
504 (let ((start (point)))
505 (insert string)
506 (add-text-properties start (point)
507 `(display ,(list (list 'slice x y dx dy) image)
508 rear-nonsticky (display)))
509 (setq x (+ x dx))))
510 (setq x 0.0
511 y (+ y dy))
512 (insert (propertize "\n" 'line-height t)))))
516 ;;;###autoload
517 (defun remove-images (start end &optional buffer)
518 "Remove images between START and END in BUFFER.
519 Remove only images that were put in BUFFER with calls to `put-image'.
520 BUFFER nil or omitted means use the current buffer."
521 (unless buffer
522 (setq buffer (current-buffer)))
523 (let ((overlays (overlays-in start end)))
524 (while overlays
525 (let ((overlay (car overlays)))
526 (when (overlay-get overlay 'put-image)
527 (delete-overlay overlay)))
528 (setq overlays (cdr overlays)))))
530 (defun image-search-load-path (file &optional path)
531 (unless path
532 (setq path image-load-path))
533 (let (element found filename)
534 (while (and (not found) (consp path))
535 (setq element (car path))
536 (cond
537 ((stringp element)
538 (setq found
539 (file-readable-p
540 (setq filename (expand-file-name file element)))))
541 ((and (symbolp element) (boundp element))
542 (setq element (symbol-value element))
543 (cond
544 ((stringp element)
545 (setq found
546 (file-readable-p
547 (setq filename (expand-file-name file element)))))
548 ((consp element)
549 (if (setq filename (image-search-load-path file element))
550 (setq found t))))))
551 (setq path (cdr path)))
552 (if found filename)))
554 ;;;###autoload
555 (defun find-image (specs)
556 "Find an image, choosing one of a list of image specifications.
558 SPECS is a list of image specifications.
560 Each image specification in SPECS is a property list. The contents of
561 a specification are image type dependent. All specifications must at
562 least contain the properties `:type TYPE' and either `:file FILE' or
563 `:data DATA', where TYPE is a symbol specifying the image type,
564 e.g. `xbm', FILE is the file to load the image from, and DATA is a
565 string containing the actual image data. The specification whose TYPE
566 is supported, and FILE exists, is used to construct the image
567 specification to be returned. Return nil if no specification is
568 satisfied.
570 The image is looked for in `image-load-path'.
572 Image files should not be larger than specified by `max-image-size'."
573 (let (image)
574 (while (and specs (null image))
575 (let* ((spec (car specs))
576 (type (plist-get spec :type))
577 (data (plist-get spec :data))
578 (file (plist-get spec :file))
579 found)
580 (when (image-type-available-p type)
581 (cond ((stringp file)
582 (if (setq found (image-search-load-path file))
583 (setq image
584 (cons 'image (plist-put (copy-sequence spec)
585 :file found)))))
586 ((not (null data))
587 (setq image (cons 'image spec)))))
588 (setq specs (cdr specs))))
589 image))
592 ;;;###autoload
593 (defmacro defimage (symbol specs &optional doc)
594 "Define SYMBOL as an image.
596 SPECS is a list of image specifications. DOC is an optional
597 documentation string.
599 Each image specification in SPECS is a property list. The contents of
600 a specification are image type dependent. All specifications must at
601 least contain the properties `:type TYPE' and either `:file FILE' or
602 `:data DATA', where TYPE is a symbol specifying the image type,
603 e.g. `xbm', FILE is the file to load the image from, and DATA is a
604 string containing the actual image data. The first image
605 specification whose TYPE is supported, and FILE exists, is used to
606 define SYMBOL.
608 Example:
610 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
611 (:type xbm :file \"~/test1.xbm\")))"
612 (declare (doc-string 3))
613 `(defvar ,symbol (find-image ',specs) ,doc))
616 ;;; Animated image API
618 (defvar image-default-frame-delay 0.1
619 "Default interval in seconds between frames of a multi-frame image.
620 Only used if the image does not specify a value.")
622 (defun image-multi-frame-p (image)
623 "Return non-nil if IMAGE contains more than one frame.
624 The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is
625 the number of frames (or sub-images) in the image and DELAY is the delay
626 in seconds that the image specifies between each frame. DELAY may be nil,
627 in which case you might want to use `image-default-frame-delay'."
628 (when (fboundp 'image-metadata)
629 (let* ((metadata (image-metadata image))
630 (images (plist-get metadata 'count))
631 (delay (plist-get metadata 'delay)))
632 (when (and images (> images 1))
633 (if (or (not (numberp delay)) (< delay 0))
634 (setq delay image-default-frame-delay))
635 (cons images delay)))))
637 (defun image-animated-p (image)
638 "Like `image-multi-frame-p', but returns nil if no delay is specified."
639 (let ((multi (image-multi-frame-p image)))
640 (and (cdr multi) multi)))
642 (make-obsolete 'image-animated-p 'image-multi-frame-p "24.4")
644 ;; "Destructively"?
645 (defun image-animate (image &optional index limit)
646 "Start animating IMAGE.
647 Animation occurs by destructively altering the IMAGE spec list.
649 With optional INDEX, begin animating from that animation frame.
650 LIMIT specifies how long to animate the image. If omitted or
651 nil, play the animation until the end. If t, loop forever. If a
652 number, play until that number of seconds has elapsed."
653 (let ((animation (image-multi-frame-p image))
654 timer)
655 (when animation
656 (if (setq timer (image-animate-timer image))
657 (cancel-timer timer))
658 (run-with-timer 0.2 nil 'image-animate-timeout
659 image (or index 0) (car animation)
660 0 limit))))
662 (defun image-animate-timer (image)
663 "Return the animation timer for image IMAGE."
664 ;; See cancel-function-timers
665 (let ((tail timer-list) timer)
666 (while tail
667 (setq timer (car tail)
668 tail (cdr tail))
669 (if (and (eq (timer--function timer) 'image-animate-timeout)
670 (eq (car-safe (timer--args timer)) image))
671 (setq tail nil)
672 (setq timer nil)))
673 timer))
675 (defconst image-minimum-frame-delay 0.01
676 "Minimum interval in seconds between frames of an animated image.")
678 (defun image-current-frame (image)
679 "The current frame number of IMAGE, indexed from 0."
680 (or (plist-get (cdr image) :index) 0))
682 (defun image-show-frame (image n &optional nocheck)
683 "Show frame N of IMAGE.
684 Frames are indexed from 0. Optional argument NOCHECK non-nil means
685 do not check N is within the range of frames present in the image."
686 (unless nocheck
687 (if (< n 0) (setq n 0)
688 (setq n (min n (1- (car (image-multi-frame-p image)))))))
689 (plist-put (cdr image) :index n)
690 (force-window-update))
692 (defun image-animate-get-speed (image)
693 "Return the speed factor for animating IMAGE."
694 (or (plist-get (cdr image) :speed) 1))
696 (defun image-animate-set-speed (image value &optional multiply)
697 "Set the speed factor for animating IMAGE to VALUE.
698 With optional argument MULTIPLY non-nil, treat VALUE as a
699 multiplication factor for the current value."
700 (plist-put (cdr image) :speed
701 (if multiply
702 (* value (image-animate-get-speed image))
703 value)))
705 ;; FIXME? The delay may not be the same for different sub-images,
706 ;; hence we need to call image-multi-frame-p to return it.
707 ;; But it also returns count, so why do we bother passing that as an
708 ;; argument?
709 (defun image-animate-timeout (image n count time-elapsed limit)
710 "Display animation frame N of IMAGE.
711 N=0 refers to the initial animation frame.
712 COUNT is the total number of frames in the animation.
713 TIME-ELAPSED is the total time that has elapsed since
714 `image-animate-start' was called.
715 LIMIT determines when to stop. If t, loop forever. If nil, stop
716 after displaying the last animation frame. Otherwise, stop
717 after LIMIT seconds have elapsed.
718 The minimum delay between successive frames is `image-minimum-frame-delay'.
720 If the image has a non-nil :speed property, it acts as a multiplier
721 for the animation speed. A negative value means to animate in reverse."
722 (image-show-frame image n t)
723 (let* ((speed (image-animate-get-speed image))
724 (time (float-time))
725 (animation (image-multi-frame-p image))
726 ;; Subtract off the time we took to load the image from the
727 ;; stated delay time.
728 (delay (max (+ (* (or (cdr animation) image-default-frame-delay)
729 (/ 1 (abs speed)))
730 time (- (float-time)))
731 image-minimum-frame-delay))
732 done)
733 (setq n (if (< speed 0)
734 (1- n)
735 (1+ n)))
736 (if limit
737 (cond ((>= n count) (setq n 0))
738 ((< n 0) (setq n (1- count))))
739 (and (or (>= n count) (< n 0)) (setq done t)))
740 (setq time-elapsed (+ delay time-elapsed))
741 (if (numberp limit)
742 (setq done (>= time-elapsed limit)))
743 (unless done
744 (run-with-timer delay nil 'image-animate-timeout
745 image n count time-elapsed limit))))
748 (defvar imagemagick-types-inhibit)
749 (defvar imagemagick-enabled-types)
751 (defun imagemagick-filter-types ()
752 "Return a list of the ImageMagick types to be treated as images, or nil.
753 This is the result of `imagemagick-types', including only elements
754 that match `imagemagick-enabled-types' and do not match
755 `imagemagick-types-inhibit'."
756 (when (fboundp 'imagemagick-types)
757 (cond ((null imagemagick-enabled-types) nil)
758 ((eq imagemagick-types-inhibit t) nil)
760 (delq nil
761 (mapcar
762 (lambda (type)
763 (unless (memq type imagemagick-types-inhibit)
764 (if (eq imagemagick-enabled-types t) type
765 (catch 'found
766 (dolist (enable imagemagick-enabled-types nil)
767 (if (cond ((symbolp enable) (eq enable type))
768 ((stringp enable)
769 (string-match enable
770 (symbol-name type))))
771 (throw 'found type)))))))
772 (imagemagick-types)))))))
774 (defvar imagemagick--file-regexp nil
775 "File extension regexp for ImageMagick files, if any.
776 This is the extension installed into `auto-mode-alist' and
777 `image-type-file-name-regexps' by `imagemagick-register-types'.")
779 ;;;###autoload
780 (defun imagemagick-register-types ()
781 "Register file types that can be handled by ImageMagick.
782 This function is called at startup, after loading the init file.
783 It registers the ImageMagick types returned by `imagemagick-filter-types'.
785 Registered image types are added to `auto-mode-alist', so that
786 Emacs visits them in Image mode. They are also added to
787 `image-type-file-name-regexps', so that the `image-type' function
788 recognizes these files as having image type `imagemagick'.
790 If Emacs is compiled without ImageMagick support, this does nothing."
791 (when (fboundp 'imagemagick-types)
792 (let* ((types (mapcar (lambda (type) (downcase (symbol-name type)))
793 (imagemagick-filter-types)))
794 (re (if types (concat "\\." (regexp-opt types) "\\'")))
795 (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode)
796 auto-mode-alist)))
797 (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick)
798 image-type-file-name-regexps))))
799 (if (not re)
800 (setq auto-mode-alist (delete ama-elt auto-mode-alist)
801 image-type-file-name-regexps
802 (delete itfnr-elt image-type-file-name-regexps))
803 (if ama-elt
804 (setcar ama-elt re)
805 (push (cons re 'image-mode) auto-mode-alist))
806 (if itfnr-elt
807 (setcar itfnr-elt re)
808 ;; Append to `image-type-file-name-regexps', so that we
809 ;; preferentially use specialized image libraries.
810 (add-to-list 'image-type-file-name-regexps
811 (cons re 'imagemagick) t)))
812 (setq imagemagick--file-regexp re))))
814 (defcustom imagemagick-types-inhibit
815 '(C HTML HTM INFO M TXT PDF)
816 "List of ImageMagick types that should never be treated as images.
817 This should be a list of symbols, each of which should be one of
818 the ImageMagick types listed by `imagemagick-types'. The listed
819 image types are not registered by `imagemagick-register-types'.
821 If the value is t, inhibit the use of ImageMagick for images.
823 If you change this without using customize, you must call
824 `imagemagick-register-types' afterwards.
826 If Emacs is compiled without ImageMagick support, this variable
827 has no effect."
828 :type '(choice (const :tag "Support all ImageMagick types" nil)
829 (const :tag "Disable all ImageMagick types" t)
830 (repeat symbol))
831 :initialize 'custom-initialize-default
832 :set (lambda (symbol value)
833 (set-default symbol value)
834 (imagemagick-register-types))
835 :version "24.3"
836 :group 'image)
838 (defcustom imagemagick-enabled-types
839 '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW
840 CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF
841 GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25
842 KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL
843 PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM
844 PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT
845 SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF
846 UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV
847 XWD YCbCr YCbCrA YUV)
848 "List of ImageMagick types to treat as images.
849 Each list element should be a string or symbol, representing one
850 of the image types returned by `imagemagick-types'. If the
851 element is a string, it is handled as a regexp that enables all
852 matching types.
854 The value of `imagemagick-enabled-types' may also be t, meaning
855 to enable all types that ImageMagick supports.
857 The variable `imagemagick-types-inhibit' overrides this variable.
859 If you change this without using customize, you must call
860 `imagemagick-register-types' afterwards.
862 If Emacs is compiled without ImageMagick support, this variable
863 has no effect."
864 :type '(choice (const :tag "Support all ImageMagick types" t)
865 (const :tag "Disable all ImageMagick types" nil)
866 (repeat :tag "List of types"
867 (choice (symbol :tag "type")
868 (regexp :tag "regexp"))))
869 :initialize 'custom-initialize-default
870 :set (lambda (symbol value)
871 (set-default symbol value)
872 (imagemagick-register-types))
873 :version "24.3"
874 :group 'image)
876 (imagemagick-register-types)
878 (provide 'image)
880 ;;; image.el ends here