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 | |
8259030d LMI |
102 | (defvar image-content-type-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 :content-type | |
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 CY |
293 | (and type |
294 | (memq type image-types) | |
295 | type))) | |
7840ced1 GM |
296 | |
297 | ||
162dec01 GM |
298 | ;;;###autoload |
299 | (defun image-type-from-file-header (file) | |
300 | "Determine the type of image file FILE from its first few bytes. | |
301 | Value is a symbol specifying the image type, or nil if type cannot | |
302 | be determined." | |
4fde92ef KS |
303 | (unless (or (file-readable-p file) |
304 | (file-name-absolute-p file)) | |
305 | (setq file (image-search-load-path file))) | |
306 | (and file | |
307 | (file-readable-p file) | |
308 | (with-temp-buffer | |
309 | (set-buffer-multibyte nil) | |
310 | (insert-file-contents-literally file nil 0 256) | |
6642c904 | 311 | (image-type-from-buffer)))) |
4fde92ef KS |
312 | |
313 | ||
314 | ;;;###autoload | |
315 | (defun image-type-from-file-name (file) | |
316 | "Determine the type of image file FILE from its name. | |
317 | Value is a symbol specifying the image type, or nil if type cannot | |
318 | be determined." | |
ea1d4aac | 319 | (let (type first) |
11fef14a GM |
320 | (catch 'found |
321 | (dolist (elem image-type-file-name-regexps first) | |
322 | (when (string-match-p (car elem) file) | |
323 | (if (image-type-available-p (setq type (cdr elem))) | |
324 | (throw 'found type) | |
325 | ;; If nothing seems to be supported, return first type that matched. | |
326 | (or first (setq first type)))))))) | |
162dec01 | 327 | |
7840ced1 | 328 | ;;;###autoload |
7d88e015 | 329 | (defun image-type (source &optional type data-p) |
e7968373 | 330 | "Determine and return image type. |
7d88e015 | 331 | SOURCE is an image file name or image data. |
7840ced1 | 332 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 | 333 | or nil, try to determine the image type from its first few bytes |
7d88e015 | 334 | of image data. If that doesn't work, and SOURCE is a file name, |
9ea8de1b | 335 | use its file extension as image type. |
7d88e015 CY |
336 | Optional DATA-P non-nil means SOURCE is a string containing image data." |
337 | (when (and (not data-p) (not (stringp source))) | |
338 | (error "Invalid image file name `%s'" source)) | |
162dec01 | 339 | (unless type |
7d88e015 CY |
340 | (setq type (if data-p |
341 | (image-type-from-data source) | |
342 | (or (image-type-from-file-header source) | |
343 | (image-type-from-file-name source)))) | |
344 | (or type (error "Cannot determine image type"))) | |
28681e35 | 345 | (or (memq type (and (boundp 'image-types) image-types)) |
7d88e015 | 346 | (error "Invalid image type `%s'" type)) |
e7968373 KS |
347 | type) |
348 | ||
6642c904 | 349 | |
82ff1d13 GM |
350 | (if (fboundp 'image-metadata) ; eg not --without-x |
351 | (define-obsolete-function-alias 'image-extension-data | |
352 | 'image-metadata' "24.1")) | |
353 | ||
2e288d54 JB |
354 | (define-obsolete-variable-alias |
355 | 'image-library-alist | |
356 | 'dynamic-library-alist "24.1") | |
aa360da1 | 357 | |
e7968373 KS |
358 | ;;;###autoload |
359 | (defun image-type-available-p (type) | |
360 | "Return non-nil if image type TYPE is available. | |
361 | Image types are symbols like `xbm' or `jpeg'." | |
362 | (and (fboundp 'init-image-library) | |
d07ff9db | 363 | (init-image-library type))) |
e7968373 KS |
364 | |
365 | ||
6642c904 JB |
366 | ;;;###autoload |
367 | (defun image-type-auto-detected-p () | |
4837b516 | 368 | "Return t if the current buffer contains an auto-detectable image. |
7d88e015 CY |
369 | This function is intended to be used from `magic-fallback-mode-alist'. |
370 | ||
371 | The buffer is considered to contain an auto-detectable image if | |
372 | its beginning matches an image type in `image-type-header-regexps', | |
1e83e2ca GM |
373 | and that image type is present in `image-type-auto-detectable' with a |
374 | non-nil value. If that value is non-nil, but not t, then the image type | |
375 | must be available." | |
6642c904 JB |
376 | (let* ((type (image-type-from-buffer)) |
377 | (auto (and type (cdr (assq type image-type-auto-detectable))))) | |
d567be22 | 378 | (and auto |
7d88e015 | 379 | (or (eq auto t) (image-type-available-p type))))) |
6642c904 JB |
380 | |
381 | ||
e7968373 KS |
382 | ;;;###autoload |
383 | (defun create-image (file-or-data &optional type data-p &rest props) | |
384 | "Create an image. | |
385 | FILE-OR-DATA is an image file name or image data. | |
386 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | |
387 | or nil, try to determine the image type from its first few bytes | |
388 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
389 | use its file extension as image type. | |
390 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. | |
391 | Optional PROPS are additional image attributes to assign to the image, | |
392 | like, e.g. `:mask MASK'. | |
393 | Value is the image created, or nil if images of type TYPE are not supported. | |
394 | ||
2467875c GM |
395 | Images should not be larger than specified by `max-image-size'. |
396 | ||
397 | Image file names that are not absolute are searched for in the | |
398 | \"images\" sub-directory of `data-directory' and | |
399 | `x-bitmap-file-path' (in that order)." | |
400 | ;; It is x_find_image_file in image.c that sets the search path. | |
e7968373 | 401 | (setq type (image-type file-or-data type data-p)) |
7840ced1 | 402 | (when (image-type-available-p type) |
162dec01 GM |
403 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
404 | props))) | |
7840ced1 GM |
405 | |
406 | ||
407 | ;;;###autoload | |
cedbd8d0 | 408 | (defun put-image (image pos &optional string area) |
1571b5ff | 409 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 410 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
411 | IMAGE is displayed by putting an overlay into the current buffer with a |
412 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 413 | image. STRING is defaulted if you omit it. |
12dc863d | 414 | The overlay created will have the `put-image' property set to t. |
7840ced1 | 415 | POS may be an integer or marker. |
7840ced1 GM |
416 | AREA is where to display the image. AREA nil or omitted means |
417 | display it in the text area, a value of `left-margin' means | |
418 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 419 | means display it in the right marginal area." |
0ad550ba | 420 | (unless string (setq string "x")) |
1571b5ff | 421 | (let ((buffer (current-buffer))) |
cedbd8d0 | 422 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
423 | (error "Not an image: %s" image)) |
424 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
425 | (error "Invalid area %s" area)) | |
46655c94 | 426 | (setq string (copy-sequence string)) |
1571b5ff | 427 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
428 | (prop (if (null area) image (list (list 'margin area) image)))) |
429 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff | 430 | (overlay-put overlay 'put-image t) |
bc72b5d9 LMI |
431 | (overlay-put overlay 'before-string string) |
432 | overlay))) | |
7840ced1 GM |
433 | |
434 | ||
435 | ;;;###autoload | |
5af275e0 | 436 | (defun insert-image (image &optional string area slice) |
7840ced1 | 437 | "Insert IMAGE into current buffer at point. |
46655c94 | 438 | IMAGE is displayed by inserting STRING into the current buffer |
0f0a88b9 | 439 | with a `display' property whose value is the image. STRING |
913c8291 | 440 | defaults to a single space if you omit it. |
7840ced1 GM |
441 | AREA is where to display the image. AREA nil or omitted means |
442 | display it in the text area, a value of `left-margin' means | |
443 | display it in the left marginal area, a value of `right-margin' | |
5af275e0 KS |
444 | means display it in the right marginal area. |
445 | SLICE specifies slice of IMAGE to insert. SLICE nil or omitted | |
446 | means insert whole image. SLICE is a list (X Y WIDTH HEIGHT) | |
447 | specifying the X and Y positions and WIDTH and HEIGHT of image area | |
448 | to insert. A float value 0.0 - 1.0 means relative to the width or | |
449 | height of the image; integer values are taken as pixel values." | |
0ad550ba DL |
450 | ;; Use a space as least likely to cause trouble when it's a hidden |
451 | ;; character in the buffer. | |
452 | (unless string (setq string " ")) | |
cedbd8d0 | 453 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
454 | (error "Not an image: %s" image)) |
455 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
456 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
457 | (if area |
458 | (setq image (list (list 'margin area) image)) | |
459 | ;; Cons up a new spec equal but not eq to `image' so that | |
460 | ;; inserting it twice in a row (adjacently) displays two copies of | |
461 | ;; the image. Don't try to avoid this by looking at the display | |
462 | ;; properties on either side so that we DTRT more often with | |
463 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
464 | ;; of it loses anyway.) | |
465 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
466 | (let ((start (point))) |
467 | (insert string) | |
468 | (add-text-properties start (point) | |
5af275e0 KS |
469 | `(display ,(if slice |
470 | (list (cons 'slice slice) image) | |
471 | image) rear-nonsticky (display))))) | |
472 | ||
473 | ||
f22c36d3 | 474 | ;;;###autoload |
5af275e0 | 475 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
476 | "Insert IMAGE into current buffer at point. |
477 | IMAGE is displayed by inserting STRING into the current buffer | |
913c8291 GM |
478 | with a `display' property whose value is the image. The default |
479 | STRING is a single space. | |
b48bc937 KS |
480 | AREA is where to display the image. AREA nil or omitted means |
481 | display it in the text area, a value of `left-margin' means | |
482 | display it in the left marginal area, a value of `right-margin' | |
483 | means display it in the right marginal area. | |
68ba6c49 | 484 | The image is automatically split into ROWS x COLS slices." |
5af275e0 KS |
485 | (unless string (setq string " ")) |
486 | (unless (eq (car-safe image) 'image) | |
487 | (error "Not an image: %s" image)) | |
488 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
489 | (error "Invalid area %s" area)) | |
490 | (if area | |
491 | (setq image (list (list 'margin area) image)) | |
492 | ;; Cons up a new spec equal but not eq to `image' so that | |
493 | ;; inserting it twice in a row (adjacently) displays two copies of | |
494 | ;; the image. Don't try to avoid this by looking at the display | |
495 | ;; properties on either side so that we DTRT more often with | |
496 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
497 | ;; of it loses anyway.) | |
498 | (setq image (cons 'image (cdr image)))) | |
499 | (let ((x 0.0) (dx (/ 1.0001 (or cols 1))) | |
500 | (y 0.0) (dy (/ 1.0001 (or rows 1)))) | |
501 | (while (< y 1.0) | |
502 | (while (< x 1.0) | |
503 | (let ((start (point))) | |
504 | (insert string) | |
505 | (add-text-properties start (point) | |
506 | `(display ,(list (list 'slice x y dx dy) image) | |
507 | rear-nonsticky (display))) | |
508 | (setq x (+ x dx)))) | |
509 | (setq x 0.0 | |
510 | y (+ y dy)) | |
82dc617b | 511 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 512 | |
10e2102e | 513 | |
7840ced1 GM |
514 | |
515 | ;;;###autoload | |
516 | (defun remove-images (start end &optional buffer) | |
517 | "Remove images between START and END in BUFFER. | |
518 | Remove only images that were put in BUFFER with calls to `put-image'. | |
519 | BUFFER nil or omitted means use the current buffer." | |
520 | (unless buffer | |
521 | (setq buffer (current-buffer))) | |
522 | (let ((overlays (overlays-in start end))) | |
523 | (while overlays | |
524 | (let ((overlay (car overlays))) | |
525 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
526 | (delete-overlay overlay))) |
527 | (setq overlays (cdr overlays))))) | |
7840ced1 | 528 | |
4fde92ef KS |
529 | (defun image-search-load-path (file &optional path) |
530 | (unless path | |
531 | (setq path image-load-path)) | |
532 | (let (element found filename) | |
5fc5ac38 | 533 | (while (and (not found) (consp path)) |
93a75651 | 534 | (setq element (car path)) |
5fc5ac38 | 535 | (cond |
93a75651 | 536 | ((stringp element) |
5fc5ac38 CY |
537 | (setq found |
538 | (file-readable-p | |
4fde92ef | 539 | (setq filename (expand-file-name file element))))) |
93a75651 CY |
540 | ((and (symbolp element) (boundp element)) |
541 | (setq element (symbol-value element)) | |
542 | (cond | |
543 | ((stringp element) | |
544 | (setq found | |
545 | (file-readable-p | |
4fde92ef | 546 | (setq filename (expand-file-name file element))))) |
93a75651 | 547 | ((consp element) |
4fde92ef | 548 | (if (setq filename (image-search-load-path file element)) |
93a75651 | 549 | (setq found t)))))) |
5fc5ac38 | 550 | (setq path (cdr path))) |
4fde92ef | 551 | (if found filename))) |
7840ced1 GM |
552 | |
553 | ;;;###autoload | |
349c034d GM |
554 | (defun find-image (specs) |
555 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 556 | |
cedbd8d0 | 557 | SPECS is a list of image specifications. |
7840ced1 GM |
558 | |
559 | Each image specification in SPECS is a property list. The contents of | |
560 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
561 | least contain the properties `:type TYPE' and either `:file FILE' or |
562 | `:data DATA', where TYPE is a symbol specifying the image type, | |
563 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
cedbd8d0 DL |
564 | string containing the actual image data. The specification whose TYPE |
565 | is supported, and FILE exists, is used to construct the image | |
566 | specification to be returned. Return nil if no specification is | |
567 | satisfied. | |
568 | ||
8646a62e CY |
569 | The image is looked for in `image-load-path'. |
570 | ||
571 | Image files should not be larger than specified by `max-image-size'." | |
7840ced1 GM |
572 | (let (image) |
573 | (while (and specs (null image)) | |
574 | (let* ((spec (car specs)) | |
575 | (type (plist-get spec :type)) | |
162dec01 | 576 | (data (plist-get spec :data)) |
7f228379 GM |
577 | (file (plist-get spec :file)) |
578 | found) | |
162dec01 GM |
579 | (when (image-type-available-p type) |
580 | (cond ((stringp file) | |
4fde92ef | 581 | (if (setq found (image-search-load-path file)) |
5fc5ac38 CY |
582 | (setq image |
583 | (cons 'image (plist-put (copy-sequence spec) | |
584 | :file found))))) | |
f64ae86c | 585 | ((not (null data)) |
162dec01 GM |
586 | (setq image (cons 'image spec))))) |
587 | (setq specs (cdr specs)))) | |
349c034d GM |
588 | image)) |
589 | ||
590 | ||
591 | ;;;###autoload | |
592 | (defmacro defimage (symbol specs &optional doc) | |
593 | "Define SYMBOL as an image. | |
594 | ||
595 | SPECS is a list of image specifications. DOC is an optional | |
596 | documentation string. | |
597 | ||
598 | Each image specification in SPECS is a property list. The contents of | |
599 | a specification are image type dependent. All specifications must at | |
600 | least contain the properties `:type TYPE' and either `:file FILE' or | |
601 | `:data DATA', where TYPE is a symbol specifying the image type, | |
602 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
603 | string containing the actual image data. The first image | |
604 | specification whose TYPE is supported, and FILE exists, is used to | |
605 | define SYMBOL. | |
606 | ||
607 | Example: | |
608 | ||
609 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
610 | (:type xbm :file \"~/test1.xbm\")))" | |
fd8e7a80 | 611 | (declare (doc-string 3)) |
349c034d | 612 | `(defvar ,symbol (find-image ',specs) ,doc)) |
7840ced1 | 613 | |
0608aa45 KS |
614 | \f |
615 | ;;; Animated image API | |
7840ced1 | 616 | |
99e619b6 GM |
617 | (defvar image-default-frame-delay 0.1 |
618 | "Default interval in seconds between frames of a multi-frame image. | |
619 | Only used if the image does not specify a value.") | |
620 | ||
ed8d7fca GM |
621 | (defun image-multi-frame-p (image) |
622 | "Return non-nil if IMAGE contains more than one frame. | |
623 | The actual return value is a cons (NIMAGES . DELAY), where NIMAGES is | |
624 | the number of frames (or sub-images) in the image and DELAY is the delay | |
625 | in seconds that the image specifies between each frame. DELAY may be nil, | |
626 | in which case you might want to use `image-default-frame-delay'." | |
627 | (let* ((metadata (image-metadata image)) | |
628 | (images (plist-get metadata 'count)) | |
629 | (delay (plist-get metadata 'delay))) | |
630 | (when (and images (> images 1)) | |
631 | (if (or (not (numberp delay)) (< delay 0)) | |
632 | (setq delay image-default-frame-delay)) | |
633 | (cons images delay)))) | |
634 | ||
dd8620de GM |
635 | (defun image-animated-p (image) |
636 | "Like `image-multi-frame-p', but returns nil if no delay is specified." | |
637 | (let ((multi (image-multi-frame-p image))) | |
638 | (and (cdr multi) multi))) | |
639 | ||
640 | (make-obsolete 'image-animated-p 'image-multi-frame-p "24.4") | |
0608aa45 | 641 | |
eea14f31 | 642 | ;; "Destructively"? |
18af70d0 CY |
643 | (defun image-animate (image &optional index limit) |
644 | "Start animating IMAGE. | |
645 | Animation occurs by destructively altering the IMAGE spec list. | |
646 | ||
647 | With optional INDEX, begin animating from that animation frame. | |
648 | LIMIT specifies how long to animate the image. If omitted or | |
649 | nil, play the animation until the end. If t, loop forever. If a | |
650 | number, play until that number of seconds has elapsed." | |
ed8d7fca | 651 | (let ((animation (image-multi-frame-p image)) |
1100a63c CY |
652 | timer) |
653 | (when animation | |
18af70d0 CY |
654 | (if (setq timer (image-animate-timer image)) |
655 | (cancel-timer timer)) | |
1100a63c CY |
656 | (run-with-timer 0.2 nil 'image-animate-timeout |
657 | image (or index 0) (car animation) | |
658 | 0 limit)))) | |
0608aa45 KS |
659 | |
660 | (defun image-animate-timer (image) | |
661 | "Return the animation timer for image IMAGE." | |
662 | ;; See cancel-function-timers | |
663 | (let ((tail timer-list) timer) | |
664 | (while tail | |
665 | (setq timer (car tail) | |
666 | tail (cdr tail)) | |
72eac303 PE |
667 | (if (and (eq (timer--function timer) 'image-animate-timeout) |
668 | (eq (car-safe (timer--args timer)) image)) | |
0608aa45 KS |
669 | (setq tail nil) |
670 | (setq timer nil))) | |
671 | timer)) | |
672 | ||
99e619b6 GM |
673 | (defconst image-minimum-frame-delay 0.01 |
674 | "Minimum interval in seconds between frames of an animated image.") | |
675 | ||
dc504515 GM |
676 | (defun image-current-frame (image) |
677 | "The current frame number of IMAGE, indexed from 0." | |
678 | (or (plist-get (cdr image) :index) 0)) | |
bb9dfee1 | 679 | |
dc504515 | 680 | (defun image-show-frame (image n &optional nocheck) |
c0211c4e GM |
681 | "Show frame N of IMAGE. |
682 | Frames are indexed from 0. Optional argument NOCHECK non-nil means | |
683 | do not check N is within the range of frames present in the image." | |
684 | (unless nocheck | |
685 | (if (< n 0) (setq n 0) | |
ed8d7fca | 686 | (setq n (min n (1- (car (image-multi-frame-p image))))))) |
c0211c4e | 687 | (plist-put (cdr image) :index n) |
c0211c4e GM |
688 | (force-window-update)) |
689 | ||
eea14f31 | 690 | ;; FIXME? The delay may not be the same for different sub-images, |
ed8d7fca | 691 | ;; hence we need to call image-multi-frame-p to return it. |
eea14f31 GM |
692 | ;; But it also returns count, so why do we bother passing that as an |
693 | ;; argument? | |
1100a63c | 694 | (defun image-animate-timeout (image n count time-elapsed limit) |
e8cbec34 CY |
695 | "Display animation frame N of IMAGE. |
696 | N=0 refers to the initial animation frame. | |
697 | COUNT is the total number of frames in the animation. | |
e8cbec34 CY |
698 | TIME-ELAPSED is the total time that has elapsed since |
699 | `image-animate-start' was called. | |
18af70d0 | 700 | LIMIT determines when to stop. If t, loop forever. If nil, stop |
e8cbec34 | 701 | after displaying the last animation frame. Otherwise, stop |
eea14f31 | 702 | after LIMIT seconds have elapsed. |
99e619b6 | 703 | The minimum delay between successive frames is `image-minimum-frame-delay'." |
dc504515 | 704 | (image-show-frame image n t) |
1100a63c CY |
705 | (setq n (1+ n)) |
706 | (let* ((time (float-time)) | |
ed8d7fca | 707 | (animation (image-multi-frame-p image)) |
1100a63c CY |
708 | ;; Subtract off the time we took to load the image from the |
709 | ;; stated delay time. | |
ed8d7fca GM |
710 | (delay (max (+ (or (cdr animation) image-default-frame-delay) |
711 | time (- (float-time))) | |
99e619b6 | 712 | image-minimum-frame-delay)) |
1100a63c | 713 | done) |
e8cbec34 | 714 | (if (>= n count) |
18af70d0 | 715 | (if limit |
e8cbec34 CY |
716 | (setq n 0) |
717 | (setq done t))) | |
718 | (setq time-elapsed (+ delay time-elapsed)) | |
18af70d0 CY |
719 | (if (numberp limit) |
720 | (setq done (>= time-elapsed limit))) | |
e8cbec34 CY |
721 | (unless done |
722 | (run-with-timer delay nil 'image-animate-timeout | |
1100a63c | 723 | image n count time-elapsed limit)))) |
0608aa45 KS |
724 | |
725 | \f | |
60b5f187 | 726 | (defvar imagemagick-types-inhibit) |
95f520b5 | 727 | (defvar imagemagick-enabled-types) |
60b5f187 GM |
728 | |
729 | (defun imagemagick-filter-types () | |
730 | "Return a list of the ImageMagick types to be treated as images, or nil. | |
731 | This is the result of `imagemagick-types', including only elements | |
95f520b5 | 732 | that match `imagemagick-enabled-types' and do not match |
60b5f187 GM |
733 | `imagemagick-types-inhibit'." |
734 | (when (fboundp 'imagemagick-types) | |
95f520b5 | 735 | (cond ((null imagemagick-enabled-types) nil) |
60b5f187 | 736 | ((eq imagemagick-types-inhibit t) nil) |
60b5f187 GM |
737 | (t |
738 | (delq nil | |
739 | (mapcar | |
740 | (lambda (type) | |
741 | (unless (memq type imagemagick-types-inhibit) | |
95f520b5 | 742 | (if (eq imagemagick-enabled-types t) type |
43f5c7ae | 743 | (catch 'found |
95f520b5 | 744 | (dolist (enable imagemagick-enabled-types nil) |
43f5c7ae GM |
745 | (if (cond ((symbolp enable) (eq enable type)) |
746 | ((stringp enable) | |
747 | (string-match enable | |
748 | (symbol-name type)))) | |
749 | (throw 'found type))))))) | |
60b5f187 GM |
750 | (imagemagick-types))))))) |
751 | ||
c505aaeb CY |
752 | (defvar imagemagick--file-regexp nil |
753 | "File extension regexp for ImageMagick files, if any. | |
754 | This is the extension installed into `auto-mode-alist' and | |
755 | `image-type-file-name-regexps' by `imagemagick-register-types'.") | |
3de25479 JV |
756 | |
757 | ;;;###autoload | |
ce07fa9a | 758 | (defun imagemagick-register-types () |
d66c4c7c | 759 | "Register file types that can be handled by ImageMagick. |
c505aaeb | 760 | This function is called at startup, after loading the init file. |
60b5f187 | 761 | It registers the ImageMagick types returned by `imagemagick-filter-types'. |
5319014e CY |
762 | |
763 | Registered image types are added to `auto-mode-alist', so that | |
764 | Emacs visits them in Image mode. They are also added to | |
765 | `image-type-file-name-regexps', so that the `image-type' function | |
766 | recognizes these files as having image type `imagemagick'. | |
d66c4c7c | 767 | |
32d72c2f | 768 | If Emacs is compiled without ImageMagick support, this does nothing." |
d66c4c7c | 769 | (when (fboundp 'imagemagick-types) |
60b5f187 GM |
770 | (let* ((types (mapcar (lambda (type) (downcase (symbol-name type))) |
771 | (imagemagick-filter-types))) | |
772 | (re (if types (concat "\\." (regexp-opt types) "\\'"))) | |
32d72c2f GM |
773 | (ama-elt (car (member (cons imagemagick--file-regexp 'image-mode) |
774 | auto-mode-alist))) | |
775 | (itfnr-elt (car (member (cons imagemagick--file-regexp 'imagemagick) | |
776 | image-type-file-name-regexps)))) | |
777 | (if (not re) | |
778 | (setq auto-mode-alist (delete ama-elt auto-mode-alist) | |
779 | image-type-file-name-regexps | |
780 | (delete itfnr-elt image-type-file-name-regexps)) | |
781 | (if ama-elt | |
782 | (setcar ama-elt re) | |
783 | (push (cons re 'image-mode) auto-mode-alist)) | |
784 | (if itfnr-elt | |
785 | (setcar itfnr-elt re) | |
72834e10 CY |
786 | ;; Append to `image-type-file-name-regexps', so that we |
787 | ;; preferentially use specialized image libraries. | |
788 | (add-to-list 'image-type-file-name-regexps | |
789 | (cons re 'imagemagick) t))) | |
c505aaeb CY |
790 | (setq imagemagick--file-regexp re)))) |
791 | ||
041df390 CY |
792 | (defcustom imagemagick-types-inhibit |
793 | '(C HTML HTM INFO M TXT PDF) | |
95f520b5 | 794 | "List of ImageMagick types that should never be treated as images. |
c505aaeb | 795 | This should be a list of symbols, each of which should be one of |
95f520b5 | 796 | the ImageMagick types listed by `imagemagick-types'. The listed |
c505aaeb CY |
797 | image types are not registered by `imagemagick-register-types'. |
798 | ||
799 | If the value is t, inhibit the use of ImageMagick for images. | |
800 | ||
87eb79c2 GM |
801 | If you change this without using customize, you must call |
802 | `imagemagick-register-types' afterwards. | |
803 | ||
c505aaeb CY |
804 | If Emacs is compiled without ImageMagick support, this variable |
805 | has no effect." | |
806 | :type '(choice (const :tag "Support all ImageMagick types" nil) | |
807 | (const :tag "Disable all ImageMagick types" t) | |
808 | (repeat symbol)) | |
32d72c2f | 809 | :initialize 'custom-initialize-default |
c505aaeb CY |
810 | :set (lambda (symbol value) |
811 | (set-default symbol value) | |
812 | (imagemagick-register-types)) | |
2a1e2476 | 813 | :version "24.3" |
c505aaeb | 814 | :group 'image) |
3de25479 | 815 | |
95f520b5 | 816 | (defcustom imagemagick-enabled-types |
47b36b94 | 817 | '(3FR ART ARW AVS BMP BMP2 BMP3 CAL CALS CMYK CMYKA CR2 CRW |
041df390 CY |
818 | CUR CUT DCM DCR DCX DDS DJVU DNG DPX EXR FAX FITS GBR GIF |
819 | GIF87 GRB HRZ ICB ICO ICON J2C JNG JP2 JPC JPEG JPG JPX K25 | |
820 | KDC MIFF MNG MRW MSL MSVG MTV NEF ORF OTB PBM PCD PCDS PCL | |
821 | PCT PCX PDB PEF PGM PICT PIX PJPEG PNG PNG24 PNG32 PNG8 PNM | |
822 | PPM PSD PTIF PWP RAF RAS RBG RGB RGBA RGBO RLA RLE SCR SCT | |
823 | SFW SGI SR2 SRF SUN SVG SVGZ TGA TIFF TIFF64 TILE TIM TTF | |
824 | UYVY VDA VICAR VID VIFF VST WBMP WPG X3F XBM XC XCF XPM XV | |
825 | XWD YCbCr YCbCrA YUV) | |
32d72c2f | 826 | "List of ImageMagick types to treat as images. |
95f520b5 CY |
827 | Each list element should be a string or symbol, representing one |
828 | of the image types returned by `imagemagick-types'. If the | |
829 | element is a string, it is handled as a regexp that enables all | |
830 | matching types. | |
32d72c2f | 831 | |
95f520b5 CY |
832 | The value of `imagemagick-enabled-types' may also be t, meaning |
833 | to enable all types that ImageMagick supports. | |
32d72c2f GM |
834 | |
835 | The variable `imagemagick-types-inhibit' overrides this variable. | |
836 | ||
eda82a31 | 837 | If you change this without using customize, you must call |
32d72c2f GM |
838 | `imagemagick-register-types' afterwards. |
839 | ||
840 | If Emacs is compiled without ImageMagick support, this variable | |
841 | has no effect." | |
842 | :type '(choice (const :tag "Support all ImageMagick types" t) | |
843 | (const :tag "Disable all ImageMagick types" nil) | |
844 | (repeat :tag "List of types" | |
845 | (choice (symbol :tag "type") | |
846 | (regexp :tag "regexp")))) | |
847 | :initialize 'custom-initialize-default | |
848 | :set (lambda (symbol value) | |
849 | (set-default symbol value) | |
850 | (imagemagick-register-types)) | |
2a1e2476 | 851 | :version "24.3" |
32d72c2f GM |
852 | :group 'image) |
853 | ||
854 | (imagemagick-register-types) | |
855 | ||
7840ced1 GM |
856 | (provide 'image) |
857 | ||
e43367a0 | 858 | ;;; image.el ends here |