Files
rosetta/src/Formats/codecs/image/Vp8L/init.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

121 lines
5.4 KiB
Luau

--!strict
-- codecs/image/Vp8L: WebP lossless (VP8L, "WebP Lossless Bitstream Specification", RFC 9649 section 3). A codec (layer 1):
-- image/webp uses it for VP8L frames and for compressed ALPH planes; other containers may carry it too.
-- Decoder: a port of libwebp 1.6 (BSD-3-Clause, licenses/libwebp-LICENSE.txt), bit-exact with it. Encoder: new
-- (subtract green, predictor and cross-colour transforms with per-tile mode search, colour indexing with pixel
-- bundling for <= 256 colours, LZ77 with hash chains and the 2-D distance codes, a colour cache chosen by entropy,
-- one prefix code group per image or meta codes for big images).
--
-- local Vp8L = require("@formats/codecs/image/Vp8L")
-- local rgba, w, h, alpha = Vp8L.decode(buf, off, len) -- a VP8L bitstream (0x2f signature) -> rgba8
-- local a8 = Vp8L.decodeAlpha(buf, off, len, w, h) -- a headerless stream's green channel (ALPH)
-- local bits = Vp8L.encode(rgba, w, h, { effort = 4 }) -- rgba8 -> a VP8L bitstream
--
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Decode = require("@formats/codecs/image/Vp8L/Decode")
local Encode = require("@formats/codecs/image/Vp8L/Encode")
local Vp8L = {}
local DEFAULT_MAX_PIXELS = 2 ^ 28
local function fail(msg: string?, offset: number?): Err.Err
local m = msg or "bad data"
if string.sub(m, 1, 11) == "truncated: " then
return Err.new(Err.TRUNCATED, string.sub(m, 12), offset)
elseif string.sub(m, 1, 7) == "limit: " then
return Err.new(Err.LIMIT, string.sub(m, 8), offset)
end
return Err.new(Err.CORRUPT, m, offset)
end
Vp8L.fail = fail
local function range(buf: buffer, off: number?, len: number?): (number, number)
local n = buffer.len(buf)
local o = math.clamp(off or 0, 0, n)
return o, math.clamp(len or (n - o), 0, n - o)
end
-- info(buf, off?, len?) -> { width, height, alpha } | nil, Err: the 5-byte header
function Vp8L.info(buf: buffer, off: number?, len: number?): ({ width: number, height: number, alpha: boolean }?, Err.Err?)
local o, l = range(buf, off, len)
local w, h, alpha, err = Decode.header(buf, o, l)
if not w then
return nil, fail(err, o)
end
return { width = w, height = h, alpha = alpha }, nil
end
-- decodeArgb(buf, off?, len?, maxPixels?) -> ARGB u32 pixels (0xAARRGGBB), width, height, alphaHint | nil, Err
function Vp8L.decodeArgb(buf: buffer, off: number?, len: number?, maxPixels: number?): (buffer?, any, number?, boolean?)
local o, l = range(buf, off, len)
local px, w, h, alpha, err = Decode.argb(buf, o, l, maxPixels or DEFAULT_MAX_PIXELS)
if not px then
return nil, fail(err, o)
end
return px, w, h, alpha
end
-- decode(buf, off?, len?, maxPixels?) -> rgba8 pixels, width, height, alphaHint | nil, Err
function Vp8L.decode(buf: buffer, off: number?, len: number?, maxPixels: number?): (buffer?, any, number?, boolean?)
local px, w, h, alpha = Vp8L.decodeArgb(buf, off, len, maxPixels)
if not px then
return nil, w
end
return Decode.toRgba(px, w * (h :: number)), w, h, alpha
end
-- decodeAlpha(buf, off, len, width, height) -> width * height bytes (the green channel) | nil, Err
function Vp8L.decodeAlpha(buf: buffer, off: number?, len: number?, width: number, height: number): (buffer?, Err.Err?)
local o, l = range(buf, off, len)
local px, err = Decode.stream(buf, o, l, width, height)
if not px then
return nil, fail(err, o)
end
return Decode.green(px, width * height), nil
end
-- toRgba(argb, n) -> rgba8 bytes of n ARGB pixels
Vp8L.toRgba = Decode.toRgba
export type EncodeOptions = {
effort: number?, -- 0..9, default 5: more = smaller and slower (0: no transforms, no LZ77 search)
exact: boolean?, -- default false: keep the RGB of fully transparent pixels (otherwise they may be zeroed)
alphaHint: boolean?, -- the header's "alpha is used" bit; default: any alpha < 255
}
-- encode(rgba, width, height, opts?) -> a VP8L bitstream (signature + header + image stream) | nil, Err
function Vp8L.encode(rgba: buffer, width: number, height: number, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(rgba) ~= "buffer" or type(width) ~= "number" or type(height) ~= "number" then
return nil, Err.new(Err.ARG, "encode: needs rgba8 pixels, width, height")
end
if width < 1 or height < 1 or width > 16384 or height > 16384 or width % 1 ~= 0 or height % 1 ~= 0 then
return nil, Err.new(Err.ARG, "encode: size must be 1..16384")
end
if buffer.len(rgba) < width * height * 4 then
return nil, Err.new(Err.ARG, "encode: pixel buffer too small")
end
local o: EncodeOptions = opts or {} :: EncodeOptions
local effort = o.effort or 5
if type(effort) ~= "number" or effort < 0 or effort > 9 then
return nil, Err.new(Err.ARG, "encode: effort must be 0..9")
end
return Encode.encode(rgba, width, height, effort, o.exact == true, o.alphaHint, false), nil
end
-- encodeAlpha(a8, width, height, opts?) -> a headerless stream of the plane in green (an ALPH payload)
function Vp8L.encodeAlpha(a8: buffer, width: number, height: number, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(a8) ~= "buffer" or buffer.len(a8) < width * height then
return nil, Err.new(Err.ARG, "encodeAlpha: plane too small")
end
local rgba = buffer.create(width * height * 4)
for i = 0, width * height - 1 do
buffer.writeu32(rgba, i * 4, 0xff000000 + buffer.readu8(a8, i) * 256)
end
local effort = opts and opts.effort or 5
return Encode.encode(rgba, width, height, effort, true, false, true), nil
end
return Vp8L