163 lines
7.2 KiB
Luau
163 lines
7.2 KiB
Luau
-- image/png APNG tests against ffmpeg's composited canvases (tools/fixtures/apng.py).
|
|
local T = require("@tests/T")
|
|
local Png = require("@rosetta/image/png")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Cases = require("@tests/gen/apng_cases")
|
|
|
|
local t = T.suite("apng")
|
|
|
|
t:check(Png.caps.animation == true, "caps.animation")
|
|
|
|
local function decode(data: any, opts: any?): (any, any)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
return Png.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
local DISPOSE = { [0] = "none", [1] = "background", [2] = "previous" }
|
|
|
|
local function canvasCrc(b: buffer, opaque: boolean): number
|
|
if opaque then
|
|
local c = buffer.create(buffer.len(b))
|
|
buffer.copy(c, 0, b)
|
|
for i = 3, buffer.len(c) - 1, 4 do
|
|
buffer.writeu8(c, i, 255)
|
|
end
|
|
b = c
|
|
end
|
|
return Crc32.of(b)
|
|
end
|
|
|
|
local function same(a: Types.Image, b: Types.Image): boolean
|
|
return a.width == b.width and a.height == b.height and buffer.tostring(a.pixels) == buffer.tostring(b.pixels)
|
|
end
|
|
|
|
for _, c in Cases do
|
|
local img, err = decode(c.data, { checkCrc = true })
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:eq(img.width, c.width, c.name .. ": width")
|
|
t:check(img.frames ~= nil, c.name .. ": frames")
|
|
t:eq(#img.frames, #c.crcs, c.name .. ": frame count")
|
|
t:eq(img.loops, c.plays, c.name .. ": loops")
|
|
if c.frames then
|
|
for i, e in c.frames do
|
|
local f = img.frames[i]
|
|
t:check(f.x == e.x and f.y == e.y and f.image.width == e.w and f.image.height == e.h, c.name .. ": frame " .. i .. " rect")
|
|
t:check(math.abs(f.delay - e.num / (if e.den == 0 then 100 else e.den)) < 1e-12, c.name .. ": frame " .. i .. " delay")
|
|
t:eq(f.dispose, DISPOSE[e.dispose], c.name .. ": frame " .. i .. " dispose")
|
|
t:eq(f.blend, if e.blend == 1 then "over" else "source", c.name .. ": frame " .. i .. " blend")
|
|
end
|
|
end
|
|
if c.defaultCrc then
|
|
t:eq(Crc32.of(img.pixels), c.defaultCrc, c.name .. ": hidden default image")
|
|
t:check(img.meta.apngDefaultHidden == true, c.name .. ": apngDefaultHidden")
|
|
else
|
|
t:check(img.meta.apngDefaultHidden == false, c.name .. ": default image is frame 1")
|
|
t:check(buffer.tostring(img.pixels) == buffer.tostring(img.frames[1].image.pixels), c.name .. ": pixels = frame 1")
|
|
end
|
|
local canv, cerr = Png.composite(img)
|
|
t:check(canv, c.name .. ": composite " .. tostring(cerr))
|
|
for i, want in c.crcs do
|
|
t:eq(canvasCrc(canv[i].pixels, c.opaque), want, c.name .. ": composited frame " .. i .. (if c.ffmpeg then " vs ffmpeg" else " vs reference"))
|
|
end
|
|
local enc, eerr = Png.encode(img)
|
|
t:check(enc, c.name .. ": encode " .. tostring(eerr))
|
|
local back, berr = Png.decode(Bytes.reader(enc :: buffer), { checkCrc = true })
|
|
t:check(back, c.name .. ": decode our APNG " .. tostring(berr))
|
|
t:check(same(back, img), c.name .. ": round trip default image")
|
|
t:eq(back.loops, img.loops, c.name .. ": round trip loops")
|
|
t:eq(#back.frames, #img.frames, c.name .. ": round trip frame count")
|
|
t:eq(back.meta.apngDefaultHidden, img.meta.apngDefaultHidden, c.name .. ": round trip hidden default")
|
|
for i, f in img.frames do
|
|
local g = back.frames[i]
|
|
t:check(same(g.image, f.image) and g.x == f.x and g.y == f.y and g.dispose == f.dispose and g.blend == f.blend, c.name .. ": round trip frame " .. i)
|
|
t:check(math.abs(g.delay - f.delay) < 1e-9, c.name .. ": round trip delay " .. i)
|
|
end
|
|
end
|
|
t:note("%d APNGs match the reference canvases and round-trip", #Cases)
|
|
|
|
do
|
|
local function solid(w: number, h: number, r: number, g: number, b: number, a: number): Types.Image
|
|
local im = Types.Image.new(w, h)
|
|
for i = 0, w * h - 1 do
|
|
buffer.writeu8(im.pixels, i * 4, r)
|
|
buffer.writeu8(im.pixels, i * 4 + 1, g)
|
|
buffer.writeu8(im.pixels, i * 4 + 2, b)
|
|
buffer.writeu8(im.pixels, i * 4 + 3, a)
|
|
end
|
|
return im
|
|
end
|
|
local base = solid(8, 6, 10, 20, 30, 255)
|
|
base.frames = {
|
|
{ image = base, delay = 0.1, x = 0, y = 0, dispose = "none", blend = "source" },
|
|
{ image = solid(3, 2, 200, 0, 0, 128), delay = 1 / 30, x = 5, y = 4, dispose = "background", blend = "over" },
|
|
{ image = solid(1, 1, 0, 255, 0, 255), delay = 70, x = 7, y = 0, dispose = "previous", blend = "source" },
|
|
}
|
|
base.loops = 5
|
|
local enc = Png.encode(base) :: buffer
|
|
local back = decode(enc)
|
|
t:check(back and #back.frames == 3 and back.loops == 5, "built APNG decodes")
|
|
t:check(math.abs(back.frames[2].delay - 1 / 30) < 1e-9 and back.frames[3].delay == 70 and back.frames[1].delay == 0.1, "delays")
|
|
t:eq(back.meta.source.colorType, "rgba", "colour type covers every frame (alpha in frame 2 only)")
|
|
local canv = Png.composite(back) :: { Types.Image }
|
|
-- frame 2 over: 128 alpha red over opaque (10, 20, 30)
|
|
local o = (4 * 8 + 5) * 4
|
|
t:eq(buffer.readu8(canv[2].pixels, o + 3), 255, "over: alpha")
|
|
t:eq(buffer.readu8(canv[2].pixels, o), (128 * 200 + 127 * 10 + 128) * 257 // 65536, "over: red")
|
|
t:eq(buffer.readu32(canv[3].pixels, o), 0, "background dispose cleared frame 2's rect")
|
|
local still = solid(4, 4, 1, 2, 3, 255)
|
|
local plain = decode(Png.encode(still) :: buffer)
|
|
t:check(plain.frames == nil, "still image: no acTL")
|
|
local one = decode(Png.encode(still, { apng = true }) :: buffer)
|
|
t:check(one.frames and #one.frames == 1 and same(one.frames[1].image, still), "opts.apng: a one-frame APNG")
|
|
local function bad(frames: any, label: string)
|
|
local im = solid(4, 4, 0, 0, 0, 255)
|
|
im.frames = frames
|
|
local r, e = Png.encode(im)
|
|
t:check(r == nil and e and e.code == Err.ARG, label .. ": " .. tostring(e))
|
|
end
|
|
bad({ { image = still, x = 0, y = 0 }, { image = still, x = 1, y = 0 } }, "frame outside the canvas")
|
|
bad({ { image = still }, { image = {} } }, "frame without an image")
|
|
bad({ { image = still }, { image = still, dispose = "explode" } }, "bad dispose")
|
|
bad({ { image = still }, { image = still, blend = "add" } }, "bad blend")
|
|
bad({ { image = still }, { image = still, delay = -1 } }, "negative delay")
|
|
end
|
|
|
|
do
|
|
local c = Cases[1]
|
|
local s = c.data
|
|
-- find the second fcTL and bump its sequence number (fix the CRC so checkCrc does not mask it)
|
|
local first = string.find(s, "fcTL", 1, true) :: number
|
|
local second = string.find(s, "fcTL", first + 4, true) :: number
|
|
local b = buffer.fromstring(s)
|
|
local d = second + 3 -- 0-based offset of the chunk data
|
|
buffer.writeu32(b, d, bit32.byteswap(bit32.byteswap(buffer.readu32(b, d)) + 1))
|
|
local _, e = Png.decode(Bytes.reader(b))
|
|
t:check(e and e.code == Err.CORRUPT, "bad sequence: " .. tostring(e))
|
|
local b2 = buffer.fromstring(s)
|
|
buffer.writeu32(b2, d + 12, bit32.byteswap(c.width)) -- x offset = the canvas width
|
|
local _, e2 = Png.decode(Bytes.reader(b2))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "frame outside the canvas: " .. tostring(e2))
|
|
local _, e3 = decode(s, { maxPixels = c.width * c.height + 1 })
|
|
t:check(e3 and e3.code == Err.LIMIT, "maxPixels counts the frames: " .. tostring(e3))
|
|
t:check(Png.composite({} :: any) == nil, "composite of a non-image")
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for _, c in Cases do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local img = Png.decode(Bytes.reader(b))
|
|
if img and img.frames then
|
|
Png.composite(img)
|
|
end
|
|
return img
|
|
end, samples, { mutations = 30, keepHeader = 8 })
|
|
end
|
|
|
|
t:done()
|