ws-tex: layered/ora (OpenRaster read + write through the PNG hooks)
stack.xml tree (nested isolate / auto stacks, offsets, opacity, visibility, svg composite-ops), mergedimage, thumbnail. Tested against spec-built files and our file taken apart by python zipfile / ElementTree / Pillow.
This commit is contained in:
1 parent
00d47096ed
commit
d618795ce7
6 files changed
+548
No files matched your search
@@ -80,6 +80,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| vector | svg | .svg | yes | yes | rasterize: true, text: false | `tests/vector/svg.luau` |
|
||||
| vector | tiled | .tmx .tmj .tsx .tsj | yes | yes | tilesets: true, infinite: true | `tests/vector/tiled.luau` |
|
||||
| layered | aseprite | .aseprite .ase | yes | yes | layers: true, groups: true, animation: true, depths: 8, 16, 32 | `tests/layered/aseprite.luau` |
|
||||
| layered | ora | .ora | yes | yes | layers: true, groups: true, depths: 8 | `tests/layered/ora.luau` |
|
||||
| layered | psd | .psd .psb | yes | yes | layers: true, groups: true, depths: 1, 8, 16, 32 | `tests/layered/psd.luau` |
|
||||
| subtitle | ass | .ass .ssa | yes | yes | styles: true | `tests/subtitle/ass.luau` |
|
||||
| subtitle | srt | .srt | yes | yes | styles: false | `tests/subtitle/srt.luau` |
|
||||
|
||||
@@ -914,6 +914,18 @@ local list: { Entry } = {
|
||||
return require("@formats/layered/aseprite")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "ora",
|
||||
category = "layered",
|
||||
path = "layered/ora",
|
||||
extensions = { "ora" },
|
||||
mime = { "image/openraster" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip", "codecs/text/Xml", "codecs/image/Blend" },
|
||||
caps = { decode = true, encode = true, layers = true, groups = true, depths = { 8 } },
|
||||
load = function()
|
||||
return require("@formats/layered/ora")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "psd",
|
||||
category = "layered",
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
--!strict
|
||||
-- layered/ora: OpenRaster (the OpenRaster 0.0.6 specification, freedesktop.org; new code): a zip (containers/Zip) with
|
||||
-- a stored "mimetype" first, stack.xml (codecs/text/Xml), PNG layers, mergedimage.png and Thumbnails/thumbnail.png.
|
||||
-- Decode: the stack tree (nested <stack> = groups: isolation="auto" is passThrough; <layer> name / src / x / y /
|
||||
-- opacity / visibility / composite-op, svg:* ops mapped to the codecs/image/Blend names, unknown ones kept as written),
|
||||
-- layers bottom to top as Types.Layered, the PNGs through the api hook opts.decodeImage (F.load passes it; without it:
|
||||
-- UNSUPPORTED), mergedimage.png as meta.composite, the resolution (meta.dpi). Encode: a Layered (rgba8 layers, groups)
|
||||
-- with every layer, the merged image (codecs/image/Blend flattens the tree, or opts.composite) and a thumbnail (up to
|
||||
-- 256 px, nearest) as PNGs through opts.encodeImage. Not supported: non-PNG layer sources, the <text> / <filter> /
|
||||
-- <selection> elements (ignored).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Zip = require("@formats/containers/Zip")
|
||||
local Xml = require("@formats/codecs/text/Xml")
|
||||
local Blend = require("@formats/codecs/image/Blend")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Ora = {
|
||||
name = "ora",
|
||||
category = "layered",
|
||||
extensions = { "ora" },
|
||||
mime = { "image/openraster" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Zip", "codecs/text/Xml", "codecs/image/Blend" },
|
||||
caps = { decode = true, encode = true, layers = true, groups = true, depths = { 8 } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
decodeImage: ((buffer) -> (Types.Image?, Err.Err?))?, -- decodes the PNG layers (the api passes one)
|
||||
composite: boolean?, -- decode mergedimage.png into meta.composite (default true)
|
||||
maxLayers: number?, -- default 10000
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
encodeImage: ((Types.Image, string) -> (buffer?, Err.Err?))?, -- writes the PNGs (the api passes one)
|
||||
composite: Types.Image?, -- the merged image (default: the layers flattened with codecs/image/Blend)
|
||||
thumbnail: boolean?, -- write Thumbnails/thumbnail.png (default true)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MIME = "image/openraster"
|
||||
local OPS: { [string]: string } = {
|
||||
["svg:src-over"] = "normal", ["svg:multiply"] = "multiply", ["svg:screen"] = "screen", ["svg:overlay"] = "overlay",
|
||||
["svg:darken"] = "darken", ["svg:lighten"] = "lighten", ["svg:color-dodge"] = "colorDodge", ["svg:color-burn"] = "colorBurn",
|
||||
["svg:hard-light"] = "hardLight", ["svg:soft-light"] = "softLight", ["svg:difference"] = "difference",
|
||||
["svg:exclusion"] = "exclusion", ["svg:hue"] = "hue", ["svg:saturation"] = "saturation", ["svg:color"] = "color",
|
||||
["svg:luminosity"] = "luminosity", ["svg:plus"] = "linearDodge",
|
||||
}
|
||||
local BACK: { [string]: string } = {}
|
||||
for k, v in OPS do
|
||||
BACK[v] = k
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Ora.sniff(r: Bytes.Reader): number
|
||||
-- a zip whose first (stored) entry is "mimetype" holding image/openraster
|
||||
if r:peek(4) == "PK\3\4" and r:peek(8, 30) == "mimetype" and r:peek(#MIME, 38) == MIME then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function num(v: any, default: number): number
|
||||
local n = tonumber(v)
|
||||
return if n and n == n then n else default
|
||||
end
|
||||
|
||||
function Ora.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Layered?, Err.Err?)
|
||||
local zip, zerr = Zip.open(r)
|
||||
if not zip then
|
||||
return nil, zerr
|
||||
end
|
||||
local mt = zip:readString("mimetype")
|
||||
if mt and string.gsub(mt, "%s+$", "") ~= MIME then
|
||||
return nil, Err.new(Err.CORRUPT, "mimetype is not " .. MIME)
|
||||
end
|
||||
local stackText, serr = zip:readString("stack.xml")
|
||||
if not stackText then
|
||||
return nil, serr or Err.new(Err.CORRUPT, "missing stack.xml")
|
||||
end
|
||||
local doc, xerr = Xml.decode(stackText)
|
||||
if not doc then
|
||||
return nil, xerr
|
||||
end
|
||||
local root = doc.root
|
||||
if not root or root.name ~= "image" then
|
||||
return nil, Err.new(Err.CORRUPT, "stack.xml has no <image> root")
|
||||
end
|
||||
local decodeImage = opts and opts.decodeImage
|
||||
if not decodeImage then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "PNG layers need the decodeImage hook")
|
||||
end
|
||||
local W, H = num(root.attrs.w, 0), num(root.attrs.h, 0)
|
||||
if W < 1 or H < 1 or W > 65536 or H > 65536 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad image size")
|
||||
end
|
||||
local maxLayers = opts and opts.maxLayers or 10000
|
||||
local count = 0
|
||||
local function png(path: string): (Types.Image?, Err.Err?)
|
||||
local data, derr = zip:read(path)
|
||||
if not data then
|
||||
return nil, derr or Err.new(Err.CORRUPT, "missing " .. path)
|
||||
end
|
||||
local ok, img, ierr = pcall(decodeImage, data)
|
||||
if not ok or not img then
|
||||
return nil, Err.new(Err.CORRUPT, path .. ": " .. tostring(if ok then ierr else img))
|
||||
end
|
||||
return img, nil
|
||||
end
|
||||
-- a <stack>'s children, top first in the file -> bottom to top
|
||||
local function stack(node: any, ox: number, oy: number): ({ Types.Layer }?, Err.Err?)
|
||||
local out: { Types.Layer } = {}
|
||||
for _, ch in node.children do
|
||||
if type(ch) ~= "table" then
|
||||
continue
|
||||
end
|
||||
local a = ch.attrs or {}
|
||||
local visible = a.visibility ~= "hidden"
|
||||
local op = a["composite-op"] or "svg:src-over"
|
||||
local blend = OPS[op] or op
|
||||
local x, y = ox + num(a.x, 0), oy + num(a.y, 0)
|
||||
count += 1
|
||||
if count > maxLayers then
|
||||
return nil, Err.new(Err.LIMIT, "too many layers")
|
||||
end
|
||||
if ch.name == "layer" then
|
||||
local src = a.src
|
||||
if type(src) ~= "string" then
|
||||
continue
|
||||
end
|
||||
if string.lower(string.sub(src, -4)) ~= ".png" then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "layer source " .. src)
|
||||
end
|
||||
local img, perr = png(src)
|
||||
if not img then
|
||||
return nil, perr
|
||||
end
|
||||
local layer = Types.Layered.layer(a.name or src, img :: Types.Image, x, y)
|
||||
layer.opacity = math.clamp(num(a.opacity, 1), 0, 1)
|
||||
layer.visible = visible
|
||||
layer.blend = blend
|
||||
table.insert(out, 1, layer)
|
||||
elseif ch.name == "stack" then
|
||||
local kids, kerr = stack(ch, x, y)
|
||||
if not kids then
|
||||
return nil, kerr
|
||||
end
|
||||
local g = Types.Layered.group(a.name or "group", kids :: { Types.Layer })
|
||||
g.opacity = math.clamp(num(a.opacity, 1), 0, 1)
|
||||
g.visible = visible
|
||||
g.blend = if a.isolation == "isolate" then (if blend == "normal" then "normal" else blend) else "passThrough"
|
||||
table.insert(out, 1, g)
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
local top = Xml.child(root, "stack")
|
||||
if not top then
|
||||
return nil, Err.new(Err.CORRUPT, "stack.xml has no <stack>")
|
||||
end
|
||||
local layers, lerr = stack(top, 0, 0)
|
||||
if not layers then
|
||||
return nil, lerr
|
||||
end
|
||||
local doc2 = Types.Layered.new(W, H)
|
||||
doc2.layers = layers :: { Types.Layer }
|
||||
if not (opts and opts.composite == false) and zip:find("mergedimage.png") then
|
||||
doc2.meta.composite = png("mergedimage.png")
|
||||
end
|
||||
local xr, yr = tonumber(root.attrs.xres), tonumber(root.attrs.yres)
|
||||
if xr and yr then
|
||||
doc2.meta.dpi = { x = xr, y = yr }
|
||||
end
|
||||
doc2.meta.source = { format = "ora", version = root.attrs.version, layers = count }
|
||||
return doc2, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- nearest-neighbour downscale to fit max x max
|
||||
local function thumbnail(img: Types.Image, max: number): Types.Image
|
||||
local w, h = img.width, img.height
|
||||
local s = math.min(1, max / math.max(w, h))
|
||||
local tw, th = math.max(1, math.floor(w * s)), math.max(1, math.floor(h * s))
|
||||
local t = Types.Image.new(tw, th, "rgba8")
|
||||
for y = 0, th - 1 do
|
||||
local sy = math.min(h - 1, math.floor(y / s))
|
||||
for x = 0, tw - 1 do
|
||||
local sx = math.min(w - 1, math.floor(x / s))
|
||||
buffer.writeu32(t.pixels, (y * tw + x) * 4, buffer.readu32(img.pixels, (sy * w + sx) * 4))
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function Ora.encode(doc: Types.Layered, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(doc) ~= "table" or type(doc.layers) ~= "table" or type(doc.width) ~= "number" or doc.width < 1 or doc.height < 1 then
|
||||
return nil, Err.new(Err.ARG, "needs a Layered document")
|
||||
end
|
||||
local encodeImage = opts and opts.encodeImage
|
||||
if not encodeImage then
|
||||
return nil, Err.new(Err.UNSUPPORTED, "PNG layers need the encodeImage hook")
|
||||
end
|
||||
local W, H = doc.width, doc.height
|
||||
local zw = Zip.writer()
|
||||
zw:add("mimetype", MIME, { method = "store" })
|
||||
local files: { { any } } = {}
|
||||
local n = 0
|
||||
local function png(img: Types.Image, label: string): (buffer?, Err.Err?)
|
||||
if type(img) ~= "table" or img.format ~= "rgba8" then
|
||||
return nil, Err.new(Err.ARG, label .. ": layers need rgba8 images")
|
||||
end
|
||||
local ok, data, err = pcall(encodeImage, img, "png")
|
||||
if not ok or not data then
|
||||
return nil, Err.new(Err.ARG, label .. ": " .. tostring(if ok then err else data))
|
||||
end
|
||||
return data, nil
|
||||
end
|
||||
-- layers bottom to top -> xml children top first
|
||||
local function stack(layers: { Types.Layer }): ({ any }?, Err.Err?)
|
||||
local kids = {}
|
||||
for i = #layers, 1, -1 do
|
||||
local l = layers[i]
|
||||
local attrs: { [string]: string } = {
|
||||
name = l.name or "",
|
||||
opacity = string.format("%.6g", l.opacity or 1),
|
||||
visibility = if l.visible == false then "hidden" else "visible",
|
||||
}
|
||||
if l.children then
|
||||
attrs.isolation = if l.blend == "passThrough" then "auto" else "isolate"
|
||||
if l.blend ~= "passThrough" then
|
||||
attrs["composite-op"] = BACK[l.blend] or l.blend
|
||||
end
|
||||
local sub, serr = stack(l.children)
|
||||
if not sub then
|
||||
return nil, serr
|
||||
end
|
||||
table.insert(kids, Xml.element("stack", attrs, sub))
|
||||
else
|
||||
n += 1
|
||||
local path = string.format("data/layer%03d.png", n)
|
||||
local data, perr = png(l.image, "layer " .. tostring(l.name))
|
||||
if not data then
|
||||
return nil, perr
|
||||
end
|
||||
table.insert(files, { path, data })
|
||||
attrs.src = path
|
||||
attrs.x = tostring(math.floor(l.x or 0))
|
||||
attrs.y = tostring(math.floor(l.y or 0))
|
||||
attrs["composite-op"] = BACK[l.blend or "normal"] or l.blend or "svg:src-over"
|
||||
table.insert(kids, Xml.element("layer", attrs))
|
||||
end
|
||||
end
|
||||
return kids, nil
|
||||
end
|
||||
local kids, kerr = stack(doc.layers)
|
||||
if not kids then
|
||||
return nil, kerr
|
||||
end
|
||||
local rootAttrs: { [string]: string } = { version = "0.0.6", w = tostring(W), h = tostring(H) }
|
||||
local dpi = doc.meta and doc.meta.dpi
|
||||
if dpi then
|
||||
rootAttrs.xres, rootAttrs.yres = tostring(dpi.x), tostring(dpi.y)
|
||||
end
|
||||
local xml = Xml.encode(Xml.document(Xml.element("image", rootAttrs, { Xml.element("stack", {}, kids :: any) })), { indent = 1 })
|
||||
zw:add("stack.xml", xml :: string)
|
||||
for _, f in files do
|
||||
zw:add(f[1], f[2], { method = "store" })
|
||||
end
|
||||
local merged = opts and opts.composite
|
||||
if not merged then
|
||||
local m = Types.Image.new(W, H, "rgba8")
|
||||
Blend.flatten(doc.layers, m.pixels, W, H, false)
|
||||
merged = m
|
||||
end
|
||||
local mdata, merr = png(merged :: Types.Image, "mergedimage")
|
||||
if not mdata then
|
||||
return nil, merr
|
||||
end
|
||||
zw:add("mergedimage.png", mdata, { method = "store" })
|
||||
if not (opts and opts.thumbnail == false) then
|
||||
local tdata, terr = png(thumbnail(merged :: Types.Image, 256), "thumbnail")
|
||||
if not tdata then
|
||||
return nil, terr
|
||||
end
|
||||
zw:add("Thumbnails/thumbnail.png", tdata, { method = "store" })
|
||||
end
|
||||
return zw:finish()
|
||||
end
|
||||
|
||||
return Ora
|
||||
@@ -0,0 +1,124 @@
|
||||
-- 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("@formats/layered/ora")
|
||||
local Png = require("@formats/image/png")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/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()
|
||||
@@ -168,3 +168,26 @@ if which == "icns" then
|
||||
base.pixels = base.frames[4].image.pixels
|
||||
emit("ours_legacy.icns", Icns.encode(base))
|
||||
end
|
||||
|
||||
if which == "ora" then
|
||||
-- a layered document: background, an offset half-opaque multiply layer, a hidden one, a group with a screen layer
|
||||
local Ora = require("@formats/layered/ora")
|
||||
local Png = require("@formats/image/png")
|
||||
local doc = Types.Layered.new(40, 30)
|
||||
local bg = Types.Layered.layer("background", image(40, 30, "rgba8", 0))
|
||||
for i = 0, 40 * 30 - 1 do
|
||||
buffer.writeu8(bg.image.pixels, i * 4 + 3, 255)
|
||||
end
|
||||
local mid = Types.Layered.layer("mid", image(20, 10, "rgba8", 1), 5, 7)
|
||||
mid.opacity, mid.blend = 0.5, "multiply"
|
||||
local hidden = Types.Layered.layer("hidden", image(8, 8, "rgba8", 2), 1, 1)
|
||||
hidden.visible = false
|
||||
local inner = Types.Layered.layer("inner", image(10, 10, "rgba8", 3), 20, 15)
|
||||
inner.blend = "screen"
|
||||
local g = Types.Layered.group("group", { inner })
|
||||
g.opacity = 0.75
|
||||
doc.layers = { bg, mid, hidden, g }
|
||||
emit("ours_doc.ora", Ora.encode(doc, { encodeImage = function(img: any, _f: string): (any, any)
|
||||
return Png.encode(img)
|
||||
end }))
|
||||
end
|
||||
@@ -0,0 +1,95 @@
|
||||
# OpenRaster fixtures (tools/fixtures.py): ORA files assembled here by the specification (python zipfile, the stored
|
||||
# mimetype first, stack.xml, Pillow-written PNG layers, mergedimage.png, a thumbnail): plain layers with offsets /
|
||||
# opacity / hidden / composite-ops, nested stacks (isolate and auto), a file without mergedimage; and our encoder's
|
||||
# file (tools/fixtures/_tex_encode.luau -a ora) taken apart by python (zip order and method, stack.xml, PNG pixels
|
||||
# through Pillow). References: the layer attributes and source pixels written here.
|
||||
# tests/gen/ora.luau { cases = { { name, data, w, h, layers (bottom to top, depth-first) = { { name, x, y, opacity,
|
||||
# visible, blend, depth, rgba?, w?, h? } }, merged? } }, ours = { ok, entries, layers } }
|
||||
import importlib.util, io, os, struct, xml.etree.ElementTree as ET, zipfile
|
||||
|
||||
def _load(n):
|
||||
spec = importlib.util.spec_from_file_location(n, os.path.join(os.path.dirname(os.path.abspath(__file__)), n + ".py"))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m
|
||||
|
||||
R, S, T = _load("_raster_ref"), _load("_simple"), _load("_tex_reference")
|
||||
INPUTS = ["tools/fixtures/_tex_encode.luau", "src/Formats/layered/ora"]
|
||||
OPS = {"svg:src-over": "normal", "svg:multiply": "multiply", "svg:screen": "screen", "svg:overlay": "overlay", "svg:color-dodge": "colorDodge"}
|
||||
|
||||
|
||||
def build(stack_xml, pngs, merged=True, w=40, h=30):
|
||||
buf = io.BytesIO()
|
||||
z = zipfile.ZipFile(buf, "w")
|
||||
z.writestr(zipfile.ZipInfo("mimetype"), "image/openraster", compress_type=zipfile.ZIP_STORED)
|
||||
z.writestr("stack.xml", stack_xml, compress_type=zipfile.ZIP_DEFLATED)
|
||||
for n, d in pngs.items():
|
||||
z.writestr(n, d, compress_type=zipfile.ZIP_STORED)
|
||||
if merged:
|
||||
z.writestr("mergedimage.png", R.png(w, h, bytes(w * h * 4)), compress_type=zipfile.ZIP_STORED)
|
||||
z.close()
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
cases = []
|
||||
imgs = {}
|
||||
for i, (w, h) in enumerate(((40, 30), (20, 10), (8, 8), (10, 12), (6, 5))):
|
||||
imgs["data/l%d.png" % i] = (w, h, R.photo(w, h, i + 1, i > 0))
|
||||
pngs = {k: R.png(*v) for k, v in imgs.items()}
|
||||
|
||||
def lay(src, name, x, y, op, vis, depth):
|
||||
w, h, px = imgs[src]
|
||||
return {"name": name, "x": x, "y": y, "opacity": op[0], "visible": vis, "blend": op[1], "depth": depth, "rgba": px, "w": w, "h": h}
|
||||
|
||||
xml1 = '''<?xml version="1.0" encoding="UTF-8"?>
|
||||
<image version="0.0.6" w="40" h="30" xres="144" yres="144"><stack>
|
||||
<layer name="top hidden" src="data/l2.png" x="1" y="2" visibility="hidden" opacity="1.0"/>
|
||||
<layer name="mid" src="data/l1.png" x="5" y="7" opacity="0.5" composite-op="svg:multiply"/>
|
||||
<layer name="background" src="data/l0.png"/>
|
||||
</stack></image>'''
|
||||
cases.append({"name": "plain", "data": build(xml1, pngs), "w": 40, "h": 30, "dpi": 144, "merged": True, "layers": [
|
||||
lay("data/l0.png", "background", 0, 0, (1.0, "normal"), True, 0), lay("data/l1.png", "mid", 5, 7, (0.5, "multiply"), True, 0),
|
||||
lay("data/l2.png", "top hidden", 1, 2, (1.0, "normal"), False, 0)]})
|
||||
xml2 = '''<image w="40" h="30"><stack>
|
||||
<stack name="outer" isolation="isolate" opacity="0.8" x="2" y="3">
|
||||
<layer name="a" src="data/l3.png" x="1" y="1" composite-op="svg:screen"/>
|
||||
<stack name="inner" opacity="0.5"><layer name="b" src="data/l4.png" composite-op="svg:color-dodge"/></stack>
|
||||
</stack>
|
||||
<layer name="base" src="data/l0.png"/>
|
||||
</stack></image>'''
|
||||
cases.append({"name": "nested", "data": build(xml2, pngs, merged=False), "w": 40, "h": 30, "merged": False, "layers": [
|
||||
lay("data/l0.png", "base", 0, 0, (1.0, "normal"), True, 0),
|
||||
{"name": "outer", "x": 0, "y": 0, "opacity": 0.8, "visible": True, "blend": "normal", "depth": 0, "group": True},
|
||||
{"name": "inner", "x": 0, "y": 0, "opacity": 0.5, "visible": True, "blend": "passThrough", "depth": 1, "group": True},
|
||||
lay("data/l4.png", "b", 2, 3, (1.0, "colorDodge"), True, 2),
|
||||
lay("data/l3.png", "a", 3, 4, (1.0, "screen"), True, 1)]})
|
||||
# ours, taken apart
|
||||
mine = S.ours(ctx, "ora")["ours_doc.ora"]
|
||||
z = zipfile.ZipFile(io.BytesIO(mine))
|
||||
infos = z.infolist()
|
||||
ok = infos[0].filename == "mimetype" and infos[0].compress_type == zipfile.ZIP_STORED and z.read("mimetype") == b"image/openraster"
|
||||
root = ET.fromstring(z.read("stack.xml"))
|
||||
layers = []
|
||||
|
||||
def walk(node, depth):
|
||||
for ch in node:
|
||||
a = ch.attrib
|
||||
e = {"tag": ch.tag, "name": a.get("name", ""), "depth": depth, "opacity": float(a.get("opacity", "1")),
|
||||
"visible": a.get("visibility", "visible") == "visible", "op": a.get("composite-op", ""), "isolation": a.get("isolation", "")}
|
||||
if ch.tag == "layer":
|
||||
e["x"], e["y"] = int(a.get("x", "0")), int(a.get("y", "0"))
|
||||
e["src"] = a["src"]
|
||||
layers.append(e)
|
||||
if ch.tag == "stack":
|
||||
walk(ch, depth + 1)
|
||||
walk(root.find("stack"), 0)
|
||||
srcs = [l["src"] for l in layers if l["tag"] == "layer"] + ["mergedimage.png", "Thumbnails/thumbnail.png"]
|
||||
decoded = T.pillow(ctx, [z.read(s) for s in srcs])
|
||||
px = {s: {"w": d[1], "h": d[2], "rgba": T.rgba_from(d[0], d[3])} for s, d in zip(srcs, decoded)}
|
||||
for l in layers:
|
||||
if l["tag"] == "layer":
|
||||
l.update(px[l["src"]])
|
||||
ours = {"data": mine, "ok": ok, "w": int(root.attrib["w"]), "h": int(root.attrib["h"]), "entries": [i.filename for i in infos],
|
||||
"layers": layers, "merged": px["mergedimage.png"], "thumb": [px["Thumbnails/thumbnail.png"]["w"], px["Thumbnails/thumbnail.png"]["h"]]}
|
||||
ctx.emit("ora", {"cases": cases, "ours": ours})
|
||||
Reference in new issue
Block a user