a8ca9abed536d2830657474b2bd619b25194a139
[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 file))))))
53
54 ;;;###autoload
55 (defun gnus-x-face-from-file (file)
56   "Insert an X-Face header based on an image file."
57   (interactive "fImage file name:" )
58   (when (file-exists-p file)
59     (shell-command-to-string
60      (format gnus-convert-image-to-x-face-command file))))
61
62 (defun gnus-convert-image-to-gray-x-face (file depth)
63   (let* ((mapfile (make-temp-name (expand-file-name "gnus." mm-tmp-directory)))
64          (levels (expt 2 depth))
65          (step (/ 255 (1- levels)))
66          color-alist bits bits-list mask pixel x-faces)
67     (with-temp-file mapfile
68       (insert "P3\n")
69       (insert (format "%d 1\n" levels))
70       (insert "255\n")
71       (dotimes (i levels)
72         (insert (format "%d %d %d\n"
73                         (* step i) (* step i) (* step i)))
74         (push (cons (* step i) i) color-alist)))
75     (when (file-exists-p file)
76       (with-temp-buffer
77         (insert (shell-command-to-string (format "giftopnm '%s' | ppmnorm 2>/dev/null | pnmscale -width 48 -height 48 | ppmquant -map %s 2>/dev/null | ppmtopgm | pnmtoplainpnm"
78                                file mapfile)))
79         (goto-char (point-min))
80         (forward-line 3)
81         (while (setq pixel (ignore-errors (read (current-buffer))))
82           (push (cdr (assq pixel color-alist)) bits-list))
83         (setq bits-list (nreverse bits-list))
84         (dotimes (bit-number depth)
85           (setq mask (expt 2 bit-number))
86           (with-temp-buffer
87             (insert "P1\n48 48\n")
88             (dolist (bits bits-list)
89               (insert (if (zerop (logand bits mask)) "0 " "1 ")))
90             (shell-command-on-region
91              (point-min) (point-max)
92              "pbmtoxbm | compface"
93              (current-buffer) t)
94             (push (buffer-string) x-faces))))
95       (dotimes (i (length x-faces))
96         (insert (if (zerop i) "X-Face:" (format "X-Face-%s:" i))
97                 (nth i x-faces))))
98     (delete-file mapfile)))
99
100 ;;;###autoload
101 (defun gnus-convert-gray-x-face-to-xpm (faces)
102   (let* ((depth (length faces))
103          (scale (/ 255 (1- (expt 2 depth))))
104          bit-list bit-lists pixels pixel)
105     (dolist (face faces)
106       (with-temp-buffer
107         (insert face)
108         (shell-command-on-region
109          (point-min) (point-max)
110          "uncompface -X | xbmtopbm | pnmtoplainpnm"
111          (current-buffer) t)
112         (goto-char (point-min))
113         (forward-line 2)
114         (while (not (eobp))
115           (cond
116            ((eq (following-char) ?0)
117             (push 0 bit-list))
118            ((eq (following-char) ?1)
119             (push 1 bit-list)))
120           (forward-char 1)))
121       (push bit-list bit-lists))
122     (dotimes (i (* 48 48))
123       (setq pixel 0)
124       (dotimes (plane depth)
125         (setq pixel (+ (* pixel 2) (nth i (nth plane bit-lists)))))
126       (push pixel pixels))
127     (with-temp-buffer
128       (insert "P2\n48 48\n255\n")
129       (dolist (pixel pixels)
130         (insert (number-to-string (* scale pixel)) " "))
131       (shell-command-on-region
132        (point-min) (point-max)
133        "ppmtoxpm"
134        (current-buffer) t (get-buffer-create " *junk"))
135       (buffer-string))))
136
137 ;;;###autoload
138 (defun gnus-convert-gray-x-face-region (beg end)
139   "Convert the X-Faces in region to a PPM file."
140   (interactive "r")
141   (let ((input (buffer-substring beg end))
142         faces)
143     (with-temp-buffer
144       (insert input)
145       (goto-char (point-min))
146       (while (not (eobp))
147         (save-restriction
148           (mail-header-narrow-to-field)
149           (push (mail-header-field-value) faces)
150           (goto-char (point-max)))))
151     (gnus-convert-gray-x-face-to-xpm faces)))
152
153 (provide 'gnus-fun)
154
155 ;;; gnus-fun.el ends here