KAZIK/tools/generators/pack_cinematic_master.py

70 lines
3.4 KiB
Python

"""Compile AE frames into independently seekable LZNT1 frames for native MASM."""
import argparse
import ctypes as C
import hashlib
import json
from pathlib import Path
import struct
from PIL import Image
import numpy as np
ROOT = Path(__file__).resolve().parents[2]
RAW_BYTES = 640*480*2
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--frames',type=Path,default=ROOT/'build/cinematic-v3/render')
parser.add_argument('--fps',type=int,default=30)
args = parser.parse_args()
files = sorted(args.frames.glob('film_*.png'))
assert len(files) == 600,(len(files),'Expected 20 seconds at 30 fps')
api = C.WinDLL('ntdll')
api.RtlGetCompressionWorkSpaceSize.argtypes=[C.c_ushort,C.POINTER(C.c_ulong),C.POINTER(C.c_ulong)]
api.RtlGetCompressionWorkSpaceSize.restype=C.c_long
api.RtlCompressBuffer.argtypes=[C.c_ushort,C.c_void_p,C.c_ulong,C.c_void_p,C.c_ulong,C.c_ulong,C.POINTER(C.c_ulong),C.c_void_p]
api.RtlCompressBuffer.restype=C.c_long
api.RtlDecompressBuffer.argtypes=[C.c_ushort,C.c_void_p,C.c_ulong,C.c_void_p,C.c_ulong,C.POINTER(C.c_ulong)]
api.RtlDecompressBuffer.restype=C.c_long
workspace_size=C.c_ulong(); fragment_size=C.c_ulong()
assert api.RtlGetCompressionWorkSpaceSize(2,C.byref(workspace_size),C.byref(fragment_size))==0
workspace=C.create_string_buffer(workspace_size.value)
target=C.create_string_buffer(RAW_BYTES+4096)
decoded=C.create_string_buffer(RAW_BYTES)
size=C.c_ulong();final=C.c_ulong()
output=ROOT/'assets/animations/intro/cinematic-master-v3.cfm'
offset=32+len(files)*8
entries=[]
header=struct.pack('<4s7I',b'CFM3',3,args.fps,len(files),640,480,RAW_BYTES,RAW_BYTES+4096)
temporary=output.with_suffix('.cfm.tmp')
with temporary.open('wb') as stream:
stream.write(header+b'\0'*(len(files)*8))
for index,path in enumerate(files):
im=Image.open(path).convert('RGBA')
assert im.size == (640,480),(path,im.size)
rgb=np.asarray(im.convert('RGB'),dtype=np.uint16)
pixels=((rgb[:,:,0]>>3)<<11)|((rgb[:,:,1]>>2)<<5)|(rgb[:,:,2]>>3)
raw=pixels.astype('<u2').tobytes()
source=C.create_string_buffer(raw)
status=api.RtlCompressBuffer(2,source,len(raw),target,len(target),4096,C.byref(size),workspace)
assert status>=0,(index,status)
chunk=target.raw[:size.value]
assert api.RtlDecompressBuffer(2,decoded,len(decoded),target,size.value,C.byref(final))==0
assert final.value==len(raw) and decoded.raw==raw,('Roundtrip failed',index)
entries.append((offset,len(chunk)));offset+=len(chunk)
assert offset < 256*1024*1024,'Master exceeds bounded native loader limit'
stream.write(chunk)
stream.seek(32)
stream.write(b''.join(struct.pack('<2I',*e) for e in entries))
temporary.replace(output)
with output.open('rb') as stream:
digest=hashlib.file_digest(stream,'sha256').hexdigest()
manifest=dict(format='CFM3 / RGB565 + independent LZNT1 frames',frames=len(files),fps=args.fps,
size=[640,480],bytes=offset,sha256=digest,
source='assets/cinematics/source/AssemblerCasino-v3.aep',roundtrip='all frames verified')
output.with_suffix('.json').write_bytes((json.dumps(manifest,indent=2)+'\n').encode())
print(f'Packed {len(files)} verified frames: {offset/1024/1024:.1f} MiB')
if __name__=='__main__':
main()