Files
rosetta/tests/image/ani.luau
T
4DBug 00d47096ed ws-tex: image/ani (animated cursors -> frames through the decodeImage hook; write through encodeImage)
seq / rate steps, jiffy delays, CUR hotspots, INFO title; AF_ICON clear is UNSUPPORTED. Tested against
Pillow-written ICO / CUR frames assembled into ANI files.
2026-10-31 15:09:35 -05:00

73 lines
3.1 KiB
Luau

-- image/ani tests: animated cursors around Pillow-written ICO / CUR frames (seq + rate, plain, cursor hotspots, INFO
-- title) against Pillow's frame decodes and the written steps / delays; AF_ICON clear and a missing hook are
-- UNSUPPORTED; the encoder through the ICO hook round-trips; the fuzz no-throw pass. Fixtures: tools/fixtures/ani.py.
local T = require("@tests/T")
local Ani = require("@formats/image/ani")
local Ico = require("@formats/image/ico")
local Png = require("@formats/image/png")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/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
-- re-encode through the ICO hook
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()