import sys
import Image
from array import array
from encode import encode

# tile: 120 x 136

# http://www.starnetblog.com/tileable/worn-blue-retro-tileable-patterns/

def main(filename):
    im = Image.open(filename).convert("RGB")
    screen = Image.new("RGB", (512, 512))
    for x in range(0, 512, im.size[0]):
        for y in range(0, 512, im.size[1]):
            screen.paste(im, (x, y))
    (picdata, chrdata, paldata) = encode(screen)
    mem = array('B', [0] * 16384)
    mem[0:0x1000] = picdata
    mem[0x1000:0x2000] = chrdata
    mem[0x2000:0x2800] = paldata

    memf = open(filename + ".mem", "w")
    print >>memf, "@ 0"
    for b in mem[0:0x2800]:
        print >>memf, "%02x" % b

if __name__ == "__main__":
    main(sys.argv[1])
