diff --git a/gba/tools/img2bin.py b/gba/tools/img2bin.py index 0e8f204..c8d4ec0 100644 --- a/gba/tools/img2bin.py +++ b/gba/tools/img2bin.py @@ -2,19 +2,40 @@ from PIL import Image import sys import os +import struct """Convert image to raw binary using Python Imaging Library.""" -if len(sys.argv) != 3: - print "Usage: %s " % os.path.split(sys.argv[0])[1] - print "WARNING: It only converts 8x8 pixel images at 8bit the moment." - sys.exit() +def split_seq(seq, splitsize): + newseq = [] + while seq: + newseq.append(seq[:splitsize]) + print "elem:", newseq[-1] + seq = seq[splitsize:] + return newseq -im = Image.open(sys.argv[1]) -output = open(sys.argv[2], "wb") -for x in range(8): - for y in range(8): - val = im.getpixel((x, y)) - output.write(chr(val)) +if __name__=="__main__": + if len(sys.argv) < 3: + print "Usage: %s [palette]" % os.path.split(sys.argv[0])[1] + print "WARNING: It only converts 8bit images at the moment... and no tiling." + sys.exit() + im = Image.open(sys.argv[1]) + output = open(sys.argv[2], "wb") + width, height = im.size + for y in range(height): + for x in range(width): + val = im.getpixel((x, y)) + output.write(chr(val)) + if len(sys.argv) > 3: + pal_output = open(sys.argv[3], "wb") + print "entries:", len(im.palette.palette) + for i, color in enumerate(split_seq(im.palette.palette, 3)): + r, g, b = map(lambda col : col >> 3, map(ord, color)) + + print "rgb:", r, g, b + value = (b << 10) + (g << 5) + r + print "color", i, ":", value + pal_output.write(struct.pack("