Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
3e5b7009 GM |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
4 | ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc. | |
634f4a67 PJ |
5 | |
6 | ;; Maintainer: FSF | |
04799cf5 | 7 | ;; Keywords: multimedia |
bd78fa1d | 8 | ;; Package: emacs |
7840ced1 GM |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
7840ced1 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
7840ced1 GM |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
7840ced1 GM |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;;; Code: | |
28 | ||
77f6105c MB |
29 | |
30 | (defgroup image () | |
31 | "Image support." | |
32 | :group 'multimedia) | |
33 | ||
110683ad | 34 | (defalias 'image-refresh 'image-flush) |
77f6105c | 35 | |
4fde92ef | 36 | (defconst image-type-header-regexps |
62564fc7 | 37 | `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) |
6642c904 | 38 | ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm) |
3205431d | 39 | ("\\`GIF8[79]a" . gif) |
6642c904 | 40 | ("\\`\x89PNG\r\n\x1a\n" . png) |
fa4354c1 AS |
41 | ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\ |
42 | #define \\1_height [0-9]+\n\\(\ | |
43 | #define \\1_x_hot [0-9]+\n\ | |
44 | #define \\1_y_hot [0-9]+\n\\)?\ | |
45 | static \\(unsigned \\)?char \\1_bits" . xbm) | |
6642c904 JB |
46 | ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff) |
47 | ("\\`[\t\n\r ]*%!PS" . postscript) | |
d30a05d1 | 48 | ("\\`\xff\xd8" . jpeg) ; used to be (image-jpeg-p . jpeg) |
62564fc7 JL |
49 | (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)") |
50 | (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)"))) | |
51 | (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<" | |
52 | comment-re "*" | |
53 | "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?" | |
54 | "[Ss][Vv][Gg]")) | |
55 | . svg) | |
56 | ) | |
6642c904 | 57 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. |
7840ced1 | 58 | When the first bytes of an image file match REGEXP, it is assumed to |
6642c904 JB |
59 | be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol, |
60 | IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called | |
61 | with one argument, a string containing the image data. If PREDICATE returns | |
62 | a non-nil value, TYPE is the image's type.") | |
8a8ef149 | 63 | |
4fde92ef KS |
64 | (defconst image-type-file-name-regexps |
65 | '(("\\.png\\'" . png) | |
66 | ("\\.gif\\'" . gif) | |
67 | ("\\.jpe?g\\'" . jpeg) | |
68 | ("\\.bmp\\'" . bmp) | |
69 | ("\\.xpm\\'" . xpm) | |
70 | ("\\.pbm\\'" . pbm) | |
71 | ("\\.xbm\\'" . xbm) | |
72 | ("\\.ps\\'" . postscript) | |
62564fc7 JL |
73 | ("\\.tiff?\\'" . tiff) |
74 | ("\\.svgz?\\'" . svg) | |
75 | ) | |
4fde92ef KS |
76 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files. |
77 | When the name of an image file match REGEXP, it is assumed to | |
78 | be of image type IMAGE-TYPE.") | |
79 | ||
a9bb04bb CY |
80 | ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead |
81 | ;; of content autodetection. Their contents are just C code, so it is | |
82 | ;; easy to generate false matches. | |
6642c904 JB |
83 | (defvar image-type-auto-detectable |
84 | '((pbm . t) | |
a9bb04bb | 85 | (xbm . nil) |
6642c904 JB |
86 | (bmp . maybe) |
87 | (gif . maybe) | |
88 | (png . maybe) | |
a9bb04bb | 89 | (xpm . nil) |
6642c904 JB |
90 | (jpeg . maybe) |
91 | (tiff . maybe) | |
62564fc7 | 92 | (svg . maybe) |
6642c904 JB |
93 | (postscript . nil)) |
94 | "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files. | |
95 | \(See `image-type-auto-detected-p'). | |
96 | ||
97 | AUTODETECT can be | |
98 | - t always auto-detect. | |
99 | - nil never auto-detect. | |
100 | - maybe auto-detect only if the image type is available | |
101 | (see `image-type-available-p').") | |
4fde92ef | 102 | |
45448e64 SM |
103 | (defcustom image-load-path |
104 | (list (file-name-as-directory (expand-file-name "images" data-directory)) | |
105 | 'data-directory 'load-path) | |
5fc5ac38 | 106 | "List of locations in which to search for image files. |
93a75651 CY |
107 | If an element is a string, it defines a directory to search. |
108 | If an element is a variable symbol whose value is a string, that | |
109 | value defines a directory to search. | |
110 | If an element is a variable symbol whose value is a list, the | |
45448e64 SM |
111 | value is used as a list of directories to search." |
112 | :type '(repeat (choice directory variable)) | |
113 | :initialize 'custom-initialize-delay) | |
dada660a | 114 | |
5248a565 | 115 | |
20c65d08 | 116 | (defun image-load-path-for-library (library image &optional path no-error) |
ae77c7ff | 117 | "Return a suitable search path for images used by LIBRARY. |
7c565097 | 118 | |
40db64d2 | 119 | It searches for IMAGE in `image-load-path' (excluding |
c0696e1b BW |
120 | \"`data-directory'/images\") and `load-path', followed by a path |
121 | suitable for LIBRARY, which includes \"../../etc/images\" and | |
122 | \"../etc/images\" relative to the library file itself, and then | |
123 | in \"`data-directory'/images\". | |
7c565097 | 124 | |
5248a565 BW |
125 | Then this function returns a list of directories which contains |
126 | first the directory in which IMAGE was found, followed by the | |
68ba6c49 | 127 | value of `load-path'. If PATH is given, it is used instead of |
5248a565 | 128 | `load-path'. |
20c65d08 | 129 | |
5248a565 | 130 | If NO-ERROR is non-nil and a suitable path can't be found, don't |
68ba6c49 | 131 | signal an error. Instead, return a list of directories as before, |
5248a565 | 132 | except that nil appears in place of the image directory. |
7c565097 BW |
133 | |
134 | Here is an example that uses a common idiom to provide | |
135 | compatibility with versions of Emacs that lack the variable | |
136 | `image-load-path': | |
137 | ||
9f036d33 BW |
138 | ;; Shush compiler. |
139 | (defvar image-load-path) | |
5248a565 BW |
140 | |
141 | (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\")) | |
9f036d33 BW |
142 | (image-load-path (cons (car load-path) |
143 | (when (boundp 'image-load-path) | |
144 | image-load-path)))) | |
5248a565 | 145 | (mh-tool-bar-folder-buttons-init))" |
7c565097 BW |
146 | (unless library (error "No library specified")) |
147 | (unless image (error "No image specified")) | |
c0696e1b BW |
148 | (let (image-directory image-directory-load-path) |
149 | ;; Check for images in image-load-path or load-path. | |
150 | (let ((img image) | |
151 | (dir (or | |
152 | ;; Images in image-load-path. | |
153 | (image-search-load-path image) | |
154 | ;; Images in load-path. | |
155 | (locate-library image))) | |
156 | parent) | |
157 | ;; Since the image might be in a nested directory (for | |
158 | ;; example, mail/attach.pbm), adjust `image-directory' | |
159 | ;; accordingly. | |
160 | (when dir | |
161 | (setq dir (file-name-directory dir)) | |
162 | (while (setq parent (file-name-directory img)) | |
163 | (setq img (directory-file-name parent) | |
164 | dir (expand-file-name "../" dir)))) | |
165 | (setq image-directory-load-path dir)) | |
166 | ||
167 | ;; If `image-directory-load-path' isn't Emacs' image directory, | |
168 | ;; it's probably a user preference, so use it. Then use a | |
169 | ;; relative setting if possible; otherwise, use | |
170 | ;; `image-directory-load-path'. | |
7c565097 | 171 | (cond |
c0696e1b BW |
172 | ;; User-modified image-load-path? |
173 | ((and image-directory-load-path | |
174 | (not (equal image-directory-load-path | |
175 | (file-name-as-directory | |
176 | (expand-file-name "images" data-directory))))) | |
177 | (setq image-directory image-directory-load-path)) | |
7c565097 BW |
178 | ;; Try relative setting. |
179 | ((let (library-name d1ei d2ei) | |
180 | ;; First, find library in the load-path. | |
181 | (setq library-name (locate-library library)) | |
182 | (if (not library-name) | |
183 | (error "Cannot find library %s in load-path" library)) | |
184 | ;; And then set image-directory relative to that. | |
185 | (setq | |
186 | ;; Go down 2 levels. | |
c0696e1b BW |
187 | d2ei (file-name-as-directory |
188 | (expand-file-name | |
189 | (concat (file-name-directory library-name) "../../etc/images"))) | |
7c565097 | 190 | ;; Go down 1 level. |
c0696e1b BW |
191 | d1ei (file-name-as-directory |
192 | (expand-file-name | |
193 | (concat (file-name-directory library-name) "../etc/images")))) | |
7c565097 BW |
194 | (setq image-directory |
195 | ;; Set it to nil if image is not found. | |
196 | (cond ((file-exists-p (expand-file-name image d2ei)) d2ei) | |
197 | ((file-exists-p (expand-file-name image d1ei)) d1ei))))) | |
c0696e1b BW |
198 | ;; Use Emacs' image directory. |
199 | (image-directory-load-path | |
200 | (setq image-directory image-directory-load-path)) | |
20c65d08 | 201 | (no-error |
20c65d08 | 202 | (message "Could not find image %s for library %s" image library)) |
7c565097 BW |
203 | (t |
204 | (error "Could not find image %s for library %s" image library))) | |
205 | ||
5248a565 BW |
206 | ;; Return an augmented `path' or `load-path'. |
207 | (nconc (list image-directory) | |
208 | (delete image-directory (copy-sequence (or path load-path)))))) | |
209 | ||
7c565097 | 210 | |
8a8ef149 | 211 | (defun image-jpeg-p (data) |
9b78a6a3 RS |
212 | "Value is non-nil if DATA, a string, consists of JFIF image data. |
213 | We accept the tag Exif because that is the same format." | |
24c23999 JB |
214 | (setq data (ignore-errors (string-to-unibyte data))) |
215 | (when (and data (string-match-p "\\`\xff\xd8" data)) | |
8a8ef149 GM |
216 | (catch 'jfif |
217 | (let ((len (length data)) (i 2)) | |
218 | (while (< i len) | |
219 | (when (/= (aref data i) #xff) | |
220 | (throw 'jfif nil)) | |
221 | (setq i (1+ i)) | |
222 | (when (>= (+ i 2) len) | |
223 | (throw 'jfif nil)) | |
224 | (let ((nbytes (+ (lsh (aref data (+ i 1)) 8) | |
4a9bf8a4 GM |
225 | (aref data (+ i 2)))) |
226 | (code (aref data i))) | |
227 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 228 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 229 | (throw 'jfif |
24c23999 JB |
230 | (string-match-p "JFIF\\|Exif" |
231 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 232 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
233 | |
234 | ||
235 | ;;;###autoload | |
162dec01 GM |
236 | (defun image-type-from-data (data) |
237 | "Determine the image type from image data DATA. | |
238 | Value is a symbol specifying the image type or nil if type cannot | |
7840ced1 | 239 | be determined." |
4fde92ef | 240 | (let ((types image-type-header-regexps) |
7840ced1 | 241 | type) |
4fde92ef | 242 | (while types |
7840ced1 | 243 | (let ((regexp (car (car types))) |
6642c904 | 244 | (image-type (cdr (car types)))) |
4fde92ef | 245 | (if (or (and (symbolp image-type) |
24c23999 | 246 | (string-match-p regexp data)) |
4fde92ef KS |
247 | (and (consp image-type) |
248 | (funcall (car image-type) data) | |
249 | (setq image-type (cdr image-type)))) | |
250 | (setq type image-type | |
251 | types nil) | |
252 | (setq types (cdr types))))) | |
253 | type)) | |
254 | ||
255 | ||
256 | ;;;###autoload | |
6642c904 | 257 | (defun image-type-from-buffer () |
4fde92ef | 258 | "Determine the image type from data in the current buffer. |
6642c904 JB |
259 | Value is a symbol specifying the image type or nil if type cannot |
260 | be determined." | |
4fde92ef KS |
261 | (let ((types image-type-header-regexps) |
262 | type | |
263 | (opoint (point))) | |
264 | (goto-char (point-min)) | |
265 | (while types | |
266 | (let ((regexp (car (car types))) | |
6642c904 | 267 | (image-type (cdr (car types))) |
4fde92ef KS |
268 | data) |
269 | (if (or (and (symbolp image-type) | |
24c23999 | 270 | (looking-at-p regexp)) |
4fde92ef KS |
271 | (and (consp image-type) |
272 | (funcall (car image-type) | |
273 | (or data | |
274 | (setq data | |
275 | (buffer-substring | |
276 | (point-min) | |
277 | (min (point-max) | |
278 | (+ (point-min) 256)))))) | |
279 | (setq image-type (cdr image-type)))) | |
6642c904 | 280 | (setq type image-type |
4fde92ef KS |
281 | types nil) |
282 | (setq types (cdr types))))) | |
283 | (goto-char opoint) | |
7840ced1 GM |
284 | type)) |
285 | ||
286 | ||
162dec01 GM |
287 | ;;;###autoload |
288 | (defun image-type-from-file-header (file) | |
289 | "Determine the type of image file FILE from its first few bytes. | |
290 | Value is a symbol specifying the image type, or nil if type cannot | |
291 | be determined." | |
4fde92ef KS |
292 | (unless (or (file-readable-p file) |
293 | (file-name-absolute-p file)) | |
294 | (setq file (image-search-load-path file))) | |
295 | (and file | |
296 | (file-readable-p file) | |
297 | (with-temp-buffer | |
298 | (set-buffer-multibyte nil) | |
299 | (insert-file-contents-literally file nil 0 256) | |
6642c904 | 300 | (image-type-from-buffer)))) |
4fde92ef KS |
301 | |
302 | ||
303 | ;;;###autoload | |
304 | (defun image-type-from-file-name (file) | |
305 | "Determine the type of image file FILE from its name. | |
306 | Value is a symbol specifying the image type, or nil if type cannot | |
307 | be determined." | |
24c23999 | 308 | (assoc-default file image-type-file-name-regexps 'string-match-p)) |
162dec01 GM |
309 | |
310 | ||
7840ced1 | 311 | ;;;###autoload |
7d88e015 | 312 | (defun image-type (source &optional type data-p) |
e7968373 | 313 | "Determine and return image type. |
7d88e015 | 314 | SOURCE is an image file name or image data. |
7840ced1 | 315 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 | 316 | or nil, try to determine the image type from its first few bytes |
7d88e015 | 317 | of image data. If that doesn't work, and SOURCE is a file name, |
9ea8de1b | 318 | use its file extension as image type. |
7d88e015 CY |
319 | Optional DATA-P non-nil means SOURCE is a string containing image data." |
320 | (when (and (not data-p) (not (stringp source))) | |
321 | (error "Invalid image file name `%s'" source)) | |
162dec01 | 322 | (unless type |
7d88e015 CY |
323 | (setq type (if data-p |
324 | (image-type-from-data source) | |
325 | (or (image-type-from-file-header source) | |
326 | (image-type-from-file-name source)))) | |
327 | (or type (error "Cannot determine image type"))) | |
28681e35 | 328 | (or (memq type (and (boundp 'image-types) image-types)) |
7d88e015 | 329 | (error "Invalid image type `%s'" type)) |
e7968373 KS |
330 | type) |
331 | ||
6642c904 | 332 | |
aa360da1 GM |
333 | (defvar image-library-alist) |
334 | ||
e7968373 KS |
335 | ;;;###autoload |
336 | (defun image-type-available-p (type) | |
337 | "Return non-nil if image type TYPE is available. | |
338 | Image types are symbols like `xbm' or `jpeg'." | |
339 | (and (fboundp 'init-image-library) | |
340 | (init-image-library type image-library-alist))) | |
341 | ||
342 | ||
6642c904 JB |
343 | ;;;###autoload |
344 | (defun image-type-auto-detected-p () | |
4837b516 | 345 | "Return t if the current buffer contains an auto-detectable image. |
7d88e015 CY |
346 | This function is intended to be used from `magic-fallback-mode-alist'. |
347 | ||
348 | The buffer is considered to contain an auto-detectable image if | |
349 | its beginning matches an image type in `image-type-header-regexps', | |
1e83e2ca GM |
350 | and that image type is present in `image-type-auto-detectable' with a |
351 | non-nil value. If that value is non-nil, but not t, then the image type | |
352 | must be available." | |
6642c904 JB |
353 | (let* ((type (image-type-from-buffer)) |
354 | (auto (and type (cdr (assq type image-type-auto-detectable))))) | |
d567be22 | 355 | (and auto |
7d88e015 | 356 | (or (eq auto t) (image-type-available-p type))))) |
6642c904 JB |
357 | |
358 | ||
e7968373 KS |
359 | ;;;###autoload |
360 | (defun create-image (file-or-data &optional type data-p &rest props) | |
361 | "Create an image. | |
362 | FILE-OR-DATA is an image file name or image data. | |
363 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | |
364 | or nil, try to determine the image type from its first few bytes | |
365 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
366 | use its file extension as image type. | |
367 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. | |
368 | Optional PROPS are additional image attributes to assign to the image, | |
369 | like, e.g. `:mask MASK'. | |
370 | Value is the image created, or nil if images of type TYPE are not supported. | |
371 | ||
2467875c GM |
372 | Images should not be larger than specified by `max-image-size'. |
373 | ||
374 | Image file names that are not absolute are searched for in the | |
375 | \"images\" sub-directory of `data-directory' and | |
376 | `x-bitmap-file-path' (in that order)." | |
377 | ;; It is x_find_image_file in image.c that sets the search path. | |
e7968373 | 378 | (setq type (image-type file-or-data type data-p)) |
7840ced1 | 379 | (when (image-type-available-p type) |
162dec01 GM |
380 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
381 | props))) | |
7840ced1 GM |
382 | |
383 | ||
384 | ;;;###autoload | |
cedbd8d0 | 385 | (defun put-image (image pos &optional string area) |
1571b5ff | 386 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 387 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
388 | IMAGE is displayed by putting an overlay into the current buffer with a |
389 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 390 | image. STRING is defaulted if you omit it. |
7840ced1 | 391 | POS may be an integer or marker. |
7840ced1 GM |
392 | AREA is where to display the image. AREA nil or omitted means |
393 | display it in the text area, a value of `left-margin' means | |
394 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 395 | means display it in the right marginal area." |
0ad550ba | 396 | (unless string (setq string "x")) |
1571b5ff | 397 | (let ((buffer (current-buffer))) |
cedbd8d0 | 398 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
399 | (error "Not an image: %s" image)) |
400 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
401 | (error "Invalid area %s" area)) | |
46655c94 | 402 | (setq string (copy-sequence string)) |
1571b5ff | 403 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
404 | (prop (if (null area) image (list (list 'margin area) image)))) |
405 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff GM |
406 | (overlay-put overlay 'put-image t) |
407 | (overlay-put overlay 'before-string string)))) | |
7840ced1 GM |
408 | |
409 | ||
410 | ;;;###autoload | |
5af275e0 | 411 | (defun insert-image (image &optional string area slice) |
7840ced1 | 412 | "Insert IMAGE into current buffer at point. |
46655c94 | 413 | IMAGE is displayed by inserting STRING into the current buffer |
cedbd8d0 DL |
414 | with a `display' property whose value is the image. STRING is |
415 | defaulted if you omit it. | |
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' | |
5af275e0 KS |
419 | means display it in the right marginal area. |
420 | SLICE specifies slice of IMAGE to insert. SLICE nil or omitted | |
421 | means insert whole image. SLICE is a list (X Y WIDTH HEIGHT) | |
422 | specifying the X and Y positions and WIDTH and HEIGHT of image area | |
423 | to insert. A float value 0.0 - 1.0 means relative to the width or | |
424 | height of the image; integer values are taken as pixel values." | |
0ad550ba DL |
425 | ;; Use a space as least likely to cause trouble when it's a hidden |
426 | ;; character in the buffer. | |
427 | (unless string (setq string " ")) | |
cedbd8d0 | 428 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
429 | (error "Not an image: %s" image)) |
430 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
431 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
432 | (if area |
433 | (setq image (list (list 'margin area) image)) | |
434 | ;; Cons up a new spec equal but not eq to `image' so that | |
435 | ;; inserting it twice in a row (adjacently) displays two copies of | |
436 | ;; the image. Don't try to avoid this by looking at the display | |
437 | ;; properties on either side so that we DTRT more often with | |
438 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
439 | ;; of it loses anyway.) | |
440 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
441 | (let ((start (point))) |
442 | (insert string) | |
443 | (add-text-properties start (point) | |
5af275e0 KS |
444 | `(display ,(if slice |
445 | (list (cons 'slice slice) image) | |
446 | image) rear-nonsticky (display))))) | |
447 | ||
448 | ||
f22c36d3 | 449 | ;;;###autoload |
5af275e0 | 450 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
451 | "Insert IMAGE into current buffer at point. |
452 | IMAGE is displayed by inserting STRING into the current buffer | |
453 | with a `display' property whose value is the image. STRING is | |
454 | defaulted if you omit it. | |
455 | AREA is where to display the image. AREA nil or omitted means | |
456 | display it in the text area, a value of `left-margin' means | |
457 | display it in the left marginal area, a value of `right-margin' | |
458 | means display it in the right marginal area. | |
68ba6c49 | 459 | The image is automatically split into ROWS x COLS slices." |
5af275e0 KS |
460 | (unless string (setq string " ")) |
461 | (unless (eq (car-safe image) 'image) | |
462 | (error "Not an image: %s" image)) | |
463 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
464 | (error "Invalid area %s" area)) | |
465 | (if area | |
466 | (setq image (list (list 'margin area) image)) | |
467 | ;; Cons up a new spec equal but not eq to `image' so that | |
468 | ;; inserting it twice in a row (adjacently) displays two copies of | |
469 | ;; the image. Don't try to avoid this by looking at the display | |
470 | ;; properties on either side so that we DTRT more often with | |
471 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
472 | ;; of it loses anyway.) | |
473 | (setq image (cons 'image (cdr image)))) | |
474 | (let ((x 0.0) (dx (/ 1.0001 (or cols 1))) | |
475 | (y 0.0) (dy (/ 1.0001 (or rows 1)))) | |
476 | (while (< y 1.0) | |
477 | (while (< x 1.0) | |
478 | (let ((start (point))) | |
479 | (insert string) | |
480 | (add-text-properties start (point) | |
481 | `(display ,(list (list 'slice x y dx dy) image) | |
482 | rear-nonsticky (display))) | |
483 | (setq x (+ x dx)))) | |
484 | (setq x 0.0 | |
485 | y (+ y dy)) | |
82dc617b | 486 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 487 | |
10e2102e | 488 | |
7840ced1 GM |
489 | |
490 | ;;;###autoload | |
491 | (defun remove-images (start end &optional buffer) | |
492 | "Remove images between START and END in BUFFER. | |
493 | Remove only images that were put in BUFFER with calls to `put-image'. | |
494 | BUFFER nil or omitted means use the current buffer." | |
495 | (unless buffer | |
496 | (setq buffer (current-buffer))) | |
497 | (let ((overlays (overlays-in start end))) | |
498 | (while overlays | |
499 | (let ((overlay (car overlays))) | |
500 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
501 | (delete-overlay overlay))) |
502 | (setq overlays (cdr overlays))))) | |
7840ced1 | 503 | |
4fde92ef KS |
504 | (defun image-search-load-path (file &optional path) |
505 | (unless path | |
506 | (setq path image-load-path)) | |
507 | (let (element found filename) | |
5fc5ac38 | 508 | (while (and (not found) (consp path)) |
93a75651 | 509 | (setq element (car path)) |
5fc5ac38 | 510 | (cond |
93a75651 | 511 | ((stringp element) |
5fc5ac38 CY |
512 | (setq found |
513 | (file-readable-p | |
4fde92ef | 514 | (setq filename (expand-file-name file element))))) |
93a75651 CY |
515 | ((and (symbolp element) (boundp element)) |
516 | (setq element (symbol-value element)) | |
517 | (cond | |
518 | ((stringp element) | |
519 | (setq found | |
520 | (file-readable-p | |
4fde92ef | 521 | (setq filename (expand-file-name file element))))) |
93a75651 | 522 | ((consp element) |
4fde92ef | 523 | (if (setq filename (image-search-load-path file element)) |
93a75651 | 524 | (setq found t)))))) |
5fc5ac38 | 525 | (setq path (cdr path))) |
4fde92ef | 526 | (if found filename))) |
7840ced1 GM |
527 | |
528 | ;;;###autoload | |
349c034d GM |
529 | (defun find-image (specs) |
530 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 531 | |
cedbd8d0 | 532 | SPECS is a list of image specifications. |
7840ced1 GM |
533 | |
534 | Each image specification in SPECS is a property list. The contents of | |
535 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
536 | least contain the properties `:type TYPE' and either `:file FILE' or |
537 | `:data DATA', where TYPE is a symbol specifying the image type, | |
538 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
cedbd8d0 DL |
539 | string containing the actual image data. The specification whose TYPE |
540 | is supported, and FILE exists, is used to construct the image | |
541 | specification to be returned. Return nil if no specification is | |
542 | satisfied. | |
543 | ||
8646a62e CY |
544 | The image is looked for in `image-load-path'. |
545 | ||
546 | Image files should not be larger than specified by `max-image-size'." | |
7840ced1 GM |
547 | (let (image) |
548 | (while (and specs (null image)) | |
549 | (let* ((spec (car specs)) | |
550 | (type (plist-get spec :type)) | |
162dec01 | 551 | (data (plist-get spec :data)) |
7f228379 GM |
552 | (file (plist-get spec :file)) |
553 | found) | |
162dec01 GM |
554 | (when (image-type-available-p type) |
555 | (cond ((stringp file) | |
4fde92ef | 556 | (if (setq found (image-search-load-path file)) |
5fc5ac38 CY |
557 | (setq image |
558 | (cons 'image (plist-put (copy-sequence spec) | |
559 | :file found))))) | |
f64ae86c | 560 | ((not (null data)) |
162dec01 GM |
561 | (setq image (cons 'image spec))))) |
562 | (setq specs (cdr specs)))) | |
349c034d GM |
563 | image)) |
564 | ||
565 | ||
566 | ;;;###autoload | |
567 | (defmacro defimage (symbol specs &optional doc) | |
568 | "Define SYMBOL as an image. | |
569 | ||
570 | SPECS is a list of image specifications. DOC is an optional | |
571 | documentation string. | |
572 | ||
573 | Each image specification in SPECS is a property list. The contents of | |
574 | a specification are image type dependent. All specifications must at | |
575 | least contain the properties `:type TYPE' and either `:file FILE' or | |
576 | `:data DATA', where TYPE is a symbol specifying the image type, | |
577 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
578 | string containing the actual image data. The first image | |
579 | specification whose TYPE is supported, and FILE exists, is used to | |
580 | define SYMBOL. | |
581 | ||
582 | Example: | |
583 | ||
584 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
585 | (:type xbm :file \"~/test1.xbm\")))" | |
fd8e7a80 | 586 | (declare (doc-string 3)) |
349c034d | 587 | `(defvar ,symbol (find-image ',specs) ,doc)) |
7840ced1 | 588 | |
0608aa45 KS |
589 | \f |
590 | ;;; Animated image API | |
7840ced1 | 591 | |
0608aa45 KS |
592 | (defcustom image-animate-max-time 30 |
593 | "Time in seconds to animate images." | |
594 | :type 'integer | |
3e5b7009 | 595 | :version "24.1" |
0608aa45 KS |
596 | :group 'image) |
597 | ||
598 | (defconst image-animated-types '(gif) | |
599 | "List of supported animated image types.") | |
600 | ||
601 | ;;;###autoload | |
602 | (defun create-animated-image (file-or-data &optional type data-p &rest props) | |
603 | "Create an animated image. | |
604 | FILE-OR-DATA is an image file name or image data. | |
605 | Optional TYPE is a symbol describing the image type. If TYPE is omitted | |
606 | or nil, try to determine the image type from its first few bytes | |
607 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
608 | use its file extension as image type. | |
609 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. | |
610 | Optional PROPS are additional image attributes to assign to the image, | |
611 | like, e.g. `:mask MASK'. | |
612 | Value is the image created, or nil if images of type TYPE are not supported. | |
613 | ||
614 | Images should not be larger than specified by `max-image-size'." | |
615 | (setq type (image-type file-or-data type data-p)) | |
616 | (when (image-type-available-p type) | |
617 | (let* ((animate (memq type image-animated-types)) | |
618 | (image | |
619 | (append (list 'image :type type (if data-p :data :file) file-or-data) | |
1075cd61 | 620 | (if animate '(:index 0)) |
0608aa45 KS |
621 | props))) |
622 | (if animate | |
623 | (image-animate-start image)) | |
624 | image))) | |
625 | ||
626 | (defun image-animate-timer (image) | |
627 | "Return the animation timer for image IMAGE." | |
628 | ;; See cancel-function-timers | |
629 | (let ((tail timer-list) timer) | |
630 | (while tail | |
631 | (setq timer (car tail) | |
632 | tail (cdr tail)) | |
633 | (if (and (eq (aref timer 5) #'image-animate-timeout) | |
634 | (consp (aref timer 6)) | |
635 | (eq (car (aref timer 6)) image)) | |
636 | (setq tail nil) | |
637 | (setq timer nil))) | |
638 | timer)) | |
639 | ||
640 | (defun image-animate-start (image &optional max-time) | |
641 | "Start animation of image IMAGE. | |
642 | Optional second arg MAX-TIME is number of seconds to animate image, | |
643 | or t to animate infinitely." | |
644 | (let ((anim (image-animated-p image)) | |
645 | timer tmo) | |
646 | (when anim | |
647 | (if (setq timer (image-animate-timer image)) | |
648 | (setcar (nthcdr 3 (aref timer 6)) max-time) | |
649 | (setq tmo (* (cdr anim) 0.01)) | |
650 | (setq max-time (or max-time image-animate-max-time)) | |
651 | (run-with-timer tmo nil #'image-animate-timeout | |
652 | image 1 (car anim) | |
653 | (if (numberp max-time) | |
654 | (- max-time tmo) | |
655 | max-time)))))) | |
656 | ||
657 | (defun image-animate-stop (image) | |
658 | "Stop animation of image." | |
659 | (let ((timer (image-animate-timer image))) | |
660 | (when timer | |
661 | (cancel-timer timer)))) | |
662 | ||
663 | (defun image-animate-timeout (image ino count time-left) | |
664 | (if (>= ino count) | |
665 | (setq ino 0)) | |
666 | (plist-put (cdr image) :index ino) | |
667 | (force-window-update) | |
668 | (let ((anim (image-animated-p image)) tmo) | |
669 | (when anim | |
670 | (setq tmo (* (cdr anim) 0.01)) | |
671 | (unless (and (= ino 0) (numberp time-left) (< time-left tmo)) | |
672 | (run-with-timer tmo nil #'image-animate-timeout | |
673 | image (1+ ino) count | |
674 | (if (numberp time-left) | |
675 | (- time-left tmo) | |
676 | time-left)))))) | |
677 | ||
678 | (defun image-animated-p (image) | |
679 | "Return non-nil if image is animated. | |
680 | Actually, return value is a cons (IMAGES . DELAY) where IMAGES | |
681 | is the number of sub-images in the animated image, and DELAY | |
682 | is the delay in 100ths of a second until the next sub-image | |
683 | shall be displayed." | |
684 | (cond | |
685 | ((eq (plist-get (cdr image) :type) 'gif) | |
1546c559 JL |
686 | (let* ((metadata (image-metadata image)) |
687 | (images (plist-get metadata 'count)) | |
688 | (extdata (plist-get metadata 'extension-data)) | |
fc937924 JL |
689 | (anim (plist-get extdata #xF9)) |
690 | (tmo (and (integerp images) (> images 1) | |
691 | (stringp anim) (>= (length anim) 4) | |
692 | (+ (aref anim 1) (* (aref anim 2) 256))))) | |
693 | (when tmo | |
694 | (if (eq tmo 0) (setq tmo 10)) | |
695 | (cons images tmo)))))) | |
0608aa45 KS |
696 | |
697 | \f | |
d2a2d277 JV |
698 | (defcustom imagemagick-types-inhibit |
699 | '(C HTML HTM TXT PDF) | |
ce07fa9a | 700 | "Types the imagemagick loader should not try to handle.") |
3de25479 JV |
701 | |
702 | ;;;###autoload | |
ce07fa9a | 703 | (defun imagemagick-register-types () |
3de25479 | 704 | "Register file types that imagemagick is able to handle." |
69fd8038 | 705 | (let ((im-types (imagemagick-types))) |
4917006b | 706 | (dolist (im-inhibit imagemagick-types-inhibit) |
69fd8038 | 707 | (setq im-types (remove im-inhibit im-types))) |
4917006b | 708 | (dolist (im-type im-types) |
69fd8038 | 709 | (let ((extension (downcase (symbol-name im-type)))) |
3de25479 JV |
710 | (push |
711 | (cons (concat "\\." extension "\\'") 'image-mode) | |
712 | auto-mode-alist) | |
713 | (push | |
714 | (cons (concat "\\." extension "\\'") 'imagemagick) | |
69fd8038 | 715 | image-type-file-name-regexps))))) |
3de25479 JV |
716 | |
717 | ||
718 | ||
7840ced1 GM |
719 | (provide 'image) |
720 | ||
fd8e7a80 | 721 | ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3 |
e43367a0 | 722 | ;;; image.el ends here |