image/art: Build engine ART tiles (frames per tile, picanm, PALETTE.DAT / LOOKUP.DAT through opts or resolve, shade / palookup remaps), read + write; tested against arttool from the eduke32 source
This commit is contained in:
1 parent
01c044805f
commit
75984be212
7 files changed
+857
-1
No files matched your search
@@ -0,0 +1,106 @@
|
||||
# Build engine reference tools for the art / buildmap fixtures: arttool (Jonathon Fowler, Artistic License 2.0) and
|
||||
# map2stl (Ken Silverman) compiled from the nix eduke32 source (nixpkgs#eduke32.src, never committed) with g++ and a
|
||||
# small compat.h stub written here: stdio and a few B* aliases, a NULL-safe Bfclose, and a DO_FREE_AND_NULL that leaks
|
||||
# (arttool's Bstring copy constructor frees an uninitialised pointer). Binaries are cached in tests/gen/.buildtools/.
|
||||
import os, subprocess
|
||||
|
||||
STUB = r"""
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#define Bassert assert
|
||||
#define Bfclose(f) ((f) ? fclose(f) : 0)
|
||||
#define Bfileno fileno
|
||||
#define Bfopen fopen
|
||||
#define Bfprintf fprintf
|
||||
#define Bfputc fputc
|
||||
#define Bfread fread
|
||||
#define Bfseek fseek
|
||||
#define Bfstat fstat
|
||||
#define Bftell ftell
|
||||
#define Bfwrite fwrite
|
||||
#define Bmalloc malloc
|
||||
#define Bprintf printf
|
||||
#define Brealloc realloc
|
||||
#define Brewind rewind
|
||||
#define Bstat stat
|
||||
#define Bstrcat strcat
|
||||
#define Bstrcmp strcmp
|
||||
#define Bstrdup strdup
|
||||
#define Bstrlen strlen
|
||||
#define BFILE FILE
|
||||
#define DO_FREE_AND_NULL(x) do { (x) = NULL; } while (0)
|
||||
#define EDUKE32_PREDICT_FALSE(x) (x)
|
||||
#define ATTRIBUTE(x)
|
||||
"""
|
||||
|
||||
|
||||
def tools(ctx):
|
||||
"""{"arttool": path, "map2stl": path}"""
|
||||
out = os.path.join(ctx.gen, ".buildtools")
|
||||
paths = {n: os.path.join(out, n) for n in ("arttool", "map2stl")}
|
||||
if all(os.path.exists(p) for p in paths.values()):
|
||||
return paths
|
||||
os.makedirs(out, exist_ok=True)
|
||||
src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#eduke32.src"], capture_output=True,
|
||||
check=True).stdout.decode().strip().split("\n")[-1]
|
||||
stub = os.path.join(out, "stub")
|
||||
os.makedirs(stub, exist_ok=True)
|
||||
with open(os.path.join(stub, "compat.h"), "w") as f:
|
||||
f.write(STUB)
|
||||
for name, path in paths.items():
|
||||
ctx.nix("gcc", ["g++", "-O1", "-w", "-funsigned-char", "-I" + stub, os.path.join(src, "source/tools/src/%s.cpp" % name),
|
||||
"-o", path])
|
||||
return paths
|
||||
|
||||
|
||||
def pcx_write(w, h, idx, pal):
|
||||
"""8-bit RLE PCX (version 5) of row-major indices with a 256 x RGB palette, as arttool reads it"""
|
||||
bpl = w + (w & 1)
|
||||
hd = bytearray(128)
|
||||
hd[0:4] = bytes([10, 5, 1, 8])
|
||||
hd[8:10] = (w - 1).to_bytes(2, "little")
|
||||
hd[10:12] = (h - 1).to_bytes(2, "little")
|
||||
hd[65] = 1
|
||||
hd[66:68] = bpl.to_bytes(2, "little")
|
||||
hd[68] = 1
|
||||
out = bytearray(hd)
|
||||
for y in range(h):
|
||||
row = list(idx[y * w:(y + 1) * w]) + [0] * (bpl - w)
|
||||
i = 0
|
||||
while i < bpl:
|
||||
v, n = row[i], 1
|
||||
while i + n < bpl and n < 63 and row[i + n] == v:
|
||||
n += 1
|
||||
if n > 1 or v >= 0xC0:
|
||||
out.append(0xC0 | n)
|
||||
out.append(v)
|
||||
i += n
|
||||
out.append(12)
|
||||
out += bytes(pal)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def pcx_read(data):
|
||||
"""(w, h, row-major indices, 768-byte palette) of an 8-bit RLE PCX"""
|
||||
w = int.from_bytes(data[8:10], "little") - int.from_bytes(data[4:6], "little") + 1
|
||||
h = int.from_bytes(data[10:12], "little") - int.from_bytes(data[6:8], "little") + 1
|
||||
bpl = int.from_bytes(data[66:68], "little")
|
||||
raw, p = bytearray(), 128
|
||||
while len(raw) < bpl * h:
|
||||
c = data[p]
|
||||
p += 1
|
||||
if c >= 0xC0:
|
||||
raw += bytes([data[p]]) * (c & 63)
|
||||
p += 1
|
||||
else:
|
||||
raw.append(c)
|
||||
idx = bytearray()
|
||||
for y in range(h):
|
||||
idx += raw[y * bpl:y * bpl + w]
|
||||
return w, h, bytes(idx), data[-768:]
|
||||
@@ -0,0 +1,117 @@
|
||||
# Build ART fixtures (tools/fixtures.py). No Duke 3D data (proprietary): a random PALETTE.DAT (256 distinct 6-bit
|
||||
# colours, 32 shade tables, a translucency table) and LOOKUP.DAT (palookups 1 and 5, five alternate palettes) are
|
||||
# made here. References, from the nix eduke32 source (tools/fixtures/_build_ref.py):
|
||||
# - arttool create / addtile builds TILES000.ART (tiles 10..15, two of them empty) from 8-bit PCX files written
|
||||
# here, with centre offsets and animation fields; `arttool info` gives the per-tile fields;
|
||||
# - arttool exporttile writes each tile back as PCX through palette.dat (<< 2 scaling): its RGB is the reference;
|
||||
# - an independent Python reading of the ART bytes (BUILDINF.TXT layout) gives each tile's column-major indices.
|
||||
# tests/gen/art.luau { art, palette, lookup, start, last, tiles = { { tile, w, h, idx, rgb, xofs, yofs, animType,
|
||||
# animFrames, animSpeed, picanm } }, shade = { n, table }, remap = { pal, table } }
|
||||
import os, shutil, struct, subprocess, tempfile, importlib.util
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_build_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_ref.py"))
|
||||
B = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(B)
|
||||
|
||||
INPUTS = ["tools/fixtures/_build_ref.py"]
|
||||
|
||||
|
||||
def palette_dat(rng):
|
||||
cols, seen = [], set()
|
||||
while len(cols) < 256:
|
||||
c = (rng.randrange(64), rng.randrange(64), rng.randrange(64))
|
||||
if c not in seen:
|
||||
seen.add(c)
|
||||
cols.append(c)
|
||||
pal = bytes(v for c in cols for v in c)
|
||||
shades = rng.randbytes(32 * 256)
|
||||
return pal + struct.pack("<H", 32) + shades + rng.randbytes(65536)
|
||||
|
||||
|
||||
def lookup_dat(rng):
|
||||
out = bytearray([2])
|
||||
for n in (1, 5):
|
||||
out.append(n)
|
||||
out += rng.randbytes(256)
|
||||
for _ in range(5):
|
||||
out += bytes(rng.randrange(64) for _ in range(768))
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def parse_art(data):
|
||||
"""independent ART reading: (first, last, [(w, h, picanm, column-major bytes)])"""
|
||||
ver, _, first, last = struct.unpack_from("<4I", data, 0)
|
||||
assert ver == 1
|
||||
n = last - first + 1
|
||||
xs = struct.unpack_from("<%dh" % n, data, 16)
|
||||
ys = struct.unpack_from("<%dh" % n, data, 16 + 2 * n)
|
||||
anm = struct.unpack_from("<%dI" % n, data, 16 + 4 * n)
|
||||
p, tiles = 16 + 8 * n, []
|
||||
for i in range(n):
|
||||
size = xs[i] * ys[i]
|
||||
tiles.append((xs[i], ys[i], anm[i], data[p:p + size]))
|
||||
p += size
|
||||
return first, last, tiles
|
||||
|
||||
|
||||
SPECS = [ # tile, w, h, addtile options
|
||||
(10, 16, 8, ["-x", "3", "-y", "-2"]),
|
||||
(11, 1, 5, ["-ann", "3", "-ant", "2", "-ans", "4"]),
|
||||
(13, 33, 17, ["-x", "-7", "-ann", "63", "-ant", "1", "-ans", "15"]),
|
||||
(15, 64, 64, ["-y", "100", "-ant", "3"]),
|
||||
]
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(1997)
|
||||
tools = B.tools(ctx)
|
||||
pal_dat = palette_dat(rng)
|
||||
pal8 = bytes(v << 2 for v in pal_dat[:768])
|
||||
lookup = lookup_dat(rng)
|
||||
tmp = tempfile.mkdtemp(prefix="rosetta-art-")
|
||||
try:
|
||||
run = lambda *a: subprocess.run([tools["arttool"]] + list(a), cwd=tmp, capture_output=True, check=True).stdout.decode()
|
||||
run("create", "-o", "10", "-n", "6")
|
||||
made = {}
|
||||
for tile, w, h, extra in SPECS:
|
||||
idx = bytes(rng.randrange(256) if rng.random() < 0.85 else 255 for _ in range(w * h))
|
||||
made[tile] = idx
|
||||
with open(os.path.join(tmp, "t%d.pcx" % tile), "wb") as f:
|
||||
f.write(B.pcx_write(w, h, idx, pal8))
|
||||
run("addtile", *extra, str(tile), "t%d.pcx" % tile)
|
||||
art = open(os.path.join(tmp, "tiles000.art"), "rb").read()
|
||||
with open(os.path.join(tmp, "palette.dat"), "wb") as f:
|
||||
f.write(pal_dat)
|
||||
info = {}
|
||||
for line in run("info").split("\n"):
|
||||
line = line.strip()
|
||||
if line.startswith("Tile "):
|
||||
head, rest = line.split(": ", 1)
|
||||
t = int(head.split()[1])
|
||||
size, rest = rest.split(" ", 1)
|
||||
fields = dict(kv.split(": ") for kv in rest.split(", "))
|
||||
info[t] = (size, {k: int(v) for k, v in fields.items()})
|
||||
first, last, parsed = parse_art(art)
|
||||
tiles = []
|
||||
for i, (w, h, anm, cols) in enumerate(parsed):
|
||||
t = first + i
|
||||
size, f = info[t]
|
||||
assert size == "%dx%d" % (w, h)
|
||||
e = {"tile": t, "w": w, "h": h, "picanm": anm, "xofs": f["Xofs"], "yofs": f["Yofs"], "animType": f["AnimType"],
|
||||
"animFrames": f["AnimFrames"], "animSpeed": f["AnimSpeed"]}
|
||||
if w * h > 0:
|
||||
idx = bytes(cols[x * h + y] for y in range(h) for x in range(w))
|
||||
assert idx == made[t]
|
||||
run("exporttile", str(t))
|
||||
pw, ph, pidx, ppal = B.pcx_read(open(os.path.join(tmp, "tile%04d.pcx" % t), "rb").read())
|
||||
assert (pw, ph, pidx) == (w, h, idx)
|
||||
e["idx"] = idx
|
||||
e["rgb"] = bytes(ppal[v * 3 + k] for v in pidx for k in range(3))
|
||||
tiles.append(e)
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
ctx.emit("art", {
|
||||
"art": art, "palette": pal_dat, "lookup": lookup, "start": first, "last": last, "tiles": tiles,
|
||||
"shade": {"n": 7, "table": pal_dat[770 + 7 * 256:770 + 8 * 256]},
|
||||
"remap": {"pal": 5, "table": lookup[1 + 257 + 1:1 + 257 + 257]},
|
||||
})
|
||||
Reference in new issue
Block a user