1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Keywords: multimedia
6 ;; This file is part of GNU Emacs.
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)
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.
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.
27 (defconst image-type-regexps
28 '(("^/\\*.*XPM.\\*/" . xpm
)
32 ("^\211PNG\r\n" . png
)
34 ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff
)
35 ("^%!PS" . ghostscript
))
36 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
37 When the first bytes of an image file match REGEXP, it is assumed to
38 be of image type IMAGE-TYPE.")
42 (defun image-type-from-data (data)
43 "Determine the image type from image data DATA.
44 Value is a symbol specifying the image type or nil if type cannot
46 (let ((types image-type-regexps
)
48 (while (and types
(null type
))
49 (let ((regexp (car (car types
)))
50 (image-type (cdr (car types
))))
51 (when (string-match regexp data
)
52 (setq type image-type
))
53 (setq types
(cdr types
))))
58 (defun image-type-from-file-header (file)
59 "Determine the type of image file FILE from its first few bytes.
60 Value is a symbol specifying the image type, or nil if type cannot
62 (unless (file-name-directory file
)
63 (setq file
(expand-file-name file data-directory
)))
64 (setq file
(expand-file-name file
))
65 (let ((header (with-temp-buffer
66 (insert-file-contents-literally file nil
0 256)
68 (image-type-from-data header
)))
72 (defun image-type-available-p (type)
73 "Value is non-nil if image type TYPE is available.
74 Image types are symbols like `xbm' or `jpeg'."
75 (and (boundp 'image-types
) (not (null (memq type image-types
)))))
79 (defun create-image (file-or-data &optional type data-p
&rest props
)
81 FILE-OR-DATA is an image file name or image data.
82 Optional TYPE is a symbol describing the image type. If TYPE is omitted
83 or nil, try to determine the image type from its first few bytes
84 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
85 use its file extension.as image type.
86 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
87 Optional PROPS are additional image attributes to assign to the image,
88 like, e.g. `:heuristic-mask t'.
89 Value is the image created, or nil if images of type TYPE are not supported."
90 (when (and (not data-p
) (not (stringp file-or-data
)))
91 (error "Invalid image file name `%s'" file-or-data
))
93 ;; FILE-OR-DATA is a file name.
95 (setq type
(image-type-from-file-header file-or-data
)))
96 (let ((extension (file-name-extension file-or-data
)))
98 (error "Cannot determine image type"))
99 (setq type
(intern extension
)))))
101 ;; FILE-OR-DATA contains image data.
103 (setq type
(image-type-from-data file-or-data
)))))
105 (error "Cannot determine image type"))
106 (unless (symbolp type
)
107 (error "Invalid image type `%s'" type
))
108 (when (image-type-available-p type
)
109 (append (list 'image
:type type
(if data-p
:data
:file
) file-or-data
)
114 (defun put-image (image pos
&optional string area
)
115 "Put image IMAGE in front of POS in the current buffer.
116 IMAGE must be an image created with `create-image' or `defimage'.
117 IMAGE is displayed by putting an overlay into the current buffer with a
118 `before-string' STRING that has a `display' property whose value is the
119 image. STRING is defaulted if you omit it.
120 POS may be an integer or marker.
121 AREA is where to display the image. AREA nil or omitted means
122 display it in the text area, a value of `left-margin' means
123 display it in the left marginal area, a value of `right-margin'
124 means display it in the right marginal area."
125 (unless string
(setq string
"x"))
126 (let ((buffer (current-buffer)))
127 (unless (eq (car-safe image
) 'image
)
128 (error "Not an image: %s" image
))
129 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
130 (error "Invalid area %s" area
))
131 (setq string
(copy-sequence string
))
132 (let ((overlay (make-overlay pos pos buffer
))
133 (prop (if (null area
) image
(list (list 'margin area
) image
))))
134 (put-text-property 0 (length string
) 'display prop string
)
135 (overlay-put overlay
'put-image t
)
136 (overlay-put overlay
'before-string string
))))
140 (defun insert-image (image &optional string area
)
141 "Insert IMAGE into current buffer at point.
142 IMAGE is displayed by inserting STRING into the current buffer
143 with a `display' property whose value is the image. STRING is
144 defaulted if you omit it.
145 AREA is where to display the image. AREA nil or omitted means
146 display it in the text area, a value of `left-margin' means
147 display it in the left marginal area, a value of `right-margin'
148 means display it in the right marginal area."
149 ;; Use a space as least likely to cause trouble when it's a hidden
150 ;; character in the buffer.
151 (unless string
(setq string
" "))
152 (unless (eq (car-safe image
) 'image
)
153 (error "Not an image: %s" image
))
154 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
155 (error "Invalid area %s" area
))
157 (setq image
(list (list 'margin area
) image
))
158 ;; Cons up a new spec equal but not eq to `image' so that
159 ;; inserting it twice in a row (adjacently) displays two copies of
160 ;; the image. Don't try to avoid this by looking at the display
161 ;; properties on either side so that we DTRT more often with
162 ;; cut-and-paste. (Yanking killed image text next to another copy
163 ;; of it loses anyway.)
164 (setq image
(cons 'image
(cdr image
))))
165 (let ((start (point)))
167 (add-text-properties start
(point)
169 ;; `image' has the right properties to
170 ;; mark an intangible field.
172 'rear-nonsticky
(list 'display
)))))
176 (defun remove-images (start end
&optional buffer
)
177 "Remove images between START and END in BUFFER.
178 Remove only images that were put in BUFFER with calls to `put-image'.
179 BUFFER nil or omitted means use the current buffer."
181 (setq buffer
(current-buffer)))
182 (let ((overlays (overlays-in start end
)))
184 (let ((overlay (car overlays
)))
185 (when (overlay-get overlay
'put-image
)
186 (delete-overlay overlay
)))
187 (setq overlays
(cdr overlays
)))))
191 (defun find-image (specs)
192 "Find an image, choosing one of a list of image specifications.
194 SPECS is a list of image specifications.
196 Each image specification in SPECS is a property list. The contents of
197 a specification are image type dependent. All specifications must at
198 least contain the properties `:type TYPE' and either `:file FILE' or
199 `:data DATA', where TYPE is a symbol specifying the image type,
200 e.g. `xbm', FILE is the file to load the image from, and DATA is a
201 string containing the actual image data. The specification whose TYPE
202 is supported, and FILE exists, is used to construct the image
203 specification to be returned. Return nil if no specification is
206 The image is looked for first on `load-path' and then in `data-directory'."
208 (while (and specs
(null image
))
209 (let* ((spec (car specs
))
210 (type (plist-get spec
:type
))
211 (data (plist-get spec
:data
))
212 (file (plist-get spec
:file
))
214 (when (image-type-available-p type
)
215 (cond ((stringp file
)
216 (let ((path load-path
))
217 (while (and (not found
) path
)
218 (let ((try-file (expand-file-name file
(car path
))))
219 (when (file-readable-p try-file
)
220 (setq found try-file
)))
221 (setq path
(cdr path
)))
223 (let ((try-file (expand-file-name file data-directory
)))
224 (if (file-readable-p try-file
)
225 (setq found try-file
))))
228 (cons 'image
(plist-put spec
:file found
))))))
230 (setq image
(cons 'image spec
)))))
231 (setq specs
(cdr specs
))))
236 (defmacro defimage
(symbol specs
&optional doc
)
237 "Define SYMBOL as an image.
239 SPECS is a list of image specifications. DOC is an optional
240 documentation string.
242 Each image specification in SPECS is a property list. The contents of
243 a specification are image type dependent. All specifications must at
244 least contain the properties `:type TYPE' and either `:file FILE' or
245 `:data DATA', where TYPE is a symbol specifying the image type,
246 e.g. `xbm', FILE is the file to load the image from, and DATA is a
247 string containing the actual image data. The first image
248 specification whose TYPE is supported, and FILE exists, is used to
253 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
254 (:type xbm :file \"~/test1.xbm\")))"
255 `(defvar ,symbol
(find-image ',specs
) ,doc
))
260 ;;; image.el ends here