Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 76 additions & 20 deletions bench/cases/junctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
from __future__ import annotations

import math
import re

import numpy as np

from _common import Case, Check, dist_to_polygon, poly_attr, svg_doc
from _common import INK_TOL, Case, Check, poly_attr, render, svg_doc

A, B, C = "#204080", "#c04030", "#3f8f4f"
SIZE = 128
Expand Down Expand Up @@ -50,14 +51,6 @@ def measure(t):
return measure


def _shape_area(s) -> float:
tot = 0.0
for r in s.rings:
x, y = r[:, 0], r[:, 1]
tot += abs(float(np.dot(x, np.roll(y, -1)) - np.dot(y, np.roll(x, -1)))) / 2
return tot


def _abut(name, what, body, seam_len):
measure = _seam_checks(seam_len)

Expand All @@ -79,9 +72,8 @@ def _junction(name, what, body, seam_len):
def check(t):
gap, bleed = measure(t)
# Every region that meets at the junction owns a corner there. The one whose
# boundary strays furthest is the one that moved the junction.
big = [s for s in t.out_shapes if _shape_area(s) >= 200.0]
worst = _junction_error(t, big)
# visible corner strays furthest is the one that moved the junction.
worst = _junction_error(_window(t.truth_svg), _window(t.out_svg))
return [
Check("junction_err_px", worst, 0.2, "<",
"0.2 px = 2x the output coordinate grid; beyond it the corner is visibly cut"),
Expand All @@ -94,15 +86,79 @@ def check(t):
return Case(name=name, what=what, svg=svg_doc(SIZE, body), check=check)


def _junction_error(t, big) -> float:
"""The furthest any large region's boundary sits from the true junction point."""
if not big:
return float("nan")
q = np.asarray([[JX, JY]], dtype=float)
# The junction is read from a render of the four source pixels about it, at 64 samples
# per pixel: on the whole image at SCALE the nearest sample inside a 120-degree corner can
# sit 0.2 px from its apex, the size of the limit itself.
WIN, WIN_RES = 2.0, 64


def _window(svg: str) -> np.ndarray:
"""The square of side 2*WIN about the true junction, rendered at WIN_RES per pixel.

The document is nested, as drawn on the image's pixel grid, in one whose viewBox is
the window, so the two frames (the truth's grid, inkvec's pixel centres) stay the
document's own business.
"""
n = int(2 * WIN * WIN_RES)
svg = re.sub(r"<\?xml[^>]*\?>", "", svg).strip()
m = re.search(r"<svg\b[^>]*>", svg)
if m is None: # no output: no region reaches the junction
return np.zeros((n, n, 4), dtype=np.float32)
tag = re.sub(r'\s(?:x|y|width|height)="[^"]*"', "", m.group(0))
tag = tag[:-1] + f' x="0" y="0" width="{SIZE}" height="{SIZE}">'
outer = (f'<svg xmlns="http://www.w3.org/2000/svg" width="{n}" height="{n}" '
f'viewBox="{JX - WIN} {JY - WIN} {2 * WIN} {2 * WIN}">'
f'{svg[:m.start()]}{tag}{svg[m.end():]}</svg>')
return render.render(outer, n, n)


def _regions(rgba: np.ndarray) -> list[tuple[int, np.ndarray]]:
"""Each ink's 4-connected areas of its own colour, a quarter pixel or more: (ink, mask).

Read from a render, not from the paths, because a path is not what shows. A face drawn
whole beneath the faces painted over it (its covered part completed) has no corner at
the junction of its own; its visible corner is where its covers' edges meet. Blended
pixels match no ink, so two areas of one colour meeting only at a point stay apart.
"""
from scipy import ndimage

out = []
for k, ink in enumerate((A, B, C)):
rgb = np.asarray([int(ink[i:i + 2], 16) / 255.0 for i in (1, 3, 5)], dtype=np.float32)
near = (np.abs(rgba[..., :3] - rgb).max(-1) <= INK_TOL) & (rgba[..., 3] >= 0.99)
lab, n = ndimage.label(near)
for i in range(1, n + 1):
m = lab == i
if m.sum() >= 0.25 * WIN_RES * WIN_RES:
out.append((k, m))
return out


def _reach(mask: np.ndarray) -> float:
"""How near, in source px, a region's samples come to the true junction."""
ys, xs = np.nonzero(mask)
d2 = ((xs + 0.5) / WIN_RES - WIN) ** 2 + ((ys + 0.5) / WIN_RES - WIN) ** 2
return float(np.sqrt(d2.min()))


def _junction_error(truth: np.ndarray, out: np.ndarray) -> float:
"""The furthest any of the artist's regions, as traced, sits from the true junction.

Each region of the artist's is matched to the traced area of its ink that overlaps it
most, and the truth's own reach (its blended samples about the junction, 0.01-0.03
px) is taken off. A junction moved by d leaves none of the corners further than d
away, and one of three 120-degree corners at least 0.87 d away, or one of four right
angles d away, so this reads the displacement itself: 0.05, 0.1, 0.2 and 0.5 px moves
of the true geometry read 0.06, 0.11, 0.20 and 0.43-0.50.
"""
traced = _regions(out)
worst = 0.0
for s in big:
d = min((float(dist_to_polygon(q, r)[0]) for r in s.rings), default=float("inf"))
worst = max(worst, d)
for k, m in _regions(truth):
mine = [o for j, o in traced if j == k]
best = max(mine, key=lambda o: int((o & m).sum()), default=None)
if best is None or not (best & m).any():
return float("inf")
worst = max(worst, _reach(best) - _reach(m))
return worst


Expand Down
48 changes: 48 additions & 0 deletions bindings/contract/cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"x86_64-linux-gnu": {
"bytes": 8349,
"sha256": "ef4e226361037eac0a1763f8610774ff0658d30b58d207473c291ce5470e5c46"
},
"x86_64-windows-msvc": {
"bytes": 8349,
"sha256": "4a7c8eef1de25dcdd693f56d016ff302a0d3e447e552c9b95a27025e1110e8df"
}
}
},
Expand All @@ -34,6 +38,10 @@
"x86_64-linux-gnu": {
"bytes": 8349,
"sha256": "ef4e226361037eac0a1763f8610774ff0658d30b58d207473c291ce5470e5c46"
},
"x86_64-windows-msvc": {
"bytes": 8349,
"sha256": "4a7c8eef1de25dcdd693f56d016ff302a0d3e447e552c9b95a27025e1110e8df"
}
}
},
Expand All @@ -55,6 +63,10 @@
"x86_64-linux-gnu": {
"bytes": 5647,
"sha256": "972e2f0a364778e775e62c4c87da0669cfecdbabfd227334a92ace53f0bc8800"
},
"x86_64-windows-msvc": {
"bytes": 5656,
"sha256": "5fef039bbcf98b3bc3c71510496f231bbd2caf5e730cab25808e39f41f35788e"
}
}
},
Expand All @@ -73,6 +85,10 @@
"x86_64-linux-gnu": {
"bytes": 8369,
"sha256": "89d5100eb55c47c07c134d19ef241a4bec81a0bdfd6478a15029606388b11032"
},
"x86_64-windows-msvc": {
"bytes": 8369,
"sha256": "802210f58565e985f7bafc615dba72fc221e22a431f0c79366e98fbdfb9b8cea"
}
}
},
Expand All @@ -91,6 +107,10 @@
"x86_64-linux-gnu": {
"bytes": 2964,
"sha256": "beb7db2b1ddcd9a068a51715056f526fa31cf4bf7c1d1a621975c4c8779feec2"
},
"x86_64-windows-msvc": {
"bytes": 2964,
"sha256": "31174376bc8deecf23dc22b4fce0b323246607b31e45161ec8217831a706ff0f"
}
}
},
Expand All @@ -112,6 +132,10 @@
"x86_64-linux-gnu": {
"bytes": 2964,
"sha256": "beb7db2b1ddcd9a068a51715056f526fa31cf4bf7c1d1a621975c4c8779feec2"
},
"x86_64-windows-msvc": {
"bytes": 2964,
"sha256": "31174376bc8deecf23dc22b4fce0b323246607b31e45161ec8217831a706ff0f"
}
}
},
Expand All @@ -131,6 +155,10 @@
"x86_64-linux-gnu": {
"bytes": 2980,
"sha256": "f72515edc7f187930b6d3cc95f03657ed4270959fc32cbcb09cfafbc667d1c06"
},
"x86_64-windows-msvc": {
"bytes": 2980,
"sha256": "848ba0fe63e56871f84d0e668925eaaada13f214d1390c4cebdad4a5a539a9ed"
}
}
},
Expand All @@ -147,6 +175,10 @@
"x86_64-linux-gnu": {
"bytes": 682,
"sha256": "76fe770f9a7610175a9ecf953c02f918e8d22e75617d8b77674efcefe85d0f58"
},
"x86_64-windows-msvc": {
"bytes": 682,
"sha256": "76fe770f9a7610175a9ecf953c02f918e8d22e75617d8b77674efcefe85d0f58"
}
}
},
Expand All @@ -166,6 +198,10 @@
"x86_64-linux-gnu": {
"bytes": 682,
"sha256": "76fe770f9a7610175a9ecf953c02f918e8d22e75617d8b77674efcefe85d0f58"
},
"x86_64-windows-msvc": {
"bytes": 682,
"sha256": "76fe770f9a7610175a9ecf953c02f918e8d22e75617d8b77674efcefe85d0f58"
}
}
},
Expand All @@ -184,6 +220,10 @@
"x86_64-linux-gnu": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
},
"x86_64-windows-msvc": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
}
}
},
Expand All @@ -203,6 +243,10 @@
"x86_64-linux-gnu": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
},
"x86_64-windows-msvc": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
}
}
},
Expand All @@ -225,6 +269,10 @@
"x86_64-linux-gnu": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
},
"x86_64-windows-msvc": {
"bytes": 682,
"sha256": "9fee7810aadc6f32ede16fc0f5c32ceeb1b6ef63cda375d40d9e34673eec713e"
}
}
},
Expand Down
16 changes: 8 additions & 8 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ <h1>Exact vectors from your logo, in your browser.</h1>
</section>

