Files
rosetta/tools/fixtures/xpm.py
T
4DBug ef460955d4 ws-tex: image/xbm + image/xpm (read + write)
XBM: X11 / X10 arrays, hex / decimal / octal, hotspot; xbmtopbm reference (netpbm, ImageMagick, Pillow files).
XPM: XPM 3 + XPM 2, c / g / g4 / m keys, hex depths, None, CSS / X11 names + grey0..100 (image/xpm/Names);
ImageMagick reference.
2026-10-31 14:58:20 -05:00

59 lines
2.6 KiB
Python

# XPM fixtures (tools/fixtures.py): files from netpbm (ppmtoxpm -hexonly), ImageMagick (with alpha: None) and
# hand-written ones (colour names, #RGB / #RRRRGGGGBBBB, g / m keys, two chars per pixel, XPM 2); the reference is
# ImageMagick's decode (the XPM 2 file's values are written here: ImageMagick does not read XPM 2); our encoder's files (tools/fixtures/_tex_encode.luau -a xpm) read by ImageMagick.
# tests/gen/xpm.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/xpm"]
HAND = b'''/* XPM */
static char * hand_xpm[] = {
/* width height ncolors cpp */
"6 4 6 2",
" c None",
".. c red",
"XX g4 #888 c LightBlue m white",
"oo c #FFFF00000000",
"++ s mine c navy",
"@@ c Gold",
" ..XXoo++@@",
"..XXoo++@@ ",
"XXoo++@@ ..",
"oo++@@ ..XX"};
'''
XPM2 = b"! XPM2\n3 2 2 1\na c #102030\nb c white\nabb\nbab\n"
def generate(ctx):
w = R.Work(ctx)
try:
w.png("pal.png", 31, 19, R.palette_image(31, 19, 40))
w.png("alpha.png", 20, 11, bytes(v for i in range(20 * 11) for v in (((i * 40) % 256, 80, 200, 255 if i % 3 else 0))))
w.sh("netpbm", ["pngtopam pal.png | pamtopnm > pal.ppm", "ppmtoxpm -hexonly pal.ppm > nb.xpm"])
w.sh("imagemagick", ["magick pal.png -alpha off im.xpm", "magick alpha.png im_alpha.xpm"])
w.write("hand.xpm", HAND)
w.write("hand2.xpm", XPM2)
names = sorted(f for f in os.listdir(w.dir) if f.endswith(".xpm") and f != "hand2.xpm")
mine = S.ours(ctx, "xpm")
for n, d in mine.items():
w.write(n, d)
ref = S.magick_cases(R, w, names + sorted(mine), "xpm")
out = {"cases": [], "ours": []}
for n in names + sorted(mine):
ww, hh, rgba = ref[n]
# ImageMagick keeps the colour of transparent pixels; we decode None as transparent black
rgba = bytes(0 if rgba[i - i % 4 + 3] == 0 else v for i, v in enumerate(rgba))
out["ours" if n.startswith("ours_") else "cases"].append({"name": n, "data": w.read(n), "w": ww, "h": hh, "rgba": rgba})
a, b = bytes((0x10, 0x20, 0x30, 255)), b"\xff" * 4
out["cases"].append({"name": "hand2.xpm (XPM 2)", "data": XPM2, "w": 3, "h": 2, "rgba": a + b + b + b + a + b})
ctx.emit("xpm", out)
finally:
w.close()