rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,253 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- image/png/Decode: the PNG pixel stage. Undo the per-row filters (none / sub / up / average / Paeth) in place in the
|
||||
-- inflated image data, then expand every colour type and bit depth to RGBA8, for one image or the 7 Adam7 passes.
|
||||
-- Absorbed from png-luau src/Png/Decode.luau (same author).
|
||||
--
|
||||
-- The caller (init) has validated the header and checked that the data holds at least the expected number of
|
||||
-- bytes, so every index here is in range. A bad filter type returns false, message: nothing in this module throws.
|
||||
local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift
|
||||
local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
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
|
||||
|
||||
-- bytes per row (without the filter byte) for a pass width
|
||||
function Decode.rowBytes(pw: number, channels: number, depth: number): number
|
||||
return (pw * channels * depth + 7) // 8
|
||||
end
|
||||
|
||||
-- Unfilter ph rows of rb bytes starting at raw[p] (each row: a filter byte, then rb bytes). bpp: bytes per
|
||||
-- complete pixel (at least 1). Returns true, or false + the bad filter type.
|
||||
local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: number): (boolean, number)
|
||||
local stride = rb + 1
|
||||
for r = 0, ph - 1 do
|
||||
local f = readu8(raw, p)
|
||||
local cur = p + 1
|
||||
if f == 0 then
|
||||
-- none
|
||||
elseif f == 1 then
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp))
|
||||
end
|
||||
elseif f == 2 then
|
||||
if r > 0 then
|
||||
local i = cur
|
||||
local e4 = cur + rb - 4
|
||||
while i <= e4 do
|
||||
-- four bytes at once: add without carries between the byte lanes
|
||||
local x, y = readu32(raw, i), readu32(raw, i - stride)
|
||||
local lo = band(x, 0x7F7F7F7F) + band(y, 0x7F7F7F7F)
|
||||
writeu32(raw, i, bit32.bxor(lo, band(bit32.bxor(x, y), 0x80808080)))
|
||||
i += 4
|
||||
end
|
||||
while i < cur + rb 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
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp) + readu8(raw, i - stride), 1))
|
||||
end
|
||||
else
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
writeu8(raw, i, readu8(raw, i) + rshift(readu8(raw, i - bpp), 1))
|
||||
end
|
||||
end
|
||||
elseif f == 4 then
|
||||
if r > 0 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
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
local a = readu8(raw, i - bpp)
|
||||
local b = readu8(raw, i - stride)
|
||||
local c = readu8(raw, i - stride - bpp)
|
||||
local pa = math.abs(b - c)
|
||||
local pb = math.abs(a - c)
|
||||
local pc = math.abs(a + b - c - c)
|
||||
local v
|
||||
if pa <= pb and pa <= pc then
|
||||
v = a
|
||||
elseif pb <= pc then
|
||||
v = b
|
||||
else
|
||||
v = c
|
||||
end
|
||||
writeu8(raw, i, readu8(raw, i) + v)
|
||||
end
|
||||
else
|
||||
-- no previous row: Paeth is sub
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp))
|
||||
end
|
||||
end
|
||||
else
|
||||
return false, f
|
||||
end
|
||||
p += stride
|
||||
end
|
||||
return true, 0
|
||||
end
|
||||
|
||||
-- Expand one unfiltered row (pw pixels at raw[ro]) to RGBA8 at out[oo], oo stepping by ostep bytes.
|
||||
-- pal: 256 RGBA8 entries (index -> u32 little-endian RGBA). tr: the tRNS key for grey / RGB (-1 if none),
|
||||
-- as a sample value (grey) or r * 2^32 + g * 2^16 + b (RGB), at the image's bit depth.
|
||||
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 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
|
||||
|
||||
-- Decode the inflated data raw (at least the expected size) of a w x h image to out (w * h * 4 bytes, RGBA8).
|
||||
-- Returns true, or false + message.
|
||||
function Decode.run(raw: buffer, w: number, h: number, depth: number, ct: number, interlace: number, pal: buffer, tr: number, out: buffer): (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
|
||||
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)
|
||||
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)
|
||||
end
|
||||
p += ph * (rb + 1)
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Decode
|
||||
Reference in new issue
Block a user