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