ws-game: image/vtf (Valve VTF 7.0-7.5 read: every common texel format incl. DXT / ATIxN via codecs/image/Bcn, halves / floats, frames, mips / faces / slices; checked against srctools)
This commit is contained in:
1 parent
7a86e357ed
commit
3d01293b82
6 files changed
+540
No files matched your search
@@ -0,0 +1,100 @@
|
||||
# Valve VTF fixtures (tools/fixtures.py). srctools (nix python3Packages.srctools: a VTF reader / writer with
|
||||
# libsquish for DXT) writes VTFs of random pixels in every texel format it supports, in versions 7.2 / 7.4 / 7.5,
|
||||
# with mips and several frames; its reader decodes them back -> the RGBA of frame 0, mip 0 (and the last frame) as
|
||||
# reference. Formats whose conversion differs from the DXT rule (srctools / libsquish round 5 / 6-bit endpoints
|
||||
# differently) are compared within a tolerance.
|
||||
# tests/gen/vtf.luau { { name, data, width, height, frames, version, fmt, rgba, last?, tol } }
|
||||
import json
|
||||
|
||||
CODE = r"""
|
||||
import io, json, random, sys
|
||||
import srctools.vtf as V
|
||||
args = json.loads(sys.stdin.read())
|
||||
rng = random.Random(args["seed"])
|
||||
out = []
|
||||
FMTS = ["RGBA8888", "ABGR8888", "RGB888", "BGR888", "RGB565", "I8", "IA88", "A8", "RGB888_BLUESCREEN", "BGR888_BLUESCREEN",
|
||||
"ARGB8888", "BGRA8888", "DXT1", "DXT3", "DXT5", "BGRX8888", "BGR565", "BGRX5551", "BGRA4444", "DXT1_ONEBITALPHA",
|
||||
"BGRA5551", "UV88", "UVWQ8888", "RGBA16161616F", "RGBA16161616", "UVLX8888", "ATI1N", "ATI2N"]
|
||||
for i, name in enumerate(FMTS):
|
||||
fmt = V.ImageFormats[name]
|
||||
version = [(7, 2), (7, 4), (7, 5)][i % 3]
|
||||
w, h = [(16, 8), (8, 8), (4, 16)][i % 3] if not name.startswith(("DXT", "ATI")) else (16, 8)
|
||||
frames = 2 if i % 4 == 0 else 1
|
||||
try:
|
||||
vtf = V.VTF(w, h, version, fmt=fmt, frames=frames)
|
||||
for f in range(frames):
|
||||
px = bytes(rng.randrange(256) for _ in range(w * h * 4))
|
||||
if "BLUESCREEN" in name:
|
||||
px = bytearray(px)
|
||||
for k in range(0, len(px), 16):
|
||||
px[k:k + 4] = b"\x00\x00\xff\xff"
|
||||
px = bytes(px)
|
||||
vtf.get(frame=f).copy_from(px)
|
||||
b = io.BytesIO()
|
||||
vtf.save(b)
|
||||
data = b.getvalue()
|
||||
back = V.VTF.read(io.BytesIO(data))
|
||||
back.load()
|
||||
rgba = bytes(back.get(frame=0)._data)
|
||||
e = {"name": name, "data": data.hex(), "width": w, "height": h, "frames": frames, "version": "%d.%d" % version, "rgba": rgba.hex()}
|
||||
if frames > 1:
|
||||
e["last"] = bytes(back.get(frame=frames - 1)._data).hex()
|
||||
out.append(e)
|
||||
except Exception as ex:
|
||||
out.append({"name": name, "failed": repr(ex)})
|
||||
print(json.dumps(out))
|
||||
"""
|
||||
|
||||
TOL = {"DXT1": 8, "DXT3": 8, "DXT5": 8, "DXT1_ONEBITALPHA": 8, "ATI1N": 2, "ATI2N": 2, "RGBA16161616F": 1, "RGBA16161616": 1}
|
||||
|
||||
|
||||
def own(rng):
|
||||
# 7.2 files written here for the formats srctools cannot write (one mip, no thumbnail); expected RGBA by the
|
||||
# documented rules: halves / floats clamped to 0..1 and rounded, 16-bit integers (v + 128) // 257
|
||||
import struct, math
|
||||
out = []
|
||||
w, h = 4, 4
|
||||
def header(fmt):
|
||||
hd = bytearray(80)
|
||||
struct.pack_into("<4sIIIHHIHH", hd, 0, b"VTF\0", 7, 2, 80, w, h, 0, 1, 0)
|
||||
struct.pack_into("<f", hd, 48, 1.0)
|
||||
struct.pack_into("<iBiBBH", hd, 52, fmt, 1, -1, 0, 0, 1)
|
||||
return bytes(hd)
|
||||
def c8(f):
|
||||
return 0 if f != f or f <= 0 else 255 if f >= 1 else math.floor(f * 255 + 0.5)
|
||||
for name, fmt, kind, ch in (("RGBA16161616F", 24, "e", 4), ("RGBA16161616", 25, "H", 4), ("R32F", 27, "f", 1),
|
||||
("RGB323232F", 28, "f", 3), ("RGBA32323232F", 29, "f", 4)):
|
||||
vals, rgba = [], bytearray()
|
||||
for i in range(w * h):
|
||||
px = [0, 0, 0, 255]
|
||||
for k in range(ch):
|
||||
if kind == "H":
|
||||
v = rng.randrange(65536)
|
||||
px[k] = (v + 128) // 257
|
||||
else:
|
||||
v = rng.uniform(-0.5, 1.5)
|
||||
v = struct.unpack("<" + kind, struct.pack("<" + kind, v))[0]
|
||||
px[k] = c8(v)
|
||||
vals.append(v)
|
||||
if ch == 1:
|
||||
px[1] = px[2] = px[0]
|
||||
rgba += bytes(px)
|
||||
data = header(fmt) + struct.pack("<%d%s" % (len(vals), kind), *vals)
|
||||
out.append({"name": name, "data": data, "width": w, "height": h, "frames": 1, "version": "7.2", "rgba": bytes(rgba), "tol": 0, "mips": 1})
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
res = json.loads(ctx.python(["srctools", "pillow"], CODE, input=json.dumps({"seed": 7}).encode()))
|
||||
out = []
|
||||
for e in res:
|
||||
if "failed" in e:
|
||||
print("fixtures: vtf: srctools cannot write %s: %s" % (e["name"], e["failed"]))
|
||||
continue
|
||||
c = {"name": e["name"], "data": bytes.fromhex(e["data"]), "width": e["width"], "height": e["height"], "frames": e["frames"],
|
||||
"version": e["version"], "rgba": bytes.fromhex(e["rgba"]), "tol": TOL.get(e["name"], 0)}
|
||||
if "last" in e:
|
||||
c["last"] = bytes.fromhex(e["last"])
|
||||
out.append(c)
|
||||
out += own(ctx.rng(9))
|
||||
ctx.emit("vtf", out)
|
||||
Reference in new issue
Block a user