-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxbm_repack.py
81 lines (69 loc) · 2.63 KB
/
xbm_repack.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""xbm_repack.py: Adapt XBM images to use in Python"""
__author__ = "Aleksey.Zholdak"
__version__ = "1.0.1"
__email__ = "aleksey@zholdak.com"
import argparse
import os
import datetime
from bytewriter import ByteAsBytearrayWriter
def get_xbm_data(sourcefile):
errmsg = ''.join(("File: '", sourcefile, "' is not a valid XBM file"))
try:
with open(sourcefile, 'r') as f:
phase = 0
for line in f:
if phase < 2:
if line.startswith('#define'):
yield int(line.split(' ')[-1])
phase += 1
if phase == 2:
start = line.find('{')
if start >= 0:
line = line[start + 1:]
phase += 1
if phase == 3:
if not line.isspace():
phase += 1
if phase == 4:
end = line.find('}')
if end >= 0:
line = line[:end]
phase += 1
hexnums = line.split(',')
if hexnums[0] != '':
for hexnum in [q for q in hexnums if not q.isspace()]:
yield int(hexnum, 16)
if phase != 5:
print(errmsg)
except OSError:
print("Can't open " + sourcefile + " for reading")
def repack_xbm(xbm_file_name, stream):
xbm = get_xbm_data(xbm_file_name)
width = next(xbm)
height = next(xbm)
stream.write("# Created by {} v{} @ {}\n".format(os.path.basename(__file__), __version__,
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
stream.write("width = {:d}\n".format(width))
stream.write("height = {:d}\n".format(height))
writer = ByteAsBytearrayWriter(stream, "bits")
writer.out_data(xbm)
writer.eot()
del writer
del xbm
DESC = """xbm_repack.py
Utility to repack xbm images into xbm_py images.
Sample usage:
xbm_repack.py <infile.xbm> <outfile.py>
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(__file__, description=DESC, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('infile', type=str, help='Input file path')
parser.add_argument('outfile', type=str, help='Path and name of output file')
args = parser.parse_args()
try:
with open(args.outfile, 'w', encoding='utf-8') as stream:
repack_xbm(args.infile, stream)
except OSError:
print("Can't open", args.infile, 'for writing')