-
Notifications
You must be signed in to change notification settings - Fork 2
/
img2ass.py
242 lines (217 loc) · 6.18 KB
/
img2ass.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python3
import struct, sys, os, io
import argparse
from PIL import Image
from pathlib import Path
parser = argparse.ArgumentParser(description='Convert an image to the A3X .api format.')
parser.add_argument('inFile', help='source image file')
parser.add_argument('outFile', nargs='?', help='target .api file')
parser.add_argument('-r', '--raw', help='skip compression', action='store_true')
parser.add_argument('-v', '--verbose', help='use verbose output', action='store_true')
parser.add_argument('-g', '--nograds', help='skip HDMA gradients', action='store_true')
parser.add_argument('-c', '--clipgrads', help='clip HDMA gradients', action='store_true')
parser.add_argument('-t', '--truepal', help='store minimal palette data', action='store_true')
args = parser.parse_args()
stem = Path(args.inFile).stem
if not args.outFile:
args.outFile = stem + '.api'
if args.verbose:
print(f'No output file given, assuming {args.outFile}.')
im = Image.open(args.inFile)
if im.mode != 'P':
print('Image is not indexed.')
quit()
inPal = im.getpalette()
outData = bytes(im.getdata())
truePalLength = max(outData) + 1
fourBits = truePalLength < 17
if args.verbose:
if fourBits:
print('Image uses only 16 colors.')
else:
print('Image uses more than 16 colors.');
stride = im.width
if fourBits:
stride = im.width // 2
palSize = 32 if fourBits else 512
if args.truepal:
palSize = truePalLength * 2
palLength = 16 if fourBits else 256
palOffset = 0x18
dataOffset = palOffset + palSize
size = im.width * im.height
if not fourBits:
size /= 2
compressed = True
depth = 4 if fourBits else 8
if fourBits:
newData = bytearray()
i = 0
while i < len(outData) - 1:
theFour = outData[i + 0]
theFour |= outData[i + 1] << 4
i += 2
newData.append(theFour)
outData = newData
def rleCompress(data):
if args.verbose:
print(f'Attempting to compress {len(data)} bytes...')
ret = bytearray()
i, count = 0, 0
def emit(data, i, count):
if i >= len(data):
return
if data[i] >= 0xC0 or count > 0:
ret.append(0xC0 | (count + 1))
ret.append(data[i])
return 0
while i < len(data) - 1:
if data[i] == data[i + 1]:
if count == 62:
count = emit(data, i, count)
else:
count += 1
else:
count = emit(data, i, count)
i += 1
count = emit(data, i, count)
ret.append(0xC0)
ret.append(0xC0)
if args.verbose:
print(f'Ended up with {len(ret)} bytes.')
return ret
if args.raw:
compressed = False
if args.verbose:
print('Skipping compression by request.')
else:
compData = rleCompress(outData)
if len(compData) < len(outData):
outData = compData
else:
compressed = False
dataSize = len(outData)
dataOffset = palOffset + palSize
flags = 1 if compressed else 0
hdmaChannels = []
for i in range(0, 8):
hdmaFile = stem + f'-h{i}.png'
if os.path.exists(hdmaFile):
hdmaChannels.append(i)
if args.nograds:
if len(hdmaChannels):
print(f'Ignoring {len(hdmaChannels)} HDMA channel(s).')
hdmaChannels = []
if len(hdmaChannels) > 0:
flags = flags | 2
palOffset = palOffset + 4
dataOffset = dataOffset + 4
hdmaOffset = dataOffset + dataSize
if args.verbose:
print(f'depth: {depth}')
print(f'flags: {flags}')
print(f'width: {im.width}, height: {im.height}, stride: {stride}')
print(f'palLength: {palLength} (true {truePalLength})')
print(f'palSize: 0x{palSize:X} ({palSize})')
print(f'palOffset: 0x{palOffset:X}')
print(f'dataSize: 0x{len(outData):X}')
print(f'dataOffset: 0x{dataOffset:X}')
if len(hdmaChannels) > 0:
print(f'hdma: {hdmaChannels}')
print(f'hdmaOffset: 0x{hdmaOffset:X}')
bf = io.BytesIO()
bf.write(b'AIMG')
bf.write(struct.pack('>b', depth))
bf.write(struct.pack('>b', flags))
bf.write(struct.pack('>H', im.width))
bf.write(struct.pack('>H', im.height))
bf.write(struct.pack('>H', stride))
bf.write(struct.pack('>L', im.height * stride)) #len(outData)))
bf.write(struct.pack('>L', palOffset))
bf.write(struct.pack('>L', dataOffset))
if len(hdmaChannels) > 0:
bf.write(struct.pack('>L', hdmaOffset))
for i in range(palLength):
if i < truePalLength:
r, g, b = inPal[(i * 3) + 0], inPal[(i * 3) + 1], inPal[(i * 3) + 2]
else:
if args.truepal:
break;
r, g, b = 0, 0, 0
snes = ((b >> 3) << 10) | ((g >> 3) << 5) | (r >> 3)
bf.write(struct.pack('>H', snes))
bf.write(outData)
if len(hdmaChannels) > 0:
bf.write(struct.pack('>b', len(hdmaChannels)))
for i in hdmaChannels:
clipgrads = args.clipgrads
im = Image.open(stem + f'-h{i}.png')
if im.mode != 'RGB':
im = im.convert('RGB')
tl = im.getpixel((0,0))
tr = im.getpixel((im.size[0]-1,0))
if tr == (255, 255, 255) and tl != (255, 255, 255):
clipgrads = False
if args.verbose:
print(f'Disabling clipping for channel {i}.')
imgh = im.size[1]
gradient = []
for y in range(imgh):
r, g, b = im.getpixel((0, y))
snes = ((b >> 3) << 10) | ((g >> 3) << 5) | (r >> 3)
gradient.append(snes)
start = 0
stop = imgh
if clipgrads:
first = gradient[start]
last = gradient[stop - 1]
for y in range(start, stop):
if gradient[y] != first:
start = y
break
for y in range(stop - 1, start + 2, -1):
if gradient[y] != last:
stop = y
break
gradient = gradient[start:stop]
if imgh <= 240:
start = start * 2
stop = stop * 2
length = stop - start
hdmaControl = 1 | (1 << 4) | (start << 8) | (length << 20)
if imgh == 240:
hdmaControl = hdmaControl | 0x80
bf.write(struct.pack('>H', len(gradient) * 2))
bf.write(struct.pack('>L', hdmaControl))
for y in gradient:
bf.write(struct.pack('>H', y))
if args.verbose:
print(f'hdma[{i}]: 0x{hdmaControl:0>8X}, size {len(gradient)}, {start} to {stop}, for {length}')
if Path(args.outFile).suffix == '.c' or Path(args.outFile).suffix == '.s':
asS = not Path(args.outFile).suffix == '.c'
of = open(args.outFile, "w")
data = bf.getvalue()
if asS:
of.write('\t.section .rodata\n')
of.write('\t.align 2\n')
of.write(f'\t.global {stem}\n')
if asS:
of.write(f'{stem}:')
else:
of.write(f'const unsigned char {stem}[{len(data)}] = {{')
i = 0
for v in data:
if i % 16 == 0:
of.write('\n\t.byte ' if asS else '\n\t')
elif asS:
of.write(',')
i += 1
of.write(f'0x{v:02X}')
if not asS:
of.write(', ')
of.write('\n')
if not asS:
of.write('};\n')
else:
with open(args.outFile, "wb") as of:
of.write(bf.getbuffer())