merge ws/webp

This commit is contained in:
4DBug committed 2026-10-31 09:12:40 -05:00
commit 646d18f056
20 files changed
+5607

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
-- codecs/Vp8 tests: the VP8 key frames of the WebP fixtures (cwebp at many settings and libvpx through ffmpeg, with
-- 1..8 token partitions) decoded through the codec API: the yuv420p planes against dwebp -yuv, toRgba against
-- dwebp -pam (fancy upsampling) and -nofancy, the decoder object over successive frames, headers, error codes and the
-- fuzz no-throw pass on raw frames. Fixtures: tools/fixtures/webp.py.
local T = require("@tests/T")
local Vp8 = require("@formats/codecs/Vp8")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Cases = require("@tests/gen/webp_cases")
local t = T.suite("vp8")
local function chunk(data: string): (buffer?, number, number)
if string.sub(data, 13, 16) ~= "VP8 " then
return nil, 0, 0
end
local b = buffer.fromstring(data)
return b, 20, buffer.readu32(b, 16)
end
local n, vpx = 0, 0
local frames = {}
local dec = Vp8.newDecoder()
for _, c in Cases do
local b, off, len = chunk(c.data)
if b then
local img, err = Vp8.decodeFrame(b, off, len)
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.format == "yuv420p" and img.width == c.width and img.height == c.height, c.name .. ": size")
t:eq(Crc32.of(img.pixels), c.yuv, c.name .. ": yuv420p (dwebp -yuv)")
local rgba = Vp8.toRgba(img) :: any
t:eq(Crc32.of(rgba.pixels), c.crc, c.name .. ": rgba (dwebp -pam)")
local nf = Vp8.toRgba(img, false) :: any
t:eq(Crc32.of(nf.pixels), c.nofancy, c.name .. ": rgba (dwebp -nofancy -pam)")
-- the same frame through one long-lived decoder
local again = dec:decodeFrame(b, off, len)
t:check(again and Crc32.of(again.pixels) == c.yuv, c.name .. ": decoder object")
local y, u, v, w, h = Vp8.decodePlanes(b, off, len)
t:check(y and w == c.width and h == c.height and buffer.len(u) == buffer.len(v :: buffer), c.name .. ": decodePlanes")
local info = Vp8.info(b, off, len)
t:check(info and info.keyFrame and info.show and info.width == c.width, c.name .. ": info")
n += 1
if string.find(c.name, "vpx", 1, true) then
vpx += 1
end
if #frames < 10 and len < 6000 then
table.insert(frames, buffer.readstring(b, off, len))
end
end
end
t:note("%d VP8 key frames (%d from libvpx) match dwebp: planes, fancy and point upsampling", n, vpx)
-- errors
do
local f = buffer.fromstring(frames[1])
local inter = buffer.fromstring(frames[1])
buffer.writeu8(inter, 0, bit32.bor(buffer.readu8(inter, 0), 1))
local _, e1 = Vp8.decodeFrame(inter)
t:check(e1 and e1.code == Err.UNSUPPORTED, "inter frame: " .. tostring(e1))
local bad = buffer.fromstring(frames[1])
buffer.writeu8(bad, 3, 0)
local _, e2 = Vp8.decodeFrame(bad)
t:check(e2 and e2.code == Err.CORRUPT, "bad start code: " .. tostring(e2))
local _, e3 = Vp8.decodeFrame(f, 0, 12)
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
local _, e4 = Vp8.decodeFrame(f, 0, buffer.len(f), { maxPixels = 4 })
t:check(e4 and e4.code == Err.LIMIT, "maxPixels: " .. tostring(e4))
local _, e5 = Vp8.toRgba({} :: any)
t:check(e5 and e5.code == Err.ARG, "toRgba of a non-yuv image")
end
-- fuzz on raw frames
t:fuzz("decodeFrame", function(b: buffer)
return Vp8.decodeFrame(b, 0, buffer.len(b), { maxPixels = 2 ^ 20 })
end, frames, { mutations = 12, keepHeader = 10 })
t:fuzz("decodeFrame (headers too)", function(b: buffer)
return Vp8.decodeFrame(b, 0, buffer.len(b), { maxPixels = 2 ^ 20 })
end, frames, { mutations = 4, garbage = 100 })
t:done()
+120
View File
@@ -0,0 +1,120 @@
-- codecs/Vp8L tests: the VP8L bitstreams of the WebP fixtures (cwebp -z 0..9, -exact, near-lossless) decoded through
-- the codec API against dwebp, the encoder at every effort on generated images (gradients, noise, palettes of 1..300
-- colours, odd sizes, alpha), headerless alpha streams, error codes, and the fuzz no-throw pass on raw bitstreams.
-- Fixtures: tools/fixtures/webp.py.
local T = require("@tests/T")
local Vp8L = require("@formats/codecs/Vp8L")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Cases = require("@tests/gen/webp_cases")
local t = T.suite("vp8l")
-- the VP8L chunk of a simple-format file
local function chunk(data: string): (buffer?, number, number)
if string.sub(data, 13, 16) ~= "VP8L" then
return nil, 0, 0
end
local b = buffer.fromstring(data)
return b, 20, buffer.readu32(b, 16)
end
local n = 0
local streams = {}
for _, c in Cases do
local b, off, len = chunk(c.data)
if b then
local px, w, h = Vp8L.decode(b, off, len)
t:check(px, c.name .. ": " .. tostring(w))
t:check(w == c.width and h == c.height, c.name .. ": size")
t:eq(Crc32.of(px :: buffer), c.crc, c.name .. ": pixels (dwebp)")
local info = Vp8L.info(b, off, len)
t:check(info and info.width == c.width and info.height == c.height and info.alpha == c.alpha, c.name .. ": info")
local argb = Vp8L.decodeArgb(b, off, len)
t:check(argb and Crc32.of(Vp8L.toRgba(argb, c.width * c.height)) == c.crc, c.name .. ": decodeArgb + toRgba")
n += 1
if #streams < 12 and len < 8000 then
table.insert(streams, buffer.readstring(b, off, len))
end
end
end
t:note("%d VP8L bitstreams match dwebp", n)
-- the encoder: round trips at every effort
local rand = T.rng(5)
local function gen(w: number, h: number, kind: string): buffer
local px = buffer.create(w * h * 4)
local pal = {}
local np = if kind == "pal1" then 1 elseif kind == "pal4" then 4 elseif kind == "pal40" then 40 elseif kind == "pal300" then 300 else 0
for i = 1, np do
pal[i] = rand(2 ^ 24) * 256 + (if i % 3 == 0 then rand(256) else 255)
end
for y = 0, h - 1 do
for x = 0, w - 1 do
local o = (y * w + x) * 4
if np > 0 then
local v = pal[(x * 7 + y * 3 + (x * y) % 5) % np + 1]
buffer.writeu32(px, o, v)
elseif kind == "noise" then
buffer.writeu32(px, o, rand(2 ^ 16) * 65536 + rand(2 ^ 16))
else
buffer.writeu8(px, o, (x * 4 + y) % 256)
buffer.writeu8(px, o + 1, (y * 3) % 256)
buffer.writeu8(px, o + 2, (x * y) % 256)
buffer.writeu8(px, o + 3, if kind == "alpha" then (x * 9) % 256 else 255)
end
end
end
return px
end
local count = 0
for _, kind in { "grad", "alpha", "noise", "pal1", "pal4", "pal40", "pal300" } do
for _, size in { { 1, 1 }, { 2, 3 }, { 17, 5 }, { 64, 33 }, { 130, 2 } } do
local w, h = size[1], size[2]
local px = gen(w, h, kind)
for effort = 0, 9 do
local bits, err = Vp8L.encode(px, w, h, { effort = effort, exact = true })
t:check(bits, kind .. ": encode: " .. tostring(err))
local back, bw, bh = Vp8L.decode(bits :: buffer)
t:check(back and bw == w and bh == h, string.format("%s %dx%d effort %d: decode: %s", kind, w, h, effort, tostring(bw)))
t:check(buffer.tostring(back :: buffer) == buffer.tostring(px), string.format("%s %dx%d effort %d: round trip", kind, w, h, effort))
count += 1
end
-- a headerless alpha plane
local a = buffer.create(w * h)
for i = 0, w * h - 1 do
buffer.writeu8(a, i, buffer.readu8(px, i * 4 + 1))
end
local s = Vp8L.encodeAlpha(a, w, h) :: buffer
local ab = Vp8L.decodeAlpha(s, 0, buffer.len(s), w, h)
t:check(ab and buffer.tostring(ab) == buffer.tostring(a), kind .. ": alpha stream round trip")
end
end
t:note("%d encoder round trips", count)
-- errors
do
local _, e1 = Vp8L.decode(buffer.fromstring("\x2e\0\0\0\0"))
t:check(e1 and e1.code == Err.CORRUPT, "bad signature: " .. tostring(e1))
local _, e2 = Vp8L.decode(buffer.fromstring("\x2f\0\0\0\x20"))
t:check(e2 and e2.code == Err.CORRUPT, "bad version: " .. tostring(e2))
local _, e3 = Vp8L.decode(buffer.fromstring("\x2f\0"))
t:check(e3 and e3.code == Err.TRUNCATED, "short header: " .. tostring(e3))
local big = Vp8L.encode(gen(64, 33, "grad"), 64, 33) :: buffer
local _, e4 = Vp8L.decode(big, 0, buffer.len(big), 100)
t:check(e4 and e4.code == Err.LIMIT, "maxPixels: " .. tostring(e4))
local _, e5 = Vp8L.decode(big, 0, buffer.len(big) // 2)
t:check(e5 and e5.code == Err.TRUNCATED, "truncated stream: " .. tostring(e5))
local _, e6 = Vp8L.encode(buffer.create(4), 2, 2)
t:check(e6 and e6.code == Err.ARG, "encode: short pixel buffer")
end
-- fuzz on raw bitstreams
t:fuzz("decode", function(b: buffer)
return Vp8L.decode(b, 0, buffer.len(b), 2 ^ 20)
end, streams, { mutations = 10, keepHeader = 5 })
t:fuzz("decodeAlpha", function(b: buffer)
return Vp8L.decodeAlpha(b, 0, buffer.len(b), 37, 11)
end, streams, { mutations = 4, garbage = 100 })
t:done()
+246
View File
@@ -0,0 +1,246 @@
-- image/webp tests: every still file of tools/fixtures/webp.py (cwebp lossless / lossy / alpha variants, libvpx key
-- frames with token partitions, metadata) against dwebp (-pam, -nofancy -pam, -yuv: bit-exact), animations (img2webp,
-- gif2webp, webpmux) against Pillow's WebPAnimDecoder frames (bit-exact), our encoder (round trips at every effort,
-- dwebp / Pillow decoding our files at fixture time, metadata, animations), error codes, and the fuzz no-throw pass.
-- Fixtures: tools/fixtures/webp.py.
local T = require("@tests/T")
local Webp = require("@formats/image/webp")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Cases = require("@tests/gen/webp_cases")
local Anims = require("@tests/gen/webp_anim")
local Src = require("@tests/gen/webp_src")
local Enc = require("@tests/gen/webp_enc")
local t = T.suite("webp")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Webp.decode(Bytes.reader(b), opts)
end
-- the contract
t:eq(Webp.name, "webp", "name")
t:eq(Webp.category, "image", "category")
t:check(table.find(Webp.extensions, "webp") ~= nil, "extensions")
t:check(Webp.caps.decode and Webp.caps.encode and Webp.caps.animation and Webp.caps.alpha, "caps")
-- still files, bit-exact with dwebp
do
local lossy, lossless, alpha = 0, 0, 0
for _, c in Cases do
local img, err = decode(c.data)
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.kind == "image" and img.format == "rgba8" and img.width == c.width and img.height == c.height, c.name .. ": size")
local crc = Crc32.of(img.pixels)
t:check(crc == c.crc, string.format("%s: pixels crc %08x, dwebp %08x", c.name, crc, c.crc))
t:eq(img.meta.source.format, "webp", c.name .. ": meta.source")
t:eq(img.meta.source.lossless, not c.lossy, c.name .. ": lossless flag")
if c.lossy then
lossy += 1
local nf = decode(c.data, { upsampling = "nearest" })
t:check(Crc32.of(nf.pixels) == c.nofancy, c.name .. ": -nofancy")
if not c.alpha then
local y = decode(c.data, { native = true })
t:check(y.format == "yuv420p" and Crc32.of(y.pixels) == c.yuv, c.name .. ": yuv420p planes (dwebp -yuv)")
end
else
lossless += 1
end
if c.alpha then
alpha += 1
end
if c.icc then
t:check(img.meta.icc and Crc32.of(img.meta.icc) == c.icc, c.name .. ": ICC profile")
end
if c.exif then
t:check(img.meta.exif and Crc32.of(img.meta.exif) == c.exif, c.name .. ": EXIF")
t:check(img.meta.xmp and buffer.tostring(img.meta.xmp) == c.xmp, c.name .. ": XMP")
end
end
-- inside a bigger buffer (the Reader's range)
local c = Cases[3]
local big = buffer.create(#c.data + 11)
buffer.writestring(big, 5, c.data)
local img = Webp.decode(Bytes.reader(big, 5, #c.data))
t:check(img and Crc32.of(img.pixels) == c.crc, "decode from a sub-range")
t:note("%d lossless and %d lossy files (%d with alpha) match dwebp", lossless, lossy, alpha)
end
-- animations, bit-exact with Pillow (libwebp WebPAnimDecoder)
local function opaque(px: buffer, n: number): buffer
local b = buffer.create(n * 4)
buffer.copy(b, 0, px, 0, n * 4)
for i = 0, n - 1 do
buffer.writeu8(b, i * 4 + 3, 255)
end
return b
end
local function checkAnim(a: any, label: string)
local img, err = decode(a.data, { coalesce = true })
t:check(img and img.frames, label .. ": " .. tostring(err))
t:eq(#img.frames, #a.frames, label .. ": frame count")
t:eq(img.loops, a.loops, label .. ": loops")
local n = img.width * img.height
for i, f in img.frames do
local px = if a.rgba == false then opaque(f.image.pixels, n) else f.image.pixels
t:check(Crc32.of(px) == a.frames[i].crc, string.format("%s: frame %d", label, i))
t:eq(math.floor(f.delay * 1000 + 0.5), a.frames[i].ms, label .. ": duration")
t:check(f.x == 0 and f.y == 0 and f.dispose == "none" and f.blend == "source", label .. ": coalesced frame fields")
end
t:check(buffer.tostring(img.pixels) == buffer.tostring(img.frames[1].image.pixels), label .. ": pixels = first canvas")
-- the frames as stored, then composited by Webp.coalesce: the same canvases
local raw = decode(a.data)
local canv = Webp.coalesce(raw) :: { buffer }
for i, c in canv do
local px = if a.rgba == false then opaque(c, n) else c
t:check(Crc32.of(px) == a.frames[i].crc, string.format("%s: Webp.coalesce frame %d", label, i))
end
local only = decode(a.data, { frames = false })
t:check(only.frames == nil and buffer.tostring(only.pixels) == buffer.tostring(img.pixels), label .. ": frames = false")
return raw
end
for _, a in Anims do
local raw = checkAnim(a, a.name)
t:check(raw.meta.source.animated and raw.frames[1].image.meta.source.codec ~= nil, a.name .. ": meta")
if a.name == "anim_mux_icc.webp" then
t:check(raw.meta.icc ~= nil, a.name .. ": ICC")
t:check(raw.meta.background[1] == 1 and raw.meta.background[2] == 2 and raw.meta.background[3] == 3 and raw.meta.background[4] == 255, a.name .. ": background colour")
t:check(raw.frames[2].x == 10 and raw.frames[2].y == 6 and raw.frames[2].dispose == "background" and raw.frames[2].blend == "over", a.name .. ": frame placement")
end
end
t:note("%d animations match Pillow", #Anims)
-- the encoder: round trips, the fixture-time dwebp / Pillow checks of our files, metadata, animations
local function image(s: any): Types.Image
local img = Types.Image.new(s.width, s.height)
img.pixels = buffer.fromstring(s.rgba)
return img
end
do
local rt = 0
for _, s in Src do
local img = image(s)
for _, effort in { 0, 2, 5, 7 } do
local out, err = Webp.encode(img, { effort = effort, exact = true })
t:check(out, s.name .. ": encode: " .. tostring(err))
local back = decode(out)
t:check(back and buffer.tostring(back.pixels) == s.rgba, string.format("%s effort %d: lossless round trip", s.name, effort))
rt += 1
end
-- without exact, fully transparent pixels may change, nothing else
local back = decode(Webp.encode(img))
local same = true
for i = 0, s.width * s.height - 1 do
local a = buffer.readu8(img.pixels, i * 4 + 3)
if a ~= 0 and buffer.readu32(back.pixels, i * 4) ~= buffer.readu32(img.pixels, i * 4) then
same = false
elseif a == 0 and buffer.readu8(back.pixels, i * 4 + 3) ~= 0 then
same = false
end
end
t:check(same, s.name .. ": default (not exact) round trip")
end
local ok, ours, cwebp = 0, 0, 0
for _, e in Enc.still do
t:check(e.ok, string.format("dwebp decodes our %s (effort %d%s)", e.name, e.effort, if e.exact then ", exact" else ""))
ok += 1
if e.effort == 9 and e.exact then
ours += e.size
cwebp += e.cwebp
end
end
t:note("%d round trips; dwebp reads our %d files; effort 9: %d bytes, cwebp -lossless: %d bytes", rt, ok, ours, cwebp)
for _, a in Enc.anim do
t:check(a.ok, a.name .. ": Pillow composites our animation as its input frames")
checkAnim({ data = a.data, frames = (function()
local f = {}
for i, c in a.frames do
f[i] = { crc = c, ms = a.ms[i] }
end
return f
end)(), loops = a.loops, rgba = true }, "our " .. a.name)
end
-- metadata chunks
local img = image(Src[1])
img.meta.icc = buffer.fromstring("icc profile bytes")
img.meta.exif = buffer.fromstring("Exif\0\0MM")
img.meta.xmp = "<x:xmpmeta/>"
local back = decode(Webp.encode(img))
t:check(back.meta.icc and buffer.tostring(back.meta.icc) == "icc profile bytes", "ICC round trip")
t:check(back.meta.exif and buffer.tostring(back.meta.exif) == "Exif\0\0MM", "EXIF round trip")
t:check(back.meta.xmp and buffer.tostring(back.meta.xmp) == "<x:xmpmeta/>", "XMP round trip")
t:check(back.meta.source.extended and buffer.tostring(back.pixels) == Src[1].rgba, "VP8X still round trip")
-- an animation round trip through our own decoder (frame fields kept)
local anim = Types.Image.new(8, 6)
local f1 = Types.Image.new(8, 6)
buffer.fill(f1.pixels, 0, 200)
local f2 = Types.Image.new(4, 2)
buffer.fill(f2.pixels, 0, 77)
anim.frames = {
{ image = f1, delay = 0.25, x = 0, y = 0, dispose = "none", blend = "source" },
{ image = f2, delay = 1.5, x = 2, y = 4, dispose = "background", blend = "over" },
}
anim.loops = 7
local ab = decode(Webp.encode(anim, { background = { 10, 20, 30, 40 } }))
t:check(ab.frames and #ab.frames == 2 and ab.loops == 7, "animation round trip: frames, loops")
t:check(ab.frames[2].x == 2 and ab.frames[2].y == 4 and ab.frames[2].dispose == "background" and ab.frames[2].blend == "over" and ab.frames[2].delay == 1.5, "animation round trip: frame fields")
t:check(buffer.tostring(ab.frames[2].image.pixels) == buffer.tostring(f2.pixels), "animation round trip: frame pixels")
t:check(ab.meta.background[1] == 10 and ab.meta.background[4] == 40, "animation round trip: background")
-- bad arguments
local _, e1 = Webp.encode({} :: any)
t:check(e1 and e1.code == Err.ARG, "encode a non-image: ARG")
local _, e2 = Webp.encode(img, { lossless = false })
t:check(e2 and e2.code == Err.UNSUPPORTED, "lossy encode: UNSUPPORTED")
local _, e3 = Webp.encode(img, { effort = 11 })
t:check(e3 and e3.code == Err.ARG, "bad effort: ARG")
anim.frames[2].x = 6
local _, e4 = Webp.encode(anim)
t:check(e4 and e4.code == Err.ARG, "frame outside the canvas: ARG")
end
-- sniff, info, errors
do
local c = Cases[1]
t:eq(Webp.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff WebP")
t:eq(Webp.sniff(Bytes.reader(buffer.fromstring("RIFF\0\0\0\0WAVEfmt "))), 0, "sniff WAV")
t:eq(Webp.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
local r = Bytes.reader(buffer.fromstring(c.data))
local info = Webp.info(r)
t:check(info and info.width == c.width and info.height == c.height and info.frames == 1, "info")
local ai = Webp.info(Bytes.reader(buffer.fromstring(Anims[1].data)))
t:check(ai and ai.animated and ai.frames == #Anims[1].frames, "info of an animation")
local _, e1 = decode("RIFF\4\0\0\0WEBP")
t:check(e1 and e1.code == Err.CORRUPT, "RIFF size too small: " .. tostring(e1))
local _, e2 = decode(string.sub(c.data, 1, #c.data - 5))
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
local _, e3 = decode(c.data, { maxPixels = 10 })
t:check(e3 and e3.code == Err.LIMIT, "maxPixels: " .. tostring(e3))
local _, e4 = decode("not a webp file at all")
t:check(e4 and e4.code == Err.CORRUPT, "junk: " .. tostring(e4))
end
-- fuzz: truncations, mutations and garbage never throw
do
local samples = {}
for i, c in Cases do
if i % 7 == 0 and #c.data < 20000 then
table.insert(samples, c.data)
end
end
for _, a in Anims do
if #a.data < 40000 then
table.insert(samples, a.data)
end
end
t:fuzz("decode", function(b: buffer)
return Webp.decode(Bytes.reader(b), { maxPixels = 2 ^ 20 })
end, samples, { mutations = 6 })
t:fuzz("decode coalesce", function(b: buffer)
return Webp.decode(Bytes.reader(b), { maxPixels = 2 ^ 20, coalesce = true, upsampling = "nearest" })
end, samples, { mutations = 3, keepHeader = 30, garbage = 20 })
end
t:done()