Files
rosetta/tests/image/gif.luau
T

210 lines
8.3 KiB
Luau

-- image/gif tests against ImageMagick's decodes (tools/fixtures/gif.py).
local T = require("@tests/T")
local Gif = require("@rosetta/image/gif")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/gif")
local t = T.suite("gif")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Gif.decode(Bytes.reader(b), opts)
end
-- rgba8 with alpha-0 pixels zeroed (the fixtures' convention)
local function norm(px: buffer): buffer
local n = buffer.len(px)
local c = buffer.create(n)
buffer.copy(c, 0, px)
for o = 0, n - 4, 4 do
if buffer.readu8(c, o + 3) == 0 then
buffer.writeu32(c, o, 0)
end
end
return c
end
t:eq(Gif.name, "gif", "name")
t:eq(Gif.category, "image", "category")
t:check(Gif.caps.decode and Gif.caps.encode and Gif.caps.animation, "caps")
local nframes = 0
for _, c in Fx do
local img, err = decode(c.data)
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.width and img.height == c.height and img.format == "rgba8", c.name .. ": canvas")
t:check(not img.meta.truncated, c.name .. ": flagged truncated")
t:eq(Crc32.of(norm(img.pixels)), c.first, c.name .. ": first frame composited")
local frames = img.frames or { { image = img, x = 0, y = 0, delay = 0, dispose = "none" } }
t:eq(#frames, #c.frames, c.name .. ": frame count")
for i, rf in c.frames do
local f = frames[i]
local label = c.name .. " frame " .. i
if img.frames then
t:check(f.image.width == rf.w and f.image.height == rf.h and f.x == rf.x and f.y == rf.y, label .. ": rectangle")
t:eq(math.floor(f.delay * 100 + 0.5), rf.delay, label .. ": delay")
local want = if rf.dispose == "undefined" then "none" else rf.dispose
t:eq(f.dispose, want, label .. ": dispose")
t:eq(Crc32.of(norm(f.image.pixels)), rf.crc, label .. ": pixels")
else
t:eq(Crc32.of(norm(img.pixels)), rf.crc, label .. ": pixels")
end
nframes += 1
end
if c.loops then
t:eq(img.loops, if c.loops == 0 then 0 else c.loops + 1, c.name .. ": loops (plays)")
end
if c.comment then
t:eq(img.meta.text and img.meta.text.comment, c.comment, c.name .. ": comment")
end
end
t:note("%d files, %d frames match ImageMagick", #Fx, nframes)
do
local c = Fx[1]
t:eq(Gif.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff a GIF")
t:eq(Gif.sniff(Bytes.reader(buffer.fromstring("\137PNG\r\n\26\n"))), 0, "sniff a PNG")
t:eq(Gif.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
local img = decode(c.data)
t:check(img.meta.source.format == "gif" and img.meta.source.version == "89a", "meta.source")
t:check(img.meta.palette and #img.meta.palette >= 2 and #img.meta.palette[1] == 4, "meta.palette")
local _, e = decode(c.data, { maxPixels = 100 })
t:check(e and e.code == Err.LIMIT, "maxPixels: " .. tostring(e))
local _, e2 = decode("GIF89a")
t:check(e2 and e2.code == Err.TRUNCATED, "header only: " .. tostring(e2))
local _, e3 = decode("NOTGIF1234567")
t:check(e3 and e3.code == Err.CORRUPT, "junk: " .. tostring(e3))
-- a truncated file keeps its frames
for _, f in Fx do
if f.name == "anim_none" then
local cut = string.sub(f.data, 1, #f.data - 20)
local part = decode(cut)
t:check(part and part.meta.truncated, "truncated animation decodes partially")
local one = decode(f.data, { maxFrames = 1 })
t:check(one and one.frames == nil, "maxFrames = 1")
local big = buffer.create(#f.data + 9)
buffer.writestring(big, 5, f.data)
local sub = Gif.decode(Bytes.reader(big, 5, #f.data))
t:check(sub and sub.frames and #sub.frames == 4, "decode from a sub-range")
end
end
end
local rand = T.rng(17)
local function image(w: number, h: number, colors: number?, alpha: boolean?): Types.Image
local img = Types.Image.new(w, h)
for i = 0, w * h - 1 do
local x, y = i % w, i // w
local r, g, b
if colors then
local k = (x // 3 + y // 2 * 7 + rand(2)) % colors
r, g, b = (k * 53) % 256, (k * 97) % 256, (k * 31) % 256
else
r, g, b = (x * 255) // math.max(1, w - 1), (y * 255) // math.max(1, h - 1), ((x + y) * 3 + rand(16)) % 256
end
local a = if alpha and (x * x + y * y) % 11 == 0 then 0 else 255
buffer.writeu32(img.pixels, i * 4, r + g * 256 + b * 65536 + a * 16777216)
end
return img
end
local function same(a: buffer, b: buffer): boolean
return buffer.tostring(norm(a)) == buffer.tostring(norm(b))
end
local function psnr(a: buffer, b: buffer): number
local se, n = 0, 0
for o = 0, buffer.len(a) - 4, 4 do
for k = 0, 2 do
local d = buffer.readu8(a, o + k) - buffer.readu8(b, o + k)
se += d * d
n += 1
end
end
if se == 0 then
return 99
end
return 10 * math.log10(255 * 255 * n / se)
end
do
for _, colors in { 2, 3, 16, 200, 256 } do
for _, alpha in { false, true } do
local ncol = if alpha and colors == 256 then 255 else colors
local img = image(37, 23, ncol, alpha)
local gif, err = Gif.encode(img)
t:check(gif, "encode: " .. tostring(err))
local back = decode(gif)
t:check(back and same(back.pixels, img.pixels), string.format("%d colours alpha=%s: lossless round trip", ncol, tostring(alpha)))
t:check(back.frames == nil, "single image")
end
end
-- more than 256 colours: quantized, every method, with and without dithering
local photo = image(96, 64)
for _, method in { "mediancut", "octree" } do
for _, dither in { false, true } do
local gif = Gif.encode(photo, { method = method, dither = dither }) :: buffer
local back = decode(gif)
local p = psnr(back.pixels, photo.pixels)
t:check(p > (if dither then 28 else 30), string.format("%s dither=%s: PSNR %.1f", method, tostring(dither), p))
t:note("encode %s dither=%s: %d bytes, PSNR %.1f dB", method, tostring(dither), buffer.len(gif), p)
end
end
local small = Gif.encode(photo, { maxColors = 4 }) :: buffer
local sb = decode(small)
t:check(sb.meta.palette and #sb.meta.palette <= 4, "maxColors = 4")
local anim = image(40, 30, 50)
anim.frames = {
{ image = image(40, 30, 50), x = 0, y = 0, delay = 0.1, dispose = "none", blend = "source" },
{ image = image(10, 12, 7, true), x = 5, y = 6, delay = 0.25, dispose = "background", blend = "over" },
{ image = image(20, 5), x = 20, y = 25, delay = 1.5, dispose = "previous", blend = "source" },
}
anim.loops = 4
anim.meta.text = { comment = "hello gif" }
for _, global in { false, true } do
local gif, err = Gif.encode(anim, { globalPalette = global })
t:check(gif, "animation encode: " .. tostring(err))
local back = decode(gif)
t:check(back.frames and #back.frames == 3, "animation frames")
t:eq(back.loops, 4, "loops")
t:eq(back.meta.text.comment, "hello gif", "comment")
for i, f in anim.frames :: { Types.Frame } do
local g = back.frames[i]
t:check(g.x == f.x and g.y == f.y and g.image.width == f.image.width and g.image.height == f.image.height, "frame rectangle " .. i)
t:eq(g.delay, f.delay, "frame delay " .. i)
t:eq(g.dispose, f.dispose, "frame dispose " .. i)
end
t:check(same(back.frames[1].image.pixels, anim.frames[1].image.pixels), "frame 1 lossless")
t:check(same(back.frames[2].image.pixels, anim.frames[2].image.pixels), "frame 2 lossless (transparency)")
t:eq(back.frames[2].blend, "over", "frame 2 blend")
t:check(psnr(back.frames[3].image.pixels, anim.frames[3].image.pixels) > 25, "frame 3 quantized")
t:eq(back.meta.source.localPalettes, if global then 0 else 3, "palettes")
end
t:eq(Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), "deterministic")
local _, e = Gif.encode({} :: any)
t:check(e and e.code == Err.ARG, "encode of a non-image")
local _, e2 = Gif.encode(photo, { maxColors = 1 })
t:check(e2 and e2.code == Err.ARG, "maxColors = 1")
local _, e3 = Gif.encode(photo, { method = "kmeans" } :: any)
t:check(e3 and e3.code == Err.ARG, "unknown method")
local _, e4 = Gif.encode(Types.Image.new(70000, 1))
t:check(e4 and e4.code == Err.ARG, "too wide")
end
do
local samples = {}
for _, c in Fx do
if #c.data < 40000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Gif.decode(Bytes.reader(b))
end, samples, { mutations = 12, keepHeader = 6 })
t:fuzz("decode (header too)", function(b: buffer)
return Gif.decode(Bytes.reader(b), { maxPixels = 1e6 })
end, samples, { mutations = 4, garbage = 50 })
end
t:done()