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

+8 -25
View File
@@ -1,16 +1,8 @@
--!strict
--!native
--!optimize 2
-- codecs/image/Jpeg/Scan: Huffman-coded scan decoding into quantised DCT coefficients (native; never throws). Absorbed from
-- jpeg-luau 1.0 src/Jpeg/Scan.luau (same author); unchanged but for this header.
--
-- `destuff` copies one scan's entropy-coded bytes into a clean buffer: it drops the stuffed 0x00 after each 0xFF,
-- records where each RSTn marker fell, and stops at any other marker. The clean buffer is followed by at least
-- PAD zero bytes, so the bit reader can read 32 bits at any position up to PAD bytes past the end without a
-- bounds check. `run` checks the position once per MCU, and no MCU (at most 10 blocks, enforced by the parser)
-- can consume PAD bytes. So every buffer index stays in range, even on garbage data.
--
-- The bit reader keeps no state but a bit position: `peek` reads the big-endian 32-bit word that holds it.
-- codecs/image/Jpeg/Scan: Huffman-coded scan decoding into quantised DCT coefficients. From jpeg-luau 1.0 (same author).
-- The clean scan data is followed by PAD zero bytes, so the 32-bit big-endian bit reader needs no bounds check.
-- Huffman tables (built by `buildTable`) are buffers:
-- [0, 1024) fast lookup by the next 9 bits: u16 (length << 8) | symbol, 0 = longer code
-- [1024, 1096) maxcode[l], l = 0..17: u32, one past the last code of length l, left-aligned to 16 bits
@@ -58,8 +50,7 @@ do
end
Scan.ZIGZAG = ZZ
-- Builds a Huffman table from the 16 code-length counts and the symbols (Luau arrays, 1-based). An
-- over-subscribed table keeps the codes that fit; the rest decode as invalid.
-- an over-subscribed table keeps the codes that fit; the rest decode as invalid
function Scan.buildTable(counts: { number }, symbols: { number }): buffer
local t = buffer.create(Scan.TABLE_SIZE)
local code, k = 0, 0
@@ -87,7 +78,6 @@ function Scan.buildTable(counts: { number }, symbols: { number }): buffer
writeu8(t, T_VAL + i - 1, symbols[i])
end
writeu16(t, T_COUNT, k)
-- fast lookups
for i = 1, k do
local l = sizes[i]
if l <= 9 then
@@ -110,9 +100,6 @@ function Scan.buildTable(counts: { number }, symbols: { number }): buffer
return t
end
-- Copies the entropy-coded data at src[pos..n) into dst (at least n - pos + PAD bytes): byte stuffing removed,
-- RSTn offsets (in dst) stored in rst[1..]. Returns the source position of the marker that ended the scan (or n),
-- the clean length, and the RST count. The PAD bytes after the clean data are zeroed.
function Scan.destuff(src: buffer, pos: number, n: number, dst: buffer, rst: { number }): (number, number, number)
local o, nr = 0, 0
while pos < n do
@@ -170,7 +157,6 @@ local function huff(d: buffer, pos: number, t: buffer): (number, number)
return readu8(t, T_VAL + idx), pos + l
end
-- the next s bits (1 <= s <= 16) as an unsigned number
local function bits(d: buffer, pos: number, s: number): number
return rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 32 - s)
end
@@ -182,7 +168,6 @@ local function extend(v: number, s: number): number
return v
end
-- sequential (baseline / extended) block: returns pos, DC predictor
local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, act: buffer, pred: number): (number, number)
local s
s, pos = huff(d, pos, dct)
@@ -354,11 +339,11 @@ local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number,
end
export type ScanComp = {
coef: buffer, -- i16 coefficients, 64 per block in natural order, blocks row-major on a bpl-wide grid
coef: buffer,
bpl: number,
h: number,
v: number,
bw: number, -- blocks across / down the component itself (non-interleaved scans)
bw: number,
bh: number,
dct: buffer?,
act: buffer?,
@@ -369,16 +354,14 @@ export type ScanParams = {
comps: { ScanComp },
mcusX: number,
mcusY: number,
ri: number, -- restart interval in MCUs (0 = none)
mode: number, -- 0 sequential, 1 DC first, 2 DC refine, 3 AC first, 4 AC refine
ri: number,
mode: number,
ss: number,
se: number,
al: number,
hook: (() -> ())?, -- a cooperative yield point after every MCU row (pcall'ed: it can't throw through here)
hook: (() -> ())?,
}
-- Decodes one scan from the clean data d (cleanLen bytes, then PAD zeros). Returns the final bit position and
-- whether the data ran out (or overran) before the last MCU.
function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P: ScanParams): (number, boolean)
local comps = P.comps
local nc = #comps