diff --git a/defextract.py b/defextract.py index d3b20f2..f477788 100644 --- a/defextract.py +++ b/defextract.py @@ -7,6 +7,7 @@ import struct from PIL import Image, ImageDraw from collections import defaultdict import os +import json from common import crc24_func, font, sanitize_filename def get_color(fname): @@ -31,7 +32,6 @@ def extract_def(infile,outdir,shred=True): palette.extend((r,g,b)) offsets = defaultdict(list) - k = 0 # for naming bogus filename entries for i in range(blocks): # bid - block id # entries - number of images in this block @@ -41,23 +41,18 @@ def extract_def(infile,outdir,shred=True): # a list of 13 character long filenames for j in range(entries): name, = struct.unpack("13s", f.read(13)) - name = sanitize_filename(name) - # if nothing remains, create bogus name - if len(name) == 0: - num = "%02d"%k - if len(bn)+len(num) > 9: # truncate name - name = bn[:9-len(num)]+num - else: - name = bn+num - k+=1 - names.append(name) # a list of offsets - for n in names: + for j in range(entries): offs, = struct.unpack(">5)+1)<<5 - rm = lm+w - im = im.crop((lm,tm,rm,bm)) - if im.mode != 'P': - print "input images must have a palette" - return False - cursig =(t,p,fw,fh,im.getpalette(),fmt) - if not sig: - sig = cursig - else: - if sig != cursig: - print "sigs must match - got:" - print sig - print cursig + for seq in in_json["sequences"]: + bid = seq["group"] + for f in seq["frames"]: + im = Image.open(os.path.join(d,f)) + fw,fh = im.size + if fmt == 2 and (fw != 32 or fh != 32): + print "format 2 must have width and height 32" + return False + lm,tm,rm,bm = im.getbbox() or (0,0,0,0) + # format 3 has to have width and lm divisible by 32 + if fmt == 3 and lm%32 != 0: + # shrink lm to the previous multiple of 32 + lm = (lm/32)*32 + w,h = rm-lm,bm-tm + if fmt == 3 and w%32 != 0: + # grow rm to the next multiple of 32 + w = (((w-1)>>5)+1)<<5 + rm = lm+w + im = im.crop((lm,tm,rm,bm)) + if im.mode != 'P': + print "input images must have a palette" return False - if len(fn) > 9: - print "filename can't be longer than 9 bytes" - return False - data = fmtencoders[fmt](im) - infiles[bid].append((im,t,p,j,fn,lm,tm,fmt,data)) + cursig =(fw,fh,im.getpalette()) + if not sig: + sig = cursig + else: + if sig != cursig: + print "sigs must match - got:" + print sig + print cursig + return False + data,size = fmtencoders[fmt](im) + infiles[bid].append((w,h,lm,tm,data,size)) 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,(_,_,_,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,fmt = cursig + fw,fh,pal = cursig outname = os.path.join(outdir,p)+".def" print "writing to %s"%outname outf = open(outname, "w+") @@ -209,41 +217,27 @@ def makedef(indir, outdir): # the last two values have unknown meaning outf.write(struct.pack("