merge ws/perf-image
This commit is contained in:
commit
d5276798c3
24 files changed
+5399
-2126
No files matched your search
@@ -23,21 +23,34 @@ end
|
||||
|
||||
function Decode.toRgba(indexes: buffer, fw: number, fh: number, interlaced: boolean, palette: buffer, out: buffer)
|
||||
local rows = if interlaced then interlacedRow(fh) else nil
|
||||
local pal = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
pal[i + 1] = readu32(palette, i * 4)
|
||||
end
|
||||
local o = 0
|
||||
for y = 0, fh - 1 do
|
||||
local src = (if rows then rows[y + 1] else y) * fw
|
||||
local dst = y * fw * 4
|
||||
for x = 0, fw - 1 do
|
||||
writeu32(out, dst + x * 4, readu32(palette, readu8(indexes, src + x) * 4))
|
||||
for i = src, src + fw - 1 do
|
||||
writeu32(out, o, pal[readu8(indexes, i) + 1])
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Decode.draw(canvas: buffer, cw: number, ch: number, frame: buffer, fx: number, fy: number, fw: number, fh: number)
|
||||
-- opaque: no palette entry has alpha 0 (no transparent index), so rows copy as they are
|
||||
function Decode.draw(canvas: buffer, cw: number, ch: number, frame: buffer, fx: number, fy: number, fw: number, fh: number, opaque: boolean?)
|
||||
local x0, y0 = math.max(0, fx), math.max(0, fy)
|
||||
local x1, y1 = math.min(cw, fx + fw), math.min(ch, fy + fh)
|
||||
if x1 <= x0 then
|
||||
return
|
||||
end
|
||||
for y = y0, y1 - 1 do
|
||||
local s = ((y - fy) * fw - fx) * 4
|
||||
local d = y * cw * 4
|
||||
if opaque then
|
||||
buffer.copy(canvas, d + x0 * 4, frame, s + x0 * 4, (x1 - x0) * 4)
|
||||
continue
|
||||
end
|
||||
for x = x0, x1 - 1 do
|
||||
local v = readu32(frame, s + x * 4)
|
||||
if v >= 0x01000000 then
|
||||
|
||||
@@ -253,7 +253,7 @@ function Gif.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
|
||||
local fimg = Types.Image.new(fw, fh, "rgba8")
|
||||
Decode.toRgba(idx, fw, fh, interlaced, t, fimg.pixels)
|
||||
if #frames == 0 then
|
||||
Decode.draw(img.pixels, cw, ch, fimg.pixels, fx, fy, fw, fh)
|
||||
Decode.draw(img.pixels, cw, ch, fimg.pixels, fx, fy, fw, fh, trans < 0)
|
||||
end
|
||||
table.insert(frames, {
|
||||
image = fimg,
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- image/png/Decode: PNG unfiltering and expansion to RGBA8 (absorbed from png-luau src/Png/Decode.luau, same author).
|
||||
local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift
|
||||
local readu8, writeu8, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu32, buffer.writeu32
|
||||
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 = {}
|
||||
@@ -27,27 +28,148 @@ 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
|
||||
|
||||
-- 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
|
||||
|
||||
local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: number): (boolean, number)
|
||||
local stride = rb + 1
|
||||
local pt = paethFor(ph * rb)
|
||||
for r = 0, ph - 1 do
|
||||
local f = readu8(raw, p)
|
||||
local cur = p + 1
|
||||
local e = cur + rb
|
||||
if f == 0 then
|
||||
elseif f == 1 then
|
||||
for i = cur + bpp, cur + rb - 1 do
|
||||
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - bpp))
|
||||
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 = cur + rb - 4
|
||||
local e4 = e - 4
|
||||
while i <= e4 do
|
||||
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)))
|
||||
writeu32(raw, i, add4(readu32(raw, i), readu32(raw, i - stride)))
|
||||
i += 4
|
||||
end
|
||||
while i < cur + rb do
|
||||
while i < e do
|
||||
writeu8(raw, i, readu8(raw, i) + readu8(raw, i - stride))
|
||||
i += 1
|
||||
end
|
||||
@@ -57,42 +179,44 @@ local function unfilter(raw: buffer, p: number, ph: number, rb: number, bpp: num
|
||||
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))
|
||||
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, cur + rb - 1 do
|
||||
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
|
||||
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)
|
||||
-- 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 == 1 then
|
||||
paeth1(raw, cur, e, stride, pt)
|
||||
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
|
||||
paethN(raw, cur, e, stride, bpp, pt)
|
||||
end
|
||||
else
|
||||
return false, f
|
||||
@@ -123,7 +247,16 @@ local function convert(raw: buffer, ro: number, pw: number, out: buffer, oo: num
|
||||
end
|
||||
end
|
||||
elseif ct == 2 then
|
||||
if depth == 8 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)
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- image/png/Encode: PNG colour type choice and filtering (per-row minimum sum of |signed residuals|, as libpng).
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local Decode = require("@rosetta/image/png/Decode")
|
||||
|
||||
local readu8, writeu8, readi16 = buffer.readu8, buffer.writeu8, buffer.readi16
|
||||
local abs = math.abs
|
||||
local paethFor = Decode.paethFor
|
||||
local bcreate, bcopy = buffer.create, buffer.copy
|
||||
|
||||
local Encode = {}
|
||||
@@ -66,36 +70,58 @@ local function paeth(a: number, b: number, c: number): number
|
||||
return c
|
||||
end
|
||||
|
||||
local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean): (number, number, number, number, number)
|
||||
-- |residual as a signed byte| by residual + 256 (residual in -255 .. 255)
|
||||
local SABS = table.create(511, 0)
|
||||
for d = -255, 255 do
|
||||
local m = d % 256
|
||||
SABS[d + 256] = if m < 128 then m else 256 - m
|
||||
end
|
||||
|
||||
local function costs(src: buffer, s: number, rb: number, bpp: number, first: boolean, pt: buffer?): (number, number, number, number, number)
|
||||
local c0, c1, c2, c3, c4 = 0, 0, 0, 0, 0
|
||||
local p = s - rb
|
||||
for i = 0, rb - 1 do
|
||||
local x = readu8(src, s + i)
|
||||
local a = if i >= bpp then readu8(src, s + i - bpp) else 0
|
||||
local b, c = 0, 0
|
||||
if not first then
|
||||
b = readu8(src, p + i)
|
||||
if i >= bpp then
|
||||
c = readu8(src, p + i - bpp)
|
||||
local sabs = SABS
|
||||
if first or rb <= bpp or not pt then
|
||||
for i = 0, rb - 1 do
|
||||
local x = readu8(src, s + i)
|
||||
local a = if i >= bpp then readu8(src, s + i - bpp) else 0
|
||||
local b, c = 0, 0
|
||||
if not first then
|
||||
b = readu8(src, p + i)
|
||||
if i >= bpp then
|
||||
c = readu8(src, p + i - bpp)
|
||||
end
|
||||
end
|
||||
c0 += sabs[x + 256]
|
||||
c1 += sabs[x - a + 256]
|
||||
c2 += sabs[x - b + 256]
|
||||
c3 += sabs[x - (a + b) // 2 + 256]
|
||||
local pa, pb, pc = abs(b - c), abs(a - c), abs(a + b - c - c)
|
||||
c4 += sabs[x - (if pa <= pb and pa <= pc then a elseif pb <= pc then b else c) + 256]
|
||||
end
|
||||
local d = if x < 128 then x else 256 - x
|
||||
c0 += d
|
||||
d = (x - a) % 256
|
||||
c1 += if d < 128 then d else 256 - d
|
||||
d = (x - b) % 256
|
||||
c2 += if d < 128 then d else 256 - d
|
||||
d = (x - (a + b) // 2) % 256
|
||||
c3 += if d < 128 then d else 256 - d
|
||||
local pa, pb, pc = math.abs(b - c), math.abs(a - c), math.abs(a + b - c - c)
|
||||
local pr = if pa <= pb and pa <= pc then a elseif pb <= pc then b else c
|
||||
d = (x - pr) % 256
|
||||
c4 += if d < 128 then d else 256 - d
|
||||
return c0, c1, c2, c3, c4
|
||||
end
|
||||
for i = 0, bpp - 1 do
|
||||
local x, b = readu8(src, s + i), readu8(src, p + i)
|
||||
c0 += sabs[x + 256]
|
||||
c1 += sabs[x + 256]
|
||||
c2 += sabs[x - b + 256]
|
||||
c3 += sabs[x - b // 2 + 256]
|
||||
c4 += sabs[x - b + 256]
|
||||
end
|
||||
for i = s + bpp, s + rb - 1 do
|
||||
local x, a = readu8(src, i), readu8(src, i - bpp)
|
||||
local b, c = readu8(src, i - rb), readu8(src, i - rb - bpp)
|
||||
c0 += sabs[x + 256]
|
||||
c1 += sabs[x - a + 256]
|
||||
c2 += sabs[x - b + 256]
|
||||
c3 += sabs[x - (a + b) // 2 + 256]
|
||||
c4 += sabs[x - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120) + 256]
|
||||
end
|
||||
return c0, c1, c2, c3, c4
|
||||
end
|
||||
|
||||
local function apply(src: buffer, s: number, rb: number, bpp: number, first: boolean, f: number, out: buffer, o: number)
|
||||
local function apply(src: buffer, s: number, rb: number, bpp: number, first: boolean, f: number, out: buffer, o: number, pt: buffer?)
|
||||
writeu8(out, o, f)
|
||||
o += 1
|
||||
if f == 0 or (f == 2 and first) then
|
||||
@@ -103,6 +129,27 @@ local function apply(src: buffer, s: number, rb: number, bpp: number, first: boo
|
||||
return
|
||||
end
|
||||
local p = s - rb
|
||||
if not first and rb > bpp and f ~= 3 and pt then
|
||||
for i = 0, bpp - 1 do
|
||||
writeu8(out, o + i, readu8(src, s + i) - (if f == 1 then 0 else readu8(src, p + i)))
|
||||
end
|
||||
local d = o - s
|
||||
if f == 1 then
|
||||
for i = s + bpp, s + rb - 1 do
|
||||
writeu8(out, i + d, readu8(src, i) - readu8(src, i - bpp))
|
||||
end
|
||||
elseif f == 2 then
|
||||
for i = s + bpp, s + rb - 1 do
|
||||
writeu8(out, i + d, readu8(src, i) - readu8(src, i - rb))
|
||||
end
|
||||
else
|
||||
for i = s + bpp, s + rb - 1 do
|
||||
local a, b, c = readu8(src, i - bpp), readu8(src, i - rb), readu8(src, i - rb - bpp)
|
||||
writeu8(out, i + d, readu8(src, i) - c - readi16(pt, (a * 511 + b - c * 512) * 2 + 261120))
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
for i = 0, rb - 1 do
|
||||
local x = readu8(src, s + i)
|
||||
local a = if i >= bpp then readu8(src, s + i - bpp) else 0
|
||||
@@ -128,10 +175,11 @@ function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer
|
||||
local src = pack(px, w, h, ct)
|
||||
local rb = w * ch
|
||||
local out = bcreate(h * (rb + 1))
|
||||
local pt = paethFor(h * rb)
|
||||
for y = 0, h - 1 do
|
||||
local s = y * rb
|
||||
local first = y == 0
|
||||
local c0, c1, c2, c3, c4 = costs(src, s, rb, ch, first)
|
||||
local c0, c1, c2, c3, c4 = costs(src, s, rb, ch, first, pt)
|
||||
local best, bc = 0, c0
|
||||
if c1 < bc then
|
||||
best, bc = 1, c1
|
||||
@@ -145,7 +193,7 @@ function Encode.filter(px: buffer, w: number, h: number, ct: number): buffer
|
||||
if c4 < bc then
|
||||
best = 4
|
||||
end
|
||||
apply(src, s, rb, ch, first, best, out, y * (rb + 1))
|
||||
apply(src, s, rb, ch, first, best, out, y * (rb + 1), pt)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
@@ -8,8 +8,14 @@ local Codec = {}
|
||||
|
||||
local index = buffer.create(256)
|
||||
|
||||
-- the decoder's index as four tables (1-based by hash + 1)
|
||||
local IR, IG, IB, IA = table.create(64, 0), table.create(64, 0), table.create(64, 0), table.create(64, 0)
|
||||
|
||||
function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: number): (boolean, string?)
|
||||
buffer.fill(index, 0, 0, 256)
|
||||
local ir, ig, ib, ia = IR, IG, IB, IA
|
||||
for i = 1, 64 do
|
||||
ir[i], ig[i], ib[i], ia[i] = 0, 0, 0, 0
|
||||
end
|
||||
local r, g, bl, a = 0, 0, 0, 255
|
||||
local o = 0
|
||||
local stop = n * 4
|
||||
@@ -19,62 +25,63 @@ function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: numbe
|
||||
end
|
||||
local op = readu8(b, p)
|
||||
p += 1
|
||||
if op == 0xFE then
|
||||
if p + 3 > limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
r = readu8(b, p)
|
||||
g = readu8(b, p + 1)
|
||||
bl = readu8(b, p + 2)
|
||||
p += 3
|
||||
elseif op == 0xFF then
|
||||
if p + 4 > limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
r = readu8(b, p)
|
||||
g = readu8(b, p + 1)
|
||||
bl = readu8(b, p + 2)
|
||||
a = readu8(b, p + 3)
|
||||
p += 4
|
||||
else
|
||||
local tag = op // 64
|
||||
if tag == 0 then
|
||||
local q = op * 4
|
||||
r = readu8(index, q)
|
||||
g = readu8(index, q + 1)
|
||||
bl = readu8(index, q + 2)
|
||||
a = readu8(index, q + 3)
|
||||
elseif tag == 1 then
|
||||
r = (r + (op // 16) % 4 - 2) % 256
|
||||
g = (g + (op // 4) % 4 - 2) % 256
|
||||
bl = (bl + op % 4 - 2) % 256
|
||||
elseif tag == 2 then
|
||||
if p >= limit then
|
||||
if op >= 0xC0 then
|
||||
if op == 0xFE then
|
||||
if p + 3 > limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
local d2 = readu8(b, p)
|
||||
p += 1
|
||||
local dg = op % 64 - 32
|
||||
g = (g + dg) % 256
|
||||
r = (r + dg + d2 // 16 - 8) % 256
|
||||
bl = (bl + dg + d2 % 16 - 8) % 256
|
||||
r = readu8(b, p)
|
||||
g = readu8(b, p + 1)
|
||||
bl = readu8(b, p + 2)
|
||||
p += 3
|
||||
elseif op == 0xFF then
|
||||
if p + 4 > limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
r = readu8(b, p)
|
||||
g = readu8(b, p + 1)
|
||||
bl = readu8(b, p + 2)
|
||||
a = readu8(b, p + 3)
|
||||
p += 4
|
||||
else
|
||||
local run = op % 64 -- 1..62 more copies of the previous pixel (63 / 64 are RGB / RGBA)
|
||||
-- a run of 1..62 copies of the previous pixel (63 / 64 are RGB / RGBA); every op stores its pixel at its
|
||||
-- hash, so the index holds it already unless the run is the first op
|
||||
local v = r + g * 256 + bl * 65536 + a * 16777216
|
||||
local e = math.min(o + run * 4, stop - 4)
|
||||
local first = o == 0
|
||||
local e = math.min(o + (op - 0xBF) * 4, stop)
|
||||
while o < e do
|
||||
writeu32(out, o, v)
|
||||
o += 4
|
||||
end
|
||||
if not first then
|
||||
continue
|
||||
end
|
||||
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
|
||||
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
|
||||
continue
|
||||
end
|
||||
elseif op < 0x40 then
|
||||
local q = op + 1
|
||||
r, g, bl, a = ir[q], ig[q], ib[q], ia[q]
|
||||
elseif op < 0x80 then
|
||||
r = (r + (op // 16) % 4 - 2) % 256
|
||||
g = (g + (op // 4) % 4 - 2) % 256
|
||||
bl = (bl + op % 4 - 2) % 256
|
||||
else
|
||||
if p >= limit then
|
||||
return false, "truncated"
|
||||
end
|
||||
local d2 = readu8(b, p)
|
||||
p += 1
|
||||
local dg = op % 64 - 32
|
||||
g = (g + dg) % 256
|
||||
r = (r + dg + d2 // 16 - 8) % 256
|
||||
bl = (bl + dg + d2 % 16 - 8) % 256
|
||||
end
|
||||
writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216)
|
||||
o += 4
|
||||
local h = ((r * 3 + g * 5 + bl * 7 + a * 11) % 64) * 4
|
||||
writeu8(index, h, r)
|
||||
writeu8(index, h + 1, g)
|
||||
writeu8(index, h + 2, bl)
|
||||
writeu8(index, h + 3, a)
|
||||
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
|
||||
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
@@ -33,22 +33,44 @@ Codec.pixel = pixel
|
||||
function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number, bpp: number, kind: number, alpha1: boolean, rle: boolean, bottomUp: boolean, rtl: boolean, pal: buffer, palN: number, out: buffer): (number, boolean)
|
||||
local n = w * h
|
||||
local i = 0
|
||||
local function put(v: number)
|
||||
local row, col = i // w, i % w
|
||||
if bottomUp then
|
||||
row = h - 1 - row
|
||||
end
|
||||
if rtl then
|
||||
col = w - 1 - col
|
||||
end
|
||||
writeu32(out, (row * w + col) * 4, v)
|
||||
i += 1
|
||||
-- the output position of pixel i: row by row (bottom-up flips rows), each row left to right or right to left
|
||||
local step = if rtl then -4 else 4
|
||||
local row, col = 0, 0
|
||||
local function rowStart(r: number): number
|
||||
return ((if bottomUp then h - 1 - r else r) * w + (if rtl then w - 1 else 0)) * 4
|
||||
end
|
||||
local o = rowStart(0)
|
||||
if not rle then
|
||||
local avail = math.min(n, (limit - p) // bpp)
|
||||
for _ = 1, avail do
|
||||
put(pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
while i < avail do
|
||||
local e = math.min(avail - i, w - col)
|
||||
if kind == 3 then
|
||||
for _ = 1, e do
|
||||
writeu32(out, o, readu8(b, p + 2) + readu8(b, p + 1) * 256 + readu8(b, p) * 65536 + 0xFF000000)
|
||||
p += 3
|
||||
o += step
|
||||
end
|
||||
elseif kind == 4 then
|
||||
for _ = 1, e do
|
||||
local v = readu32(b, p)
|
||||
writeu32(out, o, v - v % 16777216 + (v // 65536) % 256 + v % 65536 - v % 256 + (v % 256) * 65536)
|
||||
p += 4
|
||||
o += step
|
||||
end
|
||||
else
|
||||
for _ = 1, e do
|
||||
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
o += step
|
||||
end
|
||||
end
|
||||
i += e
|
||||
col += e
|
||||
if col == w then
|
||||
col = 0
|
||||
row += 1
|
||||
o = rowStart(row)
|
||||
end
|
||||
end
|
||||
return i, avail < n
|
||||
end
|
||||
@@ -58,23 +80,49 @@ function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number,
|
||||
end
|
||||
local hd = readu8(b, p)
|
||||
p += 1
|
||||
local count = hd % 128 + 1
|
||||
if hd >= 128 then
|
||||
local count = math.min(hd % 128 + 1, n - i)
|
||||
local packed = hd >= 128
|
||||
local v = 0
|
||||
if packed then
|
||||
if p + bpp > limit then
|
||||
return i, true
|
||||
end
|
||||
local v = pixel(b, p, kind, alpha1, pal, palN)
|
||||
v = pixel(b, p, kind, alpha1, pal, palN)
|
||||
p += bpp
|
||||
for _ = 1, math.min(count, n - i) do
|
||||
put(v)
|
||||
end
|
||||
while count > 0 do
|
||||
local e = math.min(count, w - col)
|
||||
if packed then
|
||||
for _ = 1, e do
|
||||
writeu32(out, o, v)
|
||||
o += step
|
||||
end
|
||||
else
|
||||
if p + e * bpp > limit then
|
||||
-- the pixels that are there, then truncated
|
||||
local k = (limit - p) // bpp
|
||||
for _ = 1, k do
|
||||
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
o += step
|
||||
end
|
||||
return i + k, true
|
||||
end
|
||||
for _ = 1, e do
|
||||
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
o += step
|
||||
end
|
||||
end
|
||||
else
|
||||
for _ = 1, math.min(count, n - i) do
|
||||
if p + bpp > limit then
|
||||
return i, true
|
||||
i += e
|
||||
col += e
|
||||
count -= e
|
||||
if col == w then
|
||||
col = 0
|
||||
row += 1
|
||||
if row < h then
|
||||
o = rowStart(row)
|
||||
end
|
||||
put(pixel(b, p, kind, alpha1, pal, palN))
|
||||
p += bpp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user