<section class="sec" id="new">
<h2>What's new in 0.2.0</h2>
<p class="lede">The Studio grew from a tracer with a report into an editor for the whole job, and it now runs in the browser as well as on the desktop.</p>
<h2>What's new in 0.3.0</h2>
<p class="lede">Traces come out closer to what the artist drew, and much closer on the resized JPEGs and screenshots logos usually arrive as. Against 0.2.7 on the 246 icons of the regression gate, every gated measure is better or level, in every mode and condition.</p>
<div class="newgrid">
<div class="new"><span class="tag">Palette</span><h3>Colour groups, gradients included</h3><p>Drag one ink onto another and they are drawn as one: the shapes between them join instead of keeping a traced seam. A group can become a chosen colour or one of its members, and a gradient member is extended over the whole group. Suggestions for near-duplicate inks are offered, never applied unasked.</p></div>
<div class="new"><span class="tag">Opening an image</span><h3>Auto, or a short Custom wizard</h3><p>Every image is traced at once. A card offers to keep that, or to walk through what the image is, clean-up, colours, detail and shape, with a live preview of each choice beside Auto's trace.</p></div>
<div class="new"><span class="tag">Fabricate</span><h3>Ready for cutters and lasers</h3><p>Layered vinyl with registration marks, inlay, print-then-cut stickers, stencils with bridges, and single-line paths for pens, scoring blades and lasers, in millimetres. A preflight shows what will not cut before anything is cut. SVG, DXF with true arcs, and GRBL G-code.</p></div>
<div class="new"><span class="tag">Honesty</span><h3>Certainty bands</h3><p>Where each traced boundary could be, measured from the pixels, drawn over the trace: sure, soft and unsure edges at a glance. On a clean 128-px emoji the median edge uncertainty is 0.09 px; the same image as a JPEG at quality 20 reads 0.50 px.</p></div>
<div class="new"><span class="tag">Transparency</span><h3>Clear ground stays clear</h3><p>Transparent images are traced as they are, and the clear ground no longer takes a slot from a colour cap: on ten emoji at four colours, mean dE00 went from 0.457 to 0.186. Side-by-side fills no longer let a hairline of background through.</p></div>
<div class="new"><span class="tag">Speed</span><h3>Faster, with identical output</h3><p>The curve fit and the ring repair run in parallel blocks, the palette and the boundary solve do only the work they read, and the Studio measures in parallel: on large images the slowest stages take a third to 70% less time, every change checked byte for byte against the build before.</p></div>
<div class="new"><span class="tag">Fidelity</span><h3>Closer to the artist's file</h3><p>Quality's colour difference from the artist's own file (dE00) is 12.5% lower at 128 px, 12.1% at 512 px and 15.2% at 512 px on an opaque ground; Fast's is 9 to 12% lower. Quality also writes fewer parameters: 1.32 per artist parameter at 512 px opaque, down from 1.96.</p></div>
<div class="new"><span class="tag">Noisy input</span><h3>JPEGs and screenshots</h3><p>A JPEG's colour bleeds two or three pixels across every edge. Those halos now go back to the inks they lie between instead of becoming shapes of their own. On icons resized and saved at quality 80, Quality's dE00 is 10.4% lower and Fast writes 4.13 parameters per artist parameter, down from 7.29.</p></div>
<div class="new"><span class="tag">Shapes</span><h3>Whole shapes, under what covers them</h3><p>A shape partly hidden by another used to come back as the sliver that shows. Quality now draws it whole: the simplest shape that leaves every visible pixel as it was. A letter's counter is cut out of its glyph instead of painted over it, and each fill is one compound path.</p></div>
<div class="new"><span class="tag">Strokes</span><h3>Strokes by default</h3><p>Where a shape is a centreline and one width, Quality writes it as a stroked path whenever that is the shorter description at as good a fit. It had been opt-in; it now costs about a sixth more time on a 512 px trace.</p></div>
<div class="new"><span class="tag">Editing</span><h3>Nodes where a designer puts them</h3><p>Nodes sit at a curve's horizontal and vertical extremes, with the handles on the axis, where the fit prefers them. With editability on, a circular arc becomes cubics cut at its extremes and the joins are smooth, the way drawing tools write curves.</p></div>
<div class="new"><span class="tag">Proofs</span><h3>Proved, then checked</h3><p>A pixel's coverage is an area, so a column of pixels gives a boundary's exact mean height, whatever its slope. That theory is machine-checked in Lean, and results the theorems speak about are accepted only by checkers generated from the proofs.</p></div>
</div>
<p class="note">Full list, with the measurements behind each figure: the <a href="https://github.com/logolabs/inkvec/blob/main/CHANGELOG.md" target="_blank" rel="noopener noreferrer">changelog</a>.</p>
</section>
Expand Down
Loading