perf(image): JPEG fused YCbCr upsampling/colour (8-bit), IDCT DC-only / 4x4 paths from per-block nonzero extent, progressive refine skips zero tails; PNG Paeth table + SWAR sub/avg, rgb8 convert, encoder filter costs by table; GIF palette table + opaque draw copy; WebP fancy upsampler inlined; ImageOps 8-bit RGBA resample, opaque premultiply skip, convert / nearest fast paths; tools/bench_image.luau
This commit is contained in:
1 parent
01c044805f
commit
9855a7edd6
12 files changed
+997
-193
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
|
||||
|
||||
Reference in new issue
Block a user