import Image
import ImageDraw
import gameduino
import gameduino.prep as gdprep
import random
from gameduino.compress import Codec

im = Image.open("watterott_spr.png")
im = gdprep.palettize(im, 16)
im.save("out.png")

hh = open("../../watterott_logo.h", "w")
gdprep.dump(hh, "wlogo_palette", gdprep.getpal(im))

ir = gdprep.ImageRAM(hh)
ir.addsprites("wlogo", (16,48), im, gdprep.PALETTE16A)
spark = gdprep.palettize(Image.open("spark.png"), 4)
ir.addsprites("spark", (16,16), spark, gdprep.PALETTE4A, center=(6,5))
gdprep.dump(hh, "spark_palette", gdprep.getpal(spark))

cc = Codec(b_off = 9, b_len = 3)
uncompressed = ir.used().tostring()
wcd = cc.toarray(uncompressed)
print "compressed to", len(wcd)
gdprep.dump(hh, "wlogo_compressed", wcd)

def rpt(p):
    # center = 81,36
    (x,y) = (p[0] - 81, p[1] - 36)
    rf = 0.8 + random.random() * 0.2
    return (81 + int(x * rf), 36 + int(y * rf))
def scale(p, rf):
    # center = 81,36
    (x,y) = (p[0] - 81, p[1] - 36)
    return (81 + int(x * rf), 36 + int(y * rf))
pts = [(80,8), (87,28), (156,5), (93,37), (184,40), (86,44), (76,90), (73,46), (6,66), (65,38), (23,1), (74,28)]
anims = 5
bgfull = Image.new("RGBA", (200,anims*96))
random.seed(0)
for i in range(anims):
    bg = Image.new("RGBA", (200,96), (231,120,23,255))
    dr = ImageDraw.Draw(bg)
    szs = [0.7 + 0.3 * random.random() for p in pts]
    if 0:
        rrpp = [scale(p, s) for (p, s) in zip(pts, szs)]
        dr.polygon(rrpp, outline=(255,255,255), fill=(231,120,23))
    else:
        dr.polygon([scale(p, s) for (p, s) in zip(pts, szs)], outline=(255,255,255,255))
        ImageDraw.floodfill(bg, (0, 0), (0,0,0,0))
    bgfull.paste(bg, (0,96*i))
    bg.save("bg%d.png" % i)

bgfull.save("bg.png")
(dpic,dchr,dpal) = gdprep.encode(bgfull)
print len(dchr) / 16, "chars"

gdprep.dump(hh, "logobg_pic", dpic)
gdprep.dump(hh, "logobg_chr_compressed", cc.toarray(dchr.tostring()))
gdprep.dump(hh, "logobg_pal_compressed", cc.toarray(dpal.tostring()))
