codecs/image: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:22:45 -06:00
1 parent 0bd7b87d2b
commit 95a6b03f70
30 files changed
+224 -770

No files matched your search

+5 -27
View File
@@ -1,13 +1,9 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Vp8L/Decode: the WebP lossless (VP8L) bitstream decoder: prefix codes (two-level tables), meta prefix codes,
-- LZ77 backward references with the 2-D distance map, the colour cache, and the four inverse transforms (predictor,
-- cross colour, subtract green, colour indexing with pixel bundling). A port of libwebp 1.6 src/dec/vp8l_dec.c,
-- codecs/image/Vp8L/Decode: the WebP lossless (VP8L) bitstream decoder, a port of libwebp 1.6 src/dec/vp8l_dec.c,
-- src/utils/huffman_utils.c and src/dsp/lossless.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt); bit-exact with it.
--
-- Pixels are ARGB u32 values (0xAARRGGBB) in a buffer, 4 bytes each, rows top to bottom. Never throws: every
-- function returns nil, "message" (prefixed "truncated: " when the data ends early) on bad data.
-- Pixels are ARGB u32 values (0xAARRGGBB).
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
local readu8, readu32, writeu32, writeu8 = buffer.readu8, buffer.readu32, buffer.writeu32, buffer.writeu8
local bcreate, bcopy, blen = buffer.create, buffer.copy, buffer.len
@@ -35,9 +31,7 @@ local CODE_TO_PLANE = {
-- distance map (x offset 8 - (v & 15), y offset v >> 4) as a plain distance for a row width: done per image
Decode.CODE_TO_PLANE = CODE_TO_PLANE
-- ------------------------------------------------------------------------------------------------ bit reader
-- LSB-first; bytes past the end read as zero and are counted in `over`: the stream is overrun when fewer bits are
-- left in the buffer than phantom bits were added (bitcnt < over * 8).
-- LSB-first; bytes past the end read as zero and count in `over`: the stream is overrun when bitcnt < over * 8
export type State = {
buf: buffer,
pos: number,
@@ -68,7 +62,6 @@ local function fill(d: State)
d.bitbuf, d.bitcnt = bitbuf, bitcnt
end
-- readBits(d, n) n <= 24
local function readBits(d: State, n: number): number
if d.bitcnt < n then
fill(d)
@@ -83,7 +76,6 @@ local function eos(d: State): boolean
return d.bitcnt < d.over * 8
end
-- ------------------------------------------------------------------------------------------------ prefix codes
local count = table.create(MAX_LEN + 1, 0)
local offset = table.create(MAX_LEN + 2, 0)
@@ -112,8 +104,7 @@ local function nextKey(key: number, len: number): number
return if step ~= 0 then band(key, step - 1) + step else key
end
-- build(d, lens (0-based Lua array), n) -> table offset in d.tab, or -1 for an invalid code set (libwebp
-- BuildHuffmanTable: complete codes only, except a single used symbol, which reads 0 bits)
-- libwebp BuildHuffmanTable: complete codes only, except a single used symbol, which reads 0 bits; -1 when invalid
local sorted = table.create(2400, 0)
local function build(d: State, lens: { number }, n: number): number
for l = 0, MAX_LEN do
@@ -222,7 +213,6 @@ local function build(d: State, lens: { number }, n: number): number
return root
end
-- readSymbol(d, at) -> symbol (the reader is refilled to >= 15 bits first)
local function readSymbol(d: State, at: number): number
if d.bitcnt < MAX_LEN then
fill(d)
@@ -246,7 +236,6 @@ end
local codeLengths = table.create(2400, 0)
local clLens = table.create(19, 0)
-- readCode(d, alphabetSize) -> table offset, or -1
local function readCode(d: State, size: number): number
for i = 1, size do
codeLengths[i] = 0
@@ -325,7 +314,6 @@ local function readCode(d: State, size: number): number
return build(d, codeLengths, size)
end
-- ------------------------------------------------------------------------------------------------ pixel data
local function subSample(size: number, bits: number): number
return rshift(size + lshift(1, bits) - 1, bits)
end
@@ -340,7 +328,6 @@ local function cacheKey(argb: number, shift: number): number
end
Decode.cacheKey = cacheKey
-- decodeData: the entropy-coded pixels of one level into `data` (w * h u32)
local function decodeData(d: State, data: buffer, w: number, h: number, cacheBits: number, groups: { number }, himg: buffer?, hbits: number, hxs: number): (boolean, string?)
local total = w * h
local cache: buffer? = if cacheBits > 0 then bcreate(lshift(1, cacheBits) * 4) else nil
@@ -457,7 +444,6 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
return true, nil
end
-- ------------------------------------------------------------------------------------------------ transforms
local function addPixels(a: number, b: number): number
return band(band(a, 0xff00ff00) + band(b, 0xff00ff00), 0xff00ff00) + band(band(a, 0x00ff00ff) + band(b, 0x00ff00ff), 0x00ff00ff)
end
@@ -510,7 +496,7 @@ local function clampedHalf(c0: number, c1: number, c2: number): number
return a * 16777216 + r * 65536 + g * 256 + b
end
-- predict(mode, L, T, TR, TL) -> the predicted pixel (libwebp VP8LPredictors; 14 and 15 are black)
-- libwebp VP8LPredictors; modes 14 and 15 are black
local function predict(mode: number, L: number, T: number, TR: number, TL: number): number
if mode == 0 then
return ARGB_BLACK
@@ -618,7 +604,6 @@ local function inverseSubtractGreen(data: buffer, n: number)
end
end
-- inverseColorIndexing(src (sw x h), w, h, bits, map (2^(8 >> bits) u32)) -> w x h
local function inverseColorIndexing(src: buffer, sw: number, w: number, h: number, bits: number, map: buffer): buffer
local out = bcreate(w * h * 4)
if bits == 0 then
@@ -646,12 +631,10 @@ local function inverseColorIndexing(src: buffer, sw: number, w: number, h: numbe
return out
end
-- ------------------------------------------------------------------------------------------------ image stream
type Transform = { kind: number, xsize: number, bits: number, data: buffer? }
local decodeStream: (d: State, xs: number, ys: number, level0: boolean) -> (buffer?, string?)
-- readCodes(d, xs, ys, cacheBits, level0) -> groups (5 table offsets each), huffman image, bits, image width
local function readCodes(d: State, xs: number, ys: number, cacheBits: number, level0: boolean): ({ number }?, buffer?, number, number, string?)
local himg: buffer? = nil
local hbits, hxs = 0, 0
@@ -808,7 +791,6 @@ local function newState(buf: buffer, off: number, len: number, maxPixels: number
}
end
-- header(buf, off, len) -> width, height, alphaHint | nil, message
function Decode.header(buf: buffer, off: number, len: number): (number?, number, boolean, string?)
if len < 5 then
return nil, 0, false, "truncated: header"
@@ -826,7 +808,6 @@ function Decode.header(buf: buffer, off: number, len: number): (number?, number,
return w, h, alpha, nil
end
-- argb(buf, off, len, maxPixels) -> ARGB pixels, width, height, alphaHint | nil, message
function Decode.argb(buf: buffer, off: number, len: number, maxPixels: number): (buffer?, number, number, boolean, string?)
local w, h, alpha, err = Decode.header(buf, off, len)
if not w then
@@ -843,13 +824,11 @@ function Decode.argb(buf: buffer, off: number, len: number, maxPixels: number):
return data, w, h, alpha, nil
end
-- stream(buf, off, len, w, h) -> ARGB pixels | nil, message: a headerless image stream (the ALPH chunk's payload)
function Decode.stream(buf: buffer, off: number, len: number, w: number, h: number): (buffer?, string?)
local d = newState(buf, off, len, w * h)
return decodeStream(d, w, h, true)
end
-- toRgba(argb, n, out?, outOff?) -> rgba8 bytes
function Decode.toRgba(argb: buffer, n: number, out: buffer?, outOff: number?): buffer
local o = out or bcreate(n * 4)
local base = outOff or 0
@@ -861,7 +840,6 @@ function Decode.toRgba(argb: buffer, n: number, out: buffer?, outOff: number?):
return o
end
-- green(argb, n) -> the green channel as bytes (alpha planes)
function Decode.green(argb: buffer, n: number): buffer
local o = bcreate(n)
for k = 0, n - 1 do