75 lines
2.8 KiB
Luau
75 lines
2.8 KiB
Luau
-- image/vtf tests: VTFs written by srctools (tools/fixtures/vtf.py: every texel format it writes, versions 7.2 / 7.4
|
|
-- / 7.5, mips, two-frame animations) decode to srctools' own RGBA (DXT within its libsquish rounding), frames, the
|
|
-- mip / thumbnail layout; the half / float / 16-bit formats srctools cannot write against the fixture writer; errors;
|
|
-- the fuzz pass.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Vtf = require("@formats/image/vtf")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/vtf")
|
|
|
|
local t = T.suite("vtf")
|
|
t:eq(Vtf.name, "vtf", "name")
|
|
|
|
local function compare(px: buffer, want: string, tol: number): (number, number)
|
|
local worst, at = 0, -1
|
|
for i = 0, #want - 1 do
|
|
local d = math.abs(buffer.readu8(px, i) - string.byte(want, i + 1))
|
|
if d > worst then
|
|
worst, at = d, i
|
|
end
|
|
end
|
|
return worst, at
|
|
end
|
|
|
|
local exact = 0
|
|
for _, c in Fx do
|
|
local img, err = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(img, c.name .. ": " .. tostring(err))
|
|
t:eq(F.detect(c.data), "vtf", c.name .. ": F.detect")
|
|
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
|
local worst, at = compare(img.pixels, c.rgba, c.tol)
|
|
t:check(worst <= c.tol, string.format("%s (%s): within %d of srctools (worst %d at byte %d)", c.name, c.version, c.tol, worst, at))
|
|
if worst == 0 then
|
|
exact += 1
|
|
end
|
|
t:eq(img.meta.source.texel, c.name, c.name .. ": texel format")
|
|
t:check(img.meta.source.mips > 1 or c.mips == 1, c.name .. ": mips counted")
|
|
if c.frames > 1 then
|
|
t:eq(#img.frames, c.frames, c.name .. ": frames")
|
|
local lw = compare(img.frames[c.frames].image.pixels, c.last, c.tol)
|
|
t:check(lw <= c.tol, c.name .. ": last frame")
|
|
end
|
|
if c.mips ~= 1 then
|
|
local small = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)), { mip = 1 }) :: any
|
|
t:check(small and small.width == math.max(1, c.width // 2), c.name .. ": mip 1")
|
|
end
|
|
end
|
|
t:note("%d of %d files exactly equal to their reference (srctools or the fixture writer)", exact, #Fx)
|
|
|
|
do
|
|
local c = Fx[1]
|
|
local _, e1 = Vtf.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 20))))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
|
local bad = buffer.fromstring(c.data)
|
|
buffer.writeu32(bad, 8, 9)
|
|
local _, e2 = Vtf.decode(Bytes.reader(bad))
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7.9")
|
|
local p8 = buffer.fromstring(c.data)
|
|
buffer.writei32(p8, 52, 7)
|
|
local _, e3 = Vtf.decode(Bytes.reader(p8))
|
|
t:check(e3 and e3.code == Err.UNSUPPORTED, "P8")
|
|
end
|
|
|
|
local samples = {}
|
|
for i, c in Fx do
|
|
if i % 3 == 0 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Vtf.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 30 })
|
|
t:done()
|