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

45 lines
2.1 KiB
Luau

-- image/xpm tests: files from netpbm / ImageMagick and hand-written ones against the reference decode
-- (ImageMagick), our encoder's files read back by it, round trips, errors, the fuzz no-throw pass.
-- Fixtures: tools/fixtures/xpm.py.
local T = require("@tests/T")
local Xpm = require("@formats/image/xpm")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/xpm")
local t = T.suite("xpm")
t:eq(Xpm.name, "xpm", "name")
t:check(Xpm.caps.decode and Xpm.caps.encode, "caps")
t:note("%d files match the reference", R.decodes(t, Xpm, Fx.cases))
t:note("%d of our files read back by it", R.decodes(t, Xpm, Fx.ours))
for _, c in Fx.cases do
R.roundTrip(t, Xpm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
if c.hotspot then
local img = Xpm.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
local img = Xpm.decode(Bytes.reader(buffer.fromstring('/* XPM */\nstatic char *x[] = {"2 1 2 1 0 0", "a c gray50", "b c Unknownish", "ab"};'))) :: any
t:eq(buffer.readu32(img.pixels, 0), 0xFF7F7F7F, "gray50")
t:check(img.meta.unknownColours and img.meta.unknownColours[1] == "Unknownish", "unknown colour listed")
t:check(img.meta.hotspot and img.meta.hotspot.x == 0, "hotspot")
t:eq(Xpm.sniff(Bytes.reader(buffer.fromstring("/* XPM */\n"))), 1, "sniff")
local _, e = Xpm.decode(Bytes.reader(buffer.fromstring('/* XPM */ static char *x[] = {"2 1 5 1", "a c red"};')))
t:check(e and e.code == Err.TRUNCATED, "short colour table")
-- many colours: two chars per pixel
local big = R.image(300, 1, "")
big.pixels = buffer.create(1200)
for i = 0, 299 do
buffer.writeu32(big.pixels, i * 4, i * 997 + 0xFF000000)
end
local enc = R.roundTrip(t, Xpm, big, nil, "300 colours") :: buffer
t:check(string.find(buffer.tostring(enc), '"300 1 300 2"', 1, true) ~= nil, "two chars per pixel")
end
t:fuzz("decode", function(b: buffer)
return Xpm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()