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:
4DBug committed 2026-11-01 19:24:15 -06:00
1 parent 01c044805f
commit 9855a7edd6
12 files changed
+997 -193

No files matched your search

+73 -25
View File
@@ -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