1 ;;; compface.el --- functions for converting X-Face headers
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; TAKAI Kousuke <tak@kmc.gr.jp>
8 ;; This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (defgroup compface nil
30 "X-Face image conversion."
33 (defcustom uncompface-use-external (and (not noninteractive)
34 (executable-find "uncompface")
35 (executable-find "icontopbm")
37 "*Specify which of the internal or the external decoder should be used.
38 nil means to use the internal ELisp-based uncompface program. t means
39 to use the external decoder. In the later case, you need to have the
40 external `uncompface' and `icontopbm' programs installed. The default
41 value is nil if those external programs aren't available, otherwise
42 `undecided' which means to determine it by checking whether the host
43 machine is slow. See also `uncompface-use-external-threshold'. You
44 can skip that check by setting this value as nil or t explicitly."
45 :type '(choice (const :tag "Use the internal decoder" nil)
46 (const :tag "Use the external decoder" t)
47 (const :tag "Autodetection" undecided))
50 (defcustom uncompface-use-external-threshold 0.1
51 "*Number of seconds to check whether the host machine is slow.
52 If the host takes time larger than this value for decoding an X-Face
53 using the internal ELisp-based uncompface program, it will be changed
54 to using the external `uncompface' and `icontopbm' programs if they
55 are available. Note that the measurement may never be exact."
60 (defmacro uncompface-float-time (&optional specified-time)
61 (if (fboundp 'float-time)
62 `(float-time ,specified-time)
63 `(let ((time (or ,specified-time (current-time))))
64 (+ (* (car time) 65536.0)
66 (cond ((consp (setq time (cddr time)))
67 (/ (car time) 1000000.0))
73 (defun uncompface (face)
75 If `uncompface-use-external' is t, it requires the external programs
76 `uncompface', and `icontopbm'. On a GNU/Linux system these might be
77 in packages with names like `compface' or `faces-xface' and `netpbm'
78 or `libgr-progs', for instance."
79 (cond ((eq uncompface-use-external nil)
80 (uncompface-internal face))
81 ((eq uncompface-use-external t)
84 (and (eq 0 (apply 'call-process-region (point-min) (point-max)
86 'delete '(t nil) nil))
88 (goto-char (point-min))
89 (insert "/* Width=48, Height=48 */\n")
90 ;; I just can't get "icontopbm" to work correctly on its
91 ;; own in XEmacs. And Emacs doesn't understand un-raw pbm
93 (if (not (featurep 'xemacs))
94 (eq 0 (call-process-region (point-min) (point-max)
97 (shell-command-on-region (point-min) (point-max)
98 "icontopbm | pnmnoraw"
103 (let* ((gc-cons-threshold (eval '(lsh -1 -1)))
104 (start (current-time)))
106 (uncompface-internal face)
107 (setq uncompface-use-external
108 (and (> (- (uncompface-float-time (current-time))
109 (uncompface-float-time start))
110 uncompface-use-external-threshold)
111 (executable-find "uncompface")
112 (executable-find "icontopbm")
114 (message "Setting `uncompface-use-external' to `%s'"
115 uncompface-use-external))))))
117 ;; The following section is a bug-for-bug compatible version of
118 ;; `uncompface' program entirely implemented in Emacs-Lisp.
121 ;; The size of 48x48 is actually hard-coded into the code itself,
122 ;; so you cannot simply change those values. So we hard-code
123 ;; them into the compiled code.
124 (defconst uncompface-width 48
125 "Width of X-Face bitmap image.")
126 (defconst uncompface-height 48
127 "Height of X-Face bitmap image.")
129 ;; Again, this is also hard-coded into the compiled code.
130 (defconst uncompface-guesses
133 (let ((vector (make-vector (length x) nil))
143 (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
144 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
145 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
146 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 1
147 0 0 0 0 0 1 0 1 0 0 0 1 0 1 1 1