|
|
|
@ -4,12 +4,18 @@ import os
|
|
|
|
|
import re
|
|
|
|
|
import struct
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from PIL import Image
|
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
|
from common import crc24_func, font
|
|
|
|
|
|
|
|
|
|
def makedef(indir, outdir):
|
|
|
|
|
def get_color(fname):
|
|
|
|
|
crc = crc24_func(fname)
|
|
|
|
|
# values 0-7 must not be used as they might represent transparency
|
|
|
|
|
# so we are left with 248 values
|
|
|
|
|
return 8+crc%248
|
|
|
|
|
|
|
|
|
|
def makedef(indir, outdir, shred=True):
|
|
|
|
|
infiles = defaultdict(list)
|
|
|
|
|
sig = None
|
|
|
|
|
fnames = set()
|
|
|
|
|
# sanity checks and fill infiles dict
|
|
|
|
|
for f in os.listdir(indir):
|
|
|
|
|
m = re.match('(\d+)_([A-Za-z0-9_]+)_(\d\d)_(\d\d)_([A-Za-z0-9_]+)_(\d+)x(\d+)_(\d+)x(\d+).png', f)
|
|
|
|
@ -22,7 +28,7 @@ def makedef(indir, outdir):
|
|
|
|
|
if im.mode != 'P':
|
|
|
|
|
print "input images must have a palette"
|
|
|
|
|
return False
|
|
|
|
|
cursig =(t,p,fw,fh,im.getpalette())
|
|
|
|
|
cursig =(t,p,im.getpalette())
|
|
|
|
|
if not sig:
|
|
|
|
|
sig = cursig
|
|
|
|
|
else:
|
|
|
|
@ -31,30 +37,29 @@ def makedef(indir, outdir):
|
|
|
|
|
print sig
|
|
|
|
|
print cursig
|
|
|
|
|
return False
|
|
|
|
|
if fn in fnames:
|
|
|
|
|
print "duplicate filename: %s"%fn
|
|
|
|
|
return False
|
|
|
|
|
if w%16 != 0:
|
|
|
|
|
print "width must be divisible by 16"
|
|
|
|
|
return False
|
|
|
|
|
if len(fn) > 9:
|
|
|
|
|
print "filename can't be longer than 9 bytes"
|
|
|
|
|
return False
|
|
|
|
|
fnames.add(fn)
|
|
|
|
|
infiles[bid].append((im,t,p,j,fn,fw,fh,lm,tm))
|
|
|
|
|
|
|
|
|
|
if len(infiles) == 0:
|
|
|
|
|
print "no input files detected"
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# check if j values for all bids are correct and sort them in j order in the process
|
|
|
|
|
for bid in infiles:
|
|
|
|
|
infiles[bid].sort(key=lambda t: t[3])
|
|
|
|
|
for k,(_,_,bid,j,_,_,_,_,_) in enumerate(infiles[bid]):
|
|
|
|
|
for k,(_,_,_,j,_,_,_,_,_) in enumerate(infiles[bid]):
|
|
|
|
|
if k != j:
|
|
|
|
|
print "incorrect j value %d for bid %d should be %d"%(j,bid,k)
|
|
|
|
|
|
|
|
|
|
t,p,fw,fh,pal = cursig
|
|
|
|
|
t,p,pal = cursig
|
|
|
|
|
outf = open(outdir+"/"+p+".def", "w+")
|
|
|
|
|
|
|
|
|
|
# write the header
|
|
|
|
|
outf.write(struct.pack("<IIII", t,fw,fh,len(infiles)))
|
|
|
|
|
# full width and height are not used and not the same for all frames
|
|
|
|
|
# in some defs, so setting to zero
|
|
|
|
|
outf.write(struct.pack("<IIII", t,0,0,len(infiles)))
|
|
|
|
|
# write the palette
|
|
|
|
|
outf.write(struct.pack("768B", *pal))
|
|
|
|
|
|
|
|
|
@ -65,6 +70,7 @@ def makedef(indir, outdir):
|
|
|
|
|
|
|
|
|
|
for bid,l in infiles.items():
|
|
|
|
|
# write bid and number of frames
|
|
|
|
|
# the last two values have unknown meaning
|
|
|
|
|
outf.write(struct.pack("<IIII",bid,len(l),0,0))
|
|
|
|
|
# write filenames
|
|
|
|
|
for _,_,_,_,fn,_,_,_,_ in l:
|
|
|
|
@ -73,17 +79,20 @@ def makedef(indir, outdir):
|
|
|
|
|
for im,_,_,_,_,_,_,_,_ in l:
|
|
|
|
|
outf.write(struct.pack("<I",curoffset))
|
|
|
|
|
w,h = im.size
|
|
|
|
|
print curoffset
|
|
|
|
|
# every image occupies one byte per pixel plus 32 byte header
|
|
|
|
|
curoffset += w*h+32
|
|
|
|
|
|
|
|
|
|
for bid,l in infiles.items():
|
|
|
|
|
for im,_,_,_,_,fw,fh,lm,tm in l:
|
|
|
|
|
for im,_,p,j,_,fw,fh,lm,tm in l:
|
|
|
|
|
w,h = im.size
|
|
|
|
|
outf.write(struct.pack("<IIIIIIii",w*h,0,fw,fh,w,h,lm,tm))
|
|
|
|
|
if shred:
|
|
|
|
|
im = Image.new("P", (w*3,h*3), get_color(p))
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
draw.text((0,0),"%d%s"%(j,p),font=font)
|
|
|
|
|
im = im.resize((w,h),Image.ANTIALIAS)
|
|
|
|
|
buf = ''.join([chr(i) for i in list(im.getdata())])
|
|
|
|
|
outf.write(buf)
|
|
|
|
|
print outf.tell()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|