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