(copy-list): Moved here from cl.el.
[bpt/emacs.git] / lisp / image.el
1 ;;; image.el --- image API
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: multimedia
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
29
30 (defgroup image ()
31 "Image support."
32 :group 'multimedia)
33
34
35 (defconst image-type-regexps
36 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
37 ("\\`P[1-6]" . pbm)
38 ("\\`GIF8" . gif)
39 ("\\`\211PNG\r\n" . png)
40 ("\\`[\t\n\r ]*#define" . xbm)
41 ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
42 ("\\`[\t\n\r ]*%!PS" . postscript)
43 ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
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
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
49 a non-nil value, TYPE is the image's type ")
50
51
52 (defun image-jpeg-p (data)
53 "Value is non-nil if DATA, a string, consists of JFIF image data."
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)
64 (aref data (+ i 2))))
65 (code (aref data i)))
66 (when (and (>= code #xe0) (<= code #xef))
67 ;; APP0 LEN1 LEN2 "JFIF\0"
68 (throw 'jfif
69 (string-match "JFIF" (substring data i (+ i nbytes)))))
70 (setq i (+ i 1 nbytes))))))))
71
72
73 ;;;###autoload
74 (defun image-type-from-data (data)
75 "Determine the image type from image data DATA.
76 Value is a symbol specifying the image type or nil if type cannot
77 be determined."
78 (let ((types image-type-regexps)
79 type)
80 (while (and types (null type))
81 (let ((regexp (car (car types)))
82 (image-type (cdr (car types))))
83 (when (or (and (symbolp image-type)
84 (string-match regexp data))
85 (and (consp image-type)
86 (funcall (car image-type) data)
87 (setq image-type (cdr image-type))))
88 (setq type image-type))
89 (setq types (cdr types))))
90 type))
91
92
93 ;;;###autoload
94 (defun image-type-from-file-header (file)
95 "Determine the type of image file FILE from its first few bytes.
96 Value is a symbol specifying the image type, or nil if type cannot
97 be determined."
98 (unless (file-name-directory file)
99 (setq file (expand-file-name file data-directory)))
100 (setq file (expand-file-name file))
101 (let ((header (with-temp-buffer
102 (set-buffer-multibyte nil)
103 (insert-file-contents-literally file nil 0 256)
104 (buffer-string))))
105 (image-type-from-data header)))
106
107
108 ;;;###autoload
109 (defun image-type-available-p (type)
110 "Value is non-nil if image type TYPE is available.
111 Image types are symbols like `xbm' or `jpeg'."
112 (and (boundp 'image-types) (not (null (memq type image-types)))))
113
114
115 ;;;###autoload
116 (defun create-image (file-or-data &optional type data-p &rest props)
117 "Create an image.
118 FILE-OR-DATA is an image file name or image data.
119 Optional TYPE is a symbol describing the image type. If TYPE is omitted
120 or nil, try to determine the image type from its first few bytes
121 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
122 use its file extension as image type.
123 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
124 Optional PROPS are additional image attributes to assign to the image,
125 like, e.g. `:mask MASK'.
126 Value is the image created, or nil if images of type TYPE are not supported."
127 (when (and (not data-p) (not (stringp file-or-data)))
128 (error "Invalid image file name `%s'" file-or-data))
129 (cond ((null data-p)
130 ;; FILE-OR-DATA is a file name.
131 (unless (or type
132 (setq type (image-type-from-file-header file-or-data)))
133 (let ((extension (file-name-extension file-or-data)))
134 (unless extension
135 (error "Cannot determine image type"))
136 (setq type (intern extension)))))
137 (t
138 ;; FILE-OR-DATA contains image data.
139 (unless type
140 (setq type (image-type-from-data file-or-data)))))
141 (unless type
142 (error "Cannot determine image type"))
143 (unless (symbolp type)
144 (error "Invalid image type `%s'" type))
145 (when (image-type-available-p type)
146 (append (list 'image :type type (if data-p :data :file) file-or-data)
147 props)))
148
149
150 ;;;###autoload
151 (defun put-image (image pos &optional string area)
152 "Put image IMAGE in front of POS in the current buffer.
153 IMAGE must be an image created with `create-image' or `defimage'.
154 IMAGE is displayed by putting an overlay into the current buffer with a
155 `before-string' STRING that has a `display' property whose value is the
156 image. STRING is defaulted if you omit it.
157 POS may be an integer or marker.
158 AREA is where to display the image. AREA nil or omitted means
159 display it in the text area, a value of `left-margin' means
160 display it in the left marginal area, a value of `right-margin'
161 means display it in the right marginal area."
162 (unless string (setq string "x"))
163 (let ((buffer (current-buffer)))
164 (unless (eq (car-safe image) 'image)
165 (error "Not an image: %s" image))
166 (unless (or (null area) (memq area '(left-margin right-margin)))
167 (error "Invalid area %s" area))
168 (setq string (copy-sequence string))
169 (let ((overlay (make-overlay pos pos buffer))
170 (prop (if (null area) image (list (list 'margin area) image))))
171 (put-text-property 0 (length string) 'display prop string)
172 (overlay-put overlay 'put-image t)
173 (overlay-put overlay 'before-string string))))
174
175
176 ;;;###autoload
177 (defun insert-image (image &optional string area)
178 "Insert IMAGE into current buffer at point.
179 IMAGE is displayed by inserting STRING into the current buffer
180 with a `display' property whose value is the image. STRING is
181 defaulted if you omit it.
182 AREA is where to display the image. AREA nil or omitted means
183 display it in the text area, a value of `left-margin' means
184 display it in the left marginal area, a value of `right-margin'
185 means display it in the right marginal area."
186 ;; Use a space as least likely to cause trouble when it's a hidden
187 ;; character in the buffer.
188 (unless string (setq string " "))
189 (unless (eq (car-safe image) 'image)
190 (error "Not an image: %s" image))
191 (unless (or (null area) (memq area '(left-margin right-margin)))
192 (error "Invalid area %s" area))
193 (if area
194 (setq image (list (list 'margin area) image))
195 ;; Cons up a new spec equal but not eq to `image' so that
196 ;; inserting it twice in a row (adjacently) displays two copies of
197 ;; the image. Don't try to avoid this by looking at the display
198 ;; properties on either side so that we DTRT more often with
199 ;; cut-and-paste. (Yanking killed image text next to another copy
200 ;; of it loses anyway.)
201 (setq image (cons 'image (cdr image))))
202 (let ((start (point)))
203 (insert string)
204 (add-text-properties start (point)
205 (list 'display image
206 ;; `image' has the right properties to
207 ;; mark an intangible field.
208 'intangible image
209 'rear-nonsticky (list 'display)))))
210
211
212 ;;;###autoload
213 (defun remove-images (start end &optional buffer)
214 "Remove images between START and END in BUFFER.
215 Remove only images that were put in BUFFER with calls to `put-image'.
216 BUFFER nil or omitted means use the current buffer."
217 (unless buffer
218 (setq buffer (current-buffer)))
219 (let ((overlays (overlays-in start end)))
220 (while overlays
221 (let ((overlay (car overlays)))
222 (when (overlay-get overlay 'put-image)
223 (delete-overlay overlay)))
224 (setq overlays (cdr overlays)))))
225
226
227 ;;;###autoload
228 (defun find-image (specs)
229 "Find an image, choosing one of a list of image specifications.
230
231 SPECS is a list of image specifications.
232
233 Each image specification in SPECS is a property list. The contents of
234 a specification are image type dependent. All specifications must at
235 least contain the properties `:type TYPE' and either `:file FILE' or
236 `:data DATA', where TYPE is a symbol specifying the image type,
237 e.g. `xbm', FILE is the file to load the image from, and DATA is a
238 string containing the actual image data. The specification whose TYPE
239 is supported, and FILE exists, is used to construct the image
240 specification to be returned. Return nil if no specification is
241 satisfied.
242
243 The image is looked for first on `load-path' and then in `data-directory'."
244 (let (image)
245 (while (and specs (null image))
246 (let* ((spec (car specs))
247 (type (plist-get spec :type))
248 (data (plist-get spec :data))
249 (file (plist-get spec :file))
250 found)
251 (when (image-type-available-p type)
252 (cond ((stringp file)
253 (let ((path load-path))
254 (while (and (not found) path)
255 (let ((try-file (expand-file-name file (car path))))
256 (when (file-readable-p try-file)
257 (setq found try-file)))
258 (setq path (cdr path)))
259 (unless found
260 (let ((try-file (expand-file-name file data-directory)))
261 (if (file-readable-p try-file)
262 (setq found try-file))))
263 (if found
264 (setq image
265 (cons 'image (plist-put (copy-sequence spec)
266 :file found))))))
267 ((not (null data))
268 (setq image (cons 'image spec)))))
269 (setq specs (cdr specs))))
270 image))
271
272
273 ;;;###autoload
274 (defmacro defimage (symbol specs &optional doc)
275 "Define SYMBOL as an image.
276
277 SPECS is a list of image specifications. DOC is an optional
278 documentation string.
279
280 Each image specification in SPECS is a property list. The contents of
281 a specification are image type dependent. All specifications must at
282 least contain the properties `:type TYPE' and either `:file FILE' or
283 `:data DATA', where TYPE is a symbol specifying the image type,
284 e.g. `xbm', FILE is the file to load the image from, and DATA is a
285 string containing the actual image data. The first image
286 specification whose TYPE is supported, and FILE exists, is used to
287 define SYMBOL.
288
289 Example:
290
291 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
292 (:type xbm :file \"~/test1.xbm\")))"
293 `(defvar ,symbol (find-image ',specs) ,doc))
294
295
296 (provide 'image)
297
298 ;;; image.el ends here