94 lines
2.2 KiB
Python
Executable File
94 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
|
# 128x64
|
|
# convert gnaa.png +dither -monochrome -colors 2 image.bmp
|
|
|
|
from PIL import Image
|
|
import sys
|
|
import math
|
|
|
|
#import zlib
|
|
|
|
invert = 0
|
|
|
|
def set_bit(value, bit):
|
|
return value | (1<<bit)
|
|
|
|
def clear_bit(value, bit):
|
|
return value & ~(1<<bit)
|
|
|
|
def print_array(name, buf, bytes_per_line = 10):
|
|
c = 0
|
|
|
|
print("uint8_t " + name + "[" + str(len(buf)) + "] = {")
|
|
|
|
for v in buf:
|
|
if c == 0:
|
|
print(" ", end='')
|
|
|
|
print(format(v, '#04x') + ", ", end='')
|
|
c = c + 1
|
|
if (c == bytes_per_line):
|
|
print()
|
|
c = 0
|
|
|
|
if (c <= bytes_per_line):
|
|
print()
|
|
print("};")
|
|
print()
|
|
|
|
def print_sprites(name, height, width):
|
|
print("SpriteDef " + name +" = {" + str(height) + ", " + str(width) + ", " + name + '_Buf' + "};")
|
|
|
|
if __name__ == "__main__":
|
|
if (len(sys.argv) < 2):
|
|
sys.exit()
|
|
|
|
frames = []
|
|
|
|
counter = 0
|
|
for file in sys.argv[1:]:
|
|
im = Image.open(file)
|
|
width, height = im.size
|
|
|
|
if (height > 64 or width > 128):
|
|
sys.exit()
|
|
|
|
banks = math.ceil(height / 8);
|
|
|
|
#print(banks)
|
|
|
|
buf = []
|
|
|
|
for bank in range(0, banks):
|
|
for x in range(0, width):
|
|
value = 0
|
|
if (invert == 1):
|
|
value = 255
|
|
c = 7
|
|
#print("OI")
|
|
for y in range( (bank * 8 ) + 7, (bank * 8) - 1, -1):
|
|
#print(bank, x, y)
|
|
b = im.getpixel((x,y))
|
|
|
|
if (b == 1 or b == 255):
|
|
value = set_bit(value, c)
|
|
if (invert == 1):
|
|
value = clear_bit(value, c)
|
|
c = c - 1
|
|
buf.append(value)
|
|
|
|
#buffer = zlib.compress(bytes(buffer))
|
|
frames.append({'name': 'sprite_' + str(counter), 'buf': buf})
|
|
counter = counter + 1
|
|
|
|
|
|
print('#include "sprites.h"')
|
|
print()
|
|
for item in frames:
|
|
print_array(item['name'] + "_Buf", item['buf'])
|
|
for item in frames:
|
|
print_sprites(item['name'], height, width)
|