Files
rosetta/tests/codecs/lzw.luau
T
4DBug 51b08575c4 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
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.
2026-10-31 12:25:03 -05:00

120 lines
4.1 KiB
Luau

-- codecs/compress/Lzw tests: GIF (LSB) and TIFF (MSB, early change) code streams from Pillow / libtiff decode to their data;
-- our encoder round-trips random and structured data at every GIF minimum code size and through table resets; the
-- output limit, truncated and corrupt streams; the fuzz no-throw pass. Fixtures: tools/fixtures/lzw.py.
local T = require("@tests/T")
local Lzw = require("@formats/codecs/compress/Lzw")
local Fx = require("@tests/gen/lzw")
local t = T.suite("lzw")
local function dec(codes: buffer, n: number, variant: string, min: number?): (string, number, string?)
local out = buffer.create(n)
local got, err = Lzw.decode(codes, 0, buffer.len(codes), out, 0, n, variant, min)
return buffer.tostring(out), got, err
end
-- reference streams
for _, c in Fx.gif do
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "gif", c.min)
t:check(err == nil, "gif " .. c.name .. ": " .. tostring(err))
t:eq(n, #c.data, "gif " .. c.name .. ": length")
t:check(s == c.data, "gif " .. c.name .. ": data")
end
for _, c in Fx.tiff do
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "tiff")
t:check(err == nil, "tiff " .. c.name .. ": " .. tostring(err))
t:eq(n, #c.data, "tiff " .. c.name .. ": length")
t:check(s == c.data, "tiff " .. c.name .. ": data")
end
t:note("%d GIF and %d TIFF reference streams decode", #Fx.gif, #Fx.tiff)
-- round trips
local rand = T.rng(3)
local function data(n: number, kind: number, max: number): buffer
local b = buffer.create(n)
for i = 0, n - 1 do
local v
if kind == 0 then
v = rand(max)
elseif kind == 1 then
v = (i // 7) % max
else
v = if rand(10) == 0 then rand(max) else (i % 13) % max
end
buffer.writeu8(b, i, v)
end
return b
end
local trips = 0
for _, n in { 0, 1, 2, 3, 100, 5000, 70000 } do
for kind = 0, 2 do
for min = 2, 8 do
local d = data(n, kind, 2 ^ min)
local codes = Lzw.encode(d, 0, n, "gif", min)
local s, got, err = dec(codes, n, "gif", min)
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("gif round trip n=%d kind=%d min=%d: %s", n, kind, min, tostring(err)))
trips += 1
end
local d = data(n, kind, 256)
local codes = Lzw.encode(d, 0, n, "tiff")
local s, got, err = dec(codes, n, "tiff")
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("tiff round trip n=%d kind=%d: %s", n, kind, tostring(err)))
trips += 1
end
end
t:note("%d round trips", trips)
-- compression actually happens; a range inside a bigger buffer
do
local d = buffer.create(10000)
local codes = Lzw.encode(d, 0, 10000, "tiff")
t:check(buffer.len(codes) < 400, "zeros compress: " .. buffer.len(codes))
local big = data(300, 1, 256)
local codes2 = Lzw.encode(big, 100, 100, "gif", 8)
local out = buffer.create(120)
local n = Lzw.decode(codes2, 0, buffer.len(codes2), out, 10, 100, "gif", 8)
t:eq(n, 100, "sub-range length")
t:check(buffer.readstring(out, 10, 100) == buffer.readstring(big, 100, 100), "sub-range data")
end
-- limits, truncation, corruption
do
local d = data(1000, 2, 16)
local codes = Lzw.encode(d, 0, 1000, "gif", 4)
local s, n, err = dec(codes, 500, "gif", 4)
t:check(n == 500 and err == nil and s == buffer.readstring(d, 0, 500), "output limit")
local cut = buffer.create(buffer.len(codes) // 2)
buffer.copy(cut, 0, codes, 0, buffer.len(cut))
local _, n2, err2 = dec(cut, 1000, "gif", 4)
t:check(err2 == "truncated" and n2 > 0 and n2 < 1000, "truncated: " .. tostring(err2))
local bad = buffer.fromstring("\255\255\255\255")
local _, _, err3 = dec(bad, 100, "gif", 2)
t:check(err3 == "corrupt", "corrupt: " .. tostring(err3))
local _, _, err4 = dec(codes, 100, "gif", 0)
t:check(err4 == "corrupt", "bad min code size")
end
-- fuzz
do
local samples = {}
for i, c in Fx.gif do
if i % 2 == 1 then
table.insert(samples, c.codes)
end
end
for i, c in Fx.tiff do
if i % 2 == 0 then
table.insert(samples, c.codes)
end
end
local out = buffer.create(100000)
t:fuzz("decode gif", function(b: buffer)
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "gif", 8)
end, samples)
t:fuzz("decode tiff", function(b: buffer)
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "tiff")
end, samples)
end
t:done()