--!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