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