*** empty log message ***
[emacs.git] / lisp / image.el
blobced73c8fb3ab264cc622e2d1f5f053383771f441
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 99, 2000, 01, 04 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: multimedia
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;;; Code:
30 (defgroup image ()
31 "Image support."
32 :group 'multimedia)
35 (defconst image-type-regexps
36 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
37 ("\\`P[1-6]" . pbm)
38 ("\\`GIF8" . gif)
39 ("\\`\211PNG\r\n" . png)
40 ("\\`[\t\n\r ]*#define" . xbm)
41 ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
42 ("\\`[\t\n\r ]*%!PS" . postscript)
43 ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
44 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
45 When the first bytes of an image file match REGEXP, it is assumed to
46 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
47 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
48 with one argument, a string containing the image data. If PREDICATE returns
49 a non-nil value, TYPE is the image's type.")
51 ;;;###autoload
52 (defvar image-library-alist nil
53 "Alist of image types vs external libraries needed to display them.
55 Each element is a list (IMAGE-TYPE LIBRARY...), where the car is a symbol
56 representing a supported image type, and the rest are strings giving
57 alternate filenames for the corresponding external libraries.
59 Emacs tries to load the libraries in the order they appear on the
60 list; if none is loaded, the running session of Emacs won't
61 support the image type. Types 'pbm and 'xbm don't need to be
62 listed; they're always supported.")
63 ;;;###autoload (put 'image-library-alist 'risky-local-variable t)
65 (defun image-jpeg-p (data)
66 "Value is non-nil if DATA, a string, consists of JFIF image data.
67 We accept the tag Exif because that is the same format."
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)
78 (aref data (+ i 2))))
79 (code (aref data i)))
80 (when (and (>= code #xe0) (<= code #xef))
81 ;; APP0 LEN1 LEN2 "JFIF\0"
82 (throw 'jfif
83 (string-match "JFIF\\|Exif"
84 (substring data i (min (+ i nbytes) len)))))
85 (setq i (+ i 1 nbytes))))))))
88 ;;;###autoload
89 (defun image-type-from-data (data)
90 "Determine the image type from image data DATA.
91 Value is a symbol specifying the image type or nil if type cannot
92 be determined."
93 (let ((types image-type-regexps)
94 type)
95 (while (and types (null type))
96 (let ((regexp (car (car types)))
97 (image-type (cdr (car types))))
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))))
103 (setq type image-type))
104 (setq types (cdr types))))
105 type))
108 ;;;###autoload
109 (defun image-type-from-file-header (file)
110 "Determine the type of image file FILE from its first few bytes.
111 Value is a symbol specifying the image type, or nil if type cannot
112 be 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
117 (set-buffer-multibyte nil)
118 (insert-file-contents-literally file nil 0 256)
119 (buffer-string))))
120 (image-type-from-data header)))
123 ;;;###autoload
124 (defun image-type-available-p (type)
125 "Value is non-nil if image type TYPE is available.
126 Image types are symbols like `xbm' or `jpeg'."
127 (and (fboundp 'init-image-library)
128 (init-image-library type image-library-alist)))
130 ;;;###autoload
131 (defun create-image (file-or-data &optional type data-p &rest props)
132 "Create an image.
133 FILE-OR-DATA is an image file name or image data.
134 Optional TYPE is a symbol describing the image type. If TYPE is omitted
135 or nil, try to determine the image type from its first few bytes
136 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
137 use its file extension as image type.
138 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
139 Optional PROPS are additional image attributes to assign to the image,
140 like, e.g. `:mask MASK'.
141 Value is the image created, or nil if images of type TYPE are not supported."
142 (when (and (not data-p) (not (stringp file-or-data)))
143 (error "Invalid image file name `%s'" file-or-data))
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)))))
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"))
158 (unless (symbolp type)
159 (error "Invalid image type `%s'" type))
160 (when (image-type-available-p type)
161 (append (list 'image :type type (if data-p :data :file) file-or-data)
162 props)))
165 ;;;###autoload
166 (defun put-image (image pos &optional string area)
167 "Put image IMAGE in front of POS in the current buffer.
168 IMAGE must be an image created with `create-image' or `defimage'.
169 IMAGE 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
171 image. STRING is defaulted if you omit it.
172 POS may be an integer or marker.
173 AREA is where to display the image. AREA nil or omitted means
174 display it in the text area, a value of `left-margin' means
175 display it in the left marginal area, a value of `right-margin'
176 means display it in the right marginal area."
177 (unless string (setq string "x"))
178 (let ((buffer (current-buffer)))
179 (unless (eq (car-safe image) 'image)
180 (error "Not an image: %s" image))
181 (unless (or (null area) (memq area '(left-margin right-margin)))
182 (error "Invalid area %s" area))
183 (setq string (copy-sequence string))
184 (let ((overlay (make-overlay pos pos buffer))
185 (prop (if (null area) image (list (list 'margin area) image))))
186 (put-text-property 0 (length string) 'display prop string)
187 (overlay-put overlay 'put-image t)
188 (overlay-put overlay 'before-string string))))
191 ;;;###autoload
192 (defun insert-image (image &optional string area slice)
193 "Insert IMAGE into current buffer at point.
194 IMAGE is displayed by inserting STRING into the current buffer
195 with a `display' property whose value is the image. STRING is
196 defaulted if you omit it.
197 AREA is where to display the image. AREA nil or omitted means
198 display it in the text area, a value of `left-margin' means
199 display it in the left marginal area, a value of `right-margin'
200 means display it in the right marginal area.
201 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
202 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
203 specifying the X and Y positions and WIDTH and HEIGHT of image area
204 to insert. A float value 0.0 - 1.0 means relative to the width or
205 height of the image; integer values are taken as pixel values."
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 " "))
209 (unless (eq (car-safe image) 'image)
210 (error "Not an image: %s" image))
211 (unless (or (null area) (memq area '(left-margin right-margin)))
212 (error "Invalid area %s" area))
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))))
222 (let ((start (point)))
223 (insert string)
224 (add-text-properties start (point)
225 `(display ,(if slice
226 (list (cons 'slice slice) image)
227 image) rear-nonsticky (display)))))
230 ;;;###autoload
231 (defun insert-sliced-image (image &optional string area rows cols)
232 "Insert IMAGE into current buffer at point.
233 IMAGE is displayed by inserting STRING into the current buffer
234 with a `display' property whose value is the image. STRING is
235 defaulted if you omit it.
236 AREA is where to display the image. AREA nil or omitted means
237 display it in the text area, a value of `left-margin' means
238 display it in the left marginal area, a value of `right-margin'
239 means display it in the right marginal area.
240 The image is automatically split into ROW x COLS slices."
241 (unless string (setq string " "))
242 (unless (eq (car-safe image) 'image)
243 (error "Not an image: %s" image))
244 (unless (or (null area) (memq area '(left-margin right-margin)))
245 (error "Invalid area %s" area))
246 (if area
247 (setq image (list (list 'margin area) image))
248 ;; Cons up a new spec equal but not eq to `image' so that
249 ;; inserting it twice in a row (adjacently) displays two copies of
250 ;; the image. Don't try to avoid this by looking at the display
251 ;; properties on either side so that we DTRT more often with
252 ;; cut-and-paste. (Yanking killed image text next to another copy
253 ;; of it loses anyway.)
254 (setq image (cons 'image (cdr image))))
255 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
256 (y 0.0) (dy (/ 1.0001 (or rows 1))))
257 (while (< y 1.0)
258 (while (< x 1.0)
259 (let ((start (point)))
260 (insert string)
261 (add-text-properties start (point)
262 `(display ,(list (list 'slice x y dx dy) image)
263 rear-nonsticky (display)))
264 (setq x (+ x dx))))
265 (setq x 0.0
266 y (+ y dy))
267 (insert (propertize "\n" 'line-height t)))))
271 ;;;###autoload
272 (defun remove-images (start end &optional buffer)
273 "Remove images between START and END in BUFFER.
274 Remove only images that were put in BUFFER with calls to `put-image'.
275 BUFFER nil or omitted means use the current buffer."
276 (unless buffer
277 (setq buffer (current-buffer)))
278 (let ((overlays (overlays-in start end)))
279 (while overlays
280 (let ((overlay (car overlays)))
281 (when (overlay-get overlay 'put-image)
282 (delete-overlay overlay)))
283 (setq overlays (cdr overlays)))))
286 ;;;###autoload
287 (defun find-image (specs)
288 "Find an image, choosing one of a list of image specifications.
290 SPECS is a list of image specifications.
292 Each image specification in SPECS is a property list. The contents of
293 a specification are image type dependent. All specifications must at
294 least contain the properties `:type TYPE' and either `:file FILE' or
295 `:data DATA', where TYPE is a symbol specifying the image type,
296 e.g. `xbm', FILE is the file to load the image from, and DATA is a
297 string containing the actual image data. The specification whose TYPE
298 is supported, and FILE exists, is used to construct the image
299 specification to be returned. Return nil if no specification is
300 satisfied.
302 The image is looked for first on `load-path' and then in `data-directory'."
303 (let (image)
304 (while (and specs (null image))
305 (let* ((spec (car specs))
306 (type (plist-get spec :type))
307 (data (plist-get spec :data))
308 (file (plist-get spec :file))
309 found)
310 (when (image-type-available-p type)
311 (cond ((stringp file)
312 (let ((path load-path))
313 (while (and (not found) path)
314 (let ((try-file (expand-file-name file (car path))))
315 (when (file-readable-p try-file)
316 (setq found try-file)))
317 (setq path (cdr path)))
318 (unless found
319 (let ((try-file (expand-file-name file data-directory)))
320 (if (file-readable-p try-file)
321 (setq found try-file))))
322 (if found
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))
332 ;;;###autoload
333 (defmacro defimage (symbol specs &optional doc)
334 "Define SYMBOL as an image.
336 SPECS is a list of image specifications. DOC is an optional
337 documentation string.
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.
348 Example:
350 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
351 (:type xbm :file \"~/test1.xbm\")))"
352 `(defvar ,symbol (find-image ',specs) ,doc))
355 (provide 'image)
357 ;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
358 ;;; image.el ends here