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.
This commit is contained in:
1 parent
403a4d6f31
commit
ef460955d4
10 files changed
+707
No files matched your search
@@ -136,6 +136,10 @@ local function simple(): any
|
||||
return require("@formats/image/sgi")
|
||||
elseif which == "sunras" then
|
||||
return require("@formats/image/sunras")
|
||||
elseif which == "xbm" then
|
||||
return require("@formats/image/xbm")
|
||||
elseif which == "xpm" then
|
||||
return require("@formats/image/xpm")
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
# 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/Formats/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()
|
||||
@@ -0,0 +1,58 @@
|
||||
# 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()
|
||||
Reference in new issue
Block a user