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