Files

42 lines
1.8 KiB
Luau

-- image/sunras tests against ImageMagick and rasttopnm (tools/fixtures/sunras.py).
local T = require("@tests/T")
local Sunras = require("@rosetta/image/sunras")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/sunras")
local t = T.suite("sunras")
t:eq(Sunras.name, "sunras", "name")
t:check(Sunras.caps.decode and Sunras.caps.encode, "caps")
t:note("%d files match rasttopnm / ImageMagick", R.decodes(t, Sunras, Fx.cases))
-- our files: rasttopnm reads the colours (it drops alpha)
for _, o in Fx.ours do
local img = Sunras.decode(Bytes.reader(buffer.fromstring(o.data))) :: any
local px = buffer.fromstring(buffer.tostring(img.pixels))
for i = 0, o.w * o.h - 1 do
buffer.writeu8(px, i * 4 + 3, 255)
end
t:eq(R.maxDiff(px, buffer.fromstring(o.rgba), o.w * o.h), 0, o.name .. ": rasttopnm reads our file")
end
for _, c in Fx.cases do
R.roundTrip(t, Sunras, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Sunras, R.image(c.w, c.h, c.rgba), { rle = false }, c.name .. " re-encoded without RLE")
end
do
local enc = Sunras.encode(R.image(2, 2, string.rep("\1\2\3\4", 4))) :: buffer
t:eq(Sunras.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Sunras.sniff(Bytes.reader(buffer.fromstring("GIF89a"))), 0, "sniff other")
local _, e = Sunras.decode(Bytes.reader(buffer.fromstring(string.sub(buffer.tostring(enc), 1, 20))))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = Sunras.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
local _, e3 = Sunras.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "bad image")
end
t:fuzz("decode", function(b: buffer)
return Sunras.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()