mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:18:38 +00:00
Shader pipeline (carve, FSSM unpack, gfx1013 disassembly, RDNA2 to SPIR-V), asset export via rpf9-explorer's Core (submodule), PE loader and shim skeleton, and make_game.py, which builds from the user's own copy of the game. No game data, keys or executables are included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
"""Exact per-block average colour for BC1 (8-byte) and BC3 (16-byte, colour half) blocks, vectorised."""
|
|
import numpy as np
|
|
|
|
|
|
def _565(c):
|
|
r = ((c >> 11) & 31).astype(np.float32) * (255 / 31)
|
|
g = ((c >> 5) & 63).astype(np.float32) * (255 / 63)
|
|
b = (c & 31).astype(np.float32) * (255 / 31)
|
|
return np.stack([r, g, b], -1)
|
|
|
|
|
|
def bc1_average(blocks: np.ndarray) -> np.ndarray:
|
|
"""blocks: (N, 8) uint8 -> (N, 3) float average colour of the 16 decoded pixels."""
|
|
c0 = blocks[:, 0].astype(np.uint32) | (blocks[:, 1].astype(np.uint32) << 8)
|
|
c1 = blocks[:, 2].astype(np.uint32) | (blocks[:, 3].astype(np.uint32) << 8)
|
|
p0, p1 = _565(c0), _565(c1)
|
|
four = (c0 > c1)[:, None]
|
|
p2 = np.where(four, (2 * p0 + p1) / 3, (p0 + p1) / 2)
|
|
p3 = np.where(four, (p0 + 2 * p1) / 3, 0)
|
|
idx = blocks[:, 4:8].astype(np.uint32)
|
|
bits = idx[:, 0] | (idx[:, 1] << 8) | (idx[:, 2] << 16) | (idx[:, 3] << 24)
|
|
counts = np.zeros((len(blocks), 4), np.float32)
|
|
for k in range(16):
|
|
sel = (bits >> (2 * k)) & 3
|
|
counts[np.arange(len(blocks)), sel] += 1
|
|
pal = np.stack([p0, p1, p2, p3], 1) # (N, 4, 3)
|
|
return (pal * counts[:, :, None]).sum(1) / 16
|
|
|
|
|
|
def block_average(blocks: np.ndarray, bpe: int) -> np.ndarray:
|
|
return bc1_average(blocks if bpe == 8 else blocks[:, 8:16])
|
|
|
|
|
|
def smoothness(img: np.ndarray) -> float:
|
|
"""Neighbour difference relative to the image's own spread (lower = more coherent)."""
|
|
d = np.abs(np.diff(img, axis=0)).mean() + np.abs(np.diff(img, axis=1)).mean()
|
|
return float(d / (img.std() + 1e-6))
|