Files

122 lines
5.7 KiB
Python

# IFF ILBM fixtures (tools/fixtures.py): files from netpbm's ppmtoilbm (palette ByteRun1 / uncompressed, few planes,
# HAM6, HAM8, 24-bit deep) and hand-written ones (EHB, chunky PBM, a mask plane, a transparent colour index, an odd
# CMAP), decoded by ilbmtoppm as the reference (alpha separately: ilbmtoppm writes PPM; HAM values within the scaling
# difference: ilbmtoppm shifts, we replicate bits); our encoder's files
# (tools/fixtures/_tex_encode.luau -a ilbm) read by ilbmtoppm.
# tests/gen/ilbm.luau { cases = { { name, data, w, h, rgba, transparent? } }, ours = { ... } }
import importlib.util, os, random, struct
def _load(n):
spec = importlib.util.spec_from_file_location(n, os.path.join(os.path.dirname(os.path.abspath(__file__)), n + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
R, S = _load("_raster_ref"), _load("_simple")
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Rosetta/image/ilbm"]
def chunk(cid, data):
return cid + struct.pack(">I", len(data)) + data + (b"\0" if len(data) % 2 else b"")
def byterun1(row):
out = bytearray()
i = 0
while i < len(row):
run = 1
while i + run < len(row) and run < 128 and row[i + run] == row[i]:
run += 1
if run >= 2:
out += bytes((257 - run, row[i]))
i += run
else:
j = i
while j < len(row) and j - i < 128 and not (j + 1 < len(row) and row[j + 1] == row[j]):
j += 1
j = max(j, i + 1)
out += bytes((j - i - 1,)) + row[i:j]
i = j
return bytes(out)
def planar(w, h, idx, planes, mask=None, comp=True):
rb = ((w + 15) // 16) * 2
body = bytearray()
for y in range(h):
rows = []
for k in range(planes):
row = bytearray(rb)
for x in range(w):
if (idx[y * w + x] >> k) & 1:
row[x // 8] |= 0x80 >> (x % 8)
rows.append(bytes(row))
if mask is not None:
row = bytearray(rb)
for x in range(w):
if mask[y * w + x]:
row[x // 8] |= 0x80 >> (x % 8)
rows.append(bytes(row))
for row in rows:
body += byterun1(row) if comp else row
return bytes(body)
def ilbm(w, h, planes, body, cmap=b"", camg=None, masking=0, trans=0, comp=1, form=b"ILBM", extra=b""):
bmhd = struct.pack(">HHhhBBBxHBBhh", w, h, 0, 0, planes, masking, comp, trans, 10, 11, w, h)
data = form + chunk(b"BMHD", bmhd) + (chunk(b"CMAP", cmap) if cmap else b"") + (chunk(b"CAMG", struct.pack(">I", camg)) if camg is not None else b"") + extra + chunk(b"BODY", body)
return b"FORM" + struct.pack(">I", len(data)) + data
def generate(ctx):
rng = random.Random(8)
w = R.Work(ctx)
try:
w.png("photo.png", 37, 23, R.photo(37, 23))
w.png("pal.png", 31, 19, R.palette_image(31, 19, 60))
w.png("pal8.png", 26, 11, R.palette_image(26, 11, 7))
w.sh("netpbm", ["pngtopam photo.png | pamtopnm > photo.ppm", "pngtopam pal.png | pamtopnm > pal.ppm", "pngtopam pal8.png | pamtopnm > pal8.ppm",
"ppmtoilbm pal.ppm > nb_pal.iff", "ppmtoilbm -nocompress pal.ppm > nb_pal_raw.iff", "ppmtoilbm pal8.ppm > nb_3planes.iff",
"ppmtoilbm -ham6 photo.ppm > nb_ham6.iff", "ppmtoilbm -ham8 photo.ppm > nb_ham8.iff", "ppmtoilbm -24force photo.ppm > nb_24.iff"])
hand = []
# EHB: 6 planes, 32 colours
W, H = 20, 7
cm = bytes(rng.randrange(256) for _ in range(32 * 3))
idx = [rng.randrange(64) for _ in range(W * H)]
hand.append(("hand_ehb.iff", ilbm(W, H, 6, planar(W, H, idx, 6), cm, 0x80), None))
# chunky PBM (rows padded to even), 256 colours
cm2 = bytes(rng.randrange(256) for _ in range(256 * 3))
W2 = 14
idx2 = [rng.randrange(256) for _ in range(W2 * H)]
hand.append(("hand_pbm.lbm", ilbm(W2, H, 8, b"".join(byterun1(bytes(idx2[y * W2:(y + 1) * W2])) for y in range(H)), cm2, form=b"PBM "), None))
# a mask plane and a transparent colour index
cm3 = bytes(rng.randrange(256) for _ in range(16 * 3))
idx3 = [rng.randrange(16) for _ in range(W * H)]
mask = [rng.random() < 0.7 for _ in range(W * H)]
hand.append(("hand_mask.iff", ilbm(W, H, 4, planar(W, H, idx3, 4, mask), cm3, masking=1), [not m for m in mask]))
hand.append(("hand_transparent.iff", ilbm(W, H, 4, planar(W, H, idx3, 4, comp=False), cm3, masking=2, trans=5, comp=0,
extra=chunk(b"NAME", b"tiny\0")), [i == 5 for i in idx3]))
for n, data, _ in hand:
w.write(n, data)
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".iff") or f.endswith(".lbm"))
mine = S.ours(ctx, "ilbm")
for n, d in mine.items():
w.write(n, d)
allnames = names + sorted(mine)
allnames = [n for n in allnames if n != "ours_alpha.ilbm"] # ilbmtoppm reads no 32-plane files
w.sh("netpbm", ["ilbmtoppm %s > %s.ppm" % (n, n) for n in allnames])
tmap = {n: t for n, _, t in hand}
out = {"cases": [], "ours": []}
for n in allnames:
ww, hh, rgba = R.pnm_rgba(w.read(n + ".ppm"))
c = {"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba}
if "ham" in n:
# ilbmtoppm scales HAM values by shifting (4-bit v -> v << 4); we replicate the bits (v * 17)
c["tol"] = 15 if "ham6" in n else 3
if tmap.get(n):
c["transparent"] = [bool(v) for v in tmap[n]]
out["ours" if n.startswith("ours_") else "cases"].append(c)
ctx.emit("ilbm", out)
finally:
w.close()