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
@@ -5,6 +5,7 @@
|
||||
-- Sample kinds: 1 = u8, 2 = u16, 4 = f32 (little endian). Resampling follows Pillow's Resample.c (separable, 22-bit
|
||||
-- fixed point for 8-bit) and Geometry.c (affine through pixel centres); compositing is Pillow's AlphaComposite.c.
|
||||
local readu8, writeu8, readu16, writeu16, readf32, writef32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readf32, buffer.writef32
|
||||
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
||||
local floor, clamp = math.floor, math.clamp
|
||||
|
||||
local K = {}
|
||||
@@ -69,6 +70,32 @@ local function luma(r: number, g: number, b: number, depth: number): number
|
||||
end
|
||||
|
||||
function K.convert(src: buffer, sf: number, dst: buffer, df: number, n: number)
|
||||
if sf == 1 and df == 5 then
|
||||
for i = 0, n - 1 do
|
||||
local o = i * 4
|
||||
writeu8(dst, i, (readu8(src, o) * 19595 + readu8(src, o + 1) * 38470 + readu8(src, o + 2) * 7471 + 32768) // 65536)
|
||||
end
|
||||
return
|
||||
elseif sf == 1 and df == 4 and n > 0 then
|
||||
-- a u32 per pixel; its fourth byte is overwritten by the next pixel (the last one is written as 2 + 1 bytes)
|
||||
for i = 0, n - 2 do
|
||||
writeu32(dst, i * 3, readu32(src, i * 4))
|
||||
end
|
||||
local o, d = (n - 1) * 4, (n - 1) * 3
|
||||
writeu16(dst, d, readu16(src, o))
|
||||
writeu8(dst, d + 2, readu8(src, o + 2))
|
||||
return
|
||||
elseif df == 1 and (sf == 4 or sf == 5) then
|
||||
for i = 0, n - 1 do
|
||||
if sf == 4 then
|
||||
local o = i * 3
|
||||
writeu32(dst, i * 4, readu8(src, o) + readu8(src, o + 1) * 256 + readu8(src, o + 2) * 65536 + 0xFF000000)
|
||||
else
|
||||
writeu32(dst, i * 4, readu8(src, i) * 0x10101 + 0xFF000000)
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
local sd, dd = DEPTH[sf] :: number, DEPTH[df] :: number
|
||||
local smax = if sd == 8 then 255 elseif sd == 16 then 65535 else 1
|
||||
local toGray = df == 5 or df == 6 or df == 7
|
||||
@@ -295,6 +322,52 @@ function K.coeffs(inSize: number, outSize: number, filter: string): Coeffs
|
||||
return { start = start, count = count, weights = weights }
|
||||
end
|
||||
|
||||
-- 8-bit RGBA: the four channels of a tap together, the pixel written as one u32
|
||||
local function resample4(src: buffer, w: number, h: number, dst: buffer, outN: number, start: { number }, count: { number }, iw: { { number } }, horizontal: boolean)
|
||||
local o = 0
|
||||
if horizontal then
|
||||
for y = 0, h - 1 do
|
||||
local row = y * w * 4
|
||||
for xx = 1, outN do
|
||||
local wt = iw[xx]
|
||||
local p = row + start[xx] * 4
|
||||
local a0, a1, a2, a3 = 2097152, 2097152, 2097152, 2097152
|
||||
for k = 1, count[xx] do
|
||||
local wk = wt[k]
|
||||
a0 += readu8(src, p) * wk
|
||||
a1 += readu8(src, p + 1) * wk
|
||||
a2 += readu8(src, p + 2) * wk
|
||||
a3 += readu8(src, p + 3) * wk
|
||||
p += 4
|
||||
end
|
||||
writeu32(dst, o, clamp(a0 // 4194304, 0, 255) + clamp(a1 // 4194304, 0, 255) * 256 + clamp(a2 // 4194304, 0, 255) * 65536 + clamp(a3 // 4194304, 0, 255) * 16777216)
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
else
|
||||
local rs = w * 4
|
||||
for yy = 1, outN do
|
||||
local wt, n = iw[yy], count[yy]
|
||||
local base = start[yy] * rs
|
||||
for x = 0, rs - 4, 4 do
|
||||
local p = base + x
|
||||
local a0, a1, a2, a3 = 2097152, 2097152, 2097152, 2097152
|
||||
for k = 1, n do
|
||||
local wk = wt[k]
|
||||
a0 += readu8(src, p) * wk
|
||||
a1 += readu8(src, p + 1) * wk
|
||||
a2 += readu8(src, p + 2) * wk
|
||||
a3 += readu8(src, p + 3) * wk
|
||||
p += rs
|
||||
end
|
||||
writeu32(dst, o, clamp(a0 // 4194304, 0, 255) + clamp(a1 // 4194304, 0, 255) * 256 + clamp(a2 // 4194304, 0, 255) * 65536 + clamp(a3 // 4194304, 0, 255) * 16777216)
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
end
|
||||
local _ = h
|
||||
end
|
||||
|
||||
function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: number, dst: buffer, outN: number, c: Coeffs, horizontal: boolean)
|
||||
local start, count, weights = c.start, c.count, c.weights
|
||||
local size = kind
|
||||
@@ -313,6 +386,10 @@ function K.resamplePass(src: buffer, w: number, h: number, ch: number, kind: num
|
||||
iw[i] = t
|
||||
end
|
||||
end
|
||||
if fixed and ch == 4 then
|
||||
resample4(src, w, h, dst, outN, start, count, iw, horizontal)
|
||||
return
|
||||
end
|
||||
if horizontal then
|
||||
for y = 0, h - 1 do
|
||||
local row = y * w
|
||||
@@ -365,11 +442,23 @@ end
|
||||
-- nearest: dst pixel (x, y) = src pixel (floor((x + 0.5) * w / ow), ...)
|
||||
function K.nearest(src: buffer, w: number, h: number, bpp: number, dst: buffer, ow: number, oh: number)
|
||||
local sx, sy = w / ow, h / oh
|
||||
local xs = table.create(ow, 0)
|
||||
for x = 0, ow - 1 do
|
||||
xs[x + 1] = math.min(floor((x + 0.5) * sx), w - 1) * bpp
|
||||
end
|
||||
local o = 0
|
||||
for y = 0, oh - 1 do
|
||||
local iy = math.min(floor((y + 0.5) * sy), h - 1)
|
||||
for x = 0, ow - 1 do
|
||||
local ix = math.min(floor((x + 0.5) * sx), w - 1)
|
||||
buffer.copy(dst, (y * ow + x) * bpp, src, (iy * w + ix) * bpp, bpp)
|
||||
local row = math.min(floor((y + 0.5) * sy), h - 1) * w * bpp
|
||||
if bpp == 4 then
|
||||
for x = 1, ow do
|
||||
writeu32(dst, o, readu32(src, row + xs[x]))
|
||||
o += 4
|
||||
end
|
||||
else
|
||||
for x = 1, ow do
|
||||
buffer.copy(dst, o, src, row + xs[x], bpp)
|
||||
o += bpp
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -424,6 +513,10 @@ function K.overRgba8(dst: buffer, dw: number, src: buffer, sw: number, sx: numbe
|
||||
end
|
||||
if sa == 0 then
|
||||
continue
|
||||
elseif sa == 255 then
|
||||
-- coef1 = 255 * 128, coef2 = 0: the source pixel exactly
|
||||
writeu32(dst, doff, readu32(src, so))
|
||||
continue
|
||||
end
|
||||
local da = readu8(dst, doff + 3)
|
||||
local blend = da * (255 - sa)
|
||||
@@ -471,7 +564,44 @@ function K.copyRect(dst: buffer, dw: number, src: buffer, sw: number, sx: number
|
||||
end
|
||||
end
|
||||
|
||||
-- a = 255 leaves the colour as it is both ways (MULDIV255(v, 255) = v, v * 255 // 255 = v)
|
||||
local function premultiply4(b: buffer, n: number, un: boolean)
|
||||
for o = 3, n * 4 - 1, 4 do
|
||||
local a = readu8(b, o)
|
||||
if a == 255 then
|
||||
continue
|
||||
end
|
||||
if a == 0 then
|
||||
writeu16(b, o - 3, 0)
|
||||
writeu8(b, o - 1, 0)
|
||||
continue
|
||||
end
|
||||
for c = o - 3, o - 1 do
|
||||
local v = readu8(b, c)
|
||||
if un then
|
||||
writeu8(b, c, math.min(255, (v * 255) // a)) -- Pillow's rgbA2rgba
|
||||
else
|
||||
local t = v * a + 128 -- Pillow's MULDIV255
|
||||
writeu8(b, c, ((t // 256) + t) // 256)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function K.opaque(b: buffer, n: number): boolean
|
||||
for o = 3, n * 4 - 1, 4 do
|
||||
if readu8(b, o) ~= 255 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function K.premultiply(b: buffer, n: number, ch: number, kind: number, un: boolean)
|
||||
if kind == 1 and ch == 4 then
|
||||
premultiply4(b, n, un)
|
||||
return
|
||||
end
|
||||
local max = if kind == 1 then 255 elseif kind == 2 then 65535 else 1
|
||||
local px = ch * kind
|
||||
for i = 0, n - 1 do
|
||||
|
||||
@@ -228,6 +228,10 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
|
||||
local kind, ch = K.KIND[fmt], K.CHANNELS[fmt]
|
||||
local src, sw, sh = im.pixels, im.width, im.height
|
||||
local alpha = ALPHA_CH[fmt]
|
||||
local unAlpha = alpha
|
||||
if alpha == 4 and kind == 1 and K.opaque(src, sw * sh) then
|
||||
alpha = nil -- premultiplying is the identity; the resampled alpha can still round to 254: unpremultiplied below
|
||||
end
|
||||
if alpha then
|
||||
src = buffer.create(buffer.len(im.pixels))
|
||||
buffer.copy(src, 0, im.pixels)
|
||||
@@ -246,8 +250,8 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts
|
||||
else
|
||||
out.pixels = src
|
||||
end
|
||||
if alpha then
|
||||
K.premultiply(out.pixels, w * h, alpha, kind, true)
|
||||
if unAlpha then
|
||||
K.premultiply(out.pixels, w * h, unAlpha, kind, true)
|
||||
end
|
||||
return out, nil
|
||||
end)
|
||||
|
||||
Reference in new issue
Block a user