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
@@ -4,14 +4,23 @@
|
||||
-- codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws).
|
||||
-- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact
|
||||
-- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md.
|
||||
local readi16, readu16, writeu16, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.readu8, buffer.writei16
|
||||
local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16
|
||||
local clamp = math.clamp
|
||||
|
||||
local Dct = {}
|
||||
|
||||
local ws = table.create(64, 0)
|
||||
|
||||
local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local function dequant(q: buffer): { number }
|
||||
local t = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
t[i + 1] = readu16(q, i * 2)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function idct8(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local ws = ws
|
||||
local s1 = 2 ^ (13 - p1)
|
||||
local h1 = s1 / 2
|
||||
local s2 = 2 ^ (16 + p1)
|
||||
@@ -19,11 +28,10 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 7 do
|
||||
local o = off + c * 2
|
||||
local qo = c * 2
|
||||
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48)
|
||||
local c4, c5, c6, c7 = readi16(coef, o + 64), readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112)
|
||||
if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then
|
||||
local dc = readi16(coef, o) * readu16(q, qo) * dcm
|
||||
local dc = readi16(coef, o) * qt[c + 1] * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
ws[c + 17] = dc
|
||||
@@ -33,48 +41,136 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
ws[c + 49] = dc
|
||||
ws[c + 57] = dc
|
||||
else
|
||||
-- even part
|
||||
local z2 = c2 * readu16(q, qo + 32)
|
||||
local z3 = c6 * readu16(q, qo + 96)
|
||||
-- even part (the pass-1 rounding folded into tmp0 / tmp1)
|
||||
local z2 = c2 * qt[c + 17]
|
||||
local z3 = c6 * qt[c + 49]
|
||||
local z1 = (z2 + z3) * 4433
|
||||
local tmp2 = z1 - z3 * 15137
|
||||
local tmp3 = z1 + z2 * 6270
|
||||
z2 = readi16(coef, o) * readu16(q, qo)
|
||||
z3 = c4 * readu16(q, qo + 64)
|
||||
local tmp0 = (z2 + z3) * 8192
|
||||
local tmp1 = (z2 - z3) * 8192
|
||||
z2 = readi16(coef, o) * qt[c + 1]
|
||||
z3 = c4 * qt[c + 33]
|
||||
local tmp0 = (z2 + z3) * 8192 + h1
|
||||
local tmp1 = (z2 - z3) * 8192 + h1
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
-- odd part
|
||||
local t0 = c7 * readu16(q, qo + 112)
|
||||
local t1 = c5 * readu16(q, qo + 80)
|
||||
local t2 = c3 * readu16(q, qo + 48)
|
||||
local t3 = c1 * readu16(q, qo + 16)
|
||||
local t0 = c7 * qt[c + 57]
|
||||
local t1 = c5 * qt[c + 41]
|
||||
local t2 = c3 * qt[c + 25]
|
||||
local t3 = c1 * qt[c + 9]
|
||||
z1 = t0 + t3
|
||||
z2 = t1 + t2
|
||||
z3 = t0 + t2
|
||||
local z4 = t1 + t3
|
||||
local z5 = (z3 + z4) * 9633
|
||||
t0 *= 2446
|
||||
t1 *= 16819
|
||||
t2 *= 25172
|
||||
t3 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
t0 += z1 + z3
|
||||
t1 += z2 + z4
|
||||
t2 += z2 + z3
|
||||
t3 += z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3 + h1) // s1
|
||||
ws[c + 57] = (tmp10 - t3 + h1) // s1
|
||||
ws[c + 9] = (tmp11 + t2 + h1) // s1
|
||||
ws[c + 49] = (tmp11 - t2 + h1) // s1
|
||||
ws[c + 17] = (tmp12 + t1 + h1) // s1
|
||||
ws[c + 41] = (tmp12 - t1 + h1) // s1
|
||||
ws[c + 25] = (tmp13 + t0 + h1) // s1
|
||||
ws[c + 33] = (tmp13 - t0 + h1) // s1
|
||||
t0 = t0 * 2446 + z1 + z3
|
||||
t1 = t1 * 16819 + z2 + z4
|
||||
t2 = t2 * 25172 + z2 + z3
|
||||
t3 = t3 * 12299 + z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3) // s1
|
||||
ws[c + 57] = (tmp10 - t3) // s1
|
||||
ws[c + 9] = (tmp11 + t2) // s1
|
||||
ws[c + 49] = (tmp11 - t2) // s1
|
||||
ws[c + 17] = (tmp12 + t1) // s1
|
||||
ws[c + 41] = (tmp12 - t1) // s1
|
||||
ws[c + 25] = (tmp13 + t0) // s1
|
||||
ws[c + 33] = (tmp13 - t0) // s1
|
||||
end
|
||||
end
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local hd = sd / 2 + center * sd
|
||||
-- u16 pairs written as one u32 (little endian: the left sample in the low half)
|
||||
for r = 0, 7 do
|
||||
local b = r * 8
|
||||
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
|
||||
local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8]
|
||||
local o = (out + r * stride) * 2
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
else
|
||||
local z1 = (w2 + w6) * 4433
|
||||
local tmp2 = z1 - w6 * 15137
|
||||
local tmp3 = z1 + w2 * 6270
|
||||
local tmp0 = (w0 + w4) * 8192 + h2
|
||||
local tmp1 = (w0 - w4) * 8192 + h2
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
z1 = w7 + w1
|
||||
local z2 = w5 + w3
|
||||
local z3 = w7 + w3
|
||||
local z4 = w5 + w1
|
||||
local z5 = (z3 + z4) * 9633
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
local t0 = w7 * 2446 + z1 + z3
|
||||
local t1 = w5 * 16819 + z2 + z4
|
||||
local t2 = w3 * 25172 + z2 + z3
|
||||
local t3 = w1 * 12299 + z1 + z4
|
||||
writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- idct8 with every nonzero coefficient in the top-left 4 x 4 (zigzag index <= 9): the same sums without the zero terms
|
||||
local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local ws = ws
|
||||
local s1 = 2 ^ (13 - p1)
|
||||
local h1 = s1 / 2
|
||||
local s2 = 2 ^ (16 + p1)
|
||||
local h2 = s2 / 2 + center * s2
|
||||
local dcm = 2 ^ p1
|
||||
for c = 0, 3 do
|
||||
local o = off + c * 2
|
||||
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48)
|
||||
if c1 == 0 and c2 == 0 and c3 == 0 then
|
||||
local dc = readi16(coef, o) * qt[c + 1] * dcm
|
||||
ws[c + 1] = dc
|
||||
ws[c + 9] = dc
|
||||
ws[c + 17] = dc
|
||||
ws[c + 25] = dc
|
||||
ws[c + 33] = dc
|
||||
ws[c + 41] = dc
|
||||
ws[c + 49] = dc
|
||||
ws[c + 57] = dc
|
||||
else
|
||||
local z2 = c2 * qt[c + 17]
|
||||
local tmp2 = z2 * 4433
|
||||
local tmp3 = tmp2 + z2 * 6270
|
||||
local tmp0 = readi16(coef, o) * qt[c + 1] * 8192 + h1
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
local t2 = c3 * qt[c + 25]
|
||||
local t3 = c1 * qt[c + 9]
|
||||
local z5 = (t2 + t3) * 9633
|
||||
local z1 = t3 * -7373
|
||||
z2 = t2 * -20995
|
||||
local z3 = t2 * -16069 + z5
|
||||
local z4 = t3 * -3196 + z5
|
||||
local t0 = z1 + z3
|
||||
local t1 = z2 + z4
|
||||
t2 = t2 * 25172 + z2 + z3
|
||||
t3 = t3 * 12299 + z1 + z4
|
||||
ws[c + 1] = (tmp10 + t3) // s1
|
||||
ws[c + 57] = (tmp10 - t3) // s1
|
||||
ws[c + 9] = (tmp11 + t2) // s1
|
||||
ws[c + 49] = (tmp11 - t2) // s1
|
||||
ws[c + 17] = (tmp12 + t1) // s1
|
||||
ws[c + 41] = (tmp12 - t1) // s1
|
||||
ws[c + 25] = (tmp13 + t0) // s1
|
||||
ws[c + 33] = (tmp13 - t0) // s1
|
||||
end
|
||||
end
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
@@ -82,51 +178,51 @@ local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
for r = 0, 7 do
|
||||
local b = r * 8
|
||||
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
|
||||
local w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8]
|
||||
local o = (out + r * stride) * 2
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv)
|
||||
for i = 0, 14, 2 do
|
||||
writeu16(plane, o + i, v)
|
||||
end
|
||||
if w1 == 0 and w2 == 0 and w3 == 0 then
|
||||
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
else
|
||||
local z1 = (w2 + w6) * 4433
|
||||
local tmp2 = z1 - w6 * 15137
|
||||
local tmp3 = z1 + w2 * 6270
|
||||
local tmp0 = (w0 + w4) * 8192
|
||||
local tmp1 = (w0 - w4) * 8192
|
||||
local tmp2 = w2 * 4433
|
||||
local tmp3 = tmp2 + w2 * 6270
|
||||
local tmp0 = w0 * 8192 + h2
|
||||
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
|
||||
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
|
||||
local t0, t1, t2, t3 = w7, w5, w3, w1
|
||||
z1 = t0 + t3
|
||||
local z2 = t1 + t2
|
||||
local z3 = t0 + t2
|
||||
local z4 = t1 + t3
|
||||
local z5 = (z3 + z4) * 9633
|
||||
t0 *= 2446
|
||||
t1 *= 16819
|
||||
t2 *= 25172
|
||||
t3 *= 12299
|
||||
z1 *= -7373
|
||||
z2 *= -20995
|
||||
z3 = z3 * -16069 + z5
|
||||
z4 = z4 * -3196 + z5
|
||||
t0 += z1 + z3
|
||||
t1 += z2 + z4
|
||||
t2 += z2 + z3
|
||||
t3 += z1 + z4
|
||||
writeu16(plane, o, clamp((tmp10 + t3 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 14, clamp((tmp10 - t3 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 2, clamp((tmp11 + t2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 12, clamp((tmp11 - t2 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 4, clamp((tmp12 + t1 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 10, clamp((tmp12 - t1 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 6, clamp((tmp13 + t0 + h2) // s2, 0, maxv))
|
||||
writeu16(plane, o + 8, clamp((tmp13 - t0 + h2) // s2, 0, maxv))
|
||||
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
|
||||
local z5 = (w3 + w1) * 9633
|
||||
local z1 = w1 * -7373
|
||||
local z2 = w3 * -20995
|
||||
local z3 = w3 * -16069 + z5
|
||||
local z4 = w1 * -3196 + z5
|
||||
local t0 = z1 + z3
|
||||
local t1 = z2 + z4
|
||||
local t2 = w3 * 25172 + z2 + z3
|
||||
local t3 = w1 * 12299 + z1 + z4
|
||||
writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536)
|
||||
writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- a block with only its DC coefficient: one value (the column and row passes' DC shortcuts)
|
||||
local function idct8dc(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local sd = 2 ^ (p1 + 3)
|
||||
local v = clamp((readi16(coef, off) * qt[1] * 2 ^ p1 + sd / 2 + center * sd) // sd, 0, maxv) * 65537
|
||||
local o = out * 2
|
||||
local rs = stride * 2
|
||||
for _ = 0, 7 do
|
||||
writeu32(plane, o, v)
|
||||
writeu32(plane, o + 4, v)
|
||||
writeu32(plane, o + 8, v)
|
||||
writeu32(plane, o + 12, v)
|
||||
o += rs
|
||||
end
|
||||
end
|
||||
|
||||
local function idct4(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
local s1 = 2 ^ (14 - p1)
|
||||
local h1 = s1 / 2
|
||||
@@ -236,17 +332,36 @@ local function idct2(coef: buffer, off: number, q: buffer, plane: buffer, out: n
|
||||
end
|
||||
|
||||
-- p1 = PASS1_BITS (2 for 8-bit, 1 for 12-bit)
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?)
|
||||
-- nz (optional): u8 per block, the highest zigzag index that may be nonzero
|
||||
function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?, nz: buffer?)
|
||||
local stride = bpl * size
|
||||
local qt = dequant(q)
|
||||
for by = 0, bpc - 1 do
|
||||
if hook and by > 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
if size == 8 and nz then
|
||||
local bi = by * bpl
|
||||
local out = by * 8 * stride
|
||||
for off = bi * 128, (bi + bpl - 1) * 128, 128 do
|
||||
local k = readu8(nz, bi)
|
||||
if k == 0 then
|
||||
idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
elseif k <= 9 then
|
||||
idct8x4(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
else
|
||||
idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
end
|
||||
bi += 1
|
||||
out += 8
|
||||
end
|
||||
continue
|
||||
end
|
||||
for bx = 0, bpl - 1 do
|
||||
local off = (by * bpl + bx) * 128
|
||||
local out = by * size * stride + bx * size
|
||||
if size == 8 then
|
||||
idct8(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
|
||||
elseif size == 4 then
|
||||
idct4(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
elseif size == 2 then
|
||||
@@ -260,7 +375,7 @@ function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, si
|
||||
end
|
||||
|
||||
function Dct.idctBlock(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
|
||||
idct8(coef, off, q, plane, out, stride, p1, center, maxv)
|
||||
idct8(coef, off, dequant(q), plane, out, stride, p1, center, maxv)
|
||||
end
|
||||
|
||||
local fw = table.create(64, 0)
|
||||
|
||||
Reference in new issue
Block a user