-
Notifications
You must be signed in to change notification settings - Fork 25
/
conv_skin.py
53 lines (43 loc) · 1.26 KB
/
conv_skin.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
#!/usr/bin/python
from PIL import Image
import lzf
import struct
FILENAMES = ('gb_skin.png', 'gbc_skin.png')
def compress(data):
return struct.pack('<H', len(data)) + lzf.compress(data)
outdata = b''
for name in FILENAMES:
img = Image.open(name)
palette = bytearray()
pal = img.getpalette()
for (r, g, b) in zip(pal[0:48:3], pal[1:48:3], pal[2:48:3]):
color = ((g & 4) << 13) + ((b >> 3) << 10) + ((g >> 3) << 5) + (r >> 3)
palette += struct.pack('<H', color)
data = bytearray()
last_pixel = None
for pixel in (img.getpixel((x, y)) for y in range(img.height) for x in
range(img.width)):
if last_pixel is None:
last_pixel = pixel
else:
data.append(last_pixel * 16 + pixel)
last_pixel = None
if last_pixel is not None:
data.append(last_pixel * 16)
data = palette + compress(bytes(data))
print(len(data))
outdata += data
fout = open('skin.asm', 'w')
line = '.db '
comma = 0
for byte in outdata:
to_append = ',' * comma + '$%02X' % byte
if len(line + to_append) > 100:
fout.write(line + '\n')
line = '.db $%02X' % byte
else:
line += to_append
comma = 1
if comma:
fout.write(line + '\n')
fout.close()