2002-01-22 Lars Magne Ingebrigtsen <larsi@gnus.org>
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
29   "*Directory where X-Face PBM files are stored."
30   :group 'gnus-fun
31   :type 'directory)
32
33 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
34   "Command for converting a PBM to an X-Face."
35   :group 'gnus-fun
36   :type 'string)
37
38 (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm 2>/dev/null | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface"
39   "Command for converting a GIF to an X-Face."
40   :group 'gnus-fun
41   :type 'string)
42
43 ;;;###autoload
44 (defun gnus-random-x-face ()
45   "Insert a random X-Face header from `gnus-x-face-directory'."
46   (interactive)
47   (when (file-exists-p gnus-x-face-directory)
48     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
49            (file (nth (random (length files)) files)))
50       (when file
51         (shell-command-to-string
52          (format gnus-convert-pbm-to-x-face-command
53                  (shell-quote-argument file)))))))
54
55 ;;;###autoload
56 (defun gnus-x-face-from-file (file)
57   "Insert an X-Face header based on an image file."
58   (interactive "fImage file name:" )
59   (when (file-exists-p file)
60     (shell-command-to-string
61      (format gnus-convert-image-to-x-face-command
62              (shell-quote-argument file)))))
63
64 (defun gnus-convert-image-to-gray-x-face (file depth)
65   (let* ((mapfile (make-temp-name (expand-file-name "gnus." mm-tmp-directory)))
66          (levels (expt 2 depth))
67          (step (/ 255 (1- levels)))
68          color-alist bits bits-list mask pixel x-faces)
69     (with-temp-file mapfile
70       (insert "P3\n")
71       (insert (format "%d 1\n" levels))
72       (insert "255\n")
73       (dotimes (i levels)
74         (insert (format "%d %d %d\n"
75                         (* step i) (* step i) (* step i)))
76         (push (cons (* step i) i) color-alist)))
77     (when (file-exists-p file)
78       (with-temp-buffer
79         (insert (shell-command-to-string
80                  (format "giftopnm %s | ppmnorm 2>/dev/null | pnmscale -width 48 -height 48 | ppmquant -fs -map %s 2>/dev/null | ppmtopgm | pnmnoraw"
81                          (shell-quote-argument file)
82                          mapfile)))
83         (goto-char (point-min))
84         (forward-line 3)
85         (while (setq pixel (ignore-errors (read (current-buffer))))
86           (push (cdr (assq pixel color-alist)) bits-list))
87         (setq bits-list (nreverse bits-list))
88         (dotimes (bit-number depth)
89           (setq mask (expt 2 bit-number))
90           (with-temp-buffer
91             (insert "P1\n48 48\n")
92             (dolist (bits bits-list)
93               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
94             (shell-command-on-region
95              (point-min) (point-max)
96              ;; the following is taken from xbmtoikon:
97              "pbmtoicon | sed '/^[      ]*[*\\\\/]/d; s/[       ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface"
98              (current-buffer) t)
99             (push (buffer-string) x-faces))))
100       (dotimes (i (length x-faces))
101         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
102                 (nth i x-faces))))
103     (delete-file mapfile)))
104
105 ;;;###autoload
106 (defun gnus-convert-gray-x-face-to-xpm (faces)
107   (let* ((depth (length faces))
108          (scale (/ 255 (1- (expt 2 depth))))
109          (ok-p t)
110          bit-list bit-lists pixels pixel)
111     (dolist (face faces)
112       (setq bit-list nil)
113       (with-temp-buffer
114         (insert (uncompface face))
115         (shell-command-on-region
116          (point-min) (point-max)
117          "pnmnoraw 2>/dev/null"
118          (current-buffer) t)
119         (goto-char (point-min))
120         (forward-line 2)
121         (while (not (eobp))
122           (cond
123            ((eq (following-char) ?0)
124             (push 0 bit-list))
125            ((eq (following-char) ?1)
126             (push 1 bit-list)))
127           (forward-char 1)))
128       (unless (= (length bit-list) (* 48 48))
129         (setq ok-p nil))
130       (push bit-list bit-lists))
131     (when ok-p
132       (dotimes (i (* 48 48))
133         (setq pixel 0)
134         (dotimes (plane depth)
135           (setq pixel (+ (* pixel 2) (nth i (nth plane bit-lists)))))
136         (push pixel pixels))
137       (with-temp-buffer
138         (insert "P2\n48 48\n255\n")
139         (dolist (pixel pixels)
140           (insert (number-to-string (* scale pixel)) " "))
141         (shell-command-on-region
142          (point-min) (point-max)
143          "ppmtoxpm 2>/dev/null"
144          (current-buffer) t)
145         (buffer-string)))))
146
147 ;;;###autoload
148 (defun gnus-convert-gray-x-face-region (beg end)
149   "Convert the X-Faces in region to a PPM file."
150   (interactive "r")
151   (let ((input (buffer-substring beg end))
152         faces)
153     (with-temp-buffer
154       (insert input)
155       (goto-char (point-min))
156       (while (not (eobp))
157         (save-restriction
158           (mail-header-narrow-to-field)
159           (push (mail-header-field-value) faces)
160           (goto-char (point-max)))))
161     (gnus-convert-gray-x-face-to-xpm faces)))
162
163 (defface gnus-x-face '((t (:foreground "black" :background "white")))
164   "Face to show X-Face.
165 The colors from this face are used as the foreground and background
166 colors of the displayed X-Faces."
167   :group 'gnus-article-headers)
168
169 (defun gnus-display-x-face-in-from (data)
170   "Display the X-Face DATA in the From header."
171   (let ((default-enable-multibyte-characters nil)
172         pbm)
173     (when (or (gnus-image-type-available-p 'xface)
174               (and (gnus-image-type-available-p 'pbm)
175                    (setq pbm (uncompface data))))
176       (save-excursion
177         (save-restriction
178           (article-narrow-to-head)
179           (gnus-article-goto-header "from")
180           (when (bobp) 
181             (insert "From: [no `from' set]\n")
182             (forward-char -17))
183           (gnus-add-image
184            'xface
185            (gnus-put-image
186             (if (gnus-image-type-available-p 'xface)
187                 (gnus-create-image
188                  (concat "X-Face: " data)
189                  'xface t :ascent 'center :face 'gnus-x-face)
190               (gnus-create-image
191                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
192           (gnus-add-wash-type 'xface))))))
193
194 (defun gnus-grab-cam-x-face ()
195   "Grab a picture off the camera and make it into an X-Face."
196   (interactive)
197   (shell-command "xawtv-remote snap ppm")
198   (let ((file nil))
199     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
200                                              t "snap.*ppm")))
201       (sleep-for 1))
202     (setq file (car file))
203     (with-temp-buffer
204       (shell-command
205        (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 | ppmtopgm | pgmtopbm -threshold -value 0.92 | pbmtoxbm | compface"
206                file)
207        (current-buffer))
208       ;;(sleep-for 3)
209       (delete-file file)
210       (buffer-string))))
211
212 (defun gnus-grab-gray-x-face ()
213   "Grab a picture off the camera and make it into an X-Face."
214   (interactive)
215   (shell-command "xawtv-remote snap ppm")
216   (let ((file nil))
217     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
218                                              t "snap.*ppm")))
219       (sleep-for 1))
220     (setq file (car file))
221     (with-temp-buffer
222       (shell-command
223        (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
224                file file)
225        (current-buffer))
226       (delete-file file))
227     (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
228     (delete-file (concat file ".gif"))))
229
230 (provide 'gnus-fun)
231
232 ;;; gnus-fun.el ends here