File: XFace.py

package info (click to toggle)
xpn 1.2.6-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,500 kB
  • ctags: 1,685
  • sloc: python: 12,693; xml: 206; makefile: 16; sh: 1
file content (359 lines) | stat: -rw-r--r-- 20,369 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# This module is a translation from JavaScript to Python of the Mnheny (a Thunderbird extension) X-Face decoder.
# Thanks to:
# James Ashton <jaa@cs.su.oz.au> (author of the original C source of 'uncompface')
# Andrew Taylor <ataylor@its.to> (author the JavaScript translation of 'uncompface')
# Karsten Dasterloh <mnenhy@tprac.de> (author of the Mnenhy Thunderbird extension)

import re

LENGTH=48
PIXELS=(LENGTH * LENGTH)

FIRSTPRINT = ord('!')
LASTPRINT  = ord('~')
NUMPRINTS = (LASTPRINT - FIRSTPRINT + 1)

BITSPERWORD=8
WORDCARRY=(1 << BITSPERWORD)
WORDMASK=(WORDCARRY - 1)
MAXWORDS=((PIXELS * 2 + BITSPERWORD - 1) / BITSPERWORD)

BLACK=0
GREY =1
WHITE=2

F=[]
for i in range(0,PIXELS):
    F.append(0)

levels=[
    [{"p_offset":255, "p_range":1  }, {"p_offset":0, "p_range":251}, {"p_offset":251, "p_range":4  }],# Top of tree almost always grey 
    [{"p_offset":255, "p_range":1  }, {"p_offset":0, "p_range":200}, {"p_offset":200, "p_range":55 }],
    [{"p_offset":223, "p_range":33 }, {"p_offset":0, "p_range":159}, {"p_offset":159, "p_range":64 }],
    [{"p_offset":0,   "p_range":131}, {"p_offset":0, "p_range":0  }, {"p_offset":131, "p_range":125}] # Grey disallowed at bottom 
  ]

freqs =[
    {"p_offset":0,   "p_range":0 }, {"p_offset":0,   "p_range":38}, {"p_offset":38,  "p_range":38}, {"p_offset":152, "p_range":13},
    {"p_offset":76,  "p_range":38}, {"p_offset":165, "p_range":13}, {"p_offset":178, "p_range":13}, {"p_offset":230, "p_range":6 },
    {"p_offset":114, "p_range":38}, {"p_offset":191, "p_range":13}, {"p_offset":204, "p_range":13}, {"p_offset":236, "p_range":6 },
    {"p_offset":217, "p_range":13}, {"p_offset":242, "p_range":6 }, {"p_offset":248, "p_range":5 }, {"p_offset":253, "p_range":3 }
  ]

