1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
7 ;; Keywords: multimedia
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
36 (defconst image-type-header-regexps
37 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm
)
40 ("\\`\211PNG\r\n" . png
)
41 ("\\`[\t\n\r ]*#define" . xbm
)
42 ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff
)
43 ("\\`[\t\n\r ]*%!PS" . postscript
)
44 ("\\`\xff\xd8" .
(image-jpeg-p . jpeg
)))
45 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
46 When the first bytes of an image file match REGEXP, it is assumed to
47 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
48 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
49 with one argument, a string containing the image data. If PREDICATE returns
50 a non-nil value, TYPE is the image's type.")
52 (defconst image-type-file-name-regexps
55 ("\\.jpe?g\\'" . jpeg
)
60 ("\\.ps\\'" . postscript
)
61 ("\\.tiff?\\'" . tiff
))
62 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
63 When the name of an image file match REGEXP, it is assumed to
64 be of image type IMAGE-TYPE.")
67 (defvar image-load-path nil
68 "List of locations in which to search for image files.
69 If an element is a string, it defines a directory to search.
70 If an element is a variable symbol whose value is a string, that
71 value defines a directory to search.
72 If an element is a variable symbol whose value is a list, the
73 value is used as a list of directories to search.")
77 (list (file-name-as-directory (expand-file-name "images" data-directory
))
78 'data-directory
'load-path
)))
80 (defun image-load-path-for-library (library image
&optional path no-error
)
81 "Return a suitable search path for images relative to LIBRARY.
83 Images for LIBRARY are searched for in \"../../etc/images\" and
84 \"../etc/images\" relative to the files in \"lisp/LIBRARY\" as
85 well as in `image-load-path' and `load-path'.
87 This function returns the value of `load-path' augmented with the
88 directory containing IMAGE. If PATH is given, it is used instead
89 of `load-path'. If PATH is t, just return the directory that
92 If NO-ERROR is non-nil, return nil if a suitable path can't be
93 found rather than signaling an error.
95 Here is an example that uses a common idiom to provide
96 compatibility with versions of Emacs that lack the variable
100 (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
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))
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.
117 d2ei
(expand-file-name
118 (concat (file-name-directory library-name
) "../../etc/images"))
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.
129 ;; Images in image-load-path.
130 (image-search-load-path image
)
131 ;; Images in load-path.
132 (locate-library image
)))
134 ;; Since the image might be in a nested directory (for
135 ;; example, mail/attach.pbm), adjust `image-directory'
138 (setq dir
(file-name-directory dir
))
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
)))))
145 ;; In this case we will return nil.
146 (message "Could not find image %s for library %s" image library
))
148 (error "Could not find image %s for library %s" image library
)))
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
)
156 ((and path
(symbolp path
))
157 (nconc (list image-directory
)
158 (delete image-directory
160 (copy-sequence (symbol-value path
))
163 (nconc (list image-directory
)
164 (delete image-directory
(copy-sequence load-path
)))))))
166 (defun image-jpeg-p (data)
167 "Value is non-nil if DATA, a string, consists of JFIF image data.
168 We accept the tag Exif because that is the same format."
169 (when (string-match "\\`\xff\xd8" data
)
171 (let ((len (length data
)) (i 2))
173 (when (/= (aref data i
) #xff
)
176 (when (>= (+ i
2) len
)
178 (let ((nbytes (+ (lsh (aref data
(+ i
1)) 8)
179 (aref data
(+ i
2))))
180 (code (aref data i
)))
181 (when (and (>= code
#xe0
) (<= code
#xef
))
182 ;; APP0 LEN1 LEN2 "JFIF\0"
184 (string-match "JFIF\\|Exif"
185 (substring data i
(min (+ i nbytes
) len
)))))
186 (setq i
(+ i
1 nbytes
))))))))
190 (defun image-type-from-data (data)
191 "Determine the image type from image data DATA.
192 Value is a symbol specifying the image type or nil if type cannot
194 (let ((types image-type-header-regexps
)
197 (let ((regexp (car (car types
)))
198 (image-type (cdr (car types
))))
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
206 (setq types
(cdr types
)))))
211 (defun image-type-from-buffer ()
212 "Determine the image type from data in the current buffer.
213 Value is a symbol specifying the image type or nil if type cannot
215 (let ((types image-type-header-regexps
)
218 (goto-char (point-min))
220 (let ((regexp (car (car types
)))
221 (image-type (cdr (car types
)))
223 (if (or (and (symbolp image-type
)
225 (and (consp image-type
)
226 (funcall (car image-type
)
232 (+ (point-min) 256))))))
233 (setq image-type
(cdr image-type
))))
234 (setq type image-type
236 (setq types
(cdr types
)))))
242 (defun image-type-from-file-header (file)
243 "Determine the type of image file FILE from its first few bytes.
244 Value is a symbol specifying the image type, or nil if type cannot
246 (unless (or (file-readable-p file
)
247 (file-name-absolute-p file
))
248 (setq file
(image-search-load-path file
)))
250 (file-readable-p file
)
252 (set-buffer-multibyte nil
)
253 (insert-file-contents-literally file nil
0 256)
254 (image-type-from-buffer))))
258 (defun image-type-from-file-name (file)
259 "Determine the type of image file FILE from its name.
260 Value is a symbol specifying the image type, or nil if type cannot
262 (let ((types image-type-file-name-regexps
)
265 (if (string-match (car (car types
)) file
)
266 (setq type
(cdr (car types
))
268 (setq types
(cdr types
))))
273 (defun image-type-available-p (type)
274 "Return non-nil if image type TYPE is available.
275 Image types are symbols like `xbm' or `jpeg'."
276 (and (fboundp 'init-image-library
)
277 (init-image-library type image-library-alist
)))
281 (defun create-image (file-or-data &optional type data-p
&rest props
)
283 FILE-OR-DATA is an image file name or image data.
284 Optional TYPE is a symbol describing the image type. If TYPE is omitted
285 or nil, try to determine the image type from its first few bytes
286 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
287 use its file extension as image type.
288 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
289 Optional PROPS are additional image attributes to assign to the image,
290 like, e.g. `:mask MASK'.
291 Value is the image created, or nil if images of type TYPE are not supported.
293 Images should not be larger than specified by `max-image-size'."
294 (when (and (not data-p
) (not (stringp file-or-data
)))
295 (error "Invalid image file name `%s'" file-or-data
))
297 ;; FILE-OR-DATA is a file name.
299 (setq type
(image-type-from-file-header file-or-data
)))
300 (let ((extension (file-name-extension file-or-data
)))
302 (error "Cannot determine image type"))
303 (setq type
(intern extension
)))))
305 ;; FILE-OR-DATA contains image data.
307 (setq type
(image-type-from-data file-or-data
)))))
309 (error "Cannot determine image type"))
310 (unless (symbolp type
)
311 (error "Invalid image type `%s'" type
))
312 (when (image-type-available-p type
)
313 (append (list 'image
:type type
(if data-p
:data
:file
) file-or-data
)
318 (defun put-image (image pos
&optional string area
)
319 "Put image IMAGE in front of POS in the current buffer.
320 IMAGE must be an image created with `create-image' or `defimage'.
321 IMAGE 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
323 image. STRING is defaulted if you omit it.
324 POS may be an integer or marker.
325 AREA is where to display the image. AREA nil or omitted means
326 display it in the text area, a value of `left-margin' means
327 display it in the left marginal area, a value of `right-margin'
328 means display it in the right marginal area."
329 (unless string
(setq string
"x"))
330 (let ((buffer (current-buffer)))
331 (unless (eq (car-safe image
) 'image
)
332 (error "Not an image: %s" image
))
333 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
334 (error "Invalid area %s" area
))
335 (setq string
(copy-sequence string
))
336 (let ((overlay (make-overlay pos pos buffer
))
337 (prop (if (null area
) image
(list (list 'margin area
) image
))))
338 (put-text-property 0 (length string
) 'display prop string
)
339 (overlay-put overlay
'put-image t
)
340 (overlay-put overlay
'before-string string
))))
344 (defun insert-image (image &optional string area slice
)
345 "Insert IMAGE into current buffer at point.
346 IMAGE is displayed by inserting STRING into the current buffer
347 with a `display' property whose value is the image. STRING is
348 defaulted if you omit it.
349 AREA is where to display the image. AREA nil or omitted means
350 display it in the text area, a value of `left-margin' means
351 display it in the left marginal area, a value of `right-margin'
352 means display it in the right marginal area.
353 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
354 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
355 specifying the X and Y positions and WIDTH and HEIGHT of image area
356 to insert. A float value 0.0 - 1.0 means relative to the width or
357 height of the image; integer values are taken as pixel values."
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
" "))
361 (unless (eq (car-safe image
) 'image
)
362 (error "Not an image: %s" image
))
363 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
364 (error "Invalid area %s" 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
))))
374 (let ((start (point)))
376 (add-text-properties start
(point)
378 (list (cons 'slice slice
) image
)
379 image
) rear-nonsticky
(display)))))
383 (defun insert-sliced-image (image &optional string area rows cols
)
384 "Insert IMAGE into current buffer at point.
385 IMAGE is displayed by inserting STRING into the current buffer
386 with a `display' property whose value is the image. STRING is
387 defaulted if you omit it.
388 AREA is where to display the image. AREA nil or omitted means
389 display it in the text area, a value of `left-margin' means
390 display it in the left marginal area, a value of `right-margin'
391 means display it in the right marginal area.
392 The image is automatically split into ROW x COLS slices."
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
))
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))))
411 (let ((start (point)))
413 (add-text-properties start
(point)
414 `(display ,(list (list 'slice x y dx dy
) image
)
415 rear-nonsticky
(display)))
419 (insert (propertize "\n" 'line-height t
)))))
424 (defun remove-images (start end
&optional buffer
)
425 "Remove images between START and END in BUFFER.
426 Remove only images that were put in BUFFER with calls to `put-image'.
427 BUFFER nil or omitted means use the current buffer."
429 (setq buffer
(current-buffer)))
430 (let ((overlays (overlays-in start end
)))
432 (let ((overlay (car overlays
)))
433 (when (overlay-get overlay
'put-image
)
434 (delete-overlay overlay
)))
435 (setq overlays
(cdr overlays
)))))
437 (defun image-search-load-path (file &optional path
)
439 (setq path image-load-path
))
440 (let (element found filename
)
441 (while (and (not found
) (consp path
))
442 (setq element
(car path
))
447 (setq filename
(expand-file-name file element
)))))
448 ((and (symbolp element
) (boundp element
))
449 (setq element
(symbol-value element
))
454 (setq filename
(expand-file-name file element
)))))
456 (if (setq filename
(image-search-load-path file element
))
458 (setq path
(cdr path
)))
459 (if found filename
)))
462 (defun find-image (specs)
463 "Find an image, choosing one of a list of image specifications.
465 SPECS is a list of image specifications.
467 Each image specification in SPECS is a property list. The contents of
468 a specification are image type dependent. All specifications must at
469 least contain the properties `:type TYPE' and either `:file FILE' or
470 `:data DATA', where TYPE is a symbol specifying the image type,
471 e.g. `xbm', FILE is the file to load the image from, and DATA is a
472 string containing the actual image data. The specification whose TYPE
473 is supported, and FILE exists, is used to construct the image
474 specification to be returned. Return nil if no specification is
477 The image is looked for in `image-load-path'.
479 Image files should not be larger than specified by `max-image-size'."
481 (while (and specs
(null image
))
482 (let* ((spec (car specs
))
483 (type (plist-get spec
:type
))
484 (data (plist-get spec
:data
))
485 (file (plist-get spec
:file
))
487 (when (image-type-available-p type
)
488 (cond ((stringp file
)
489 (if (setq found
(image-search-load-path file
))
491 (cons 'image
(plist-put (copy-sequence spec
)
494 (setq image
(cons 'image spec
)))))
495 (setq specs
(cdr specs
))))
500 (defmacro defimage
(symbol specs
&optional doc
)
501 "Define SYMBOL as an image.
503 SPECS is a list of image specifications. DOC is an optional
504 documentation string.
506 Each image specification in SPECS is a property list. The contents of
507 a specification are image type dependent. All specifications must at
508 least contain the properties `:type TYPE' and either `:file FILE' or
509 `:data DATA', where TYPE is a symbol specifying the image type,
510 e.g. `xbm', FILE is the file to load the image from, and DATA is a
511 string containing the actual image data. The first image
512 specification whose TYPE is supported, and FILE exists, is used to
517 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
518 (:type xbm :file \"~/test1.xbm\")))"
519 (declare (doc-string 3))
520 `(defvar ,symbol
(find-image ',specs
) ,doc
))
525 ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
526 ;;; image.el ends here