Files
rosetta/tools/fixtures/tim.py
T

74 lines
3.4 KiB
Python

# PlayStation TIM fixtures (tools/fixtures.py): TIM files written here (4-bit with two CLUTs, 8-bit, 15-bit direct
# with STP bits and 0x0000 transparency, 24-bit with an odd width, VRAM positions) decoded by ImageMagick's TIM
# reader (nix imagemagick, RGB: it ignores alpha) -> CRC-32 of the RGB bytes per CLUT row, plus the transparent
# pixel count (colour 0x0000) computed here.
# ImageMagick cannot read several CLUT rows: the 2-row file's reference is a copy with row 0 only.
# tests/gen/tim.luau { { name, data, width, height, bpp, rgb = { crc of CLUT row 0 }, transparent, cluts } }
import os, shutil, struct, tempfile, zlib
def tim(rng, bpp, w, h, cluts=1):
flags = {4: 0, 8: 1, 16: 2, 24: 3}[bpp]
out = b""
colours = []
if bpp <= 8:
n = 16 if bpp == 4 else 256
flags |= 8
for _ in range(cluts):
row = [rng.randrange(65536) for _ in range(n)]
row[0] = 0
row[1] = 0x8000
colours.append(row)
out += struct.pack("<IHHHH", 12 + n * 2 * cluts, 16, 480, n, cluts) + b"".join(struct.pack("<H", c) for r in colours for c in r)
if bpp == 4:
iw = (w + 3) // 4
elif bpp == 8:
iw = (w + 1) // 2
elif bpp == 16:
iw = w
else:
iw = (w * 3 + 1) // 2
stride = iw * 2
rows = []
transparent = 0
for y in range(h):
if bpp == 4:
idx = [rng.randrange(16) for _ in range(iw * 4)]
transparent += sum(1 for i in idx[:w] if colours[0][i] == 0)
rows.append(bytes(idx[2 * k] | (idx[2 * k + 1] << 4) for k in range(iw * 2)))
elif bpp == 8:
idx = [rng.randrange(256) for _ in range(iw * 2)]
transparent += sum(1 for i in idx[:w] if colours[0][i] == 0)
rows.append(bytes(idx))
elif bpp == 16:
cs = [rng.choice([0, 0x8000, rng.randrange(65536)]) if rng.random() < 0.2 else rng.randrange(65536) for _ in range(iw)]
transparent += sum(1 for c in cs if c == 0)
rows.append(b"".join(struct.pack("<H", c) for c in cs))
else:
rows.append(bytes(rng.randrange(256) for _ in range(stride)))
image = struct.pack("<IHHHH", 12 + stride * h, 320, 0, iw, h) + b"".join(rows)
data = struct.pack("<II", 0x10, flags) + out + image
# ImageMagick reads one CLUT row only: its reference copy keeps row 0
ref = data
if bpp <= 8 and cluts > 1:
n = 16 if bpp == 4 else 256
ref = struct.pack("<II", 0x10, flags) + struct.pack("<IHHHH", 12 + n * 2, 16, 480, n, 1) + b"".join(struct.pack("<H", c) for c in colours[0]) + image
return data, ref, transparent, (iw * 4 if bpp == 4 else iw * 2 if bpp == 8 else iw if bpp == 16 else (iw * 2) // 3)
def generate(ctx):
rng = ctx.rng(111)
specs = [("4bit", 4, 16, 6, 2), ("8bit", 8, 10, 7, 1), ("16bit", 16, 9, 5, 1), ("24bit", 24, 5, 4, 1), ("4bit-odd", 4, 13, 3, 1)]
out = []
tmp = tempfile.mkdtemp(prefix="formats-tim-")
try:
for name, bpp, w, h, cluts in specs:
data, ref, transparent, width = tim(rng, bpp, w, h, cluts)
p = os.path.join(tmp, name + ".tim")
open(p, "wb").write(ref)
rgb = [zlib.crc32(ctx.nix("imagemagick", ["magick", "TIM:" + p, "rgb:-"]))]
out.append({"name": name, "data": data, "bpp": bpp, "width": width, "height": h, "rgb": rgb, "transparent": transparent, "cluts": cluts})
finally:
shutil.rmtree(tmp)
ctx.emit("tim", out)