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@unixuser.org>
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 (defsubst mime-image-normalize-xbm-buffer (buffer)
50 (let ((case-fold-search t) width height xbytes right margin)
51 (goto-char (point-min))
52 (or (re-search-forward "_width[\t ]+\\([0-9]+\\)" nil t)
53 (error "!! Illegal xbm file format" (current-buffer)))
54 (setq width (string-to-int (match-string 1))
55 xbytes (/ (+ width 7) 8))
56 (goto-char (point-min))
57 (or (re-search-forward "_height[\t ]+\\([0-9]+\\)" nil t)
58 (error "!! Illegal xbm file format" (current-buffer)))
59 (setq height (string-to-int (match-string 1)))
60 (goto-char (point-min))
61 (re-search-forward "0x[0-9a-f][0-9a-f],")
62 (delete-region (point-min) (match-beginning 0))
63 (goto-char (point-min))
64 (while (re-search-forward "[\n\r\t ,;}]" nil t)
66 (goto-char (point-min))
67 (while (re-search-forward "0x" nil t)
68 (replace-match "\\x" nil t))
69 (goto-char (point-min))
70 (insert "(" (number-to-string width) " "
71 (number-to-string height) " \"")
72 (goto-char (point-max))
74 (goto-char (point-min))
75 (read (current-buffer)))))
77 (static-if (featurep 'xemacs)
79 (defun mime-image-type-available-p (type)
80 (memq type (image-instantiator-format-list)))
82 (defun mime-image-create (file-or-data &optional type data-p &rest props)
83 (when (and data-p (eq type 'xbm))
87 (mime-image-normalize-xbm-buffer (current-buffer)))))
90 (if (and type (mime-image-type-available-p type))
92 (list type (if data-p :data :file) file-or-data)
95 (if (nothing-image-instance-p (glyph-image-instance glyph)) nil
98 (defun mime-image-insert (image &optional string area)
99 (let ((extent (make-extent (point)
103 (set-extent-property extent 'invisible t)
104 (set-extent-end-glyph extent image))))
108 (defalias 'mime-image-type-available-p 'image-type-available-p)
109 (defun mime-image-create
110 (file-or-data &optional type data-p &rest props)
111 (if (and data-p (eq type 'xbm))
113 (insert file-or-data)
115 (mime-image-normalize-xbm-buffer (current-buffer)))
116 (apply #'create-image (nth 2 file-or-data) type data-p
118 (list :width (car file-or-data)
119 :height (nth 1 file-or-data))
121 (apply #'create-image file-or-data type data-p props)))
122 (defalias 'mime-image-insert 'insert-image))
126 (require (if (featurep 'mule) 'bitmap ""))
127 (defun mime-image-read-xbm-buffer (buffer)
129 (mapconcat #'bitmap-compose
130 (append (bitmap-decode-xbm
131 (bitmap-read-xbm-buffer
132 (current-buffer))) nil) "\n")
134 (defun mime-image-insert (image &optional string area)
137 (defalias 'mime-image-read-xbm-buffer
138 'mime-image-normalize-xbm-buffer)
139 (defun mime-image-insert (image &optional string area)
141 (narrow-to-region (point)(point))
142 (let ((face (gensym "mii")))
143 (or (facep face) (make-face face))
144 (set-face-stipple face image)
145 (let ((row (make-string (/ (car image) (frame-char-width)) ? ))
146 (height (/ (nth 1 image) (frame-char-height)))
149 (set-text-properties (point) (progn (insert row)(point))
152 (setq i (1+ i)))))))))
154 (defun mime-image-type-available-p (type)
157 (defun mime-image-create (file-or-data &optional type data-p &rest props)
158 (when (or (null type) (eq type 'xbm))
161 (insert file-or-data)
162 (insert-file-contents file-or-data))
163 (mime-image-read-xbm-buffer (current-buffer))))))))
165 (defvar mime-image-format-alist
172 (image x-xpixmap xpm)
175 (dolist (rule mime-image-format-alist)
176 (when (mime-image-type-available-p (nth 2 rule))
177 (ctree-set-calist-strictly
178 'mime-preview-condition
179 (list (cons 'type (car rule))(cons 'subtype (nth 1 rule))
181 (cons 'body-presentation-method #'mime-display-image)
182 (cons 'image-format (nth 2 rule))))))
185 ;;; @ content filter for images
187 ;; (for XEmacs 19.12 or later)
189 (defun mime-display-image (entity situation)
190 (message "Decoding image...")
191 (let ((format (cdr (assq 'image-format situation)))
193 (setq image (mime-image-create (mime-entity-content entity) format 'data))
195 (message "Invalid glyph!")
197 (mime-image-insert image)
199 (message "Decoding image... done")))))
204 (provide 'mime-image)
206 ;;; mime-image.el ends here