-- codecs/Quantize tests: palette size bounds for every method and colour budget, the exact (lossless) palette at -- <= maxColors colours, transparency handling, PSNR of quantized images above thresholds, determinism of both -- methods and of Floyd-Steinberg dithering (the same CRC on both runtimes), bad arguments, and a no-throw pass. local T = require("@tests/T") local Quantize = require("@formats/codecs/Quantize") local Crc32 = require("@formats/core/Crc32") local t = T.suite("quantize") local rand = T.rng(5) local function photo(w: number, h: number): buffer local px = buffer.create(w * h * 4) for i = 0, w * h - 1 do local x, y = i % w, i // w local r = (x * 255) // (w - 1) local g = (y * 255) // (h - 1) local b = math.floor(128 + 100 * math.sin(x * 0.11) * math.cos(y * 0.07)) + rand(9) buffer.writeu32(px, i * 4, r + g * 256 + math.clamp(b, 0, 255) * 65536 + 0xFF000000) end return px end local function render(idx: buffer, pal: { { number } }, n: number): buffer local out = buffer.create(n * 4) for i = 0, n - 1 do local c = pal[buffer.readu8(idx, i) + 1] buffer.writeu32(out, i * 4, c[1] + c[2] * 256 + c[3] * 65536 + 0xFF000000) end return out end local function psnr(a: buffer, b: buffer): number local se, n = 0, 0 for o = 0, buffer.len(a) - 4, 4 do for k = 0, 2 do local d = buffer.readu8(a, o + k) - buffer.readu8(b, o + k) se += d * d n += 1 end end return if se == 0 then 99 else 10 * math.log10(255 * 255 * n / se) end local W, H = 128, 96 local px = photo(W, H) -- palette bounds and quality local MIN_PSNR = { [256] = 30, [64] = 24, [16] = 17, [2] = 10 } for _, method in { "mediancut", "octree" } do for _, k in { 2, 16, 64, 256 } do local pal, exact = Quantize.palette(px, W, H, k, { method = method }) t:check(#pal >= 1 and #pal <= k, string.format("%s %d: palette size %d", method, k, #pal)) t:check(not exact, method .. ": not exact") for _, c in pal do t:check(#c == 4 and c[4] == 255 and c[1] % 1 == 0 and c[1] >= 0 and c[1] <= 255, "palette entries") end for _, dither in { false, true } do local idx = Quantize.map(px, W, H, pal, { dither = dither }) t:eq(buffer.len(idx), W * H, "index buffer size") local p = psnr(render(idx, pal, W * H), px) local want = MIN_PSNR[k] - (if dither then 4 else 0) t:check(p > want, string.format("%s %d dither=%s: PSNR %.1f > %d", method, k, tostring(dither), p, want)) if k == 256 or k == 16 then t:note("%-9s %3d colours dither=%-5s PSNR %.1f dB, crc %08x", method, k, tostring(dither), p, Crc32.of(idx)) end end end end -- exact palettes: lossless do local n = 50 * 40 local b = buffer.create(n * 4) for i = 0, n - 1 do local k = (i * 7 + i // 50) % 200 buffer.writeu32(b, i * 4, (k * 3) % 256 + ((k * 11) % 256) * 256 + k * 65536 + 0xFF000000) end local pal, exact = Quantize.palette(b, 50, 40, 256) t:check(exact and #pal == 200, "exact palette of 200 colours: " .. #pal) local idx = Quantize.map(b, 50, 40, pal, { dither = true }) t:check(buffer.tostring(render(idx, pal, n)) == buffer.tostring(b), "exact palette maps losslessly (dither too)") local pal2, exact2 = Quantize.palette(b, 50, 40, 199) t:check(not exact2 and #pal2 <= 199, "199 colours is not exact") local i3, p3, e3 = Quantize.quantize(b, 50, 40, 256, { dither = true }) t:check(e3 and #p3 == 200 and buffer.tostring(render(i3, p3, n)) == buffer.tostring(b), "quantize() exact") end -- transparency do local b = buffer.create(16 * 4) for i = 0, 15 do buffer.writeu32(b, i * 4, if i % 3 == 0 then 0x10FFFFFF else 0xFF0000FF + i * 256) end local pal, exact = Quantize.palette(b, 4, 4, 255) t:check(exact and #pal == 10, "transparent pixels are not in the palette") table.insert(pal, { 0, 0, 0, 0 }) local idx = Quantize.map(b, 4, 4, pal, { transparentIndex = #pal - 1, dither = true }) for i = 0, 15 do local v = buffer.readu8(idx, i) t:check((i % 3 == 0) == (v == #pal - 1), "transparent index " .. i) end end -- determinism (also across runtimes: these CRCs are pinned) do local pal = Quantize.palette(px, W, H, 64, { method = "mediancut" }) 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)") 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") end -- bad arguments never throw do t:eq(#Quantize.palette(nil :: any, 1, 1, 4), 0, "nil pixels") t:eq(#Quantize.palette(buffer.create(3), 1, 1, 4), 0, "short buffer") t:eq(buffer.len(Quantize.map(px, W, H, {})), W * H, "empty palette") t:eq(buffer.len(Quantize.map(buffer.create(1), 2, 2, { { 1, 2, 3, 255 } })), 0, "short buffer map") local ok = pcall(function() for i = 1, 20 do local w, h = 1 + rand(20), 1 + rand(20) local b = buffer.create(w * h * 4) for k = 0, w * h * 4 - 1 do buffer.writeu8(b, k, rand(256)) end local pal = Quantize.palette(b, w, h, 1 + rand(256), { method = if i % 2 == 0 then "octree" else "mediancut" }) Quantize.map(b, w, h, pal, { dither = i % 3 == 0, transparentIndex = if i % 4 == 0 then 0 else nil }) end end) t:check(ok, "random images never throw") end t:done()