(finder-known-keywords): Add `multimedia'.
[bpt/emacs.git] / lisp / image.el
CommitLineData
7840ced1
GM
1;;; image.el --- image API
2
3;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
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.
11
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.
16
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.
21
22;;; Commentary:
23
24;;; Code:
25
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.
36When the first bytes of an image file match REGEXP, it is assumed to
37be of image type IMAGE-TYPE.")
38
39
40;;;###autoload
41(defun image-type-from-file-header (file)
42 "Determine the type of image file FILE from its first few bytes.
43Value is a symbol specifying the image type, or nil if type cannot
44be 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))
60
61
62;;;###autoload
63(defun image-type-available-p (type)
64 "Value is non-nil if image type TYPE is available.
65Image types are symbols like `xbm' or `jpeg'."
66 (not (null (memq type image-types))))
67
68
69;;;###autoload
70(defun create-image (file &optional type &rest props)
71 "Create an image which will be loaded from FILE.
72Optional TYPE is a symbol describing the image type. If TYPE is omitted
73or nil, try to determine the image file type from its first few bytes.
74If that doesn't work, use FILE's extension.as image type.
75Optional PROPS are additional image attributes to assign to the image,
76like, e.g. `:heuristic-mask t'.
77Value 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)))
90
91
92;;;###autoload
93(defun put-image (image pos &optional buffer area)
94 "Put image IMAGE in front of POS in BUFFER.
95IMAGE must be an image created with `create-image' or `defimage'.
96POS may be an integer or marker.
97BUFFER nil or omitted means use the current buffer.
98AREA is where to display the image. AREA nil or omitted means
99display it in the text area, a value of `left-margin' means
100display it in the left marginal area, a value of `right-margin'
101means display it in the right marginal area.
102IMAGE is displayed by putting an overlay into BUFFER with a
103`before-string' that has a `display' property whose value is the
104image."
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)))
117
118
119;;;###autoload
120(defun insert-image (image &optional area)
121 "Insert IMAGE into current buffer at point.
122AREA is where to display the image. AREA nil or omitted means
123display it in the text area, a value of `left-margin' means
124display it in the left marginal area, a value of `right-margin'
125means display it in the right marginal area.
126IMAGE is displayed by inserting an \"x\" into the current buffer
127having 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))))
136
137
138;;;###autoload
139(defun remove-images (start end &optional buffer)
140 "Remove images between START and END in BUFFER.
141Remove only images that were put in BUFFER with calls to `put-image'.
142BUFFER 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)))))))
151
152
153;;;###autoload
154(defmacro defimage (symbol specs &optional doc)
155 "Define SYMBOL as an image.
156
157SPECS is a list of image specifications. DOC is an optional
158documentation string.
159
160Each image specification in SPECS is a property list. The contents of
161a specification are image type dependent. All specifications must at
162least contain the properties `:type TYPE' and `:file FILE', where TYPE
163is a symbol specifying the image type, e.g. `xbm', and FILE is the
164file to load the image from. The first image specification whose TYPE
165is supported, and FILE exists, is used to define SYMBOL.
166
167Example:
168
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)))
184
185
186(provide 'image)
187
188 ;; image.el ends here.
189
190
191
192