-- 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 = "" 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) == "", "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()