Skip to content

Commit

Permalink
-Change: extractwsa.py: correctly handle "multipart" WSA files
Browse files Browse the repository at this point in the history
  • Loading branch information
miniupnp committed Apr 19, 2018
1 parent f4264ae commit e1356f7
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tools/extractwsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
from westwood_codecs import *
from save_pictures import *

palette = None

def load_palette(filename):
global palette
with open(filename, 'rb') as pal_file:
palette = ''.join(map(lambda c: chr((ord(c) << 2) + (ord(c) >> 6)), list(pal_file.read())))
return ''.join(map(lambda c: chr((ord(c) << 2) + (ord(c) >> 6)), list(pal_file.read())))

def xor_strings(s1, s2):
dest = ''
for i in range(min(len(s1),len(s2))):
dest += chr(ord(s1[i]) ^ ord(s2[i]))
return dest

def extract_wsa(filename):
# frame : set to last frame of previous WSA for "continuation" WSA
def extract_wsa(filename, palette = None, frame = None):
with open(filename, 'rb') as wsa_file:
wsa = wsa_file.read()
(frames, width, height, req_buffer_size, has_palette) = unpack_from('<HHHHH', wsa)
Expand All @@ -38,22 +36,36 @@ def extract_wsa(filename):
print ' ', map(lambda e : len(e), datablocks)
pictures = map(decode_format40, datablocks)
print ' ', map(lambda e : len(e), pictures)
pixels = chr(0) * (width * height)
frames = []
for i in range(len(pictures)):
if len(pictures[0]) > 0:
pixels = pictures[0]
frames.append(pixels)
else:
if frame is None:
pixels = chr(0) * (width * height)
print 'no first frame provided'
else:
# continue from last frame of previous WSA
pixels = frame
if len(pixels) != (width * height):
print 'frame size mismatch : ', len(pixels), width * height
for i in range(1, len(pictures)):
pbm_filename = '%s_%02d.pbm' % (filename, i)
if len(pictures[i]) > 0:
pixels = xor_strings(pixels, pictures[i])
#save_pbm(pbm_filename, width, height, pixels, palette)
frames.append(pixels)
save_flc(filename + '.flc', width, height, frames, palette)
return frames[-1]

if len(sys.argv) <= 1:
print "usage : %s [-p palette.PAL] file.WSA" % sys.argv[0]
else:
args = sys.argv[1:]
frame = None
palette = None
if args[0] == '-p':
load_palette(args[1])
palette = load_palette(args[1])
args = args[2:]
for filename in args:
extract_wsa(filename)
frame = extract_wsa(filename, palette, frame)

0 comments on commit e1356f7

Please sign in to comment.