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