Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
2b8f2f46 | 3 | ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
634f4a67 PJ |
4 | |
5 | ;; Maintainer: FSF | |
04799cf5 | 6 | ;; Keywords: multimedia |
7840ced1 GM |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;;; Code: | |
28 | ||
77f6105c MB |
29 | |
30 | (defgroup image () | |
31 | "Image support." | |
32 | :group 'multimedia) | |
33 | ||
34 | ||
7840ced1 | 35 | (defconst image-type-regexps |
3bdbead3 | 36 | '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) |
69ebef1d GM |
37 | ("\\`P[1-6]" . pbm) |
38 | ("\\`GIF8" . gif) | |
69ebef1d | 39 | ("\\`\211PNG\r\n" . png) |
3bdbead3 | 40 | ("\\`[\t\n\r ]*#define" . xbm) |
6ea3db8a | 41 | ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff) |
3bdbead3 | 42 | ("\\`[\t\n\r ]*%!PS" . postscript) |
8a8ef149 | 43 | ("\\`\xff\xd8" . (image-jpeg-p . jpeg))) |
7840ced1 GM |
44 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. |
45 | When the first bytes of an image file match REGEXP, it is assumed to | |
8a8ef149 GM |
46 | be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol, |
47 | IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called | |
48 | with one argument, a string containing the image data. If PREDICATE returns | |
37d4a06e | 49 | a non-nil value, TYPE is the image's type.") |
8a8ef149 GM |
50 | |
51 | ||
52 | (defun image-jpeg-p (data) | |
9b78a6a3 RS |
53 | "Value is non-nil if DATA, a string, consists of JFIF image data. |
54 | We accept the tag Exif because that is the same format." | |
8a8ef149 GM |
55 | (when (string-match "\\`\xff\xd8" data) |
56 | (catch 'jfif | |
57 | (let ((len (length data)) (i 2)) | |
58 | (while (< i len) | |
59 | (when (/= (aref data i) #xff) | |
60 | (throw 'jfif nil)) | |
61 | (setq i (1+ i)) | |
62 | (when (>= (+ i 2) len) | |
63 | (throw 'jfif nil)) | |
64 | (let ((nbytes (+ (lsh (aref data (+ i 1)) 8) | |
4a9bf8a4 GM |
65 | (aref data (+ i 2)))) |
66 | (code (aref data i))) | |
67 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 68 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 69 | (throw 'jfif |
32b3c766 JPW |
70 | (string-match "JFIF\\|Exif" |
71 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 72 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
73 | |
74 | ||
75 | ;;;###autoload | |
162dec01 GM |
76 | (defun image-type-from-data (data) |
77 | "Determine the image type from image data DATA. | |
78 | Value is a symbol specifying the image type or nil if type cannot | |
7840ced1 | 79 | be determined." |
162dec01 | 80 | (let ((types image-type-regexps) |
7840ced1 GM |
81 | type) |
82 | (while (and types (null type)) | |
83 | (let ((regexp (car (car types))) | |
84 | (image-type (cdr (car types)))) | |
8a8ef149 GM |
85 | (when (or (and (symbolp image-type) |
86 | (string-match regexp data)) | |
87 | (and (consp image-type) | |
88 | (funcall (car image-type) data) | |
89 | (setq image-type (cdr image-type)))) | |
7840ced1 GM |
90 | (setq type image-type)) |
91 | (setq types (cdr types)))) | |
92 | type)) | |
93 | ||
94 | ||
162dec01 GM |
95 | ;;;###autoload |
96 | (defun image-type-from-file-header (file) | |
97 | "Determine the type of image file FILE from its first few bytes. | |
98 | Value is a symbol specifying the image type, or nil if type cannot | |
99 | be determined." | |
100 | (unless (file-name-directory file) | |
101 | (setq file (expand-file-name file data-directory))) | |
102 | (setq file (expand-file-name file)) | |
103 | (let ((header (with-temp-buffer | |
ee485a83 | 104 | (set-buffer-multibyte nil) |
162dec01 GM |
105 | (insert-file-contents-literally file nil 0 256) |
106 | (buffer-string)))) | |
107 | (image-type-from-data header))) | |
108 | ||
109 | ||
7840ced1 GM |
110 | ;;;###autoload |
111 | (defun image-type-available-p (type) | |
112 | "Value is non-nil if image type TYPE is available. | |
113 | Image types are symbols like `xbm' or `jpeg'." | |
24978190 | 114 | (and (boundp 'image-types) (not (null (memq type image-types))))) |
7840ced1 GM |
115 | |
116 | ||
117 | ;;;###autoload | |
162dec01 GM |
118 | (defun create-image (file-or-data &optional type data-p &rest props) |
119 | "Create an image. | |
120 | FILE-OR-DATA is an image file name or image data. | |
7840ced1 | 121 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 GM |
122 | or nil, try to determine the image type from its first few bytes |
123 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
9ea8de1b | 124 | use its file extension as image type. |
162dec01 | 125 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. |
7840ced1 | 126 | Optional PROPS are additional image attributes to assign to the image, |
d1d3c685 | 127 | like, e.g. `:mask MASK'. |
7840ced1 | 128 | Value is the image created, or nil if images of type TYPE are not supported." |
f64ae86c GM |
129 | (when (and (not data-p) (not (stringp file-or-data))) |
130 | (error "Invalid image file name `%s'" file-or-data)) | |
162dec01 GM |
131 | (cond ((null data-p) |
132 | ;; FILE-OR-DATA is a file name. | |
133 | (unless (or type | |
134 | (setq type (image-type-from-file-header file-or-data))) | |
135 | (let ((extension (file-name-extension file-or-data))) | |
136 | (unless extension | |
137 | (error "Cannot determine image type")) | |
138 | (setq type (intern extension))))) | |
139 | (t | |
140 | ;; FILE-OR-DATA contains image data. | |
141 | (unless type | |
142 | (setq type (image-type-from-data file-or-data))))) | |
143 | (unless type | |
144 | (error "Cannot determine image type")) | |
7840ced1 | 145 | (unless (symbolp type) |
162dec01 | 146 | (error "Invalid image type `%s'" type)) |
7840ced1 | 147 | (when (image-type-available-p type) |
162dec01 GM |
148 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
149 | props))) | |
7840ced1 GM |
150 | |
151 | ||
152 | ;;;###autoload | |
cedbd8d0 | 153 | (defun put-image (image pos &optional string area) |
1571b5ff | 154 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 155 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
156 | IMAGE is displayed by putting an overlay into the current buffer with a |
157 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 158 | image. STRING is defaulted if you omit it. |
7840ced1 | 159 | POS may be an integer or marker. |
7840ced1 GM |
160 | AREA is where to display the image. AREA nil or omitted means |
161 | display it in the text area, a value of `left-margin' means | |
162 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 163 | means display it in the right marginal area." |
0ad550ba | 164 | (unless string (setq string "x")) |
1571b5ff | 165 | (let ((buffer (current-buffer))) |
cedbd8d0 | 166 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
167 | (error "Not an image: %s" image)) |
168 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
169 | (error "Invalid area %s" area)) | |
46655c94 | 170 | (setq string (copy-sequence string)) |
1571b5ff | 171 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
172 | (prop (if (null area) image (list (list 'margin area) image)))) |
173 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff GM |
174 | (overlay-put overlay 'put-image t) |
175 | (overlay-put overlay 'before-string string)))) | |
7840ced1 GM |
176 | |
177 | ||
178 | ;;;###autoload | |
cedbd8d0 | 179 | (defun insert-image (image &optional string area) |
7840ced1 | 180 | "Insert IMAGE into current buffer at point. |
46655c94 | 181 | IMAGE is displayed by inserting STRING into the current buffer |
cedbd8d0 DL |
182 | with a `display' property whose value is the image. STRING is |
183 | defaulted if you omit it. | |
7840ced1 GM |
184 | AREA is where to display the image. AREA nil or omitted means |
185 | display it in the text area, a value of `left-margin' means | |
186 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 187 | means display it in the right marginal area." |
0ad550ba DL |
188 | ;; Use a space as least likely to cause trouble when it's a hidden |
189 | ;; character in the buffer. | |
190 | (unless string (setq string " ")) | |
cedbd8d0 | 191 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
192 | (error "Not an image: %s" image)) |
193 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
194 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
195 | (if area |
196 | (setq image (list (list 'margin area) image)) | |
197 | ;; Cons up a new spec equal but not eq to `image' so that | |
198 | ;; inserting it twice in a row (adjacently) displays two copies of | |
199 | ;; the image. Don't try to avoid this by looking at the display | |
200 | ;; properties on either side so that we DTRT more often with | |
201 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
202 | ;; of it loses anyway.) | |
203 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
204 | (let ((start (point))) |
205 | (insert string) | |
206 | (add-text-properties start (point) | |
5f1fbf6b | 207 | `(display ,image rear-nonsticky (display))))) |
10e2102e | 208 | |
7840ced1 GM |
209 | |
210 | ;;;###autoload | |
211 | (defun remove-images (start end &optional buffer) | |
212 | "Remove images between START and END in BUFFER. | |
213 | Remove only images that were put in BUFFER with calls to `put-image'. | |
214 | BUFFER nil or omitted means use the current buffer." | |
215 | (unless buffer | |
216 | (setq buffer (current-buffer))) | |
217 | (let ((overlays (overlays-in start end))) | |
218 | (while overlays | |
219 | (let ((overlay (car overlays))) | |
220 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
221 | (delete-overlay overlay))) |
222 | (setq overlays (cdr overlays))))) | |
7840ced1 GM |
223 | |
224 | ||
225 | ;;;###autoload | |
349c034d GM |
226 | (defun find-image (specs) |
227 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 228 | |
cedbd8d0 | 229 | SPECS is a list of image specifications. |
7840ced1 GM |
230 | |
231 | Each image specification in SPECS is a property list. The contents of | |
232 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
233 | least contain the properties `:type TYPE' and either `:file FILE' or |
234 | `:data DATA', where TYPE is a symbol specifying the image type, | |
235 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
cedbd8d0 DL |
236 | string containing the actual image data. The specification whose TYPE |
237 | is supported, and FILE exists, is used to construct the image | |
238 | specification to be returned. Return nil if no specification is | |
239 | satisfied. | |
240 | ||
241 | The image is looked for first on `load-path' and then in `data-directory'." | |
7840ced1 GM |
242 | (let (image) |
243 | (while (and specs (null image)) | |
244 | (let* ((spec (car specs)) | |
245 | (type (plist-get spec :type)) | |
162dec01 | 246 | (data (plist-get spec :data)) |
7f228379 GM |
247 | (file (plist-get spec :file)) |
248 | found) | |
162dec01 GM |
249 | (when (image-type-available-p type) |
250 | (cond ((stringp file) | |
7f228379 GM |
251 | (let ((path load-path)) |
252 | (while (and (not found) path) | |
253 | (let ((try-file (expand-file-name file (car path)))) | |
254 | (when (file-readable-p try-file) | |
255 | (setq found try-file))) | |
256 | (setq path (cdr path))) | |
257 | (unless found | |
cedbd8d0 DL |
258 | (let ((try-file (expand-file-name file data-directory))) |
259 | (if (file-readable-p try-file) | |
260 | (setq found try-file)))) | |
261 | (if found | |
262 | (setq image | |
9bb7a286 DL |
263 | (cons 'image (plist-put (copy-sequence spec) |
264 | :file found)))))) | |
f64ae86c | 265 | ((not (null data)) |
162dec01 GM |
266 | (setq image (cons 'image spec))))) |
267 | (setq specs (cdr specs)))) | |
349c034d GM |
268 | image)) |
269 | ||
270 | ||
271 | ;;;###autoload | |
272 | (defmacro defimage (symbol specs &optional doc) | |
273 | "Define SYMBOL as an image. | |
274 | ||
275 | SPECS is a list of image specifications. DOC is an optional | |
276 | documentation string. | |
277 | ||
278 | Each image specification in SPECS is a property list. The contents of | |
279 | a specification are image type dependent. All specifications must at | |
280 | least contain the properties `:type TYPE' and either `:file FILE' or | |
281 | `:data DATA', where TYPE is a symbol specifying the image type, | |
282 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
283 | string containing the actual image data. The first image | |
284 | specification whose TYPE is supported, and FILE exists, is used to | |
285 | define SYMBOL. | |
286 | ||
287 | Example: | |
288 | ||
289 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
290 | (:type xbm :file \"~/test1.xbm\")))" | |
291 | `(defvar ,symbol (find-image ',specs) ,doc)) | |
7840ced1 GM |
292 | |
293 | ||
294 | (provide 'image) | |
295 | ||
ab5796a9 | 296 | ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3 |
e43367a0 | 297 | ;;; image.el ends here |