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