Files
rosetta/tests/codecs/dct.luau
T

155 lines
4.4 KiB
Luau

-- codecs/image/Dct tests: the islow IDCT against a double-precision reference, the forward DCT + quantiser.
-- The bit-exact djpeg / cjpeg checks are in tests/image/jpeg.luau.
local T = require("@tests/T")
local Dct = require("@rosetta/codecs/image/Dct")
local t = T.suite("dct")
local rand = T.rng(11)
local C = {}
for u = 0, 7 do
C[u] = if u == 0 then math.sqrt(0.5) else 1
end
local cosT = {}
for x = 0, 7 do
for u = 0, 7 do
cosT[x * 8 + u] = math.cos((2 * x + 1) * u * math.pi / 16)
end
end
local ones = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(ones, i * 2, 1)
end
-- inverse: random sparse coefficients, compare with the float IDCT
do
local worst = 0
for trial = 1, 300 do
local coef = buffer.create(128)
local F = {}
for i = 0, 63 do
local v = 0
if rand(4) == 0 or i == 0 then
v = rand(512) - 256
if i == 0 then
v = rand(2048) - 1024
end
end
F[i] = v
buffer.writei16(coef, i * 2, v)
end
local plane = buffer.create(128)
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
for y = 0, 7 do
for x = 0, 7 do
local s = 0
for v = 0, 7 do
for u = 0, 7 do
s += C[u] * C[v] * F[v * 8 + u] * cosT[x * 8 + u] * cosT[y * 8 + v]
end
end
local want = math.clamp(math.floor(s / 4 + 128 + 0.5), 0, 255)
local got = buffer.readu16(plane, (y * 8 + x) * 2)
worst = math.max(worst, math.abs(got - want))
end
end
local _ = trial
end
t:check(worst <= 1, "idct within 1 of the float reference: " .. worst)
t:note("idct: worst error %d over 300 random blocks", worst)
end
do
local worst = 0
for _ = 1, 200 do
local src = buffer.create(64)
for i = 0, 63 do
buffer.writeu8(src, i, rand(256))
end
local q = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(q, i * 2, 1 + rand(40))
end
local coef = buffer.create(128)
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, q, coef, 0)
for v = 0, 7 do
for u = 0, 7 do
local s = 0
for y = 0, 7 do
for x = 0, 7 do
s += (buffer.readu8(src, y * 8 + x) - 128) * cosT[x * 8 + u] * cosT[y * 8 + v]
end
end
s *= C[u] * C[v] / 4
local want = s / buffer.readu16(q, (v * 8 + u) * 2)
worst = math.max(worst, math.abs(buffer.readi16(coef, (v * 8 + u) * 2) - want))
end
end
end
t:check(worst <= 0.75, "fdct + quantise within rounding of the float reference: " .. worst)
local rt = 0
for _ = 1, 100 do
local src = buffer.create(64)
for i = 0, 63 do
buffer.writeu8(src, i, rand(256))
end
local coef = buffer.create(128)
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, ones, coef, 0)
local plane = buffer.create(128)
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
for i = 0, 63 do
rt = math.max(rt, math.abs(buffer.readu16(plane, i * 2) - buffer.readu8(src, i)))
end
end
t:check(rt <= 1, "fdct -> idct round trip at q = 1: " .. rt)
-- edge replication: a 3x2 image is the same as its 8x8 edge-extended version
local small = T.bytes({ 10, 200, 30, 40, 50, 250 })
local ext = buffer.create(64)
for y = 0, 7 do
for x = 0, 7 do
buffer.writeu8(ext, y * 8 + x, buffer.readu8(small, math.min(y, 1) * 3 + math.min(x, 2)))
end
end
local a, b = buffer.create(128), buffer.create(128)
Dct.fdctQuantize(small, 3, 0, 0, 3, 2, ones, a, 0)
Dct.fdctQuantize(ext, 8, 0, 0, 8, 8, ones, b, 0)
t:check(buffer.tostring(a) == buffer.tostring(b), "edge replication")
end
-- components and garbage: clamped output, no throw
do
local coef = buffer.create(4 * 3 * 128)
for i = 0, buffer.len(coef) // 2 - 1 do
buffer.writei16(coef, i * 2, rand(65536) - 32768)
end
local q = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(q, i * 2, rand(65536))
end
for _, size in { 8, 4, 2, 1 } do
for _, prec in { { 2, 128, 255 }, { 1, 2048, 4095 } } do
local plane = buffer.create(4 * size * 3 * size * 2)
local ok = pcall(Dct.idctComponent, coef, q, 4, 3, size, plane, prec[1], prec[2], prec[3])
t:check(ok, "idctComponent size " .. size .. " on garbage")
local inRange = true
for i = 0, buffer.len(plane) // 2 - 1 do
if buffer.readu16(plane, i * 2) > prec[3] then
inRange = false
end
end
t:check(inRange, "clamped output, size " .. size)
end
end
local samples = { string.rep("\0", 128) }
t:fuzz("idctBlock", function(b: buffer)
local cb = buffer.create(128)
buffer.copy(cb, 0, b, 0, math.min(128, buffer.len(b)))
local plane = buffer.create(128)
Dct.idctBlock(cb, 0, q, plane, 0, 8, 2, 128, 255)
return plane
end, samples, { garbage = 50 })
end
t:done()