G ={
  "g_00":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        1,1,1,0,0,0,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,1,1,0,0,0,0,0,1,0,1,0,0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11":[0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,1,1,1,1,1],
  "g_21":[0,0,0,1,0,1,1,1],
  "g_31":[0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,1,1,1,1,1,1],
  "g_41":[0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,
        0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  "g_12":[0,1],
  "g_22":[0],
  "g_32":[0,0,0,1],
  "g_42":[0,0,0,1]
}

def BigPop(p):
    ''' p is freqs oder levels[lev]'''
    global B
    r= B&255 #it is the LS BYTE
    B= B>>8
    i=0
    while ((r < p[i]["p_offset"]) or (r >= p[i]["p_range"] + p[i]["p_offset"])):
        i=i+1

    B=B*p[i]["p_range"]
    B=B+(r-p[i]["p_offset"])
    return i

def PopGreys(off, len):
    global B
    global F
    if (len > 3): 
        len /= 2
        PopGreys(off,                      len)
        PopGreys(off + len,                len)
        PopGreys(off + LENGTH * len,       len)
        PopGreys(off + LENGTH * len + len, len)
    else:
        len = BigPop(freqs)
        if (len & 1):
            F[off] = 1
        if (len & 2):
            F[off + 1] = 1
        if (len & 4):
            F[off + LENGTH] = 1
        if (len & 8):
            F[off + LENGTH + 1] = 1


def UnCompress(off, len, lev):
    global B
    big_pop=BigPop(levels[lev])
    if big_pop==WHITE:
        return
    elif big_pop==BLACK:
        PopGreys(off, len)
        return;
    else :
        len /= 2
        lev=lev+1
        UnCompress(off,                      len, lev)
        UnCompress(off + len,                len, lev)
        UnCompress(off + len * LENGTH,       len, lev)
        UnCompress(off + len * LENGTH + len, len, lev)
        return


def UnCompAll(fbuf):
    global B
    global F
    B=0L #B is a long integer
    # convert base 94 to base 256
    kl = len(fbuf)
    for i in range(0,kl):
        B=B*NUMPRINTS
        B=B+(ord(fbuf[i]) - FIRSTPRINT)
    # empty icon
    for i in range(0,PIXELS):
        F[i] = 0
    #uncompress
    UnCompress(0,    16, 0)
    UnCompress(16,   16, 0)
    UnCompress(32,   16, 0)
    UnCompress(768,  16, 0)
    UnCompress(784,  16, 0)
    UnCompress(800,  16, 0)
    UnCompress(1536, 16, 0)
    UnCompress(1552, 16, 0)
    UnCompress(1568, 16, 0)



def Gen():
    global F 
    m= l= k= j= i= h=0
    for j in range(0,LENGTH): 
        for i in range(0,LENGTH):
            k = 0
            for l in range(i-2,i+2+1):
                for m in range(j-2,j+1):
                    if ((l >= i) and (m == j)):
                        continue
                    if ((l > 0) and (l <= LENGTH) and (m > 0)):
                        if F[l+m*LENGTH]:
                            k=k*2+1
                        else:
                            k=k*2
            if i== 1 :
                if   j== 1 : F[h] ^= G["g_22"][k]
                elif j== 2 : F[h] ^= G["g_21"][k]
                else       : F[h] ^= G["g_20"][k]
            elif i== 2 :
                if   j== 1 : F[h] ^= G["g_12"][k]
                elif j== 2 : F[h] ^= G["g_11"][k]
                else       : F[h] ^= G["g_10"][k]
            elif i== LENGTH - 1 :
                if   j== 1 : F[h] ^= G["g_42"][k]
                elif j== 2 : F[h] ^= G["g_41"][k] 
                else       : F[h] ^= G["g_40"][k]
            elif i== LENGTH :
                if   j== 1 : F[h] ^= G["g_32"][k]
                elif j== 2 : F[h] ^= G["g_31"][k]
                else       : F[h] ^= G["g_30"][k]
            else :
                if   j== 1 : F[h] ^= G["g_02"][k]
                elif j== 2 : F[h] ^= G["g_01"][k]
                else       : F[h] ^= G["g_00"][k]
            h=h+1

def parseInt(num_string):
    '''It should convert the numeric string in to a number base 2.

    For example parseInt("1111") returns 15
    If it finds a character that is not in base 2 it stops there converting the
    leftmost part'''
    
    num=""
    for char in num_string:
        if char=="0" or char=="1":
            num=num+char
        else:
            break
    conv=0
    for i in range(len(num)-1,-1,-1):
        conv=conv+int(num[len(num)-1-i])*pow(2,i)
    return conv

def join_num(list):
    string_list=[]
    for item in list:
        string_list.append(str(item))
    return "".join(string_list)

def replacer1(matchobject):
    g=matchobject.group(0)
    return g+","

def replacer2(matchobject):
    g1,g2,g3,g4,g5,g6= matchobject.groups()
    return chr(parseInt(g1))+\
           chr(parseInt(g2))+\
           chr(parseInt(g3))+\
           chr(parseInt(g4))+\
           chr(parseInt(g5))+\
           chr(parseInt(g6)) + "\0\0"

def XFaceToBMP(face):
    global F
    face=re.sub("[^!-~]","",face)
    UnCompAll(face)
    Gen()
    bmp = "BM\xBE\1\0\0\0\0\0\0>\0\0\0(\0\0\0\x30\0\0\0\x30\0\0\0\1\0\1\0\0\0\0\0\x80\1\0\0\xC4\x0E\0\0\xC4\x0E\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\0\0\0\0\0";
    ff=join_num(F)
    ff=re.sub("(.{48})",replacer1,ff)
    ff_l=ff.split(",")
    ff_l.reverse()
    ff="".join(ff_l)
    ff=re.sub("(.{8})(.{8})(.{8})(.{8})(.{8})(.{8})",replacer2,ff) 
    bmp=bmp+ff
    return bmp

def XFaceToBuffer(face):
    global F
    face=re.sub("[^!-~]","",face)
    UnCompAll(face)
    Gen()
    ff=join_num(F)
    buff_pos=ff.replace("1",chr(0)).replace("0",chr(255))
    return buff_pos