Files
rosetta/tests/image/ilbm.luau
T

70 lines
2.5 KiB
Luau

-- image/ilbm tests against netpbm's ilbmtoppm (tools/fixtures/ilbm.py).
local T = require("@tests/T")
local Ilbm = require("@rosetta/image/ilbm")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local R = require("@tests/image/_raster")
local Fx = require("@tests/gen/ilbm")
local t = T.suite("ilbm")
t:eq(Ilbm.name, "ilbm", "name")
t:check(Ilbm.caps.decode and Ilbm.caps.encode, "caps")
local function check(c: any)
local img, err = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h, c.name .. ": size")
local ref = buffer.fromstring(c.rgba)
local bad, badA = 0, 0
for i = 0, c.w * c.h - 1 do
local transparent = c.transparent and c.transparent[i + 1]
local v = buffer.readu32(img.pixels, i * 4)
if transparent then
badA += if v == 0 then 0 else 1
else
for k = 0, 3 do
if math.abs(buffer.readu8(img.pixels, i * 4 + k) - buffer.readu8(ref, i * 4 + k)) > (c.tol or 0) then
bad += 1
break
end
end
end
end
t:eq(bad, 0, c.name .. ": colours against ilbmtoppm")
t:eq(badA, 0, c.name .. ": transparent pixels")
return img
end
for _, c in Fx.cases do
check(c)
if not c.transparent and not c.tol then
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), nil, c.name .. " re-encoded")
R.roundTrip(t, Ilbm, R.image(c.w, c.h, c.rgba), { compress = false }, c.name .. " re-encoded uncompressed")
end
end
for _, o in Fx.ours do
check(o)
end
t:note("%d files and %d of ours match ilbmtoppm", #Fx.cases, #Fx.ours)
do
local img = R.image(3, 2, string.rep("\1\2\3\128", 6))
local enc = R.roundTrip(t, Ilbm, img, nil, "alpha: 32 planes") :: buffer
t:eq(Ilbm.sniff(Bytes.reader(enc)), 1, "sniff")
t:eq(Ilbm.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\4AIFF"))), 0, "sniff AIFF")
local back = Ilbm.decode(Bytes.reader(enc)) :: any
t:eq(back.meta.source.planes, 32, "32 planes")
local _, e = Ilbm.decode(Bytes.reader(buffer.fromstring("FORM\0\0\0\4ILBM")))
t:check(e and e.code == Err.CORRUPT, "no BMHD")
local _, e2 = Ilbm.decode(Bytes.reader(enc), { maxPixels = 2 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
for _, c in Fx.cases do
if c.name == "hand_transparent.iff" then
local im = Ilbm.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:eq(im.meta.text and im.meta.text.name, "tiny", "NAME chunk")
end
end
end
t:fuzz("decode", function(b: buffer)
return Ilbm.decode(Bytes.reader(b))
end, R.samples(Fx.cases), { mutations = 6 })
t:done()