ws-game: image/blp (BLP1 JPEG / palette, BLP2 palette + alpha planes / DXT1-5 via codecs/image/Bcn / BGRA read; BLP2 BGRA write); codecs/image/Jpeg: opts.raw (components without colour conversion, 4th as alpha; additive)
This commit is contained in:
1 parent
e5c0873a79
commit
7a86e357ed
8 files changed
+547
No files matched your search
@@ -0,0 +1,89 @@
|
||||
-- image/blp tests: BLP1 / BLP2 palette files written by Pillow and DXT1 / DXT3 / DXT5 / palette-with-alpha files
|
||||
-- written by tools/fixtures/blp.py decode to Pillow's RGBA; BLP1 JPEG (3 components) to Pillow's, 4 components (BGRA)
|
||||
-- within a PSNR of the source; BLP2 BGRA and BLP1 palette + alpha against the writer; our BLP2 writer round-trips
|
||||
-- (with mips); errors; the fuzz pass. Fixtures: tools/fixtures/blp.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Blp = require("@formats/image/blp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/blp")
|
||||
|
||||
local t = T.suite("blp")
|
||||
t:eq(Blp.name, "blp", "name")
|
||||
|
||||
local function psnr(a: buffer, b: string): number
|
||||
local se = 0
|
||||
for i = 0, #b - 1 do
|
||||
local d = buffer.readu8(a, i) - string.byte(b, i + 1)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(255 * 255 / (se / #b))
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = Blp.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "blp", c.name .. ": F.detect")
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
if c.crc then
|
||||
t:eq(Crc32.of(img.pixels), c.crc, c.name .. ": pixels against Pillow")
|
||||
elseif c.ref then
|
||||
local worst, alpha = 0, true
|
||||
for i = 0, #c.ref - 1 do
|
||||
local d = math.abs(buffer.readu8(img.pixels, i) - string.byte(c.ref, i + 1))
|
||||
if i % 4 == 3 then
|
||||
alpha = alpha and d == 0
|
||||
else
|
||||
worst = math.max(worst, d)
|
||||
end
|
||||
end
|
||||
t:check(worst <= c.tol, c.name .. ": colours within " .. c.tol .. " of Pillow (worst " .. worst .. ")")
|
||||
t:check(alpha, c.name .. ": alpha equal to Pillow's")
|
||||
elseif c.rgbref then
|
||||
local ok = true
|
||||
for i = 0, img.width * img.height - 1 do
|
||||
for k = 0, 2 do
|
||||
ok = ok and buffer.readu8(img.pixels, i * 4 + k) == string.byte(c.rgbref, i * 4 + k + 1)
|
||||
end
|
||||
ok = ok and buffer.readu8(img.pixels, i * 4 + 3) == string.byte(c.alpha, i + 1)
|
||||
end
|
||||
t:check(ok, c.name .. ": RGB against Pillow, alpha plane against the writer")
|
||||
elseif c.psnr then
|
||||
local p = psnr(img.pixels, c.rgba)
|
||||
t:check(p >= c.psnr, string.format("%s: PSNR %.1f dB", c.name, p))
|
||||
t:note("%s: PSNR %.1f dB against the source", c.name, p)
|
||||
else
|
||||
t:check(buffer.tostring(img.pixels) == c.rgba, c.name .. ": pixels against the writer")
|
||||
end
|
||||
-- our writer: BLP2 BGRA, lossless
|
||||
local enc = Blp.encode(img, { mips = true }) :: buffer
|
||||
local back = Blp.decode(Bytes.reader(enc)) :: any
|
||||
t:eq(Crc32.of(back.pixels), Crc32.of(img.pixels), c.name .. ": BLP2 BGRA round trip")
|
||||
t:check(back.meta.source.mips > 1 or (img.width == 1 and img.height == 1), c.name .. ": mips written")
|
||||
end
|
||||
|
||||
do
|
||||
local _, e1 = Blp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx[3].data, 1, 1200))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Blp.decode(Bytes.reader(buffer.fromstring("BLP0" .. string.rep("\0", 200))))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "BLP0")
|
||||
local _, e3 = Blp.encode({ format = "rgba16" } :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "encode needs rgba8")
|
||||
local one = Types.Image.new(1, 1)
|
||||
t:check(Blp.decode(Bytes.reader(Blp.encode(one) :: buffer)) ~= nil, "1 x 1")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Blp.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 40 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user