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
@@ -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
|
||||
Reference in new issue
Block a user