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