-- codecs/image/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/image/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()