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:
1 parent
9a928bd5be
commit
e8ea7a989d
3 files changed
+115
-33
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user