(gnus-face-from-file): Decrease quant in smaller
[gnus] / lisp / gnus-fun.el
1 ;;; gnus-fun.el --- various frivolous extension functions to Gnus
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl))
31
32 (require 'mm-util)
33 (require 'gnus-ems)
34 (require 'gnus-util)
35
36 (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory)
37   "*Directory where X-Face PBM files are stored."
38   :version "22.1"
39   :group 'gnus-fun
40   :type 'directory)
41
42 (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface"
43   "Command for converting a PBM to an X-Face."
44   :version "22.1"
45   :group 'gnus-fun
46   :type 'string)
47
48 (defcustom gnus-convert-image-to-x-face-command
49   "convert -scale 48x48! %s xbm:- | xbm2xface.pl"
50   "Command for converting an image to an X-Face.
51 The command must take a image filename (use \"%s\") as input.
52 The output must be the Face header data on stdout in PNG format.
53
54 By default it takes a GIF filename and output the X-Face header data
55 on stdout."
56   :version "22.1"
57   :group 'gnus-fun
58   :type '(choice (const :tag "giftopnm, netpbm (GIF input only)"
59                         "giftopnm %s | ppmnorm | pnmscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface")
60                  (const :tag "convert"
61                         "convert -scale 48x48! %s xbm:- | xbm2xface.pl")
62                  (string)))
63
64 (defcustom gnus-convert-image-to-face-command
65   "convert -scale 48x48! %s -colors %d png:-"
66   "Command for converting an image to a Face.
67
68 The command must take an image filename (first format
69 argument\"%s\") and the number of colors (second format argument:
70 \"%d\") as input.  The output must be the Face header data on
71 stdout in PNG format."
72   :version "22.1"
73   :group 'gnus-fun
74   :type '(choice (const :tag "djpeg, netpbm (JPG input only)"
75                         "djpeg %s | ppmnorm | pnmscale -width 48 -height 48 | ppmquant %d | pnmtopng")
76                  (const :tag "convert"
77                         "convert -scale 48x48! %s -colors %d png:-")
78                  (string)))
79
80 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
81                                           '((xface . (:face gnus-x-face)))
82                                         '((pbm . (:face gnus-x-face))
83                                           (png . nil)))
84   "Alist of image types and properties applied to Face and X-Face images.
85 Here are examples:
86
87 ;; Specify the altitude of Face images in the From header.
88 \(setq gnus-face-properties-alist
89       '((pbm . (:face gnus-x-face :ascent 80))
90         (png . (:ascent 80))))
91
92 ;; Show Face images as pressed buttons.
93 \(setq gnus-face-properties-alist
94       '((pbm . (:face gnus-x-face :relief -2))
95         (png . (:relief -2))))
96
97 See the manual for the valid properties for various image types.
98 Currently, `pbm' is used for X-Face images and `png' is used for Face
99 images in Emacs.  Only the `:face' property is effective on the `xface'
100 image type in XEmacs if it is built with the libcompface library."
101   :group 'gnus-fun
102   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
103
104 (defun gnus-shell-command-to-string (command)
105   "Like `shell-command-to-string' except not mingling ERROR."
106   (with-output-to-string
107     (call-process shell-file-name nil (list standard-output nil)
108                   nil shell-command-switch command)))
109
110 (defun gnus-shell-command-on-region (start end command)
111   "A simplified `shell-command-on-region'.
112 Output to the current buffer, replace text, and don't mingle error."
113   (call-process-region start end shell-file-name t
114                        (list (current-buffer) nil)
115                        nil shell-command-switch command))
116
117 ;;;###autoload
118 (defun gnus-random-x-face ()
119   "Return X-Face header data chosen randomly from `gnus-x-face-directory'."
120   (interactive)
121   (when (file-exists-p gnus-x-face-directory)
122     (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$"))
123            (file (nth (random (length files)) files)))
124       (when file
125         (gnus-shell-command-to-string
126          (format gnus-convert-pbm-to-x-face-command
127                  (shell-quote-argument file)))))))
128
129 ;;;###autoload
130 (defun gnus-insert-random-x-face-header ()
131   "Insert a random X-Face header from `gnus-x-face-directory'."
132   (interactive)
133   (let ((data (gnus-random-x-face)))
134     (save-excursion
135       (message-goto-eoh)
136       (if data
137           (insert "X-Face: " data)
138         (message
139          "No face returned by `gnus-random-x-face'.  Does %s/*.pbm exist?"
140          gnus-x-face-directory)))))
141
142 ;;;###autoload
143 (defun gnus-x-face-from-file (file)
144   "Insert an X-Face header based on an image file.
145
146 Depending on `gnus-convert-image-to-x-face-command' it may accept
147 different input formats."
148   (interactive "fImage file name: ")
149   (when (file-exists-p file)
150     (gnus-shell-command-to-string
151      (format gnus-convert-image-to-x-face-command
152              (shell-quote-argument (expand-file-name file))))))
153
154 ;;;###autoload
155 (defun gnus-face-from-file (file)
156   "Return a Face header based on an image file.
157
158 Depending on `gnus-convert-image-to-face-command' it may accept
159 different input formats."
160   (interactive "fImage file name: ")
161   (when (file-exists-p file)
162     (let ((done nil)
163           (attempt "")
164           (quant 16))
165       (while (and (not done)
166                   (> quant 1))
167         (setq attempt
168               (let ((coding-system-for-read 'binary))
169                 (gnus-shell-command-to-string
170                  (format gnus-convert-image-to-face-command
171                          (shell-quote-argument (expand-file-name file))
172                          quant))))
173         (if (> (length attempt) 726)
174             (progn
175               (setq quant (- quant (if (< quant 10) 1 2)))
176               (gnus-message 9 "Length %d; trying quant %d"
177                             (length attempt) quant))
178           (setq done t)))
179       (if done
180           (mm-with-unibyte-buffer
181             (insert attempt)
182             (gnus-face-encode))
183         nil))))
184
185 (defun gnus-face-encode ()
186   (let ((step 72))
187     (base64-encode-region (point-min) (point-max))
188     (goto-char (point-min))
189     (while (search-forward "\n" nil t)
190       (replace-match ""))
191     (goto-char (point-min))
192     (while (> (- (point-max) (point))
193               step)
194       (forward-char step)
195       (insert "\n ")
196       (setq step 76))
197     (buffer-string)))
198
199 ;;;###autoload
200 (defun gnus-convert-face-to-png (face)
201   "Convert FACE (which is base64-encoded) to a PNG.
202 The PNG is returned as a string."
203   (mm-with-unibyte-buffer
204     (insert face)
205     (ignore-errors
206       (base64-decode-region (point-min) (point-max)))
207     (buffer-string)))
208
209 ;;;###autoload
210 (defun gnus-convert-png-to-face (file)
211   "Convert FILE to a Face.
212 FILE should be a PNG file that's 48x48 and smaller than or equal to
213 726 bytes."
214   (mm-with-unibyte-buffer
215     (insert-file-contents file)
216     (when (> (buffer-size) 726)
217       (error "The file is %d bytes long, which is too long"
218              (buffer-size)))
219     (gnus-face-encode)))
220
221 (defface gnus-x-face '((t (:foreground "black" :background "white")))
222   "Face to show X-Face.
223 The colors from this face are used as the foreground and background
224 colors of the displayed X-Faces."
225   :group 'gnus-article-headers)
226
227 (defun gnus-display-x-face-in-from (data)
228   "Display the X-Face DATA in the From header."
229   (let ((default-enable-multibyte-characters nil)
230         pbm)
231     (when (or (gnus-image-type-available-p 'xface)
232               (and (gnus-image-type-available-p 'pbm)
233                    (setq pbm (uncompface data))))
234       (save-excursion
235         (save-restriction
236           (article-narrow-to-head)
237           (gnus-article-goto-header "from")
238           (when (bobp)
239             (insert "From: [no `from' set]\n")
240             (forward-char -17))
241           (gnus-add-image
242            'xface
243            (gnus-put-image
244             (if (gnus-image-type-available-p 'xface)
245                 (apply 'gnus-create-image (concat "X-Face: " data) 'xface t
246                        (cdr (assq 'xface gnus-face-properties-alist)))
247               (apply 'gnus-create-image pbm 'pbm t
248                      (cdr (assq 'pbm gnus-face-properties-alist))))
249             nil 'xface))
250           (gnus-add-wash-type 'xface))))))
251
252 (defun gnus-grab-cam-x-face ()
253   "Grab a picture off the camera and make it into an X-Face."
254   (interactive)
255   (shell-command "xawtv-remote snap ppm")
256   (let ((file nil))
257     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
258                                              t "snap.*ppm")))
259       (sleep-for 1))
260     (setq file (car file))
261     (with-temp-buffer
262       (shell-command
263        (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"
264                file)
265        (current-buffer))
266       ;;(sleep-for 3)
267       (delete-file file)
268       (buffer-string))))
269
270 (defun gnus-grab-cam-face ()
271   "Grab a picture off the camera and make it into an X-Face."
272   (interactive)
273   (shell-command "xawtv-remote snap ppm")
274   (let ((file nil)
275         result)
276     (while (null (setq file (directory-files "/tftpboot/sparky/tmp"
277                                              t "snap.*ppm")))
278       (sleep-for 1))
279     (setq file (car file))
280     (shell-command
281      (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmscale -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm"
282              file))
283     (let ((gnus-convert-image-to-face-command
284            (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng"
285                    (gnus-fun-ppm-change-string))))
286       (setq result (gnus-face-from-file "/tmp/gnus.face.ppm")))
287     (delete-file file)
288     ;;(delete-file "/tmp/gnus.face.ppm")
289     result))
290
291 (defun gnus-fun-ppm-change-string ()
292   (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x"
293                         "%02x%02x00" "00%02x%02x" "%02x00%02x"))
294          (format (concat "'#%02x%02x%02x' '#"
295                          (nth (random 6) possibilites)
296                          "'"))
297          (values nil))
298   (dotimes (i 255)
299     (push (format format i i i i i i)
300           values))
301   (mapconcat 'identity values " ")))
302
303 (provide 'gnus-fun)
304
305 ;;; arch-tag: 9d000a69-15cc-4491-9dc0-4627484f50c1
306 ;;; gnus-fun.el ends here