]> code.delx.au - gnu-emacs/blob - lisp/image.el
Update years in copyright notice; nfc.
[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 (defun image-jpeg-p (data)
53 "Value is non-nil if DATA, a string, consists of JFIF image data.
54 We accept the tag Exif because that is the same format."
55 (when (string-match "\\`\xff\xd8" data)
56 (catch 'jfif
57 (let ((len (length data)) (i 2))
58 (while (< i len)
59 (when (/= (aref data i) #xff)
60 (throw 'jfif nil))
61 (setq i (1+ i))
62 (when (>= (+ i 2) len)
63 (throw 'jfif nil))
64 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
65 (aref data (+ i 2))))
66 (code (aref data i)))
67 (when (and (>= code #xe0) (<= code #xef))
68 ;; APP0 LEN1 LEN2 "JFIF\0"
69 (throw 'jfif
70 (string-match "JFIF\\|Exif"
71 (substring data i (min (+ i nbytes) len)))))
72 (setq i (+ i 1 nbytes))))))))
73
74
75 ;;;###autoload
76 (defun image-type-from-data (data)
77 "Determine the image type from image data DATA.
78 Value is a symbol specifying the image type or nil if type cannot
79 be determined."
80 (let ((types image-type-regexps)
81 type)
82 (while (and types (null type))
83 (let ((regexp (car (car types)))
84 (image-type (cdr (car types))))
85 (when (or (and (symbolp image-type)
86 (string-match regexp data))
87 (and (consp image-type)
88 (funcall (car image-type) data)
89 (setq image-type (cdr image-type))))
90 (setq type image-type))
91 (setq types (cdr types))))
92 type))
93
94
95 ;;;###autoload
96 (defun image-type-from-file-header (file)
97 "Determine the type of image file FILE from its first few bytes.
98 Value is a symbol specifying the image type, or nil if type cannot
99 be determined."
100 (unless (file-name-directory file)
101 (setq file (expand-file-name file data-directory)))
102 (setq file (expand-file-name file))
103 (let ((header (with-temp-buffer
104 (set-buffer-multibyte nil)
105 (insert-file-contents-literally file nil 0 256)
106 (buffer-string))))
107 (image-type-from-data header)))
108
109
110 ;;;###autoload
111 (defun image-type-available-p (type)
112 "Return non-nil if image type TYPE is available.
113 Image types are symbols like `xbm' or `jpeg'."
114 (and (fboundp 'init-image-library)
115 (init-image-library type image-library-alist)))
116
117 ;;;###autoload
118 (defun create-image (file-or-data &optional type data-p &rest props)
119 "Create an image.
120 FILE-OR-DATA is an image file name or image data.
121 Optional TYPE is a symbol describing the image type. If TYPE is omitted
122 or nil, try to determine the image type from its first few bytes
123 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
124 use its file extension as image type.
125 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
126 Optional PROPS are additional image attributes to assign to the image,
127 like, e.g. `:mask MASK'.
128 Value is the image created, or nil if images of type TYPE are not supported."
129 (when (and (not data-p) (not (stringp file-or-data)))
130 (error "Invalid image file name `%s'" file-or-data))
131 (cond ((null data-p)
132 ;; FILE-OR-DATA is a file name.
133 (unless (or type
134 (setq type (image-type-from-file-header file-or-data)))
135 (let ((extension (file-name-extension file-or-data)))
136 (unless extension
137 (error "Cannot determine image type"))
138 (setq type (intern extension)))))
139 (t
140 ;; FILE-OR-DATA contains image data.
141 (unless type
142 (setq type (image-type-from-data file-or-data)))))
143 (unless type
144 (error "Cannot determine image type"))
145 (unless (symbolp type)
146 (error "Invalid image type `%s'" type))
147 (when (image-type-available-p type)
148 (append (list 'image :type type (if data-p :data :file) file-or-data)
149 props)))
150
151
152 ;;;###autoload
153 (defun put-image (image pos &optional string area)
154 "Put image IMAGE in front of POS in the current buffer.
155 IMAGE must be an image created with `create-image' or `defimage'.
156 IMAGE is displayed by putting an overlay into the current buffer with a
157 `before-string' STRING that has a `display' property whose value is the
158 image. STRING is defaulted if you omit it.
159 POS may be an integer or marker.
160 AREA is where to display the image. AREA nil or omitted means
161 display it in the text area, a value of `left-margin' means
162 display it in the left marginal area, a value of `right-margin'
163 means display it in the right marginal area."
164 (unless string (setq string "x"))
165 (let ((buffer (current-buffer)))
166 (unless (eq (car-safe image) 'image)
167 (error "Not an image: %s" image))
168 (unless (or (null area) (memq area '(left-margin right-margin)))
169 (error "Invalid area %s" area))
170 (setq string (copy-sequence string))
171 (let ((overlay (make-overlay pos pos buffer))
172 (prop (if (null area) image (list (list 'margin area) image))))
173 (put-text-property 0 (length string) 'display prop string)
174 (overlay-put overlay 'put-image t)
175 (overlay-put overlay 'before-string string))))
176
177
178 ;;;###autoload
179 (defun insert-image (image &optional string area slice)
180 "Insert IMAGE into current buffer at point.
181 IMAGE is displayed by inserting STRING into the current buffer
182 with a `display' property whose value is the image. STRING is
183 defaulted if you omit it.
184 AREA is where to display the image. AREA nil or omitted means
185 display it in the text area, a value of `left-margin' means
186 display it in the left marginal area, a value of `right-margin'
187 means display it in the right marginal area.
188 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
189 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
190 specifying the X and Y positions and WIDTH and HEIGHT of image area
191 to insert. A float value 0.0 - 1.0 means relative to the width or
192 height of the image; integer values are taken as pixel values."
193 ;; Use a space as least likely to cause trouble when it's a hidden
194 ;; character in the buffer.
195 (unless string (setq string " "))
196 (unless (eq (car-safe image) 'image)
197 (error "Not an image: %s" image))
198 (unless (or (null area) (memq area '(left-margin right-margin)))
199 (error "Invalid area %s" area))
200 (if area
201 (setq image (list (list 'margin area) image))
202 ;; Cons up a new spec equal but not eq to `image' so that
203 ;; inserting it twice in a row (adjacently) displays two copies of
204 ;; the image. Don't try to avoid this by looking at the display
205 ;; properties on either side so that we DTRT more often with
206 ;; cut-and-paste. (Yanking killed image text next to another copy
207 ;; of it loses anyway.)
208 (setq image (cons 'image (cdr image))))
209 (let ((start (point)))
210 (insert string)
211 (add-text-properties start (point)
212 `(display ,(if slice
213 (list (cons 'slice slice) image)
214 image) rear-nonsticky (display)))))
215
216
217 ;;;###autoload
218 (defun insert-sliced-image (image &optional string area rows cols)
219 "Insert IMAGE into current buffer at point.
220 IMAGE is displayed by inserting STRING into the current buffer
221 with a `display' property whose value is the image. STRING is
222 defaulted if you omit it.
223 AREA is where to display the image. AREA nil or omitted means
224 display it in the text area, a value of `left-margin' means
225 display it in the left marginal area, a value of `right-margin'
226 means display it in the right marginal area.
227 The image is automatically split into ROW x COLS slices."
228 (unless string (setq string " "))
229 (unless (eq (car-safe image) 'image)
230 (error "Not an image: %s" image))
231 (unless (or (null area) (memq area '(left-margin right-margin)))
232 (error "Invalid area %s" area))
233 (if area
234 (setq image (list (list 'margin area) image))
235 ;; Cons up a new spec equal but not eq to `image' so that
236 ;; inserting it twice in a row (adjacently) displays two copies of
237 ;; the image. Don't try to avoid this by looking at the display
238 ;; properties on either side so that we DTRT more often with
239 ;; cut-and-paste. (Yanking killed image text next to another copy
240 ;; of it loses anyway.)
241 (setq image (cons 'image (cdr image))))
242 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
243 (y 0.0) (dy (/ 1.0001 (or rows 1))))
244 (while (< y 1.0)
245 (while (< x 1.0)
246 (let ((start (point)))
247 (insert string)
248 (add-text-properties start (point)
249 `(display ,(list (list 'slice x y dx dy) image)
250 rear-nonsticky (display)))
251 (setq x (+ x dx))))
252 (setq x 0.0
253 y (+ y dy))
254 (insert (propertize "\n" 'line-height t)))))
255
256
257
258 ;;;###autoload
259 (defun remove-images (start end &optional buffer)
260 "Remove images between START and END in BUFFER.
261 Remove only images that were put in BUFFER with calls to `put-image'.
262 BUFFER nil or omitted means use the current buffer."
263 (unless buffer
264 (setq buffer (current-buffer)))
265 (let ((overlays (overlays-in start end)))
266 (while overlays
267 (let ((overlay (car overlays)))
268 (when (overlay-get overlay 'put-image)
269 (delete-overlay overlay)))
270 (setq overlays (cdr overlays)))))
271
272
273 ;;;###autoload
274 (defun find-image (specs)
275 "Find an image, choosing one of a list of image specifications.
276
277 SPECS is a list of image specifications.
278
279 Each image specification in SPECS is a property list. The contents of
280 a specification are image type dependent. All specifications must at
281 least contain the properties `:type TYPE' and either `:file FILE' or
282 `:data DATA', where TYPE is a symbol specifying the image type,
283 e.g. `xbm', FILE is the file to load the image from, and DATA is a
284 string containing the actual image data. The specification whose TYPE
285 is supported, and FILE exists, is used to construct the image
286 specification to be returned. Return nil if no specification is
287 satisfied.
288
289 The image is looked for first on `load-path' and then in `data-directory'."
290 (let (image)
291 (while (and specs (null image))
292 (let* ((spec (car specs))
293 (type (plist-get spec :type))
294 (data (plist-get spec :data))
295 (file (plist-get spec :file))
296 found)
297 (when (image-type-available-p type)
298 (cond ((stringp file)
299 (let ((path load-path))
300 (while (and (not found) path)
301 (let ((try-file (expand-file-name file (car path))))
302 (when (file-readable-p try-file)
303 (setq found try-file)))
304 (setq path (cdr path)))
305 (unless found
306 (let ((try-file (expand-file-name file data-directory)))
307 (if (file-readable-p try-file)
308 (setq found try-file))))
309 (if found
310 (setq image
311 (cons 'image (plist-put (copy-sequence spec)
312 :file found))))))
313 ((not (null data))
314 (setq image (cons 'image spec)))))
315 (setq specs (cdr specs))))
316 image))
317
318
319 ;;;###autoload
320 (defmacro defimage (symbol specs &optional doc)
321 "Define SYMBOL as an image.
322
323 SPECS is a list of image specifications. DOC is an optional
324 documentation string.
325
326 Each image specification in SPECS is a property list. The contents of
327 a specification are image type dependent. All specifications must at
328 least contain the properties `:type TYPE' and either `:file FILE' or
329 `:data DATA', where TYPE is a symbol specifying the image type,
330 e.g. `xbm', FILE is the file to load the image from, and DATA is a
331 string containing the actual image data. The first image
332 specification whose TYPE is supported, and FILE exists, is used to
333 define SYMBOL.
334
335 Example:
336
337 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
338 (:type xbm :file \"~/test1.xbm\")))"
339 `(defvar ,symbol (find-image ',specs) ,doc))
340
341
342 (provide 'image)
343
344 ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
345 ;;; image.el ends here