Files

42 lines
1.9 KiB
Luau

-- image/xpm tests against ImageMagick (tools/fixtures/xpm.py).
local T = require("@tests/T")
local Xpm = require("@rosetta/image/xpm")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/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")
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()