C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
121 lines
4.8 KiB
Luau
121 lines
4.8 KiB
Luau
-- 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()
|