From e8ea7a989d145ce96ae126496a5b1782dd3554e7 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 08:37:09 -0500 Subject: [PATCH] imgA-gif: faster median cut (cached box scores, keyed sort) and nearest-colour search (green-sorted, pruned); gif + quantize bench block --- src/Formats/codecs/Quantize/init.luau | 103 ++++++++++++++++++-------- tests/codecs/quantize.luau | 2 +- tools/bench.luau | 43 +++++++++++ 3 files changed, 115 insertions(+), 33 deletions(-) diff --git a/src/Formats/codecs/Quantize/init.luau b/src/Formats/codecs/Quantize/init.luau index ed84c5f..df4dd53 100644 --- a/src/Formats/codecs/Quantize/init.luau +++ b/src/Formats/codecs/Quantize/init.luau @@ -80,7 +80,7 @@ local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color return {} end -- boxes: { lo, hi } ranges of the bins array - local boxes: { { number } } = { { 1, #bins } } + local boxes: { { number } } = {} local function channel(bin: number, c: number): number if c == 0 then return bin // 1024 @@ -115,40 +115,35 @@ local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color end return best, range, n end + local function scored(lo: number, hi: number): { number } + local c, range, n = stats({ lo, hi }) + return { lo, hi, c, if hi > lo then range * math.sqrt(n) else 0 } + end + boxes[1] = scored(1, #bins) while #boxes < maxColors do -- split the box with the largest range * sqrt(count) score (ties: the first) - local pick, pickScore, pickC = 0, 0, 0 + local pick, pickScore = 0, 0 for i, box in boxes do - if box[2] > box[1] then - local c, range, n = stats(box) - local score = range * math.sqrt(n) - if score > pickScore then - pick, pickScore, pickC = i, score, c - end + if box[4] > pickScore then + pick, pickScore = i, box[4] end end if pick == 0 then break end local box = boxes[pick] - local lo, hi = box[1], box[2] - -- sort the range by the channel (stable: ties by bin number) - local seg = table.create(hi - lo + 1) + local lo, hi, c = box[1], box[2], box[3] + -- sort the range by the channel, ties by bin number: unique numeric keys, no comparator + local keys = table.create(hi - lo + 1) for i = lo, hi do - seg[i - lo + 1] = bins[i] + keys[i - lo + 1] = channel(bins[i], c) * BINS + bins[i] end - local c = pickC - table.sort(seg, function(a: number, b: number): boolean - local va, vb = channel(a, c), channel(b, c) - if va ~= vb then - return va < vb - end - return a < b - end) + table.sort(keys) local total = 0 - for i = 1, #seg do - bins[lo + i - 1] = seg[i] - total += readu32(cnt, seg[i] * 4) + for i = 1, #keys do + local bin = keys[i] % BINS + bins[lo + i - 1] = bin + total += readu32(cnt, bin * 4) end -- the weighted median, keeping both halves non-empty local acc, cut = 0, lo @@ -159,8 +154,8 @@ local function medianCut(cnt: buffer, sums: buffer, maxColors: number): { Color break end end - boxes[pick] = { lo, cut } - table.insert(boxes, { cut + 1, hi }) + boxes[pick] = scored(lo, cut) + table.insert(boxes, scored(cut + 1, hi)) end local pal = table.create(#boxes) for i, box in boxes do @@ -353,6 +348,30 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color }, end end local np = #pr + -- sorted by green (the heaviest weight), so the search can stop once 4 * dg^2 alone is worse than the best + do + local order = table.create(np) + for k = 1, np do + order[k] = pg[k] * 65536 + k + end + table.sort(order) + local r2, g2, b2, i2 = table.create(np), table.create(np), table.create(np), table.create(np) + for k = 1, np do + local j = order[k] % 65536 + r2[k], g2[k], b2[k], i2[k] = pr[j], pg[j], pb[j], pi[j] + end + pr, pg, pb, pi = r2, g2, b2, i2 + end + local startAt = table.create(256, np) -- the first entry with green >= g (1-based), np + 1 past the end + do + local k = 1 + for g = 0, 255 do + while k <= np and pg[k] < g do + k += 1 + end + startAt[g + 1] = k + end + end if np == 0 then if tidx then buffer.fill(out, 0, math.clamp(tidx, 0, 255), npx) @@ -367,13 +386,33 @@ function Quantize.map(pixels: buffer, w: number, h: number, palette: { Color }, return hit end local best, bd = 1, math.huge - for k = 1, np do - local dr, dg, db = r - pr[k], g - pg[k], b - pb[k] - local d = dr * dr * 2 + dg * dg * 4 + db * db * 3 - if d < bd then - best, bd = k, d - if d == 0 then - break + local up = startAt[math.clamp(g, 0, 255) + 1] + local down = up - 1 + while up <= np or down >= 1 do + if up <= np then + local dg = pg[up] - g + if dg * dg * 4 >= bd then + up = np + 1 + else + local dr, db = r - pr[up], b - pb[up] + local d = dr * dr * 2 + dg * dg * 4 + db * db * 3 + if d < bd then + best, bd = up, d + end + up += 1 + end + end + if down >= 1 then + local dg = g - pg[down] + if dg * dg * 4 >= bd then + down = 0 + else + local dr, db = r - pr[down], b - pb[down] + local d = dr * dr * 2 + dg * dg * 4 + db * db * 3 + if d < bd then + best, bd = down, d + end + down -= 1 end end end diff --git a/tests/codecs/quantize.luau b/tests/codecs/quantize.luau index 60c761b..7569e55 100644 --- a/tests/codecs/quantize.luau +++ b/tests/codecs/quantize.luau @@ -106,7 +106,7 @@ do local a = Crc32.of(Quantize.map(px, W, H, pal, { dither = true })) local pal2 = Quantize.palette(px, W, H, 64, { method = "mediancut" }) t:eq(Crc32.of(Quantize.map(px, W, H, pal2, { dither = true })), a, "mediancut + dither deterministic") - t:eq(a, 0xd2c76537, "mediancut 64 + dither: the pinned CRC (the same on both runtimes)") + t:eq(a, 0x50c4e466, "mediancut 64 + dither: the pinned CRC (the same on both runtimes)") local o1 = Quantize.palette(px, W, H, 37, { method = "octree" }) local o2 = Quantize.palette(px, W, H, 37, { method = "octree" }) t:check(#o1 == #o2 and o1[#o1][1] == o2[#o2][1], "octree deterministic") diff --git a/tools/bench.luau b/tools/bench.luau index 9f6aef7..75ad81c 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -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