png: row-streaming decode through a streaming inflate (no whole inflated buffer, no IDAT concatenation); opts.maxSide / reduce (power-of-two box reduction), opts.yield / yieldRows

codecs/compress/Deflate: Inflate.raw takes an optional sink (output window: 32 KB history + a chunk) and feed (input
window over split ranges); Deflate.zlibStream(buf, ranges, sink, window?). The existing entry points are unchanged
(bench: no regression under luau -O2 or LuaJIT).
This commit is contained in:
4DBug committed 2026-11-02 22:48:27 -06:00
1 parent 89b363af78
commit e3e6273799
4 files changed
+422 -67

No files matched your search

+162 -16
View File
@@ -120,10 +120,10 @@ local function paeth1(raw: buffer, cur: number, e: number, stride: number, pt: b
end
end
local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: number): (boolean, number)
-- top = 0: the first row has no previous row; 1: the row before p is its previous row
local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: number, pt: buffer?, top: number): (boolean, number)
local stride = rb + 1
local pt = paethFor(ph * rb)
for r = 0, ph - 1 do
for r = top, top + ph - 1 do
local f = readu8(raw, p)
local cur = p + 1
local e = cur + rb
@@ -333,34 +333,180 @@ local function convert(raw: buffer, ro: number, pw: number, out: buffer, oo: num
end
end
function Decode.run(raw: buffer, w: number, h: number, depth: number, ct: number, interlace: number, pal: buffer, tr: number, out: buffer): (boolean, string?)
-- the k x k box reduction (k a power of two; edge blocks are smaller): the alpha-weighted mean, colour =
-- round(sum(c * a) / sum(a)) and alpha = round(sum(a) / n), halves up; a fully transparent block is 0, 0, 0, 0. sums
-- holds sum(c * a) and sum(a) per output pixel, or plain sums of r, g, b when the image is opaque (the same result).
local function accumRgba(row: buffer, ro: number, w: number, k: number, sums: { number }, opaque: boolean)
local i = 1
for x0 = 0, w - 1, k do
local e = ro + (math.min(x0 + k, w) - 1) * 4
local r, g, b, a = 0, 0, 0, 0
if opaque then
for q = ro + x0 * 4, e, 4 do
local v = readu32(row, q)
r += band(v, 255)
g += band(rshift(v, 8), 255)
b += band(rshift(v, 16), 255)
end
else
for q = ro + x0 * 4, e, 4 do
local v = readu32(row, q)
local al = rshift(v, 24)
r += band(v, 255) * al
g += band(rshift(v, 8), 255) * al
b += band(rshift(v, 16), 255) * al
a += al
end
end
sums[i] += r
sums[i + 1] += g
sums[i + 2] += b
sums[i + 3] += a
i += 4
end
end
-- truecolour without tRNS, straight from the unfiltered row (16-bit samples: the high byte, as convert does)
local function accumRgb(raw: buffer, ro: number, w: number, k: number, sums: { number }, depth: number)
local step = if depth == 16 then 6 else 3
local go = step // 3
local bo = go * 2
local i = 1
for x0 = 0, w - 1, k do
local r, g, b = 0, 0, 0
for q = ro + x0 * step, ro + (math.min(x0 + k, w) - 1) * step, step do
r += readu8(raw, q)
g += readu8(raw, q + go)
b += readu8(raw, q + bo)
end
sums[i] += r
sums[i + 1] += g
sums[i + 2] += b
i += 4
end
end
local function emit(sums: { number }, out: buffer, oo: number, w: number, k: number, bh: number, opaque: boolean)
local i = 1
for x0 = 0, w - 1, k do
local n = (math.min(x0 + k, w) - x0) * bh
local r, g, b, a = sums[i], sums[i + 1], sums[i + 2], sums[i + 3]
local v = 0
if opaque then
local hn = n // 2
v = (r + hn) // n + (g + hn) // n * 256 + (b + hn) // n * 65536 + 0xFF000000
elseif a > 0 then
local ha = a // 2
v = (r + ha) // a + (g + ha) // a * 256 + (b + ha) // a * 65536 + (a + n // 2) // n * 16777216
end
writeu32(out, oo, v)
oo += 4
sums[i], sums[i + 1], sums[i + 2], sums[i + 3] = 0, 0, 0, 0
i += 4
end
end
function Decode.opaque(ct: number, tr: number, pal: buffer): boolean
if ct == 4 or ct == 6 then
return false
elseif ct == 3 then
for i = 3, 1023, 4 do
if readu8(pal, i) ~= 255 then
return false
end
end
return true
end
return tr < 0
end
function Decode.reduce(src: buffer, w: number, h: number, k: number, out: buffer, opaque: boolean)
local ow = (w + k - 1) // k
local sums = table.create(ow * 4, 0)
for y = 0, h - 1 do
accumRgba(src, y * w * 4, w, k, sums, opaque)
if (y + 1) % k == 0 or y == h - 1 then
emit(sums, out, (y // k) * ow * 4, w, k, y % k + 1, opaque)
end
end
end
-- a row-streaming decoder of a non-interlaced image for a streaming inflate sink: unfilters each row against the
-- previous one, writes it (k = 1) or box-reduces it into out; hook is called every `every` rows
function Decode.stream(w: number, h: number, depth: number, ct: number, pal: buffer, tr: number, k: number, out: buffer, hook: (() -> ())?, every: number): ((buffer, number, number) -> number, () -> (number, string?))
local channels = CHANNELS[ct]
local bpp = math.max(1, (channels * depth) // 8)
local rb = Decode.rowBytes(w, channels, depth)
local stride = rb + 1
local pt = paethFor(h * rb)
local scratch = buffer.create(stride * 2)
local row = buffer.create(if k > 1 then w * 4 else 0)
local ow = (w + k - 1) // k
local sums = table.create(if k > 1 then ow * 4 else 0, 0)
local opaque = Decode.opaque(ct, tr, pal)
local direct = ct == 2 and tr < 0
local y, pending = 0, 0
local err: string? = nil
local function sink(win: buffer, from: number, to: number): number
local p = from - pending
while y < h and to - p >= stride do
bcopy(scratch, stride, win, p, stride)
local ok, f = unfilter(scratch, stride, 1, rb, bpp, pt, if y == 0 then 0 else 1)
if not ok then
err = "bad filter type " .. f
return -1
end
if k == 1 then
convert(scratch, stride + 1, w, out, y * w * 4, 4, ct, depth, pal, tr)
else
if direct then
accumRgb(scratch, stride + 1, w, k, sums, depth)
else
convert(scratch, stride + 1, w, row, 0, 4, ct, depth, pal, tr)
accumRgba(row, 0, w, k, sums, opaque)
end
if (y + 1) % k == 0 or y == h - 1 then
emit(sums, out, (y // k) * ow * 4, w, k, y % k + 1, opaque)
end
end
bcopy(scratch, 0, scratch, stride, stride)
y += 1
p += stride
if hook and y % every == 0 then
pcall(hook)
end
end
pending = if y < h then to - p else 0
return pending
end
return sink, function(): (number, string?)
return y, err
end
end
-- Adam7 (the whole inflated stream in raw)
function Decode.run(raw: buffer, w: number, h: number, depth: number, ct: number, pal: buffer, tr: number, out: buffer, hook: (() -> ())?, every: number): (boolean, string?)
local channels = CHANNELS[ct]
local bpp = math.max(1, (channels * depth) // 8)
local p = 0
if interlace == 0 then
local rb = Decode.rowBytes(w, channels, depth)
local ok, f = unfilter(raw, 0, h, rb, bpp)
if not ok then
return false, "bad filter type " .. f
end
for y = 0, h - 1 do
convert(raw, y * (rb + 1) + 1, w, out, y * w * 4, 4, ct, depth, pal, tr)
end
return true, nil
end
local rows = 0
for _, pass in PASSES do
local x0, y0, dx, dy = pass[1], pass[2], pass[3], pass[4]
local pw = if w > x0 then (w - x0 + dx - 1) // dx else 0
local ph = if h > y0 then (h - y0 + dy - 1) // dy else 0
if pw > 0 and ph > 0 then
local rb = Decode.rowBytes(pw, channels, depth)
local ok, f = unfilter(raw, p, ph, rb, bpp)
local ok, f = unfilter(raw, p, ph, rb, bpp, paethFor(ph * rb), 0)
if not ok then
return false, "bad filter type " .. f
end
for r = 0, ph - 1 do
local y = y0 + r * dy
convert(raw, p + r * (rb + 1) + 1, pw, out, (y * w + x0) * 4, dx * 4, ct, depth, pal, tr)
rows += 1
if hook and rows % every == 0 then
pcall(hook)
end
end
p += ph * (rb + 1)
end