New shared codecs: codecs/image/Texel (uncompressed GPU texel formats incl. packed, shared exponent, YUY2), codecs/image/Texture (dispatch over Bcn / Etc / Texel, surface assembly, mip halving). Tested against nvcompress / nvassemble / ImageMagick files (Pillow per surface, Godot for BC6H floats), hand-written formats, and our files read back by Pillow + nvddsinfo.
213 lines
8.6 KiB
Luau
213 lines
8.6 KiB
Luau
-- image/dds tests: files from NVIDIA Texture Tools (BGRA, BC1..BC7 incl. BC6H, mips, DX10, cube / array / volume)
|
|
-- and ImageMagick against every surface's reference decode (Pillow per surface, Godot floats for BC6H); hand-written
|
|
-- uncompressed DXGI / legacy / FourCC formats against their values (rgba8 and native floats); our encoder's files read
|
|
-- by Pillow and nvddsinfo, and round trips; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/dds.py.
|
|
local T = require("@tests/T")
|
|
local Dds = require("@formats/image/dds")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Types = require("@formats/Types")
|
|
local Fx = require("@tests/gen/dds")
|
|
|
|
local t = T.suite("dds")
|
|
|
|
t:eq(Dds.name, "dds", "name")
|
|
t:check(Dds.caps.decode and Dds.caps.encode, "caps")
|
|
|
|
local function dec(data: any, opts: any?): (any, any)
|
|
return Dds.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
|
|
|
|
-- tool-written files
|
|
local surfaces = 0
|
|
for _, c in Fx.cases do
|
|
local img, err = dec(c.data, { mips = true })
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:eq(img.meta.layout, c.layout, c.name .. ": layout")
|
|
t:eq(img.meta.source.mips, c.mips, c.name .. ": mips")
|
|
local frames = img.frames or { { image = img } }
|
|
local native = nil
|
|
for _, s in c.surfaces do
|
|
local im
|
|
if s.level == 0 then
|
|
local idx = s.layer * (if c.layout == "volume" then c.depth else 1) + s.slice + 1
|
|
im = frames[idx] and frames[idx].image
|
|
else
|
|
im = img.meta.mipmaps and img.meta.mipmaps[s.level]
|
|
end
|
|
local label = string.format("%s: level %d layer %d slice %d", c.name, s.level, s.layer, s.slice)
|
|
t:check(im and im.width == s.w and im.height == s.h, label .. ": surface size")
|
|
if s.floats then
|
|
native = native or dec(c.data, { native = true, mips = true })
|
|
local nim = if s.level == 0 then native else native.meta.mipmaps[s.level]
|
|
t:eq(nim.format, "rgbaf32", label .. ": native rgbaf32")
|
|
local g = buffer.fromstring(s.floats)
|
|
local worst = 0
|
|
for i = 0, s.w * s.h * 4 - 1 do
|
|
if i % 4 ~= 3 then
|
|
local a, b = buffer.readf32(nim.pixels, i * 4), buffer.readf32(g, i * 4)
|
|
worst = math.max(worst, math.abs(a - b) / math.max(math.abs(b), 1e-3))
|
|
end
|
|
end
|
|
t:check(worst < 1e-3, label .. ": floats against Godot " .. worst)
|
|
else
|
|
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
|
|
surfaces += 1
|
|
end
|
|
-- surfaces = false keeps only the first
|
|
local one = dec(c.data, { surfaces = false })
|
|
t:check(one and one.frames == nil, c.name .. ": surfaces = false")
|
|
end
|
|
t:note("%d tool-written files, %d surfaces match the references", #Fx.cases, surfaces)
|
|
|
|
-- hand-written formats
|
|
for _, c in Fx.hand do
|
|
local img, err = dec(c.data)
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
local n = img.width * img.height
|
|
local d = maxDiff(img.pixels, buffer.fromstring(c.rgba8), n)
|
|
t:check(d <= 1, string.format("%s: rgba8 %d off", c.name, d))
|
|
if c.floats then
|
|
local nimg = dec(c.data, { native = true })
|
|
local f = buffer.fromstring(c.floats)
|
|
local worst = 0
|
|
local bpp = Types.BPP[nimg.format]
|
|
for i = 0, n * 4 - 1 do
|
|
local want = buffer.readf32(f, i * 4)
|
|
local got
|
|
if nimg.format == "rgbaf32" then
|
|
got = buffer.readf32(nimg.pixels, i * 4)
|
|
elseif nimg.format == "rgba16" then
|
|
got = buffer.readu16(nimg.pixels, i * 2) / 65535
|
|
else
|
|
got = buffer.readu8(nimg.pixels, i) / 255
|
|
end
|
|
worst = math.max(worst, math.abs(got - want) / math.max(1, math.abs(want)))
|
|
end
|
|
local tol = if nimg.format == "rgba8" then 1 / 255 elseif nimg.format == "rgba16" then 1 / 65535 else 1e-6
|
|
t:check(worst <= tol + 1e-9, string.format("%s: native %s off by %g", c.name, nimg.format, worst))
|
|
t:check(bpp ~= nil, c.name .. ": a standard format")
|
|
end
|
|
end
|
|
t:note("%d hand-written formats match", #Fx.hand)
|
|
|
|
-- our encoder: Pillow / nvddsinfo read the files, and they round-trip
|
|
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
|
|
local img, err = dec(o.data, { mips = true, native = true })
|
|
t:check(img, o.name .. ": " .. tostring(err))
|
|
t:check(o.width == img.width and o.height == img.height, o.name .. ": nvddsinfo size")
|
|
local mips = img.meta.source.mips
|
|
t:eq(math.max(1, o.mipcount), mips, o.name .. ": nvddsinfo mip count")
|
|
if string.find(o.name, "mips") or string.find(o.name, "rgba16") or string.find(o.name, "rgba32f") or string.find(o.name, "cube") or string.find(o.name, "dx10") then
|
|
if not string.find(o.name, "rgba16f") and not string.find(o.name, "array") then
|
|
t:check(mips > 1 and #img.meta.mipmaps == mips - 1, o.name .. ": a mip chain")
|
|
end
|
|
end
|
|
if o.pillow then
|
|
local p = buffer.fromstring(o.pillow)
|
|
local exact = string.find(o.name, "rgba8") or string.find(o.name, "bgra8")
|
|
local d = maxDiff(img.pixels, p, img.width * img.height)
|
|
t:check(d == 0, string.format("%s: Pillow reads %d off our decode", o.name, d))
|
|
if exact then
|
|
t:eq(buffer.tostring(p), buffer.tostring(src), o.name .. ": Pillow reads the source exactly")
|
|
end
|
|
end
|
|
if string.find(o.name, "cube") then
|
|
t:eq(img.meta.layout, "cube", o.name .. ": cube")
|
|
t:eq(#img.frames, 6, o.name .. ": 6 faces")
|
|
t:check(string.find(o.caps2, "FE00", 1, true) ~= nil, o.name .. ": nvddsinfo sees all faces")
|
|
elseif string.find(o.name, "array") then
|
|
t:eq(#img.frames, 6, o.name .. ": 6 layers")
|
|
t:eq(o.array, "6", o.name .. ": nvddsinfo array size")
|
|
end
|
|
if string.find(o.name, "rgba32f") then
|
|
t:check(string.find(o.dxgi, "R32G32B32A32_FLOAT", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
|
|
t:eq(img.format, "rgbaf32", o.name .. ": rgbaf32")
|
|
t:check(math.abs(buffer.readf32(img.pixels, 36 * 16) - 4) < 1e-6, o.name .. ": float values kept")
|
|
elseif string.find(o.name, "rgba16f") then
|
|
t:check(string.find(o.dxgi, "R16G16B16A16_FLOAT", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
|
|
t:check(math.abs(buffer.readf32(img.pixels, 36 * 16) - 4) < 1e-3, o.name .. ": half values")
|
|
elseif string.find(o.name, "rgba16") then
|
|
t:check(string.find(o.dxgi, "R16G16B16A16_UNORM", 1, true) ~= nil, o.name .. ": DXGI format " .. o.dxgi)
|
|
t:eq(img.format, "rgba16", o.name .. ": rgba16")
|
|
end
|
|
end
|
|
t:note("%d of our files read by Pillow / nvddsinfo", #Fx.ours)
|
|
|
|
-- round trips
|
|
do
|
|
local img = Types.Image.new(37, 23)
|
|
img.pixels = src
|
|
for _, f in { "rgba8", "bgra8" } do
|
|
local back = dec(Dds.encode(img, { format = f :: any }) :: buffer)
|
|
t:eq(buffer.tostring(back.pixels), buffer.tostring(src), f .. " 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(Dds.encode(f) :: buffer, { native = true })
|
|
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), "rgba32f round trip")
|
|
local _, e1 = Dds.encode(f, { format = "bc1" })
|
|
t:check(e1 and e1.code == Err.ARG, "bc1 needs rgba8")
|
|
local _, e2 = Dds.encode(img, { format = "bc7" :: any })
|
|
t:check(e2 and e2.code == Err.ARG, "no bc7 encoder")
|
|
end
|
|
|
|
-- sniff and errors
|
|
do
|
|
local c = Fx.cases[1]
|
|
t:eq(Dds.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
|
t:eq(Dds.sniff(Bytes.reader(buffer.fromstring("DDX \124\0\0\0"))), 0, "sniff other")
|
|
local _, e = dec(string.sub(c.data, 1, 100))
|
|
t:check(e and e.code == Err.TRUNCATED, "short header")
|
|
local _, e2 = dec(string.sub(c.data, 1, 140))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "no pixels: " .. tostring(e2))
|
|
local bad = buffer.fromstring(c.data)
|
|
buffer.writeu32(bad, 4, 100)
|
|
local _, e3 = dec(bad)
|
|
t:check(e3 and e3.code == Err.CORRUPT, "bad header size")
|
|
local _, e4 = dec(c.data, { maxPixels = 10 })
|
|
t:check(e4 and e4.code == Err.LIMIT, "limit")
|
|
local info = Dds.info(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(info and info.width > 0, "info")
|
|
end
|
|
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if i % 3 == 0 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
for _, c in Fx.hand do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return dec(b, { mips = true })
|
|
end, samples, { mutations = 6 })
|
|
t:done()
|