Add arch taglines
[bpt/emacs.git] / lisp / image.el
index e0d19f5..0e71bd4 100644 (file)
@@ -1,6 +1,8 @@
 ;;; image.el --- image API
 
 ;; Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+
+;; Maintainer: FSF
 ;; Keywords: multimedia
 
 ;; This file is part of GNU Emacs.
 
 
 (defconst image-type-regexps
-  '(("\\`/\\*.*XPM.\\*/" . xpm)
+  '(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
     ("\\`P[1-6]" . pbm)
     ("\\`GIF8" . gif)
-    ;; The following is from JPEG File Interchange Format, Version 1.02.
-    ("\\`\xff\xd8\xff\xe0..JFIF\0" . jpeg)
     ("\\`\211PNG\r\n" . png)
-    ("\\`#define" . xbm)
-    ("\\`\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
-    ("\\`%!PS" . postscript))
+    ("\\`[\t\n\r ]*#define" . xbm)
+    ("\\`\\(MM\0\\*\\|II\\*\0\\)" . tiff)
+    ("\\`[\t\n\r ]*%!PS" . postscript)
+    ("\\`\xff\xd8" . (image-jpeg-p . jpeg)))
   "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
 When the first bytes of an image file match REGEXP, it is assumed to
-be of image type IMAGE-TYPE.")
+be of image type IMAGE-TYPE if IMAGE-TYPE is a symbol.  If not a symbol,
+IMAGE-TYPE must be a pair (PREDICATE . TYPE).  PREDICATE is called
+with one argument, a string containing the image data.  If PREDICATE returns
+a non-nil value, TYPE is the image's type.")
+
+
+(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)
+    (catch 'jfif
+      (let ((len (length data)) (i 2))
+       (while (< i len)
+         (when (/= (aref data i) #xff)
+           (throw 'jfif nil))
+         (setq i (1+ i))
+         (when (>= (+ i 2) len)
+           (throw 'jfif nil))
+         (let ((nbytes (+ (lsh (aref data (+ i 1)) 8)
+                          (aref data (+ i 2))))
+               (code (aref data i)))
+           (when (and (>= code #xe0) (<= code #xef))
+             ;; APP0 LEN1 LEN2 "JFIF\0"
+             (throw 'jfif
+                    (string-match "JFIF\\|Exif"
+                                  (substring data i (min (+ i nbytes) len)))))
+           (setq i (+ i 1 nbytes))))))))
 
 
 ;;;###autoload
@@ -55,7 +82,11 @@ be determined."
     (while (and types (null type))
       (let ((regexp (car (car types)))
            (image-type (cdr (car types))))
-       (when (string-match regexp data)
+       (when (or (and (symbolp image-type)
+                      (string-match regexp data))
+                 (and (consp image-type)
+                      (funcall (car image-type) data)
+                      (setq image-type (cdr image-type))))
          (setq type image-type))
        (setq types (cdr types))))
     type))
@@ -70,6 +101,7 @@ be determined."
     (setq file (expand-file-name file data-directory)))
   (setq file (expand-file-name file))
   (let ((header (with-temp-buffer
+                 (set-buffer-multibyte nil)
                  (insert-file-contents-literally file nil 0 256)
                  (buffer-string))))
     (image-type-from-data header)))
@@ -172,11 +204,7 @@ means display it in the right marginal area."
   (let ((start (point)))
     (insert string)
     (add-text-properties start (point)
-                        (list 'display image
-                              ;; `image' has the right properties to
-                              ;; mark an intangible field.
-                              'intangible image
-                              'rear-nonsticky (list 'display)))))
+                        `(display ,image rear-nonsticky (display)))))
 
 
 ;;;###autoload
@@ -265,4 +293,5 @@ Example:
 
 (provide 'image)
 
+;;; arch-tag: 8e76a07b-eb48-4f3e-a7a0-1a7ba9f096b3
 ;;; image.el ends here