imgA-gif: faster median cut (cached box scores, keyed sort) and nearest-colour search (green-sorted, pruned); gif + quantize bench block

This commit is contained in:
4DBug committed 2026-10-31 08:37:09 -05:00
1 parent 603689bc14
commit a507a52bda
3 files changed
+115 -33

No files matched your search

+43
View File
@@ -56,3 +56,46 @@ do
Png.encode(img)
end)
end
-- ---------------------------------------------------------------------------------------- ws-imgA: gif + quantize
do
local Gif = require("@formats/image/gif")
local Quantize = require("@formats/codecs/Quantize")
local Types = require("@formats/Types")
local GB = require("@tests/gen/gif_bench")
local still = buffer.fromstring(GB.still)
local anim = buffer.fromstring(GB.anim)
local simg = assert(Gif.decode(Bytes.reader(still)))
local aimg = assert(Gif.decode(Bytes.reader(anim)))
local sw, sh = simg.width, simg.height
bench("gif decode 512x384", 5, sw * sh * 4, function()
Gif.decode(Bytes.reader(still))
end)
bench("gif decode 40 x 128x128 anim", 3, #(aimg.frames :: any) * 128 * 128 * 4, function()
Gif.decode(Bytes.reader(anim))
end)
bench("gif encode 512x384 (exact pal)", 3, sw * sh * 4, function()
Gif.encode(simg)
end)
local photo = Types.Image.new(sw, sh)
for i = 0, sw * sh - 1 do
local x, y = i % sw, i // sw
buffer.writeu32(photo.pixels, i * 4, (x % 256) + (y % 256) * 256 + ((x * y) % 256) * 65536 + 0xFF000000)
end
bench("quantize mediancut 256", 3, sw * sh * 4, function()
Quantize.palette(photo.pixels, sw, sh, 256)
end)
bench("quantize octree 256", 3, sw * sh * 4, function()
Quantize.palette(photo.pixels, sw, sh, 256, { method = "octree" })
end)
local pal = Quantize.palette(photo.pixels, sw, sh, 256)
bench("quantize map", 3, sw * sh * 4, function()
Quantize.map(photo.pixels, sw, sh, pal)
end)
bench("quantize map + dither", 3, sw * sh * 4, function()
Quantize.map(photo.pixels, sw, sh, pal, { dither = true })
end)
bench("gif encode 512x384 (quantize)", 2, sw * sh * 4, function()
Gif.encode(photo)
end)
end