Files

44 lines
2.3 KiB
Python

# XBM fixtures (tools/fixtures.py): files from netpbm (pbmtoxbm X11 and -x10), ImageMagick and Pillow (with a hotspot),
# a hand-written one (comments, odd spacing); the reference is xbmtopbm (netpbm); our encoder's files
# (tools/fixtures/_tex_encode.luau -a xbm) read by xbmtopbm.
# tests/gen/xbm.luau { cases = { { name, data, w, h, rgba, hotspot? } }, 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/Rosetta/image/xbm"]
def generate(ctx):
w = R.Work(ctx)
try:
mono = bytes(v for i in range(29 * 13) for v in ((0,) * 3 if (i * 7 + i // 29) % 5 < 2 else (255,) * 3) + (255,))
w.png("mono.png", 29, 13, mono)
w.png("mono2.png", 16, 9, bytes(v for i in range(16 * 9) for v in ((0,) * 3 if (i % 3 == 0) else (255,) * 3) + (255,)))
w.sh("netpbm", ["pngtopam mono.png | pamtopnm | ppmtopgm | pgmtopbm -threshold > mono.pbm", "pbmtoxbm mono.pbm > nb_x11.xbm",
"pbmtoxbm -x10 mono.pbm > nb_x10.xbm"])
w.sh("imagemagick", ["magick mono2.png im.xbm"])
ctx.python(["pillow"], "from PIL import Image\nimport os\nos.chdir(%r)\nImage.open('mono.png').convert('1').save('pil.xbm', hotspot=(3, 4))\n" % w.dir)
w.write("hand.xbm", b"/* a comment */\n#define hand_width 10\n#define hand_height 2\nstatic unsigned char hand_bits[] = {\n 0xff,0x03, /* row 0 */\n 0x00, 0x01 };\n")
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".xbm"))
mine = S.ours(ctx, "xbm")
for n, d in mine.items():
w.write(n, d)
allnames = names + sorted(mine)
w.sh("netpbm", ["xbmtopbm %s > %s.pbm" % (n, n) for n in allnames])
out = {"cases": [], "ours": []}
for n in allnames:
ww, hh, rgba = R.pnm_rgba(w.read(n + ".pbm"))
c = {"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba}
if n == "pil.xbm":
c["hotspot"] = [3, 4]
out["ours" if n.startswith("ours_") else "cases"].append(c)
ctx.emit("xbm", out)
finally:
w.close()