1 ;;; image.el --- image API
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 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
)
38 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm
)
39 ("\\`GIF8[79]a" . gif
)
40 ("\\`\x89PNG\r\n\x1a\n" . png
)
41 ("\\`[\t\n\r ]*#define \\([a-z0-9]+\\)_width [0-9]+\n\
42 #define \\1_height [0-9]+\n\
43 static char \\1_bits" . xbm
)
44 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff
)
45 ("\\`[\t\n\r ]*%!PS" . postscript
)
46 ("\\`\xff\xd8" .
(image-jpeg-p . jpeg
)))
47 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
48 When the first bytes of an image file match REGEXP, it is assumed to
49 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
50 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
51 with one argument, a string containing the image data. If PREDICATE returns
52 a non-nil value, TYPE is the image's type.")
54 (defconst image-type-file-name-regexps
57 ("\\.jpe?g\\'" . jpeg
)
62 ("\\.ps\\'" . postscript
)
63 ("\\.tiff?\\'" . tiff
))
64 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
65 When the name of an image file match REGEXP, it is assumed to
66 be of image type IMAGE-TYPE.")
68 ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
69 ;; of content autodetection. Their contents are just C code, so it is
70 ;; easy to generate false matches.
71 (defvar image-type-auto-detectable
81 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
82 \(See `image-type-auto-detected-p').
85 - t always auto-detect.
86 - nil never auto-detect.
87 - maybe auto-detect only if the image type is available
88 (see `image-type-available-p').")
90 (defvar image-load-path nil
91 "List of locations in which to search for image files.
92 If an element is a string, it defines a directory to search.
93 If an element is a variable symbol whose value is a string, that
94 value defines a directory to search.
95 If an element is a variable symbol whose value is a list, the
96 value is used as a list of directories to search.")
100 (list (file-name-as-directory (expand-file-name "images" data-directory
))
101 'data-directory
'load-path
)))
104 (defun image-load-path-for-library (library image
&optional path no-error
)
105 "Return a suitable search path for images used by LIBRARY.
107 It searches for IMAGE in `image-load-path' (excluding
108 \"`data-directory'/images\") and `load-path', followed by a path
109 suitable for LIBRARY, which includes \"../../etc/images\" and
110 \"../etc/images\" relative to the library file itself, and then
111 in \"`data-directory'/images\".
113 Then this function returns a list of directories which contains
114 first the directory in which IMAGE was found, followed by the
115 value of `load-path'. If PATH is given, it is used instead of
118 If NO-ERROR is non-nil and a suitable path can't be found, don't
119 signal an error. Instead, return a list of directories as before,
120 except that nil appears in place of the image directory.
122 Here is an example that uses a common idiom to provide
123 compatibility with versions of Emacs that lack the variable
127 (defvar image-load-path)
129 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
130 (image-load-path (cons (car load-path)
131 (when (boundp 'image-load-path)
133 (mh-tool-bar-folder-buttons-init))"
134 (unless library
(error "No library specified"))
135 (unless image
(error "No image specified"))
136 (let (image-directory image-directory-load-path
)
137 ;; Check for images in image-load-path or load-path.
140 ;; Images in image-load-path.
141 (image-search-load-path image
)
142 ;; Images in load-path.
143 (locate-library image
)))
145 ;; Since the image might be in a nested directory (for
146 ;; example, mail/attach.pbm), adjust `image-directory'
149 (setq dir
(file-name-directory dir
))
150 (while (setq parent
(file-name-directory img
))
151 (setq img
(directory-file-name parent
)
152 dir
(expand-file-name "../" dir
))))
153 (setq image-directory-load-path dir
))
155 ;; If `image-directory-load-path' isn't Emacs' image directory,
156 ;; it's probably a user preference, so use it. Then use a
157 ;; relative setting if possible; otherwise, use
158 ;; `image-directory-load-path'.
160 ;; User-modified image-load-path?
161 ((and image-directory-load-path
162 (not (equal image-directory-load-path
163 (file-name-as-directory
164 (expand-file-name "images" data-directory
)))))
165 (setq image-directory image-directory-load-path
))
166 ;; Try relative setting.
167 ((let (library-name d1ei d2ei
)
168 ;; First, find library in the load-path.
169 (setq library-name
(locate-library library
))
170 (if (not library-name
)
171 (error "Cannot find library %s in load-path" library
))
172 ;; And then set image-directory relative to that.
175 d2ei
(file-name-as-directory
177 (concat (file-name-directory library-name
) "../../etc/images")))
179 d1ei
(file-name-as-directory
181 (concat (file-name-directory library-name
) "../etc/images"))))
182 (setq image-directory
183 ;; Set it to nil if image is not found.
184 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
185 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
186 ;; Use Emacs' image directory.
187 (image-directory-load-path
188 (setq image-directory image-directory-load-path
))
190 (message "Could not find image %s for library %s" image library
))
192 (error "Could not find image %s for library %s" image library
)))
194 ;; Return an augmented `path' or `load-path'.
195 (nconc (list image-directory
)
196 (delete image-directory
(copy-sequence (or path load-path
))))))
199 (defun image-jpeg-p (data)
200 "Value is non-nil if DATA, a string, consists of JFIF image data.
201 We accept the tag Exif because that is the same format."
202 (when (string-match "\\`\xff\xd8" data
)
204 (let ((len (length data
)) (i 2))
206 (when (/= (aref data i
) #xff
)
209 (when (>= (+ i
2) len
)
211 (let ((nbytes (+ (lsh (aref data
(+ i
1)) 8)
212 (aref data
(+ i
2))))
213 (code (aref data i
)))
214 (when (and (>= code
#xe0
) (<= code
#xef
))
215 ;; APP0 LEN1 LEN2 "JFIF\0"
217 (string-match "JFIF\\|Exif"
218 (substring data i
(min (+ i nbytes
) len
)))))
219 (setq i
(+ i
1 nbytes
))))))))
223 (defun image-type-from-data (data)
224 "Determine the image type from image data DATA.
225 Value is a symbol specifying the image type or nil if type cannot
227 (let ((types image-type-header-regexps
)
230 (let ((regexp (car (car types
)))
231 (image-type (cdr (car types
))))
232 (if (or (and (symbolp image-type
)
233 (string-match regexp data
))
234 (and (consp image-type
)
235 (funcall (car image-type
) data
)
236 (setq image-type
(cdr image-type
))))
237 (setq type image-type
239 (setq types
(cdr types
)))))
244 (defun image-type-from-buffer ()
245 "Determine the image type from data in the current buffer.
246 Value is a symbol specifying the image type or nil if type cannot
248 (let ((types image-type-header-regexps
)
251 (goto-char (point-min))
253 (let ((regexp (car (car types
)))
254 (image-type (cdr (car types
)))
256 (if (or (and (symbolp image-type
)
258 (and (consp image-type
)
259 (funcall (car image-type
)
265 (+ (point-min) 256))))))
266 (setq image-type
(cdr image-type
))))
267 (setq type image-type
269 (setq types
(cdr types
)))))
275 (defun image-type-from-file-header (file)
276 "Determine the type of image file FILE from its first few bytes.
277 Value is a symbol specifying the image type, or nil if type cannot
279 (unless (or (file-readable-p file
)
280 (file-name-absolute-p file
))
281 (setq file
(image-search-load-path file
)))
283 (file-readable-p file
)
285 (set-buffer-multibyte nil
)
286 (insert-file-contents-literally file nil
0 256)
287 (image-type-from-buffer))))
291 (defun image-type-from-file-name (file)
292 "Determine the type of image file FILE from its name.
293 Value is a symbol specifying the image type, or nil if type cannot
295 (assoc-default file image-type-file-name-regexps
'string-match
))
299 (defun image-type (source &optional type data-p
)
300 "Determine and return image type.
301 SOURCE is an image file name or image data.
302 Optional TYPE is a symbol describing the image type. If TYPE is omitted
303 or nil, try to determine the image type from its first few bytes
304 of image data. If that doesn't work, and SOURCE is a file name,
305 use its file extension as image type.
306 Optional DATA-P non-nil means SOURCE is a string containing image data."
307 (when (and (not data-p
) (not (stringp source
)))
308 (error "Invalid image file name `%s'" source
))
310 (setq type
(if data-p
311 (image-type-from-data source
)
312 (or (image-type-from-file-header source
)
313 (image-type-from-file-name source
))))
314 (or type
(error "Cannot determine image type")))
315 (or (memq type image-types
)
316 (error "Invalid image type `%s'" type
))
321 (defun image-type-available-p (type)
322 "Return non-nil if image type TYPE is available.
323 Image types are symbols like `xbm' or `jpeg'."
324 (and (fboundp 'init-image-library
)
325 (init-image-library type image-library-alist
)))
329 (defun image-type-auto-detected-p ()
330 "Return t iff the current buffer contains an auto-detectable image.
331 This function is intended to be used from `magic-fallback-mode-alist'.
333 The buffer is considered to contain an auto-detectable image if
334 its beginning matches an image type in `image-type-header-regexps',
335 and that image type is present in `image-type-auto-detectable'."
336 (let* ((type (image-type-from-buffer))
337 (auto (and type
(cdr (assq type image-type-auto-detectable
)))))
339 (or (eq auto t
) (image-type-available-p type
)))))
343 (defun create-image (file-or-data &optional type data-p
&rest props
)
345 FILE-OR-DATA is an image file name or image data.
346 Optional TYPE is a symbol describing the image type. If TYPE is omitted
347 or nil, try to determine the image type from its first few bytes
348 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
349 use its file extension as image type.
350 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
351 Optional PROPS are additional image attributes to assign to the image,
352 like, e.g. `:mask MASK'.
353 Value is the image created, or nil if images of type TYPE are not supported.
355 Images should not be larger than specified by `max-image-size'."
356 (setq type
(image-type file-or-data type data-p
))
357 (when (image-type-available-p type
)
358 (append (list 'image
:type type
(if data-p
:data
:file
) file-or-data
)
363 (defun put-image (image pos
&optional string area
)
364 "Put image IMAGE in front of POS in the current buffer.
365 IMAGE must be an image created with `create-image' or `defimage'.
366 IMAGE is displayed by putting an overlay into the current buffer with a
367 `before-string' STRING that has a `display' property whose value is the
368 image. STRING is defaulted if you omit it.
369 POS may be an integer or marker.
370 AREA is where to display the image. AREA nil or omitted means
371 display it in the text area, a value of `left-margin' means
372 display it in the left marginal area, a value of `right-margin'
373 means display it in the right marginal area."
374 (unless string
(setq string
"x"))
375 (let ((buffer (current-buffer)))
376 (unless (eq (car-safe image
) 'image
)
377 (error "Not an image: %s" image
))
378 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
379 (error "Invalid area %s" area
))
380 (setq string
(copy-sequence string
))
381 (let ((overlay (make-overlay pos pos buffer
))
382 (prop (if (null area
) image
(list (list 'margin area
) image
))))
383 (put-text-property 0 (length string
) 'display prop string
)
384 (overlay-put overlay
'put-image t
)
385 (overlay-put overlay
'before-string string
))))
389 (defun insert-image (image &optional string area slice
)
390 "Insert IMAGE into current buffer at point.
391 IMAGE is displayed by inserting STRING into the current buffer
392 with a `display' property whose value is the image. STRING is
393 defaulted if you omit it.
394 AREA is where to display the image. AREA nil or omitted means
395 display it in the text area, a value of `left-margin' means
396 display it in the left marginal area, a value of `right-margin'
397 means display it in the right marginal area.
398 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
399 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
400 specifying the X and Y positions and WIDTH and HEIGHT of image area
401 to insert. A float value 0.0 - 1.0 means relative to the width or
402 height of the image; integer values are taken as pixel values."
403 ;; Use a space as least likely to cause trouble when it's a hidden
404 ;; character in the buffer.
405 (unless string
(setq string
" "))
406 (unless (eq (car-safe image
) 'image
)
407 (error "Not an image: %s" image
))
408 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
409 (error "Invalid area %s" area
))
411 (setq image
(list (list 'margin area
) image
))
412 ;; Cons up a new spec equal but not eq to `image' so that
413 ;; inserting it twice in a row (adjacently) displays two copies of
414 ;; the image. Don't try to avoid this by looking at the display
415 ;; properties on either side so that we DTRT more often with
416 ;; cut-and-paste. (Yanking killed image text next to another copy
417 ;; of it loses anyway.)
418 (setq image
(cons 'image
(cdr image
))))
419 (let ((start (point)))
421 (add-text-properties start
(point)
423 (list (cons 'slice slice
) image
)
424 image
) rear-nonsticky
(display)))))
428 (defun insert-sliced-image (image &optional string area rows cols
)
429 "Insert IMAGE into current buffer at point.
430 IMAGE is displayed by inserting STRING into the current buffer
431 with a `display' property whose value is the image. STRING is
432 defaulted if you omit it.
433 AREA is where to display the image. AREA nil or omitted means
434 display it in the text area, a value of `left-margin' means
435 display it in the left marginal area, a value of `right-margin'
436 means display it in the right marginal area.
437 The image is automatically split into ROW x COLS slices."
438 (unless string
(setq string
" "))
439 (unless (eq (car-safe image
) 'image
)
440 (error "Not an image: %s" image
))
441 (unless (or (null area
) (memq area
'(left-margin right-margin
)))
442 (error "Invalid area %s" area
))
444 (setq image
(list (list 'margin area
) image
))
445 ;; Cons up a new spec equal but not eq to `image' so that
446 ;; inserting it twice in a row (adjacently) displays two copies of
447 ;; the image. Don't try to avoid this by looking at the display
448 ;; properties on either side so that we DTRT more often with
449 ;; cut-and-paste. (Yanking killed image text next to another copy
450 ;; of it loses anyway.)
451 (setq image
(cons 'image
(cdr image
))))
452 (let ((x 0.0) (dx (/ 1.0001 (or cols
1)))
453 (y 0.0) (dy (/ 1.0001 (or rows
1))))
456 (let ((start (point)))
458 (add-text-properties start
(point)
459 `(display ,(list (list 'slice x y dx dy
) image
)
460 rear-nonsticky
(display)))
464 (insert (propertize "\n" 'line-height t
)))))
469 (defun remove-images (start end
&optional buffer
)
470 "Remove images between START and END in BUFFER.
471 Remove only images that were put in BUFFER with calls to `put-image'.
472 BUFFER nil or omitted means use the current buffer."
474 (setq buffer
(current-buffer)))
475 (let ((overlays (overlays-in start end
)))
477 (let ((overlay (car overlays
)))
478 (when (overlay-get overlay
'put-image
)
479 (delete-overlay overlay
)))
480 (setq overlays
(cdr overlays
)))))
482 (defun image-search-load-path (file &optional path
)
484 (setq path image-load-path
))
485 (let (element found filename
)
486 (while (and (not found
) (consp path
))
487 (setq element
(car path
))
492 (setq filename
(expand-file-name file element
)))))
493 ((and (symbolp element
) (boundp element
))
494 (setq element
(symbol-value element
))
499 (setq filename
(expand-file-name file element
)))))
501 (if (setq filename
(image-search-load-path file element
))
503 (setq path
(cdr path
)))
504 (if found filename
)))
507 (defun find-image (specs)
508 "Find an image, choosing one of a list of image specifications.
510 SPECS is a list of image specifications.
512 Each image specification in SPECS is a property list. The contents of
513 a specification are image type dependent. All specifications must at
514 least contain the properties `:type TYPE' and either `:file FILE' or
515 `:data DATA', where TYPE is a symbol specifying the image type,
516 e.g. `xbm', FILE is the file to load the image from, and DATA is a
517 string containing the actual image data. The specification whose TYPE
518 is supported, and FILE exists, is used to construct the image
519 specification to be returned. Return nil if no specification is
522 The image is looked for in `image-load-path'.
524 Image files should not be larger than specified by `max-image-size'."
526 (while (and specs
(null image
))
527 (let* ((spec (car specs
))
528 (type (plist-get spec
:type
))
529 (data (plist-get spec
:data
))
530 (file (plist-get spec
:file
))
532 (when (image-type-available-p type
)
533 (cond ((stringp file
)
534 (if (setq found
(image-search-load-path file
))
536 (cons 'image
(plist-put (copy-sequence spec
)
539 (setq image
(cons 'image spec
)))))
540 (setq specs
(cdr specs
))))
545 (defmacro defimage
(symbol specs
&optional doc
)
546 "Define SYMBOL as an image.
548 SPECS is a list of image specifications. DOC is an optional
549 documentation string.
551 Each image specification in SPECS is a property list. The contents of
552 a specification are image type dependent. All specifications must at
553 least contain the properties `:type TYPE' and either `:file FILE' or
554 `:data DATA', where TYPE is a symbol specifying the image type,
555 e.g. `xbm', FILE is the file to load the image from, and DATA is a
556 string containing the actual image data. The first image
557 specification whose TYPE is supported, and FILE exists, is used to
562 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
563 (:type xbm :file \"~/test1.xbm\")))"
564 (declare (doc-string 3))
565 `(defvar ,symbol
(find-image ',specs
) ,doc
))
570 ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
571 ;;; image.el ends here