Tested against netpbm ppmtopcx / ImageMagick / Pillow files decoded by pcxtoppm; our files read by ImageMagick. Shared test helper tests/image/_raster.luau and fixture helper tools/fixtures/_raster_ref.py.
59 lines
3.5 KiB
Python
59 lines
3.5 KiB
Python
# PCX fixtures (tools/fixtures.py): files from netpbm's ppmtopcx (24-bit planes, 8-bit palette, packed 1 / 2 / 4-bit,
|
|
# 4-plane EGA, 1-bit mono), ImageMagick (24-bit, palette) and Pillow (RGB, P, L, 1), decoded by pcxtoppm (netpbm) as
|
|
# the reference; our encoder's files (tools/fixtures/_tex_encode.luau -a pcx) decoded by ImageMagick (it reads 4-plane alpha).
|
|
# tests/gen/pcx.luau { cases = { { name, data, w, h, rgba } }, ours = { { name, data, w, h, rgba } } }
|
|
import importlib.util, os, shutil, subprocess
|
|
|
|
_spec = importlib.util.spec_from_file_location("_raster_ref", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_raster_ref.py"))
|
|
R = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(R)
|
|
|
|
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Formats/image/pcx"]
|
|
|
|
|
|
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 generate(ctx):
|
|
w = R.Work(ctx)
|
|
try:
|
|
w.png("photo.png", 37, 23, R.photo(37, 23))
|
|
w.png("pal.png", 40, 30, R.palette_image(40, 30, 200))
|
|
w.png("c16.png", 33, 17, R.palette_image(33, 17, 16, 5))
|
|
w.png("c4.png", 21, 9, R.palette_image(21, 9, 4, 6))
|
|
w.png("mono.png", 29, 13, bytes(v for i in range(29 * 13) for v in ((255,) * 3 if (i * 7) % 5 < 2 else (0,) * 3) + (255,)))
|
|
nb = ["pngtopam photo.png | pamtopnm > photo.ppm", "pngtopam pal.png | pamtopnm > pal.ppm", "pngtopam c16.png | pamtopnm > c16.ppm",
|
|
"pngtopam c4.png | pamtopnm > c4.ppm", "pngtopam mono.png | pamtopnm | ppmtopgm | pgmtopbm > mono.pbm",
|
|
"ppmtopcx -24bit photo.ppm > nb_24bit.pcx", "ppmtopcx -8bit pal.ppm > nb_8bit.pcx", "ppmtopcx -packed c16.ppm > nb_packed4.pcx",
|
|
"ppmtopcx -packed c4.ppm > nb_packed2.pcx", "ppmtopcx -planes 4 c16.ppm > nb_planes4.pcx", "ppmtopcx mono.pbm > nb_mono.pcx",
|
|
"ppmtopcx -planes 3 c4.ppm > nb_planes3.pcx"]
|
|
w.sh("netpbm", nb)
|
|
w.sh("imagemagick", ["magick photo.png -alpha off im_24bit.pcx", "magick pal.png -alpha off -type palette im_palette.pcx"])
|
|
code = ("from PIL import Image\nimport os\nos.chdir(%r)\nim=Image.open('photo.png').convert('RGB')\nim.save('pil_rgb.pcx')\n"
|
|
"Image.open('pal.png').convert('RGB').quantize(256).save('pil_p.pcx')\nim.convert('L').save('pil_l.pcx')\n"
|
|
"Image.open('mono.png').convert('1').save('pil_1.pcx')\n") % w.dir
|
|
ctx.python(["pillow"], code)
|
|
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".pcx"))
|
|
mine = ours(ctx, "pcx")
|
|
for n, d in mine.items():
|
|
w.write(n, d)
|
|
allnames = names + sorted(mine)
|
|
w.sh("netpbm", ["pcxtoppm %s > %s.ppm" % (n, n) for n in names])
|
|
w.sh("imagemagick", ["magick %s -depth 8 pam:%s.ppm" % (n, n) for n in sorted(mine)])
|
|
cases, our = [], []
|
|
for n in allnames:
|
|
ww, hh, rgba = R.pnm_rgba(w.read(n + ".ppm"))
|
|
(our if n.startswith("ours_") else cases).append({"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba})
|
|
ctx.emit("pcx", {"cases": cases, "ours": our})
|
|
finally:
|
|
w.close()
|