125 lines
4.7 KiB
Luau
125 lines
4.7 KiB
Luau
-- layered/ora tests: OpenRaster files assembled by the spec (offsets, opacity, hidden, composite-ops, nested isolate /
|
|
-- auto stacks, no merged image) against the attributes and pixels written there; our encoder's file taken apart by
|
|
-- python (stored mimetype first, stack.xml, Pillow's read of every PNG); round trips; hooks; errors; the fuzz no-throw
|
|
-- pass. Fixtures: tools/fixtures/ora.py.
|
|
local T = require("@tests/T")
|
|
local Ora = require("@rosetta/layered/ora")
|
|
local Png = require("@rosetta/image/png")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/ora")
|
|
|
|
local t = T.suite("ora")
|
|
t:eq(Ora.name, "ora", "name")
|
|
t:eq(Ora.category, "layered", "category")
|
|
t:check(Ora.caps.decode and Ora.caps.encode, "caps")
|
|
|
|
local hooks = {
|
|
decodeImage = function(b: buffer): (any, any)
|
|
return Png.decode(Bytes.reader(b))
|
|
end,
|
|
encodeImage = function(img: any, _f: string): (any, any)
|
|
return Png.encode(img)
|
|
end,
|
|
}
|
|
|
|
-- the tree depth-first, bottom to top, groups before their children
|
|
local function walk(layers: { any }, depth: number, out: { any })
|
|
for _, l in layers do
|
|
table.insert(out, { layer = l, depth = depth })
|
|
if l.children then
|
|
walk(l.children, depth + 1, out)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function compare(label: string, doc: any, want: { any })
|
|
local got = walk(doc.layers, 0, {})
|
|
t:eq(#got, #want, label .. ": layer count")
|
|
for i, w in want do
|
|
local g = got[i]
|
|
if not g then
|
|
break
|
|
end
|
|
local l = g.layer
|
|
local ll = label .. ": " .. w.name
|
|
t:eq(l.name, w.name, ll .. " name")
|
|
t:eq(g.depth, w.depth, ll .. " depth")
|
|
t:check(math.abs(l.opacity - w.opacity) < 1e-6, ll .. " opacity")
|
|
t:eq(l.visible, w.visible, ll .. " visibility")
|
|
t:eq(l.blend, w.blend, ll .. " blend")
|
|
if not w.group then
|
|
t:check(l.x == w.x and l.y == w.y, ll .. " position")
|
|
t:check(l.image.width == w.w and l.image.height == w.h and buffer.tostring(l.image.pixels) == w.rgba, ll .. " pixels")
|
|
else
|
|
t:check(l.children ~= nil, ll .. " is a group")
|
|
end
|
|
end
|
|
end
|
|
|
|
for _, c in Fx.cases do
|
|
local doc, err = Ora.decode(Bytes.reader(buffer.fromstring(c.data)), hooks)
|
|
t:check(doc, c.name .. ": " .. tostring(err))
|
|
t:check(doc.kind == "layered" and doc.width == c.w and doc.height == c.h, c.name .. ": size")
|
|
compare(c.name, doc, c.layers)
|
|
t:eq(doc.meta.composite ~= nil, c.merged, c.name .. ": merged image")
|
|
if c.dpi then
|
|
t:eq(doc.meta.dpi.x, c.dpi, c.name .. ": dpi")
|
|
end
|
|
-- round trip
|
|
local enc, eerr = Ora.encode(doc, hooks)
|
|
t:check(enc, c.name .. ": encode " .. tostring(eerr))
|
|
local back = Ora.decode(Bytes.reader(enc :: buffer), hooks)
|
|
compare(c.name .. " round trip", back, c.layers)
|
|
t:eq(Ora.sniff(Bytes.reader(enc :: buffer)), 1, c.name .. ": sniff our file")
|
|
end
|
|
|
|
-- our encoder's file, as python and Pillow read it
|
|
do
|
|
local o = Fx.ours
|
|
t:check(o.ok, "ours: mimetype first, stored")
|
|
t:eq(o.entries[1], "mimetype", "ours: first entry")
|
|
t:check(table.find(o.entries, "mergedimage.png") ~= nil and table.find(o.entries, "Thumbnails/thumbnail.png") ~= nil, "ours: merged image and thumbnail")
|
|
t:check(o.thumb[1] == 40 and o.thumb[2] == 30, "ours: thumbnail size")
|
|
local doc = Ora.decode(Bytes.reader(buffer.fromstring(o.data)), hooks) :: any
|
|
t:check(doc and doc.width == o.w and doc.height == o.h, "ours: decodes")
|
|
-- python's walk is top first (xml order): compare by name
|
|
local byName = {}
|
|
for _, g in walk(doc.layers, 0, {}) do
|
|
byName[g.layer.name] = g
|
|
end
|
|
for _, l in o.layers do
|
|
local g = byName[l.name]
|
|
t:check(g, "ours: layer " .. l.name)
|
|
if g then
|
|
t:check(math.abs(g.layer.opacity - l.opacity) < 1e-6 and g.layer.visible == l.visible and g.depth == l.depth, "ours: " .. l.name .. " attributes")
|
|
if l.tag == "layer" then
|
|
t:check(g.layer.x == l.x and g.layer.y == l.y, "ours: " .. l.name .. " position")
|
|
t:eq(buffer.tostring(g.layer.image.pixels), l.rgba, "ours: " .. l.name .. " pixels through Pillow")
|
|
end
|
|
end
|
|
end
|
|
t:eq(buffer.tostring(doc.meta.composite.pixels), o.merged.rgba, "ours: merged image through Pillow")
|
|
end
|
|
|
|
do
|
|
local c = Fx.cases[1]
|
|
local _, e = Ora.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(e and e.code == Err.UNSUPPORTED, "no decodeImage hook")
|
|
local doc = Ora.decode(Bytes.reader(buffer.fromstring(c.data)), hooks) :: any
|
|
local _, e2 = Ora.encode(doc)
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "no encodeImage hook")
|
|
local _, e3 = Ora.encode({} :: any, hooks)
|
|
t:check(e3 and e3.code == Err.ARG, "not a document")
|
|
t:eq(Ora.sniff(Bytes.reader(buffer.fromstring("PK\3\4"))), 0, "sniff a plain zip")
|
|
end
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Ora.decode(Bytes.reader(b), hooks)
|
|
end, samples, { mutations = 12 })
|
|
t:done()
|