(mime-preview-condition): Set 'with-filter in
[elisp/semi.git] / mime-image.el
1 ;;; mime-image.el --- mime-view filter to display images
2
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4 ;; Copyright (C) 1996 Dan Rich
5
6 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Dan Rich <drich@morpheus.corp.sgi.com>
8 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; Created: 1995/12/15
10 ;;      Renamed: 1997/2/21 from tm-image.el
11
12 ;; Keywords: image, picture, X-Face, MIME, multimedia, mail, news
13
14 ;; This file is part of SEMI (Showy Emacs MIME Interfaces).
15
16 ;; This program is free software; you can redistribute it and/or
17 ;; modify it under the terms of the GNU General Public License as
18 ;; published by the Free Software Foundation; either version 2, or (at
19 ;; your option) any later version.
20
21 ;; This program is distributed in the hope that it will be useful, but
22 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 ;; General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU XEmacs; see the file COPYING.  If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
30
31 ;;; Commentary:
32 ;;      If you use this program with MULE, please install
33 ;;      etl8x16-bitmap.bdf font included in tl package.
34
35 ;;; Code:
36
37 (require 'mime-view)
38 (require 'alist)
39
40 (cond ((featurep 'xemacs)
41        (require 'images)
42        
43        (defun-maybe image-inline-p (format)
44          (or (memq format image-native-formats)
45              (find-if (function
46                        (lambda (native)
47                          (image-converter-chain format native)
48                          ))
49                       image-native-formats)
50              ))
51        
52        (image-register-netpbm-utilities)
53        (image-register-converter 'pic 'ppm "pictoppm")
54        (image-register-converter 'mag 'ppm "magtoppm")
55        
56        (defun bitmap-insert-xbm-file (file)
57          (let ((gl (make-glyph (list (cons 'x file))))
58                (e (make-extent (point) (point)))
59                )
60            (set-extent-end-glyph e gl)
61            ))
62        
63        ;;
64        ;; X-Face
65        ;;
66        (autoload 'highlight-headers "highlight-headers")
67        
68        (defun mime-preview-x-face-function-use-highlight-headers ()
69          (highlight-headers (point-min) (re-search-forward "^$" nil t) t)
70          )
71        
72        (add-hook 'mime-view-content-header-filter-hook
73                  'mime-preview-x-face-function-use-highlight-headers)
74        
75        )
76       ((featurep 'mule)
77        ;; for MULE 2.* or mule merged EMACS
78        (require 'x-face-mule)
79
80        (defvar image-native-formats '(xbm))
81        
82        (defun-maybe image-inline-p (format)
83          (memq format image-native-formats)
84          )
85        
86        (defun-maybe image-normalize (format data)
87          (and (eq format 'xbm)
88               (vector 'xbm ':data data)
89               ))
90        
91        ;;
92        ;; X-Face
93        ;;
94        (if (exec-installed-p uncompface-program exec-path)
95            (add-hook 'mime-view-content-header-filter-hook
96                      'x-face-decode-message-header)
97          )
98        ))
99
100 (or (fboundp 'image-invalid-glyph-p)
101     (defsubst image-invalid-glyph-p (glyph)
102       (or (null (aref glyph 0))
103           (null (aref glyph 2))
104           (equal (aref glyph 2) "")
105           ))
106     )
107
108 (defvar mime-view-image-converter-alist nil)
109
110 (mapcar (function
111          (lambda (rule)
112            (let ((type    (car rule))
113                  (subtype (nth 1 rule))
114                  (format  (nth 2 rule)))
115              (if (image-inline-p format)
116                  (let ((type/subtype (mime-type/subtype-string type subtype)))
117                    ;; (set-alist 'mime-view-content-filter-alist
118                    ;;            type/subtype #'mime-view-filter-for-image)
119                    (set-alist 'mime-view-image-converter-alist
120                               type/subtype format)
121                    ;; (add-to-list
122                    ;;  'mime-view-visible-media-type-list
123                    ;;  ctype)
124                    (ctree-set-calist-strictly
125                     'mime-preview-condition
126                     (list (cons 'type type)(cons 'subtype subtype)
127                           '(body . visible)
128                           '(body-presentation-method . with-filter)
129                           (cons 'body-filter
130                                 #'mime-view-filter-for-image)))
131                    )
132                ))))
133         '((image jpeg           jpeg)
134           (image gif            gif)
135           (image tiff           tiff)
136           (image x-tiff         tiff)
137           (image xbm            xbm)
138           (image x-xbm          xbm)
139           (image x-xpixmap      xpm)
140           (image x-pic          pic)
141           (image x-mag          mag)
142           (image png            png)
143           ))
144
145 (defvar mime-view-ps-to-gif-command "pstogif")
146
147
148 ;;; @ content filter for images
149 ;;;
150 ;;    (for XEmacs 19.12 or later)
151
152 (defun mime-view-filter-for-image (ctype params encoding)
153   (let ((beg (point-min))
154         (end (point-max)))
155     (remove-text-properties beg end '(face nil))
156     (message "Decoding image...")
157     (mime-decode-region beg end encoding)
158     (let* ((minor (cdr (assoc ctype mime-view-image-converter-alist)))
159            (gl (image-normalize minor (buffer-string)))
160            e)
161       (delete-region (point-min)(point-max))
162       (cond ((image-invalid-glyph-p gl)
163              (setq gl nil)
164              (message "Invalid glyph!")
165              )
166             ((eq (aref gl 0) 'xbm)
167              (let ((xbm-file
168                     (make-temp-name
169                      (expand-file-name "tm" mime-temp-directory))))
170                (insert (aref gl 2))
171                (write-region (point-min)(point-max) xbm-file)
172                (message "Decoding image...")
173                (delete-region (point-min)(point-max))
174                (bitmap-insert-xbm-file xbm-file)
175                (delete-file xbm-file)
176                )
177              (message "Decoding image... done")
178              )
179             (t
180              (setq gl (make-glyph gl))
181              (setq e (make-extent (point) (point)))
182              (set-extent-end-glyph e gl)
183              (message "Decoding image... done")
184              ))
185       )
186     (insert "\n")
187     ))
188
189
190 ;;; @ content filter for Postscript
191 ;;;
192 ;;    (for XEmacs 19.14 or later)
193
194 (defun mime-view-filter-for-application/postscript (ctype params encoding)
195   (let* ((beg (point-min)) (end (point-max))
196          (file-base
197           (make-temp-name (expand-file-name "tm" mime-temp-directory)))
198          (ps-file (concat file-base ".ps"))
199          (gif-file (concat file-base ".gif"))
200          )
201     (remove-text-properties beg end '(face nil))
202     (message "Decoding Postscript...")
203     (mime-decode-region beg end encoding)
204     (write-region (point-min)(point-max) ps-file) 
205     (message "Decoding Postscript...")
206     (delete-region (point-min)(point-max))
207     (call-process mime-view-ps-to-gif-command nil nil nil ps-file)
208     (set-extent-end-glyph (make-extent (point) (point))
209                           (make-glyph (vector 'gif :file gif-file)))
210     (message "Decoding Postscript... done")
211     (delete-file ps-file)
212     (delete-file gif-file)
213     ))
214
215 ;; If you would like to display inline Postscript image, please
216 ;; activate following:
217
218 ;; (set-alist 'mime-view-content-filter-alist
219 ;;            "application/postscript"
220 ;;            (function mime-view-filter-for-application/postscript))
221
222 ;; (if (featurep 'gif)
223 ;;     (add-to-list
224 ;;      'mime-view-visible-media-type-list "application/postscript")
225 ;;   )
226
227
228 ;;; @ end
229 ;;;
230
231 (provide 'mime-image)
232
233 ;;; mime-image.el ends here