(dired-readin): Locally bind file-name-coding-system.
[bpt/emacs.git] / lisp / image.el
CommitLineData
7840ced1
GM
1;;; image.el --- image API
2
0d30b337 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
aaef169d 4;; 2004, 2005, 2006 Free Software Foundation, Inc.
634f4a67
PJ
5
6;; Maintainer: FSF
04799cf5 7;; Keywords: multimedia
7840ced1
GM
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
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
7840ced1
GM
25
26;;; Commentary:
27
28;;; Code:
29
77f6105c
MB
30
31(defgroup image ()
32 "Image support."
33 :group 'multimedia)
34
35
4fde92ef 36(defconst image-type-header-regexps
3bdbead3 37 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
69ebef1d
GM
38 ("\\`P[1-6]" . pbm)
39 ("\\`GIF8" . gif)
69ebef1d 40 ("\\`\211PNG\r\n" . png)
3bdbead3 41 ("\\`[\t\n\r ]*#define" . xbm)
6ea3db8a 42 ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
3bdbead3 43 ("\\`[\t\n\r ]*%!PS" . postscript)
8a8ef149 44 ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
7840ced1
GM
45 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
46When the first bytes of an image file match REGEXP, it is assumed to
8a8ef149
GM
47be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
48IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
49with one argument, a string containing the image data. If PREDICATE returns
37d4a06e 50a non-nil value, TYPE is the image's type.")
8a8ef149 51
4fde92ef
KS
52(defconst image-type-file-name-regexps
53 '(("\\.png\\'" . png)
54 ("\\.gif\\'" . gif)
55 ("\\.jpe?g\\'" . jpeg)
56 ("\\.bmp\\'" . bmp)
57 ("\\.xpm\\'" . xpm)
58 ("\\.pbm\\'" . pbm)
59 ("\\.xbm\\'" . xbm)
60 ("\\.ps\\'" . postscript)
61 ("\\.tiff?\\'" . tiff))
62 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
63When the name of an image file match REGEXP, it is assumed to
64be of image type IMAGE-TYPE.")
65
66
dada660a 67(defvar image-load-path nil
5fc5ac38 68 "List of locations in which to search for image files.
93a75651
CY
69If an element is a string, it defines a directory to search.
70If an element is a variable symbol whose value is a string, that
71value defines a directory to search.
72If an element is a variable symbol whose value is a list, the
73value is used as a list of directories to search.")
5fc5ac38 74
dada660a
RS
75(eval-at-startup
76 (setq image-load-path
77 (list (file-name-as-directory (expand-file-name "images" data-directory))
78 'data-directory 'load-path)))
79
5248a565 80
20c65d08 81(defun image-load-path-for-library (library image &optional path no-error)
ae77c7ff 82 "Return a suitable search path for images used by LIBRARY.
7c565097 83
40db64d2 84It searches for IMAGE in `image-load-path' (excluding
c0696e1b
BW
85\"`data-directory'/images\") and `load-path', followed by a path
86suitable for LIBRARY, which includes \"../../etc/images\" and
87\"../etc/images\" relative to the library file itself, and then
88in \"`data-directory'/images\".
7c565097 89
5248a565
BW
90Then this function returns a list of directories which contains
91first the directory in which IMAGE was found, followed by the
92value of `load-path'. If PATH is given, it is used instead of
93`load-path'.
20c65d08 94
5248a565
BW
95If NO-ERROR is non-nil and a suitable path can't be found, don't
96signal an error. Instead, return a list of directories as before,
97except that nil appears in place of the image directory.
7c565097
BW
98
99Here is an example that uses a common idiom to provide
100compatibility with versions of Emacs that lack the variable
101`image-load-path':
102
9f036d33
BW
103 ;; Shush compiler.
104 (defvar image-load-path)
5248a565
BW
105
106 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
9f036d33
BW
107 (image-load-path (cons (car load-path)
108 (when (boundp 'image-load-path)
109 image-load-path))))
5248a565 110 (mh-tool-bar-folder-buttons-init))"
7c565097
BW
111 (unless library (error "No library specified"))
112 (unless image (error "No image specified"))
c0696e1b
BW
113 (let (image-directory image-directory-load-path)
114 ;; Check for images in image-load-path or load-path.
115 (let ((img image)
116 (dir (or
117 ;; Images in image-load-path.
118 (image-search-load-path image)
119 ;; Images in load-path.
120 (locate-library image)))
121 parent)
122 ;; Since the image might be in a nested directory (for
123 ;; example, mail/attach.pbm), adjust `image-directory'
124 ;; accordingly.
125 (when dir
126 (setq dir (file-name-directory dir))
127 (while (setq parent (file-name-directory img))
128 (setq img (directory-file-name parent)
129 dir (expand-file-name "../" dir))))
130 (setq image-directory-load-path dir))
131
132 ;; If `image-directory-load-path' isn't Emacs' image directory,
133 ;; it's probably a user preference, so use it. Then use a
134 ;; relative setting if possible; otherwise, use
135 ;; `image-directory-load-path'.
7c565097 136 (cond
c0696e1b
BW
137 ;; User-modified image-load-path?
138 ((and image-directory-load-path
139 (not (equal image-directory-load-path
140 (file-name-as-directory
141 (expand-file-name "images" data-directory)))))
142 (setq image-directory image-directory-load-path))
7c565097
BW
143 ;; Try relative setting.
144 ((let (library-name d1ei d2ei)
145 ;; First, find library in the load-path.
146 (setq library-name (locate-library library))
147 (if (not library-name)
148 (error "Cannot find library %s in load-path" library))
149 ;; And then set image-directory relative to that.
150 (setq
151 ;; Go down 2 levels.
c0696e1b
BW
152 d2ei (file-name-as-directory
153 (expand-file-name
154 (concat (file-name-directory library-name) "../../etc/images")))
7c565097 155 ;; Go down 1 level.
c0696e1b
BW
156 d1ei (file-name-as-directory
157 (expand-file-name
158 (concat (file-name-directory library-name) "../etc/images"))))
7c565097
BW
159 (setq image-directory
160 ;; Set it to nil if image is not found.
161 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
162 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
c0696e1b
BW
163 ;; Use Emacs' image directory.
164 (image-directory-load-path
165 (setq image-directory image-directory-load-path))
20c65d08 166 (no-error
20c65d08 167 (message "Could not find image %s for library %s" image library))
7c565097
BW
168 (t
169 (error "Could not find image %s for library %s" image library)))
170
5248a565
BW
171 ;; Return an augmented `path' or `load-path'.
172 (nconc (list image-directory)
173 (delete image-directory (copy-sequence (or path load-path))))))
174
7c565097 175
8a8ef149 176(defun image-jpeg-p (data)
9b78a6a3
RS
177 "Value is non-nil if DATA, a string, consists of JFIF image data.
178We accept the tag Exif because that is the same format."
8a8ef149
GM
179 (when (string-match "\\`\xff\xd8" data)
180 (catch 'jfif
181 (let ((len (length data)) (i 2))
182 (while (< i len)
183 (when (/= (aref data i) #xff)
184 (throw 'jfif nil))
185 (setq i (1+ i))
186 (when (>= (+ i 2) len)
187 (throw 'jfif nil))
188 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
4a9bf8a4
GM
189 (aref data (+ i 2))))
190 (code (aref data i)))
191 (when (and (>= code #xe0) (<= code #xef))
8a8ef149 192 ;; APP0 LEN1 LEN2 "JFIF\0"
71296446 193 (throw 'jfif
32b3c766
JPW
194 (string-match "JFIF\\|Exif"
195 (substring data i (min (+ i nbytes) len)))))
3bdbead3 196 (setq i (+ i 1 nbytes))))))))
7840ced1
GM
197
198
199;;;###autoload
162dec01
GM
200(defun image-type-from-data (data)
201 "Determine the image type from image data DATA.
202Value is a symbol specifying the image type or nil if type cannot
7840ced1 203be determined."
4fde92ef 204 (let ((types image-type-header-regexps)
7840ced1 205 type)
4fde92ef 206 (while types
7840ced1
GM
207 (let ((regexp (car (car types)))
208 (image-type (cdr (car types))))
4fde92ef
KS
209 (if (or (and (symbolp image-type)
210 (string-match regexp data))
211 (and (consp image-type)
212 (funcall (car image-type) data)
213 (setq image-type (cdr image-type))))
214 (setq type image-type
215 types nil)
216 (setq types (cdr types)))))
217 type))
218
219
220;;;###autoload
221(defun image-type-from-buffer ()
222 "Determine the image type from data in the current buffer.
223Value is a symbol specifying the image type or nil if type cannot
224be determined."
225 (let ((types image-type-header-regexps)
226 type
227 (opoint (point)))
228 (goto-char (point-min))
229 (while types
230 (let ((regexp (car (car types)))
231 (image-type (cdr (car types)))
232 data)
233 (if (or (and (symbolp image-type)
234 (looking-at regexp))
235 (and (consp image-type)
236 (funcall (car image-type)
237 (or data
238 (setq data
239 (buffer-substring
240 (point-min)
241 (min (point-max)
242 (+ (point-min) 256))))))
243 (setq image-type (cdr image-type))))
244 (setq type image-type
245 types nil)
246 (setq types (cdr types)))))
247 (goto-char opoint)
7840ced1
GM
248 type))
249
250
162dec01
GM
251;;;###autoload
252(defun image-type-from-file-header (file)
253 "Determine the type of image file FILE from its first few bytes.
254Value is a symbol specifying the image type, or nil if type cannot
255be determined."
4fde92ef
KS
256 (unless (or (file-readable-p file)
257 (file-name-absolute-p file))
258 (setq file (image-search-load-path file)))
259 (and file
260 (file-readable-p file)
261 (with-temp-buffer
262 (set-buffer-multibyte nil)
263 (insert-file-contents-literally file nil 0 256)
264 (image-type-from-buffer))))
265
266
267;;;###autoload
268(defun image-type-from-file-name (file)
269 "Determine the type of image file FILE from its name.
270Value is a symbol specifying the image type, or nil if type cannot
271be determined."
272 (let ((types image-type-file-name-regexps)
273 type)
274 (while types
275 (if (string-match (car (car types)) file)
276 (setq type (cdr (car types))
277 types nil)
278 (setq types (cdr types))))
279 type))
162dec01
GM
280
281
7840ced1 282;;;###autoload
e7968373
KS
283(defun image-type (file-or-data &optional type data-p)
284 "Determine and return image type.
162dec01 285FILE-OR-DATA is an image file name or image data.
7840ced1 286Optional TYPE is a symbol describing the image type. If TYPE is omitted
162dec01
GM
287or nil, try to determine the image type from its first few bytes
288of image data. If that doesn't work, and FILE-OR-DATA is a file name,
9ea8de1b 289use its file extension as image type.
e7968373 290Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data."
f64ae86c
GM
291 (when (and (not data-p) (not (stringp file-or-data)))
292 (error "Invalid image file name `%s'" file-or-data))
162dec01
GM
293 (cond ((null data-p)
294 ;; FILE-OR-DATA is a file name.
295 (unless (or type
296 (setq type (image-type-from-file-header file-or-data)))
297 (let ((extension (file-name-extension file-or-data)))
298 (unless extension
299 (error "Cannot determine image type"))
300 (setq type (intern extension)))))
301 (t
302 ;; FILE-OR-DATA contains image data.
303 (unless type
304 (setq type (image-type-from-data file-or-data)))))
305 (unless type
306 (error "Cannot determine image type"))
7840ced1 307 (unless (symbolp type)
162dec01 308 (error "Invalid image type `%s'" type))
e7968373
KS
309 type)
310
311;;;###autoload
312(defun image-type-available-p (type)
313 "Return non-nil if image type TYPE is available.
314Image types are symbols like `xbm' or `jpeg'."
315 (and (fboundp 'init-image-library)
316 (init-image-library type image-library-alist)))
317
318
319;;;###autoload
320(defun create-image (file-or-data &optional type data-p &rest props)
321 "Create an image.
322FILE-OR-DATA is an image file name or image data.
323Optional TYPE is a symbol describing the image type. If TYPE is omitted
324or nil, try to determine the image type from its first few bytes
325of image data. If that doesn't work, and FILE-OR-DATA is a file name,
326use its file extension as image type.
327Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
328Optional PROPS are additional image attributes to assign to the image,
329like, e.g. `:mask MASK'.
330Value is the image created, or nil if images of type TYPE are not supported.
331
332Images should not be larger than specified by `max-image-size'."
333 (setq type (image-type file-or-data type data-p))
7840ced1 334 (when (image-type-available-p type)
162dec01
GM
335 (append (list 'image :type type (if data-p :data :file) file-or-data)
336 props)))
7840ced1
GM
337
338
339;;;###autoload
cedbd8d0 340(defun put-image (image pos &optional string area)
1571b5ff 341 "Put image IMAGE in front of POS in the current buffer.
7840ced1 342IMAGE must be an image created with `create-image' or `defimage'.
46655c94
GM
343IMAGE is displayed by putting an overlay into the current buffer with a
344`before-string' STRING that has a `display' property whose value is the
cedbd8d0 345image. STRING is defaulted if you omit it.
7840ced1 346POS may be an integer or marker.
7840ced1
GM
347AREA is where to display the image. AREA nil or omitted means
348display it in the text area, a value of `left-margin' means
349display it in the left marginal area, a value of `right-margin'
46655c94 350means display it in the right marginal area."
0ad550ba 351 (unless string (setq string "x"))
1571b5ff 352 (let ((buffer (current-buffer)))
cedbd8d0 353 (unless (eq (car-safe image) 'image)
1571b5ff
GM
354 (error "Not an image: %s" image))
355 (unless (or (null area) (memq area '(left-margin right-margin)))
356 (error "Invalid area %s" area))
46655c94 357 (setq string (copy-sequence string))
1571b5ff 358 (let ((overlay (make-overlay pos pos buffer))
46655c94
GM
359 (prop (if (null area) image (list (list 'margin area) image))))
360 (put-text-property 0 (length string) 'display prop string)
1571b5ff
GM
361 (overlay-put overlay 'put-image t)
362 (overlay-put overlay 'before-string string))))
7840ced1
GM
363
364
365;;;###autoload
5af275e0 366(defun insert-image (image &optional string area slice)
7840ced1 367 "Insert IMAGE into current buffer at point.
46655c94 368IMAGE is displayed by inserting STRING into the current buffer
cedbd8d0
DL
369with a `display' property whose value is the image. STRING is
370defaulted if you omit it.
7840ced1
GM
371AREA is where to display the image. AREA nil or omitted means
372display it in the text area, a value of `left-margin' means
373display it in the left marginal area, a value of `right-margin'
5af275e0
KS
374means display it in the right marginal area.
375SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
376means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
377specifying the X and Y positions and WIDTH and HEIGHT of image area
378to insert. A float value 0.0 - 1.0 means relative to the width or
379height of the image; integer values are taken as pixel values."
0ad550ba
DL
380 ;; Use a space as least likely to cause trouble when it's a hidden
381 ;; character in the buffer.
382 (unless string (setq string " "))
cedbd8d0 383 (unless (eq (car-safe image) 'image)
7840ced1
GM
384 (error "Not an image: %s" image))
385 (unless (or (null area) (memq area '(left-margin right-margin)))
386 (error "Invalid area %s" area))
0dc91c57
DL
387 (if area
388 (setq image (list (list 'margin area) image))
389 ;; Cons up a new spec equal but not eq to `image' so that
390 ;; inserting it twice in a row (adjacently) displays two copies of
391 ;; the image. Don't try to avoid this by looking at the display
392 ;; properties on either side so that we DTRT more often with
393 ;; cut-and-paste. (Yanking killed image text next to another copy
394 ;; of it loses anyway.)
395 (setq image (cons 'image (cdr image))))
46655c94
GM
396 (let ((start (point)))
397 (insert string)
398 (add-text-properties start (point)
5af275e0
KS
399 `(display ,(if slice
400 (list (cons 'slice slice) image)
401 image) rear-nonsticky (display)))))
402
403
f22c36d3 404;;;###autoload
5af275e0 405(defun insert-sliced-image (image &optional string area rows cols)
b48bc937
KS
406 "Insert IMAGE into current buffer at point.
407IMAGE is displayed by inserting STRING into the current buffer
408with a `display' property whose value is the image. STRING is
409defaulted if you omit it.
410AREA is where to display the image. AREA nil or omitted means
411display it in the text area, a value of `left-margin' means
412display it in the left marginal area, a value of `right-margin'
413means display it in the right marginal area.
414The image is automatically split into ROW x COLS slices."
5af275e0
KS
415 (unless string (setq string " "))
416 (unless (eq (car-safe image) 'image)
417 (error "Not an image: %s" image))
418 (unless (or (null area) (memq area '(left-margin right-margin)))
419 (error "Invalid area %s" area))
420 (if area
421 (setq image (list (list 'margin area) image))
422 ;; Cons up a new spec equal but not eq to `image' so that
423 ;; inserting it twice in a row (adjacently) displays two copies of
424 ;; the image. Don't try to avoid this by looking at the display
425 ;; properties on either side so that we DTRT more often with
426 ;; cut-and-paste. (Yanking killed image text next to another copy
427 ;; of it loses anyway.)
428 (setq image (cons 'image (cdr image))))
429 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
430 (y 0.0) (dy (/ 1.0001 (or rows 1))))
431 (while (< y 1.0)
432 (while (< x 1.0)
433 (let ((start (point)))
434 (insert string)
435 (add-text-properties start (point)
436 `(display ,(list (list 'slice x y dx dy) image)
437 rear-nonsticky (display)))
438 (setq x (+ x dx))))
439 (setq x 0.0
440 y (+ y dy))
82dc617b 441 (insert (propertize "\n" 'line-height t)))))
5af275e0 442
10e2102e 443
7840ced1
GM
444
445;;;###autoload
446(defun remove-images (start end &optional buffer)
447 "Remove images between START and END in BUFFER.
448Remove only images that were put in BUFFER with calls to `put-image'.
449BUFFER nil or omitted means use the current buffer."
450 (unless buffer
451 (setq buffer (current-buffer)))
452 (let ((overlays (overlays-in start end)))
453 (while overlays
454 (let ((overlay (car overlays)))
455 (when (overlay-get overlay 'put-image)
0c898dd9
DL
456 (delete-overlay overlay)))
457 (setq overlays (cdr overlays)))))
7840ced1 458
4fde92ef
KS
459(defun image-search-load-path (file &optional path)
460 (unless path
461 (setq path image-load-path))
462 (let (element found filename)
5fc5ac38 463 (while (and (not found) (consp path))
93a75651 464 (setq element (car path))
5fc5ac38 465 (cond
93a75651 466 ((stringp element)
5fc5ac38
CY
467 (setq found
468 (file-readable-p
4fde92ef 469 (setq filename (expand-file-name file element)))))
93a75651
CY
470 ((and (symbolp element) (boundp element))
471 (setq element (symbol-value element))
472 (cond
473 ((stringp element)
474 (setq found
475 (file-readable-p
4fde92ef 476 (setq filename (expand-file-name file element)))))
93a75651 477 ((consp element)
4fde92ef 478 (if (setq filename (image-search-load-path file element))
93a75651 479 (setq found t))))))
5fc5ac38 480 (setq path (cdr path)))
4fde92ef 481 (if found filename)))
7840ced1
GM
482
483;;;###autoload
349c034d
GM
484(defun find-image (specs)
485 "Find an image, choosing one of a list of image specifications.
7840ced1 486
cedbd8d0 487SPECS is a list of image specifications.
7840ced1
GM
488
489Each image specification in SPECS is a property list. The contents of
490a specification are image type dependent. All specifications must at
bc283707
WP
491least contain the properties `:type TYPE' and either `:file FILE' or
492`:data DATA', where TYPE is a symbol specifying the image type,
493e.g. `xbm', FILE is the file to load the image from, and DATA is a
cedbd8d0
DL
494string containing the actual image data. The specification whose TYPE
495is supported, and FILE exists, is used to construct the image
496specification to be returned. Return nil if no specification is
497satisfied.
498
8646a62e
CY
499The image is looked for in `image-load-path'.
500
501Image files should not be larger than specified by `max-image-size'."
7840ced1
GM
502 (let (image)
503 (while (and specs (null image))
504 (let* ((spec (car specs))
505 (type (plist-get spec :type))
162dec01 506 (data (plist-get spec :data))
7f228379
GM
507 (file (plist-get spec :file))
508 found)
162dec01
GM
509 (when (image-type-available-p type)
510 (cond ((stringp file)
4fde92ef 511 (if (setq found (image-search-load-path file))
5fc5ac38
CY
512 (setq image
513 (cons 'image (plist-put (copy-sequence spec)
514 :file found)))))
f64ae86c 515 ((not (null data))
162dec01
GM
516 (setq image (cons 'image spec)))))
517 (setq specs (cdr specs))))
349c034d
GM
518 image))
519
520
521;;;###autoload
522(defmacro defimage (symbol specs &optional doc)
523 "Define SYMBOL as an image.
524
525SPECS is a list of image specifications. DOC is an optional
526documentation string.
527
528Each image specification in SPECS is a property list. The contents of
529a specification are image type dependent. All specifications must at
530least contain the properties `:type TYPE' and either `:file FILE' or
531`:data DATA', where TYPE is a symbol specifying the image type,
532e.g. `xbm', FILE is the file to load the image from, and DATA is a
533string containing the actual image data. The first image
534specification whose TYPE is supported, and FILE exists, is used to
535define SYMBOL.
536
537Example:
538
539 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
540 (:type xbm :file \"~/test1.xbm\")))"
fd8e7a80 541 (declare (doc-string 3))
349c034d 542 `(defvar ,symbol (find-image ',specs) ,doc))
7840ced1
GM
543
544
545(provide 'image)
546
fd8e7a80 547;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
e43367a0 548;;; image.el ends here