summaryrefslogtreecommitdiff
path: root/tools/svgdiff.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/svgdiff.py')
-rw-r--r--tools/svgdiff.py116
1 files changed, 116 insertions, 0 deletions
diff --git a/tools/svgdiff.py b/tools/svgdiff.py
new file mode 100644
index 0000000..80e15a9
--- /dev/null
+++ b/tools/svgdiff.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python3
+"""Differential test of the vector renderers against resvg.
+
+Every SVG (and every Typst document, compiled to SVG first) in the corpus is
+rendered by resvg as the reference, then by sketchbook's CPU and GPU
+backends through the real host, headless. Each result is compared with the
+reference, and GPU with CPU; side-by-side images of every case land in the
+output directory.
+
+usage: svgdiff.py SVGVIEW_EXE CORPUS_DIR OUT_DIR
+"""
+import re
+import subprocess
+import sys
+from pathlib import Path
+
+import numpy as np
+from PIL import Image
+
+WIDTH = 480
+BACKGROUND = "#808080"
+# resvg samples coverage on four sub-scanlines, so thin edges come out in
+# quarter steps. Rendering it 4x larger and box-filtering down gives the
+# reference exact-enough coverage to hold both renderers to.
+SUPERSAMPLE = 4
+# Per case: mean absolute channel error, and the share of pixels whose worst
+# channel is off by more than `EDGE` (anti-aliasing differences sit below it).
+MEAN_LIMIT = 1.0
+EDGE = 48
+BAD_LIMIT = 0.005
+
+
+def load(path):
+ return np.asarray(Image.open(path).convert("RGB"), dtype=np.int16)
+
+
+def compare(a, b):
+ if a.shape != b.shape:
+ return float("inf"), 1.0
+ diff = np.abs(a - b)
+ return float(diff.mean()), float((diff.max(axis=2) > EDGE).mean())
+
+
+def strip(img, ref, ours):
+ """Reference | ours | difference, amplified."""
+ diff = np.clip(np.abs(ref - ours) * 4, 0, 255)
+ return Image.fromarray(np.concatenate([ref, ours, diff], axis=1).astype(np.uint8)).save(img)
+
+
+def main():
+ exe, corpus, out = Path(sys.argv[1]).resolve(), Path(sys.argv[2]), Path(sys.argv[3])
+ out.mkdir(parents=True, exist_ok=True)
+ cases = []
+ for source in sorted(corpus.iterdir()):
+ if source.suffix == ".typ":
+ svg = out / (source.stem + ".svg")
+ subprocess.run(["typst", "compile", "--format", "svg", source, svg], check=True)
+ # Sizes in pt make resvg round to whole pixels before scaling,
+ # which shifts its picture by a fraction; give it plain units.
+ text = svg.read_text()
+ box = re.search(r'viewBox="0 0 ([\d.]+) ([\d.]+)"', text)
+ text = re.sub(r'width="[\d.]+pt" height="[\d.]+pt"', f'width="{box[1]}" height="{box[2]}"', text, count=1)
+ svg.write_text(text)
+ elif source.suffix == ".svg":
+ svg = source
+ else:
+ continue
+ ref = out / f"{source.stem}-resvg.png"
+ big = out / f"{source.stem}-resvg-x{SUPERSAMPLE}.png"
+ subprocess.run(["resvg", "-w", str(WIDTH * SUPERSAMPLE), "--background", BACKGROUND, svg, big], check=True)
+ hi = Image.open(big).convert("RGB")
+ # Whole blocks only: a height that is no multiple is padded, not
+ # squeezed, which would shift every edge below by a fraction.
+ w, h = WIDTH, -(-hi.size[1] // SUPERSAMPLE)
+ whole = Image.new("RGB", (w * SUPERSAMPLE, h * SUPERSAMPLE), BACKGROUND)
+ whole.paste(hi, (0, 0))
+ whole.resize((w, h), Image.BOX).save(ref)
+ cases.append((source.stem, svg.resolve(), ref, w, h))
+
+ listing = out / "list.txt"
+ listing.write_text("".join(f"{svg} {w} {h}\n" for _, svg, _, w, h in cases))
+ script = out / "render.script"
+ lines = []
+ for i, (stem, *_rest) in enumerate(cases):
+ for backend in ("gpu", "cpu"):
+ lines += [f"console set index {i}", f"console set backend {backend}", "frames 2",
+ f"screenshot {out.resolve()}/{stem}-{backend}.png"]
+ script.write_text("\n".join(lines) + "\n")
+ subprocess.run([exe, "--headless", "--fresh", "--state", out / "svgview.state", "--script", script],
+ check=True, env={"SVGVIEW_LIST": str(listing.resolve()), **__import__("os").environ})
+
+ failed = []
+ print(f"{'case':<18} {'cpu mean':>9} {'cpu bad':>8} {'gpu mean':>9} {'gpu bad':>8} {'gpu~cpu':>8}")
+ for stem, _svg, ref_path, _w, _h in cases:
+ ref = load(ref_path)
+ row = [stem]
+ results = {}
+ for backend in ("cpu", "gpu"):
+ ours = load(out / f"{stem}-{backend}.png")
+ mean, bad = compare(ref, ours)
+ results[backend] = (mean, bad)
+ strip(out / f"{stem}-{backend}-diff.png", ref, ours)
+ row += [f"{mean:9.3f}", f"{bad * 100:7.2f}%"]
+ if mean > MEAN_LIMIT or bad > BAD_LIMIT:
+ failed.append(f"{stem} ({backend})")
+ mean, _ = compare(load(out / f"{stem}-cpu.png"), load(out / f"{stem}-gpu.png"))
+ row.append(f"{mean:8.3f}")
+ print(f"{row[0]:<18} " + " ".join(row[1:]))
+ print(f"\nlimits: mean < {MEAN_LIMIT}, pixels off by > {EDGE}: < {BAD_LIMIT * 100:.0f}%. Images in {out}")
+ if failed:
+ print("FAILED: " + ", ".join(failed))
+ sys.exit(1)
+
+
+if __name__ == "__main__":
+ main()