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