26 lines
1.3 KiB
Python
26 lines
1.3 KiB
Python
"""Write BLP2 DXT1 textures with a full mip chain (same layout as native 70334 loading screens)."""
|
|
import struct, subprocess, tempfile, os
|
|
from PIL import Image
|
|
def dxt1_levels(img):
|
|
w, h = img.size; levels = []
|
|
with tempfile.TemporaryDirectory() as t:
|
|
while True:
|
|
p = os.path.join(t, 'l.png'); d = os.path.join(t, 'l.dds')
|
|
img.resize((w, h), Image.LANCZOS).convert('RGB').save(p)
|
|
subprocess.run(['magick', p, '-define', 'dds:compression=dxt1', '-define', 'dds:mipmaps=0', d], check=True)
|
|
raw = open(d, 'rb').read()[128:]
|
|
want = max(1, (w+3)//4)*max(1, (h+3)//4)*8
|
|
assert len(raw) == want, (len(raw), want)
|
|
levels.append(raw)
|
|
if w == 1 and h == 1 or len(levels) == 16: break
|
|
w, h = max(1, w//2), max(1, h//2)
|
|
return levels
|
|
def write_blp(img, path):
|
|
levels = dxt1_levels(img)
|
|
offs, sizes, at = [], [], 1172
|
|
for l in levels: offs.append(at); sizes.append(len(l)); at += len(l)
|
|
offs += [0]*(16-len(offs)); sizes += [0]*(16-len(sizes))
|
|
hdr = b'BLP2' + struct.pack('<I', 1) + bytes([2, 0, 0, 1]) + struct.pack('<II', *img.size) + struct.pack('<16I', *offs) + struct.pack('<16I', *sizes) + bytes(1024)
|
|
assert len(hdr) == 1172
|
|
open(path, 'wb').write(hdr + b''.join(levels))
|