1 ;;; mime-image.el --- mime-view filter to display images
3 ;; Copyright (C) 1995,1996,1997,1998 MORIOKA Tomohiko
4 ;; Copyright (C) 1996 Dan Rich
6 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Dan Rich <drich@morpheus.corp.sgi.com>
8 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
9 ;; Katsumi Yamaoka <yamaoka@jpl.org>
10 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
11 ;; Created: 1995/12/15
12 ;; Renamed: 1997/2/21 from tm-image.el
14 ;; Keywords: image, picture, X-Face, MIME, multimedia, mail, news
16 ;; This file is part of SEMI (Showy Emacs MIME Interfaces).
18 ;; This program is free software; you can redistribute it and/or
19 ;; modify it under the terms of the GNU General Public License as
20 ;; published by the Free Software Foundation; either version 2, or (at
21 ;; your option) any later version.
23 ;; This program is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU XEmacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
34 ;; If you use this program with MULE, please install
35 ;; etl8x16-bitmap.bdf font included in tl package.
39 (eval-when-compile (require 'cl))
41 (eval-when-compile (require 'static))
47 (static-if (featurep 'xemacs)
49 (defun mime-image-type-available-p (type)
50 (memq type (image-instantiator-format-list)))
52 (defun mime-image-create (file-or-data &optional type data-p &rest props)
55 (if (and type (mime-image-type-available-p type))
56 (vector type (if data-p :data :file) file-or-data)
59 (if (nothing-image-instance-p instance) nil
60 (make-glyph instance))))
62 (defun mime-image-insert (image string &optional area)
63 (let ((extent (make-extent (point) (progn (insert string)(point)))))
64 (set-extent-property extent 'invisible t)
65 (set-extent-end-glyph extent image))))
69 (defalias 'mime-image-type-available-p 'image-type-available-p)
70 (defalias 'mime-image-create 'create-image)
71 (defalias 'mime-image-insert 'insert-image))
75 (require (if (featurep 'mule) 'bitmap ""))
76 (defun mime-image-read-xbm-buffer (buffer)
78 (mapconcat #'bitmap-compose
79 (append (bitmap-decode-xbm
80 (bitmap-read-xbm-buffer
81 (current-buffer))) nil) "\n")
83 (defun mime-image-insert (image string &optional area)
86 (defun mime-image-read-xbm-buffer (buffer)
89 (let ((case-fold-search t) width height xbytes right margin)
90 (goto-char (point-min))
91 (or (re-search-forward "_width[\t ]+\\([0-9]+\\)" nil t)
92 (error "!! Illegal xbm file format" (current-buffer)))
93 (setq width (string-to-int (match-string 1))
94 xbytes (/ (+ width 7) 8))
95 (goto-char (point-min))
96 (or (re-search-forward "_height[\t ]+\\([0-9]+\\)" nil t)
97 (error "!! Illegal xbm file format" (current-buffer)))
98 (setq height (string-to-int (match-string 1)))
99 (goto-char (point-min))
100 (re-search-forward "0x[0-9a-f][0-9a-f],")
101 (delete-region (point-min) (match-beginning 0))
102 (goto-char (point-min))
103 (while (re-search-forward "[\n\r\t ,;}]" nil t)
105 (goto-char (point-min))
106 (while (re-search-forward "0x" nil t)
107 (replace-match "\\x" nil t))
108 (goto-char (point-min))
109 (insert "(" (number-to-string width) " "
110 (number-to-string height) " \"")
111 (goto-char (point-max))
113 (goto-char (point-min))
114 (read (current-buffer)))))
116 (defun mime-image-insert (image string &optional area)
118 (narrow-to-region (point)(point))
119 (let ((face (gensym "mis")))
120 (or (facep face) (make-face face))
121 (set-face-stipple face image)
122 (let ((row (make-string (/ (car image) (frame-char-width)) ? ))
123 (height (/ (nth 1 image) (frame-char-height)))
126 (set-text-properties (point) (progn (insert row)(point))
129 (setq i (1+ i)))))))))
131 (defun mime-image-type-available-p (type)
134 (defun mime-image-create (file-or-data &optional type data-p &rest props)
135 (when (or (null type) (eq type 'xbm))
138 (insert file-or-data)
139 (insert-file-contents file-or-data))
140 (mime-image-read-xbm-buffer (current-buffer))))))))
142 (defvar mime-image-format-alist
149 (image x-xpixmap xpm)
152 (dolist (rule mime-image-format-alist)
153 (when (mime-image-type-available-p (nth 2 rule))
154 (ctree-set-calist-strictly
155 'mime-preview-condition
156 (list (cons 'type (car rule))(cons 'subtype (nth 1 rule))
158 (cons 'body-presentation-method #'mime-display-image)
159 (cons 'image-format (nth 2 rule))))))
162 ;;; @ content filter for images
164 ;; (for XEmacs 19.12 or later)
166 (defun mime-display-image (entity situation)
167 (message "Decoding image...")
168 (let ((format (cdr (assq 'image-format situation)))
170 (make-temp-name (expand-file-name "tm" temporary-file-directory)))
171 (orig-mode (default-file-modes))
175 (set-default-file-modes 448)
176 (mime-write-entity-content entity image-file)
177 (if (null (setq image (mime-image-create image-file format)))
178 (message "Invalid glyph!")
180 (mime-image-insert image "x")
182 (save-window-excursion
183 (set-window-buffer (selected-window)(current-buffer))
185 (message "Decoding image... done"))))
186 (set-default-file-modes orig-mode)
188 (delete-file image-file)
194 (provide 'mime-image)
196 ;;; mime-image.el ends here