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