(gnus-fun-ppm-change-string): New function.
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivoluos extension functions to Gnus
2 ;; Copyright (C) 2002, 2003 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 an image to an X-Face.
40 By default it takes a GIF filename and output the X-Face header data
41 on stdout."
42   :group 'gnus-fun
43   :type 'string)
44
45 (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng"
46   "Command for converting an image to an Face.
47 By default it takes a JPEG filename and output the Face header data
48 on stdout."
49   :group 'gnus-fun
50   :type 'string)
51
52 (defun gnus-shell-command-to-string (command)
53   "Like `shell-command-to-string' except not mingling ERROR."
54   (with-output-to-string
55     (call-process shell-file-name nil (list standard-output nil)
56                   nil shell-command-switch command)))
57
58 (defun gnus-shell-command-on-region (start end command)
59   "A simplified `shell-command-on-region'.
60 Output to the current buffer, replace text, and don't mingle error."
61   (call-process-region start end shell-file-name t
62                        (list (current-buffer) nil)
63                        nil shell-command-switch command))
64
65 ;;;###autoload
66 (defun gnus-random-x-face ()
67   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
68   (interactive)
69   (when (file-exists-p gnus-x-face-directory)
70     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
71            (file (nth (random (length files)) files)))
72       (when file
73         (gnus-shell-command-to-string
74          (format gnus-convert-pbm-to-x-face-command
75                  (shell-quote-argument file)))))))
76
77 ;;;###autoload
78 (defun gnus-insert-random-x-face-header ()
79   "Insert a random X-Face header from `gnus-x-face-directory'."
80   (interactive)
81   (let ((data (gnus-random-x-face)))
82     (save-excursion
83       (message-goto-eoh)
84       (if data
85           (insert "X-Face: " data)
86         (message
87          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
88          gnus-x-face-directory)))))
89
90 ;;;###autoload
91 (defun gnus-x-face-from-file (file)
92   "Insert an X-Face header based on an image file."
93   (interactive "fImage file name (by default GIF): ")
94   (when (file-exists-p file)
95     (gnus-shell-command-to-string
96      (format gnus-convert-image-to-x-face-command
97              (shell-quote-argument (expand-file-name file))))))
98
99 ;;;###autoload
100 (defun gnus-face-from-file (file)
101   "Return an Face header based on an image file."
102   (interactive "fImage file name (by default JPEG): ")
103   (when (file-exists-p file)
104     (let ((done nil)
105           (attempt "")
106           (quant 16))
107       (while (and (not done)
108                   (> quant 1))
109         (setq attempt
110               (gnus-shell-command-to-string
111                (format gnus-convert-image-to-face-command
112                        (shell-quote-argument (expand-file-name file))
113                        quant)))
114         (if (> (length attempt) 740)
115             (progn
116               (setq quant (- quant 2))
117               (message "Length %d; trying quant %d"
118                        (length attempt) quant))
119           (setq done t)))
120       (if done
121           (mm-with-unibyte-buffer       
122             (insert attempt)
123             (gnus-face-encode))
124         nil))))
125
126 (defun gnus-face-encode ()
127   (let ((step 72))
128     (base64-encode-region (point-min) (point-max))
129     (goto-char (point-min))
130     (while (search-forward "\n" nil t)
131       (replace-match ""))
132     (goto-char (point-min))
133     (while (> (- (point-max) (point))
134               step)
135       (forward-char step)
136       (insert "\n ")
137       (setq step 76))
138     (buffer-string)))
139
140 ;;;###autoload
141 (defun gnus-convert-face-to-png (face)
142   "Convert FACE (which is base64-encoded) to a PNG.
143 The PNG is returned as a string."
144   (mm-with-unibyte-buffer
145     (insert face)
146     (ignore-errors
147       (base64-decode-region (point-min) (point-max)))
148     (buffer-string)))
149
150 ;;;#autoload
151 (defun gnus-convert-png-to-face (file)
152   "Convert FILE to a Face.
153 FILE should be a PNG file that's 48x48 and smaller than or equal to
154 740 bytes."
155   (mm-with-unibyte-buffer
156     (insert-file-contents file)
157     (when (> (buffer-size) 740)
158       (error "The file is %d bytes long, which is too long"
159              (buffer-size)))
160     (gnus-face-encode)))
161
162 (defface gnus-x-face '((t (:foreground "black" :background "white")))
163   "Face to show X-Face.
164 The colors from this face are used as the foreground and background
165 colors of the displayed X-Faces."
166   :group 'gnus-article-headers)
167
168 (defun gnus-display-x-face-in-from (data)
169   "Display the X-Face DATA in the From header."
170   (let ((default-enable-multibyte-characters nil)
171         pbm)
172     (when (or (gnus-image-type-available-p 'xface)
173               (and (gnus-image-type-available-p 'pbm)
174                    (setq pbm (uncompface data))))
175       (save-excursion
176         (save-restriction
177           (article-narrow-to-head)
178           (gnus-article-goto-header "from")
179           (when (bobp)
180             (insert "From: [no `from' set]\n")
181             (forward-char -17))
182           (gnus-add-image
183            'xface
184            (gnus-put-image
185             (if (gnus-image-type-available-p 'xface)
186                 (gnus-create-image
187                  (concat "X-Face: " data)
188                  'xface t :ascent 'center :face 'gnus-x-face)
189               (gnus-create-image
190                pbm 'pbm t :ascent 'center :face 'gnus-x-face))))
191           (gnus-add-wash-type 'xface))))))
192
193 (defun gnus-grab-cam-x-face ()
194   "Grab a picture off the camera and make it into an X-Face."
195   (interactive)
196   (shell-command "xawtv-remote snap ppm")
197   (let ((file nil))
198     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
199                                              t "snap.*ppm")))
200       (sleep-for 1))
201     (setq file (car file))
202     (with-temp-buffer
203       (shell-command
204        (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"
205                file)
206        (current-buffer))
207       ;;(sleep-for 3)
208       (delete-file file)
209       (buffer-string))))
210
211 (defun gnus-grab-cam-face ()
212   "Grab a picture off the camera and make it into an X-Face."
213   (interactive)
214   (shell-command "xawtv-remote snap ppm")
215   (let ((file nil)
216         result)
217     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
218                                              t "snap.*ppm")))
219       (sleep-for 1))
220     (setq file (car file))
221     (shell-command
222      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmnorm 2>/dev/null | ppmnorm | ppmtopgm | pnmscale -width 48 -height 48 > /tmp/gnus.face.ppm"
223              file))
224     (let ((gnus-convert-image-to-face-command
225            (format "cat '%%s' | ppmchange %s | ppmquant %%d | pnmtopng"
226                    (gnus-fun-ppm-change-string))))
227       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
228     (delete-file file)
229     ;;(delete-file "/tmp/gnus.face.ppm")
230     result))
231
232 (defun gnus-fun-ppm-change-string ()
233   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
234                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
235          (format (concat "'#%02x%02x%02x' '#"
236                          (nth (random 6) possibilites)
237                          "'"))
238          (values nil))
239   (dotimes (i 255)
240     (push (format format i i i i i i)
241           values))
242   (mapconcat 'identity values " ")))
243
244 (provide 'gnus-fun)
245
246 ;;; gnus-fun.el ends here