Files

38 lines
1.6 KiB
Luau

-- image/pcx tests against netpbm's pcxtoppm and ImageMagick (tools/fixtures/pcx.py).
local T = require("@tests/T")
local Pcx = require("@rosetta/image/pcx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/pcx")
local t = T.suite("pcx")
t:eq(Pcx.name, "pcx", "name")
t:check(Pcx.caps.decode and Pcx.caps.encode, "caps")
t:note("%d files match pcxtoppm", R.decodes(t, Pcx, Fx.cases))
-- our files: ImageMagick reads what we decode
for _, o in Fx.ours do
local img = Pcx.decode(Bytes.reader(buffer.fromstring(o.data))) :: any
t:eq(R.maxDiff(img.pixels, buffer.fromstring(o.rgba), o.w * o.h), 0, o.name .. ": ImageMagick reads our file")
end
for _, c in Fx.cases do
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Pcx, R.image(c.w, c.h, c.rgba), { palette = false }, c.name .. " re-encoded 24-bit")
end
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Pcx, img, nil, "alpha: 4 planes") :: buffer
t:eq(buffer.readu8(enc, 65), 4, "alpha -> 4 planes")
t:eq(Pcx.sniff(Bytes.reader(enc)), 0.8, "sniff")
t:eq(Pcx.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Pcx.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 60))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Pcx.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
end
t:fuzz("decode", function(b: buffer)
return Pcx.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()