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:
4DBug committed 2026-10-31 14:58:20 -05:00
1 parent 403a4d6f31
commit ef460955d4
10 files changed
+707

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
-- 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()
+44
View File
@@ -0,0 +1,44 @@
-- 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()