Files
rosetta/src/Rosetta/image/png/Decode.luau
T

544 lines
18 KiB
Luau

--!strict
--!native
--!optimize 2
-- image/png/Decode: PNG unfiltering and expansion to RGBA8 (absorbed from png-luau src/Png/Decode.luau, same author).
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
local abs = math.abs
local readu8, writeu8, readu32, writeu32, readi16, writei16 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32, buffer.readi16, buffer.writei16
local bcopy = buffer.copy
local Decode = {}
-- Adam7: x0, y0, dx, dy per pass
local PASSES = {
{ 0, 0, 8, 8 },
{ 4, 0, 8, 8 },
{ 0, 4, 4, 8 },
{ 2, 0, 4, 4 },
{ 0, 2, 2, 4 },
{ 1, 0, 2, 2 },
{ 0, 1, 1, 2 },
}
Decode.PASSES = PASSES
local CHANNELS = { [0] = 1, [2] = 3, [3] = 1, [4] = 2, [6] = 4 }
Decode.CHANNELS = CHANNELS
function Decode.rowBytes(pw: number, channels: number, depth: number): number
return (pw * channels * depth + 7) // 8
end
-- per-byte sums mod 256 of four packed bytes; floor((a + b) / 2) per byte
local function add4(x: number, y: number): number
return bxor(band(x, 0x7F7F7F7F) + band(y, 0x7F7F7F7F), band(bxor(x, y), 0x80808080))
end
-- Paeth as a table: v - c by (a - c, b - c), i16 at ((a - c + 255) * 511 + b - c + 255) * 2; built on the first large image
local PT: buffer? = nil
local function paethTable(): buffer
local t = PT
if t then
return t
end
local nt = buffer.create(511 * 511 * 2)
for da = -255, 255 do
for db = -255, 255 do
local pa, pb, pc = abs(db), abs(da), abs(da + db)
writei16(nt, ((da + 255) * 511 + db + 255) * 2, if pa <= pb and pa <= pc then da elseif pb <= pc then db else 0)
end
end
PT = nt
return nt
end
-- the table for n bytes of image data: nil (compute instead) for small images while it is not built yet
local function paethFor(n: number): buffer?
return if PT or n >= 262144 then paethTable() else nil
end
Decode.paethFor = paethFor
local function paeth(a: number, b: number, c: number): number
local pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - c - c)
return if pa <= pb and pa <= pc then a elseif pb <= pc then b else c
end
-- Paeth on four-byte pixels: the left (a) and upper-left (c) bytes carried, the row read and written as u32
local function paeth4(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
local a0, a1, a2, a3 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2), readu8(raw, cur + 3)
local up = readu32(raw, cur - stride)
local c0, c1, c2, c3 = up % 256, up // 256 % 256, up // 65536 % 256, up // 16777216
for i = cur + 4, e - 4, 4 do
local x, bw = readu32(raw, i), readu32(raw, i - stride)
local b = bw % 256
a0 = (x + c0 + readi16(pt, (a0 * 511 + b - c0 * 512) * 2 + 261120)) % 256
c0 = b
b = bw // 256 % 256
a1 = (x // 256 + c1 + readi16(pt, (a1 * 511 + b - c1 * 512) * 2 + 261120)) % 256
c1 = b
b = bw // 65536 % 256
a2 = (x // 65536 + c2 + readi16(pt, (a2 * 511 + b - c2 * 512) * 2 + 261120)) % 256
c2 = b
b = bw // 16777216
a3 = (x // 16777216 + c3 + readi16(pt, (a3 * 511 + b - c3 * 512) * 2 + 261120)) % 256
c3 = b
writeu32(raw, i, a0 + a1 * 256 + a2 * 65536 + a3 * 16777216)
end
end
local function paeth3(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
local a0, a1, a2 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2)
local u = cur - stride
local c0, c1, c2 = readu8(raw, u), readu8(raw, u + 1), readu8(raw, u + 2)
for i = cur + 3, e - 3, 3 do
local j = i - stride
local b0, b1, b2 = readu8(raw, j), readu8(raw, j + 1), readu8(raw, j + 2)
a0 = (readu8(raw, i) + c0 + readi16(pt, (a0 * 511 + b0 - c0 * 512) * 2 + 261120)) % 256
a1 = (readu8(raw, i + 1) + c1 + readi16(pt, (a1 * 511 + b1 - c1 * 512) * 2 + 261120)) % 256
a2 = (readu8(raw, i + 2) + c2 + readi16(pt, (a2 * 511 + b2 - c2 * 512) * 2 + 261120)) % 256
writeu8(raw, i, a0)
writeu8(raw, i + 1, a1)
writeu8(raw, i + 2, a2)
c0, c1, c2 = b0, b1, b2
end
end
-- 16-bit RGB: two three-byte lanes carried as in paeth3
local function paeth6(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
local a0, a1, a2, a3, a4, a5 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2), readu8(raw, cur + 3), readu8(raw, cur + 4), readu8(raw, cur + 5)
local u = cur - stride
local c0, c1, c2, c3, c4, c5 = readu8(raw, u), readu8(raw, u + 1), readu8(raw, u + 2), readu8(raw, u + 3), readu8(raw, u + 4), readu8(raw, u + 5)
for i = cur + 6, e - 6, 6 do
local j = i - stride
local b0, b1, b2, b3, b4, b5 = readu8(raw, j), readu8(raw, j + 1), readu8(raw, j + 2), readu8(raw, j + 3), readu8(raw, j + 4), readu8(raw, j + 5)
a0 = (readu8(raw, i) + c0 + readi16(pt, (a0 * 511 + b0 - c0 * 512) * 2 + 261120)) % 256
a1 = (readu8(raw, i + 1) + c1 + readi16(pt, (a1 * 511 + b1 - c1 * 512) * 2 + 261120)) % 256
a2 = (readu8(raw, i + 2) + c2 + readi16(pt, (a2 * 511 + b2 - c2 * 512) * 2 + 261120)) % 256
a3 = (readu8(raw, i + 3) + c3 + readi16(pt, (a3 * 511 + b3 - c3 * 512) * 2 + 261120)) % 256
a4 = (readu8(raw, i + 4) + c4 + readi16(pt, (a4 * 511 + b4 - c4 * 512) * 2 + 261120)) % 256
a5 = (readu8(raw, i + 5) + c5 + readi16(pt, (a5 * 511 + b5 - c5 * 512) * 2 + 261120)) % 256
writeu8(raw, i, a0)
writeu8(raw, i + 1, a1)
writeu8(raw, i + 2, a2)
writeu8(raw, i + 3, a3)
writeu8(raw, i + 4, a4)
writeu8(raw, i + 5, a5)
c0, c1, c2, c3, c4, c5 = b0, b1, b2, b3, b4, b5
end
end
-- any bpp: the left byte is the one written bpp bytes earlier
local function paethN(raw: buffer, cur: number, e: number, stride: number, bpp: number, pt: buffer)
for i = cur + bpp, e - 1 do
local a, b, c = readu8(raw, i - bpp), readu8(raw, i - stride), readu8(raw, i - stride - bpp)
writeu8(raw, i, readu8(raw, i) + c + readi16(pt, (a * 511 + b - c * 512) * 2 + 261120))
end
end
local function paeth1(raw: buffer, cur: number, e: number, stride: number, pt: buffer)
local a, c = readu8(raw, cur), readu8(raw, cur - stride)
for i = cur + 1, e - 1 do
local b = readu8(raw, i - stride)
a = (readu8(raw, i) + c + readi16(pt, (a * 511 + b - c * 512) * 2 + 261120)) % 256
writeu8(raw, i, a)
c = b
end
end
-- 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
for r = top, top + ph - 1 do
local f = readu8(raw, p)
local cur = p + 1
local e = cur + rb
if f == 0 then
elseif f == 1 or (f == 4 and r == 0) then
-- Paeth without a previous row is sub
if bpp >= 4 then
local i = cur + bpp
while i <= e - 4 do
writeu32(raw, i, add4(readu32(raw, i), readu32(raw, i - bpp)))
i += 4
end
while i < e do
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp))
i += 1
end
elseif bpp == 1 then
local a = readu8(raw, cur)
for i = cur + 1, e - 1 do
a = (readu8(raw, i) + a) % 256
writeu8(raw, i, a)
end
elseif bpp == 3 then
local a0, a1, a2 = readu8(raw, cur), readu8(raw, cur + 1), readu8(raw, cur + 2)
for i = cur + 3, e - 3, 3 do
a0 = (readu8(raw, i) + a0) % 256
a1 = (readu8(raw, i + 1) + a1) % 256
a2 = (readu8(raw, i + 2) + a2) % 256
writeu8(raw, i, a0)
writeu8(raw, i + 1, a1)
writeu8(raw, i + 2, a2)
end
else
for i = cur + bpp, e - 1 do
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp))
end
end
elseif f == 2 then
if r > 0 then
local i = cur
local e4 = e - 4
while i <= e4 do
writeu32(raw, i, add4(readu32(raw, i), readu32(raw, i - stride)))
i += 4
end
while i < e do
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride))
i += 1
end
end
elseif f == 3 then
if r > 0 then
for i = cur, cur + bpp - 1 do
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - stride), 1))
end
if bpp >= 4 then
local i = cur + bpp
while i <= e - 4 do
local a, b = readu32(raw, i - bpp), readu32(raw, i - stride)
writeu32(raw, i, add4(readu32(raw, i), band(a, b) + rshift(band(bxor(a, b), 0xFEFEFEFE), 1)))
i += 4
end
while i < e do
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1))
i += 1
end
else
for i = cur + bpp, e - 1 do
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1))
end
end
else
for i = cur + bpp, e - 1 do
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp), 1))
end
end
elseif f == 4 then
-- the first pixel: a = c = 0, so Paeth picks b
for i = cur, cur + bpp - 1 do
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride))
end
if not pt then
for i = cur + bpp, e - 1 do
writeu8(raw, i, readu8(raw, i) + paeth(readu8(raw, i - bpp), readu8(raw, i - stride), readu8(raw, i - stride - bpp)))
end
elseif bpp == 4 then
paeth4(raw, cur, e, stride, pt)
elseif bpp == 3 then
paeth3(raw, cur, e, stride, pt)
elseif bpp == 6 then
paeth6(raw, cur, e, stride, pt)
elseif bpp == 1 then
paeth1(raw, cur, e, stride, pt)
else
paethN(raw, cur, e, stride, bpp, pt)
end
else
return false, f
end
p += stride
end
return true, 0
end
local function convert(raw: buffer, ro: number, pw: number, out: buffer, oo: number, ostep: number, ct: number, depth: number, pal: buffer, tr: number)
if ct == 6 then
if depth == 8 then
if ostep == 4 then
bcopy(out, oo, raw, ro, pw * 4)
else
for x = 0, pw - 1 do
writeu32(out, oo + x * ostep, readu32(raw, ro + x * 4))
end
end
else
for x = 0, pw - 1 do
local s = ro + x * 8
local o = oo + x * ostep
writeu8(out, o, readu8(raw, s))
writeu8(out, o + 1, readu8(raw, s + 2))
writeu8(out, o + 2, readu8(raw, s + 4))
writeu8(out, o + 3, readu8(raw, s + 6))
end
end
elseif ct == 2 then
if depth == 8 and tr < 0 then
-- the u32 at a pixel's last byte + 1 is the next pixel's (or the next row's filter byte), except at the end
local o = oo
for s = ro, ro + (pw - 2) * 3, 3 do
writeu32(out, o, readu32(raw, s) % 16777216 + 0xFF000000)
o += ostep
end
local s = ro + (pw - 1) * 3
writeu32(out, o, readu8(raw, s) + readu8(raw, s + 1) * 256 + readu8(raw, s + 2) * 65536 + 0xFF000000)
elseif depth == 8 then
for x = 0, pw - 1 do
local s = ro + x * 3
local r, g, b = readu8(raw, s), readu8(raw, s + 1), readu8(raw, s + 2)
local a = 255
if tr >= 0 and r * 4294967296 + g * 65536 + b == tr then
a = 0
end
writeu32(out, oo + x * ostep, r + g * 256 + b * 65536 + a * 16777216)
end
else
for x = 0, pw - 1 do
local s = ro + x * 6
local r = readu8(raw, s) * 256 + readu8(raw, s + 1)
local g = readu8(raw, s + 2) * 256 + readu8(raw, s + 3)
local b = readu8(raw, s + 4) * 256 + readu8(raw, s + 5)
local a = 255
if tr >= 0 and r * 4294967296 + g * 65536 + b == tr then
a = 0
end
writeu32(out, oo + x * ostep, rshift(r, 8) + band(g, 0xFF00) + lshift(rshift(b, 8), 16) + a * 16777216)
end
end
elseif ct == 3 then
if depth == 8 then
for x = 0, pw - 1 do
writeu32(out, oo + x * ostep, readu32(pal, readu8(raw, ro + x) * 4))
end
else
local per = 8 // depth
local mask = lshift(1, depth) - 1
for x = 0, pw - 1 do
local byte = readu8(raw, ro + x // per)
local idx = band(rshift(byte, 8 - depth - (x % per) * depth), mask)
writeu32(out, oo + x * ostep, readu32(pal, idx * 4))
end
end
elseif ct == 0 then
if depth == 8 then
for x = 0, pw - 1 do
local v = readu8(raw, ro + x)
local a = if v == tr then 0 else 255
writeu32(out, oo + x * ostep, v * 0x10101 + a * 16777216)
end
elseif depth == 16 then
for x = 0, pw - 1 do
local hi = readu8(raw, ro + x * 2)
local v = hi * 256 + readu8(raw, ro + x * 2 + 1)
local a = if v == tr then 0 else 255
writeu32(out, oo + x * ostep, hi * 0x10101 + a * 16777216)
end
else
local per = 8 // depth
local mask = lshift(1, depth) - 1
local scale = 255 // mask
for x = 0, pw - 1 do
local byte = readu8(raw, ro + x // per)
local v = band(rshift(byte, 8 - depth - (x % per) * depth), mask)
local a = if v == tr then 0 else 255
writeu32(out, oo + x * ostep, v * scale * 0x10101 + a * 16777216)
end
end
else -- ct == 4: grey + alpha
if depth == 8 then
for x = 0, pw - 1 do
local v, a = readu8(raw, ro + x * 2), readu8(raw, ro + x * 2 + 1)
writeu32(out, oo + x * ostep, v * 0x10101 + a * 16777216)
end
else
for x = 0, pw - 1 do
local v, a = readu8(raw, ro + x * 4), readu8(raw, ro + x * 4 + 2)
writeu32(out, oo + x * ostep, v * 0x10101 + a * 16777216)
end
end
end
end
-- 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
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, 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
end
return true, nil
end
return Decode