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