1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-27 01:51:35 +00:00
8bitworkshop/tools/pcx2will.py

47 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/python
import sys, array, string
2017-04-06 19:09:30 +00:00
col0 = 0
def tocolor(x):
if x == 0:
return 0
else:
return x + col0
def tohex2(v):
return '0x%02x'%v
with open(sys.argv[1],'rb') as f:
data = array.array('B', f.read())
assert data[0] == 0xa
assert data[3] == 8
# palette
2018-11-22 19:11:56 +00:00
print("byte palette[16] = {", end='')
for i in range(0,16):
r = data[16+i*3]
g = data[17+i*3]
b = data[18+i*3]
entry = (r>>5) | ((g>>2)&0x38) | (b&0xc0)
2018-11-22 19:11:56 +00:00
print('%s,' % (tohex2(entry)), end='')
print("}")
# image data
width = (data[9] << 8) + data[8] + 1
height = (data[11] << 8) + data[10] + 1
rowlen = (data[0x43] << 8) + data[0x42]
2018-11-22 19:11:56 +00:00
print("const byte sprite[] = {")
print("%d,%d," % ((width+1)/2,height))
for y in range(0,height):
ofs = 0x80 + y*rowlen
output = []
for x in range(0,width,2):
2017-04-06 19:09:30 +00:00
b = (tocolor(data[ofs]) << 4) + tocolor(data[ofs+1])
output.append(b)
ofs += 2
2018-11-22 19:11:56 +00:00
print(','.join(map(tohex2,output)) + ',')
print("}")
2017-04-06 19:09:30 +00:00