# 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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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
|