Files
rosetta/tools/fixtures/ora.py
T
4DBug d618795ce7 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.
2026-10-31 15:12:53 -05:00

96 lines
5.5 KiB
Python

# 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})