-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogo_to_h.py
151 lines (121 loc) · 3.22 KB
/
logo_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
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
#!/usr/bin/python
# read an ascii ppm generated by gimp
#
import sys
if len(sys.argv) < 3:
print "usage: " + sys.argv[0] + " file.ppm src/logo.h\n\n"
sys.exit(1)
fn = sys.argv[1]
fh = open(fn, "r")
outfn = sys.argv[2]
out = open(outfn, "w")
header = fh.readline()
comment = fh.readline()
dimension = fh.readline()
maxcol = fh.readline()
width = int(dimension.split()[0])
height = int(dimension.split()[1])
x = 0
y = 0
byte_out = 0
byte_count = 0;
# width has to be a multiple of 8:
if not (width % 8 == 0):
print("ERROR: image width has to be a multiple of 8!")
sys.exit(1)
imagedata = [[], []] # white and black bits
i = 0
for line in fh:
#print(hex(int(line)))
# skip blue and green:
fh.next()
fh.next()
red_val = int(line)
if (red_val == 0):
# black overlay
imagedata[0].append(0)
imagedata[1].append(1)
elif (red_val == 255):
# white overlay
imagedata[0].append(1)
imagedata[1].append(0)
elif (red_val > 60) and (red_val < 180):
# transparent
imagedata[0].append(0)
imagedata[1].append(0)
else:
# "gray" overlay
imagedata[0].append(1)
imagedata[1].append(1)
# next bit
i = i + 1
image_bytes = [[], []] # white and black bytes
for col in range(2):
byte = 0
byte_count = 0
for bit in imagedata[col]:
byte = byte << 1
if (bit != 0):
byte = byte | 0x01
if (byte_count == 8):
# byte finished!
image_bytes[col].append(byte)
byte_count = 0
byte = 0
byte_count = byte_count + 1
out.write("#ifndef LOGO_H__\n")
out.write("#define LOGO_H__\n")
out.write("#include <stdint.h>\n")
out.write("\n")
out.write("#define LOGO_WIDTH " + str(width) + "\n")
out.write("#define LOGO_HEIGHT " + str(height) + "\n")
out.write("\n")
# generate 16bit logo data:
image_hwords = [[], []]
for c in range(2):
count = 1-c #white is 1 byte shifted
hw = 0x00
for x in image_bytes[c]:
hw = (hw>>8) | (x<<8)
if (count == 2):
image_hwords[c].append(hw)
hw = 0
count = 0
count = count + 1
out.write("static const uint16_t logo_data16[2][LOGO_HEIGHT * (LOGO_WIDTH/8) / 2] = {\n")
out.write(" {\n ");
count = 0
for x in image_hwords[0]:
out.write(hex(x) + ", ")
if ((count % 16) == 15):
out.write("\n ")
count = count + 1
out.write("\n }, \n")
out.write(" {\n ");
count = 0
for x in image_hwords[1]:
out.write(hex(x) + ", ")
if ((count % 16) == 15):
out.write("\n ")
count = count + 1
out.write("\n }\n};")
out.write("static const uint8_t logo_data8[2][LOGO_HEIGHT * (LOGO_WIDTH/8)] = {\n")
out.write(" {\n ");
count = 0
for x in image_bytes[0]:
out.write(hex(x) + ", ")
if ((count % 16) == 15):
out.write("\n ")
count = count + 1
out.write("\n }, \n")
out.write(" {\n ");
count = 0
for x in image_bytes[1]:
out.write(hex(x) + ", ")
if ((count % 16) == 15):
out.write("\n ")
count = count + 1
out.write("\n }\n};")
out.write("\n#endif // LOGO_H__\n")
fh.close()
out.close()