Fix maintainer address.
[emacs.git] / lisp / image.el
blob800196ddc65b4f5bcc7eb5c0756626f78703923d
1 ;;; image.el --- image API
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
22 ;;; Commentary:
24 ;;; Code:
26 (defconst image-type-regexps
27 '(("^/\\*.*XPM.\\*/" . xpm)
28 ("^P[1-6]" . pbm)
29 ("^GIF8" . gif)
30 ("JFIF" . jpeg)
31 ("^\211PNG\r\n" . png)
32 ("^#define" . xbm)
33 ("^\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
34 ("^%!PS" . ghostscript))
35 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
36 When the first bytes of an image file match REGEXP, it is assumed to
37 be of image type IMAGE-TYPE.")
40 ;;;###autoload
41 (defun image-type-from-file-header (file)
42 "Determine the type of image file FILE from its first few bytes.
43 Value is a symbol specifying the image type, or nil if type cannot
44 be determined."
45 (unless (file-name-directory file)
46 (setq file (concat data-directory file)))
47 (setq file (expand-file-name file))
48 (let ((header (with-temp-buffer
49 (insert-file-contents-literally file nil 0 256)
50 (buffer-string)))
51 (types image-type-regexps)
52 type)
53 (while (and types (null type))
54 (let ((regexp (car (car types)))
55 (image-type (cdr (car types))))
56 (when (string-match regexp header)
57 (setq type image-type))
58 (setq types (cdr types))))
59 type))
62 ;;;###autoload
63 (defun image-type-available-p (type)
64 "Value is non-nil if image type TYPE is available.
65 Image types are symbols like `xbm' or `jpeg'."
66 (not (null (memq type image-types))))
69 ;;;###autoload
70 (defun create-image (file &optional type &rest props)
71 "Create an image which will be loaded from FILE.
72 Optional TYPE is a symbol describing the image type. If TYPE is omitted
73 or nil, try to determine the image file type from its first few bytes.
74 If that doesn't work, use FILE's extension.as image type.
75 Optional PROPS are additional image attributes to assign to the image,
76 like, e.g. `:heuristic-mask t'.
77 Value is the image created, or nil if images of type TYPE are not supported."
78 (unless (stringp file)
79 (error "Invalid image file name %s" file))
80 (unless (or type
81 (setq type (image-type-from-file-header file)))
82 (let ((extension (file-name-extension file)))
83 (unless extension
84 (error "Cannot determine image type"))
85 (setq type (intern extension))))
86 (unless (symbolp type)
87 (error "Invalid image type %s" type))
88 (when (image-type-available-p type)
89 (append (list 'image :type type :file file) props)))
92 ;;;###autoload
93 (defun put-image (image pos &optional buffer area)
94 "Put image IMAGE in front of POS in BUFFER.
95 IMAGE must be an image created with `create-image' or `defimage'.
96 POS may be an integer or marker.
97 BUFFER nil or omitted means use the current buffer.
98 AREA is where to display the image. AREA nil or omitted means
99 display it in the text area, a value of `left-margin' means
100 display it in the left marginal area, a value of `right-margin'
101 means display it in the right marginal area.
102 IMAGE is displayed by putting an overlay into BUFFER with a
103 `before-string' that has a `display' property whose value is the
104 image."
105 (unless buffer
106 (setq buffer (current-buffer)))
107 (unless (eq (car image) 'image)
108 (error "Not an image: %s" image))
109 (unless (or (null area) (memq area '(left-margin right-margin)))
110 (error "Invalid area %s" area))
111 (let ((overlay (make-overlay pos pos buffer))
112 (string (make-string 1 ?x))
113 (prop (if (null area) image (cons area image))))
114 (put-text-property 0 1 'display prop string)
115 (overlay-put overlay 'put-image t)
116 (overlay-put overlay 'before-string string)))
119 ;;;###autoload
120 (defun insert-image (image &optional area)
121 "Insert IMAGE into current buffer at point.
122 AREA is where to display the image. AREA nil or omitted means
123 display it in the text area, a value of `left-margin' means
124 display it in the left marginal area, a value of `right-margin'
125 means display it in the right marginal area.
126 IMAGE is displayed by inserting an \"x\" into the current buffer
127 having a `display' property whose value is the image."
128 (unless (eq (car image) 'image)
129 (error "Not an image: %s" image))
130 (unless (or (null area) (memq area '(left-margin right-margin)))
131 (error "Invalid area %s" area))
132 (insert "x")
133 (add-text-properties (1- (point)) (point)
134 (list 'display (if (null area) image (cons area image))
135 'rear-nonsticky (list 'display))))
138 ;;;###autoload
139 (defun remove-images (start end &optional buffer)
140 "Remove images between START and END in BUFFER.
141 Remove only images that were put in BUFFER with calls to `put-image'.
142 BUFFER nil or omitted means use the current buffer."
143 (unless buffer
144 (setq buffer (current-buffer)))
145 (let ((overlays (overlays-in start end)))
146 (while overlays
147 (let ((overlay (car overlays)))
148 (when (overlay-get overlay 'put-image)
149 (delete-overlay overlay)
150 (setq overlays (cdr overlays)))))))
153 ;;;###autoload
154 (defmacro defimage (symbol specs &optional doc)
155 "Define SYMBOL as an image.
157 SPECS is a list of image specifications. DOC is an optional
158 documentation string.
160 Each image specification in SPECS is a property list. The contents of
161 a specification are image type dependent. All specifications must at
162 least contain the properties `:type TYPE' and `:file FILE', where TYPE
163 is a symbol specifying the image type, e.g. `xbm', and FILE is the
164 file to load the image from. The first image specification whose TYPE
165 is supported, and FILE exists, is used to define SYMBOL.
167 Example:
169 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
170 (:type xbm :file \"~/test1.xbm\")))"
171 (let (image)
172 (while (and specs (null image))
173 (let* ((spec (car specs))
174 (type (plist-get spec :type))
175 (file (plist-get spec :file)))
176 (when (and (image-type-available-p type) (stringp file))
177 (setq file (expand-file-name file))
178 (unless (file-name-absolute-p file)
179 (setq file (concat data-directory "/" file)))
180 (when (file-exists-p file)
181 (setq image (cons 'image spec))))
182 (setq specs (cdr specs))))
183 `(defvar ,symbol ',image ,doc)))
186 (provide 'image)
188 ;; image.el ends here.