Files
rosetta/tests/image/xbm.luau
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

44 lines
2.4 KiB
Luau

-- image/xbm tests: files from netpbm / ImageMagick / Pillow and hand-written ones against the reference decode
-- (xbmtopbm), our encoder's files read back by it, round trips, errors, the fuzz no-throw pass.
-- Fixtures: tools/fixtures/xbm.py.
local T = require("@tests/T")
local Xbm = require("@formats/image/xbm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/xbm")
local t = T.suite("xbm")
t:eq(Xbm.name, "xbm", "name")
t:check(Xbm.caps.decode and Xbm.caps.encode, "caps")
t:note("%d files match the reference", R.decodes(t, Xbm, Fx.cases))
t:note("%d of our files read back by it", R.decodes(t, Xbm, Fx.ours))
for _, c in Fx.cases do
R.roundTrip(t, Xbm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
if c.hotspot then
local img = Xbm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:check(img.meta.hotspot and img.meta.hotspot.x == c.hotspot[1] and img.meta.hotspot.y == c.hotspot[2], c.name .. ": hotspot")
end
end
do
-- decimal and octal values, the X10 short arrays
local img = Xbm.decode(Bytes.reader(buffer.fromstring("#define a_width 9\n#define a_height 1\nstatic char a_bits[] = { 1, 0400 };"))) :: any
t:eq(buffer.readu32(img.pixels, 0), 0xFF000000, "decimal: pixel 0 black")
t:eq(buffer.readu32(img.pixels, 4), 0xFFFFFFFF, "decimal: pixel 1 white")
local x10 = Xbm.decode(Bytes.reader(buffer.fromstring("#define b_width 17\n#define b_height 1\nstatic short b_bits[] = { 0x8001, 0x0001 };"))) :: any
t:eq(buffer.readu32(x10.pixels, 15 * 4), 0xFF000000, "x10: bit 15")
t:eq(buffer.readu32(x10.pixels, 16 * 4), 0xFF000000, "x10: second short")
local tr = Xbm.decode(Bytes.reader(buffer.fromstring("#define c_width 2\n#define c_height 1\nstatic char c_bits[] = { 0x01 };")), { transparent = true }) :: any
t:eq(buffer.readu32(tr.pixels, 4), 0x00FFFFFF, "transparent clear bits")
t:eq(Xbm.sniff(Bytes.reader(buffer.fromstring("#define c_width 2\n"))), 0.9, "sniff")
local _, e = Xbm.decode(Bytes.reader(buffer.fromstring("static char x[] = {};")))
t:check(e and e.code == Err.CORRUPT, "no defines")
local _, e2 = Xbm.encode(R.image(1, 1, "\0\0\0\255"), { name = "1bad" })
t:check(e2 and e2.code == Err.ARG, "bad name")
end
t:fuzz("decode", function(b: buffer)
return Xbm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()