2014-03-11 16:12:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import zlib
|
|
|
|
import struct
|
|
|
|
import os
|
2014-03-13 09:20:19 +00:00
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
|
|
|
|
from common import crc24_func, get_complement, font
|
2014-03-11 16:12:36 +00:00
|
|
|
|
|
|
|
def is_pcx(data):
|
|
|
|
size,width,height = struct.unpack("<III",data[:12])
|
|
|
|
return size == width*height or size == width*height*3
|
|
|
|
|
|
|
|
def read_pcx(data):
|
|
|
|
size,width,height = struct.unpack("<III",data[:12])
|
|
|
|
if size == width*height:
|
|
|
|
im = Image.fromstring('P', (width,height),data[12:12+width*height])
|
|
|
|
palette = []
|
|
|
|
for i in range(256):
|
|
|
|
offset=12+width*height+i*3
|
|
|
|
r,g,b = struct.unpack("<BBB",data[offset:offset+3])
|
|
|
|
palette.extend((r,g,b))
|
|
|
|
im.putpalette(palette)
|
|
|
|
return im
|
|
|
|
elif size == width*height*3:
|
|
|
|
return Image.fromstring('RGB', (width,height),data[12:])
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2014-03-13 09:20:19 +00:00
|
|
|
def unpack_lod(infile,outdir,shred=True):
|
2014-03-11 16:12:36 +00:00
|
|
|
f = open(infile)
|
|
|
|
|
2014-03-15 14:31:40 +00:00
|
|
|
header = f.read(4)
|
|
|
|
if header != 'LOD\0':
|
|
|
|
print "not LOD file: %s"%header
|
2014-03-11 16:12:36 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
f.seek(8)
|
|
|
|
total, = struct.unpack("<I", f.read(4))
|
|
|
|
f.seek(92)
|
|
|
|
|
|
|
|
files=[]
|
|
|
|
for i in range(total):
|
|
|
|
filename, = struct.unpack("16s", f.read(16))
|
2014-03-15 14:31:40 +00:00
|
|
|
filename = filename[:filename.index('\0')].lower()
|
2014-03-11 16:12:36 +00:00
|
|
|
offset,size,_,csize = struct.unpack("<IIII", f.read(16))
|
|
|
|
files.append((filename,offset,size,csize))
|
|
|
|
|
|
|
|
for filename,offset,size,csize in files:
|
|
|
|
filename=os.path.join(outdir,filename)
|
|
|
|
print filename
|
|
|
|
f.seek(offset)
|
|
|
|
if csize != 0:
|
|
|
|
data = zlib.decompress(f.read(csize))
|
|
|
|
else:
|
|
|
|
data = f.read(size)
|
|
|
|
if is_pcx(data):
|
|
|
|
im = read_pcx(data)
|
|
|
|
if im:
|
2014-03-13 09:20:19 +00:00
|
|
|
if shred:
|
|
|
|
crc = crc24_func(filename)
|
|
|
|
r = crc>>16
|
|
|
|
g = (crc&0xff00)>>8
|
|
|
|
b = crc&0xff
|
|
|
|
w,h = im.size
|
2014-03-15 14:31:40 +00:00
|
|
|
pixels = im.load()
|
|
|
|
for i in range(w):
|
|
|
|
for j in range(h):
|
|
|
|
if pixels[i,j] > 7:
|
|
|
|
if im.mode == 'P':
|
|
|
|
pixels[i,j] = 8+crc%248
|
|
|
|
else:
|
|
|
|
pixels[i,j] = (r,g,b)
|
|
|
|
im.resize((w*3,h*3))
|
2014-03-13 09:20:19 +00:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-03-15 14:31:40 +00:00
|
|
|
tw,th = draw.textsize(os.path.basename(filename),font=font)
|
|
|
|
tpos = ((w*3-tw)/2,(h*3-th)/2)
|
|
|
|
if im.mode == 'P':
|
|
|
|
# we can't really have a complement in palette mode, so just get some color
|
|
|
|
draw.text(tpos,os.path.basename(filename),255,font=font)
|
|
|
|
else:
|
|
|
|
draw.text(tpos,os.path.basename(filename),get_complement(r,g,b),font=font)
|
2014-03-13 09:20:19 +00:00
|
|
|
im = im.resize((w,h),Image.ANTIALIAS)
|
2014-03-11 16:12:36 +00:00
|
|
|
im.save(filename, "PNG")
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
o = open(filename,"w+")
|
|
|
|
o.write(data)
|
|
|
|
o.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
print "usage: %s infile.lod ./outdir"%sys.argv[0]
|
|
|
|
print ""
|
|
|
|
print "usually after installing the normal way:"
|
2014-03-15 14:31:40 +00:00
|
|
|
print " %s .vcmi/Data/H3bitmap.lod .vcmi/Mods/vcmi/Data/"%sys.argv[0]
|
2014-03-11 16:12:36 +00:00
|
|
|
print " rm .vcmi/Data/H3bitmap.lod"
|
2014-03-15 14:31:40 +00:00
|
|
|
print " %s .vcmi/Data/H3sprite.lod .vcmi/Mods/vcmi/Data/"%sys.argv[0]
|
2014-03-11 16:12:36 +00:00
|
|
|
print " rm .vcmi/Data/H3sprite.lod"
|
2014-03-15 14:31:40 +00:00
|
|
|
exit(1)
|
2014-03-11 16:12:36 +00:00
|
|
|
ret = unpack_lod(sys.argv[1], sys.argv[2])
|
|
|
|
exit(0 if ret else 1)
|