imgA-gif: image/gif (read + write, animation, transparency, loops, comments) and codecs/Quantize (median cut, octree, Floyd-Steinberg); ImageMagick / Pillow fixtures
This commit is contained in:
1 parent
ba91879bc5
commit
603689bc14
8 files changed
+1549
No files matched your search
@@ -0,0 +1,135 @@
|
||||
-- codecs/Quantize tests: palette size bounds for every method and colour budget, the exact (lossless) palette at
|
||||
-- <= maxColors colours, transparency handling, PSNR of quantized images above thresholds, determinism of both
|
||||
-- methods and of Floyd-Steinberg dithering (the same CRC on both runtimes), bad arguments, and a no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local Quantize = require("@formats/codecs/Quantize")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local t = T.suite("quantize")
|
||||
local rand = T.rng(5)
|
||||
|
||||
local function photo(w: number, h: number): buffer
|
||||
local px = buffer.create(w * h * 4)
|
||||
for i = 0, w * h - 1 do
|
||||
local x, y = i % w, i // w
|
||||
local r = (x * 255) // (w - 1)
|
||||
local g = (y * 255) // (h - 1)
|
||||
local b = math.floor(128 + 100 * math.sin(x * 0.11) * math.cos(y * 0.07)) + rand(9)
|
||||
buffer.writeu32(px, i * 4, r + g * 256 + math.clamp(b, 0, 255) * 65536 + 0xFF000000)
|
||||
end
|
||||
return px
|
||||
end
|
||||
|
||||
local function render(idx: buffer, pal: { { number } }, n: number): buffer
|
||||
local out = buffer.create(n * 4)
|
||||
for i = 0, n - 1 do
|
||||
local c = pal[buffer.readu8(idx, i) + 1]
|
||||
buffer.writeu32(out, i * 4, c[1] + c[2] * 256 + c[3] * 65536 + 0xFF000000)
|
||||
end
|
||||
return out
|
||||
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
|
||||
return if se == 0 then 99 else 10 * math.log10(255 * 255 * n / se)
|
||||
end
|
||||
|
||||
local W, H = 128, 96
|
||||
local px = photo(W, H)
|
||||
-- palette bounds and quality
|
||||
local MIN_PSNR = { [256] = 30, [64] = 24, [16] = 17, [2] = 10 }
|
||||
for _, method in { "mediancut", "octree" } do
|
||||
for _, k in { 2, 16, 64, 256 } do
|
||||
local pal, exact = Quantize.palette(px, W, H, k, { method = method })
|
||||
t:check(#pal >= 1 and #pal <= k, string.format("%s %d: palette size %d", method, k, #pal))
|
||||
t:check(not exact, method .. ": not exact")
|
||||
for _, c in pal do
|
||||
t:check(#c == 4 and c[4] == 255 and c[1] % 1 == 0 and c[1] >= 0 and c[1] <= 255, "palette entries")
|
||||
end
|
||||
for _, dither in { false, true } do
|
||||
local idx = Quantize.map(px, W, H, pal, { dither = dither })
|
||||
t:eq(buffer.len(idx), W * H, "index buffer size")
|
||||
local p = psnr(render(idx, pal, W * H), px)
|
||||
local want = MIN_PSNR[k] - (if dither then 4 else 0)
|
||||
t:check(p > want, string.format("%s %d dither=%s: PSNR %.1f > %d", method, k, tostring(dither), p, want))
|
||||
if k == 256 or k == 16 then
|
||||
t:note("%-9s %3d colours dither=%-5s PSNR %.1f dB, crc %08x", method, k, tostring(dither), p, Crc32.of(idx))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- exact palettes: lossless
|
||||
do
|
||||
local n = 50 * 40
|
||||
local b = buffer.create(n * 4)
|
||||
for i = 0, n - 1 do
|
||||
local k = (i * 7 + i // 50) % 200
|
||||
buffer.writeu32(b, i * 4, (k * 3) % 256 + ((k * 11) % 256) * 256 + k * 65536 + 0xFF000000)
|
||||
end
|
||||
local pal, exact = Quantize.palette(b, 50, 40, 256)
|
||||
t:check(exact and #pal == 200, "exact palette of 200 colours: " .. #pal)
|
||||
local idx = Quantize.map(b, 50, 40, pal, { dither = true })
|
||||
t:check(buffer.tostring(render(idx, pal, n)) == buffer.tostring(b), "exact palette maps losslessly (dither too)")
|
||||
local pal2, exact2 = Quantize.palette(b, 50, 40, 199)
|
||||
t:check(not exact2 and #pal2 <= 199, "199 colours is not exact")
|
||||
local i3, p3, e3 = Quantize.quantize(b, 50, 40, 256, { dither = true })
|
||||
t:check(e3 and #p3 == 200 and buffer.tostring(render(i3, p3, n)) == buffer.tostring(b), "quantize() exact")
|
||||
end
|
||||
|
||||
-- transparency
|
||||
do
|
||||
local b = buffer.create(16 * 4)
|
||||
for i = 0, 15 do
|
||||
buffer.writeu32(b, i * 4, if i % 3 == 0 then 0x10FFFFFF else 0xFF0000FF + i * 256)
|
||||
end
|
||||
local pal, exact = Quantize.palette(b, 4, 4, 255)
|
||||
t:check(exact and #pal == 10, "transparent pixels are not in the palette")
|
||||
table.insert(pal, { 0, 0, 0, 0 })
|
||||
local idx = Quantize.map(b, 4, 4, pal, { transparentIndex = #pal - 1, dither = true })
|
||||
for i = 0, 15 do
|
||||
local v = buffer.readu8(idx, i)
|
||||
t:check((i % 3 == 0) == (v == #pal - 1), "transparent index " .. i)
|
||||
end
|
||||
end
|
||||
|
||||
-- determinism (also across runtimes: these CRCs are pinned)
|
||||
do
|
||||
local pal = Quantize.palette(px, W, H, 64, { method = "mediancut" })
|
||||
local a = Crc32.of(Quantize.map(px, W, H, pal, { dither = true }))
|
||||
local pal2 = Quantize.palette(px, W, H, 64, { method = "mediancut" })
|
||||
t:eq(Crc32.of(Quantize.map(px, W, H, pal2, { dither = true })), a, "mediancut + dither deterministic")
|
||||
t:eq(a, 0xd2c76537, "mediancut 64 + dither: the pinned CRC (the same on both runtimes)")
|
||||
local o1 = Quantize.palette(px, W, H, 37, { method = "octree" })
|
||||
local o2 = Quantize.palette(px, W, H, 37, { method = "octree" })
|
||||
t:check(#o1 == #o2 and o1[#o1][1] == o2[#o2][1], "octree deterministic")
|
||||
end
|
||||
|
||||
-- bad arguments never throw
|
||||
do
|
||||
t:eq(#Quantize.palette(nil :: any, 1, 1, 4), 0, "nil pixels")
|
||||
t:eq(#Quantize.palette(buffer.create(3), 1, 1, 4), 0, "short buffer")
|
||||
t:eq(buffer.len(Quantize.map(px, W, H, {})), W * H, "empty palette")
|
||||
t:eq(buffer.len(Quantize.map(buffer.create(1), 2, 2, { { 1, 2, 3, 255 } })), 0, "short buffer map")
|
||||
local ok = pcall(function()
|
||||
for i = 1, 20 do
|
||||
local w, h = 1 + rand(20), 1 + rand(20)
|
||||
local b = buffer.create(w * h * 4)
|
||||
for k = 0, w * h * 4 - 1 do
|
||||
buffer.writeu8(b, k, rand(256))
|
||||
end
|
||||
local pal = Quantize.palette(b, w, h, 1 + rand(256), { method = if i % 2 == 0 then "octree" else "mediancut" })
|
||||
Quantize.map(b, w, h, pal, { dither = i % 3 == 0, transparentIndex = if i % 4 == 0 then 0 else nil })
|
||||
end
|
||||
end)
|
||||
t:check(ok, "random images never throw")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,221 @@
|
||||
-- image/gif tests: GIFs from ImageMagick and Pillow (static, interlaced, 2..256 colours, grey, transparency,
|
||||
-- animations with every disposal, sub-rectangles, local colour tables, loop counts, comments) against ImageMagick's
|
||||
-- decode of every frame; the encoder (lossless round trips at <= 256 colours, quantized PSNR above, animations with
|
||||
-- frames / delays / disposal / transparency / loops, options); error codes; the fuzz no-throw pass.
|
||||
-- Fixtures: tools/fixtures/gif.py.
|
||||
local T = require("@tests/T")
|
||||
local Gif = require("@formats/image/gif")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/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
|
||||
|
||||
-- the contract
|
||||
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")
|
||||
|
||||
-- every fixture against ImageMagick
|
||||
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)
|
||||
|
||||
-- sniff, meta, limits, errors
|
||||
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
|
||||
|
||||
-- the encoder
|
||||
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")
|
||||
-- animation: frames, delays, disposal, transparency, loops, comment, local / global palettes
|
||||
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
|
||||
-- deterministic output
|
||||
t:eq(Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), Crc32.of(Gif.encode(photo, { dither = true }) :: buffer), "deterministic")
|
||||
-- bad arguments
|
||||
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
|
||||
|
||||
-- fuzz
|
||||
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()
|
||||
Reference in new issue
Block a user