init.luau (small, backward compatible): loadTextures forwards those four options to F.load.
476 lines
12 KiB
Luau
476 lines
12 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- 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
|
|
-- [1096, 1168) delta[l]: i32, symbol index = code + delta[l]
|
|
-- [1168, 1424) symbols
|
|
-- [1424, 1426) symbol count
|
|
-- [1428, 3476) fast AC lookup by the next 9 bits: i32 value * 256 + run * 16 + total length, when the code
|
|
-- and its extra bits fit in 9 bits (0 otherwise)
|
|
local readu8, readu16, readu32, readi32, readi16 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi32, buffer.readi16
|
|
local writeu8, writeu16, writeu32, writei32, writei16 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writei32, buffer.writei16
|
|
local band, bor, lshift, rshift, byteswap = bit32.band, bit32.bor, bit32.lshift, bit32.rshift, bit32.byteswap
|
|
|
|
local Scan = {}
|
|
|
|
Scan.PAD = 4096
|
|
Scan.TABLE_SIZE = 3476
|
|
|
|
local T_MAX = 1024
|
|
local T_DELTA = 1096
|
|
local T_VAL = 1168
|
|
local T_COUNT = 1424
|
|
local T_FASTAC = 1428
|
|
|
|
-- zigzag index -> natural (row-major) index, padded with 63s so a run past the end stays in range
|
|
local ZZ = buffer.create(80)
|
|
do
|
|
local i = 0
|
|
for s = 0, 14 do
|
|
local lo, hi = math.max(0, s - 7), math.min(s, 7)
|
|
if s % 2 == 0 then
|
|
for r = hi, lo, -1 do
|
|
writeu8(ZZ, i, r * 8 + (s - r))
|
|
i += 1
|
|
end
|
|
else
|
|
for r = lo, hi do
|
|
writeu8(ZZ, i, r * 8 + (s - r))
|
|
i += 1
|
|
end
|
|
end
|
|
end
|
|
for j = 64, 79 do
|
|
writeu8(ZZ, j, 63)
|
|
end
|
|
end
|
|
Scan.ZIGZAG = ZZ
|
|
|
|
-- 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
|
|
local nsym = #symbols
|
|
local codes, sizes = {}, {}
|
|
local broken = false
|
|
for l = 1, 16 do
|
|
writei32(t, T_DELTA + l * 4, k - code)
|
|
local n = counts[l] or 0
|
|
for _ = 1, n do
|
|
if broken or code >= lshift(1, l) or k >= nsym or k >= 256 then
|
|
broken = true
|
|
break
|
|
end
|
|
k += 1
|
|
codes[k] = code
|
|
sizes[k] = l
|
|
code += 1
|
|
end
|
|
writeu32(t, T_MAX + l * 4, if broken then 0 else lshift(code, 16 - l))
|
|
code *= 2
|
|
end
|
|
writeu32(t, T_MAX + 17 * 4, 0xFFFFFFFF)
|
|
for i = 1, k do
|
|
writeu8(t, T_VAL + i - 1, symbols[i])
|
|
end
|
|
writeu16(t, T_COUNT, k)
|
|
for i = 1, k do
|
|
local l = sizes[i]
|
|
if l <= 9 then
|
|
local sym = symbols[i]
|
|
local base = lshift(codes[i], 9 - l)
|
|
for j = 0, lshift(1, 9 - l) - 1 do
|
|
local idx = base + j
|
|
writeu16(t, idx * 2, l * 256 + sym)
|
|
local r, s = rshift(sym, 4), band(sym, 15)
|
|
if s ~= 0 and l + s <= 9 then
|
|
local v = band(rshift(idx, 9 - l - s), lshift(1, s) - 1)
|
|
if v < lshift(1, s - 1) then
|
|
v = v - lshift(1, s) + 1
|
|
end
|
|
writei32(t, T_FASTAC + idx * 4, v * 256 + r * 16 + l + s)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return t
|
|
end
|
|
|
|
function Scan.destuff(src: buffer, pos: number, n: number, dst: buffer, rst: { number }, hook: (() -> ())?): (number, number, number)
|
|
local o, nr = 0, 0
|
|
local done = false
|
|
while not done and pos < n do
|
|
local lim = if hook then math.min(n, pos + 65536) else n
|
|
while pos < lim do
|
|
local b = readu8(src, pos)
|
|
if b ~= 0xFF then
|
|
writeu8(dst, o, b)
|
|
o += 1
|
|
pos += 1
|
|
else
|
|
local q = pos + 1
|
|
while q < n and readu8(src, q) == 0xFF do
|
|
q += 1
|
|
end
|
|
if q >= n then
|
|
pos = n
|
|
break
|
|
end
|
|
local m = readu8(src, q)
|
|
if m == 0 then
|
|
writeu8(dst, o, 0xFF)
|
|
o += 1
|
|
pos = q + 1
|
|
elseif m >= 0xD0 and m <= 0xD7 then
|
|
nr += 1
|
|
rst[nr] = o
|
|
pos = q + 1
|
|
else
|
|
pos = q - 1
|
|
done = true
|
|
break
|
|
end
|
|
end
|
|
end
|
|
if hook and not done and pos < n then
|
|
pcall(hook)
|
|
end
|
|
end
|
|
buffer.fill(dst, o, 0, Scan.PAD)
|
|
return pos, o, nr
|
|
end
|
|
|
|
local function huff(d: buffer, pos: number, t: buffer): (number, number)
|
|
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
|
local f = readu16(t, rshift(w, 23) * 2)
|
|
if f ~= 0 then
|
|
return band(f, 255), pos + rshift(f, 8)
|
|
end
|
|
local c = rshift(w, 16)
|
|
local l = 10
|
|
while c >= readu32(t, T_MAX + l * 4) do
|
|
l += 1
|
|
end
|
|
if l > 16 then
|
|
return 0, pos + 16 -- invalid code
|
|
end
|
|
local idx = rshift(c, 16 - l) + readi32(t, T_DELTA + l * 4)
|
|
if idx < 0 or idx >= readu16(t, T_COUNT) then
|
|
return 0, pos + l
|
|
end
|
|
return readu8(t, T_VAL + idx), pos + l
|
|
end
|
|
|
|
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
|
|
|
|
local function extend(v: number, s: number): number
|
|
if v < lshift(1, s - 1) then
|
|
return v - lshift(1, s) + 1
|
|
end
|
|
return v
|
|
end
|
|
|
|
local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, act: buffer, pred: number, nz: buffer, bi: number): (number, number)
|
|
local s
|
|
s, pos = huff(d, pos, dct)
|
|
if s ~= 0 then
|
|
if s > 16 then
|
|
s = 16
|
|
end
|
|
pred += extend(bits(d, pos, s), s)
|
|
pos += s
|
|
end
|
|
writei16(coef, off, pred)
|
|
local k = 1
|
|
local last = 0
|
|
while k < 64 do
|
|
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
|
local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4)
|
|
if fa ~= 0 then
|
|
local low = fa % 256
|
|
k += low // 16
|
|
pos += low % 16
|
|
if k > 63 then
|
|
break
|
|
end
|
|
writei16(coef, off + readu8(ZZ, k) * 2, fa // 256)
|
|
last = k
|
|
k += 1
|
|
else
|
|
local rs
|
|
rs, pos = huff(d, pos, act)
|
|
local r, sz = rshift(rs, 4), band(rs, 15)
|
|
if sz == 0 then
|
|
if r ~= 15 then
|
|
break
|
|
end
|
|
k += 16
|
|
else
|
|
k += r
|
|
if k > 63 then
|
|
break
|
|
end
|
|
writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, sz), sz))
|
|
pos += sz
|
|
last = k
|
|
k += 1
|
|
end
|
|
end
|
|
end
|
|
writeu8(nz, bi, last)
|
|
return pos, pred
|
|
end
|
|
|
|
local function blockDcFirst(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, pred: number, al: number): (number, number)
|
|
local s
|
|
s, pos = huff(d, pos, dct)
|
|
if s ~= 0 then
|
|
if s > 16 then
|
|
s = 16
|
|
end
|
|
pred += extend(bits(d, pos, s), s)
|
|
pos += s
|
|
end
|
|
writei16(coef, off, pred * lshift(1, al))
|
|
return pos, pred
|
|
end
|
|
|
|
local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number, nz: buffer, bi: number): (number, number)
|
|
if eobrun > 0 then
|
|
return pos, eobrun - 1
|
|
end
|
|
local mul = lshift(1, al)
|
|
local k = ss
|
|
local last = readu8(nz, bi)
|
|
while k <= se do
|
|
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
|
local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4)
|
|
if fa ~= 0 then
|
|
local low = fa % 256
|
|
k += low // 16
|
|
pos += low % 16
|
|
if k > se then
|
|
break
|
|
end
|
|
writei16(coef, off + readu8(ZZ, k) * 2, (fa // 256) * mul)
|
|
if k > last then
|
|
last = k
|
|
end
|
|
k += 1
|
|
else
|
|
local rs
|
|
rs, pos = huff(d, pos, act)
|
|
local r, s = rshift(rs, 4), band(rs, 15)
|
|
if s == 0 then
|
|
if r < 15 then
|
|
eobrun = lshift(1, r) - 1
|
|
if r > 0 then
|
|
eobrun += bits(d, pos, r)
|
|
pos += r
|
|
end
|
|
break
|
|
end
|
|
k += 16
|
|
else
|
|
k += r
|
|
if k > se then
|
|
break
|
|
end
|
|
writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, s), s) * mul)
|
|
pos += s
|
|
if k > last then
|
|
last = k
|
|
end
|
|
k += 1
|
|
end
|
|
end
|
|
end
|
|
writeu8(nz, bi, last)
|
|
return pos, eobrun
|
|
end
|
|
|
|
-- successive-approximation refinement of AC coefficients ss..se (as stb_image / libjpeg decode_mcu_AC_refine)
|
|
-- coefficients past nz[bi] are zero: in an EOB run (or after the block's EOB) they need no correction bits
|
|
local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number, nz: buffer, bi: number): (number, number)
|
|
local bit = lshift(1, al)
|
|
local last = readu8(nz, bi)
|
|
if eobrun > 0 then
|
|
for k = ss, if last < se then last else se do
|
|
local o = off + readu8(ZZ, k) * 2
|
|
local p = readi16(coef, o)
|
|
if p ~= 0 then
|
|
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
|
pos += 1
|
|
if b ~= 0 and band(p, bit) == 0 then
|
|
writei16(coef, o, if p > 0 then p + bit else p - bit)
|
|
end
|
|
end
|
|
end
|
|
return pos, eobrun - 1
|
|
end
|
|
local k = ss
|
|
while k <= se do
|
|
local rs
|
|
rs, pos = huff(d, pos, act)
|
|
local s, r = band(rs, 15), rshift(rs, 4)
|
|
local val = 0
|
|
if s == 0 then
|
|
if r < 15 then
|
|
eobrun = lshift(1, r) - 1
|
|
if r > 0 then
|
|
eobrun += bits(d, pos, r)
|
|
pos += r
|
|
end
|
|
r = 64 -- the rest of the block only refines
|
|
if last < se then
|
|
se = last
|
|
end
|
|
end
|
|
else
|
|
-- s must be 1; anything else is corrupt and treated as 1
|
|
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
|
pos += 1
|
|
val = if b ~= 0 then bit else -bit
|
|
end
|
|
while k <= se do
|
|
local o = off + readu8(ZZ, k) * 2
|
|
k += 1
|
|
local p = readi16(coef, o)
|
|
if p ~= 0 then
|
|
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
|
pos += 1
|
|
if b ~= 0 and band(p, bit) == 0 then
|
|
writei16(coef, o, if p > 0 then p + bit else p - bit)
|
|
end
|
|
else
|
|
if r == 0 then
|
|
if val ~= 0 then
|
|
writei16(coef, o, val)
|
|
if k - 1 > last then
|
|
last = k - 1
|
|
end
|
|
end
|
|
break
|
|
end
|
|
r -= 1
|
|
end
|
|
end
|
|
end
|
|
writeu8(nz, bi, last)
|
|
-- an EOB run that started in this block counts it already (eobrun = run length - 1)
|
|
return pos, eobrun
|
|
end
|
|
|
|
export type ScanComp = {
|
|
coef: buffer,
|
|
nz: buffer,
|
|
bpl: number,
|
|
h: number,
|
|
v: number,
|
|
bw: number,
|
|
bh: number,
|
|
dct: buffer?,
|
|
act: buffer?,
|
|
pred: number,
|
|
}
|
|
|
|
export type ScanParams = {
|
|
comps: { ScanComp },
|
|
mcusX: number,
|
|
mcusY: number,
|
|
ri: number,
|
|
mode: number,
|
|
ss: number,
|
|
se: number,
|
|
al: number,
|
|
hook: (() -> ())?,
|
|
}
|
|
|
|
function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P: ScanParams): (number, boolean)
|
|
local comps = P.comps
|
|
local nc = #comps
|
|
local mode, ss, se, al, ri = P.mode, P.ss, P.se, P.al, P.ri
|
|
local endBits = cleanLen * 8
|
|
local units, ux
|
|
if nc == 1 then
|
|
ux = comps[1].bw
|
|
units = ux * comps[1].bh
|
|
else
|
|
ux = P.mcusX
|
|
units = ux * P.mcusY
|
|
end
|
|
for i = 1, nc do
|
|
comps[i].pred = 0
|
|
end
|
|
local pos, eobrun, rstIdx = 0, 0, 0
|
|
local truncated, skipping = false, false
|
|
local bit = lshift(1, al)
|
|
local hook = P.hook
|
|
for u = 0, units - 1 do
|
|
if hook and u > 0 and u % ux == 0 then
|
|
pcall(hook)
|
|
end
|
|
if ri > 0 and u > 0 and u % ri == 0 then
|
|
rstIdx += 1
|
|
if rstIdx <= nrst then
|
|
pos = rst[rstIdx] * 8
|
|
skipping = false
|
|
end
|
|
for i = 1, nc do
|
|
comps[i].pred = 0
|
|
end
|
|
eobrun = 0
|
|
end
|
|
if pos > endBits then
|
|
truncated = true
|
|
if ri == 0 or rstIdx >= nrst then
|
|
break
|
|
end
|
|
skipping = true
|
|
end
|
|
if not skipping then
|
|
local my, mx = u // ux, u % ux
|
|
for ci = 1, nc do
|
|
local c = comps[ci]
|
|
local bh, bw, by0, bx0
|
|
if nc == 1 then
|
|
bh, bw, by0, bx0 = 1, 1, my, mx
|
|
else
|
|
bh, bw, by0, bx0 = c.v, c.h, my * c.v, mx * c.h
|
|
end
|
|
local coef, bpl, nz = c.coef, c.bpl, c.nz
|
|
for by = by0, by0 + bh - 1 do
|
|
for bx = bx0, bx0 + bw - 1 do
|
|
local bi = by * bpl + bx
|
|
local off = bi * 128
|
|
if mode == 0 then
|
|
pos, c.pred = blockSeq(d, pos, coef, off, c.dct :: buffer, c.act :: buffer, c.pred, nz, bi)
|
|
elseif mode == 1 then
|
|
pos, c.pred = blockDcFirst(d, pos, coef, off, c.dct :: buffer, c.pred, al)
|
|
elseif mode == 2 then
|
|
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
|
pos += 1
|
|
if b ~= 0 then
|
|
writei16(coef, off, bor(readi16(coef, off), bit))
|
|
end
|
|
elseif mode == 3 then
|
|
pos, eobrun = blockAcFirst(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun, nz, bi)
|
|
else
|
|
pos, eobrun = blockAcRefine(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun, nz, bi)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return pos, truncated
|
|
end
|
|
|
|
return Scan
|