Crusader_Decomp/tools/make_crop_no_pil.py
2026-04-12 14:45:08 +02:00

52 lines
1.6 KiB
Python

import struct, zlib, os
IN_BIN = r"K:\ghidra\Crusader_Decomp\binary\Crusader - No Remorse Memdump Weapons.bin"
OUT_PNG = r"K:\ghidra\Crusader_Decomp\binary\crop_weapon_row_nopil.png"
W,H = 1024,512
# box: left,top,right,bot (exclusive right/bot matching PIL convention)
left,top,right,bot = 80,44,360,92
w = right - left
h = bot - top
with open(IN_BIN,'rb') as f:
data = f.read()
count = min(len(data)//2, W*H)
# build rows
rows = []
for y in range(H):
row = bytearray()
for x in range(W):
i = y*W + x
if i < count:
off = i*2
val = data[off] | (data[off+1]<<8)
b = (val & 0x1F) << 3
g = ((val >>5) & 0x1F) << 3
r = ((val >>10) & 0x1F) << 3
else:
r=g=b=0
row.extend([r,g,b])
rows.append(bytes(row))
# compose crop raw (PNG filter 0 per row)
rawrows = []
for y in range(top, top+h):
rawrows.append(b"\x00" + rows[y][left*3:(left+w)*3])
raw = b"".join(rawrows)
comp = zlib.compress(raw, level=9)
def chunk(t,d):
out = struct.pack('>I', len(d)) + t + d
crc = zlib.crc32(t + d) & 0xffffffff
out += struct.pack('>I', crc)
return out
png = b"\x89PNG\r\n\x1a\n"
png += chunk(b'IHDR', struct.pack('>IIBBBBB', w, h, 8, 2, 0, 0, 0))
png += chunk(b'IDAT', comp)
png += chunk(b'IEND', b'')
with open(OUT_PNG, 'wb') as f:
f.write(png)
# compute file offsets
start_idx = top*W + left
end_idx = (top+h-1)*W + (left + w -1)
so = start_idx * 2
eo = ((end_idx)+1)*2 -1
print('wrote', OUT_PNG)
print(f'pixel box=({left},{top})-({left+w-1},{top+h-1}) size={w}x{h}')
print(f'vram byte offsets: 0x{so:06x}-0x{eo:06x} (inclusive)')