25003
|
1 ;;; image.el --- image API
|
|
2
|
64762
|
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
|
79721
|
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
|
42316
|
5
|
|
6 ;; Maintainer: FSF
|
25309
|
7 ;; Keywords: multimedia
|
25003
|
8
|
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
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
|
78236
|
13 ;; the Free Software Foundation; either version 3, or (at your option)
|
25003
|
14 ;; any later version.
|
|
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
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
64091
|
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
24 ;; Boston, MA 02110-1301, USA.
|
25003
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;;; Code:
|
|
29
|
32166
|
30
|
|
31 (defgroup image ()
|
|
32 "Image support."
|
|
33 :group 'multimedia)
|
|
34
|
|
35
|
66296
|
36 (defconst image-type-header-regexps
|
82835
|
37 `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
38 ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
|
75472
|
39 ("\\`GIF8[79]a" . gif)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
40 ("\\`\x89PNG\r\n\x1a\n" . png)
|
75115
|
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)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
44 ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
45 ("\\`[\t\n\r ]*%!PS" . postscript)
|
82521
|
46 ("\\`\xff\xd8" . (image-jpeg-p . jpeg))
|
82835
|
47 (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
|
|
48 (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
|
|
49 (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
|
|
50 comment-re "*"
|
|
51 "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?"
|
|
52 "[Ss][Vv][Gg]"))
|
|
53 . svg)
|
|
54 )
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
55 "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
|
25003
|
56 When the first bytes of an image file match REGEXP, it is assumed to
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
57 be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol. If not a symbol,
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
58 IMAGE-TYPE must be a pair (PREDICATE . TYPE). PREDICATE is called
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
59 with one argument, a string containing the image data. If PREDICATE returns
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
60 a non-nil value, TYPE is the image's type.")
|
38732
|
61
|
66296
|
62 (defconst image-type-file-name-regexps
|
|
63 '(("\\.png\\'" . png)
|
|
64 ("\\.gif\\'" . gif)
|
|
65 ("\\.jpe?g\\'" . jpeg)
|
|
66 ("\\.bmp\\'" . bmp)
|
|
67 ("\\.xpm\\'" . xpm)
|
|
68 ("\\.pbm\\'" . pbm)
|
|
69 ("\\.xbm\\'" . xbm)
|
|
70 ("\\.ps\\'" . postscript)
|
82835
|
71 ("\\.tiff?\\'" . tiff)
|
|
72 ("\\.svgz?\\'" . svg)
|
|
73 )
|
66296
|
74 "Alist of (REGEXP . IMAGE-TYPE) pairs used to identify image files.
|
|
75 When the name of an image file match REGEXP, it is assumed to
|
|
76 be of image type IMAGE-TYPE.")
|
|
77
|
75744
|
78 ;; We rely on `auto-mode-alist' to detect xbm and xpm files, instead
|
|
79 ;; of content autodetection. Their contents are just C code, so it is
|
|
80 ;; easy to generate false matches.
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
81 (defvar image-type-auto-detectable
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
82 '((pbm . t)
|
75744
|
83 (xbm . nil)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
84 (bmp . maybe)
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
85 (gif . maybe)
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
86 (png . maybe)
|
75744
|
87 (xpm . nil)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
88 (jpeg . maybe)
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
89 (tiff . maybe)
|
82835
|
90 (svg . maybe)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
91 (postscript . nil))
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
92 "Alist of (IMAGE-TYPE . AUTODETECT) pairs used to auto-detect image files.
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
93 \(See `image-type-auto-detected-p').
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
94
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
95 AUTODETECT can be
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
96 - t always auto-detect.
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
97 - nil never auto-detect.
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
98 - maybe auto-detect only if the image type is available
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
99 (see `image-type-available-p').")
|
66296
|
100
|
66307
|
101 (defvar image-load-path nil
|
65544
|
102 "List of locations in which to search for image files.
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
103 If an element is a string, it defines a directory to search.
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
104 If an element is a variable symbol whose value is a string, that
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
105 value defines a directory to search.
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
106 If an element is a variable symbol whose value is a list, the
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
107 value is used as a list of directories to search.")
|
65544
|
108
|
66307
|
109 (eval-at-startup
|
|
110 (setq image-load-path
|
|
111 (list (file-name-as-directory (expand-file-name "images" data-directory))
|
|
112 'data-directory 'load-path)))
|
|
113
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
114
|
69365
|
115 (defun image-load-path-for-library (library image &optional path no-error)
|
69569
|
116 "Return a suitable search path for images used by LIBRARY.
|
69360
|
117
|
69535
|
118 It searches for IMAGE in `image-load-path' (excluding
|
69516
|
119 \"`data-directory'/images\") and `load-path', followed by a path
|
|
120 suitable for LIBRARY, which includes \"../../etc/images\" and
|
|
121 \"../etc/images\" relative to the library file itself, and then
|
|
122 in \"`data-directory'/images\".
|
69360
|
123
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
124 Then this function returns a list of directories which contains
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
125 first the directory in which IMAGE was found, followed by the
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
126 value of `load-path'. If PATH is given, it is used instead of
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
127 `load-path'.
|
69365
|
128
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
129 If NO-ERROR is non-nil and a suitable path can't be found, don't
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
130 signal an error. Instead, return a list of directories as before,
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
131 except that nil appears in place of the image directory.
|
69360
|
132
|
|
133 Here is an example that uses a common idiom to provide
|
|
134 compatibility with versions of Emacs that lack the variable
|
|
135 `image-load-path':
|
|
136
|
69493
4f9233243d97
(image-load-path-for-library): Fix example by not recommending that
Bill Wohler <wohler@newt.com>
diff
changeset
|
137 ;; Shush compiler.
|
4f9233243d97
(image-load-path-for-library): Fix example by not recommending that
Bill Wohler <wohler@newt.com>
diff
changeset
|
138 (defvar image-load-path)
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
139
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
140 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
|
69493
4f9233243d97
(image-load-path-for-library): Fix example by not recommending that
Bill Wohler <wohler@newt.com>
diff
changeset
|
141 (image-load-path (cons (car load-path)
|
4f9233243d97
(image-load-path-for-library): Fix example by not recommending that
Bill Wohler <wohler@newt.com>
diff
changeset
|
142 (when (boundp 'image-load-path)
|
4f9233243d97
(image-load-path-for-library): Fix example by not recommending that
Bill Wohler <wohler@newt.com>
diff
changeset
|
143 image-load-path))))
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
144 (mh-tool-bar-folder-buttons-init))"
|
69360
|
145 (unless library (error "No library specified"))
|
|
146 (unless image (error "No image specified"))
|
69516
|
147 (let (image-directory image-directory-load-path)
|
|
148 ;; Check for images in image-load-path or load-path.
|
|
149 (let ((img image)
|
|
150 (dir (or
|
|
151 ;; Images in image-load-path.
|
|
152 (image-search-load-path image)
|
|
153 ;; Images in load-path.
|
|
154 (locate-library image)))
|
|
155 parent)
|
|
156 ;; Since the image might be in a nested directory (for
|
|
157 ;; example, mail/attach.pbm), adjust `image-directory'
|
|
158 ;; accordingly.
|
|
159 (when dir
|
|
160 (setq dir (file-name-directory dir))
|
|
161 (while (setq parent (file-name-directory img))
|
|
162 (setq img (directory-file-name parent)
|
|
163 dir (expand-file-name "../" dir))))
|
|
164 (setq image-directory-load-path dir))
|
|
165
|
|
166 ;; If `image-directory-load-path' isn't Emacs' image directory,
|
|
167 ;; it's probably a user preference, so use it. Then use a
|
|
168 ;; relative setting if possible; otherwise, use
|
|
169 ;; `image-directory-load-path'.
|
69360
|
170 (cond
|
69516
|
171 ;; User-modified image-load-path?
|
|
172 ((and image-directory-load-path
|
|
173 (not (equal image-directory-load-path
|
|
174 (file-name-as-directory
|
|
175 (expand-file-name "images" data-directory)))))
|
|
176 (setq image-directory image-directory-load-path))
|
69360
|
177 ;; Try relative setting.
|
|
178 ((let (library-name d1ei d2ei)
|
|
179 ;; First, find library in the load-path.
|
|
180 (setq library-name (locate-library library))
|
|
181 (if (not library-name)
|
|
182 (error "Cannot find library %s in load-path" library))
|
|
183 ;; And then set image-directory relative to that.
|
|
184 (setq
|
|
185 ;; Go down 2 levels.
|
69516
|
186 d2ei (file-name-as-directory
|
|
187 (expand-file-name
|
|
188 (concat (file-name-directory library-name) "../../etc/images")))
|
69360
|
189 ;; Go down 1 level.
|
69516
|
190 d1ei (file-name-as-directory
|
|
191 (expand-file-name
|
|
192 (concat (file-name-directory library-name) "../etc/images"))))
|
69360
|
193 (setq image-directory
|
|
194 ;; Set it to nil if image is not found.
|
|
195 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
|
|
196 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
|
69516
|
197 ;; Use Emacs' image directory.
|
|
198 (image-directory-load-path
|
|
199 (setq image-directory image-directory-load-path))
|
69365
|
200 (no-error
|
|
201 (message "Could not find image %s for library %s" image library))
|
69360
|
202 (t
|
|
203 (error "Could not find image %s for library %s" image library)))
|
|
204
|
69471
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
205 ;; Return an augmented `path' or `load-path'.
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
206 (nconc (list image-directory)
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
207 (delete image-directory (copy-sequence (or path load-path))))))
|
6f07bf99b6f8
(image-load-path-for-library): Pass value of path rather than symbol.
Bill Wohler <wohler@newt.com>
diff
changeset
|
208
|
69360
|
209
|
38732
|
210 (defun image-jpeg-p (data)
|
46810
|
211 "Value is non-nil if DATA, a string, consists of JFIF image data.
|
|
212 We accept the tag Exif because that is the same format."
|
38732
|
213 (when (string-match "\\`\xff\xd8" data)
|
|
214 (catch 'jfif
|
|
215 (let ((len (length data)) (i 2))
|
|
216 (while (< i len)
|
|
217 (when (/= (aref data i) #xff)
|
|
218 (throw 'jfif nil))
|
|
219 (setq i (1+ i))
|
|
220 (when (>= (+ i 2) len)
|
|
221 (throw 'jfif nil))
|
|
222 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
|
38776
|
223 (aref data (+ i 2))))
|
|
224 (code (aref data i)))
|
|
225 (when (and (>= code #xe0) (<= code #xef))
|
38732
|
226 ;; APP0 LEN1 LEN2 "JFIF\0"
|
49588
|
227 (throw 'jfif
|
52344
|
228 (string-match "JFIF\\|Exif"
|
|
229 (substring data i (min (+ i nbytes) len)))))
|
38746
|
230 (setq i (+ i 1 nbytes))))))))
|
25003
|
231
|
|
232
|
|
233 ;;;###autoload
|
27073
|
234 (defun image-type-from-data (data)
|
|
235 "Determine the image type from image data DATA.
|
|
236 Value is a symbol specifying the image type or nil if type cannot
|
|
237 be determined."
|
66296
|
238 (let ((types image-type-header-regexps)
|
27073
|
239 type)
|
66296
|
240 (while types
|
27073
|
241 (let ((regexp (car (car types)))
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
242 (image-type (cdr (car types))))
|
66296
|
243 (if (or (and (symbolp image-type)
|
|
244 (string-match regexp data))
|
|
245 (and (consp image-type)
|
|
246 (funcall (car image-type) data)
|
|
247 (setq image-type (cdr image-type))))
|
|
248 (setq type image-type
|
|
249 types nil)
|
|
250 (setq types (cdr types)))))
|
|
251 type))
|
|
252
|
|
253
|
|
254 ;;;###autoload
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
255 (defun image-type-from-buffer ()
|
66296
|
256 "Determine the image type from data in the current buffer.
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
257 Value is a symbol specifying the image type or nil if type cannot
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
258 be determined."
|
66296
|
259 (let ((types image-type-header-regexps)
|
|
260 type
|
|
261 (opoint (point)))
|
|
262 (goto-char (point-min))
|
|
263 (while types
|
|
264 (let ((regexp (car (car types)))
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
265 (image-type (cdr (car types)))
|
66296
|
266 data)
|
|
267 (if (or (and (symbolp image-type)
|
|
268 (looking-at regexp))
|
|
269 (and (consp image-type)
|
|
270 (funcall (car image-type)
|
|
271 (or data
|
|
272 (setq data
|
|
273 (buffer-substring
|
|
274 (point-min)
|
|
275 (min (point-max)
|
|
276 (+ (point-min) 256))))))
|
|
277 (setq image-type (cdr image-type))))
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
278 (setq type image-type
|
66296
|
279 types nil)
|
|
280 (setq types (cdr types)))))
|
|
281 (goto-char opoint)
|
27073
|
282 type))
|
|
283
|
|
284
|
|
285 ;;;###autoload
|
25003
|
286 (defun image-type-from-file-header (file)
|
|
287 "Determine the type of image file FILE from its first few bytes.
|
|
288 Value is a symbol specifying the image type, or nil if type cannot
|
|
289 be determined."
|
66296
|
290 (unless (or (file-readable-p file)
|
|
291 (file-name-absolute-p file))
|
|
292 (setq file (image-search-load-path file)))
|
|
293 (and file
|
|
294 (file-readable-p file)
|
|
295 (with-temp-buffer
|
|
296 (set-buffer-multibyte nil)
|
|
297 (insert-file-contents-literally file nil 0 256)
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
298 (image-type-from-buffer))))
|
66296
|
299
|
|
300
|
|
301 ;;;###autoload
|
|
302 (defun image-type-from-file-name (file)
|
|
303 "Determine the type of image file FILE from its name.
|
|
304 Value is a symbol specifying the image type, or nil if type cannot
|
|
305 be determined."
|
81013
|
306 (assoc-default file image-type-file-name-regexps 'string-match))
|
25003
|
307
|
|
308
|
|
309 ;;;###autoload
|
81013
|
310 (defun image-type (source &optional type data-p)
|
70170
|
311 "Determine and return image type.
|
81013
|
312 SOURCE is an image file name or image data.
|
70170
|
313 Optional TYPE is a symbol describing the image type. If TYPE is omitted
|
|
314 or nil, try to determine the image type from its first few bytes
|
81013
|
315 of image data. If that doesn't work, and SOURCE is a file name,
|
70170
|
316 use its file extension as image type.
|
81013
|
317 Optional DATA-P non-nil means SOURCE is a string containing image data."
|
|
318 (when (and (not data-p) (not (stringp source)))
|
|
319 (error "Invalid image file name `%s'" source))
|
70170
|
320 (unless type
|
81013
|
321 (setq type (if data-p
|
|
322 (image-type-from-data source)
|
|
323 (or (image-type-from-file-header source)
|
|
324 (image-type-from-file-name source))))
|
|
325 (or type (error "Cannot determine image type")))
|
85072
|
326 (or (memq type (and (boundp 'image-types) image-types))
|
81013
|
327 (error "Invalid image type `%s'" type))
|
70170
|
328 type)
|
|
329
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
330
|
70170
|
331 ;;;###autoload
|
25003
|
332 (defun image-type-available-p (type)
|
62816
|
333 "Return non-nil if image type TYPE is available.
|
25003
|
334 Image types are symbols like `xbm' or `jpeg'."
|
56106
5b3f0e4cf47f
(image-library-alist): New variable to map image types to external
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
335 (and (fboundp 'init-image-library)
|
5b3f0e4cf47f
(image-library-alist): New variable to map image types to external
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
336 (init-image-library type image-library-alist)))
|
25003
|
337
|
66296
|
338
|
25003
|
339 ;;;###autoload
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
340 (defun image-type-auto-detected-p ()
|
78492
|
341 "Return t if the current buffer contains an auto-detectable image.
|
77873
|
342 This function is intended to be used from `magic-fallback-mode-alist'.
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
343
|
77873
|
344 The buffer is considered to contain an auto-detectable image if
|
|
345 its beginning matches an image type in `image-type-header-regexps',
|
84944
|
346 and that image type is present in `image-type-auto-detectable' with a
|
|
347 non-nil value. If that value is non-nil, but not t, then the image type
|
|
348 must be available."
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
349 (let* ((type (image-type-from-buffer))
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
350 (auto (and type (cdr (assq type image-type-auto-detectable)))))
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
351 (and auto
|
77873
|
352 (or (eq auto t) (image-type-available-p type)))))
|
74972
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
353
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
354
|
94afc7cac404
(image-type-header-regexps, image-type-from-data, image-type-from-buffer,
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
355 ;;;###autoload
|
27073
|
356 (defun create-image (file-or-data &optional type data-p &rest props)
|
|
357 "Create an image.
|
|
358 FILE-OR-DATA is an image file name or image data.
|
25003
|
359 Optional TYPE is a symbol describing the image type. If TYPE is omitted
|
27073
|
360 or nil, try to determine the image type from its first few bytes
|
|
361 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
|
34378
5ae7af279df7
(create-image): Doc fix; spotted by Per Cederqvist <ceder@lysator.liu.se>.
Eli Zaretskii <eliz@gnu.org>
diff
changeset
|
362 use its file extension as image type.
|
27073
|
363 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
|
25003
|
364 Optional PROPS are additional image attributes to assign to the image,
|
31635
|
365 like, e.g. `:mask MASK'.
|
66196
2e59eea25fcf
* image.el (create-image, find-image): Mention max-image-size in
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
366 Value is the image created, or nil if images of type TYPE are not supported.
|
2e59eea25fcf
* image.el (create-image, find-image): Mention max-image-size in
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
367
|
82734
|
368 Images should not be larger than specified by `max-image-size'.
|
|
369
|
|
370 Image 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.
|
70170
|
374 (setq type (image-type file-or-data type data-p))
|
25003
|
375 (when (image-type-available-p type)
|
27073
|
376 (append (list 'image :type type (if data-p :data :file) file-or-data)
|
|
377 props)))
|
25003
|
378
|
|
379
|
|
380 ;;;###autoload
|
29470
|
381 (defun put-image (image pos &optional string area)
|
25617
|
382 "Put image IMAGE in front of POS in the current buffer.
|
25003
|
383 IMAGE must be an image created with `create-image' or `defimage'.
|
25816
|
384 IMAGE 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
|
29470
|
386 image. STRING is defaulted if you omit it.
|
25003
|
387 POS may be an integer or marker.
|
|
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'
|
25816
|
391 means display it in the right marginal area."
|
29626
|
392 (unless string (setq string "x"))
|
25617
|
393 (let ((buffer (current-buffer)))
|
29470
|
394 (unless (eq (car-safe image) 'image)
|
25617
|
395 (error "Not an image: %s" image))
|
|
396 (unless (or (null area) (memq area '(left-margin right-margin)))
|
|
397 (error "Invalid area %s" area))
|
25816
|
398 (setq string (copy-sequence string))
|
25617
|
399 (let ((overlay (make-overlay pos pos buffer))
|
25816
|
400 (prop (if (null area) image (list (list 'margin area) image))))
|
|
401 (put-text-property 0 (length string) 'display prop string)
|
25617
|
402 (overlay-put overlay 'put-image t)
|
|
403 (overlay-put overlay 'before-string string))))
|
25003
|
404
|
|
405
|
|
406 ;;;###autoload
|
55033
|
407 (defun insert-image (image &optional string area slice)
|
25003
|
408 "Insert IMAGE into current buffer at point.
|
25816
|
409 IMAGE is displayed by inserting STRING into the current buffer
|
29470
|
410 with a `display' property whose value is the image. STRING is
|
|
411 defaulted if you omit it.
|
25003
|
412 AREA is where to display the image. AREA nil or omitted means
|
|
413 display it in the text area, a value of `left-margin' means
|
|
414 display it in the left marginal area, a value of `right-margin'
|
55033
|
415 means display it in the right marginal area.
|
|
416 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
|
|
417 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
|
|
418 specifying the X and Y positions and WIDTH and HEIGHT of image area
|
|
419 to insert. A float value 0.0 - 1.0 means relative to the width or
|
|
420 height of the image; integer values are taken as pixel values."
|
29626
|
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 " "))
|
29470
|
424 (unless (eq (car-safe image) 'image)
|
25003
|
425 (error "Not an image: %s" image))
|
|
426 (unless (or (null area) (memq area '(left-margin right-margin)))
|
|
427 (error "Invalid area %s" area))
|
29607
|
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))))
|
25816
|
437 (let ((start (point)))
|
|
438 (insert string)
|
|
439 (add-text-properties start (point)
|
55033
|
440 `(display ,(if slice
|
|
441 (list (cons 'slice slice) image)
|
|
442 image) rear-nonsticky (display)))))
|
|
443
|
|
444
|
60688
|
445 ;;;###autoload
|
55033
|
446 (defun insert-sliced-image (image &optional string area rows cols)
|
57196
|
447 "Insert IMAGE into current buffer at point.
|
|
448 IMAGE is displayed by inserting STRING into the current buffer
|
|
449 with a `display' property whose value is the image. STRING is
|
|
450 defaulted if you omit it.
|
|
451 AREA is where to display the image. AREA nil or omitted means
|
|
452 display it in the text area, a value of `left-margin' means
|
|
453 display it in the left marginal area, a value of `right-margin'
|
|
454 means display it in the right marginal area.
|
|
455 The image is automatically split into ROW x COLS slices."
|
55033
|
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))
|
59276
|
482 (insert (propertize "\n" 'line-height t)))))
|
55033
|
483
|
26007
|
484
|
25003
|
485
|
|
486 ;;;###autoload
|
|
487 (defun remove-images (start end &optional buffer)
|
|
488 "Remove images between START and END in BUFFER.
|
|
489 Remove only images that were put in BUFFER with calls to `put-image'.
|
|
490 BUFFER 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)
|
25857
|
497 (delete-overlay overlay)))
|
|
498 (setq overlays (cdr overlays)))))
|
25003
|
499
|
66296
|
500 (defun image-search-load-path (file &optional path)
|
|
501 (unless path
|
|
502 (setq path image-load-path))
|
|
503 (let (element found filename)
|
65544
|
504 (while (and (not found) (consp path))
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
505 (setq element (car path))
|
65544
|
506 (cond
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
507 ((stringp element)
|
65544
|
508 (setq found
|
|
509 (file-readable-p
|
66296
|
510 (setq filename (expand-file-name file element)))))
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
511 ((and (symbolp element) (boundp element))
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
512 (setq element (symbol-value element))
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
513 (cond
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
514 ((stringp element)
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
515 (setq found
|
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
516 (file-readable-p
|
66296
|
517 (setq filename (expand-file-name file element)))))
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
518 ((consp element)
|
66296
|
519 (if (setq filename (image-search-load-path file element))
|
65599
39114a2ae201
(image-load-path): Use symbol `data-directory' instead of its value,
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
520 (setq found t))))))
|
65544
|
521 (setq path (cdr path)))
|
66296
|
522 (if found filename)))
|
25003
|
523
|
|
524 ;;;###autoload
|
28721
|
525 (defun find-image (specs)
|
|
526 "Find an image, choosing one of a list of image specifications.
|
25003
|
527
|
29470
|
528 SPECS is a list of image specifications.
|
25003
|
529
|
|
530 Each image specification in SPECS is a property list. The contents of
|
|
531 a specification are image type dependent. All specifications must at
|
27059
6bb2a4a0413e
* image.el (defimage): Images with the :data keyword should be considered valid as well.
William M. Perry <wmperry@aventail.com>
diff
changeset
|
532 least contain the properties `:type TYPE' and either `:file FILE' or
|
6bb2a4a0413e
* image.el (defimage): Images with the :data keyword should be considered valid as well.
William M. Perry <wmperry@aventail.com>
diff
changeset
|
533 `:data DATA', where TYPE is a symbol specifying the image type,
|
6bb2a4a0413e
* image.el (defimage): Images with the :data keyword should be considered valid as well.
William M. Perry <wmperry@aventail.com>
diff
changeset
|
534 e.g. `xbm', FILE is the file to load the image from, and DATA is a
|
29470
|
535 string containing the actual image data. The specification whose TYPE
|
|
536 is supported, and FILE exists, is used to construct the image
|
|
537 specification to be returned. Return nil if no specification is
|
|
538 satisfied.
|
|
539
|
66196
2e59eea25fcf
* image.el (create-image, find-image): Mention max-image-size in
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
540 The image is looked for in `image-load-path'.
|
2e59eea25fcf
* image.el (create-image, find-image): Mention max-image-size in
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
541
|
2e59eea25fcf
* image.el (create-image, find-image): Mention max-image-size in
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
542 Image files should not be larger than specified by `max-image-size'."
|
25003
|
543 (let (image)
|
|
544 (while (and specs (null image))
|
|
545 (let* ((spec (car specs))
|
27073
|
546 (type (plist-get spec :type))
|
27059
6bb2a4a0413e
* image.el (defimage): Images with the :data keyword should be considered valid as well.
William M. Perry <wmperry@aventail.com>
diff
changeset
|
547 (data (plist-get spec :data))
|
27921
|
548 (file (plist-get spec :file))
|
|
549 found)
|
27073
|
550 (when (image-type-available-p type)
|
|
551 (cond ((stringp file)
|
66296
|
552 (if (setq found (image-search-load-path file))
|
65544
|
553 (setq image
|
|
554 (cons 'image (plist-put (copy-sequence spec)
|
|
555 :file found)))))
|
27075
|
556 ((not (null data))
|
27073
|
557 (setq image (cons 'image spec)))))
|
|
558 (setq specs (cdr specs))))
|
28721
|
559 image))
|
|
560
|
|
561
|
|
562 ;;;###autoload
|
|
563 (defmacro defimage (symbol specs &optional doc)
|
|
564 "Define SYMBOL as an image.
|
|
565
|
|
566 SPECS is a list of image specifications. DOC is an optional
|
|
567 documentation string.
|
|
568
|
|
569 Each image specification in SPECS is a property list. The contents of
|
|
570 a specification are image type dependent. All specifications must at
|
|
571 least contain the properties `:type TYPE' and either `:file FILE' or
|
|
572 `:data DATA', where TYPE is a symbol specifying the image type,
|
|
573 e.g. `xbm', FILE is the file to load the image from, and DATA is a
|
|
574 string containing the actual image data. The first image
|
|
575 specification whose TYPE is supported, and FILE exists, is used to
|
|
576 define SYMBOL.
|
|
577
|
|
578 Example:
|
|
579
|
|
580 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
|
|
581 (:type xbm :file \"~/test1.xbm\")))"
|
66402
|
582 (declare (doc-string 3))
|
28721
|
583 `(defvar ,symbol (find-image ',specs) ,doc))
|
25003
|
584
|
|
585
|
|
586 (provide 'image)
|
|
587
|
66402
|
588 ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
|
25872
|
589 ;;; image.el ends here
|