]> code.delx.au - gnu-emacs/blob - lisp/image.el
message format spec fixes, commit # 10
[gnu-emacs] / lisp / image.el
1 ;;; image.el --- image API
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: multimedia
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30
31 (defgroup image ()
32 "Image support."
33 :group 'multimedia)
34
35
36 (defconst image-type-regexps
37 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
38 ("\\`P[1-6]" . pbm)
39 ("\\`GIF8" . gif)
40 ("\\`\211PNG\r\n" . png)
41 ("\\`[\t\n\r ]*#define" . xbm)
42 ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
43 ("\\`[\t\n\r ]*%!PS" . postscript)
44 ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
45 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
46 When the first bytes of an image file match REGEXP, it is assumed to
47 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
48 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
49 with one argument, a string containing the image data. If PREDICATE returns
50 a non-nil value, TYPE is the image's type.")
51
52 (defvar image-load-path
53 (list (file-name-as-directory (expand-file-name "images" data-directory))
54 data-directory 'load-path)
55 "List of locations in which to search for image files.
56 If an element is a string, it defines a directory to search in.
57 If an element is a variable symbol, the value of that variable is
58 used as a list of directories to search.")
59
60 (defun image-jpeg-p (data)
61 "Value is non-nil if DATA, a string, consists of JFIF image data.
62 We accept the tag Exif because that is the same format."
63 (when (string-match "\\`\xff\xd8" data)
64 (catch 'jfif
65 (let ((len (length data)) (i 2))
66 (while (< i len)
67 (when (/= (aref data i) #xff)
68 (throw 'jfif nil))
69 (setq i (1+ i))
70 (when (>= (+ i 2) len)
71 (throw 'jfif nil))
72 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
73 (aref data (+ i 2))))
74 (code (aref data i)))
75 (when (and (>= code #xe0) (<= code #xef))
76 ;; APP0 LEN1 LEN2 "JFIF\0"
77 (throw 'jfif
78 (string-match "JFIF\\|Exif"
79 (substring data i (min (+ i nbytes) len)))))
80 (setq i (+ i 1 nbytes))))))))
81
82
83 ;;;###autoload
84 (defun image-type-from-data (data)
85 "Determine the image type from image data DATA.
86 Value is a symbol specifying the image type or nil if type cannot
87 be determined."
88 (let ((types image-type-regexps)
89 type)
90 (while (and types (null type))
91 (let ((regexp (car (car types)))
92 (image-type (cdr (car types))))
93 (when (or (and (symbolp image-type)
94 (string-match regexp data))
95 (and (consp image-type)
96 (funcall (car image-type) data)
97 (setq image-type (cdr image-type))))
98 (setq type image-type))
99 (setq types (cdr types))))
100 type))
101
102
103 ;;;###autoload
104 (defun image-type-from-file-header (file)
105 "Determine the type of image file FILE from its first few bytes.
106 Value is a symbol specifying the image type, or nil if type cannot
107 be determined."
108 (unless (file-name-directory file)
109 (setq file (expand-file-name file data-directory)))
110 (setq file (expand-file-name file))
111 (let ((header (with-temp-buffer
112 (set-buffer-multibyte nil)
113 (insert-file-contents-literally file nil 0 256)
114 (buffer-string))))
115 (image-type-from-data header)))
116
117
118 ;;;###autoload
119 (defun image-type-available-p (type)
120 "Return non-nil if image type TYPE is available.
121 Image types are symbols like `xbm' or `jpeg'."
122 (and (fboundp 'init-image-library)
123 (init-image-library type image-library-alist)))
124
125 ;;;###autoload
126 (defun create-image (file-or-data &optional type data-p &rest props)
127 "Create an image.
128 FILE-OR-DATA is an image file name or image data.
129 Optional TYPE is a symbol describing the image type. If TYPE is omitted
130 or nil, try to determine the image type from its first few bytes
131 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
132 use its file extension as image type.
133 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
134 Optional PROPS are additional image attributes to assign to the image,
135 like, e.g. `:mask MASK'.
136 Value is the image created, or nil if images of type TYPE are not supported."
137 (when (and (not data-p) (not (stringp file-or-data)))
138 (error "Invalid image file name `%s'" file-or-data))
139 (cond ((null data-p)
140 ;; FILE-OR-DATA is a file name.
141 (unless (or type
142 (setq type (image-type-from-file-header file-or-data)))
143 (let ((extension (file-name-extension file-or-data)))
144 (unless extension
145 (error "Cannot determine image type"))
146 (setq type (intern extension)))))
147 (t
148 ;; FILE-OR-DATA contains image data.
149 (unless type
150 (setq type (image-type-from-data file-or-data)))))
151 (unless type
152 (error "Cannot determine image type"))
153 (unless (symbolp type)
154 (error "Invalid image type `%s'" type))
155 (when (image-type-available-p type)
156 (append (list 'image :type type (if data-p :data :file) file-or-data)
157 props)))
158
159
160 ;;;###autoload
161 (defun put-image (image pos &optional string area)
162 "Put image IMAGE in front of POS in the current buffer.
163 IMAGE must be an image created with `create-image' or `defimage'.
164 IMAGE is displayed by putting an overlay into the current buffer with a
165 `before-string' STRING that has a `display' property whose value is the
166 image. STRING is defaulted if you omit it.
167 POS may be an integer or marker.
168 AREA is where to display the image. AREA nil or omitted means
169 display it in the text area, a value of `left-margin' means
170 display it in the left marginal area, a value of `right-margin'
171 means display it in the right marginal area."
172 (unless string (setq string "x"))
173 (let ((buffer (current-buffer)))
174 (unless (eq (car-safe image) 'image)
175 (error "Not an image: %s" image))
176 (unless (or (null area) (memq area '(left-margin right-margin)))
177 (error "Invalid area %s" area))
178 (setq string (copy-sequence string))
179 (let ((overlay (make-overlay pos pos buffer))
180 (prop (if (null area) image (list (list 'margin area) image))))
181 (put-text-property 0 (length string) 'display prop string)
182 (overlay-put overlay 'put-image t)
183 (overlay-put overlay 'before-string string))))
184
185
186 ;;;###autoload
187 (defun insert-image (image &optional string area slice)
188 "Insert IMAGE into current buffer at point.
189 IMAGE is displayed by inserting STRING into the current buffer
190 with a `display' property whose value is the image. STRING is
191 defaulted if you omit it.
192 AREA is where to display the image. AREA nil or omitted means
193 display it in the text area, a value of `left-margin' means
194 display it in the left marginal area, a value of `right-margin'
195 means display it in the right marginal area.
196 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
197 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
198 specifying the X and Y positions and WIDTH and HEIGHT of image area
199 to insert. A float value 0.0 - 1.0 means relative to the width or
200 height of the image; integer values are taken as pixel values."
201 ;; Use a space as least likely to cause trouble when it's a hidden
202 ;; character in the buffer.
203 (unless string (setq string " "))
204 (unless (eq (car-safe image) 'image)
205 (error "Not an image: %s" image))
206 (unless (or (null area) (memq area '(left-margin right-margin)))
207 (error "Invalid area %s" area))
208 (if area
209 (setq image (list (list 'margin area) image))
210 ;; Cons up a new spec equal but not eq to `image' so that
211 ;; inserting it twice in a row (adjacently) displays two copies of
212 ;; the image. Don't try to avoid this by looking at the display
213 ;; properties on either side so that we DTRT more often with
214 ;; cut-and-paste. (Yanking killed image text next to another copy
215 ;; of it loses anyway.)
216 (setq image (cons 'image (cdr image))))
217 (let ((start (point)))
218 (insert string)
219 (add-text-properties start (point)
220 `(display ,(if slice
221 (list (cons 'slice slice) image)
222 image) rear-nonsticky (display)))))
223
224
225 ;;;###autoload
226 (defun insert-sliced-image (image &optional string area rows cols)
227 "Insert IMAGE into current buffer at point.
228 IMAGE is displayed by inserting STRING into the current buffer
229 with a `display' property whose value is the image. STRING is
230 defaulted if you omit it.
231 AREA is where to display the image. AREA nil or omitted means
232 display it in the text area, a value of `left-margin' means
233 display it in the left marginal area, a value of `right-margin'
234 means display it in the right marginal area.
235 The image is automatically split into ROW x COLS slices."
236 (unless string (setq string " "))
237 (unless (eq (car-safe image) 'image)
238 (error "Not an image: %s" image))
239 (unless (or (null area) (memq area '(left-margin right-margin)))
240 (error "Invalid area %s" area))
241 (if area
242 (setq image (list (list 'margin area) image))
243 ;; Cons up a new spec equal but not eq to `image' so that
244 ;; inserting it twice in a row (adjacently) displays two copies of
245 ;; the image. Don't try to avoid this by looking at the display
246 ;; properties on either side so that we DTRT more often with
247 ;; cut-and-paste. (Yanking killed image text next to another copy
248 ;; of it loses anyway.)
249 (setq image (cons 'image (cdr image))))
250 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
251 (y 0.0) (dy (/ 1.0001 (or rows 1))))
252 (while (< y 1.0)
253 (while (< x 1.0)
254 (let ((start (point)))
255 (insert string)
256 (add-text-properties start (point)
257 `(display ,(list (list 'slice x y dx dy) image)
258 rear-nonsticky (display)))
259 (setq x (+ x dx))))
260 (setq x 0.0
261 y (+ y dy))
262 (insert (propertize "\n" 'line-height t)))))
263
264
265
266 ;;;###autoload
267 (defun remove-images (start end &optional buffer)
268 "Remove images between START and END in BUFFER.
269 Remove only images that were put in BUFFER with calls to `put-image'.
270 BUFFER nil or omitted means use the current buffer."
271 (unless buffer
272 (setq buffer (current-buffer)))
273 (let ((overlays (overlays-in start end)))
274 (while overlays
275 (let ((overlay (car overlays)))
276 (when (overlay-get overlay 'put-image)
277 (delete-overlay overlay)))
278 (setq overlays (cdr overlays)))))
279
280 (defun image-search-load-path (file path)
281 (let (found pathname)
282 (while (and (not found) (consp path))
283 (cond
284 ((stringp (car path))
285 (setq found
286 (file-readable-p
287 (setq pathname (expand-file-name file (car path))))))
288 ((and (symbolp (car path)) (boundp (car path)))
289 (if (setq pathname (image-search-load-path
290 file (symbol-value (car path))))
291 (setq found t))))
292 (setq path (cdr path)))
293 (if found pathname)))
294
295 ;;;###autoload
296 (defun find-image (specs)
297 "Find an image, choosing one of a list of image specifications.
298
299 SPECS is a list of image specifications.
300
301 Each image specification in SPECS is a property list. The contents of
302 a specification are image type dependent. All specifications must at
303 least contain the properties `:type TYPE' and either `:file FILE' or
304 `:data DATA', where TYPE is a symbol specifying the image type,
305 e.g. `xbm', FILE is the file to load the image from, and DATA is a
306 string containing the actual image data. The specification whose TYPE
307 is supported, and FILE exists, is used to construct the image
308 specification to be returned. Return nil if no specification is
309 satisfied.
310
311 The image is looked for in `image-load-path'."
312 (let (image)
313 (while (and specs (null image))
314 (let* ((spec (car specs))
315 (type (plist-get spec :type))
316 (data (plist-get spec :data))
317 (file (plist-get spec :file))
318 found)
319 (when (image-type-available-p type)
320 (cond ((stringp file)
321 (if (setq found (image-search-load-path
322 file image-load-path))
323 (setq image
324 (cons 'image (plist-put (copy-sequence spec)
325 :file found)))))
326 ((not (null data))
327 (setq image (cons 'image spec)))))
328 (setq specs (cdr specs))))
329 image))
330
331
332 ;;;###autoload
333 (defmacro defimage (symbol specs &optional doc)
334 "Define SYMBOL as an image.
335
336 SPECS is a list of image specifications. DOC is an optional
337 documentation string.
338
339 Each image specification in SPECS is a property list. The contents of
340 a specification are image type dependent. All specifications must at
341 least contain the properties `:type TYPE' and either `:file FILE' or
342 `:data DATA', where TYPE is a symbol specifying the image type,
343 e.g. `xbm', FILE is the file to load the image from, and DATA is a
344 string containing the actual image data. The first image
345 specification whose TYPE is supported, and FILE exists, is used to
346 define SYMBOL.
347
348 Example:
349
350 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
351 (:type xbm :file \"~/test1.xbm\")))"
352 `(defvar ,symbol (find-image ',specs) ,doc))
353
354
355 (provide 'image)
356
357 ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
358 ;;; image.el ends here