+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 (+ i nbytes)))))
+ (setq i (+ i 1 nbytes))))))))