Decoders ported from Pillow BcnDecode.c (CC0) with D3D11 rules (BC6H +32 rounding, float output, reserved modes, signed RGTC). Tests against Pillow and Godot 4 (bcdec) over random blocks of every BC6H / BC7 mode; encoder PSNR above Pillow's encoder. Bench lines in tools/bench.luau.
242 lines
8.5 KiB
Luau
242 lines
8.5 KiB
Luau
-- codecs/image/Bcn tests: random BC1..BC7 blocks (every BC7 and BC6H mode, the BC1 3-colour mode, odd sizes) against
|
|
-- Pillow's and Godot's (bcdec) decodes; BC6H floats against Godot; the encoders (BC1 / BC3 / BC4 / BC5) within reach
|
|
-- of Pillow's encoder PSNR, exact on flat blocks; argument errors; the fuzz no-throw pass. Fixtures: tools/fixtures/bcn.py.
|
|
local T = require("@tests/T")
|
|
local Bcn = require("@formats/codecs/image/Bcn")
|
|
local Half = require("@formats/codecs/image/Half")
|
|
local Fx = require("@tests/gen/bcn")
|
|
|
|
local t = T.suite("bcn")
|
|
|
|
-- the largest per-byte difference between two rgba8 buffers (channels: which of r, g, b, a count)
|
|
local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number)
|
|
local worst, at = 0, -1
|
|
for i = 0, n - 1 do
|
|
for _, c in channels do
|
|
local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c))
|
|
if d > worst then
|
|
worst, at = d, i
|
|
end
|
|
end
|
|
end
|
|
return worst, at
|
|
end
|
|
|
|
local RGBA, RGB, RG, R = { 0, 1, 2, 3 }, { 0, 1, 2 }, { 0, 1 }, { 0 }
|
|
-- which channels each reference defines, and the allowed difference to it
|
|
local PILLOW: { [string]: { any } } = {
|
|
bc1 = { RGBA, 0 },
|
|
bc2 = { RGBA, 0 },
|
|
bc3 = { RGBA, 0 },
|
|
bc4 = { RGB, 0 },
|
|
bc5 = { RG, 0 },
|
|
bc5s = { RG, 1 }, -- Pillow interpolates the biased unsigned values with truncation
|
|
bc7 = { RGBA, 0 },
|
|
}
|
|
|
|
local stats = { pillow = 0, godot = 0 }
|
|
for _, c in Fx.cases do
|
|
local src = buffer.fromstring(c.data)
|
|
local px, err = Bcn.decode(c.kind, src, 0, c.w, c.h)
|
|
t:check(px, c.name .. ": " .. tostring(err))
|
|
local n = c.w * c.h
|
|
if Bcn.FLOAT[c.kind] then
|
|
t:eq(buffer.len(px), n * 16, c.name .. ": rgbaf32 size")
|
|
-- Godot (bcdec) floats: exact half values
|
|
local g = buffer.fromstring(c.godot)
|
|
local worst = 0
|
|
for i = 0, n - 1 do
|
|
for ch = 0, 2 do
|
|
local a, b = buffer.readf32(px, i * 16 + ch * 4), buffer.readf32(g, i * 16 + ch * 4)
|
|
local d = math.abs(a - b) / math.max(math.abs(b), 1e-3)
|
|
worst = math.max(worst, d)
|
|
end
|
|
end
|
|
t:check(worst < 1e-3, string.format("%s: relative error %g against Godot", c.name, worst))
|
|
stats.godot += 1
|
|
-- Pillow: 8-bit truncation of the clamped float (and no rounding term in its interpolation): within 1. Its
|
|
-- signed BC6H differs from bcdec and the spec (negative endpoints), so only BC6H UF16 is compared.
|
|
local p = buffer.fromstring(c.pillow)
|
|
local w2 = 0
|
|
for i = 0, n - 1 do
|
|
for ch = 0, 2 do
|
|
local v = math.clamp(buffer.readf32(px, i * 16 + ch * 4), 0, 1)
|
|
w2 = math.max(w2, math.abs(math.floor(v * 255) - buffer.readu8(p, i * 4 + ch)))
|
|
end
|
|
end
|
|
if c.kind == "bc6h" then
|
|
t:check(w2 <= 1, string.format("%s: %d off Pillow", c.name, w2))
|
|
stats.pillow += 1
|
|
end
|
|
else
|
|
t:eq(buffer.len(px), n * 4, c.name .. ": rgba8 size")
|
|
local ref = PILLOW[c.kind]
|
|
local d, at = maxDiff(px, buffer.fromstring(c.pillow), n, ref[1])
|
|
t:check(d <= ref[2], string.format("%s: %d off Pillow at pixel %d", c.name, d, at))
|
|
stats.pillow += 1
|
|
if c.godot then
|
|
local chans = if c.kind == "bc4" then R elseif c.kind == "bc5" then RG else RGBA
|
|
local dg, atg = maxDiff(px, buffer.fromstring(c.godot), n, chans)
|
|
-- bcdec rounds some interpolated values where Pillow truncates: within 1
|
|
t:check(dg <= 1, string.format("%s: %d off Godot at pixel %d", c.name, dg, atg))
|
|
stats.godot += 1
|
|
end
|
|
end
|
|
-- into a bigger buffer at an offset, from an offset
|
|
local big = buffer.create(buffer.len(src) + 5)
|
|
buffer.copy(big, 5, src)
|
|
local bpp = if Bcn.FLOAT[c.kind] then 16 else 4
|
|
local out = buffer.create(n * bpp + 8)
|
|
local px2 = Bcn.decode(c.kind, big, 5, c.w, c.h, out, 8)
|
|
t:check(px2 == out and buffer.readstring(out, 8, n * bpp) == buffer.readstring(px, 0, n * bpp), c.name .. ": offsets")
|
|
end
|
|
t:note("%d block images decoded: %d Pillow, %d Godot comparisons", #Fx.cases, stats.pillow, stats.godot)
|
|
|
|
-- BC4 SNORM = the red channel of BC5 SNORM
|
|
do
|
|
for _, c in Fx.cases do
|
|
if c.kind == "bc5s" then
|
|
local src = buffer.fromstring(c.data)
|
|
local nb = buffer.len(src) // 16
|
|
local b4 = buffer.create(nb * 8)
|
|
for i = 0, nb - 1 do
|
|
buffer.copy(b4, i * 8, src, i * 16, 8)
|
|
end
|
|
local p4 = Bcn.decode("bc4s", b4, 0, c.w, c.h) :: buffer
|
|
local p5 = Bcn.decode("bc5s", src, 0, c.w, c.h) :: buffer
|
|
local ok = true
|
|
for i = 0, c.w * c.h - 1 do
|
|
ok = ok and buffer.readu8(p4, i * 4) == buffer.readu8(p5, i * 4) and buffer.readu8(p4, i * 4 + 1) == buffer.readu8(p5, i * 4)
|
|
end
|
|
t:check(ok, c.name .. ": bc4s = bc5s red")
|
|
end
|
|
end
|
|
end
|
|
|
|
-- encoders
|
|
local function psnr(a: buffer, b: buffer, n: number, channels: { number }): number
|
|
local se = 0
|
|
for i = 0, n - 1 do
|
|
for _, c in channels do
|
|
local d = buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c)
|
|
se += d * d
|
|
end
|
|
end
|
|
return if se == 0 then 99 else 10 * math.log10(255 * 255 * n * #channels / se)
|
|
end
|
|
|
|
for _, e in Fx.enc do
|
|
local px = buffer.fromstring(e.rgba)
|
|
local n = e.w * e.h
|
|
for _, k in { { "bc1", RGB }, { "bc3", RGBA }, { "bc5", RG }, { "bc4", R } } do
|
|
local kind, ch = k[1], k[2]
|
|
local blocks, err = Bcn.encode(kind, px, e.w, e.h)
|
|
t:check(blocks, kind .. " encode: " .. tostring(err))
|
|
t:eq(buffer.len(blocks), Bcn.size(kind, e.w, e.h), e.name .. " " .. kind .. ": size")
|
|
local back = Bcn.decode(kind, blocks, 0, e.w, e.h) :: buffer
|
|
local p = psnr(px, back, n, ch)
|
|
local base = e.psnr[kind]
|
|
if base then
|
|
t:check(p >= base - 0.5, string.format("%s %s: PSNR %.2f dB, Pillow %.2f dB", e.name, kind, p, base))
|
|
t:note("%s %s: %.2f dB (Pillow %.2f dB)", e.name, kind, p, base)
|
|
else
|
|
t:check(p >= 35, string.format("%s %s: PSNR %.2f dB", e.name, kind, p))
|
|
end
|
|
if kind == "bc1" then
|
|
-- the transparent pixels survive (3-colour mode)
|
|
local bad = 0
|
|
for i = 0, n - 1 do
|
|
local a = buffer.readu8(px, i * 4 + 3)
|
|
if (a >= 128) ~= (buffer.readu8(back, i * 4 + 3) == 255) then
|
|
bad += 1
|
|
end
|
|
end
|
|
t:eq(bad, 0, e.name .. " bc1: alpha mask")
|
|
end
|
|
end
|
|
end
|
|
|
|
-- flat colours are exact through the single-colour tables (BC1 4-colour and 3-colour, BC4)
|
|
do
|
|
local rand = T.rng(3)
|
|
local worst = 0
|
|
for _ = 1, 200 do
|
|
local r, g, b = rand(256), rand(256), rand(256)
|
|
local px = buffer.create(64)
|
|
for i = 0, 15 do
|
|
buffer.writeu32(px, i * 4, r + g * 256 + b * 65536 + (if i == 5 then 0 else 255) * 16777216)
|
|
end
|
|
for _, opaque in { true, false } do
|
|
if opaque then
|
|
buffer.writeu8(px, 23, 255)
|
|
else
|
|
buffer.writeu8(px, 23, 0)
|
|
end
|
|
local back = Bcn.decode("bc1", Bcn.encode("bc1", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
|
|
for i = 0, 15 do
|
|
if opaque or i ~= 5 then
|
|
for c = 0, 2 do
|
|
worst = math.max(worst, math.abs(buffer.readu8(back, i * 4 + c) - buffer.readu8(px, i * 4 + c)))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- 5 / 6-bit endpoints reach every 8-bit value within 1 (4-colour) / 2 (3-colour midpoints)
|
|
t:check(worst <= 2, "flat bc1 colours within " .. worst)
|
|
local px = buffer.create(64)
|
|
for v = 0, 255, 5 do
|
|
for i = 0, 15 do
|
|
buffer.writeu32(px, i * 4, v + 0xFF000000)
|
|
end
|
|
local back = Bcn.decode("bc4", Bcn.encode("bc4", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
|
|
t:eq(buffer.readu8(back, 0), v, "flat bc4 " .. v)
|
|
end
|
|
end
|
|
|
|
-- Half
|
|
do
|
|
t:eq(Half.fromFloat(1), 0x3C00, "half 1")
|
|
t:eq(Half.fromFloat(-2), 0xC000, "half -2")
|
|
t:eq(Half.fromFloat(65504), 0x7BFF, "half max")
|
|
t:eq(Half.fromFloat(1e6), 0x7C00, "half overflow")
|
|
t:eq(Half.fromFloat(2 ^ -24), 1, "half smallest subnormal")
|
|
t:eq(Half.fromFloat(0 / 0), 0x7E00, "half NaN")
|
|
local ok = true
|
|
for h = 0, 65535 do
|
|
local f = Half.TABLE[h]
|
|
if f == f and Half.fromFloat(f) ~= h and not (h == 0x8000) then
|
|
ok = false
|
|
end
|
|
end
|
|
t:check(ok, "half round trip of every pattern")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local _, e1 = Bcn.decode("bc9", buffer.create(8), 0, 4, 4)
|
|
t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind")
|
|
local _, e2 = Bcn.decode("bc1", buffer.create(7), 0, 4, 4)
|
|
t:check(e2 and string.find(e2, "truncated") ~= nil, "short data")
|
|
local _, e3 = Bcn.encode("bc7", buffer.create(64), 4, 4)
|
|
t:check(e3 ~= nil, "no bc7 encoder")
|
|
local _, e4 = Bcn.encode("bc1", buffer.create(10), 4, 4)
|
|
t:check(e4 ~= nil, "short pixels")
|
|
end
|
|
|
|
-- fuzz: random block data of every kind and size
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
table.insert(samples, buffer.fromstring(c.data))
|
|
end
|
|
for _, kind in { "bc1", "bc2", "bc3", "bc4", "bc4s", "bc5", "bc5s", "bc6h", "bc6hs", "bc7" } do
|
|
t:fuzz("decode " .. kind, function(b: buffer)
|
|
local n = buffer.len(b) // Bcn.BLOCK[kind]
|
|
if n < 1 then
|
|
return Bcn.decode(kind, b, 0, 4, 4)
|
|
end
|
|
return Bcn.decode(kind, b, 0, 4 * n - 1, 3)
|
|
end, samples, { mutations = 2 })
|
|
end
|
|
t:done()
|