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