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.
46 lines
2.6 KiB
Python
46 lines
2.6 KiB
Python
# 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()
|