ws-voxel: model/qb (Qubicle Binary read + write: RGBA / BGRA, z orientation, RLE, visibility masks; exact colours in Voxels.colors)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4cbde068d2
commit
87c8476302
6 files changed
+734
No files matched your search
@@ -0,0 +1,34 @@
|
||||
-- tools/fixtures/qb.py helper: our model/qb encoder's output for the reference reader there. Prints "<name>\t<hex>".
|
||||
local Qb = require("@formats/model/qb")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/qb")
|
||||
|
||||
local function hex(b: buffer): string
|
||||
local t = table.create(buffer.len(b))
|
||||
for i = 0, buffer.len(b) - 1 do
|
||||
t[i + 1] = string.format("%02x", buffer.readu8(b, i))
|
||||
end
|
||||
return table.concat(t)
|
||||
end
|
||||
local function emit(name: string, b: buffer?, e: any)
|
||||
if not b then
|
||||
error(name .. ": " .. tostring(e))
|
||||
end
|
||||
print(name .. "\t" .. hex(b))
|
||||
end
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local s = assert(Qb.decode(Bytes.reader(buffer.fromstring(c.data))))
|
||||
emit(c.name .. "_same", Qb.encode(s))
|
||||
emit(c.name .. "_flip", Qb.encode(s, { rightHanded = not c.rightHanded, compressed = not c.compressed, colorFormat = if c.colorFormat == "rgba" then "bgra" else "rgba" }))
|
||||
end
|
||||
-- a palette grid (no true colours) with a visibility mask computed by the encoder
|
||||
local v = Types.Voxels.new(3, 3, 3)
|
||||
v.palette[1] = { 200, 10, 10, 255 }
|
||||
v.palette[2] = { 10, 200, 10, 255 }
|
||||
for i = 0, 26 do
|
||||
buffer.writeu8(v.data, i, if i == 13 then 2 elseif i % 2 == 0 then 1 else 0)
|
||||
end
|
||||
emit("palette_mask", Qb.encode(v, { visibilityMask = true }))
|
||||
emit("palette_plain", Qb.encode(v))
|
||||
@@ -0,0 +1,156 @@
|
||||
# model/qb fixtures: Qubicle Binary files from a reader / writer written here from the format description in the
|
||||
# Qubicle documentation (no Qubicle tool runs under nix), with every option: RGBA / BGRA, left- / right-handed,
|
||||
# uncompressed / RLE, visibility masks; plus our encoder's output (tools/fixtures/_qb_encode.luau) read back by it.
|
||||
# tests/gen/qb.luau { cases = { { name, data, colorFormat, rightHanded, compressed, visibilityMask,
|
||||
# matrices = { { name, size, pos (scene coordinates: left-handed files mirrored on z), crc
|
||||
# (CRC-32 of the r, g, b, a bytes of every cell in Voxels order), filled } } } },
|
||||
# ours = { [name] = { same fields as cases } } }
|
||||
import importlib.util, os, pickle, struct, subprocess, zlib, random
|
||||
|
||||
INPUTS = ["src/Formats/model/qb", "tools/fixtures/_qb_encode.luau"]
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_model_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_model_ref.py"))
|
||||
R = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(R)
|
||||
|
||||
CODEFLAG, NEXTSLICEFLAG = 2, 6
|
||||
|
||||
|
||||
def write_qb(matrices, bgra, right, compressed, mask):
|
||||
"""matrices: [(name, (sx, sy, sz), (px, py, pz), {(x, y, z): (r, g, b, a)})] in file coordinates"""
|
||||
out = bytearray(b"\x01\x01\x00\x00")
|
||||
out += struct.pack("<IIIII", 1 if bgra else 0, 1 if right else 0, 1 if compressed else 0, 1 if mask else 0, len(matrices))
|
||||
for name, (sx, sy, sz), (px, py, pz), cells in matrices:
|
||||
nb = name.encode()
|
||||
out += bytes([len(nb)]) + nb + struct.pack("<IIIiii", sx, sy, sz, px, py, pz)
|
||||
|
||||
def val(x, y, z):
|
||||
c = cells.get((x, y, z))
|
||||
if not c:
|
||||
return 0
|
||||
r, g, b, a = c
|
||||
if bgra:
|
||||
r, b = b, r
|
||||
return r | g << 8 | b << 16 | a << 24
|
||||
for z in range(sz):
|
||||
vals = [val(x, y, z) for y in range(sy) for x in range(sx)]
|
||||
if not compressed:
|
||||
out += b"".join(struct.pack("<I", v) for v in vals)
|
||||
continue
|
||||
i = 0
|
||||
while i < len(vals):
|
||||
run = 1
|
||||
while i + run < len(vals) and vals[i + run] == vals[i]:
|
||||
run += 1
|
||||
if run > 2:
|
||||
out += struct.pack("<III", CODEFLAG, run, vals[i])
|
||||
else:
|
||||
out += b"".join(struct.pack("<I", vals[i]) for _ in range(run))
|
||||
i += run
|
||||
out += struct.pack("<I", NEXTSLICEFLAG)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def read_qb(data):
|
||||
"""-> settings, [(name, size, pos, {(x, y, z): (r, g, b, a)})] in file coordinates"""
|
||||
assert data[:4] == b"\x01\x01\x00\x00"
|
||||
fmt, zaxis, comp, mask, n = struct.unpack_from("<IIIII", data, 4)
|
||||
p = 24
|
||||
mats = []
|
||||
for _ in range(n):
|
||||
ln = data[p]; p += 1
|
||||
name = data[p:p + ln].decode(); p += ln
|
||||
sx, sy, sz, px, py, pz = struct.unpack_from("<IIIiii", data, p); p += 24
|
||||
cells = {}
|
||||
|
||||
def put(x, y, z, v):
|
||||
if v >> 24 == 0:
|
||||
return
|
||||
r, g, b, a = v & 255, v >> 8 & 255, v >> 16 & 255, v >> 24
|
||||
if fmt == 1:
|
||||
r, b = b, r
|
||||
cells[(x, y, z)] = (r, g, b, a)
|
||||
for z in range(sz):
|
||||
if not comp:
|
||||
for y in range(sy):
|
||||
for x in range(sx):
|
||||
put(x, y, z, struct.unpack_from("<I", data, p)[0]); p += 4
|
||||
continue
|
||||
i = 0
|
||||
while True:
|
||||
d = struct.unpack_from("<I", data, p)[0]; p += 4
|
||||
if d == NEXTSLICEFLAG:
|
||||
break
|
||||
if d == CODEFLAG:
|
||||
run, v = struct.unpack_from("<II", data, p); p += 8
|
||||
for _ in range(run):
|
||||
put(i % sx, i // sx, z, v); i += 1
|
||||
else:
|
||||
put(i % sx, i // sx, z, d); i += 1
|
||||
mats.append((name, (sx, sy, sz), (px, py, pz), cells))
|
||||
return {"colorFormat": "bgra" if fmt else "rgba", "rightHanded": zaxis == 1, "compressed": comp == 1, "visibilityMask": mask == 1}, mats
|
||||
|
||||
|
||||
def describe(settings, mats):
|
||||
out = []
|
||||
for name, (sx, sy, sz), (px, py, pz), cells in mats:
|
||||
right = settings["rightHanded"]
|
||||
buf = bytearray()
|
||||
for z in range(sz):
|
||||
for y in range(sy):
|
||||
for x in range(sx):
|
||||
fz = z if right else sz - 1 - z
|
||||
buf += bytes(cells.get((x, y, fz), (0, 0, 0, 0)))
|
||||
out.append({"name": name, "size": [sx, sy, sz], "pos": [px, py, pz if right else -(pz + sz)],
|
||||
"crc": zlib.crc32(bytes(buf)), "filled": len(cells)})
|
||||
d = dict(settings)
|
||||
d["matrices"] = out
|
||||
return d
|
||||
|
||||
|
||||
def mask_of(cells, x, y, z):
|
||||
a = 1
|
||||
for (dx, dy, dz), bit in (((-1, 0, 0), 2), ((1, 0, 0), 4), ((0, 1, 0), 8), ((0, -1, 0), 16), ((0, 0, 1), 32), ((0, 0, -1), 64)):
|
||||
if (x + dx, y + dy, z + dz) not in cells:
|
||||
a += bit
|
||||
return a
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = random.Random(4)
|
||||
cases = []
|
||||
for k, (bgra, right, comp, mask) in enumerate([(False, True, True, False), (True, False, False, False), (False, False, True, True),
|
||||
(True, True, False, True), (False, True, False, False), (False, True, True, False)]):
|
||||
mats = []
|
||||
for m in range(1 + k % 3):
|
||||
size = (rng.randrange(1, 9), rng.randrange(1, 7), rng.randrange(1, 6))
|
||||
many = k == 5 # more than 255 colours
|
||||
pal = [(rng.randrange(256), rng.randrange(256), rng.randrange(256)) for _ in range(400 if many else 6)]
|
||||
cells = {}
|
||||
for x in range(size[0]):
|
||||
for y in range(size[1]):
|
||||
for z in range(size[2]):
|
||||
if rng.random() < 0.6:
|
||||
cells[(x, y, z)] = pal[rng.randrange(len(pal))] + (255,)
|
||||
if k == 5:
|
||||
size = (20, 20, 3)
|
||||
cells = {(x, y, z): pal[(x + 20 * y + z) % 400] + (255,) for x in range(20) for y in range(20) for z in range(3)}
|
||||
if mask:
|
||||
cells = {p: c[:3] + (mask_of(cells, *p),) for p, c in cells.items()}
|
||||
mats.append(("m%d_%d" % (k, m), size, (rng.randrange(-20, 20), rng.randrange(-5, 5), rng.randrange(-20, 20)), cells))
|
||||
data = write_qb(mats, bgra, right, comp, mask)
|
||||
settings, back = read_qb(data)
|
||||
d = describe(settings, back)
|
||||
d["name"] = "case%d" % k
|
||||
d["data"] = data
|
||||
cases.append(d)
|
||||
ctx.emit("qb", {"cases": cases})
|
||||
try:
|
||||
files = R.luau_encode(ctx, "tools/fixtures/_qb_encode.luau")
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise RuntimeError("our encoder failed:\n" + (e.stderr or b"").decode(errors="replace")[-3000:])
|
||||
ours = {}
|
||||
for name, data in files.items():
|
||||
settings, mats = read_qb(data)
|
||||
ours[name] = describe(settings, mats)
|
||||
ctx.emit("qb", {"cases": cases, "ours": ours})
|
||||
Reference in new issue
Block a user