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