Files

162 lines
5.8 KiB
Luau

-- image/ktx tests against KTX-Software (ktx create / validate / extract), toktx and nvcompress (tools/fixtures/ktx.py).
local T = require("@tests/T")
local Ktx = require("@rosetta/image/ktx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ktx")
local t = T.suite("ktx")
t:eq(Ktx.name, "ktx", "name")
t:check(Ktx.caps.decode and Ktx.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Ktx.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function maxDiff(a: buffer, b: buffer, n: number): number
local worst = 0
for i = 0, n * 4 - 1 do
worst = math.max(worst, math.abs(buffer.readu8(a, i) - buffer.readu8(b, i)))
end
return worst
end
local nsurf = 0
for _, c in Fx.cases do
if c.unsupported then
local img, e = dec(c.data)
t:check(img == nil and e and e.code == Err.UNSUPPORTED, c.name .. ": UNSUPPORTED (" .. tostring(e) .. ")")
continue
end
local img, err = dec(c.data, { mips = true })
t:check(img, c.name .. ": " .. tostring(err))
if c.layout then
t:eq(img.meta.layout, c.layout, c.name .. ": layout")
end
if c.mips then
t:eq(img.meta.source.mips, c.mips, c.name .. ": mip count")
end
if c.srgb then
t:eq(img.meta.colorSpace, "srgb", c.name .. ": sRGB")
end
local native = nil
local frames = img.frames or { { image = img } }
for _, s in c.surfaces do
local label = string.format("%s: level %d layer %d slice %d", c.name, s.level, s.layer, s.slice)
local im
if s.level == 0 then
local f = frames[s.layer + s.slice + 1]
im = f and f.image
else
im = img.meta.mipmaps and img.meta.mipmaps[s.level]
end
t:check(im and im.width == s.w and im.height == s.h, label .. ": surface size")
if s.rgba then
local d = maxDiff(im.pixels, buffer.fromstring(s.rgba), s.w * s.h)
t:check(d <= s.tol, string.format("%s: %d off the reference", label, d))
end
if s.floats and s.level == 0 and s.layer == 0 then
native = native or dec(c.data, { native = true })
local f = buffer.fromstring(s.floats)
local worst = 0
for i = 0, s.w * s.h * 4 - 1 do
local want = buffer.readf32(f, i * 4)
local got
if native.format == "rgbaf32" then
got = buffer.readf32(native.pixels, i * 4)
elseif native.format == "rgba16" then
got = buffer.readu16(native.pixels, i * 2) / 65535
else
got = buffer.readu8(native.pixels, i) / 255
end
if i % 4 ~= 3 or not string.find(c.name, "bc6h") then
worst = math.max(worst, math.abs(got - want) / math.max(1, math.abs(want)))
end
end
local tol = if native.format == "rgba8" then 1 / 255 elseif native.format == "rgba16" then 1 / 65535 else 1e-3
t:check(worst <= tol + 1e-9, string.format("%s: native %s off by %g", label, native.format, worst))
end
nsurf += 1
end
end
t:note("%d files, %d surfaces match", #Fx.cases, nsurf)
-- our files: ktx validate accepts them, ktx extract reads the first image
local function source(w: number, h: number): buffer
local px = buffer.create(w * h * 4)
for y = 0, h - 1 do
for x = 0, w - 1 do
local o = (y * w + x) * 4
buffer.writeu8(px, o, math.floor(x / (w - 1) * 255 + 0.5))
buffer.writeu8(px, o + 1, math.floor(y / (h - 1) * 255 + 0.5))
buffer.writeu8(px, o + 2, math.floor(((x * 7 + y * 13) % 64) / 63 * 255 + 0.5))
buffer.writeu8(px, o + 3, math.floor(((x + y) % 5) / 4 * 255 + 0.5))
end
end
return px
end
local src = source(37, 23)
for _, o in Fx.ours do
t:check(o.valid, o.name .. ": ktx validate: " .. tostring(o.report))
local img, err = dec(o.data, { mips = true, native = true })
t:check(img, o.name .. ": " .. tostring(err))
if o.png and (string.find(o.name, "rgba8") or string.find(o.name, "srgb")) then
t:eq(o.png, buffer.tostring(src), o.name .. ": ktx extract gives the source")
end
if string.find(o.name, "mips") or string.find(o.name, "zlib") then
t:eq(#img.meta.mipmaps, 5, o.name .. ": mip chain")
end
if string.find(o.name, "cube") then
t:eq(img.meta.layout, "cube", o.name .. ": cube")
t:eq(#img.frames, 6, o.name .. ": faces")
end
end
t:note("%d of our files pass ktx validate", #Fx.ours)
do
local img = Types.Image.new(37, 23)
img.pixels = src
for _, o in { {}, { mips = true }, { zlib = 9 }, { srgb = true } } do
local back = dec(Ktx.encode(img, o :: any) :: buffer)
t:eq(buffer.tostring(back.pixels), buffer.tostring(src), "rgba8 round trip")
end
local f = Types.Image.new(3, 2, "rgbaf32")
for i = 0, 23 do
buffer.writef32(f.pixels, i * 4, i * 0.37 - 2)
end
local back = dec(Ktx.encode(f) :: buffer, { native = true })
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
local _, e = Ktx.encode(f, { pixelFormat = "rgba8" })
t:check(e and e.code == Err.ARG, "format mismatch")
local _, e2 = Ktx.encode(img, { zlib = 12 })
t:check(e2 and e2.code == Err.ARG, "zlib level")
end
do
local c = Fx.cases[1]
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff KTX2")
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring("\171KTX 11\187\r\n\26\n"))), 1, "sniff KTX1")
t:eq(Ktx.sniff(Bytes.reader(buffer.fromstring("PNG"))), 0, "sniff other")
local info = Ktx.info(Bytes.reader(buffer.fromstring(c.data)))
t:check(info and info.version == 2 and info.vkFormat == 37, "info")
local _, e = dec(string.sub(c.data, 1, 60))
t:check(e and e.code == Err.TRUNCATED, "short header")
local _, e2 = dec(string.sub(c.data, 1, #c.data - 100))
t:check(e2 and e2.code == Err.TRUNCATED, "cut data: " .. tostring(e2))
local _, e3 = dec(c.data, { maxPixels = 10 })
t:check(e3 and e3.code == Err.LIMIT, "limit")
end
local samples = {}
for i, c in Fx.cases do
if i % 2 == 0 or c.unsupported then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return dec(b, { mips = true })
end, samples, { mutations = 6 })
t:done()