Last active
May 18, 2022 02:51
-
-
Save lucaspcamargo/23f2632c08de4c5a6794bb1fd7febc9b to your computer and use it in GitHub Desktop.
Dweep Gold data file dumper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dweep Gold data file dumper | |
# run this from program folder. it'll look for './Dweep.DAT' | |
# and dump what it can in ./out/ | |
import os, sys | |
import struct | |
import PIL | |
from PIL import Image | |
destination = "./out/" | |
os.makedirs(destination, exist_ok=True) | |
framedata_reg = {} | |
with open("Dweep.DAT", 'rb') as f: | |
filecount = struct.unpack('i', f.read(4))[0] | |
positions = [] | |
for i in range(filecount): | |
item = f.read(17) | |
sz = struct.unpack('i', item[:4])[0] | |
name = item[4:].decode('ascii').rstrip(' \t\r\n\0') | |
positions.append((name, sz, )) | |
for i, pos in enumerate(positions[:-1]): | |
currname = pos[0] | |
currpos = pos[1] | |
nextpos = positions[i+1][1] | |
sz = nextpos - pos[1] | |
print(f'#{i}: {currname} at {currpos}: {sz} bytes long') | |
f.seek(currpos) | |
contents = f.read(sz) | |
destfname = destination+currname | |
with open(destfname, 'wb') as fout: | |
fout.write(contents) | |
# HACK: initial attempt at decoding the bitmaps | |
if destfname.endswith(".DAT"): | |
# must be a simple image definition | |
num_strips = struct.unpack('i', contents[:4])[0] | |
strip_base = 4 | |
strips = [] | |
for istrip in range(num_strips): | |
print("\tStrip {istrip}") | |
numframes = struct.unpack('i', contents[strip_base:strip_base+4])[0] | |
strip_frames = [] | |
framesz = 5*4 | |
for fi in range(numframes): | |
print("\tFrame {fi}") | |
data_start, w, h, cx, cy = struct.unpack('iiiii', contents[ | |
strip_base+4+fi*framesz: | |
strip_base+4+(fi+1)*framesz]) | |
framedata = { | |
'data_start': data_start, | |
'w': w, | |
'h': h, | |
'cx': cx, | |
'cy': cy | |
} | |
print("\t", fi, framedata) | |
strip_frames.append(framedata) | |
strips.append(strip_frames) | |
strip_base = strip_base+4+numframes*framesz | |
framedata_reg[currname.replace(".DAT", "")] = strips | |
elif destfname.endswith('.RIP'): | |
basename = currname.replace('.RIP', '') | |
if basename in framedata_reg: | |
frames = {} | |
for istrip, strip in enumerate(framedata_reg[basename]): | |
for iframe, frame in enumerate(strip): | |
frames[f'_{istrip}_{iframe}'] = frame | |
for suffix, framedata in frames.items(): | |
print(framedata) | |
imgw = framedata['w'] | |
imgh = framedata['h'] | |
imgsz = imgw*imgh*2 | |
datastart = framedata['data_start'] | |
imgcontents = contents[datastart:datastart+imgsz] | |
#with open(f'{destination}{basename}{suffix}.RAW', 'wb') as imgfile: | |
# imgfile.write(imgcontents) | |
# build rgb | |
rgb = b'' | |
for pi in range(imgw*imgh): # TODO can we identify palleted? maybe hardcode | |
paddr = datastart+2*pi | |
lower = contents[paddr] if len(contents) > paddr else 0 | |
higher = contents[paddr+1] if len(contents)-1 > paddr else 0 | |
val = lower + higher*256 | |
b = int(((val >> 0) & 0x1f)*255/31) | |
g = int(((val >> 5) & 0x3f)*255/63) | |
r = int(((val >> 11) & 0x1f)*255/31) | |
rgb += struct.pack('ccc', r.to_bytes(1, 'little'), g.to_bytes(1, 'little'), b.to_bytes(1, 'little')) | |
proper = Image.frombytes('RGB', (imgw, imgh,), rgb) | |
proper.save(f'{destination}{basename}{suffix}.png') | |
else: | |
print("Framedata not found, is dat really before? Did it parse?") | |
#sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment