Subject: bug#10112: ImageMagick doesn't display some image formats

From: Juri Linkov <[email-masked]>
Date: Fri, 16 Dec 2011 01:57:52 +0200

> But this is still a problem.  Currently we can't view images supported
> by ImageMagick when they are visited from archives.  That's because of
> the problem you mentioned: there are no ImageMagick headers in
> `image-type-header-regexps'.

Since we have only filename extensions to detect ImageMagick-only formats,
but not file headers, one way to solve this problem is to try matching
buffer names when matching file names fails.  This can't be done without
affecting other functionality, so perhaps this should be postponed to 24.2.

> Another problem is that we can't use ImageMagick transformations on
> default image types JPEG, GIF, PNG because they are not visited
> with the help of ImageMagick.

This problem can be fixed without affecting other functionality.
It is localized to just one function `imagemagick-register-types'
so it's safe to install for 24.1.

The patch below does for `image-type-header-regexps' exactly the same thing
as already is done for `image-type-file-name-regexps' - adding entries
that match ImageMagick image formats.  (However, there is one difference
where unlike grouping filename regexps into one regexp with `regexp-opt'
can't be used to group file header regexps; I don't why but the result of
`regexp-opt' matches nothing; maybe because these regexps are quite complicated).

=== modified file 'lisp/image.el'
--- lisp/image.el	2011-09-16 13:46:42 +0000
+++ lisp/image.el	2011-12-15 23:48:03 +0000
@@ -32,7 +32,7 @@ (defgroup image ()
 
 (defalias 'image-refresh 'image-flush)
 
-(defconst image-type-header-regexps
+(defvar image-type-header-regexps
   `(("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm)
     ("\\`P[1-6][[:space:]]+\\(?:#.*[[:space:]]+\\)*[0-9]+[[:space:]]+[0-9]+" . pbm)
     ("\\`GIF8[79]a" . gif)
@@ -702,7 +704,14 @@ (defun imagemagick-register-types ()
       (let ((extension (concat "\\." (regexp-opt im-types) "\\'")))
         (push (cons extension 'image-mode) auto-mode-alist)
         (push (cons extension 'imagemagick)
-              image-type-file-name-regexps)))))
+              image-type-file-name-regexps))
+      (dolist (header-regexp image-type-header-regexps)
+        (let ((im-type (cdr header-regexp)))
+	  (when (and (member (symbol-name im-type) im-types)
+		     (not (memq (intern (upcase (symbol-name im-type)))
+				imagemagick-types-inhibit)))
+	    (push (cons (car header-regexp) 'imagemagick)
+		  image-type-header-regexps)))))))
 
 (provide 'image)