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
|
import struct
import sys
rgbformat = "BBB"
CHAR_WIDTH = 4
CHAR_HEIGHT = 8
context = {
'nrows' : 0,
'ncols' : 0,
'width' : 0,
'height' : 0,
}
def rgbto565(r, g, b):
return (( (r & 0xf8) ) | ((g & 0xe0) >> 5),
( (g & 0x1c) << 3 ) | ((b & 0xf8) >> 3))
def gentbl():
c = 0
a = ""
for i in range(32, 127):
a += "%c" % chr(i)
c += 1
if c == 16:
c = 0
a = ""
def output_chr(context, out, data, offset):
width = context['width']
for i in range(CHAR_HEIGHT):
off = offset + i * width
for j in range(CHAR_WIDTH):
o = 3 * (off + j)
r, g, b = struct.unpack(rgbformat, data[o:o + 3])
rgb565 = rgbto565(r, g, b)
out += chr(rgb565[0])
out += chr(rgb565[1])
return out
def convert2table(context, data):
out = ""
width = context['width']
for i in xrange(context['nrows']):
off = i * CHAR_HEIGHT * width
for j in xrange(context['ncols']):
o = off + j * CHAR_WIDTH
out = output_chr(context, out, data, o)
return out
def readpnm(context, prefix):
pnm = open(prefix + ".pnm", "r")
d = pnm.readline()
d = pnm.readline()
l = pnm.readline()
a, b = l.split()
x, y = int(a), int(b)
a = pnm.readline()
l = x * y
context['width'] = x
context['height'] = y
context['ncols'] = x / CHAR_WIDTH
context['nrows'] = y / CHAR_HEIGHT
data = pnm.read()
pnm.close()
return data
def convert2raw(context, data):
out = ""
for i in xrange(l):
off = 3 * i
r, g, b = struct.unpack(rgbformat, data[off:off + 3])
rgb565 = rgbto565(r, g, b)
out += chr(rgb565[0])
out += chr(rgb565[1])
return out
# gentbl()
c = {}
out = readpnm(c, sys.argv[1])
out = convert2table(c, out)
f = open(sys.argv[1] + ".bin", "w")
f.write(out)
f.close()
|