Import No Gnus v0.2.
[elisp/gnus.git-] / lisp / compface.el
1 ;;; compface.el --- functions for converting X-Face headers
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      TAKAI Kousuke <tak@kmc.gr.jp>
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (defgroup compface nil
30   "X-Face image conversion."
31   :group 'extensions)
32
33 (defcustom uncompface-use-external (and (not noninteractive)
34                                         (executable-find "uncompface")
35                                         (executable-find "icontopbm")
36                                         'undecided)
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))
48   :group 'compface)
49
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."
56   :type 'number
57   :group 'compface)
58
59 (eval-when-compile
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)
65             (cadr time)
66             (cond ((consp (setq time (cddr time)))
67                    (/ (car time) 1000000.0))
68                   (time
69                    (/ time 1000000.0))
70                   (t
71                    0)))))))
72
73 (defun uncompface (face)
74   "Convert FACE to pbm.
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)
82          (with-temp-buffer
83            (insert face)
84            (and (eq 0 (apply 'call-process-region (point-min) (point-max)
85                              "uncompface"
86                              'delete '(t nil) nil))
87                 (progn
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
92                   ;; files.
93                   (if (not (featurep 'xemacs))
94                       (eq 0 (call-process-region (point-min) (point-max)
95                                                  "icontopbm"
96                                                  'delete '(t nil)))
97                     (shell-command-on-region (point-min) (point-max)
98                                              "icontopbm | pnmnoraw"
99                                              (current-buffer) t)
100                     t))
101                 (buffer-string))))
102         (t
103          (let* ((gc-cons-threshold (eval '(lsh -1 -1)))
104                 (start (current-time)))
105            (prog1
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")
113                         t)))))))
114
115 ;; The following section is a bug-for-bug compatible version of
116 ;; `uncompface' program entirely implemented in Emacs-Lisp.
117
118 (eval-when-compile
119   ;; The size of 48x48 is actually hard-coded into the code itself,
120   ;; so you cannot simply change those values.  So we hard-code
121   ;; them into the compiled code.
122   (defconst uncompface-width 48
123     "Width of X-Face bitmap image.")
124   (defconst uncompface-height 48
125     "Height of X-Face bitmap image.")
126
127   ;; Again, this is also hard-coded into the compiled code.
128   (defconst uncompface-guesses
129     (mapcar (lambda (x)
130               (mapcar (lambda (x)
131                         (let ((vector (make-vector (length x) nil))
132                               (i 0))
133                           (while x
134                             (or (zerop (car x))
135                                 (aset vector i t))
136                             (setq x (cdr x)
137                                   i (1+ i)))
138                           vector))
139                       x))
140             '((;; g_00
141                (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
142                 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
143                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
144                 1 1 1 0 0 0 1 1 1 1 0 1 1 1 1 1
145                 0 0 0 0 0 1 0 1 0 0 0 1 0 1 1 1
146                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
147                 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1
148                 0 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1
149                 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
150                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
151                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
152                 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 1
153                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
154                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
155                 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1
156                 0 1 0 0 0 1 0 1 0 0 1 0 1 1 1 1
157                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
158                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
159                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
160                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
161                 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
162                 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
163                 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1
164                 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
165                 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
166                 1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 1
167                 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
168                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
169                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
170                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
171                 0 0 0 1 1 0 1 1 0 0 0 1 1 1 1 1
172                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
173                 0 1 0 0 1 1 1 1 0 1 0 1 0 1 0 0
174                 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1
175                 0 1 0 1 0 1 1 1 0 1 0 0 0 1 1 1
176                 1 1 0 1 0 1 1 1 0 0 1 1 1 1 0 1
177                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
178                 0 1 0 1 1 1 1 1 0 0 0 1 1 1 1 1
179                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
180                 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
181                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
182                 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1
183                 0 0 0 0 1 1 1 1 0 1 0 1 1 1 1 1
184                 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1
185                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
186                 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1
187                 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
188                 0 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1
189                 0 0 0 0 1 1 1 1 0 1 0 1 1 1 1 1
190                 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
191                 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1
192                 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1
193                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
194                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
195                 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
196                 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1
197                 0 0 0 0 1 1 1 1 0 1 0 0 1 1 1 1
198                 1 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1
199                 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1
200                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
201                 0 1 1 0 0 1 1 1 1 0 1 1 1 1 1 1
202                 0 1 0 1 0 1 1 0 0 0 1 0 0 1 0 1
203                 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1
204                 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
205                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
206                 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
207                 0 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
208                 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1
209                 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0
210                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
211                 0 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0
212                 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 1
213                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
214                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
215                 0 0 0 0 1 1 1 1 0 1 0 1 1 1 1 1
216                 0 0 0 1 1 0 0 0 1 1 0 1 0 1 1 1
217                 1 0 0 1 0 1 0 0 0 1 1 1 0 0 0 1
218                 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
219                 0 0 0 1 1 1 1 1 1 0 1 1 0 1 1 1
220                 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1
221                 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
222                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
223                 0 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1
224                 1 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1
225                 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 1
226                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
227                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
228                 1 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1
229                 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1
230                 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
231                 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
232                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
233                 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0
234                 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
235                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
236                 1 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1
237                 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0
238                 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1
239                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
240                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
241                 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
242                 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1
243                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
244                 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
245                 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1
246                 1 1 0 1 0 1 1 1 0 1 0 1 1 1 1 1
247                 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1
248                 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
249                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
250                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
251                 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1
252                 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
253                 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1
254                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
255                 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
256                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
257                 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
258                 0 0 0 0 1 1 1 1 0 1 0 0 1 1 1 1
259                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
260                 1 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1
261                 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1
262                 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
263                 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
264                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
265                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
266                 1 1 0 0 1 1 0 1 0 0 0 0 1 1 1 1
267                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
268                 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
269                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
270                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
271                 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0
272                 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1
273                 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
274                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
275                 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0
276                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
277                 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1
278                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
279                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
280                 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0
281                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
282                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
283                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
284                 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1
285                 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
286                 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
287                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
288                 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
289                 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0
290                 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1
291                 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 1
292                 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1
293                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
294                 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
295                 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
296                 0 1 0 0 0 1 1 0 0 0 0 0 1 1 0 0
297                 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
298                 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
299                 0 0 0 0 1 1 1 1 0 0 0 1 0 1 0 1
300                 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
301                 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
302                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
303                 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1
304                 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1
305                 0 1 0 0 1 1 1 1 0 0 1 1 1 1 1 0
306                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
307                 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
308                 1 0 0 1 0 1 0 1 0 1 0 0 1 1 1 1
309                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
310                 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1
311                 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1
312                 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1
313                 0 0 1 0 0 1 0 1 0 0 0 0 1 1 1 0
314                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1
315                 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1
316                 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
317                 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0
318                 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 1
319                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
320                 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1
321                 0 1 0 0 0 1 1 1 1 1 1 0 1 1 0 1
322                 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 1
323                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
324                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
325                 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 1
326                 1 1 0 1 1 0 0 0 0 1 0 1 1 1 1 1
327                 0 0 0 0 1 1 1 1 0 1 1 1 1 1 1 1
328                 1 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1
329                 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 1
330                 1 0 0 1 0 1 0 0 0 0 0 0 1 1 0 1
331                 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
332                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
333                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
334                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
335                 0 1 0 0 0 1 1 0 0 1 0 1 0 1 1 1
336                 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 1
337                 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0
338                 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1
339                 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 0
340                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
341                 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
342                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
343                 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 1
344                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
345                 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
346                 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1
347                 0 0 0 0 1 1 1 1 0 1 1 1 1 1 1 1
348                 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 1
349                 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 0
350                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
351                 0 0 0 0 1 1 1 1 0 1 0 0 0 0 0 1
352                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
353                 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0
354                 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
355                 0 0 0 0 1 1 1 1 0 1 1 1 1 1 1 1
356                 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1
357                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
358                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
359                 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
360                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
361                 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0
362                 1 1 0 0 1 1 1 1 0 0 0 0 0 1 0 1
363                 1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1
364                 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1
365                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
366                 0 0 0 1 0 1 1 1 0 1 0 1 1 1 1 1
367                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
368                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
369                 0 1 0 0 0 1 1 0 0 0 0 0 1 0 0 1
370                 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1
371                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
372                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
373                 0 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0
374                 1 0 1 0 0 1 1 1 0 1 1 1 1 1 1 1
375                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
376                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
377                 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0
378                 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1
379                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
380                 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
381                 0 0 0 0 1 1 1 0 1 1 1 0 0 1 1 0
382                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
383                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
384                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
385                 0 0 0 0 1 1 1 1 1 1 1 0 1 1 0 0
386                 1 0 0 0 1 1 1 1 0 1 0 0 1 1 1 1
387                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
388                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
389                 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1
390                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
391                 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1
392                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
393                 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0
394                 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 1
395                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
396                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
397                ;; g_10
398                (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
399                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
400                 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
401                 1 1 1 1 0 0 1 1 0 1 0 1 1 1 1 1
402                 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0
403                 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 1
404                 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1
405                 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1
406                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
407                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
408                 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1
409                 0 0 1 1 0 0 1 1 1 1 0 1 0 1 1 1
410                 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1
411                 0 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1
412                 0 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1
413                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
414                 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
415                 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
416                 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0
417                 0 0 0 1 0 0 0 1 0 1 0 1 0 1 1 1
418                 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1
419                 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1
420                 0 0 1 1 0 1 0 1 1 0 1 1 1 1 1 1
421                 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
422                 0 0 0 0 0 1 1 1 0 1 1 0 1 1 1 1
423                 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
424                 0 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0
425                 1 1 1 1 1 0 1 0 1 1 1 0 1 0 0 0
426                 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1
427                 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1
428                 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
429                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
430                ;; g_20
431                (0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
432                 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
433                 0 1 0 0 0 0 1 1 0 0 1 0 1 1 1 0
434                 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1)
435                ;; g_40
436                (0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
437                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
438                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
439                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
440                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
441                 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0
442                 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 1
443                 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1
444                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
445                 0 1 0 0 0 1 0 0 0 1 0 0 1 1 1 1
446                 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0
447                 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
448                 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 1
449                 0 1 0 0 0 1 0 1 0 1 1 1 1 1 1 1
450                 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
451                 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
452                 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
453                 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1
454                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
455                 0 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1
456                 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1
457                 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
458                 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
459                 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 1
460                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
461                 0 1 0 0 1 1 1 1 0 1 0 1 1 1 1 1
462                 0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1
463                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
464                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
465                 0 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1
466                 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
467                 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
468                 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
469                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
470                 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1
471                 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1
472                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
473                 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1
474                 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 1
475                 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1 1
476                 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
477                 0 1 0 0 0 0 0 1 0 1 0 1 0 1 1 1
478                 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
479                 0 0 0 0 0 1 0 1 0 1 1 1 0 1 1 1
480                 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1
481                 0 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1
482                 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
483                 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
484                 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
485                 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1
486                 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1
487                 0 0 0 0 0 1 0 1 0 1 1 1 1 1 0 1
488                 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1
489                 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1
490                 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0
491                 0 0 0 0 1 1 0 1 1 1 1 1 1 1 0 1
492                 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1
493                 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1
494                 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 1
495                 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1
496                 0 0 0 0 1 1 0 1 0 1 1 1 1 1 1 1
497                 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1
498                 0 1 0 0 1 1 0 1 0 1 1 1 1 1 0 1
499                 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1))
500               (;; g_01
501                (0 0 1 1 0 1 1 1 0 1 1 1 0 0 1 1
502                 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1
503                 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1
504                 1 1 1 1 0 1 0 1 1 1 1 1 1 0 1 1
505                 0 1 1 1 0 0 0 0 0 0 1 1 0 0 1 1
506                 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 1
507                 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
508                 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
509                ;; g_11
510                (0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1
511                 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 1)
512                ;; g_21
513                (0 0 0 1 0 1 1 1)
514                ;; g_41
515                (0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
516                 0 0 0 0 0 0 0 1 0 0 0 1 1 1 1 1
517                 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
518                 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1))
519               (;; g_02
520                (0 1 0 1)
521                ;; g_12
522                (0 1)
523                ;; g_22
524                (0)
525                ;; g_42
526                (0 0 0 1))))
527     "Static prediction table for X-Face image compression algorithm.")
528
529   ;; Macros for inlining critical values. 
530   (defmacro uncompface-width () (list 'quote uncompface-width))
531   (defmacro uncompface-height () (list 'quote uncompface-height))
532   (defmacro uncompface-guesses () (list 'quote uncompface-guesses))
533
534   (defmacro uncompface-loop (&rest body)
535     "Eval BODY and repeat if last expression of BODY yields non-nil."
536     (list 'while (cons 'progn body))))
537
538 ;; (defun uncompface-print-bignum (bignum &optional prefix)
539 ;;   (princ (format (concat prefix "<%s>\n")
540 ;;               (mapconcat (lambda (x) (format "%02x" x))
541 ;;                          (reverse bignum) " "))))
542
543 ;; Shut up the byte-compiler.
544 ;; These variables are once bound in `uncompface' and all subfunctions
545 ;; accesses them directly rather than creating their own bindings.
546 (eval-when-compile
547   (defvar bignum)
548   (defvar face))
549
550 ;; Big-number facilities.
551 ;; These functions were used to be implemented with `lsh' and `logand',
552 ;; but rewritten to use `/' and `%'.  The last two are mapped into
553 ;; byte-code directly, but the formers are normal functions even in
554 ;; compiled code which involve expensive `funcall' operations.
555 (eval-when-compile
556   (defsubst uncompface-big-mul-add (multiplier adder)
557     "Multiply BIGNUM by MULTIPLIER and add ADDER and put result in `bignum'."
558     (setq bignum (if (= multiplier 0)
559                      (cons 0 bignum)
560                    (prog1 bignum
561                      (while (progn
562                               (setcar bignum (% (setq adder (+ (* (car bignum)
563                                                                   multiplier)
564                                                                adder))
565                                                 256))
566                               (setq adder (/ adder 256))
567                               (cdr bignum))
568                        (setq bignum (cdr bignum)))
569                      (or (= adder 0)
570                          (setcdr bignum (list adder))))))))
571
572 ;; This trick is for XEmacs 21.4 which doesn't allow inlining a function
573 ;; using `defsubst' into another function also defined with `defsubst'.
574 (eval-when-compile
575   (when (featurep 'xemacs)
576     (defvar uncompface-big-mul-add (symbol-function 'uncompface-big-mul-add))
577     (defmacro uncompface-big-mul-add (multiplier adder)
578       `(,uncompface-big-mul-add ,multiplier ,adder))))
579
580 ;; Separate `eval-when-compile' for the byte compiler
581 ;; to properly define `uncompface-big-mul-add' before `uncompface-big-pop'.
582 (eval-when-compile
583   (defsubst uncompface-big-pop (prob)
584     (let ((n (car bignum)) (i 0))
585       (if (cdr bignum)
586           (setq bignum (cdr bignum))
587         (setcar bignum 0))
588       (while (or (< n (cdr (car prob)))
589                  (>= n (+ (cdr (car prob)) (car (car prob)))))
590         (setq prob (cdr prob)
591               i (1+ i)))
592       (uncompface-big-mul-add (car (car prob)) (- n (cdr (car prob))))
593       i)))
594
595 ;; This function cannot be inlined due to recursive calls.
596 (defun uncompface-pop-grays (offset size)
597   (if (<= size 3)
598       (let ((bits (uncompface-big-pop
599                    ;; This is freqs[16] in compface_private.h.
600                    '(( 0 .   0) (38 .   0) (38 .  38) (13 . 152)
601                      (38 .  76) (13 . 165) (13 . 178) ( 6 . 230)
602                      (38 . 114) (13 . 191) (13 . 204) ( 6 . 236)
603                      (13 . 217) ( 6 . 242) ( 5 . 248) ( 3 . 253)))))
604 ;;      (if (/= (logand bits 1) 0)
605 ;;          (aset face offset t))
606 ;;      (if (/= (logand bits 2) 0)
607 ;;          (aset face (1+ offset) t))
608 ;;      (if (/= (logand bits 4) 0)
609 ;;          (aset face (+ offset (uncompface-width)) t))
610 ;;      (if (/= (logand bits 8) 0)
611 ;;          (aset face (+ offset (uncompface-width) 1) t))
612         (when (>= bits 8)
613           (aset face (+ offset (uncompface-width) 1) t)
614           (setq bits (- bits 8)))
615         (when (>= bits 4)
616           (aset face (+ offset (uncompface-width)) t)
617           (setq bits (- bits 4)))
618         (or (eq (if (< bits 2)
619                     bits
620                   (aset face (1+ offset) t)
621                   (- bits 2))
622                 0)
623             (aset face offset t))
624         )
625     (setq size (/ size 2))
626     (uncompface-pop-grays offset size)
627     (uncompface-pop-grays (+ offset size) size)
628     (uncompface-pop-grays (+ offset (* (uncompface-width) size)) size)
629     (uncompface-pop-grays (+ offset (* (uncompface-width) size) size) size)))
630
631 ;; Again, this function call itself recursively.
632 (defun uncompface-uncompress (offset size level)
633   ;; This used to be (funcall (aref [(lambda ...) ...] (u-big-pop ...)))
634   ;; but this was slow due to function call.
635   (let ((i (uncompface-big-pop (car level))))
636     (cond ((eq i 0)                     ; black
637            (uncompface-pop-grays offset size))
638           ((eq i 1)                     ; gray
639            (setq size (/ size 2)
640                  level (cdr level))
641            (uncompface-uncompress offset size level)
642            (uncompface-uncompress (+ offset size) size level)
643            (uncompface-uncompress (+ offset (* size (uncompface-width)))
644                                   size level)
645            (uncompface-uncompress (+ offset (* size (uncompface-width)) size)
646                                   size level))
647           ;; ((eq i 2) nil)
648           ;; (t (error "Cannot happen"))
649           )))
650
651 (eval-when-compile
652   (defmacro uncompface-shift-in (k dy dx)
653     `(+ k k (if (aref face (+ i (* ,dy (uncompface-width)) ,dx)) 1 0))))
654
655 (defun uncompface-internal (string &optional raw)
656   "Decode X-Face data STRING and return an image in the pbm format.
657 If the optional RAW is non-nil, return a raw bitmap as a vector."
658   (let (;; `bignum' and `face' are semi-global variables.
659         ;; Do not use '(0) below, because BIGNUM is modified in-place.
660         (bignum (list 0))
661         (face (make-vector (* (uncompface-width) (uncompface-height)) nil))
662         ;;(uncompface-big-shift -16)
663         ;;(uncompface-big-mask 65535)
664         (y 0) x)
665     (mapc (lambda (c)
666             (and (>= c ?!) (<= c ?~)
667                  (uncompface-big-mul-add (1+ (- ?~ ?!)) (- c ?!))))
668           string)
669     ;;(uncompface-print-bignum bignum)
670     ;;(setq y 0)
671     (uncompface-loop
672       (setq x 0)
673       (uncompface-loop
674         (uncompface-uncompress (+ (* (uncompface-width) y) x) 16
675                                ;; This is levels[4][3] in compface_private.h.
676                                '(;; Top of tree almost always grey
677                                  ((  1 . 255) (251 .   0) (  4 . 251))
678                                  ((  1 . 255) (200 .   0) ( 55 . 200))
679                                  (( 33 . 223) (159 .   0) ( 64 . 159))
680                                  ;; Grey disallowed at bottom
681                                  ((131 .   0) (  0 .   0) (125 . 131))))
682         (< (setq x (+ x 16)) (uncompface-width)))
683       (< (setq y (+ y 16)) (uncompface-height)))
684     (setq y 0)
685     (let ((i 0) guesses k)
686       (uncompface-loop
687         (setq guesses (cond ((= y 1) (nth 2 (uncompface-guesses)))
688                             ((= y 2) (nth 1 (uncompface-guesses)))
689                             (t       (nth 0 (uncompface-guesses))))
690               x 0)
691         (uncompface-loop
692           (setq k 0)
693           (when (>= x 1)
694             (when (>= x 2)
695               (when (>= x 3)
696                 (when (>= y 1)
697                   (when (>= y 2)
698                     (when (>= y 3)
699                       (setq k (uncompface-shift-in k -2 -2)))
700                     (setq k (uncompface-shift-in k -1 -2)))
701                   (setq k (uncompface-shift-in k 0 -2))))
702               (when (>= y 1)
703                 (when (>= y 2)
704                   (when (>= y 3)
705                     (setq k (uncompface-shift-in k -2 -1)))
706                   (setq k (uncompface-shift-in k -1 -1)))
707                 (setq k (uncompface-shift-in k 0 -1))))
708             (when (>= y 2)
709               (when (>= y 3)
710                 (setq k (uncompface-shift-in k -2 0)))
711               (setq k (uncompface-shift-in k -1 0)))
712             (when (>= y 2)
713               (when (>= y 3)
714                 (setq k (uncompface-shift-in k -2 1)))
715               (setq k (uncompface-shift-in k -1 1)))
716             (when (<= x (- (uncompface-width) 2))
717               (when (>= y 2)
718                 (when (>= y 3)
719                   (setq k (uncompface-shift-in k -2 2)))
720                 (setq k (uncompface-shift-in k -1 2)))))
721           (if (aref (car (cond ((= x 1)
722                                 (cdr (cdr guesses)))
723                                ((= x 2)
724                                 (cdr guesses))
725                                ((= x (1- (uncompface-width)))
726                                 (cdr (cdr (cdr guesses))))
727                                (t
728                                 guesses))) k)
729               (aset face i (not (aref face i))))
730           (setq i (1+ i))
731           (< (setq x (1+ x)) (uncompface-width)))
732         (< (setq y (1+ y)) (uncompface-height))))
733     (if raw
734         face
735       (concat (eval-when-compile
736                 (format "P1\n%d %d\n" uncompface-width uncompface-height))
737               (mapconcat (lambda (bit) (if bit "1" "0")) face " ")
738               "\n"))))
739
740 (provide 'compface)
741
742 ;; Local variables:
743 ;; eval: (put 'uncompface-loop 'lisp-indent-hook 0)
744 ;; End:
745
746 ;;; compface.el ends here