Files

136 lines
5.3 KiB
Luau

-- image/exr tests against the OpenEXR 3.5 bindings, exrmaketiled, exrmultipart and exrheader (tools/fixtures/exr.py).
local T = require("@tests/T")
local Exr = require("@rosetta/image/exr")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/exr")
local t = T.suite("exr")
t:eq(Exr.name, "exr", "name")
t:check(Exr.caps.decode and Exr.caps.encode, "caps")
local function dec(data: any, opts: any?): (any, any)
return Exr.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function worst(a: buffer, b: buffer, n: number): number
local m = 0
for i = 0, n * 4 - 1 do
local x, y = buffer.readf32(a, i * 4), buffer.readf32(b, i * 4)
if x ~= y then
m = math.max(m, math.abs(x - y) / math.max(1, math.abs(y)))
end
end
return m
end
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 opts = { native = true, layer = c.layer, part = c.part, mips = c.mips }
local img, err = dec(c.data, opts)
t:check(img, c.name .. ": " .. tostring(err))
t:check(img.width == c.w and img.height == c.h and img.format == "rgbaf32", c.name .. ": size")
local d = worst(img.pixels, buffer.fromstring(c.floats), c.w * c.h)
t:check(d < 1e-6, string.format("%s: off by %g", c.name, d))
if c.partName then
local byName = dec(c.data, { native = true, part = c.partName })
t:check(byName and buffer.tostring(byName.pixels) == buffer.tostring(img.pixels), c.name .. ": part by name")
t:eq(img.meta.source.parts[c.part].name, c.partName, c.name .. ": part names")
end
if c.mips and string.find(c.name, "mip") then
local mm = img.meta.mipmaps
t:check(mm and #mm >= 5, c.name .. ": mip levels")
local up = string.find(c.name, "up") ~= nil
t:eq(mm[1].width, if up then math.ceil(c.w / 2) else c.w // 2, c.name .. ": level 1 width")
end
local d8 = dec(c.data, { layer = c.layer, part = c.part })
t:eq(d8.format, "rgba8", c.name .. ": rgba8 by default")
end
t:note("%d files match the OpenEXR bindings", #Fx.cases)
do
for _, c in Fx.cases do
if c.name == "window_zip.exr" then
local img = dec(c.data)
t:check(img.meta.dataWindow.x == 5 and img.meta.dataWindow.y == -3, "data window origin")
t:check(img.meta.displayWindow.width == 40 and img.meta.displayWindow.height == 20, "display window")
t:eq(img.meta.source.compression, "zip", "compression name")
end
end
end
-- our files: the bindings read our values back, exrheader accepts them
for _, o in Fx.ours do
t:check(o.headerOk, o.name .. ": exrheader")
local img = dec(o.data, { native = true })
local n = img.width * img.height
local d = worst(img.pixels, buffer.fromstring(o.floats), n)
t:check(d == 0, string.format("%s: the bindings read %g off our decode", o.name, d))
end
t:note("%d of our files read back by the OpenEXR bindings", #Fx.ours)
-- round trips: FLOAT exactly, HALF to half precision
do
local f = Types.Image.new(13, 9, "rgbaf32")
local rand = T.rng(5)
for i = 0, 13 * 9 * 4 - 1 do
buffer.writef32(f.pixels, i * 4, (rand(20000) - 5000) / 997)
end
for _, comp in { "none", "zips", "zip" } do
local back = dec(Exr.encode(f, { compression = comp :: any, pixelType = "float" }) :: buffer, { native = true })
t:eq(buffer.tostring(back.pixels), buffer.tostring(f.pixels), comp .. ": float round trip")
local hb = dec(Exr.encode(f, { compression = comp :: any }) :: buffer, { native = true })
t:check(worst(hb.pixels, f.pixels, 13 * 9) < 1e-3, comp .. ": half round trip")
end
local img8 = Types.Image.new(4, 4)
for i = 0, 63 do
buffer.writeu8(img8.pixels, i, (i * 37) % 256)
end
local back8 = dec(Exr.encode(img8, { pixelType = "float" }) :: buffer)
local ok = true
for i = 0, 63 do
ok = ok and math.abs(buffer.readu8(back8.pixels, i) - buffer.readu8(img8.pixels, i)) <= 1
end
t:check(ok, "rgba8 through the display curve and back")
local _, e = Exr.encode(f, { compression = "piz" :: any })
t:check(e and e.code == Err.ARG, "no PIZ encoder")
end
do
local c = Fx.cases[1]
t:eq(Exr.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
t:eq(Exr.sniff(Bytes.reader(buffer.fromstring("v/1\0"))), 0, "sniff other")
local info = Exr.info(Bytes.reader(buffer.fromstring(c.data)))
t:check(info and #info.parts == 1 and #info.parts[1].channels == 4, "info")
local _, e = dec(string.sub(c.data, 1, 40))
t:check(e and e.code == Err.TRUNCATED, "short header: " .. tostring(e))
local _, e2 = dec(c.data, { maxPixels = 10 })
t:check(e2 and e2.code == Err.LIMIT, "limit")
local _, e3 = dec(c.data, { part = 3 })
t:check(e3 and e3.code == Err.ARG, "no such part")
-- a zeroed offset table is rebuilt
local b = buffer.fromstring(c.data)
local info2 = Exr.info(Bytes.reader(b)) :: any
local tab = info2.dataStart - 8 * info2.parts[1].chunks
buffer.fill(b, tab, 0, 8 * info2.parts[1].chunks)
local img = dec(b, { native = true })
t:check(img and buffer.tostring(img.pixels) == buffer.tostring(dec(c.data, { native = true }).pixels), "rebuilt offset table")
end
local samples = {}
for i, c in Fx.cases do
if i % 3 == 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()