perf(image): Floyd-Steinberg error sums in tables and locals (same float sums), map cache checked inline; build: regenerate bundles

This commit is contained in:
4DBug committed 2026-11-01 20:17:06 -06:00
1 parent 903e239a0e
commit 3f788e9d22
5 files changed
+2916 -1384

No files matched your search

+26 -24
View File
@@ -402,43 +402,45 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color },
if tidx and readu8(pixels, o + 3) < 128 then
writeu8(out, i, tbyte)
else
writeu8(out, i, pi[nearest(readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2))])
local r, g, b = readu8(pixels, o), readu8(pixels, o + 1), readu8(pixels, o + 2)
local hit = cache[r * 65536 + g * 256 + b]
writeu8(out, i, pi[hit or nearest(r, g, b)])
end
end
return out
end
local rowLen = (w + 2) * 24
local cur, nxt = buffer.create(rowLen), buffer.create(rowLen)
-- Floyd-Steinberg with the error sums kept as the buffered version adds them: the right neighbour's share is
-- carried (its upper-row sum + 7/16 e), and each lower-row slot is (e[x-1] / 16 + 5/16 e[x]) + 3/16 e[x+1]
local cur, nxt = table.create(w * 3 + 6, 0), table.create(w * 3 + 6, 0)
for y = 0, h - 1 do
buffer.fill(nxt, 0, 0, rowLen)
local cr, cg, cb = 0, 0, 0 -- 7/16 of the left pixel's error
local p0r, p0g, p0b = 0, 0, 0 -- slot x of the next row: e[x-1] / 16
local p1r, p1g, p1b = 0, 0, 0 -- slot x - 1 of the next row: e[x-2] / 16 + 5/16 e[x-1]
local i = y * w
for x = 0, w - 1 do
local i = y * w + x
local o = i * 4
local q = x * 3 + 1
local er, eg, eb = 0, 0, 0
if tidx and readu8(pixels, o + 3) < 128 then
writeu8(out, i, tbyte)
else
local e = (x + 1) * 24
local r = math.clamp(floor(readu8(pixels, o) + readf64(cur, e) + 0.5), 0, 255)
local g = math.clamp(floor(readu8(pixels, o + 1) + readf64(cur, e + 8) + 0.5), 0, 255)
local b = math.clamp(floor(readu8(pixels, o + 2) + readf64(cur, e + 16) + 0.5), 0, 255)
local k = nearest(r, g, b)
local r = math.clamp(floor(readu8(pixels, o) + (cur[q] + cr) + 0.5), 0, 255)
local g = math.clamp(floor(readu8(pixels, o + 1) + (cur[q + 1] + cg) + 0.5), 0, 255)
local b = math.clamp(floor(readu8(pixels, o + 2) + (cur[q + 2] + cb) + 0.5), 0, 255)
local k = cache[r * 65536 + g * 256 + b] or nearest(r, g, b)
writeu8(out, i, pi[k])
local er, eg, eb = r - pr[k], g - pg[k], b - pb[k]
-- 7/16 right, 3/16 down-left, 5/16 down, 1/16 down-right
writef64(cur, e + 24, readf64(cur, e + 24) + er * 0.4375)
writef64(cur, e + 32, readf64(cur, e + 32) + eg * 0.4375)
writef64(cur, e + 40, readf64(cur, e + 40) + eb * 0.4375)
writef64(nxt, e - 24, readf64(nxt, e - 24) + er * 0.1875)
writef64(nxt, e - 16, readf64(nxt, e - 16) + eg * 0.1875)
writef64(nxt, e - 8, readf64(nxt, e - 8) + eb * 0.1875)
writef64(nxt, e, readf64(nxt, e) + er * 0.3125)
writef64(nxt, e + 8, readf64(nxt, e + 8) + eg * 0.3125)
writef64(nxt, e + 16, readf64(nxt, e + 16) + eb * 0.3125)
writef64(nxt, e + 24, readf64(nxt, e + 24) + er * 0.0625)
writef64(nxt, e + 32, readf64(nxt, e + 32) + eg * 0.0625)
writef64(nxt, e + 40, readf64(nxt, e + 40) + eb * 0.0625)
er, eg, eb = r - pr[k], g - pg[k], b - pb[k]
end
cr, cg, cb = er * 0.4375, eg * 0.4375, eb * 0.4375
if x > 0 then
nxt[q - 3], nxt[q - 2], nxt[q - 1] = p1r + er * 0.1875, p1g + eg * 0.1875, p1b + eb * 0.1875
end
p1r, p1g, p1b = p0r + er * 0.3125, p0g + eg * 0.3125, p0b + eb * 0.3125
p0r, p0g, p0b = er * 0.0625, eg * 0.0625, eb * 0.0625
i += 1
end
local q = (w - 1) * 3 + 1
nxt[q], nxt[q + 1], nxt[q + 2] = p1r, p1g, p1b
cur, nxt = nxt, cur
end
return out