Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
3c3b34d2 | 3 | ;; Copyright (C) 1998, 99, 2000, 01, 04 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 | 50 | |
8a8ef149 | 51 | (defun image-jpeg-p (data) |
9b78a6a3 RS |
52 | "Value is non-nil if DATA, a string, consists of JFIF image data. |
53 | We accept the tag Exif because that is the same format." | |
8a8ef149 GM |
54 | (when (string-match "\\`\xff\xd8" data) |
55 | (catch 'jfif | |
56 | (let ((len (length data)) (i 2)) | |
57 | (while (< i len) | |
58 | (when (/= (aref data i) #xff) | |
59 | (throw 'jfif nil)) | |
60 | (setq i (1+ i)) | |
61 | (when (>= (+ i 2) len) | |
62 | (throw 'jfif nil)) | |
63 | (let ((nbytes (+ (lsh (aref data (+ i 1)) 8) | |
4a9bf8a4 GM |
64 | (aref data (+ i 2)))) |
65 | (code (aref data i))) | |
66 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 67 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 68 | (throw 'jfif |
32b3c766 JPW |
69 | (string-match "JFIF\\|Exif" |
70 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 71 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
72 | |
73 | ||
74 | ;;;###autoload | |
162dec01 GM |
75 | (defun image-type-from-data (data) |
76 | "Determine the image type from image data DATA. | |
77 | Value is a symbol specifying the image type or nil if type cannot | |
7840ced1 | 78 | be determined." |
162dec01 | 79 | (let ((types image-type-regexps) |
7840ced1 GM |
80 | type) |
81 | (while (and types (null type)) | |
82 | (let ((regexp (car (car types))) | |
83 | (image-type (cdr (car types)))) | |
8a8ef149 GM |
84 | (when (or (and (symbolp image-type) |
85 | (string-match regexp data)) | |
86 | (and (consp image-type) | |
87 | (funcall (car image-type) data) | |
88 | (setq image-type (cdr image-type)))) | |
7840ced1 GM |
89 | (setq type image-type)) |
90 | (setq types (cdr types)))) | |
91 | type)) | |
92 | ||
93 | ||
162dec01 GM |
94 | ;;;###autoload |
95 | (defun image-type-from-file-header (file) | |
96 | "Determine the type of image file FILE from its first few bytes. | |
97 | Value is a symbol specifying the image type, or nil if type cannot | |
98 | be determined." | |
99 | (unless (file-name-directory file) | |
100 | (setq file (expand-file-name file data-directory))) | |
101 | (setq file (expand-file-name file)) | |
102 | (let ((header (with-temp-buffer | |
ee485a83 | 103 | (set-buffer-multibyte nil) |
162dec01 GM |
104 | (insert-file-contents-literally file nil 0 256) |
105 | (buffer-string)))) | |
106 | (image-type-from-data header))) | |
107 | ||
108 | ||
7840ced1 GM |
109 | ;;;###autoload |
110 | (defun image-type-available-p (type) | |
c6d1b446 | 111 | "Return non-nil if image type TYPE is available. |
7840ced1 | 112 | Image types are symbols like `xbm' or `jpeg'." |
3c3b34d2 JB |
113 | (and (fboundp 'init-image-library) |
114 | (init-image-library type image-library-alist))) | |
7840ced1 GM |
115 | |
116 | ;;;###autoload | |
162dec01 GM |
117 | (defun create-image (file-or-data &optional type data-p &rest props) |
118 | "Create an image. | |
119 | FILE-OR-DATA is an image file name or image data. | |
7840ced1 | 120 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 GM |
121 | or nil, try to determine the image type from its first few bytes |
122 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
9ea8de1b | 123 | use its file extension as image type. |
162dec01 | 124 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. |
7840ced1 | 125 | Optional PROPS are additional image attributes to assign to the image, |
d1d3c685 | 126 | like, e.g. `:mask MASK'. |
7840ced1 | 127 | Value is the image created, or nil if images of type TYPE are not supported." |
f64ae86c GM |
128 | (when (and (not data-p) (not (stringp file-or-data))) |
129 | (error "Invalid image file name `%s'" file-or-data)) | |
162dec01 GM |
130 | (cond ((null data-p) |
131 | ;; FILE-OR-DATA is a file name. | |
132 | (unless (or type | |
133 | (setq type (image-type-from-file-header file-or-data))) | |
134 | (let ((extension (file-name-extension file-or-data))) | |
135 | (unless extension | |
136 | (error "Cannot determine image type")) | |
137 | (setq type (intern extension))))) | |
138 | (t | |
139 | ;; FILE-OR-DATA contains image data. | |
140 | (unless type | |
141 | (setq type (image-type-from-data file-or-data))))) | |
142 | (unless type | |
143 | (error "Cannot determine image type")) | |
7840ced1 | 144 | (unless (symbolp type) |
162dec01 | 145 | (error "Invalid image type `%s'" type)) |
7840ced1 | 146 | (when (image-type-available-p type) |
162dec01 GM |
147 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
148 | props))) | |
7840ced1 GM |
149 | |
150 | ||
151 | ;;;###autoload | |
cedbd8d0 | 152 | (defun put-image (image pos &optional string area) |
1571b5ff | 153 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 154 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
155 | IMAGE is displayed by putting an overlay into the current buffer with a |
156 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 157 | image. STRING is defaulted if you omit it. |
7840ced1 | 158 | POS may be an integer or marker. |
7840ced1 GM |
159 | AREA is where to display the image. AREA nil or omitted means |
160 | display it in the text area, a value of `left-margin' means | |
161 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 162 | means display it in the right marginal area." |
0ad550ba | 163 | (unless string (setq string "x")) |
1571b5ff | 164 | (let ((buffer (current-buffer))) |
cedbd8d0 | 165 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
166 | (error "Not an image: %s" image)) |
167 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
168 | (error "Invalid area %s" area)) | |
46655c94 | 169 | (setq string (copy-sequence string)) |
1571b5ff | 170 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
171 | (prop (if (null area) image (list (list 'margin area) image)))) |
172 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff GM |
173 | (overlay-put overlay 'put-image t) |
174 | (overlay-put overlay 'before-string string)))) | |
7840ced1 GM |
175 | |
176 | ||
177 | ;;;###autoload | |
5af275e0 | 178 | (defun insert-image (image &optional string area slice) |
7840ced1 | 179 | "Insert IMAGE into current buffer at point. |
46655c94 | 180 | IMAGE is displayed by inserting STRING into the current buffer |
cedbd8d0 DL |
181 | with a `display' property whose value is the image. STRING is |
182 | defaulted if you omit it. | |
7840ced1 GM |
183 | AREA is where to display the image. AREA nil or omitted means |
184 | display it in the text area, a value of `left-margin' means | |
185 | display it in the left marginal area, a value of `right-margin' | |
5af275e0 KS |
186 | means display it in the right marginal area. |
187 | SLICE specifies slice of IMAGE to insert. SLICE nil or omitted | |
188 | means insert whole image. SLICE is a list (X Y WIDTH HEIGHT) | |
189 | specifying the X and Y positions and WIDTH and HEIGHT of image area | |
190 | to insert. A float value 0.0 - 1.0 means relative to the width or | |
191 | height of the image; integer values are taken as pixel values." | |
0ad550ba DL |
192 | ;; Use a space as least likely to cause trouble when it's a hidden |
193 | ;; character in the buffer. | |
194 | (unless string (setq string " ")) | |
cedbd8d0 | 195 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
196 | (error "Not an image: %s" image)) |
197 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
198 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
199 | (if area |
200 | (setq image (list (list 'margin area) image)) | |
201 | ;; Cons up a new spec equal but not eq to `image' so that | |
202 | ;; inserting it twice in a row (adjacently) displays two copies of | |
203 | ;; the image. Don't try to avoid this by looking at the display | |
204 | ;; properties on either side so that we DTRT more often with | |
205 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
206 | ;; of it loses anyway.) | |
207 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
208 | (let ((start (point))) |
209 | (insert string) | |
210 | (add-text-properties start (point) | |
5af275e0 KS |
211 | `(display ,(if slice |
212 | (list (cons 'slice slice) image) | |
213 | image) rear-nonsticky (display))))) | |
214 | ||
215 | ||
f22c36d3 | 216 | ;;;###autoload |
5af275e0 | 217 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
218 | "Insert IMAGE into current buffer at point. |
219 | IMAGE is displayed by inserting STRING into the current buffer | |
220 | with a `display' property whose value is the image. STRING is | |
221 | defaulted if you omit it. | |
222 | AREA is where to display the image. AREA nil or omitted means | |
223 | display it in the text area, a value of `left-margin' means | |
224 | display it in the left marginal area, a value of `right-margin' | |
225 | means display it in the right marginal area. | |
226 | The image is automatically split into ROW x COLS slices." | |
5af275e0 KS |
227 | (unless string (setq string " ")) |
228 | (unless (eq (car-safe image) 'image) | |
229 | (error "Not an image: %s" image)) | |
230 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
231 | (error "Invalid area %s" area)) | |
232 | (if area | |
233 | (setq image (list (list 'margin area) image)) | |
234 | ;; Cons up a new spec equal but not eq to `image' so that | |
235 | ;; inserting it twice in a row (adjacently) displays two copies of | |
236 | ;; the image. Don't try to avoid this by looking at the display | |
237 | ;; properties on either side so that we DTRT more often with | |
238 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
239 | ;; of it loses anyway.) | |
240 | (setq image (cons 'image (cdr image)))) | |
241 | (let ((x 0.0) (dx (/ 1.0001 (or cols 1))) | |
242 | (y 0.0) (dy (/ 1.0001 (or rows 1)))) | |
243 | (while (< y 1.0) | |
244 | (while (< x 1.0) | |
245 | (let ((start (point))) | |
246 | (insert string) | |
247 | (add-text-properties start (point) | |
248 | `(display ,(list (list 'slice x y dx dy) image) | |
249 | rear-nonsticky (display))) | |
250 | (setq x (+ x dx)))) | |
251 | (setq x 0.0 | |
252 | y (+ y dy)) | |
82dc617b | 253 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 254 | |
10e2102e | 255 | |
7840ced1 GM |
256 | |
257 | ;;;###autoload | |
258 | (defun remove-images (start end &optional buffer) | |
259 | "Remove images between START and END in BUFFER. | |
260 | Remove only images that were put in BUFFER with calls to `put-image'. | |
261 | BUFFER nil or omitted means use the current buffer." | |
262 | (unless buffer | |
263 | (setq buffer (current-buffer))) | |
264 | (let ((overlays (overlays-in start end))) | |
265 | (while overlays | |
266 | (let ((overlay (car overlays))) | |
267 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
268 | (delete-overlay overlay))) |
269 | (setq overlays (cdr overlays))))) | |
7840ced1 GM |
270 | |
271 | ||
272 | ;;;###autoload | |
349c034d GM |
273 | (defun find-image (specs) |
274 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 275 | |
cedbd8d0 | 276 | SPECS is a list of image specifications. |
7840ced1 GM |
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 | |
bc283707 WP |
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 | |
cedbd8d0 DL |
283 | string containing the actual image data. The specification whose TYPE |
284 | is supported, and FILE exists, is used to construct the image | |
285 | specification to be returned. Return nil if no specification is | |
286 | satisfied. | |
287 | ||
288 | The image is looked for first on `load-path' and then in `data-directory'." | |
7840ced1 GM |
289 | (let (image) |
290 | (while (and specs (null image)) | |
291 | (let* ((spec (car specs)) | |
292 | (type (plist-get spec :type)) | |
162dec01 | 293 | (data (plist-get spec :data)) |
7f228379 GM |
294 | (file (plist-get spec :file)) |
295 | found) | |
162dec01 GM |
296 | (when (image-type-available-p type) |
297 | (cond ((stringp file) | |
7f228379 GM |
298 | (let ((path load-path)) |
299 | (while (and (not found) path) | |
300 | (let ((try-file (expand-file-name file (car path)))) | |
301 | (when (file-readable-p try-file) | |
302 | (setq found try-file))) | |
303 | (setq path (cdr path))) | |
304 | (unless found | |
cedbd8d0 DL |
305 | (let ((try-file (expand-file-name file data-directory))) |
306 | (if (file-readable-p try-file) | |
307 | (setq found try-file)))) | |
308 | (if found | |
309 | (setq image | |
9bb7a286 DL |
310 | (cons 'image (plist-put (copy-sequence spec) |
311 | :file found)))))) | |
f64ae86c | 312 | ((not (null data)) |
162dec01 GM |
313 | (setq image (cons 'image spec))))) |
314 | (setq specs (cdr specs)))) | |
349c034d GM |
315 | image)) |
316 | ||
317 | ||
318 | ;;;###autoload | |
319 | (defmacro defimage (symbol specs &optional doc) | |
320 | "Define SYMBOL as an image. | |
321 | ||
322 | SPECS is a list of image specifications. DOC is an optional | |
323 | documentation string. | |
324 | ||
325 | Each image specification in SPECS is a property list. The contents of | |
326 | a specification are image type dependent. All specifications must at | |
327 | least contain the properties `:type TYPE' and either `:file FILE' or | |
328 | `:data DATA', where TYPE is a symbol specifying the image type, | |
329 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
330 | string containing the actual image data. The first image | |
331 | specification whose TYPE is supported, and FILE exists, is used to | |
332 | define SYMBOL. | |
333 | ||
334 | Example: | |
335 | ||
336 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
337 | (:type xbm :file \"~/test1.xbm\")))" | |
338 | `(defvar ,symbol (find-image ',specs) ,doc)) | |
7840ced1 GM |
339 | |
340 | ||
341 | (provide 'image) | |
342 | ||
ab5796a9 | 343 | ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3 |
e43367a0 | 344 | ;;; image.el ends here |