Commit | Line | Data |
---|---|---|
7840ced1 GM |
1 | ;;; image.el --- image API |
2 | ||
0d30b337 TTN |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, |
4 | ;; 2004, 2005 Free Software Foundation, Inc. | |
634f4a67 PJ |
5 | |
6 | ;; Maintainer: FSF | |
04799cf5 | 7 | ;; Keywords: multimedia |
7840ced1 GM |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
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 | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
7840ced1 GM |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;;; Code: | |
29 | ||
77f6105c MB |
30 | |
31 | (defgroup image () | |
32 | "Image support." | |
33 | :group 'multimedia) | |
34 | ||
35 | ||
4fde92ef | 36 | (defconst image-type-header-regexps |
3bdbead3 | 37 | '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) |
69ebef1d GM |
38 | ("\\`P[1-6]" . pbm) |
39 | ("\\`GIF8" . gif) | |
69ebef1d | 40 | ("\\`\211PNG\r\n" . png) |
3bdbead3 | 41 | ("\\`[\t\n\r ]*#define" . xbm) |
6ea3db8a | 42 | ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff) |
3bdbead3 | 43 | ("\\`[\t\n\r ]*%!PS" . postscript) |
8a8ef149 | 44 | ("\\`\xff\xd8" . (image-jpeg-p . jpeg))) |
7840ced1 GM |
45 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types. |
46 | When the first bytes of an image file match REGEXP, it is assumed to | |
8a8ef149 GM |
47 | be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol, |
48 | IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called | |
49 | with one argument, a string containing the image data. If PREDICATE returns | |
37d4a06e | 50 | a non-nil value, TYPE is the image's type.") |
8a8ef149 | 51 | |
4fde92ef KS |
52 | (defconst image-type-file-name-regexps |
53 | '(("\\.png\\'" . png) | |
54 | ("\\.gif\\'" . gif) | |
55 | ("\\.jpe?g\\'" . jpeg) | |
56 | ("\\.bmp\\'" . bmp) | |
57 | ("\\.xpm\\'" . xpm) | |
58 | ("\\.pbm\\'" . pbm) | |
59 | ("\\.xbm\\'" . xbm) | |
60 | ("\\.ps\\'" . postscript) | |
61 | ("\\.tiff?\\'" . tiff)) | |
62 | "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files. | |
63 | When the name of an image file match REGEXP, it is assumed to | |
64 | be of image type IMAGE-TYPE.") | |
65 | ||
66 | ||
dada660a | 67 | (defvar image-load-path nil |
5fc5ac38 | 68 | "List of locations in which to search for image files. |
93a75651 CY |
69 | If an element is a string, it defines a directory to search. |
70 | If an element is a variable symbol whose value is a string, that | |
71 | value defines a directory to search. | |
72 | If an element is a variable symbol whose value is a list, the | |
73 | value is used as a list of directories to search.") | |
5fc5ac38 | 74 | |
dada660a RS |
75 | (eval-at-startup |
76 | (setq image-load-path | |
77 | (list (file-name-as-directory (expand-file-name "images" data-directory)) | |
78 | 'data-directory 'load-path))) | |
79 | ||
8a8ef149 | 80 | (defun image-jpeg-p (data) |
9b78a6a3 RS |
81 | "Value is non-nil if DATA, a string, consists of JFIF image data. |
82 | We accept the tag Exif because that is the same format." | |
8a8ef149 GM |
83 | (when (string-match "\\`\xff\xd8" data) |
84 | (catch 'jfif | |
85 | (let ((len (length data)) (i 2)) | |
86 | (while (< i len) | |
87 | (when (/= (aref data i) #xff) | |
88 | (throw 'jfif nil)) | |
89 | (setq i (1+ i)) | |
90 | (when (>= (+ i 2) len) | |
91 | (throw 'jfif nil)) | |
92 | (let ((nbytes (+ (lsh (aref data (+ i 1)) 8) | |
4a9bf8a4 GM |
93 | (aref data (+ i 2)))) |
94 | (code (aref data i))) | |
95 | (when (and (>= code #xe0) (<= code #xef)) | |
8a8ef149 | 96 | ;; APP0 LEN1 LEN2 "JFIF\0" |
71296446 | 97 | (throw 'jfif |
32b3c766 JPW |
98 | (string-match "JFIF\\|Exif" |
99 | (substring data i (min (+ i nbytes) len))))) | |
3bdbead3 | 100 | (setq i (+ i 1 nbytes)))))))) |
7840ced1 GM |
101 | |
102 | ||
103 | ;;;###autoload | |
162dec01 GM |
104 | (defun image-type-from-data (data) |
105 | "Determine the image type from image data DATA. | |
106 | Value is a symbol specifying the image type or nil if type cannot | |
7840ced1 | 107 | be determined." |
4fde92ef | 108 | (let ((types image-type-header-regexps) |
7840ced1 | 109 | type) |
4fde92ef | 110 | (while types |
7840ced1 GM |
111 | (let ((regexp (car (car types))) |
112 | (image-type (cdr (car types)))) | |
4fde92ef KS |
113 | (if (or (and (symbolp image-type) |
114 | (string-match regexp data)) | |
115 | (and (consp image-type) | |
116 | (funcall (car image-type) data) | |
117 | (setq image-type (cdr image-type)))) | |
118 | (setq type image-type | |
119 | types nil) | |
120 | (setq types (cdr types))))) | |
121 | type)) | |
122 | ||
123 | ||
124 | ;;;###autoload | |
125 | (defun image-type-from-buffer () | |
126 | "Determine the image type from data in the current buffer. | |
127 | Value is a symbol specifying the image type or nil if type cannot | |
128 | be determined." | |
129 | (let ((types image-type-header-regexps) | |
130 | type | |
131 | (opoint (point))) | |
132 | (goto-char (point-min)) | |
133 | (while types | |
134 | (let ((regexp (car (car types))) | |
135 | (image-type (cdr (car types))) | |
136 | data) | |
137 | (if (or (and (symbolp image-type) | |
138 | (looking-at regexp)) | |
139 | (and (consp image-type) | |
140 | (funcall (car image-type) | |
141 | (or data | |
142 | (setq data | |
143 | (buffer-substring | |
144 | (point-min) | |
145 | (min (point-max) | |
146 | (+ (point-min) 256)))))) | |
147 | (setq image-type (cdr image-type)))) | |
148 | (setq type image-type | |
149 | types nil) | |
150 | (setq types (cdr types))))) | |
151 | (goto-char opoint) | |
7840ced1 GM |
152 | type)) |
153 | ||
154 | ||
162dec01 GM |
155 | ;;;###autoload |
156 | (defun image-type-from-file-header (file) | |
157 | "Determine the type of image file FILE from its first few bytes. | |
158 | Value is a symbol specifying the image type, or nil if type cannot | |
159 | be determined." | |
4fde92ef KS |
160 | (unless (or (file-readable-p file) |
161 | (file-name-absolute-p file)) | |
162 | (setq file (image-search-load-path file))) | |
163 | (and file | |
164 | (file-readable-p file) | |
165 | (with-temp-buffer | |
166 | (set-buffer-multibyte nil) | |
167 | (insert-file-contents-literally file nil 0 256) | |
168 | (image-type-from-buffer)))) | |
169 | ||
170 | ||
171 | ;;;###autoload | |
172 | (defun image-type-from-file-name (file) | |
173 | "Determine the type of image file FILE from its name. | |
174 | Value is a symbol specifying the image type, or nil if type cannot | |
175 | be determined." | |
176 | (let ((types image-type-file-name-regexps) | |
177 | type) | |
178 | (while types | |
179 | (if (string-match (car (car types)) file) | |
180 | (setq type (cdr (car types)) | |
181 | types nil) | |
182 | (setq types (cdr types)))) | |
183 | type)) | |
162dec01 GM |
184 | |
185 | ||
7840ced1 GM |
186 | ;;;###autoload |
187 | (defun image-type-available-p (type) | |
c6d1b446 | 188 | "Return non-nil if image type TYPE is available. |
7840ced1 | 189 | Image types are symbols like `xbm' or `jpeg'." |
3c3b34d2 JB |
190 | (and (fboundp 'init-image-library) |
191 | (init-image-library type image-library-alist))) | |
7840ced1 | 192 | |
4fde92ef | 193 | |
7840ced1 | 194 | ;;;###autoload |
162dec01 GM |
195 | (defun create-image (file-or-data &optional type data-p &rest props) |
196 | "Create an image. | |
197 | FILE-OR-DATA is an image file name or image data. | |
7840ced1 | 198 | Optional TYPE is a symbol describing the image type. If TYPE is omitted |
162dec01 GM |
199 | or nil, try to determine the image type from its first few bytes |
200 | of image data. If that doesn't work, and FILE-OR-DATA is a file name, | |
9ea8de1b | 201 | use its file extension as image type. |
162dec01 | 202 | Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data. |
7840ced1 | 203 | Optional PROPS are additional image attributes to assign to the image, |
d1d3c685 | 204 | like, e.g. `:mask MASK'. |
8646a62e CY |
205 | Value is the image created, or nil if images of type TYPE are not supported. |
206 | ||
207 | Images should not be larger than specified by `max-image-size'." | |
f64ae86c GM |
208 | (when (and (not data-p) (not (stringp file-or-data))) |
209 | (error "Invalid image file name `%s'" file-or-data)) | |
162dec01 GM |
210 | (cond ((null data-p) |
211 | ;; FILE-OR-DATA is a file name. | |
212 | (unless (or type | |
213 | (setq type (image-type-from-file-header file-or-data))) | |
214 | (let ((extension (file-name-extension file-or-data))) | |
215 | (unless extension | |
216 | (error "Cannot determine image type")) | |
217 | (setq type (intern extension))))) | |
218 | (t | |
219 | ;; FILE-OR-DATA contains image data. | |
220 | (unless type | |
221 | (setq type (image-type-from-data file-or-data))))) | |
222 | (unless type | |
223 | (error "Cannot determine image type")) | |
7840ced1 | 224 | (unless (symbolp type) |
162dec01 | 225 | (error "Invalid image type `%s'" type)) |
7840ced1 | 226 | (when (image-type-available-p type) |
162dec01 GM |
227 | (append (list 'image :type type (if data-p :data :file) file-or-data) |
228 | props))) | |
7840ced1 GM |
229 | |
230 | ||
231 | ;;;###autoload | |
cedbd8d0 | 232 | (defun put-image (image pos &optional string area) |
1571b5ff | 233 | "Put image IMAGE in front of POS in the current buffer. |
7840ced1 | 234 | IMAGE must be an image created with `create-image' or `defimage'. |
46655c94 GM |
235 | IMAGE is displayed by putting an overlay into the current buffer with a |
236 | `before-string' STRING that has a `display' property whose value is the | |
cedbd8d0 | 237 | image. STRING is defaulted if you omit it. |
7840ced1 | 238 | POS may be an integer or marker. |
7840ced1 GM |
239 | AREA is where to display the image. AREA nil or omitted means |
240 | display it in the text area, a value of `left-margin' means | |
241 | display it in the left marginal area, a value of `right-margin' | |
46655c94 | 242 | means display it in the right marginal area." |
0ad550ba | 243 | (unless string (setq string "x")) |
1571b5ff | 244 | (let ((buffer (current-buffer))) |
cedbd8d0 | 245 | (unless (eq (car-safe image) 'image) |
1571b5ff GM |
246 | (error "Not an image: %s" image)) |
247 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
248 | (error "Invalid area %s" area)) | |
46655c94 | 249 | (setq string (copy-sequence string)) |
1571b5ff | 250 | (let ((overlay (make-overlay pos pos buffer)) |
46655c94 GM |
251 | (prop (if (null area) image (list (list 'margin area) image)))) |
252 | (put-text-property 0 (length string) 'display prop string) | |
1571b5ff GM |
253 | (overlay-put overlay 'put-image t) |
254 | (overlay-put overlay 'before-string string)))) | |
7840ced1 GM |
255 | |
256 | ||
257 | ;;;###autoload | |
5af275e0 | 258 | (defun insert-image (image &optional string area slice) |
7840ced1 | 259 | "Insert IMAGE into current buffer at point. |
46655c94 | 260 | IMAGE is displayed by inserting STRING into the current buffer |
cedbd8d0 DL |
261 | with a `display' property whose value is the image. STRING is |
262 | defaulted if you omit it. | |
7840ced1 GM |
263 | AREA is where to display the image. AREA nil or omitted means |
264 | display it in the text area, a value of `left-margin' means | |
265 | display it in the left marginal area, a value of `right-margin' | |
5af275e0 KS |
266 | means display it in the right marginal area. |
267 | SLICE specifies slice of IMAGE to insert. SLICE nil or omitted | |
268 | means insert whole image. SLICE is a list (X Y WIDTH HEIGHT) | |
269 | specifying the X and Y positions and WIDTH and HEIGHT of image area | |
270 | to insert. A float value 0.0 - 1.0 means relative to the width or | |
271 | height of the image; integer values are taken as pixel values." | |
0ad550ba DL |
272 | ;; Use a space as least likely to cause trouble when it's a hidden |
273 | ;; character in the buffer. | |
274 | (unless string (setq string " ")) | |
cedbd8d0 | 275 | (unless (eq (car-safe image) 'image) |
7840ced1 GM |
276 | (error "Not an image: %s" image)) |
277 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
278 | (error "Invalid area %s" area)) | |
0dc91c57 DL |
279 | (if area |
280 | (setq image (list (list 'margin area) image)) | |
281 | ;; Cons up a new spec equal but not eq to `image' so that | |
282 | ;; inserting it twice in a row (adjacently) displays two copies of | |
283 | ;; the image. Don't try to avoid this by looking at the display | |
284 | ;; properties on either side so that we DTRT more often with | |
285 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
286 | ;; of it loses anyway.) | |
287 | (setq image (cons 'image (cdr image)))) | |
46655c94 GM |
288 | (let ((start (point))) |
289 | (insert string) | |
290 | (add-text-properties start (point) | |
5af275e0 KS |
291 | `(display ,(if slice |
292 | (list (cons 'slice slice) image) | |
293 | image) rear-nonsticky (display))))) | |
294 | ||
295 | ||
f22c36d3 | 296 | ;;;###autoload |
5af275e0 | 297 | (defun insert-sliced-image (image &optional string area rows cols) |
b48bc937 KS |
298 | "Insert IMAGE into current buffer at point. |
299 | IMAGE is displayed by inserting STRING into the current buffer | |
300 | with a `display' property whose value is the image. STRING is | |
301 | defaulted if you omit it. | |
302 | AREA is where to display the image. AREA nil or omitted means | |
303 | display it in the text area, a value of `left-margin' means | |
304 | display it in the left marginal area, a value of `right-margin' | |
305 | means display it in the right marginal area. | |
306 | The image is automatically split into ROW x COLS slices." | |
5af275e0 KS |
307 | (unless string (setq string " ")) |
308 | (unless (eq (car-safe image) 'image) | |
309 | (error "Not an image: %s" image)) | |
310 | (unless (or (null area) (memq area '(left-margin right-margin))) | |
311 | (error "Invalid area %s" area)) | |
312 | (if area | |
313 | (setq image (list (list 'margin area) image)) | |
314 | ;; Cons up a new spec equal but not eq to `image' so that | |
315 | ;; inserting it twice in a row (adjacently) displays two copies of | |
316 | ;; the image. Don't try to avoid this by looking at the display | |
317 | ;; properties on either side so that we DTRT more often with | |
318 | ;; cut-and-paste. (Yanking killed image text next to another copy | |
319 | ;; of it loses anyway.) | |
320 | (setq image (cons 'image (cdr image)))) | |
321 | (let ((x 0.0) (dx (/ 1.0001 (or cols 1))) | |
322 | (y 0.0) (dy (/ 1.0001 (or rows 1)))) | |
323 | (while (< y 1.0) | |
324 | (while (< x 1.0) | |
325 | (let ((start (point))) | |
326 | (insert string) | |
327 | (add-text-properties start (point) | |
328 | `(display ,(list (list 'slice x y dx dy) image) | |
329 | rear-nonsticky (display))) | |
330 | (setq x (+ x dx)))) | |
331 | (setq x 0.0 | |
332 | y (+ y dy)) | |
82dc617b | 333 | (insert (propertize "\n" 'line-height t))))) |
5af275e0 | 334 | |
10e2102e | 335 | |
7840ced1 GM |
336 | |
337 | ;;;###autoload | |
338 | (defun remove-images (start end &optional buffer) | |
339 | "Remove images between START and END in BUFFER. | |
340 | Remove only images that were put in BUFFER with calls to `put-image'. | |
341 | BUFFER nil or omitted means use the current buffer." | |
342 | (unless buffer | |
343 | (setq buffer (current-buffer))) | |
344 | (let ((overlays (overlays-in start end))) | |
345 | (while overlays | |
346 | (let ((overlay (car overlays))) | |
347 | (when (overlay-get overlay 'put-image) | |
0c898dd9 DL |
348 | (delete-overlay overlay))) |
349 | (setq overlays (cdr overlays))))) | |
7840ced1 | 350 | |
4fde92ef KS |
351 | (defun image-search-load-path (file &optional path) |
352 | (unless path | |
353 | (setq path image-load-path)) | |
354 | (let (element found filename) | |
5fc5ac38 | 355 | (while (and (not found) (consp path)) |
93a75651 | 356 | (setq element (car path)) |
5fc5ac38 | 357 | (cond |
93a75651 | 358 | ((stringp element) |
5fc5ac38 CY |
359 | (setq found |
360 | (file-readable-p | |
4fde92ef | 361 | (setq filename (expand-file-name file element))))) |
93a75651 CY |
362 | ((and (symbolp element) (boundp element)) |
363 | (setq element (symbol-value element)) | |
364 | (cond | |
365 | ((stringp element) | |
366 | (setq found | |
367 | (file-readable-p | |
4fde92ef | 368 | (setq filename (expand-file-name file element))))) |
93a75651 | 369 | ((consp element) |
4fde92ef | 370 | (if (setq filename (image-search-load-path file element)) |
93a75651 | 371 | (setq found t)))))) |
5fc5ac38 | 372 | (setq path (cdr path))) |
4fde92ef | 373 | (if found filename))) |
7840ced1 GM |
374 | |
375 | ;;;###autoload | |
349c034d GM |
376 | (defun find-image (specs) |
377 | "Find an image, choosing one of a list of image specifications. | |
7840ced1 | 378 | |
cedbd8d0 | 379 | SPECS is a list of image specifications. |
7840ced1 GM |
380 | |
381 | Each image specification in SPECS is a property list. The contents of | |
382 | a specification are image type dependent. All specifications must at | |
bc283707 WP |
383 | least contain the properties `:type TYPE' and either `:file FILE' or |
384 | `:data DATA', where TYPE is a symbol specifying the image type, | |
385 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
cedbd8d0 DL |
386 | string containing the actual image data. The specification whose TYPE |
387 | is supported, and FILE exists, is used to construct the image | |
388 | specification to be returned. Return nil if no specification is | |
389 | satisfied. | |
390 | ||
8646a62e CY |
391 | The image is looked for in `image-load-path'. |
392 | ||
393 | Image files should not be larger than specified by `max-image-size'." | |
7840ced1 GM |
394 | (let (image) |
395 | (while (and specs (null image)) | |
396 | (let* ((spec (car specs)) | |
397 | (type (plist-get spec :type)) | |
162dec01 | 398 | (data (plist-get spec :data)) |
7f228379 GM |
399 | (file (plist-get spec :file)) |
400 | found) | |
162dec01 GM |
401 | (when (image-type-available-p type) |
402 | (cond ((stringp file) | |
4fde92ef | 403 | (if (setq found (image-search-load-path file)) |
5fc5ac38 CY |
404 | (setq image |
405 | (cons 'image (plist-put (copy-sequence spec) | |
406 | :file found))))) | |
f64ae86c | 407 | ((not (null data)) |
162dec01 GM |
408 | (setq image (cons 'image spec))))) |
409 | (setq specs (cdr specs)))) | |
349c034d GM |
410 | image)) |
411 | ||
412 | ||
413 | ;;;###autoload | |
414 | (defmacro defimage (symbol specs &optional doc) | |
415 | "Define SYMBOL as an image. | |
416 | ||
417 | SPECS is a list of image specifications. DOC is an optional | |
418 | documentation string. | |
419 | ||
420 | Each image specification in SPECS is a property list. The contents of | |
421 | a specification are image type dependent. All specifications must at | |
422 | least contain the properties `:type TYPE' and either `:file FILE' or | |
423 | `:data DATA', where TYPE is a symbol specifying the image type, | |
424 | e.g. `xbm', FILE is the file to load the image from, and DATA is a | |
425 | string containing the actual image data. The first image | |
426 | specification whose TYPE is supported, and FILE exists, is used to | |
427 | define SYMBOL. | |
428 | ||
429 | Example: | |
430 | ||
431 | (defimage test-image ((:type xpm :file \"~/test1.xpm\") | |
432 | (:type xbm :file \"~/test1.xbm\")))" | |
433 | `(defvar ,symbol (find-image ',specs) ,doc)) | |
7840ced1 GM |
434 | |
435 | ||
436 | (provide 'image) | |
437 | ||
ab5796a9 | 438 | ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3 |
e43367a0 | 439 | ;;; image.el ends here |