70 lines
2.8 KiB
Luau
70 lines
2.8 KiB
Luau
-- image/ani tests against Pillow (tools/fixtures/ani.py).
|
|
local T = require("@tests/T")
|
|
local Ani = require("@rosetta/image/ani")
|
|
local Ico = require("@rosetta/image/ico")
|
|
local Png = require("@rosetta/image/png")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local R = require("@tests/image/_raster")
|
|
local Fx = require("@tests/gen/ani")
|
|
|
|
local t = T.suite("ani")
|
|
t:eq(Ani.name, "ani", "name")
|
|
t:check(Ani.caps.decode and Ani.caps.encode, "caps")
|
|
|
|
local hooks = {
|
|
decodeImage = function(b: buffer): (any, any)
|
|
-- Pillow writes the ICO entries as PNG: the ICO decoder gets its own hook
|
|
return Ico.decode(Bytes.reader(b), { decodeImage = function(p: buffer): (any, any)
|
|
return Png.decode(Bytes.reader(p))
|
|
end })
|
|
end,
|
|
encodeImage = function(img: any, _fmt: string): (any, any)
|
|
return Ico.encode(img)
|
|
end,
|
|
}
|
|
|
|
for _, c in Fx.cases do
|
|
local img, err = Ani.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
|
|
if c.unsupported then
|
|
t:check(img == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED")
|
|
continue
|
|
end
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:check(img.width == c.w and img.height == c.h, c.name .. ": size")
|
|
t:eq(#img.frames, #c.steps, c.name .. ": steps")
|
|
for i, s in c.steps do
|
|
local f = img.frames[i]
|
|
t:check(math.abs(f.delay - s.delay) < 1e-9, c.name .. ": step " .. i .. " delay")
|
|
t:eq(R.maxDiff(f.image.pixels, buffer.fromstring(c.frames[s.frame]), c.w * c.h), 0, c.name .. ": step " .. i .. " pixels")
|
|
end
|
|
t:eq(img.loops, 0, c.name .. ": loops forever")
|
|
if c.title then
|
|
t:eq(img.meta.text.title, c.title, c.name .. ": title")
|
|
end
|
|
if c.hotspot then
|
|
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
|
|
local enc, eerr = Ani.encode(img, hooks)
|
|
t:check(enc, c.name .. ": encode " .. tostring(eerr))
|
|
local back = Ani.decode(Bytes.reader(enc :: buffer), hooks) :: any
|
|
t:eq(#back.frames, #img.frames, c.name .. ": re-encoded steps")
|
|
for i, f in back.frames do
|
|
t:eq(buffer.tostring(f.image.pixels), buffer.tostring(img.frames[i].image.pixels), c.name .. ": re-encoded frame " .. i)
|
|
t:check(math.abs(f.delay - img.frames[i].delay) < 1 / 120, c.name .. ": re-encoded delay " .. i)
|
|
end
|
|
end
|
|
do
|
|
local c = Fx.cases[1]
|
|
t:eq(Ani.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
|
t:eq(Ani.sniff(Bytes.reader(buffer.fromstring("RIFF\0\0\0\0WAVE"))), 0, "sniff WAVE")
|
|
local _, e = Ani.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(e and e.code == Err.UNSUPPORTED, "no hook")
|
|
local _, e2 = Ani.encode(Ico.decode(Bytes.reader(buffer.fromstring(c.data))) or ({ pixels = buffer.create(4) } :: any))
|
|
t:check(e2 ~= nil, "encode without the hook")
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Ani.decode(Bytes.reader(b), hooks)
|
|
end, R.samples(Fx.cases), { mutations = 10 })
|
|
t:done()
|