Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 1998-2013 Free Software Foundation, Inc. |
634f4a67 PJ |
4 | |
5 | ;; Maintainer: FSF | |
04799cf5 | 6 | ;; Keywords: multimedia |
bd78fa1d | 7 | ;; Package: emacs |
7840ced1 GM |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7840ced1 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
7840ced1 GM |
15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7840ced1 GM |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;;; Code: | |
27 | ||
77f6105c MB |
28 | |
29 | (defgroup image () | |
30 | "Image support." | |
31 | :group 'multimedia) | |
32 | ||
110683ad | 33 | (defalias 'image-refresh 'image-flush) |
77f6105c | 34 | |
4fde92ef | 35 | (defconst image-type-header-regexps |
62564fc7 | 36 | `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) |
6642c904 | 37 | ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm) |
3205431d | 38 | ("\\`GIF8[79]a" . gif) |
6642c904 | 39 | ("\\`\x89PNG\r\n\x1a\n" . png) |
fa4354c1 AS |
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) | |
6642c904 JB |
45 | ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff) |
46 | ("\\`[\t\n\r ]*%!PS" . postscript) | |
d30a05d1 | 47 | ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg) |
62564fc7 JL |
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) | |
55 | ) | |
6642c904 | 56 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. |
7840ced1 | 57 | When the first bytes of an image file match REGEXP, it is assumed to |
6642c904 JB |
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.") | |
8a8ef149 | 62 | |
5fd62452 | 63 | (defvar image-type-file-name-regexps |
4fde92ef KS |
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) | |
62564fc7 JL |
72 | ("\\.tiff?\\'" . tiff) |
73 | ("\\.svgz?\\'" . svg) | |
74 | ) | |
4fde92ef KS |
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.") | |
78 | ||
a9bb04bb CY |
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. | |
6642c904 JB |
82 | (defvar image-type-auto-detectable |
83 | '((pbm . t) | |
a9bb04bb | 84 | (xbm . nil) |
6642c904 JB |
85 | (bmp . maybe) |
86 | (gif . maybe) | |
87 | (png . maybe) | |
a9bb04bb | 88 | (xpm . nil) |
6642c904 JB |
89 | (jpeg . maybe) |
90 | (tiff . maybe) | |
62564fc7 | 91 | (svg . maybe) |
6642c904 JB |
92 | (postscript . nil)) |
93 | "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files. | |
94 | \(See `image-type-auto-detected-p'). | |
95 | ||
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').") | |
4fde92ef | 101 | |
c6b7ccaa | 102 | (defvar image-format-suffixes |
8259030d LMI |
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 | |
c6b7ccaa | 106 | image types. If `create-image' is called with a :format |
8259030d LMI |
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.") | |
109 | ||
45448e64 SM |
110 | (defcustom image-load-path |
111 | (list (file-name-as-directory (expand-file-name "images" data-directory)) | |
112 | 'data-directory 'load-path) | |
5fc5ac38 | 113 | "List of locations in which to search for image files. |
93a75651 CY |
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 | |
45448e64 SM |
118 | value is used as a list of directories to search." |
119 | :type '(repeat (choice directory variable)) | |
120 | :initialize 'custom-initialize-delay) | |
dada660a | 121 | |
5248a565 | 122 | |
20c65d08 | 123 | (defun image-load-path-for-library (library image &optional path no-error) |
ae77c7ff | 124 | "Return a suitable search path for images used by LIBRARY. |
7c565097 | 125 | |
40db64d2 | 126 | It searches for IMAGE in `image-load-path' (excluding |
c0696e1b BW |
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\". | |
7c565097 | 131 | |
5248a565 BW |
132 | Then this function returns a list of directories which contains |
133 | first the directory in which IMAGE was found, followed by the | |
68ba6c49 | 134 | value of `load-path'. If PATH is given, it is used instead of |
5248a565 | 135 | `load-path'. |
20c65d08 | 136 | |
5248a565 | 137 | If NO-ERROR is non-nil and a suitable path can't be found, don't |
68ba6c49 | 138 | signal an error. Instead, return a list of directories as before, |
5248a565 | 139 | except that nil appears in place of the image directory. |
7c565097 BW |
140 | |
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': | |
144 | ||
9f036d33 BW |
145 | ;; Shush compiler. |
146 | (defvar image-load-path) | |
5248a565 BW |
147 | |
148 | (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\")) | |
9f036d33 BW |
149 | (image-load-path (cons (car load-path) |
150 | (when (boundp 'image-load-path) | |
151 | image-load-path)))) | |
5248a565 | 152 | (mh-tool-bar-folder-buttons-init))" |
7c565097 BW |
153 | (unless library (error "No library specified")) |
154 | (unless image (error "No image specified")) | |
c0696e1b BW |
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)) | |
173 | ||
44e97401 | 174 | ;; If `image-directory-load-path' isn't Emacs's image directory, |
c0696e1b BW |
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'. | |
7c565097 | 178 | (cond |
c0696e1b BW |
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)) | |
7c565097 BW |
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. | |
c0696e1b BW |
194 | d2ei (file-name-as-directory |
195 | (expand-file-name | |
196 | (concat (file-name-directory library-name) "../../etc/images"))) | |
7c565097 | 197 | ;; Go down 1 level. |
c0696e1b BW |
198 | d1ei (file-name-as-directory |
199 | (expand-file-name | |
200 | (concat (file-name-directory library-name) "../etc/images")))) | |
7c565097 BW |
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))))) | |
44e97401 | 205 | ;; Use Emacs's image directory. |
c0696e1b BW |
206 | (image-directory-load-path |
207 | (setq image-directory image-directory-load-path)) | |
20c65d08 | 208 | (no-error |
20c65d08 | 209 | (message "Could not find image %s for library %s" image library)) |
7c565097 BW |
210 | (t |
211 | (error "Could not find image %s for library %s" image library))) | |
212 | ||
5248a565 BW |
213 | ;; Return an augmented `path' or `load-path'. |
214 | (nconc (list image-directory) | |
215 | (delete image-directory (copy-sequence (or path load-path)))))) | |
216 | ||
7c565097 | 217 | |
b4ac6e8c GM |
218 | ;; Used to be in image-type-header-regexps, but now not used anywhere |
219 | ;; (since 2009-08-28). | |
8a8ef149 | 220 | (defun image-jpeg-p (data) |
9b78a6a3 RS |
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." | |
24c23999 JB |
223 | (setq data (ignore-errors (string-to-unibyte data))) |
224 | (when (and data (string-match-p "\\`\xff\xd8" data)) | |
8a8ef149 GM |
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) | |
4a9bf8a4 GM |
234 | (aref data (+ i 2)))) |
235 | (code (aref data i))) | |
236 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 237 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 238 | (throw 'jfif |
24c23999 JB |
239 | (string-match-p "JFIF\\|Exif" |
240 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 241 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
242 | |
243 | ||
244 | ;;;###autoload | |
162dec01 GM |
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 | |
7840ced1 | 248 | be determined." |
4fde92ef | 249 | (let ((types image-type-header-regexps) |
7840ced1 | 250 | type) |
4fde92ef | 251 | (while types |
7840ced1 | 252 | (let ((regexp (car (car types))) |
6642c904 | 253 | (image-type (cdr (car types)))) |
4fde92ef | 254 | (if (or (and (symbolp image-type) |
24c23999 | 255 | (string-match-p regexp data)) |
4fde92ef KS |
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)) | |
263 | ||
264 | ||
265 | ;;;###autoload | |
6642c904 | 266 | (defun image-type-from-buffer () |
4fde92ef | 267 | "Determine the image type from data in the current buffer. |
6642c904 JB |
268 | Value is a symbol specifying the image type or nil if type cannot |
269 | be determined." | |
4fde92ef KS |
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))) | |
6642c904 | 276 | (image-type (cdr (car types))) |
4fde92ef KS |
277 | data) |
278 | (if (or (and (symbolp image-type) | |
24c23999 | 279 | (looking-at-p regexp)) |
4fde92ef KS |
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)))) | |
6642c904 | 289 | (setq type image-type |
4fde92ef KS |
290 | types nil) |
291 | (setq types (cdr types))))) | |
292 | (goto-char opoint) | |
dce04f7f | 293 | (and type |
67645389 | 294 | (boundp 'image-types) |
dce04f7f CY |
295 | (memq type image-types) |
296 | type))) | |
7840ced1 GM |
297 | |
298 | ||
162dec01 GM |
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." | |
4fde92ef KS |
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) | |
6642c904 | 312 | (image-type-from-buffer)))) |
4fde92ef KS |
313 | |
314 | ||
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." | |
ea1d4aac | 320 | (let (type first) |
11fef14a GM |
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)))))))) | |
162dec01 | 328 | |
7840ced1 | 329 | ;;;###autoload |
7d88e015 | 330 | (defun image-type (source &optional type data-p) |
e7968373 | 331 | "Determine and return image type. |
7d88e015 | 332 | SOURCE is an image file name or image data. |
7840ced1 | 333 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 | 334 | or nil, try to determine the image type from its first few bytes |
7d88e015 | 335 | of image data. If that doesn't work, and SOURCE is a file name, |
9ea8de1b | 336 | use its file extension as image type. |
7d88e015 CY |
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)) | |
162dec01 | 340 | (unless type |
7d88e015 CY |
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"))) | |
28681e35 | 346 | (or (memq type (and (boundp 'image-types) image-types)) |
7d88e015 | 347 | (error "Invalid image type `%s'" type)) |
e7968373 KS |
348 | type) |
349 | ||
6642c904 | 350 | |
82ff1d13 GM |
351 | (if (fboundp 'image-metadata) ; eg not --without-x |
352 | (define-obsolete-function-alias 'image-extension-data | |
353 | 'image-metadata' "24.1")) | |
354 | ||
2e288d54 JB |
355 | (define-obsolete-variable-alias |
356 | 'image-library-alist | |
357 | 'dynamic-library-alist "24.1") | |
aa360da1 | 358 | |
e7968373 KS |
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) | |
d07ff9db | 364 | (init-image-library type))) |
e7968373 KS |
365 | |
366 | ||
6642c904 JB |
367 | ;;;###autoload |
368 | (defun image-type-auto-detected-p () | |
4837b516 | 369 | "Return t if the current buffer contains an auto-detectable image. |
7d88e015 CY |
370 | This function is intended to be used from `magic-fallback-mode-alist'. |
371 | ||
372 | The buffer is considered to contain an auto-detectable image if | |
373 | its beginning matches an image type in `image-type-header-regexps', | |
1e83e2ca GM |
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." | |
6642c904 JB |
377 | (let* ((type (image-type-from-buffer)) |
378 | (auto (and type (cdr (assq type image-type-auto-detectable))))) | |
d567be22 | 379 | (and auto |
7d88e015 | 380 | (or (eq auto t) (image-type-available-p type))))) |
6642c904 JB |
381 | |
382 | ||
e7968373 KS |
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. | |
395 | ||
2467875c GM |
396 | Images should not be larger than specified by `max-image-size'. |
397 | ||
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. | |
e7968373 | 402 | (setq type (image-type file-or-data type data-p)) |
7840ced1 | 403 | (when (image-type-available-p type) |
162dec01 GM |
404 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
405 | props))) | |
7840ced1 GM |
406 | |
407 | ||
408 | ;;;###autoload | |
cedbd8d0 | 409 | (defun put-image (image pos &optional string area) |
1571b5ff | 410 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 411 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
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 | |
cedbd8d0 | 414 | image. STRING is defaulted if you omit it. |
12dc863d | 415 | The overlay created will have the `put-image' property set to t. |
7840ced1 | 416 | POS may be an integer or marker. |
7840ced1 GM |
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' | |
46655c94 | 420 | means display it in the right marginal area." |
0ad550ba | 421 | (unless string (setq string "x")) |
1571b5ff | 422 | (let ((buffer (current-buffer))) |
cedbd8d0 | 423 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
424 | (error "Not an image: %s" image)) |
425 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
426 | (error "Invalid area %s" area)) | |
46655c94 | 427 | (setq string (copy-sequence string)) |
1571b5ff | 428 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
429 | (prop (if (null area) image (list (list 'margin area) image)))) |
430 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff | 431 | (overlay-put overlay 'put-image t) |
bc72b5d9 LMI |
432 | (overlay-put overlay 'before-string string) |
433 | overlay))) | |
7840ced1 GM |
434 | |
435 | ||
436 | ;;;###autoload | |
5af275e0 | 437 | (defun insert-image (image &optional string area slice) |
7840ced1 | 438 | "Insert IMAGE into current buffer at point. |
46655c94 | 439 | IMAGE is displayed by inserting STRING into the current buffer |
0f0a88b9 | 440 | with a `display' property whose value is the image. STRING |
913c8291 | 441 | defaults to a single space if you omit it. |
7840ced1 GM |
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' | |
5af275e0 KS |
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." | |
0ad550ba DL |
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 " ")) | |
cedbd8d0 | 454 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
455 | (error "Not an image: %s" image)) |
456 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
457 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
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)))) | |
46655c94 GM |
467 | (let ((start (point))) |
468 | (insert string) | |
469 | (add-text-properties start (point) | |
5af275e0 KS |
470 | `(display ,(if slice |
471 | (list (cons 'slice slice) image) | |
472 | image) rear-nonsticky (display))))) | |
473 | ||
474 | ||
f22c36d3 | 475 | ;;;###autoload |
5af275e0 | 476 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
477 | "Insert IMAGE into current buffer at point. |
478 | IMAGE is displayed by inserting STRING into the current buffer | |
913c8291 GM |
479 | with a `display' property whose value is the image. The default |
480 | STRING is a single space. | |
b48bc937 KS |
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. | |
68ba6c49 | 485 | The image is automatically split into ROWS x COLS slices." |
5af275e0 KS |
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)) | |
82dc617b | 512 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 513 | |
10e2102e | 514 | |
7840ced1 GM |
515 | |
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) | |
0c898dd9 DL |
527 | (delete-overlay overlay))) |
528 | (setq overlays (cdr overlays))))) | |
7840ced1 | 529 | |
4fde92ef KS |
530 | (defun image-search-load-path (file &optional path) |
531 | (unless path | |
532 | (setq path image-load-path)) | |
533 | (let (element found filename) | |
5fc5ac38 | 534 | (while (and (not found) (consp path)) |
93a75651 | 535 | (setq element (car path)) |
5fc5ac38 | 536 | (cond |
93a75651 | 537 | ((stringp element) |
5fc5ac38 CY |
538 | (setq found |
539 | (file-readable-p | |
4fde92ef | 540 | (setq filename (expand-file-name file element))))) |
93a75651 CY |
541 | ((and (symbolp element) (boundp element)) |
542 | (setq element (symbol-value element)) | |
543 | (cond | |
544 | ((stringp element) | |
545 | (setq found | |
546 | (file-readable-p | |
4fde92ef | 547 | (setq filename (expand-file-name file element))))) |
93a75651 | 548 | ((consp element) |
4fde92ef | 549 | (if (setq filename (image-search-load-path file element)) |
93a75651 | 550 | (setq found t)))))) |
5fc5ac38 | 551 | (setq path (cdr path))) |
4fde92ef | 552 | (if found filename))) |
7840ced1 GM |
553 | |
554 | ;;;###autoload | |
349c034d GM |
555 | (defun find-image (specs) |
556 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 557 | |
cedbd8d0 | 558 | SPECS is a list of image specifications. |
7840ced1 GM |
559 | |
560 | Each image specification in SPECS is a property list. The contents of | |
561 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
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 | |
cedbd8d0 DL |
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. | |
569 | ||
8646a62e CY |
570 | The image is looked for in `image-load-path'. |
571 | ||
572 | Image files should not be larger than specified by `max-image-size'." | |
7840ced1 GM |
573 | (let (image) |
574 | (while (and specs (null image)) | |
575 | (let* ((spec (car specs)) | |
576 | (type (plist-get spec :type)) | |
162dec01 | 577 | (data (plist-get spec :data)) |
7f228379 GM |
578 | (file (plist-get spec :file)) |
579 | found) | |
162dec01 GM |
580 | (when (image-type-available-p type) |
581 | (cond ((stringp file) | |
4fde92ef | 582 | (if (setq found (image-search-load-path file)) |
5fc5ac38 CY |
583 | (setq image |
584 | (cons 'image (plist-put (copy-sequence spec) | |
585 | :file found))))) | |
f64ae86c | 586 | ((not (null data)) |
162dec01 GM |
587 | (setq image (cons 'image spec))))) |
588 | (setq specs (cdr specs)))) | |
349c034d GM |
589 | image)) |
590 | ||
591 | ||
592 | ;;;###autoload | |
593 | (defmacro defimage (symbol specs &optional doc) | |
594 | "Define SYMBOL as an image. | |
595 | ||
596 | SPECS is a list of image specifications. DOC is an optional | |
597 | documentation string. | |
598 | ||
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. | |
607 | ||
608 | Example: | |
609 | ||
610 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
611 | (:type xbm :file \"~/test1.xbm\")))" | |
fd8e7a80 | 612 | (declare (doc-string 3)) |
349c034d | 613 | `(defvar ,symbol (find-image ',specs) ,doc)) |
7840ced1 | 614 | |
0608aa45 KS |
615 | \f |
616 | ;;; Animated image API | |
7840ced1 | 617 | |
99e619b6 GM |
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.") | |
621 | ||
ed8d7fca GM |
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 | (let* ((metadata (image-metadata image)) | |
629 | (images (plist-get metadata 'count)) | |
630 | (delay (plist-get metadata 'delay))) | |
631 | (when (and images (> images 1)) | |
632 | (if (or (not (numberp delay)) (< delay 0)) | |
633 | (setq delay image-default-frame-delay)) | |
634 | (cons images delay)))) | |
635 | ||
dd8620de GM |
636 | (defun image-animated-p (image) |
637 | "Like `image-multi-frame-p', but returns nil if no delay is specified." | |
638 | (let ((multi (image-multi-frame-p image))) | |
639 | (and (cdr multi) multi))) | |
640 | ||
641 | (make-obsolete 'image-animated-p 'image-multi-frame-p "24.4") | |
0608aa45 | 642 | |
eea14f31 | 643 | ;; "Destructively"? |
18af70d0 CY |
644 | (defun image-animate (image &optional index limit) |
645 | "Start animating IMAGE. | |
646 | Animation occurs by destructively altering the IMAGE spec list. | |
647 | ||
648 | With optional INDEX, begin animating from that animation frame. | |
649 | LIMIT specifies how long to animate the image. If omitted or | |
650 | nil, play the animation until the end. If t, loop forever. If a | |
651 | number, play until that number of seconds has elapsed." | |
ed8d7fca | 652 | (let ((animation (image-multi-frame-p image)) |
1100a63c CY |
653 | timer) |
654 | (when animation | |
18af70d0 CY |
655 | (if (setq timer (image-animate-timer image)) |
656 | (cancel-timer timer)) | |
1100a63c CY |
657 | (run-with-timer 0.2 nil 'image-animate-timeout |
658 | image (or index 0) (car animation) | |
659 | 0 limit)))) | |
0608aa45 KS |
660 | |
661 | (defun image-animate-timer (image) | |
662 | "Return the animation timer for image IMAGE." | |
663 | ;; See cancel-function-timers | |
664 | (let ((tail timer-list) timer) | |
665 | (while tail | |
666 | (setq timer (car tail) | |
667 | tail (cdr tail)) | |
72eac303 PE |
668 | (if (and (eq (timer--function timer) 'image-animate-timeout) |
669 | (eq (car-safe (timer--args timer)) image)) | |
0608aa45 KS |
670 | (setq tail nil) |
671 | (setq timer nil))) | |
672 | timer)) | |
673 | ||
99e619b6 GM |
674 | (defconst image-minimum-frame-delay 0.01 |
675 | "Minimum interval in seconds between frames of an animated image.") | |
676 | ||
dc504515 GM |
677 | (defun image-current-frame (image) |
678 | "The current frame number of IMAGE, indexed from 0." | |
679 | (or (plist-get (cdr image) :index) 0)) | |
bb9dfee1 | 680 | |
dc504515 | 681 | (defun image-show-frame (image n &optional nocheck) |
c0211c4e GM |
682 | "Show frame N of IMAGE. |
683 | Frames are indexed from 0. Optional argument NOCHECK non-nil means | |
684 | do not check N is within the range of frames present in the image." | |
685 | (unless nocheck | |
686 | (if (< n 0) (setq n 0) | |
ed8d7fca | 687 | (setq n (min n (1- (car (image-multi-frame-p image))))))) |
c0211c4e | 688 | (plist-put (cdr image) :index n) |
c0211c4e GM |
689 | (force-window-update)) |
690 | ||
3a2ddc2d GM |
691 | (defun image-animate-get-speed (image) |
692 | "Return the speed factor for animating IMAGE." | |
693 | (or (plist-get (cdr image) :speed) 1)) | |
694 | ||
695 | (defun image-animate-set-speed (image value &optional multiply) | |
696 | "Set the speed factor for animating IMAGE to VALUE. | |
697 | With optional argument MULTIPLY non-nil, treat VALUE as a | |
698 | multiplication factor for the current value." | |
699 | (plist-put (cdr image) :speed | |
700 | (if multiply | |
701 | (* value (image-animate-get-speed image)) | |
702 | value))) | |
703 | ||
eea14f31 | 704 | ;; FIXME? The delay may not be the same for different sub-images, |
ed8d7fca | 705 | ;; hence we need to call image-multi-frame-p to return it. |
eea14f31 GM |
706 | ;; But it also returns count, so why do we bother passing that as an |
707 | ;; argument? | |
1100a63c | 708 | (defun image-animate-timeout (image n count time-elapsed limit) |
e8cbec34 CY |
709 | "Display animation frame N of IMAGE. |
710 | N=0 refers to the initial animation frame. | |
711 | COUNT is the total number of frames in the animation. | |
e8cbec34 CY |
712 | TIME-ELAPSED is the total time that has elapsed since |
713 | `image-animate-start' was called. | |
18af70d0 | 714 | LIMIT determines when to stop. If t, loop forever. If nil, stop |
e8cbec34 | 715 | after displaying the last animation frame. Otherwise, stop |
eea14f31 | 716 | after LIMIT seconds have elapsed. |
3a2ddc2d GM |
717 | The minimum delay between successive frames is `image-minimum-frame-delay'. |
718 | ||
719 | If the image has a non-nil :speed property, it acts as a multiplier | |
720 | for the animation speed. A negative value means to animate in reverse." | |
dc504515 | 721 | (image-show-frame image n t) |
3a2ddc2d GM |
722 | (let* ((speed (image-animate-get-speed image)) |
723 | (time (float-time)) | |
ed8d7fca | 724 | (animation (image-multi-frame-p image)) |
1100a63c CY |
725 | ;; Subtract off the time we took to load the image from the |
726 | ;; stated delay time. | |
3a2ddc2d GM |
727 | (delay (max (+ (* (or (cdr animation) image-default-frame-delay) |
728 | (/ 1 (abs speed))) | |
ed8d7fca | 729 | time (- (float-time))) |
99e619b6 | 730 | image-minimum-frame-delay)) |
1100a63c | 731 | done) |
3a2ddc2d GM |
732 | (setq n (if (< speed 0) |
733 | (1- n) | |
734 | (1+ n))) | |
735 | (if limit | |
736 | (cond ((>= n count) (setq n 0)) | |
737 | ((< n 0) (setq n (1- count)))) | |
738 | (and (or (>= n count) (< n 0)) (setq done t))) | |
e8cbec34 | 739 | (setq time-elapsed (+ delay time-elapsed)) |
18af70d0 CY |
740 | (if (numberp limit) |
741 | (setq done (>= time-elapsed limit))) | |
e8cbec34 CY |
742 | (unless done |
743 | (run-with-timer delay nil 'image-animate-timeout | |
1100a63c | 744 | image n count time-elapsed limit)))) |
0608aa45 KS |
745 | |
746 | \f | |
60b5f187 | 747 | (defvar imagemagick-types-inhibit) |
95f520b5 | 748 | (defvar imagemagick-enabled-types) |
60b5f187 GM |
749 | |
750 | (defun imagemagick-filter-types () | |
751 | "Return a list of the ImageMagick types to be treated as images, or nil. | |
752 | This is the result of `imagemagick-types', including only elements | |
95f520b5 | 753 | that match `imagemagick-enabled-types' and do not match |
60b5f187 GM |
754 | `imagemagick-types-inhibit'." |
755 | (when (fboundp 'imagemagick-types) | |
95f520b5 | 756 | (cond ((null imagemagick-enabled-types) nil) |
60b5f187 | 757 | ((eq imagemagick-types-inhibit t) nil) |
60b5f187 GM |
758 | (t |
759 | (delq nil | |
760 | (mapcar | |
761 | (lambda (type) | |
762 | (unless (memq type imagemagick-types-inhibit) | |
95f520b5 | 763 | (if (eq imagemagick-enabled-types t) type |
43f5c7ae | 764 | (catch 'found |
95f520b5 | 765 | (dolist (enable imagemagick-enabled-types nil) |
43f5c7ae GM |
766 | (if (cond ((symbolp enable) (eq enable type)) |
767 | ((stringp enable) | |
768 | (string-match enable | |
769 | (symbol-name type)))) | |
770 | (throw 'found type))))))) | |
60b5f187 GM |
771 | (imagemagick-types))))))) |
772 | ||
c505aaeb CY |
773 | (defvar imagemagick--file-regexp nil |
774 | "File extension regexp for ImageMagick files, if any. | |
775 | This is the extension installed into `auto-mode-alist' and | |
776 | `image-type-file-name-regexps' by `imagemagick-register-types'.") | |
3de25479 JV |
777 | |
778 | ;;;###autoload | |
ce07fa9a | 779 | (defun imagemagick-register-types () |
d66c4c7c | 780 | "Register file types that can be handled by ImageMagick. |
c505aaeb | 781 | This function is called at startup, after loading the init file. |
60b5f187 | 782 | It registers the ImageMagick types returned by `imagemagick-filter-types'. |
5319014e CY |
783 | |
784 | Registered image types are added to `auto-mode-alist', so that | |
785 | Emacs visits them in Image mode. They are also added to | |
786 | `image-type-file-name-regexps', so that the `image-type' function | |
787 | recognizes these files as having image type `imagemagick'. | |
d66c4c7c | 788 | |
32d72c2f | 789 | If Emacs is compiled without ImageMagick support, this does nothing." |
d66c4c7c | 790 | (when (fboundp 'imagemagick-types) |
60b5f187 GM |
791 | (let* ((types (mapcar (lambda (type) (downcase (symbol-name type))) |
792 | (imagemagick-filter-types))) | |
793 | (re (if types (concat "\\." (regexp-opt types) "\\'"))) | |
32d72c2f GM |
794 | (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode) |
795 | auto-mode-alist))) | |
796 | (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick) | |
797 | image-type-file-name-regexps)))) | |
798 | (if (not re) | |
799 | (setq auto-mode-alist (delete ama-elt auto-mode-alist) | |
800 | image-type-file-name-regexps | |
801 | (delete itfnr-elt image-type-file-name-regexps)) | |
802 | (if ama-elt | |
803 | (setcar ama-elt re) | |
804 | (push (cons re 'image-mode) auto-mode-alist)) | |
805 | (if itfnr-elt | |
806 | (setcar itfnr-elt re) | |
72834e10 CY |
807 | ;; Append to `image-type-file-name-regexps', so that we |
808 | ;; preferentially use specialized image libraries. | |
809 | (add-to-list 'image-type-file-name-regexps | |
810 | (cons re 'imagemagick) t))) | |
c505aaeb CY |
811 | (setq imagemagick--file-regexp re)))) |
812 | ||
041df390 CY |
813 | (defcustom imagemagick-types-inhibit |
814 | '(C HTML HTM INFO M TXT PDF) | |
95f520b5 | 815 | "List of ImageMagick types that should never be treated as images. |
c505aaeb | 816 | This should be a list of symbols, each of which should be one of |
95f520b5 | 817 | the ImageMagick types listed by `imagemagick-types'. The listed |
c505aaeb CY |
818 | image types are not registered by `imagemagick-register-types'. |
819 | ||
820 | If the value is t, inhibit the use of ImageMagick for images. | |
821 | ||
87eb79c2 GM |
822 | If you change this without using customize, you must call |
823 | `imagemagick-register-types' afterwards. | |
824 | ||
c505aaeb CY |
825 | If Emacs is compiled without ImageMagick support, this variable |
826 | has no effect." | |
827 | :type '(choice (const :tag "Support all ImageMagick types" nil) | |
828 | (const :tag "Disable all ImageMagick types" t) | |
829 | (repeat symbol)) | |
32d72c2f | 830 | :initialize 'custom-initialize-default |
c505aaeb CY |
831 | :set (lambda (symbol value) |
832 | (set-default symbol value) | |
833 | (imagemagick-register-types)) | |
2a1e2476 | 834 | :version "24.3" |
c505aaeb | 835 | :group 'image) |
3de25479 | 836 | |
95f520b5 | 837 | (defcustom imagemagick-enabled-types |
47b36b94 | 838 | '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW |
041df390 CY |
839 | CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF |
840 | GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25 | |
841 | KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL | |
842 | PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM | |
843 | PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT | |
844 | SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF | |
845 | UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV | |
846 | XWD YCbCr YCbCrA YUV) | |
32d72c2f | 847 | "List of ImageMagick types to treat as images. |
95f520b5 CY |
848 | Each list element should be a string or symbol, representing one |
849 | of the image types returned by `imagemagick-types'. If the | |
850 | element is a string, it is handled as a regexp that enables all | |
851 | matching types. | |
32d72c2f | 852 | |
95f520b5 CY |
853 | The value of `imagemagick-enabled-types' may also be t, meaning |
854 | to enable all types that ImageMagick supports. | |
32d72c2f GM |
855 | |
856 | The variable `imagemagick-types-inhibit' overrides this variable. | |
857 | ||
eda82a31 | 858 | If you change this without using customize, you must call |
32d72c2f GM |
859 | `imagemagick-register-types' afterwards. |
860 | ||
861 | If Emacs is compiled without ImageMagick support, this variable | |
862 | has no effect." | |
863 | :type '(choice (const :tag "Support all ImageMagick types" t) | |
864 | (const :tag "Disable all ImageMagick types" nil) | |
865 | (repeat :tag "List of types" | |
866 | (choice (symbol :tag "type") | |
867 | (regexp :tag "regexp")))) | |
868 | :initialize 'custom-initialize-default | |
869 | :set (lambda (symbol value) | |
870 | (set-default symbol value) | |
871 | (imagemagick-register-types)) | |
2a1e2476 | 872 | :version "24.3" |
32d72c2f GM |
873 | :group 'image) |
874 | ||
875 | (imagemagick-register-types) | |
876 | ||
7840ced1 GM |
877 | (provide 'image) |
878 | ||
e43367a0 | 879 | ;;; image.el ends here |