-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfont_mcm_to_h.py
115 lines (93 loc) · 2.75 KB
/
font_mcm_to_h.py
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
#!/usr/bin/python
# read an ascii ppm generated by gimp
#
import sys
import png
if len(sys.argv) < 2:
print "usage: " + sys.argv[0] + " file.mcm\n\n"
sys.exit(1)
fn = sys.argv[1]
fh = open(fn, "r")
#debug output image
output_w = 192
output_h = 287
pngh = open('font.png', 'wb')
pngw = png.Writer(output_w, output_h, greyscale=True)
#skip MAX7456 coment
version = fh.readline().strip()
font_data = []
for n in range(256):
for l in range(54):
line = fh.readline().strip()
byte = int(line, 2)
font_data.append(byte)
for l in range(10):
# skip 10 dummy bytes
fh.readline()
# reformat to tinyOSD dataset
font = [[], []]
for data in font_data:
for i in range(4):
bitset = (data & 0xC0)>>6
if (bitset == 0b00):
# black
font[0].append(0)
font[1].append(1)
elif (bitset == 0b10):
# white
font[0].append(1)
font[1].append(0)
else:
# transparent
font[0].append(0)
font[1].append(0)
data = data << 2
# debug output png
# font data is one char after the other (12px x 18px)
pngdata = []
for y in range(output_h):
row = []
for x in range(output_w):
# fetch index. we have chars from left to right, top to bot
char = (y/18)*16+(x/12)
#print "char %d at %d x %d\n" %(char, x, y)
idx = char*(12*18) + (x%12) + ((y%18)*12)
if (font[0][idx] == 1):
color = 0
elif (font[1][idx] == 1):
color = 255
else:
color = 128
row.append(color)
pngdata.append(row)
#print(pngdata)
pngw.write(pngh, pngdata)
pngh.close()
# dump to header
fheader = open('src/font.h', 'w')
fheader.write("#ifndef __FONT_H__\n")
fheader.write("#define __FONT_H__\n")
fheader.write("\n")
fheader.write("// font data\n")
fheader.write("static const uint8_t font_data[2][18][256*2] = {\n")
# blow up font to 16 bits per pixel, add 2 px in front and back
count = 0
for col in range(2):
fheader.write("\n { // color %d" % (col))
for row in range(18):
fheader.write("\n { //row %d\n " % (row))
for char in range(256):
index = char*(12*18) + row*12
word = 0
for i in range(12):
bit = font[col][index + i]
word = (word << 1) | bit
word = word << 2
fheader.write(hex((word >> 8) & 0xFF) + ", " + hex((word & 0xFF)) + ",")
fheader.write("\n ")
count = count + 1
fheader.write(" },")
fheader.write(" },")
fheader.write("};\n")
fheader.write("#endif // __FONT_H__\n")
print ("wrote %d bytes font data" % ( count))