From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.emacs.gnus.general/51463 Path: main.gmane.org!not-for-mail From: Julien Avarre Newsgroups: gmane.emacs.gnus.general Subject: gnus-face-from-file Date: Tue, 15 Apr 2003 13:23:15 +0200 Organization: Megarezo Sender: ding-owner@lists.math.uh.edu Message-ID: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-Trace: main.gmane.org 1050405853 14471 80.91.224.249 (15 Apr 2003 11:24:13 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Tue, 15 Apr 2003 11:24:13 +0000 (UTC) Original-X-From: ding-owner+M7@lists.math.uh.edu Tue Apr 15 13:24:12 2003 Return-path: Original-Received: from malifon.math.uh.edu ([129.7.128.13]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 195OXf-0003kK-00 for ; Tue, 15 Apr 2003 13:24:00 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.math.uh.edu ident=lists) by malifon.math.uh.edu with smtp (Exim 3.20 #1) id 195OXB-0006SZ-00; Tue, 15 Apr 2003 06:23:29 -0500 Original-Received: from sclp3.sclp.com ([64.157.176.121]) by malifon.math.uh.edu with smtp (Exim 3.20 #1) id 195OX2-0006ST-00 for ding@lists.math.uh.edu; Tue, 15 Apr 2003 06:23:20 -0500 Original-Received: (qmail 123 invoked by alias); 15 Apr 2003 11:23:19 -0000 Original-Received: (qmail 118 invoked from network); 15 Apr 2003 11:23:19 -0000 Original-Received: from relay-4v.club-internet.fr (194.158.96.115) by sclp3.sclp.com with SMTP; 15 Apr 2003 11:23:19 -0000 Original-Received: from localhost.localdomain (f11v-9-122.d1.club-internet.fr [213.44.168.122]) by relay-4v.club-internet.fr (Postfix) with ESMTP id 8637817F9 for ; Tue, 15 Apr 2003 13:23:16 +0200 (CEST) Original-To: ding@gnus.org X-URL: http://www.epita.fr:8000/~eole X-Important: Using PINE is bad ! X-Face: )u,BsxMlz?34"0)]\kt}RN2A0"9/hyjY>AS^WDLal ;; Keywords: news ;; This file is part of GNU Emacs. ;; GNU Emacs is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, ;; Boston, MA 02111-1307, USA. ;;; Commentary: ;;; Code: (defcustom gnus-x-face-directory (expand-file-name "x-faces" gnus-directory) "*Directory where X-Face PBM files are stored." :group 'gnus-fun :type 'directory) (defcustom gnus-convert-pbm-to-x-face-command "pbmtoxbm %s | compface" "Command for converting a PBM to an X-Face." :group 'gnus-fun :type 'string) (defcustom gnus-convert-image-to-x-face-command "giftopnm %s | ppmnorm | pn= mscale -width 48 -height 48 | ppmtopgm | pgmtopbm | pbmtoxbm | compface" "Command for converting an image to an X-Face. By default it takes a GIF filename and output the X-Face header data on stdout." :group 'gnus-fun :type 'string) (defcustom gnus-convert-image-to-face-command "djpeg %s | ppmnorm | pnmscal= e -width 48 -height 48 | ppmquant %d | pnmtopng" "Command for converting an image to an Face. By default it takes a JPEG filename and output the Face header data on stdout." :group 'gnus-fun :type 'string) (defun gnus-shell-command-to-string (command) "Like `shell-command-to-string' except not mingling ERROR." (with-output-to-string (call-process shell-file-name nil (list standard-output nil) nil shell-command-switch command))) (defun gnus-shell-command-on-region (start end command) "A simplified `shell-command-on-region'. Output to the current buffer, replace text, and don't mingle error." (call-process-region start end shell-file-name t (list (current-buffer) nil) nil shell-command-switch command)) ;;;###autoload (defun gnus-random-x-face () "Return X-Face header data chosen randomly from `gnus-x-face-directory'." (interactive) (when (file-exists-p gnus-x-face-directory) (let* ((files (directory-files gnus-x-face-directory t "\\.pbm$")) (file (nth (random (length files)) files))) (when file (gnus-shell-command-to-string (format gnus-convert-pbm-to-x-face-command (shell-quote-argument file))))))) ;;;###autoload (defun gnus-insert-random-x-face-header () "Insert a random X-Face header from `gnus-x-face-directory'." (interactive) (let ((data (gnus-random-x-face))) (save-excursion (message-goto-eoh) (if data (insert "X-Face: " data) (message "No face returned by `gnus-random-x-face'. Does %s/*.pbm exist?" gnus-x-face-directory))))) ;;;###autoload (defun gnus-x-face-from-file (file) "Insert an X-Face header based on an image file." (interactive "fImage file name (by default GIF): ") (when (file-exists-p file) (gnus-shell-command-to-string (format gnus-convert-image-to-x-face-command (shell-quote-argument (expand-file-name file)))))) ;;;###autoload (defun gnus-face-from-file (file) "Return an Face header based on an image file." (interactive "fImage file name (by default JPEG): ") (when (file-exists-p file) (let ((done nil) (attempt "") (quant 16)) (while (and (not done) (> quant 1)) (setq attempt (gnus-shell-command-to-string (format gnus-convert-image-to-face-command (shell-quote-argument (expand-file-name file)) quant))) (if (> (length attempt) 740) (progn (setq quant (- quant 2)) (message "Length %d; trying quant %d" (length attempt) quant)) (setq done t))) (if done (mm-with-unibyte-buffer (insert attempt) (gnus-face-encode)) nil)))) (defun gnus-face-encode () (let ((step 72)) (base64-encode-region (point-min) (point-max)) (goto-char (point-min)) (while (search-forward "\n" nil t) (replace-match "")) (goto-char (point-min)) (while (> (- (point-max) (point)) step) (forward-char step) (insert "\n ") (setq step 76)) (buffer-string))) ;;;###autoload (defun gnus-convert-face-to-png (face) "Convert FACE (which is base64-encoded) to a PNG. The PNG is returned as a string." (mm-with-unibyte-buffer (insert face) (ignore-errors (base64-decode-region (point-min) (point-max))) (buffer-string))) ;;;###autoload (defun gnus-convert-png-to-face (file) "Convert FILE to a Face. FILE should be a PNG file that's 48x48 and smaller than or equal to 740 bytes." (mm-with-unibyte-buffer (insert-file-contents file) (when (> (buffer-size) 740) (error "The file is %d bytes long, which is too long" (buffer-size))) (gnus-face-encode))) (defface gnus-x-face '((t (:foreground "black" :background "white"))) "Face to show X-Face. The colors from this face are used as the foreground and background colors of the displayed X-Faces." :group 'gnus-article-headers) (defun gnus-display-x-face-in-from (data) "Display the X-Face DATA in the From header." (let ((default-enable-multibyte-characters nil) pbm) (when (or (gnus-image-type-available-p 'xface) (and (gnus-image-type-available-p 'pbm) (setq pbm (uncompface data)))) (save-excursion (save-restriction (article-narrow-to-head) (gnus-article-goto-header "from") (when (bobp) (insert "From: [no `from' set]\n") (forward-char -17)) (gnus-add-image 'xface (gnus-put-image (if (gnus-image-type-available-p 'xface) (gnus-create-image (concat "X-Face: " data) 'xface t :ascent 'center :face 'gnus-x-face) (gnus-create-image pbm 'pbm t :ascent 'center :face 'gnus-x-face)))) (gnus-add-wash-type 'xface)))))) (defun gnus-grab-cam-x-face () "Grab a picture off the camera and make it into an X-Face." (interactive) (shell-command "xawtv-remote snap ppm") (let ((file nil)) (while (null (setq file (directory-files "/tftpboot/sparky/tmp" t "snap.*ppm"))) (sleep-for 1)) (setq file (car file)) (with-temp-buffer (shell-command (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | ppmn= orm 2>/dev/null | pnmscale -width 48 | ppmtopgm | pgmtopbm -threshold -valu= e 0.92 | pbmtoxbm | compface" file) (current-buffer)) ;;(sleep-for 3) (delete-file file) (buffer-string)))) (defun gnus-grab-cam-face () "Grab a picture off the camera and make it into an X-Face." (interactive) (shell-command "xawtv-remote snap ppm") (let ((file nil) result) (while (null (setq file (directory-files "/tftpboot/sparky/tmp" t "snap.*ppm"))) (sleep-for 1)) (setq file (car file)) (shell-command (format "pnmcut -left 110 -top 30 -width 144 -height 144 '%s' | pnmsca= le -width 48 -height 48 | ppmtopgm > /tmp/gnus.face.ppm" file)) (let ((gnus-convert-image-to-face-command (format "cat '%%s' | ppmquant %%d | ppmchange %s | pnmtopng" (gnus-fun-ppm-change-string)))) (setq result (gnus-face-from-file "/tmp/gnus.face.ppm"))) (delete-file file) ;;(delete-file "/tmp/gnus.face.ppm") result)) (defun gnus-fun-ppm-change-string () (let* ((possibilites '("%02x0000" "00%02x00" "0000%02x" "%02x%02x00" "00%02x%02x" "%02x00%02x")) (format (concat "'#%02x%02x%02x' '#" (nth (random 6) possibilites) "'")) (values nil)) (dotimes (i 255) (push (format format i i i i i i) values)) (mapconcat 'identity values " "))) (provide 'gnus-fun) ;;; gnus-fun.el ends here --=-=-= Content-Transfer-Encoding: quoted-printable -- Julien ``Eole'' Avarre julien at avarre dot com --=-=-=-- --==-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE+m+ujaRoHvGT4vVkRAlKDAJ9rXK3u0orSy0Ofl5VzXG1NtLb7RACfbTdr hGA5TWrhN/PPanG5PyeiZr8= =rtYh -----END PGP SIGNATURE----- --==-=-=--