-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics_compress.py
139 lines (123 loc) · 3.32 KB
/
graphics_compress.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
import argparse
import math
class GraphicsCompressor(object):
def init(self):
self.state = None
self.size = 0
self.buffer = bytearray()
self.prev = None
self.commands = []
def compress(self, data):
self.init()
for byte in data:
if self.prev is None:
self.new_literal(byte)
elif self.state == 'same':
self.same_state(byte)
elif self.state == 'inc':
self.inc_state(byte)
else: # self.state == 'literal'
self.literal_state(byte)
self.prev = byte
self.flush()
def same_state(self, byte):
if byte == self.prev:
self.size += 1
return
self.flush()
self.new_literal(byte)
def inc_state(self, byte):
if ord(byte) == ord(self.prev) + 1:
self.size += 1
return
self.flush()
self.new_literal(byte)
def literal_state(self, byte):
if byte == self.prev:
self.pull_one_flush()
self.state = 'same'
self.size = 2
self.buffer = bytearray([self.prev])
elif ord(byte) == ord(self.prev) + 1:
self.pull_one_flush()
self.state = 'inc'
self.size = 2
self.buffer = bytearray([self.prev])
else:
self.state = 'literal'
self.size += 1
self.buffer.append(byte)
def new_literal(self, byte):
self.state = 'literal'
self.size = 1
self.buffer = bytearray([byte])
def pull_one_flush(self):
self.buffer = self.buffer[:-1]
self.size -= 1
self.flush()
def flush(self):
while self.size > 0:
if self.size <= 63:
self.commands.append([self.state, self.size, self.buffer])
break
self.commands.append([self.state, 63, self.buffer])
self.size -= 63
self.buffer = bytearray()
self.size = 0
def to_bytes(self):
accum = []
for kind, size, data in self.commands:
if kind == 'same':
accum.append(0x80 | size)
accum.append(data[0])
elif kind == 'inc':
accum.append(0x40 | size)
accum.append(data[0])
elif kind == 'literal':
accum.append(0x00 | size)
accum += data
return accum
def trim_ending(data):
i = len(data) - 1
while i > 0:
if ord(data[i]) != 0:
end = int(math.ceil((i + 1) / 32.0) * 32 + 1)
return data[0:end]
i -= 1
return data
def run():
parser = argparse.ArgumentParser()
parser.add_argument('input')
parser.add_argument('-o', dest='output')
parser.add_argument('-n', dest='no_null', action='store_true')
parser.add_argument('-t', dest='trim', action='store_true')
args = parser.parse_args()
fp = open(args.input, 'r')
content = fp.read()
fp.close()
if args.trim:
content = trim_ending(content)
compressor = GraphicsCompressor()
compressor.compress(content)
fout = open(args.output, 'w')
count = 0
for kind, size, data in compressor.commands:
if kind == 'same':
fout.write('.byte $%02x\n' % (0x80 | size))
count += 1
elif kind == 'inc':
fout.write('.byte $%02x\n' % (0x40 | size))
count += 1
elif kind == 'literal':
fout.write('.byte $%02x\n' % (0x00 | size))
count += 1
fout.write('.byte %s\n' % ','.join('$%02x' % b for b in data))
count += len(data)
fout.write('\n')
if not args.no_null:
fout.write('.byte $00\n')
count += 1
fout.write('; %d\n' % count)
fout.close()
if __name__ == '__main__':
run()