Refill some copyright headers.
[bpt/emacs.git] / lisp / image.el
index 4594ae1..282b81d 100644 (file)
@@ -1,10 +1,11 @@
 ;;; image.el --- image API
 
-;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
-;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
+;;   2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: multimedia
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
   "Image support."
   :group 'multimedia)
 
+(defalias 'image-refresh 'image-flush)
 
 (defconst image-type-header-regexps
   `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
     ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
     ("\\`GIF8[79]a" . gif)
     ("\\`\x89PNG\r\n\x1a\n" . png)
-    ("\\`[\t\n\r ]*#define \\([a-z0-9]+\\)_width [0-9]+\n\
-#define \\1_height [0-9]+\n\
-static char \\1_bits" . xbm)
+    ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\
+#define \\1_height [0-9]+\n\\(\
+#define \\1_x_hot [0-9]+\n\
+#define \\1_y_hot [0-9]+\n\\)?\
+static \\(unsigned \\)?char \\1_bits" . xbm)
     ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff)
     ("\\`[\t\n\r ]*%!PS" . postscript)
-    ("\\`\xff\xd8" . (image-jpeg-p . jpeg))
+    ("\\`\xff\xd8" . jpeg)    ; used to be (image-jpeg-p . jpeg)
     (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)")
             (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)")))
        (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<"
@@ -96,18 +100,17 @@ AUTODETECT can be
  - maybe  auto-detect only if the image type is available
            (see `image-type-available-p').")
 
-(defvar image-load-path nil
+(defcustom image-load-path
+  (list (file-name-as-directory (expand-file-name "images" data-directory))
+        'data-directory 'load-path)
   "List of locations in which to search for image files.
 If an element is a string, it defines a directory to search.
 If an element is a variable symbol whose value is a string, that
 value defines a directory to search.
 If an element is a variable symbol whose value is a list, the
-value is used as a list of directories to search.")
-
-(eval-at-startup
- (setq image-load-path
-       (list (file-name-as-directory (expand-file-name "images" data-directory))
-            'data-directory 'load-path)))
+value is used as a list of directories to search."
+  :type '(repeat (choice directory variable))
+  :initialize 'custom-initialize-delay)
 
 
 (defun image-load-path-for-library (library image &optional path no-error)
@@ -121,11 +124,11 @@ in \"`data-directory'/images\".
 
 Then this function returns a list of directories which contains
 first the directory in which IMAGE was found, followed by the
-value of `load-path'. If PATH is given, it is used instead of
+value of `load-path'.  If PATH is given, it is used instead of
 `load-path'.
 
 If NO-ERROR is non-nil and a suitable path can't be found, don't
-signal an error. Instead, return a list of directories as before,
+signal an error.  Instead, return a list of directories as before,
 except that nil appears in place of the image directory.
 
 Here is an example that uses a common idiom to provide
@@ -208,7 +211,8 @@ compatibility with versions of Emacs that lack the variable
 (defun image-jpeg-p (data)
   "Value is non-nil if DATA, a string, consists of JFIF image data.
 We accept the tag Exif because that is the same format."
-  (when (string-match "\\`\xff\xd8" data)
+  (setq data (ignore-errors (string-to-unibyte data)))
+  (when (and data (string-match-p "\\`\xff\xd8" data))
     (catch 'jfif
       (let ((len (length data)) (i 2))
        (while (< i len)
@@ -223,8 +227,8 @@ We accept the tag Exif because that is the same format."
            (when (and (>= code #xe0) (<= code #xef))
              ;; APP0 LEN1 LEN2 "JFIF\0"
              (throw 'jfif
-                    (string-match "JFIF\\|Exif"
-                                  (substring data i (min (+ i nbytes) len)))))
+                    (string-match-p "JFIF\\|Exif"
+                                    (substring data i (min (+ i nbytes) len)))))
            (setq i (+ i 1 nbytes))))))))
 
 
