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