perf(image): VP8L literal symbols inline, cross-colour identity tiles skipped, T / TR / TL predictors and SWAR addPixels; JPEG IDCT 5x5 path, gray / 4:4:4 colour fast paths; QOI index as tables; TGA rows without per-pixel div/mod; BC7 / BC6H bit reads from a word table; bench_image: bcn / exr

This commit is contained in:
4DBug committed 2026-11-01 19:41:47 -06:00
1 parent 9855a7edd6
commit 903e239a0e
7 files changed
+293 -121

No files matched your search

+52 -45
View File
@@ -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
+72 -24
View File
@@ -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