forked from OTAcademy/RME
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_png2cpp.py
More file actions
65 lines (53 loc) · 1.57 KB
/
convert_png2cpp.py
File metadata and controls
65 lines (53 loc) · 1.57 KB
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
#!/usr/bin/python
"""
Converts all .png files into this directory to a .h & one .cpp
file.
"""
import sys
import os
import os.path
import re
import array
import glob
r = re.compile("^([a-zA-Z._][a-zA-Z._0-9]*)[.][pP][nN][gG]$")
header_file = open("pngfiles.h", "wb")
source_file = open("pngfiles.cpp", "wb")
header_file.write("#ifndef __PNG_HEADER_FILE_H__\n")
header_file.write("#define __PNG_HEADER_FILE_H__\n")
png_files = glob.glob("*.png")
for path in png_files:
filename = os.path.basename(path)
m = r.match(filename)
# Allow only filenames that make sense
# as C variable names
if not(m):
print "Skipped file (unsuitable filename): " + filename
continue
# Read PNG file as character array
bytes = array.array('B', open(path, "rb").read())
count = len(bytes)
# Output to header
header_file.write("extern unsigned char " + m.group(1) + "_png[" + str(count) + "];\n")
# Create the C file
text = "/* " + filename + " - " + str(count) + " bytes */\nunsigned char " + m.group(1) + "_png[" + str(count) + "] = {\n"
# Iterate the characters, we want
# lines like:
# 0x01, 0x02, .... (8 values per line maximum)
i = 0
count = len(bytes)
for byte in bytes:
# Every new line starts with two whitespaces
if (i % 8) == 0:
text += " "
# Then the hex data (up to 8 values per line)
text += "0x%02x" % (byte)
# Separate all but the last values
if (i + 1) < count:
text += ", "
if (i % 8) == 7:
text += '\n'
i += 1
# Now conclude the C source
text += "};\n/* End Of File */\n"
source_file.write(text)
header_file.write("#endif //__PNG_HEADER_FILE_H__\n")