Skip to content

Avoid redundant work in curve and stroke fitting - #41

Open
Dikluwe wants to merge 1 commit into
logolabs:mainfrom
Dikluwe:codex/preserve-camera-contours
Open

Dikluwe wants to merge 1 commit into
logolabs:mainfrom
Dikluwe:codex/preserve-camera-contours

Conversation

@Dikluwe

@Dikluwe Dikluwe commented Nov 20, 2026 •

Copy link
Copy Markdown

Changes

Reduce CPU work in curve and stroke fitting by reusing tangent measurements and identical chain fits, refining only the winning closed cut, computing boundary cost only for viable primitive candidates, and skipping stroke fitting when the outline score is non-finite.

Validation

  • Compared the base (0633985) and this patch (3b483c2) across the full Inkvec gate on Linux x86_64. All 1,968 SVG pairs (246 icons × 8 conditions) were byte-for-byte identical, with zero failures.
  • 760 Rust library tests passed across inkvec-cli, inkvec-fit, and inkvec-trace (8 ignored).
  • On the Flower 2 Lanczos reproducer with --lossy auto, median single-threaded CPU time fell from 2.48 s to 0.59 s across five paired runs.

@Dikluwe
Dikluwe marked this pull request as ready for review November 20, 2026 16:49
@Stefatorus Stefatorus self-assigned this Nov 21, 2026
@Stefatorus Stefatorus added bug Something isn't working enhancement New feature or request labels Nov 21, 2026
@Stefatorus

Stefatorus commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

On it. I'm also trying to see if we can reproduce this issue for later and potentially improve the test suite to catch it in the future. Additionally, I want to see if i can do additional improvements to your solution.

@Stefatorus

Copy link
Copy Markdown
Member

Thanks for the detailed write-up, benchmarks, and reproducer files (helped a lot with debugging on my end).

It seems to me that the distortion and slowdown in these examples come from Lanczos ringing in the input PNGs (lanczos has negative lobes).

The issue happens due to the following chain:

  1. Because Lanczos has negative lobes, downsampling creates a 1–2 px dark undershoot rim around every edge (#0e2a4c / [14, 42, 76] at 256 px — 13 levels darker than the actual #1b3555 / [27, 53, 85] ink, with 1,683 pixels darker than the ink; #0d284a at 128 px).
  2. Since the file is a PNG (--lossy auto → lossless) and ringing_score probes the 3–7 px band for 8×8 DCT ringing, the noise guard stays off (ringing=0.0000, edge_width=1.00)
  3. Because that dark rim sits outside the #1b3555–#ffffff convex blend segment (blend=false), has OKLab distance 0.048 > 0.035, and covers 1.48% > 0.4% of the image, the palette extractor admits #0e2a4c as a 3rd fake ink. That shatters a 2-colour / 5-face icon into 134 faces (camera-256) and 257 faces (flower-2-256), which is both what blows up outline_chi2 (making the distorted miter stroke win) and what makes flower-2 slow.

In short, lanczos is treated baddly when --lossy mode is auto on Lanczos PNGs.

For comparison:

  1. On a standard antialiased opaque render of Lucide camera (no Lanczos ringing) with your exact flags (--mode quality --restore off --sr off), Inkvec already produces 2 colours, 5 faces, ~1.1 KB (clean + strokes, MAE ~0.11–0.14).
  2. On your exact Lanczos PNGs, passing --lossy on (which enables the noise-aware intake for resampled/rung PNGs) yields lower MAE results than the ones proposed in your commits.
pr41_comparison

However, the bit-exact CPU optimizations in this PR are genuinely clean and useful! I'd be happy to merge a trimmed version of this PR (or a fresh PR) containing just those:

Keep (all bit-exact, no snapshot changes):

  • Commit 56a011b - precomputing tangent lengths/angles in SpanScorer (candidates.rs, candidates/bounded.rs, multimodel/scan.rs, tangents.rs).
  • Deferring refine() in solve_closed to the winning cut only (crates/inkvec-fit/src/multimodel.rs).
  • ChainFits memoization and moving boundary_cost inside the if let Some(...) branch (crates/inkvec-trace/src/ribbon.rs).
  • Skipping !chi2_outline.is_finite() before calling fit_face (crates/inkvec-cli/src/ribbons.rs).

Drop from this PR:

  • despeckle_after_blend (crates/inkvec-trace/src/regions.rs, lib.rs, and bindings/contract/cases.json).
  • BudgetLimitedMisfit heuristic pruning (crates/inkvec-trace/src/ribbon.rs).
  • Commit e3d3d44 — the lossless-only miter guard (crates/inkvec-cli/src/ribbons.rs).

If you're interested in handling 1 to 2 px Lanczos/sinc undershoot halos automatically without --lossy on, feel free to open a separate issue (that fix belongs upstream in intake/palette overshoot rejection rather than in post-blend despeckle and ribbon gating)

Let me know if you'd like to strip this PR down to the bit-exact changes (or open a new PR for them)! I can also help with integrating the needed PR work, if you need anything from me.

@Dikluwe

@Dikluwe
Dikluwe force-pushed the codex/preserve-camera-contours branch from e3d3d44 to 3b483c2 Compare November 21, 2026 17:53
@Dikluwe Dikluwe changed the title Preserve camera contours and reduce curve-fitting CPU cost Avoid redundant work in curve and stroke fitting Nov 21, 2026

Dikluwe commented Nov 21, 2026

Copy link
Copy Markdown
Author

@Stefatorus Thanks for tracing the issue to Lanczos ringing. I trimmed this PR to the CPU optimizations you listed. The updated head is 3b483c2.

I compared it against 0633985 on Linux x86_64 using the full gate’s 246 icons and all eight Quality and Fast conditions. All 1,968 SVG pairs were byte-for-byte identical, with zero failures.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants