ws-tex: image/sgi + image/sunras (read + write)

SGI: verbatim / RLE, 1-2 bytes per channel, 1-4 channels (rgba16 native); write RGB(A) RLE / verbatim.
Sun Raster: 1 / 8 / 24 / 32-bit, standard / byte-encoded / RGB types, colour maps; write 24 / 32-bit.
References: ImageMagick, netpbm (sgitopnm-era writers, rasttopnm), Pillow.
This commit is contained in:
4DBug committed 2026-10-31 14:52:47 -05:00
1 parent d294c211f9
commit 403a4d6f31
10 files changed
+745

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
# Shared by the simple raster fixture generators: run our encoder (tools/fixtures/_tex_encode.luau -a <fmt>) and
# decode files with ImageMagick into rgba8 (through PAM).
import os, shutil, subprocess
def ours(ctx, which):
exe = shutil.which("luau")
cmd = [exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]
res = subprocess.run(cmd + ["tools/fixtures/_tex_encode.luau", "-a", which], cwd=ctx.root, capture_output=True, check=True)
files = {}
for line in res.stdout.decode().split("\n"):
if line.startswith("ours_") and "\t" in line:
n, hx = line.split("\t", 1)
files[n] = bytes.fromhex(hx.strip())
return files
def magick_cases(R, w, names, fmt=None):
"""ImageMagick decodes of the named files in the work dir -> { name: (w, h, rgba) }"""
pre = (fmt + ":") if fmt else ""
w.sh("imagemagick", ["magick %s%s -depth 8 -alpha on pam:%s.pam" % (pre, n, n) for n in names])
return {n: R.pnm_rgba(w.read(n + ".pam")) for n in names}
+4
View File
@@ -132,6 +132,10 @@ end
local function simple(): any
if which == "pcx" then
return require("@formats/image/pcx")
elseif which == "sgi" then
return require("@formats/image/sgi")
elseif which == "sunras" then
return require("@formats/image/sunras")
end
return nil
end
+39
View File
@@ -0,0 +1,39 @@
# SGI fixtures (tools/fixtures.py): files from netpbm (pnmtosgi: RLE and verbatim, RGB and grey), ImageMagick (RGB,
# RGBA, 16-bit) and Pillow (L, RGB, RGBA), plus our encoder's files (tools/fixtures/_tex_encode.luau -a sgi); the
# reference is ImageMagick's decode of each file (16-bit within 1: it rounds where we keep the high byte).
# tests/gen/sgi.luau { cases = { { name, data, w, h, rgba, tol } }, ours = { ... } }
import importlib.util, os
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/Formats/image/sgi"]
def generate(ctx):
w = R.Work(ctx)
try:
w.png("photo.png", 37, 23, R.photo(37, 23))
w.png("alpha.png", 30, 17, R.photo(30, 17, 3, True))
w.sh("netpbm", ["pngtopam photo.png | pamtopnm > photo.ppm", "pnmtosgi -rle photo.ppm > nb_rle.sgi", "pnmtosgi -verbatim photo.ppm > nb_verbatim.sgi",
"ppmtopgm photo.ppm | pnmtosgi -rle > nb_grey.sgi"])
w.sh("imagemagick", ["magick photo.png -alpha off im_rgb.sgi", "magick alpha.png im_rgba.sgi", "magick photo.png -alpha off -depth 16 im_rgb16.sgi",
"magick alpha.png -compress none im_rgba_verbatim.sgi"])
ctx.python(["pillow"], ("from PIL import Image\nimport os\nos.chdir(%r)\nim=Image.open('alpha.png')\nim.save('pil_rgba.sgi')\n"
"im.convert('RGB').save('pil_rgb.sgi')\nim.convert('L').save('pil_l.sgi')\n") % w.dir)
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".sgi"))
mine = S.ours(ctx, "sgi")
for n, d in mine.items():
w.write(n, d)
ref = S.magick_cases(R, w, names + sorted(mine))
out = {"cases": [], "ours": []}
for n in names + sorted(mine):
ww, hh, rgba = ref[n]
out["ours" if n.startswith("ours_") else "cases"].append({"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba, "tol": 1 if "16" in n else 0})
ctx.emit("sgi", out)
finally:
w.close()
+45
View File
@@ -0,0 +1,45 @@
# Sun Raster fixtures (tools/fixtures.py): files from netpbm (pnmtorast: standard and byte-encoded, 1 / 8 / 24-bit) and
# ImageMagick (24-bit, 32-bit with alpha, 8-bit palette, 1-bit), plus our encoder's files (tools/fixtures/_tex_encode
# .luau -a sunras); the reference is rasttopnm for netpbm's and our files (RGB only) and ImageMagick's decode for the others.
# tests/gen/sunras.luau { cases = { { name, data, w, h, rgba } }, ours = { ... } }
import importlib.util, os
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/Formats/image/sunras"]
def generate(ctx):
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, 40))
w.png("alpha.png", 30, 17, R.photo(30, 17, 3, True))
w.sh("netpbm", ["pngtopam photo.png | pamtopnm > photo.ppm", "pngtopam pal.png | pamtopnm > pal.ppm",
"pnmtorast -standard photo.ppm > nb_24_standard.ras", "pnmtorast -rle photo.ppm > nb_24_rle.ras",
"pnmtorast -rle pal.ppm > nb_8_rle.ras", "ppmtopgm photo.ppm | pnmtorast -standard > nb_grey.ras",
"ppmtopgm photo.ppm | pgmtopbm | pnmtorast -rle > nb_1.ras"])
w.sh("imagemagick", ["magick photo.png -alpha off im_24.ras", "magick alpha.png im_32.ras", "magick pal.png -alpha off -type palette im_8.ras",
"magick photo.png -monochrome im_1.ras"])
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".ras"))
mine = S.ours(ctx, "sunras")
for n, d in mine.items():
w.write(n, d)
# netpbm's own files through rasttopnm (ImageMagick cuts the last row of nb_8_rle short), the rest through ImageMagick
nb = [n for n in names if n.startswith("nb_")] + sorted(mine)
w.sh("netpbm", ["rasttopnm %s > %s.pnm" % (n, n) for n in nb])
ref = S.magick_cases(R, w, [n for n in names if not n.startswith("nb_")], "sun")
for n in nb:
ref[n] = R.pnm_rgba(w.read(n + ".pnm"))
out = {"cases": [], "ours": []}
for n in names + sorted(mine):
ww, hh, rgba = ref[n]
out["ours" if n.startswith("ours_") else "cases"].append({"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba})
ctx.emit("sunras", out)
finally:
w.close()