(org-popup-calendar-for-date-prompt): Fix customization type.
[bpt/emacs.git] / lisp / image.el
1 ;;; image.el --- image API
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: multimedia
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30
31 (defgroup image ()
32 "Image support."
33 :group 'multimedia)
34
35
36 (defconst image-type-header-regexps
37 '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
38 ("\\`P[1-6]" . pbm)
39 ("\\`GIF8" . gif)
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.")
51
52 (defconst image-type-file-name-regexps
53 '(("\\.png\\'" . png)
54 ("\\.gif\\'" . gif)
55 ("\\.jpe?g\\'" . jpeg)
56 ("\\.bmp\\'" . bmp)
57 ("\\.xpm\\'" . xpm)
58 ("\\.pbm\\'" . pbm)
59 ("\\.xbm\\'" . xbm)
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.")
65
66
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.")
74
75 (eval-at-startup
76 (setq image-load-path
77 (list (file-name-as-directory (expand-file-name "images" data-directory))
78 'data-directory 'load-path)))
79
80
81 (defun image-load-path-for-library (library image &optional path no-error)
82 "Return a suitable search path for images relative to LIBRARY.
83
84 First it searches for IMAGE in `image-load-path' (excluding
85 \"`data-directory'/images\") and `load-path', followed by a path
86 suitable for LIBRARY, which includes \"../../etc/images\" and
87 \"../etc/images\" relative to the library file itself, and then
88 in \"`data-directory'/images\".
89
90 Then this function returns a list of directories which contains
91 first the directory in which IMAGE was found, followed by the
92 value of `load-path'. If PATH is given, it is used instead of
93 `load-path'.
94
95 If NO-ERROR is non-nil and a suitable path can't be found, don't
96 signal an error. Instead, return a list of directories as before,
97 except that nil appears in place of the image directory.
98
99 Here is an example that uses a common idiom to provide
100 compatibility with versions of Emacs that lack the variable
101 `image-load-path':
102
103 ;; Shush compiler.
104 (defvar image-load-path)
105
106 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
107 (image-load-path (cons (car load-path)
108 (when (boundp 'image-load-path)
109 image-load-path))))
110 (mh-tool-bar-folder-buttons-init))"
111 (unless library (error "No library specified"))
112 (unless image (error "No image specified"))
113 (let (image-directory image-directory-load-path)
114 ;; Check for images in image-load-path or load-path.
115 (let ((img image)
116 (dir (or
117 ;; Images in image-load-path.
118 (image-search-load-path image)
119 ;; Images in load-path.
120 (locate-library image)))
121 parent)
122 ;; Since the image might be in a nested directory (for
123 ;; example, mail/attach.pbm), adjust `image-directory'
124 ;; accordingly.
125 (when dir
126 (setq dir (file-name-directory dir))
127 (while (setq parent (file-name-directory img))
128 (setq img (directory-file-name parent)
129 dir (expand-file-name "../" dir))))
130 (setq image-directory-load-path dir))
131
132 ;; If `image-directory-load-path' isn't Emacs' image directory,
133 ;; it's probably a user preference, so use it. Then use a
134 ;; relative setting if possible; otherwise, use
135 ;; `image-directory-load-path'.
136 (cond
137 ;; User-modified image-load-path?
138 ((and image-directory-load-path
139 (not (equal image-directory-load-path
140 (file-name-as-directory
141 (expand-file-name "images" data-directory)))))
142 (setq image-directory image-directory-load-path))
143 ;; Try relative setting.
144 ((let (library-name d1ei d2ei)
145 ;; First, find library in the load-path.
146 (setq library-name (locate-library library))
147 (if (not library-name)
148 (error "Cannot find library %s in load-path" library))
149 ;; And then set image-directory relative to that.
150 (setq
151 ;; Go down 2 levels.
152 d2ei (file-name-as-directory
153 (expand-file-name
154 (concat (file-name-directory library-name) "../../etc/images")))
155 ;; Go down 1 level.
156 d1ei (file-name-as-directory
157 (expand-file-name
158 (concat (file-name-directory library-name) "../etc/images"))))
159 (setq image-directory
160 ;; Set it to nil if image is not found.
161 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
162 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
163 ;; Use Emacs' image directory.
164 (image-directory-load-path
165 (setq image-directory image-directory-load-path))
166 (no-error
167 (message "Could not find image %s for library %s" image library))
168 (t
169 (error "Could not find image %s for library %s" image library)))
170
171 ;; Return an augmented `path' or `load-path'.
172 (nconc (list image-directory)
173 (delete image-directory (copy-sequence (or path load-path))))))
174
175
176 (defun image-jpeg-p (data)
177 "Value is non-nil if DATA, a string, consists of JFIF image data.
178 We accept the tag Exif because that is the same format."
179 (when (string-match "\\`\xff\xd8" data)
180 (catch 'jfif
181 (let ((len (length data)) (i 2))
182 (while (< i len)
183 (when (/= (aref data i) #xff)
184 (throw 'jfif nil))
185 (setq i (1+ i))
186 (when (>= (+ i 2) len)
187 (throw 'jfif nil))
188 (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
189 (aref data (+ i 2))))
190 (code (aref data i)))
191 (when (and (>= code #xe0) (<= code #xef))
192 ;; APP0 LEN1 LEN2 "JFIF\0"
193 (throw 'jfif
194 (string-match "JFIF\\|Exif"
195 (substring data i (min (+ i nbytes) len)))))
196 (setq i (+ i 1 nbytes))))))))
197
198
199 ;;;###autoload
200 (defun image-type-from-data (data)
201 "Determine the image type from image data DATA.
202 Value is a symbol specifying the image type or nil if type cannot
203 be determined."
204 (let ((types image-type-header-regexps)
205 type)
206 (while types
207 (let ((regexp (car (car types)))
208 (image-type (cdr (car types))))
209 (if (or (and (symbolp image-type)
210 (string-match regexp data))
211 (and (consp image-type)
212 (funcall (car image-type) data)
213 (setq image-type (cdr image-type))))
214 (setq type image-type
215 types nil)
216 (setq types (cdr types)))))
217 type))
218
219
220 ;;;###autoload
221 (defun image-type-from-buffer ()
222 "Determine the image type from data in the current buffer.
223 Value is a symbol specifying the image type or nil if type cannot
224 be determined."
225 (let ((types image-type-header-regexps)
226 type
227 (opoint (point)))
228 (goto-char (point-min))
229 (while types
230 (let ((regexp (car (car types)))
231 (image-type (cdr (car types)))
232 data)
233 (if (or (and (symbolp image-type)
234 (looking-at regexp))
235 (and (consp image-type)
236 (funcall (car image-type)
237 (or data
238 (setq data
239 (buffer-substring
240 (point-min)
241 (min (point-max)
242 (+ (point-min) 256))))))
243 (setq image-type (cdr image-type))))
244 (setq type image-type
245 types nil)
246 (setq types (cdr types)))))
247 (goto-char opoint)
248 type))
249
250
251 ;;;###autoload
252 (defun image-type-from-file-header (file)
253 "Determine the type of image file FILE from its first few bytes.
254 Value is a symbol specifying the image type, or nil if type cannot
255 be determined."
256 (unless (or (file-readable-p file)
257 (file-name-absolute-p file))
258 (setq file (image-search-load-path file)))
259 (and file
260 (file-readable-p file)
261 (with-temp-buffer
262 (set-buffer-multibyte nil)
263 (insert-file-contents-literally file nil 0 256)
264 (image-type-from-buffer))))
265
266
267 ;;;###autoload
268 (defun image-type-from-file-name (file)
269 "Determine the type of image file FILE from its name.
270 Value is a symbol specifying the image type, or nil if type cannot
271 be determined."
272 (let ((types image-type-file-name-regexps)
273 type)
274 (while types
275 (if (string-match (car (car types)) file)
276 (setq type (cdr (car types))
277 types nil)
278 (setq types (cdr types))))
279 type))
280
281
282 ;;;###autoload
283 (defun image-type-available-p (type)
284 "Return non-nil if image type TYPE is available.
285 Image types are symbols like `xbm' or `jpeg'."
286 (and (fboundp 'init-image-library)
287 (init-image-library type image-library-alist)))
288
289
290 ;;;###autoload
291 (defun create-image (file-or-data &optional type data-p &rest props)
292 "Create an image.
293 FILE-OR-DATA is an image file name or image data.
294 Optional TYPE is a symbol describing the image type. If TYPE is omitted
295 or nil, try to determine the image type from its first few bytes
296 of image data. If that doesn't work, and FILE-OR-DATA is a file name,
297 use its file extension as image type.
298 Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
299 Optional PROPS are additional image attributes to assign to the image,
300 like, e.g. `:mask MASK'.
301 Value is the image created, or nil if images of type TYPE are not supported.
302
303 Images should not be larger than specified by `max-image-size'."
304 (when (and (not data-p) (not (stringp file-or-data)))
305 (error "Invalid image file name `%s'" file-or-data))
306 (cond ((null data-p)
307 ;; FILE-OR-DATA is a file name.
308 (unless (or type
309 (setq type (image-type-from-file-header file-or-data)))
310 (let ((extension (file-name-extension file-or-data)))
311 (unless extension
312 (error "Cannot determine image type"))
313 (setq type (intern extension)))))
314 (t
315 ;; FILE-OR-DATA contains image data.
316 (unless type
317 (setq type (image-type-from-data file-or-data)))))
318 (unless type
319 (error "Cannot determine image type"))
320 (unless (symbolp type)
321 (error "Invalid image type `%s'" type))
322 (when (image-type-available-p type)
323 (append (list 'image :type type (if data-p :data :file) file-or-data)
324 props)))
325
326
327 ;;;###autoload
328 (defun put-image (image pos &optional string area)
329 "Put image IMAGE in front of POS in the current buffer.
330 IMAGE must be an image created with `create-image' or `defimage'.
331 IMAGE is displayed by putting an overlay into the current buffer with a
332 `before-string' STRING that has a `display' property whose value is the
333 image. STRING is defaulted if you omit it.
334 POS may be an integer or marker.
335 AREA is where to display the image. AREA nil or omitted means
336 display it in the text area, a value of `left-margin' means
337 display it in the left marginal area, a value of `right-margin'
338 means display it in the right marginal area."
339 (unless string (setq string "x"))
340 (let ((buffer (current-buffer)))
341 (unless (eq (car-safe image) 'image)
342 (error "Not an image: %s" image))
343 (unless (or (null area) (memq area '(left-margin right-margin)))
344 (error "Invalid area %s" area))
345 (setq string (copy-sequence string))
346 (let ((overlay (make-overlay pos pos buffer))
347 (prop (if (null area) image (list (list 'margin area) image))))
348 (put-text-property 0 (length string) 'display prop string)
349 (overlay-put overlay 'put-image t)
350 (overlay-put overlay 'before-string string))))
351
352
353 ;;;###autoload
354 (defun insert-image (image &optional string area slice)
355 "Insert IMAGE into current buffer at point.
356 IMAGE is displayed by inserting STRING into the current buffer
357 with a `display' property whose value is the image. STRING is
358 defaulted if you omit it.
359 AREA is where to display the image. AREA nil or omitted means
360 display it in the text area, a value of `left-margin' means
361 display it in the left marginal area, a value of `right-margin'
362 means display it in the right marginal area.
363 SLICE specifies slice of IMAGE to insert. SLICE nil or omitted
364 means insert whole image. SLICE is a list (X Y WIDTH HEIGHT)
365 specifying the X and Y positions and WIDTH and HEIGHT of image area
366 to insert. A float value 0.0 - 1.0 means relative to the width or
367 height of the image; integer values are taken as pixel values."
368 ;; Use a space as least likely to cause trouble when it's a hidden
369 ;; character in the buffer.
370 (unless string (setq string " "))
371 (unless (eq (car-safe image) 'image)
372 (error "Not an image: %s" image))
373 (unless (or (null area) (memq area '(left-margin right-margin)))
374 (error "Invalid area %s" area))
375 (if area
376 (setq image (list (list 'margin area) image))
377 ;; Cons up a new spec equal but not eq to `image' so that
378 ;; inserting it twice in a row (adjacently) displays two copies of
379 ;; the image. Don't try to avoid this by looking at the display
380 ;; properties on either side so that we DTRT more often with
381 ;; cut-and-paste. (Yanking killed image text next to another copy
382 ;; of it loses anyway.)
383 (setq image (cons 'image (cdr image))))
384 (let ((start (point)))
385 (insert string)
386 (add-text-properties start (point)
387 `(display ,(if slice
388 (list (cons 'slice slice) image)
389 image) rear-nonsticky (display)))))
390
391
392 ;;;###autoload
393 (defun insert-sliced-image (image &optional string area rows cols)
394 "Insert IMAGE into current buffer at point.
395 IMAGE is displayed by inserting STRING into the current buffer
396 with a `display' property whose value is the image. STRING is
397 defaulted if you omit it.
398 AREA is where to display the image. AREA nil or omitted means
399 display it in the text area, a value of `left-margin' means
400 display it in the left marginal area, a value of `right-margin'
401 means display it in the right marginal area.
402 The image is automatically split into ROW x COLS slices."
403 (unless string (setq string " "))
404 (unless (eq (car-safe image) 'image)
405 (error "Not an image: %s" image))
406 (unless (or (null area) (memq area '(left-margin right-margin)))
407 (error "Invalid area %s" area))
408 (if area
409 (setq image (list (list 'margin area) image))
410 ;; Cons up a new spec equal but not eq to `image' so that
411 ;; inserting it twice in a row (adjacently) displays two copies of
412 ;; the image. Don't try to avoid this by looking at the display
413 ;; properties on either side so that we DTRT more often with
414 ;; cut-and-paste. (Yanking killed image text next to another copy
415 ;; of it loses anyway.)
416 (setq image (cons 'image (cdr image))))
417 (let ((x 0.0) (dx (/ 1.0001 (or cols 1)))
418 (y 0.0) (dy (/ 1.0001 (or rows 1))))
419 (while (< y 1.0)
420 (while (< x 1.0)
421 (let ((start (point)))
422 (insert string)
423 (add-text-properties start (point)
424 `(display ,(list (list 'slice x y dx dy) image)
425 rear-nonsticky (display)))
426 (setq x (+ x dx))))
427 (setq x 0.0
428 y (+ y dy))
429 (insert (propertize "\n" 'line-height t)))))
430
431
432
433 ;;;###autoload
434 (defun remove-images (start end &optional buffer)
435 "Remove images between START and END in BUFFER.
436 Remove only images that were put in BUFFER with calls to `put-image'.
437 BUFFER nil or omitted means use the current buffer."
438 (unless buffer
439 (setq buffer (current-buffer)))
440 (let ((overlays (overlays-in start end)))
441 (while overlays
442 (let ((overlay (car overlays)))
443 (when (overlay-get overlay 'put-image)
444 (delete-overlay overlay)))
445 (setq overlays (cdr overlays)))))
446
447 (defun image-search-load-path (file &optional path)
448 (unless path
449 (setq path image-load-path))
450 (let (element found filename)
451 (while (and (not found) (consp path))
452 (setq element (car path))
453 (cond
454 ((stringp element)
455 (setq found
456 (file-readable-p
457 (setq filename (expand-file-name file element)))))
458 ((and (symbolp element) (boundp element))
459 (setq element (symbol-value element))
460 (cond
461 ((stringp element)
462 (setq found
463 (file-readable-p
464 (setq filename (expand-file-name file element)))))
465 ((consp element)
466 (if (setq filename (image-search-load-path file element))
467 (setq found t))))))
468 (setq path (cdr path)))
469 (if found filename)))
470
471 ;;;###autoload
472 (defun find-image (specs)
473 "Find an image, choosing one of a list of image specifications.
474
475 SPECS is a list of image specifications.
476
477 Each image specification in SPECS is a property list. The contents of
478 a specification are image type dependent. All specifications must at
479 least contain the properties `:type TYPE' and either `:file FILE' or
480 `:data DATA', where TYPE is a symbol specifying the image type,
481 e.g. `xbm', FILE is the file to load the image from, and DATA is a
482 string containing the actual image data. The specification whose TYPE
483 is supported, and FILE exists, is used to construct the image
484 specification to be returned. Return nil if no specification is
485 satisfied.
486
487 The image is looked for in `image-load-path'.
488
489 Image files should not be larger than specified by `max-image-size'."
490 (let (image)
491 (while (and specs (null image))
492 (let* ((spec (car specs))
493 (type (plist-get spec :type))
494 (data (plist-get spec :data))
495 (file (plist-get spec :file))
496 found)
497 (when (image-type-available-p type)
498 (cond ((stringp file)
499 (if (setq found (image-search-load-path file))
500 (setq image
501 (cons 'image (plist-put (copy-sequence spec)
502 :file found)))))
503 ((not (null data))
504 (setq image (cons 'image spec)))))
505 (setq specs (cdr specs))))
506 image))
507
508
509 ;;;###autoload
510 (defmacro defimage (symbol specs &optional doc)
511 "Define SYMBOL as an image.
512
513 SPECS is a list of image specifications. DOC is an optional
514 documentation string.
515
516 Each image specification in SPECS is a property list. The contents of
517 a specification are image type dependent. All specifications must at
518 least contain the properties `:type TYPE' and either `:file FILE' or
519 `:data DATA', where TYPE is a symbol specifying the image type,
520 e.g. `xbm', FILE is the file to load the image from, and DATA is a
521 string containing the actual image data. The first image
522 specification whose TYPE is supported, and FILE exists, is used to
523 define SYMBOL.
524
525 Example:
526
527 (defimage test-image ((:type xpm :file \"~/test1.xpm\")
528 (:type xbm :file \"~/test1.xbm\")))"
529 (declare (doc-string 3))
530 `(defvar ,symbol (find-image ',specs) ,doc))
531
532
533 (provide 'image)
534
535 ;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
536 ;;; image.el ends here