-- image/blp tests against Pillow and the fixture writer (tools/fixtures/blp.py). local T = require("@tests/T") local F = require("@rosetta") local Blp = require("@rosetta/image/blp") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/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 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()