2003-01-12 Raymond Scholz <ray-2003@zonix.de>
[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 | 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 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
44   "Command for converting a GIF to an X-Face."
45   :group 'gnus-fun
46   :type 'string)
47
48 (defun gnus-shell-command-to-string (command)
49   "Like `shell-command-to-string' except not mingling ERROR."
50   (with-output-to-string
51     (call-process shell-file-name nil (list standard-output nil)
52                   nil shell-command-switch command)))
53
54 (defun gnus-shell-command-on-region (start end command)
55   "A simplified `shell-command-on-region'.
56 Output to the current buffer, replace text, and don't mingle error."
57   (call-process-region start end shell-file-name t
58                        (list (current-buffer) nil)
59                        nil shell-command-switch command))
60
61 ;;;###autoload
62 (defun gnus-random-x-face ()
63   "Insert a random X-Face header from `gnus-x-face-directory'."
64   (interactive)
65   (when (file-exists-p gnus-x-face-directory)
66     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
67            (file (nth (random (length files)) files)))
68       (when file
69         (gnus-shell-command-to-string
70          (format gnus-convert-pbm-to-x-face-command
71                  (shell-quote-argument file)))))))
72
73 ;;;###autoload
74 (defun gnus-x-face-from-file (file)
75   "Insert an X-Face header based on an image file."
76   (interactive "fImage file name:" )
77   (when (file-exists-p file)
78     (gnus-shell-command-to-string
79      (format gnus-convert-image-to-x-face-command
80              (shell-quote-argument file)))))
81
82 ;;;###autoload
83 (defun gnus-face-from-file (file)
84   "Return an Face header based on an image file."
85   (interactive "fImage file name:" )
86   (when (file-exists-p file)
87     (let ((done nil)
88           (attempt "")
89           (step 72)
90           (quant 16))
91       (while (and (not done)
92                   (> quant 1))
93         (setq attempt
94               (gnus-shell-command-to-string
95                (format gnus-convert-image-to-face-command
96                        (shell-quote-argument file)
97                        quant)))
98         (if (> (length attempt) 740)
99             (progn
100               (setq quant (- quant 2))
101               (message "Length %d; trying quant %d"
102                        (length attempt) quant))
103           (setq done t)))
104       (if done
105           (mm-with-unibyte-buffer       
106             (insert attempt)
107             (base64-encode-region (point-min) (point-max))
108             (goto-char (point-min))
109             (while (search-forward "\n" nil t)
110               (replace-match ""))
111             (goto-char (point-min))
112             (while (> (- (point-max) (point))
113                       step)
114               (forward-char step)
115               (insert "\n ")
116               (setq step 76))
117             (buffer-string))
118         nil))))
119
120 ;;;###autoload
121 (defun gnus-convert-face-to-png (face)
122   (mm-with-unibyte-buffer
123     (insert face)
124     (base64-decode-region (point-min) (point-max))
125     (buffer-string)))
126
127 (defun gnus-convert-image-to-gray-x-face (file depth)
128   (let* ((mapfile (mm-make-temp-file (expand-file-name "gnus." 
129                                                        mm-tmp-directory)))
130          (levels (expt 2 depth))
131          (step (/ 255 (1- levels)))
132          color-alist bits bits-list mask pixel x-faces)
133     (with-temp-file mapfile
134       (insert "P3\n")
135       (insert (format "%d 1\n" levels))
136       (insert "255\n")
137       (dotimes (i levels)
138         (insert (format "%d %d %d\n"
139                         (* step i) (* step i) (* step i)))
140         (push (cons (* step i) i) color-alist)))
141     (when (file-exists-p file)
142       (with-temp-buffer
143         (insert (gnus-shell-command-to-string
144                  (format "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant -fs -map %s | ppmtopgm | pnmnoraw"
145                          (shell-quote-argument file)
146                          mapfile)))
147         (goto-char (point-min))
148         (forward-line 3)
149         (while (setq pixel (ignore-errors (read (current-buffer))))
150           (push (cdr (assq pixel color-alist)) bits-list))
151         (setq bits-list (nreverse bits-list))
152         (dotimes (bit-number depth)
153           (setq mask (expt 2 bit-number))
154           (with-temp-buffer
155             (insert "P1\n48 48\n")
156             (dolist (bits bits-list)
157               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
158             (gnus-shell-command-on-region
159              (point-min) (point-max)
160              ;; the following is taken from xbmtoikon:
161              "pbmtoicon | sed '/^[      ]*[*\\\\/]/d; s/[       ]//g; s/,$//' | tr , '\\012' | sed 's/^0x//; s/^/0x/' | pr -l1 -t -w22 -3 -s, | sed 's/,*$/,/' | compface")
162             (push (buffer-string) x-faces))))
163       (dotimes (i (length x-faces))
164         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
165                 (nth i x-faces))))
166     (delete-file mapfile)))
167
168 ;;;###autoload
169 (defun gnus-convert-gray-x-face-to-xpm (faces)
170   (let* ((depth (length faces))
171          (scale (/ 255 (1- (expt 2 depth))))
172          (ok-p t)
173          (coding-system-for-read 'binary)
174          (coding-system-for-write 'binary)
175          default-enable-multibyte-characters
176          start bit-array bit-arrays pixel)
177     (with-temp-buffer
178       (dolist (face faces)
179         (erase-buffer)
180         (insert (uncompface face))
181         (gnus-shell-command-on-region
182          (point-min) (point-max)
183          "pnmnoraw")
184         (goto-char (point-min))
185         (forward-line 2)
186         (setq start (point))
187         (insert "[")
188         (while (not (eobp))
189           (forward-char 1)
190           (insert " "))
191         (insert "]")
192         (goto-char start)
193         (setq bit-array (read (current-buffer)))
194         (unless (= (length bit-array) (* 48 48))
195           (setq ok-p nil))
196         (push bit-array bit-arrays))
197       (when ok-p
198         (erase-buffer)
199         (insert "P2\n48 48\n255\n")
200         (dotimes (i (* 48 48))
201           (setq pixel 0)
202           (dotimes (plane depth)
203             (setq pixel (+ (* pixel 2) (aref (nth plane bit-arrays) i))))
204           (insert (number-to-string (* scale pixel)) " "))
205         (gnus-shell-command-on-region
206          (point-min) (point-max)
207          "ppmtoxpm")
208         (buffer-string)))))
209
210 ;;;###autoload
211 (defun gnus-convert-gray-x-face-region (beg end)
212   "Convert the X-Faces in region to a PPM file."
213   (interactive "r")
214   (let ((input (buffer-substring beg end))
215         faces)
216     (with-temp-buffer
217       (insert input)
218       (goto-char (point-min))
219       (while (not (eobp))
220         (save-restriction
221           (mail-header-narrow-to-field)
222           (push (mail-header-field-value) faces)
223           (goto-char (point-max)))))
224     (gnus-convert-gray-x-face-to-xpm faces)))
225
226 (defface gnus-x-face '((t (:foreground "black" :background "white")))
227   "Face to show X-Face.
228 The colors from this face are used as the foreground and background
229 colors of the displayed X-Faces."
230   :group 'gnus-article-headers)
231
232 (defun gnus-display-x-face-in-from (data)
233   "Display the X-Face DATA in the From header."
234   (let ((default-enable-multibyte-characters nil)
235         pbm)
236     (when (or (gnus-image-type-available-p 'xface)
237               (and (gnus-image-type-available-p 'pbm)
238                    (setq pbm (uncompface data))))
239       (save-excursion
240         (save-restriction
241           (article-narrow-to-head)
242           (gnus-article-goto-header "from")
243           (when (bobp)
244             (insert "From: [no `from' set]\n")
245             (forward-char -17))
246           (gnus-add-image
247            'xface
248            (gnus-put-image
249             (if (gnus-image-type-available-p 'xface)
250                 (gnus-create-image
251                  (concat "X-Face: " data)
252                  'xface t :ascent 'center :face 'gnus-x-face)
253               (gnus-create-image
254                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
255           (gnus-add-wash-type 'xface))))))
256
257 (defun gnus-grab-cam-x-face ()
258   "Grab a picture off the camera and make it into an X-Face."
259   (interactive)
260   (shell-command "xawtv-remote snap ppm")
261   (let ((file nil))
262     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
263                                              t "snap.*ppm")))
264       (sleep-for 1))
265     (setq file (car file))
266     (with-temp-buffer
267       (shell-command
268        (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"
269                file)
270        (current-buffer))
271       ;;(sleep-for 3)
272       (delete-file file)
273       (buffer-string))))
274
275 (defun gnus-grab-gray-x-face ()
276   "Grab a picture off the camera and make it into an X-Face."
277   (interactive)
278   (shell-command "xawtv-remote snap ppm")
279   (let ((file nil))
280     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
281                                              t "snap.*ppm")))
282       (sleep-for 1))
283     (setq file (car file))
284     (with-temp-buffer
285       (shell-command
286        (format "pnmcut -left 70 -top 100 -width 144 -height 144 '%s' | ppmquant 256 2>/dev/null | ppmtogif > '%s.gif'"
287                file file)
288        (current-buffer))
289       (delete-file file))
290     (gnus-convert-image-to-gray-x-face (concat file ".gif") 3)
291     (delete-file (concat file ".gif"))))
292
293 (provide 'gnus-fun)
294
295 ;;; gnus-fun.el ends here