@@ -239,7 +243,7 @@ be determined."
       (let ((regexp (car (car types)))
            (image-type (cdr (car types))))
        (if (or (and (symbolp image-type)
-                    (string-match regexp data))
+                    (string-match-p regexp data))
                (and (consp image-type)
                     (funcall (car image-type) data)
                     (setq image-type (cdr image-type))))
@@ -263,7 +267,7 @@ be determined."
            (image-type (cdr (car types)))
            data)
        (if (or (and (symbolp image-type)
-                    (looking-at regexp))
+                    (looking-at-p regexp))
                (and (consp image-type)
                     (funcall (car image-type)
                              (or data
@@ -301,7 +305,7 @@ be determined."
   "Determine the type of image file FILE from its name.
 Value is a symbol specifying the image type, or nil if type cannot
 be determined."
-  (assoc-default file image-type-file-name-regexps 'string-match))
+  (assoc-default file image-type-file-name-regexps 'string-match-p))
 
 
 ;;;###autoload
@@ -326,12 +330,16 @@ Optional DATA-P non-nil means SOURCE is a string containing image data."
   type)
 
 
+(define-obsolete-variable-alias
+    'image-library-alist
+    'dynamic-library-alist "24.1")
+
 ;;;###autoload
 (defun image-type-available-p (type)
   "Return non-nil if image type TYPE is available.
 Image types are symbols like `xbm' or `jpeg'."
   (and (fboundp 'init-image-library)
-       (init-image-library type image-library-alist)))
+       (init-image-library type dynamic-library-alist)))
 
 
 ;;;###autoload
@@ -450,7 +458,7 @@ AREA is where to display the image.  AREA nil or omitted means
 display it in the text area, a value of `left-margin' means
 display it in the left marginal area, a value of `right-margin'
 means display it in the right marginal area.
-The image is automatically split into ROW x COLS slices."
+The image is automatically split into ROWS x COLS slices."
   (unless string (setq string " "))
   (unless (eq (car-safe image) 'image)
     (error "Not an image: %s" image))
@@ -580,8 +588,141 @@ Example:
   (declare (doc-string 3))
   `(defvar ,symbol (find-image ',specs) ,doc))
 
+\f
+;;; Animated image API
+
+(defcustom image-animate-max-time 30
+  "Time in seconds to animate images."
+  :type 'integer
+  :version "24.1"
+  :group 'image)
+
+(defconst image-animated-types '(gif)
+  "List of supported animated image types.")
+
+;;;###autoload
+(defun create-animated-image (file-or-data &optional type data-p &rest props)
+  "Create an animated image.
+FILE-OR-DATA is an image file name or image data.
+Optional TYPE is a symbol describing the image type.  If TYPE is omitted
+or nil, try to determine the image type from its first few bytes
+of image data.  If that doesn't work, and FILE-OR-DATA is a file name,
+use its file extension as image type.
+Optional DATA-P non-nil means FILE-OR-DATA is a string containing image data.
+Optional PROPS are additional image attributes to assign to the image,
+like, e.g. `:mask MASK'.
+Value is the image created, or nil if images of type TYPE are not supported.
+
+Images should not be larger than specified by `max-image-size'."
+  (setq type (image-type file-or-data type data-p))
+  (when (image-type-available-p type)
+    (let* ((animate (memq type image-animated-types))
+          (image
+           (append (list 'image :type type (if data-p :data :file) file-or-data)
+                   (if animate '(:index 0))
+                   props)))
+      (if animate
+         (image-animate-start image))
+      image)))
+
+(defun image-animate-timer (image)
+  "Return the animation timer for image IMAGE."
+  ;; See cancel-function-timers
+  (let ((tail timer-list) timer)
+    (while tail
+      (setq timer (car tail)
+           tail (cdr tail))
+      (if (and (eq (aref timer 5) #'image-animate-timeout)
+              (consp (aref timer 6))
+              (eq (car (aref timer 6)) image))
+         (setq tail nil)
+       (setq timer nil)))
+    timer))
+
+(defun image-animate-start (image &optional max-time)
+  "Start animation of image IMAGE.
+Optional second arg MAX-TIME is number of seconds to animate image,
+or t to animate infinitely."
+  (let ((anim (image-animated-p image))
+       timer tmo)
+    (when anim
+      (if (setq timer (image-animate-timer image))
+         (setcar (nthcdr 3 (aref timer 6)) max-time)
+       (setq tmo (* (cdr anim) 0.01))
+       (setq max-time (or max-time image-animate-max-time))
+       (run-with-timer tmo nil #'image-animate-timeout
+                       image 1 (car anim)
+                       (if (numberp max-time)
+                           (- max-time tmo)
+                         max-time))))))
+
+(defun image-animate-stop (image)
+  "Stop animation of image."
+  (let ((timer (image-animate-timer image)))
+    (when timer
+      (cancel-timer timer))))
+
+(defun image-animate-timeout (image ino count time-left)
+  (if (>= ino count)
+      (setq ino 0))
+  (plist-put (cdr image) :index ino)
+  (force-window-update)
+  (let ((anim (image-animated-p image)) tmo)
+    (when anim
+      (setq tmo (* (cdr anim) 0.01))
+      (unless (and (= ino 0) (numberp time-left) (< time-left tmo))
+       (run-with-timer tmo nil #'image-animate-timeout
+                       image (1+ ino) count
+                       (if (numberp time-left)
+                           (- time-left tmo)
+                         time-left))))))
+
+(defun image-animated-p (image)
+  "Return non-nil if image is animated.
+Actually, return value is a cons (IMAGES . DELAY) where IMAGES
+is the number of sub-images in the animated image, and DELAY
+is the delay in 100ths of a second until the next sub-image
+shall be displayed."
+  (cond
+   ((eq (plist-get (cdr image) :type) 'gif)
+    (let* ((metadata (image-metadata image))
+          (images (plist-get metadata 'count))
+          (extdata (plist-get metadata 'extension-data))
+          (anim (plist-get extdata #xF9))
+          (tmo (and (integerp images) (> images 1)
+                    (stringp anim) (>= (length anim) 4)
+                    (+ (aref anim 1) (* (aref anim 2) 256)))))
+      (when tmo
+       (if (eq tmo 0) (setq tmo 10))
+       (cons images tmo))))))
+
+\f
+(defcustom imagemagick-types-inhibit
+  '(C HTML HTM TXT PDF)
+  ;; FIXME what are the possible options?
+  ;; Are these actually file-name extensions?
+  ;; Why are these upper-case when eg image-types is lower-case?
+  "Types the ImageMagick loader should not try to handle."
+  :type '(choice (const :tag "Let ImageMagick handle all the types it can" nil)
+                (repeat symbol))
+  :version "24.1"
+  :group 'image)
+
+;;;###autoload
+(defun imagemagick-register-types ()
+  "Register the file types that ImageMagick is able to handle."
+  (let ((im-types (imagemagick-types)))
+    (dolist (im-inhibit imagemagick-types-inhibit)
+      (setq im-types (remove im-inhibit im-types)))
+    (dolist (im-type im-types)
+      (let ((extension (downcase (symbol-name im-type))))
+       (push
+        (cons (concat "\\." extension "\\'") 'image-mode)
+        auto-mode-alist)
+       (push
+        (cons (concat "\\." extension "\\'") 'imagemagick)
+        image-type-file-name-regexps)))))
 
 (provide 'image)
 
-;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
 ;;; image.el ends here