diff --git a/CHANGELOG.md b/CHANGELOG.md index fef556e2..fd160311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,21 @@ API in particular should be treated as unstable release to release). ## [Unreleased] +### Fixed + +- **Thin strokes on a transparent ground keep their ink (Quality).** A stroke narrower than about two pixels covers few or no pixels completely, so the palette met its anti-aliasing before its ink, or never met the ink at all: + - A 1-1.5 px line split across two rows (every pixel translucent) was dropped entirely. Such a colour, at least half opaque and refused only for having no interior, is now read as a hairline of its opaque version. + - A 2 px diagonal came out as a translucent band around opaque specks, because its 0.83-covered pixels outnumbered its core and were accepted first. After the walk, a translucent ink whose opaque version is also an ink is weighed again as a blend of the two and dropped when it is coverage; a translucent wash with an interior of its own stays. + - A thin face of an opaque ink whose pixels are all partial coverage of the ground is painted its ink, not the ink diluted towards white, so the boundary solve places its edges by coverage instead of widening it. + - `bench/cases` `sawtooth_diag` and `ribbon_w1.5` pass. On all 1386 icons of `bench/alpha_eval.py` 90 traces change and every family's mean error is level or lower (dark ground −0.45 % overall; simple-icons −0.79 %, openmoji −0.45 %, noto-emoji −0.23 %, twemoji −0.20 %). Fast output and the speed of both modes are unchanged. +- **A line too thin to cover a pixel keeps both of its edges (all modes).** Where coverage across a boundary rises from the ground and falls back without reaching 1, the edge was read at the profile's 0.5 crossing, which for such a line is its centre line: a 1 px line between two rows came out half as wide. The edges are now the line's centre (the coverage centroid) plus or minus half its width (the coverage integrated along the normal). Regression gate: dE00 −1.6 % (Quality) and −0.6 % (Fast) at 128 px, level at 512 px. `ribbon_w1` passes. +- **Symmetry the drawing does not have is no longer imposed (all modes).** The label map can be symmetric at pixel resolution while the drawing is not, such as four colours meeting 0.27 px off the image centre. Each symmetry is now enforced only when the refined boundary confirms it: the 90th percentile of the paired points' disagreement must be within 3 standard deviations of their measured positions. Genuine mirrors measure 0.3-1.2 and contradicted ones 7.6-20. In Fast this drops four misleading symmetries out of 84 on a 500-trace sample, improving twemoji 1f7eb 4x and a flag 1.8x. +- **Go: integer remainders are right on arm64 (Apple silicon, Linux arm64).** wazero's arm64 compiler (v1.12.0, the latest) defines a remainder's register twice, a division and then a multiply-subtract, and when register pressure spills it in between, the multiply-subtract reads a quotient that was never stored. Any of the module's 346 remainders could come out wrong; in this release's symmetry pass one became an index of 4294337216 into a 256-point boundary and the trace stopped (the 0.2.7 crash the first half-turn attempt hit was the same bug). The build now rewrites every remainder as `a - div(a, b) * b` (`tools/wasm_wazero_fixes.py`, formerly `wasm_float_select.py`), exactly: same results and same traps on 1323 edge cases, both architectures. `TestWazeroRemainder` reports whether wazero still has the bug (a 40-value module where `1000 rem_u 256` returns 1000). + +### Added + +- **Half-turn symmetry.** A drawing that is its own 180° rotation about the image centre, and no mirror image, now comes back exactly so (`bench/cases` `mirror_r`, Quality and Fast), through the same pairing, confirmation and averaging as the mirrors. + ## [0.2.7] - 2026-10-08 ### Fixed diff --git a/HANDOFF.md b/HANDOFF.md index 74a310e5..5924d15f 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -37,7 +37,7 @@ Why the half-turn was reverted rather than fixed: * With it, the Go package's test on macOS (wazero, arm64) panicked in `symmetry::enforce` with an index of 2147484096 into a 256-point edge. The same input traces identically before and after natively, so this is the runtime miscompiling the new code shape, like the float - `select` bug `tools/wasm_float_select.py` works around on amd64. The revert restores the + `select` bug `tools/wasm_wazero_fixes.py` works around on amd64. The revert restores the code that passed there. ## Tests @@ -73,10 +73,24 @@ Made to run on every CI platform: `bench/quality_budget.json`: `cases:quality` 38 (37 + `glyph_ring_bar`), `cases:fast` 26 (unchanged). The coverage floors raised in `b267edf` and `test_functions` 1088 stand. -## Open - -* `mirror_r`: half-turn symmetry, validated for every mirror against the boundary - uncertainty rather than with a fixed cut-off, and checked under wazero on arm64. -* `sawtooth_diag`: a fix local to thin diagonal strokes, not a global palette reordering. -* `ribbon_w1`, `ribbon_w1.5`: off-grid hairlines on a transparent ground (Quality), which - have no interior at all; Fast keeps them through its paired-pixel scan. +## Since: the open cases closed + +On `main` after this handoff, every `bench/cases` case passes in Quality (42/42) and Fast +gains `mirror_r` (27/42): + +* `sawtooth_diag`, `ribbon_w1.5`: the native palette reads a hairline as its opaque ink and + drops a translucent ink that is only the coverage of its opaque twin + (`native/palette.rs`), and a thin face of coverage is painted its ink (`native.rs`). +* `ribbon_w1`: the sub-pixel refinement reads a stroke too thin to cover a pixel from its + coverage centroid and width (`planar::ridge_offset`). +* `mirror_r`: the half turn is a symmetry, and every symmetry is enforced only when the + refined boundary confirms it (`symmetry::AGREEMENT_Z`) -- which is what had made the + first half-turn attempt pull `junction_quad`'s junction onto the centre. +* The Go package's macOS crash (above) came back with this code, and is now understood: wazero's + arm64 compiler can compute an integer remainder from a quotient it never stored, which any + remainder in the module could hit, not only this code shape. The build rewrites every + remainder as a division (`tools/wasm_wazero_fixes.py` has the mechanism), and + `TestWazeroRemainder` reports when wazero fixes it. Reproduced and checked on linux/arm64 + under QEMU, which runs the same wazero backend as macOS. + +See `CHANGELOG.md` `[Unreleased]` for the measurements. diff --git a/bench/quality_budget.json b/bench/quality_budget.json index 9687e63e..4bfe4ab3 100644 --- a/bench/quality_budget.json +++ b/bench/quality_budget.json @@ -15,6 +15,7 @@ "junction_quad", "junction_tri", "mirror_h", + "mirror_r", "mirror_v", "real_single_ink", "ribbon_w1", @@ -54,13 +55,17 @@ "junction_quad", "junction_tri", "mirror_h", + "mirror_r", "mirror_v", "real_glyph_counters", "real_single_ink", "real_symmetry", + "ribbon_w1", + "ribbon_w1.5", "ribbon_w2", "ribbon_w3", "rings_concentric", + "sawtooth_diag", "square_r1", "square_r1.5", "square_r2", @@ -97,7 +102,7 @@ "mutation:inkvec-trace/gradient": 67, "mutation:inkvec-trace/native": 68, "rustfmt_hunks": 0, - "test_functions": 1088, + "test_functions": 1096, "untested_modules": [ "crates/inkvec-cli/src/alpha.rs", "crates/inkvec-cli/src/rings.rs", diff --git a/bindings/contract/cases.json b/bindings/contract/cases.json index 776fad57..76c4ff22 100644 --- a/bindings/contract/cases.json +++ b/bindings/contract/cases.json @@ -12,13 +12,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 8930, - "sha256": "61d21e85eb7243faa863d8203ed3effc00dfcdc5651983f8fb6f65d231839327" - }, "x86_64-linux-gnu": { - "bytes": 8942, - "sha256": "29616e966673e2c029037a872df8d74bae22c4d6c8a9b1578d5e7377309abe2b" + "bytes": 8826, + "sha256": "b605367c0ca15ff23f0be08e4ecc5598b8c5e176429c32e597c65fd2428d5c6f" + }, + "x86_64-windows-msvc": { + "bytes": 8826, + "sha256": "aaac3e48856448edc19c682a6b5c3544ecc5f704fa3c0c7b331a7f959ae1c749" } } }, @@ -35,13 +35,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 8930, - "sha256": "61d21e85eb7243faa863d8203ed3effc00dfcdc5651983f8fb6f65d231839327" - }, "x86_64-linux-gnu": { - "bytes": 8942, - "sha256": "29616e966673e2c029037a872df8d74bae22c4d6c8a9b1578d5e7377309abe2b" + "bytes": 8826, + "sha256": "b605367c0ca15ff23f0be08e4ecc5598b8c5e176429c32e597c65fd2428d5c6f" + }, + "x86_64-windows-msvc": { + "bytes": 8826, + "sha256": "aaac3e48856448edc19c682a6b5c3544ecc5f704fa3c0c7b331a7f959ae1c749" } } }, @@ -60,13 +60,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 5866, - "sha256": "34b0959c6bb45accc217e0cc1572c499b23c26db303026464a2acd657f23eabb" - }, "x86_64-linux-gnu": { - "bytes": 5870, - "sha256": "1f97c22656c57bb6dca084184ea2eb63fea8e69945b3acead58aa88aae8385df" + "bytes": 5790, + "sha256": "b6d5c649cee3b9f0d781bc508e457a560bcf55a2b4706656806f21acb5d760e0" + }, + "x86_64-windows-msvc": { + "bytes": 5782, + "sha256": "ea544ec4bda9f2c0ff71c6711164c9b88e1dcd3617838f79a58872c157ddd9cd" } } }, @@ -82,13 +82,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 8950, - "sha256": "d6cad56a7fc9eebcba5ade0124afbf89f08fd59edda98cc233d0359e76a58887" - }, "x86_64-linux-gnu": { - "bytes": 8962, - "sha256": "4277f433de48b8f09703601c9d115ec5dbf5ab5560576fd5e2986ec4c11b07b9" + "bytes": 8846, + "sha256": "6677a9b3711698a52f2cd8d9a892505c595f3f1da32f7a6abb533f6b9470c48b" + }, + "x86_64-windows-msvc": { + "bytes": 8846, + "sha256": "e558048b2aac2f534e296f307b8bc2c8cf48093f40da9f38a04a5eccabd786b6" } } }, @@ -104,13 +104,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 3104, - "sha256": "641dc56a5d8ec53390025675eabd61a919878678d28a4fe69a48a470c0e1f754" - }, "x86_64-linux-gnu": { - "bytes": 3103, - "sha256": "6a2eaa498825ea05c5f50f6bcae54a0aa2d478cc754b1ce2c1c9dfffa9139533" + "bytes": 3303, + "sha256": "61f4dd2b28791294991b26464f896af1ec5a8df0db13adb790833af3b364757f" + }, + "x86_64-windows-msvc": { + "bytes": 3303, + "sha256": "1206b5a7806c272325cdf86d6c7de9c2ba1f7d4a14dac9a5d6dee21fc782943d" } } }, @@ -129,13 +129,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 3104, - "sha256": "641dc56a5d8ec53390025675eabd61a919878678d28a4fe69a48a470c0e1f754" - }, "x86_64-linux-gnu": { - "bytes": 3103, - "sha256": "6a2eaa498825ea05c5f50f6bcae54a0aa2d478cc754b1ce2c1c9dfffa9139533" + "bytes": 3303, + "sha256": "61f4dd2b28791294991b26464f896af1ec5a8df0db13adb790833af3b364757f" + }, + "x86_64-windows-msvc": { + "bytes": 3303, + "sha256": "1206b5a7806c272325cdf86d6c7de9c2ba1f7d4a14dac9a5d6dee21fc782943d" } } }, @@ -152,13 +152,13 @@ "height": 96 }, "svg": { - "x86_64-windows-msvc": { - "bytes": 3120, - "sha256": "6d8b8f685f719c4c9e55c6d8b0b14200e0d05fd56766f162cc8d8caaef4ac1be" - }, "x86_64-linux-gnu": { - "bytes": 3119, - "sha256": "8a91cd20f350687eb32c3f4ffe7365044278be3c0eacf18479abea099865918a" + "bytes": 3319, + "sha256": "1fd4806b93c420c56098142da56f9f8a01fc694964bc30237742ef5b0c330e9c" + }, + "x86_64-windows-msvc": { + "bytes": 3319, + "sha256": "62952a5875fb97291e617fa027dc2ac8f5b37ef8669f8aff3af89244fcfbc222" } } }, @@ -172,11 +172,11 @@ "height": 64 }, "svg": { - "x86_64-windows-msvc": { + "x86_64-linux-gnu": { "bytes": 679, "sha256": "cbaf89d8dea3b3715e88d954dac5f99b8d52006cbffc236758a7a91a65f9112b" }, - "x86_64-linux-gnu": { + "x86_64-windows-msvc": { "bytes": 679, "sha256": "cbaf89d8dea3b3715e88d954dac5f99b8d52006cbffc236758a7a91a65f9112b" } @@ -195,11 +195,11 @@ "height": 64 }, "svg": { - "x86_64-windows-msvc": { + "x86_64-linux-gnu": { "bytes": 679, "sha256": "cbaf89d8dea3b3715e88d954dac5f99b8d52006cbffc236758a7a91a65f9112b" }, - "x86_64-linux-gnu": { + "x86_64-windows-msvc": { "bytes": 679, "sha256": "cbaf89d8dea3b3715e88d954dac5f99b8d52006cbffc236758a7a91a65f9112b" } @@ -217,11 +217,11 @@ "height": 64 }, "svg": { - "x86_64-windows-msvc": { + "x86_64-linux-gnu": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" }, - "x86_64-linux-gnu": { + "x86_64-windows-msvc": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" } @@ -240,11 +240,11 @@ "height": 64 }, "svg": { - "x86_64-windows-msvc": { + "x86_64-linux-gnu": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" }, - "x86_64-linux-gnu": { + "x86_64-windows-msvc": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" } @@ -266,11 +266,11 @@ "height": 64 }, "svg": { - "x86_64-windows-msvc": { + "x86_64-linux-gnu": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" }, - "x86_64-linux-gnu": { + "x86_64-windows-msvc": { "bytes": 760, "sha256": "79f85e2ec9adb8b2ea9ba1ab39d108c8872d76e49bf7e7c8c3416fe493dfeb76" } diff --git a/crates/inkvec-cli/src/mirror_fit.rs b/crates/inkvec-cli/src/mirror_fit.rs index fa4dded7..f1e59276 100644 --- a/crates/inkvec-cli/src/mirror_fit.rs +++ b/crates/inkvec-cli/src/mirror_fit.rs @@ -112,6 +112,9 @@ pub(crate) fn fit( cfg: &FitConfig, fit_plain: &dyn Fn(&Polyline) -> FittedPath, ) -> Option { + // A half turn has no axis to fit one side of; it reaches the fit through the pairing. + let mirrors: Vec = mirrors.iter().copied().filter(|m| m.is_axis()).collect(); + let mirrors = mirrors.as_slice(); let &m = mirrors.first()?; if poly.points.len() < MIN_POINTS { return None; @@ -220,6 +223,8 @@ pub(crate) fn choose( cfg: &FitConfig, fit_plain: &dyn Fn(&Polyline) -> FittedPath, ) -> FittedPath { + let mirrors: Vec = mirrors.iter().copied().filter(|m| m.is_axis()).collect(); + let mirrors = mirrors.as_slice(); let plain = fit_plain(poly); if mirrors.is_empty() || mirrors.iter().all(|&m| anchors_mirror(&plain, m)) { return plain; @@ -250,6 +255,7 @@ fn anchors_mirror(path: &FittedPath, m: Mirror) -> bool { fn axis(m: Mirror) -> f64 { match m { Mirror::V(k) | Mirror::H(k) => k as f64 * 0.5, + Mirror::Rot180(..) => unreachable!("`fit` and `choose` keep the axis mirrors only"), } } @@ -258,6 +264,7 @@ fn onto_axis(p: Point, m: Mirror) -> Point { match m { Mirror::V(_) => Point::new(axis(m), p.y), Mirror::H(_) => Point::new(p.x, axis(m)), + Mirror::Rot180(..) => unreachable!("`fit` and `choose` keep the axis mirrors only"), } } @@ -574,6 +581,7 @@ fn snap_cubic(part: &FittedPath, m: Mirror, at_end: bool) -> Option // The normal to a vertical axis is horizontal: the offset across is in x, along in y. Mirror::V(_) => (ctrl.x - cross.x, ctrl.y - cross.y), Mirror::H(_) => (ctrl.y - cross.y, ctrl.x - cross.x), + Mirror::Rot180(..) => return None, }; if dn.abs() <= 1e-12 || da.atan2(dn.abs()).abs().to_degrees() > SNAP_DEGREES { return None; @@ -581,6 +589,7 @@ fn snap_cubic(part: &FittedPath, m: Mirror, at_end: bool) -> Option let on = match m { Mirror::V(_) => Point::new(ctrl.x, cross.y), Mirror::H(_) => Point::new(cross.x, ctrl.y), + Mirror::Rot180(..) => return None, }; let mut out = part.clone(); out.segments[idx] = if at_end { diff --git a/crates/inkvec-trace/src/color/snap.rs b/crates/inkvec-trace/src/color/snap.rs index 321a9392..4f64a7d0 100644 --- a/crates/inkvec-trace/src/color/snap.rs +++ b/crates/inkvec-trace/src/color/snap.rs @@ -87,7 +87,7 @@ pub(crate) const REP_DE00: f32 = super::SAME_INK_DE00; /// /// `faces` is the face map, one id per pixel, row-major `w × h`; ids at or above `n_faces` /// are ignored. One pass, O(w·h). An image narrower or shorter than 3 px has no interior. -fn interior_counts(faces: &[u16], w: usize, h: usize, n_faces: usize) -> Vec { +pub(crate) fn interior_counts(faces: &[u16], w: usize, h: usize, n_faces: usize) -> Vec { let mut count = vec![0u32; n_faces]; if w < 3 || h < 3 || faces.len() < w * h { return count; diff --git a/crates/inkvec-trace/src/lib.rs b/crates/inkvec-trace/src/lib.rs index 80d0b0f7..d3a3c74f 100644 --- a/crates/inkvec-trace/src/lib.rs +++ b/crates/inkvec-trace/src/lib.rs @@ -1148,7 +1148,7 @@ pub(crate) fn finish_color_trace_alpha( }; // A small map is refined on this thread, after detection, as before: handing it to the // pool costs more than it saves (see `planar::refine_in_parallel`). - let (sym, refined) = if planar::refine_in_parallel(&map) { + let (mut sym, refined) = if planar::refine_in_parallel(&map) { rayon::join(|| symmetry::detect(&map, &labels, &face_color), measure) } else { (symmetry::detect(&map, &labels, &face_color), measure()) @@ -1195,7 +1195,7 @@ pub(crate) fn finish_color_trace_alpha( let symmetrised = if sym.is_empty() { 0 } else { - symmetry::enforce(&mut map, &sym) + symmetry::enforce(&mut map, &mut sym) }; sw.mark("symmetry"); ColorTrace { diff --git a/crates/inkvec-trace/src/native.rs b/crates/inkvec-trace/src/native.rs index 7ff70ab2..35b81f9b 100644 --- a/crates/inkvec-trace/src/native.rs +++ b/crates/inkvec-trace/src/native.rs @@ -804,6 +804,74 @@ fn name_carved_paint( } } +/// Share of a thin face's pixels that must read as coverage of its ink for the face to be +/// painted that ink ([`paint_thin_faces_their_ink`]). +const THIN_COVERAGE_SHARE: f32 = 0.9; + +/// Paint each flat face of an opaque ink that owns no interior pixel its ink's colour, when +/// its pixels are that ink partly covering the clear ground. Returns how many changed. +/// +/// A face with no interior pixel is all boundary: every pixel it owns is partly covered. Its +/// fill, fitted over white from those pixels, is then the ink mixed with whatever shares +/// them, and against the clear ground that is white: a 1 px line of `#204080` between two +/// rows fitted as `#7488b0`, and the boundary solve, honest about that colour, widened the +/// line to 1.6 px to darken the image enough. The palette ink is the better estimate of +/// such a face's colour, and with it the solve places the edges by coverage. +/// +/// The evidence is per pixel: over white a pixel of ink `k` at coverage `a` reads +/// `1 + a·(k − 1)` (the image is composited over white in sRGB), and its alpha is `a`. When +/// at least [`THIN_COVERAGE_SHARE`] of the face's pixels read so, within four 8-bit levels +/// per channel, the face is the ink's coverage. An opaque tint of the ink lies on the same +/// line from the ink to white but at full opacity, and a thin face between two opaque +/// colours is a blend of those, not of the ground; both are left as fitted. Faces of a +/// translucent ink keep their fill too, their opacity already accounting for the ground. +fn paint_thin_faces_their_ink( + faces: &[u16], + (w, h): (usize, usize), + (rgb, alpha): (&[[f32; 3]], &[f32]), + face_fill: &mut [gradient::FillFit], + face_color: &[usize], + pal: &Palette, +) -> usize { + const TOL: f32 = 4.0 / 255.0; + let n = face_fill.len().min(face_color.len()); + let interior = color::snap::interior_counts(faces, w, h, n); + // The opaque ink of each candidate face: flat, no interior pixel, an opaque ink. + let ink_of = |f: usize| -> Option<[f32; 3]> { + let ink = face_color[f]; + let flat = matches!(face_fill[f].model, gradient::FillModel::Flat(_)); + let opaque = pal.alpha.get(ink).is_none_or(|&a| a >= OPAQUE); + (flat && interior[f] == 0 && ink < pal.len() && opaque).then(|| pal.rgb[ink]) + }; + let inks: Vec> = (0..n).map(ink_of).collect(); + if inks.iter().all(Option::is_none) { + return 0; + } + // Per candidate face: (pixels that read as coverage of its ink, pixels). + let mut tally = vec![(0u32, 0u32); n]; + for ((&f, &c), &a) in faces.iter().zip(rgb).zip(alpha).take(w * h) { + let Some(k) = inks.get(f as usize).copied().flatten() else { + continue; + }; + let fits = (0..3).all(|i| (c[i] - (1.0 + a * (k[i] - 1.0))).abs() <= TOL); + let t = &mut tally[f as usize]; + t.0 += u32::from(fits); + t.1 += 1; + } + let mut changed = 0; + for f in 0..n { + let (Some(k), (fits, count)) = (inks[f], tally[f]) else { + continue; + }; + let covered = count > 0 && fits as f32 >= THIN_COVERAGE_SHARE * count as f32; + if covered && face_fill[f].model != gradient::FillModel::Flat(k) { + face_fill[f].model = gradient::FillModel::Flat(k); + changed += 1; + } + } + changed +} + /// The colour path with transparency carried natively. Mirrors /// [`crate::trace_color_full_with_alpha`] stage for stage; see the module docs for what /// changes and why. @@ -997,9 +1065,17 @@ pub fn trace_color(img: &Rgba, opts: &ColorOptions, alpha: &[f32]) -> ColorTrace color::snap::snap_flat_fills(&labels, w, h, &mut face_fill, &face_color, &pal, |f| { face_fade.get(f).is_some_and(Option::is_some) }); + let thin = paint_thin_faces_their_ink( + &labels, + (w, h), + (&rgb, alpha), + &mut face_fill, + &face_color, + &pal, + ); crate::diag!( "fills", - "native alpha: snapped to their ink's colour: {snapped}" + "native alpha: snapped to their ink's colour: {snapped}; thin faces given their ink: {thin}" ); // Where a face meets the ground the boundary is placed by its opacity there: a fade's // rim, a wash's one opacity. diff --git a/crates/inkvec-trace/src/native/palette.rs b/crates/inkvec-trace/src/native/palette.rs index f191f2ad..6ff1dd46 100644 --- a/crates/inkvec-trace/src/native/palette.rs +++ b/crates/inkvec-trace/src/native/palette.rs @@ -182,6 +182,7 @@ pub(crate) fn extract( claim: Claim::new(points), scratch: vec![0u8; points + 1], paldbg: inkvec_core::env::flag("INKVEC_PALDBG"), + no_interior: false, }; // The clear ground draws nothing, so it is found but not counted against the cap; once // the cap is full the scan goes on only to look for it. @@ -194,10 +195,9 @@ pub(crate) fn extract( if full && !clear(&c) { continue; } - if walk.accepts(n, c) { - walk.accept(c); - } + walk.consider(n, c); } + walk.drop_twin_coverage(); let paldbg = walk.paldbg; let mut colors = walk.colors; if colors.is_empty() { @@ -284,11 +284,151 @@ struct Walk<'v, 'a> { /// One byte per point for the straddle test. scratch: Vec, paldbg: bool, + /// Whether the last [`Walk::accepts`] refused a translucent candidate for having no + /// interior (`BlendEvidence::measure` returning `None`). + no_interior: bool, +} + +/// Opacity a translucent candidate with no interior needs before its opaque version is +/// tried in its place (see [`Walk::consider`]). +/// +/// A hairline narrower than two pixels leaves every pixel it touches partly covered, and +/// the pixels across it share its coverage: a 1 px line between two rows reads 0.62 and +/// 0.38, a 1.5 px one 0.88 and 0.62 (`bench/cases` `ribbon_w1`, `ribbon_w1.5`). The more +/// covered side of any line at least 1 px wide is at least half covered, while the faint +/// fringe of a glow or an anti-aliased rim is not, so only a candidate at least half +/// opaque is read as part of a line. +pub(super) const HAIRLINE_MIN_ALPHA: f32 = 0.5; + +/// The opaque ink a translucent colour would be partial coverage of: the same straight +/// colour at full opacity, `s = (W − (1 − a)) / a` per sRGB channel (the image is +/// composited over white in sRGB, `W = s·a + (1 − a)`). `None` for the clear ground and +/// for a colour [`TRANSLUCENT_BELOW`] treats as opaque, and when `s` leaves sRGB by more +/// than four 8-bit levels of `W` (then the colour is no coverage of one straight colour). +pub(super) fn opaque_version(c: Ink2) -> Option { + let a = c.alpha(); + if a <= CLEAR_INK_ALPHA || a >= TRANSLUCENT_BELOW { + return None; + } + let w = oklab_to_rgb(c.w); + let s = w.map(|v| (v - (1.0 - a)) / a); + let slack = 4.0 / 255.0 / a; + if s.iter().any(|&v| v < -slack || v > 1.0 + slack) { + return None; + } + Some(Ink2::opaque(rgb_to_oklab(s.map(|v| v.clamp(0.0, 1.0))))) } impl Walk<'_, '_> { + /// Decide candidate `c` (from a bin of `n` pixels): accept it, or, for a hairline, its + /// opaque version. + /// + /// A line narrower than about a pixel and a half covers no pixel completely, so its ink + /// never shows at full opacity: every pixel it touches is a translucent mix of the ink + /// and the clear ground. Each such colour is refused for having no interior + /// (`BlendEvidence::measure`), and before this the line vanished (`bench/cases` + /// `ribbon_w1`, `ribbon_w1.5`). So when `c` is refused for that reason and is at least + /// [`HAIRLINE_MIN_ALPHA`] opaque, its opaque version ([`opaque_version`]) is weighed in + /// its place, through every gate a candidate passes. The partly covered pixels are then + /// blends of that ink and the ground, which the labels and the sub-pixel stage read as + /// coverage. A version within the merge radius of an accepted ink is not tried: `c` is + /// that ink's anti-aliasing, and the blend tests have already said so. + fn consider(&mut self, n: u32, c: Ink2) { + if self.accepts(n, c) { + self.accept(c); + return; + } + if !self.no_interior || c.alpha() < HAIRLINE_MIN_ALPHA { + return; + } + let Some(o) = opaque_version(c) else { + return; + }; + if same_ink_as_accepted(o, &self.colors, self.ev.same_ink_de00) + || self + .colors + .iter() + .any(|&p| p.dist(o) <= self.merge_distance) + { + return; + } + if self.accepts(n, o) { + if self.paldbg { + eprintln!( + " native hairline: {} a={:.3} read as opaque {}", + color::to_hex(oklab_to_rgb(c.w)), + c.alpha(), + color::to_hex(oklab_to_rgb(o.w)) + ); + } + self.accept(o); + } + } + + /// After the walk, drop every translucent ink that is the anti-aliasing of an opaque + /// ink of its own colour (its [`opaque_version`] within the merge radius of an accepted + /// opaque ink). Returns how many were dropped. + /// + /// The walk visits colours by pixel count, and a stroke about two pixels wide has more + /// partly covered pixels than covered ones. Its partial coverage then comes up first, + /// as a colour that is no blend of anything yet accepted, and can pass with an interior + /// borrowed from the solid core it claims while that core is not yet an ink: a 2 px + /// diagonal (`bench/cases` `sawtooth_diag`) was accepted at 0.83 opacity, then its + /// core at 1.0, and came out as a translucent band around opaque specks. With both + /// accepted, the translucent one is weighed again as what it is -- with every other + /// ink in place, a blend of its opaque twin and the clear ground -- and dropped when the + /// blend tests call it coverage. A translucent wash beside a solid of its colour keeps + /// its interior and stays. Only a palette holding both versions of one colour is + /// touched; each re-weighing rebuilds the nearest-ink table, O(inks · points). + fn drop_twin_coverage(&mut self) -> usize { + let mut dropped = 0; + let mut i = 0; + while i < self.colors.len() { + let c = self.colors[i]; + let twin = opaque_version(c).is_some_and(|o| { + self.colors + .iter() + .any(|&p| p.alpha() >= OPAQUE && p.dist(o) <= self.merge_distance) + }); + if !twin { + i += 1; + continue; + } + let mut others = self.colors.clone(); + others.remove(i); + self.rebuild(&others); + if self.accepts(0, c) { + others.insert(i, c); + self.rebuild(&others); + i += 1; + } else { + if self.paldbg { + eprintln!( + " native drop: {} a={:.3} is the coverage of its opaque twin", + color::to_hex(oklab_to_rgb(c.w)), + c.alpha() + ); + } + dropped += 1; + } + } + dropped + } + + /// Reset the walk to the inks `inks`, accepted in that order. + fn rebuild(&mut self, inks: &[Ink2]) { + self.colors.clear(); + self.six = InkSix::default(); + self.nearest.fill(f32::INFINITY); + self.nearest_ink.fill(u32::MAX); + for &p in inks { + self.accept(p); + } + } + /// Whether candidate `c` (from a bin of `n` pixels) passes every gate. fn accepts(&mut self, n: u32, c: Ink2) -> bool { + self.no_interior = false; let PaletteEvidence { sigma_noise, lambda, @@ -354,6 +494,7 @@ impl Walk<'_, '_> { } } let Some(shape) = BlendEvidence::measure(self, c, escaped) else { + self.no_interior = true; return false; }; if self.paldbg { diff --git a/crates/inkvec-trace/src/native/reference_tests.rs b/crates/inkvec-trace/src/native/reference_tests.rs index 4fef1a45..9cd78167 100644 --- a/crates/inkvec-trace/src/native/reference_tests.rs +++ b/crates/inkvec-trace/src/native/reference_tests.rs @@ -239,16 +239,10 @@ pub fn extract_palette( // The clear ground draws nothing, so it is found but not counted against the cap; once // the cap is full the scan goes on only to look for it. let clear = |c: &Ink2| c.alpha() <= CLEAR_INK_ALPHA; - for &(n, _key, c) in &modes { - let full = colors.iter().filter(|p| !clear(p)).count() >= max_colors; - if full && colors.iter().any(clear) { - break; - } - if full && !clear(&c) { - continue; - } - let (claim, spread) = - claim_spread(&view.px, &nearest_px, c, merge_distance, view.stride_px); + // Every gate of the shipped walk on one candidate: `Some(true)` accepts, `Some(false)` + // refuses, `None` refuses a translucent candidate for having no interior. + let gate = |c: Ink2, n: u32, colors: &[Ink2], nearest_px: &[f32]| -> Option { + let (claim, spread) = claim_spread(&view.px, nearest_px, c, merge_distance, view.stride_px); // The rarity exemption of 2026-10-02 (`palette::rarity_exempt`: the first ink that // draws something is exempt, not only the first ink), applied here as well, so this // oracle still tests the per-point rewrite and nothing else. @@ -260,8 +254,8 @@ pub fn extract_palette( .map(|&p| p.dist(c)) .fold(f32::INFINITY, f32::min); let reach = noise_sigmas * spread; - if same_ink_as_accepted(c, &colors, same_ink_de00) { - continue; + if same_ink_as_accepted(c, colors, same_ink_de00) { + return Some(false); } let escaped = nearest <= merge_distance.max(reach); if escaped { @@ -271,17 +265,13 @@ pub fn extract_palette( && 0.5 * (claim as f64) * ((nearest as f64 / sigma_noise).powi(2)) > lambda * PARAMS_PER_INK; if !worth_it { - continue; + return Some(false); } } - if rare && !represented_per_pixel(&view, c, &colors, &nearest_px, sigma_noise) { - continue; + if rare && !represented_per_pixel(&view, c, colors, nearest_px, sigma_noise) { + return Some(false); } - let Some(shape) = - BlendEvidence::measure(&view, c, &colors, &nearest_px, merge_distance, escaped) - else { - continue; - }; + let shape = BlendEvidence::measure(&view, c, colors, nearest_px, merge_distance, escaped)?; if paldbg { eprintln!( " native cand w={} k={} a={:.3} bin={n} claim={claim} near={nearest:.4} blend={} interior={:.3} straddle={:.3}", @@ -293,18 +283,74 @@ pub fn extract_palette( shape.straddle ); } - if shape.is_coverage() { - continue; - } // The escape rule (`crate::color::escape_needs_interior`), as the shipped walk. - if color::escape_needs_interior(escaped, shape.blend, shape.interior) { - continue; - } + Some( + !shape.is_coverage() + && !color::escape_needs_interior(escaped, shape.blend, shape.interior), + ) + }; + let lower = |nearest_px: &mut [f32], c: Ink2| { nearest_px .par_iter_mut() .zip(view.px.par_iter()) .for_each(|(d, &q)| *d = d.min(q.dist(c))); - colors.push(c); + }; + for &(n, _key, c) in &modes { + let full = colors.iter().filter(|p| !clear(p)).count() >= max_colors; + if full && colors.iter().any(clear) { + break; + } + if full && !clear(&c) { + continue; + } + let ink = match gate(c, n, &colors, &nearest_px) { + Some(true) => Some(c), + Some(false) => None, + // The hairline rule (`palette::Walk::consider`): a translucent candidate with no + // interior, at least half opaque, is weighed again as its opaque version. + None if c.alpha() >= palette::HAIRLINE_MIN_ALPHA => { + palette::opaque_version(c).filter(|&o| { + !same_ink_as_accepted(o, &colors, same_ink_de00) + && colors.iter().all(|&p| p.dist(o) > merge_distance) + && gate(o, n, &colors, &nearest_px) == Some(true) + }) + } + None => None, + }; + if let Some(ink) = ink { + lower(&mut nearest_px, ink); + colors.push(ink); + } + } + // The twin rule (`palette::Walk::drop_twin_coverage`): a translucent ink whose opaque + // version is an accepted opaque ink is weighed again with every other ink in place. + let mut i = 0; + while i < colors.len() { + let c = colors[i]; + let twin = palette::opaque_version(c).is_some_and(|o| { + colors + .iter() + .any(|&p| p.alpha() >= OPAQUE && p.dist(o) <= merge_distance) + }); + if !twin { + i += 1; + continue; + } + let others: Vec = colors + .iter() + .enumerate() + .filter(|&(j, _)| j != i) + .map(|(_, &p)| p) + .collect(); + let mut near = vec![f32::INFINITY; view.px.len()]; + for &p in &others { + lower(&mut near, p); + } + if gate(c, 0, &others, &near) == Some(true) { + i += 1; + } else { + colors = others; + } } if colors.is_empty() { colors.push(modes.first().map(|m| m.2).unwrap_or(Ink2::opaque(Oklab { @@ -490,7 +536,7 @@ impl BlendEvidence { // (dark 0.0077 -> 0.0079). let translucent = { let a = c.alpha(); - a > 0.0 && a < super::TRANSLUCENT_BELOW + a > 0.0 && a < TRANSLUCENT_BELOW }; // Measured for an escaped candidate too: the escape rule reads it. let interior = if blend || translucent || escaped { diff --git a/crates/inkvec-trace/src/native/tests.rs b/crates/inkvec-trace/src/native/tests.rs index 9ded1c05..40070d14 100644 --- a/crates/inkvec-trace/src/native/tests.rs +++ b/crates/inkvec-trace/src/native/tests.rs @@ -908,9 +908,12 @@ fn a_translucent_wash_is_one_ink_with_its_own_opacity() { } #[test] -fn a_hairline_needs_to_be_opaque_to_be_an_ink_and_a_clear_gap_is_always_one() { - // One pixel wide: opaque paint is an ink, a translucent wash of it is not (it has no - // interior), and a transparent gap through paint is the clear ground. +fn a_hairline_is_an_opaque_ink_however_little_of_a_pixel_it_covers_and_a_clear_gap_is_one() { + // One pixel wide: opaque paint is an ink. A column at 0.6 has no interior, so it is no + // translucent ink; it is read as a hairline of the opaque paint covering 0.6 of each + // pixel (`palette::Walk::consider`). A faint one, below `HAIRLINE_MIN_ALPHA`, is the + // fringe of something else and stays out. A transparent gap through paint is the clear + // ground. let line = |a: f32| { image( 12, @@ -920,7 +923,11 @@ fn a_hairline_needs_to_be_opaque_to_be_an_ink_and_a_clear_gap_is_always_one() { let (rgb, alpha) = line(1.0); let pal = extract_palette(&rgb, &alpha, 12, 12, 0.035, 64, evidence(0.0, 1.0, 1.5)); assert_eq!(pal.alpha, vec![0.0, 1.0]); - let (rgb, alpha) = line(0.5); + let (rgb, alpha) = line(0.6); + let pal = extract_palette(&rgb, &alpha, 12, 12, 0.035, 64, evidence(0.0, 1.0, 1.5)); + assert_eq!(pal.alpha, vec![0.0, 1.0]); + assert!(close(pal.rgb[1], GREEN, 0.01), "{:?}", pal.rgb[1]); + let (rgb, alpha) = line(0.3); let pal = extract_palette(&rgb, &alpha, 12, 12, 0.035, 64, evidence(0.0, 1.0, 1.5)); assert_eq!(pal.alpha, vec![0.0]); let (rgb, alpha) = image(12, |x, _| if x == 5 { ([0.0; 3], 0.0) } else { (RED, 1.0) }); @@ -928,6 +935,76 @@ fn a_hairline_needs_to_be_opaque_to_be_an_ink_and_a_clear_gap_is_always_one() { assert_eq!(pal.alpha, vec![1.0, 0.0]); } +#[test] +fn opaque_version_is_the_straight_colour_at_full_opacity() { + let o = palette::opaque_version(ink(BLUE, 0.4)).expect("translucent"); + assert!( + close(oklab_to_rgb(o.w), BLUE, 0.002), + "{:?}", + oklab_to_rgb(o.w) + ); + assert_eq!(o.w, o.k); + assert!(palette::opaque_version(ink(BLUE, 1.0)).is_none()); + assert!(palette::opaque_version(ink(BLUE, 0.0)).is_none()); + // At or above `TRANSLUCENT_BELOW` a colour already counts as opaque. + assert!(palette::opaque_version(ink(BLUE, 0.99)).is_none()); +} + +#[test] +fn a_line_split_between_two_rows_is_one_opaque_ink() { + // A 1 px line straddling two rows at 0.62 and 0.38 (`bench/cases` `ribbon_w1`): no + // pixel shows the paint at full opacity, and the palette still has it, opaque. + let (rgb, alpha) = image(16, |_, y| match y { + 7 => (BLUE, 0.62), + 8 => (BLUE, 0.38), + _ => ([0.0; 3], 0.0), + }); + let pal = extract_palette(&rgb, &alpha, 16, 16, 0.035, 64, evidence(0.0, 1.0, 1.5)); + assert_eq!(pal.alpha, vec![0.0, 1.0], "{:?}", pal.rgb); + assert!(close(pal.rgb[1], BLUE, 0.01), "{:?}", pal.rgb[1]); +} + +#[test] +fn a_stroke_whose_coverage_outnumbers_its_core_is_one_opaque_ink() { + // Two columns: a solid core one pixel wide, and on each side a column at 0.8 coverage + // of the same paint. The 0.8 pixels outnumber the core two to one, so the walk meets + // them first and, claiming the core, they show an interior; once the core is an ink + // they are its anti-aliasing (`palette::Walk::drop_twin_coverage`). + let (rgb, alpha) = image(16, |x, y| { + if !(2..14).contains(&y) { + return ([0.0; 3], 0.0); + } + match x { + 7 => (BLUE, 1.0), + 6 | 8 => (BLUE, 0.8), + _ => ([0.0; 3], 0.0), + } + }); + let pal = extract_palette(&rgb, &alpha, 16, 16, 0.035, 64, evidence(0.0, 1.0, 1.5)); + assert_eq!(pal.alpha, vec![0.0, 1.0], "{:?}", pal.rgb); +} + +#[test] +fn a_wash_beside_a_solid_of_its_colour_keeps_its_own_opacity() { + // The twin rule drops only coverage: a half-opacity wash with an interior next to a + // solid of the same paint is an ink of its own. + let (rgb, alpha) = image(16, |x, y| { + if !(2..14).contains(&y) { + ([0.0; 3], 0.0) + } else if (2..6).contains(&x) { + (BLUE, 1.0) + } else if (6..14).contains(&x) { + (BLUE, 0.5) + } else { + ([0.0; 3], 0.0) + } + }); + let pal = extract_palette(&rgb, &alpha, 16, 16, 0.035, 64, evidence(0.0, 1.0, 1.5)); + find(&pal, 1.0); + find(&pal, 0.5); + find(&pal, 0.0); +} + /// Two opaque halves `d` apart in OKLab lightness. fn halves(d: f32) -> (Vec<[f32; 3]>, Vec) { // 0.58 is bin 13 of lightness, 0.60 and 0.61 bin 14: the halves are two modes. @@ -1175,3 +1252,60 @@ fn tracing_a_glow_hands_its_fade_to_the_face() { assert!((a - alpha[x]).abs() < 0.02, "x={x}: {a} vs {}", alpha[x]); } } + +// --------------------------------------------------------------------------------------- +// Thin faces +// --------------------------------------------------------------------------------------- + +#[test] +fn a_thin_face_of_coverage_is_painted_its_ink_and_a_tint_or_a_solid_face_is_not() { + // 8 x 8, faces by row band: 0 the clear ground; 1 a one-row line of BLUE at 0.6 + // coverage; 2 a one-row line of an opaque tint of BLUE, the colour 0.6 of BLUE shows + // over white but at full opacity; 3 a solid three-row block, filled at the same tint. + let (w, h) = (8, 8); + let face = |y: usize| -> u16 { + match y { + 1 => 1, + 3 => 2, + 5..=7 => 3, + _ => 0, + } + }; + let tint = on_white(BLUE, 0.6); + let faces: Vec = (0..w * h).map(|i| face(i / w)).collect(); + let (rgb, alpha): (Vec<[f32; 3]>, Vec) = (0..w * h) + .map(|i| match face(i / w) { + 0 => ([1.0; 3], 0.0), + 1 => (tint, 0.6), + _ => (tint, 1.0), + }) + .unzip(); + let pal = Palette { + colors: vec![rgb_to_oklab([1.0; 3]), rgb_to_oklab(BLUE)], + rgb: vec![[1.0; 3], BLUE], + weight: vec![0.5, 0.5], + alpha: vec![0.0, 1.0], + }; + let flat = |c: [f32; 3]| gradient::FillFit { + model: FillModel::Flat(c), + chi2: 0.0, + params: gradient::PARAMS_FLAT, + cost: 0.0, + }; + let mut fills = vec![flat([1.0; 3]), flat(tint), flat(tint), flat(tint)]; + let colour = [0, 1, 1, 1]; + let changed = + paint_thin_faces_their_ink(&faces, (w, h), (&rgb, &alpha), &mut fills, &colour, &pal); + assert_eq!(changed, 1); + assert_eq!(fills[1].model, FillModel::Flat(BLUE)); + assert_eq!( + fills[2].model, + FillModel::Flat(tint), + "an opaque tint keeps its fill" + ); + assert_eq!( + fills[3].model, + FillModel::Flat(tint), + "a face with an interior keeps its fill" + ); +} diff --git a/crates/inkvec-trace/src/planar.rs b/crates/inkvec-trace/src/planar.rs index fb722f65..3329e205 100644 --- a/crates/inkvec-trace/src/planar.rs +++ b/crates/inkvec-trace/src/planar.rs @@ -824,7 +824,11 @@ fn refine_vertex( // applied at gradient boundaries, no loss without. let both_flat = !fa.is_gradient() && !fb.is_gradient(); if !is_step_like(&probes, corner, both_flat, axis.contrast, ctx.min_contrast) { - hit = root_find_half(ctx, &axis, p, nx, ny); + hit = if both_flat && !corner { + ridge_offset(ctx, &axis, p, nx, ny).or_else(|| root_find_half(ctx, &axis, p, nx, ny)) + } else { + root_find_half(ctx, &axis, p, nx, ny) + }; } if ctx.debug { eprintln!( @@ -1174,6 +1178,76 @@ fn is_step_like( && contrast >= 2.0 * min_contrast } +/// The offset of this vertex's edge when the coverage across it is a ridge: a stroke too +/// thin to cover any pixel completely, with the same face on both sides. `None` otherwise. +/// +/// A pixel cut by one straight edge reads that edge's offset, which is what +/// [`invert_step`] and [`root_find_half`] read. A stroke narrower than about a pixel and a +/// half cuts every pixel it touches with *both* of its edges, and the coverage across it +/// never reaches 1: a 1 px line between two rows reads 0.62 / 0.38. Its 0.5 crossing then +/// falls between those two pixels, on the stroke's centre line, and the edge was put there +/// (`bench/cases` `ribbon_w1`, drawn half as wide as it is). What such a profile does give +/// exactly is the stroke's width and centre: box-filtered coverage integrates to the width +/// along the normal (the pixel's footprint has unit area), `W = ∫ a ds`, and its centroid +/// is the stroke's centre. The edges are the centre ± `W/2`; this vertex takes the one on +/// its own side of the centre. +/// +/// Pixel centres are read along the normal over ±2.5 px. The reading applies when the +/// profile starts and ends on the ground (coverage ≤ 0.03 at both ends), has one peak +/// between, and no pixel reaches 0.97 (a covered pixel means [`invert_step`]'s single edge +/// reading holds on that side). The integral is the trapezoid rule over the pixel centres' +/// projections, exact on an axis and close on a slant. +fn ridge_offset(ctx: &RefineCtx, axis: &UnmixAxis, p: Point, nx: f64, ny: f64) -> Option { + const GROUND: f64 = 0.03; + const COVERED: f64 = 0.97; + let (w, h) = (ctx.src.w as f64, ctx.src.h as f64); + let mut probes: Vec<(f64, f64)> = Vec::with_capacity(11); + for i in -5..=5 { + let u = 0.5 * i as f64; + let (cx, cy) = ((p.x + nx * u).round(), (p.y + ny * u).round()); + if cx < 0.0 || cy < 0.0 || cx >= w || cy >= h { + continue; + } + let s = (cx - p.x) * nx + (cy - p.y) * ny; + if probes.iter().any(|q| (q.0 - s).abs() < 1e-9) { + continue; + } + probes.push((s, axis.coverage(&ctx.src, cx, cy)?)); + } + probes.sort_by(|a, b| a.0.total_cmp(&b.0)); + let (first, last) = (probes.first()?, probes.last()?); + if first.1 > GROUND || last.1 > GROUND { + return None; + } + let peak = probes.iter().map(|q| q.1).fold(0.0f64, f64::max); + if peak <= GROUND || peak >= COVERED { + return None; + } + // One peak: coverage rises to it and falls after it (to within 0.05). + let top = probes.iter().position(|q| q.1 == peak)?; + let rises = probes[..=top].windows(2).all(|q| q[1].1 >= q[0].1 - 0.05); + let falls = probes[top..].windows(2).all(|q| q[1].1 <= q[0].1 + 0.05); + if !rises || !falls { + return None; + } + let (mut width, mut moment) = (0.0, 0.0); + for q in probes.windows(2) { + let ds = q[1].0 - q[0].0; + width += 0.5 * (q[0].1 + q[1].1) * ds; + moment += 0.5 * (q[0].0 * q[0].1 + q[1].0 * q[1].1) * ds; + } + if width <= 1e-6 { + return None; + } + let centre = moment / width; + let half = 0.5 * width; + Some(if centre < 0.0 { + centre + half + } else { + centre - half + }) +} + /// The first crossing of coverage 0.5 along the normal, by linear interpolation between /// ten bilinear samples evenly spaced over `u` in `[−1, 1]` px. `None` when coverage /// never crosses 0.5 there. diff --git a/crates/inkvec-trace/src/planar/refine_tests.rs b/crates/inkvec-trace/src/planar/refine_tests.rs index f7b0566e..5174cad4 100644 --- a/crates/inkvec-trace/src/planar/refine_tests.rs +++ b/crates/inkvec-trace/src/planar/refine_tests.rs @@ -173,3 +173,55 @@ fn parallel_refinement_equals_the_serial_loop() { assert_eq!(refine_in_parallel(&base), w >= 96, "{w}x{h}"); } } + +/// A 1 px line split between two rows at 0.62 / 0.38 coverage (`bench/cases` `ribbon_w1`): +/// no pixel is fully covered, and both edges come back where the line is, not on its +/// centre line ([`ridge_offset`]). +#[test] +fn a_line_too_thin_to_cover_a_pixel_keeps_both_of_its_edges() { + let (w, h) = (24usize, 12usize); + let (top, bottom) = (4.88f64, 5.88f64); // pixel row r spans [r - 0.5, r + 0.5] + let (ground, ink) = ([1.0f32, 1.0, 1.0], [0.1f32, 0.2, 0.5]); + let cover = |y: usize| -> f64 { + let (lo, hi) = (y as f64 - 0.5, y as f64 + 0.5); + (hi.min(bottom) - lo.max(top)).max(0.0) + }; + let on_line = |x: usize| (2..22).contains(&x); + let rgb: Vec<[f32; 3]> = (0..w * h) + .map(|p| { + let a = if on_line(p % w) { + cover(p / w) as f32 + } else { + 0.0 + }; + std::array::from_fn(|i| a * ink[i] + (1.0 - a) * ground[i]) + }) + .collect(); + // The labels give the line the row it covers more of. + let labels: Vec = (0..w * h) + .map(|p| u16::from(on_line(p % w) && p / w == 5)) + .collect(); + let mut map = build(&labels, w, h, 2); + let fills = [FillModel::Flat(ground), FillModel::Flat(ink)]; + refine_subpixel(&mut map, &rgb, &fills, 0.004, false); + // Away from the line's ends, every boundary point sits on one of the two edges. + let (mut tops, mut bottoms) = (0, 0); + for e in map.edges.iter().filter(|e| e.left == 1 || e.right == 1) { + for p in e.points.iter().filter(|p| (6.0..=18.0).contains(&p.x)) { + let (target, n) = if p.y < 5.38 { + (top, &mut tops) + } else { + (bottom, &mut bottoms) + }; + assert!( + (p.y - target).abs() < 0.02, + "{p:?} is not on the edge at {target}" + ); + *n += 1; + } + } + assert!( + tops > 0 && bottoms > 0, + "{tops} top and {bottoms} bottom points" + ); +} diff --git a/crates/inkvec-trace/src/symmetry.rs b/crates/inkvec-trace/src/symmetry.rs index c4f8d1c8..2e5455b5 100644 --- a/crates/inkvec-trace/src/symmetry.rs +++ b/crates/inkvec-trace/src/symmetry.rs @@ -17,10 +17,19 @@ //! //! So the symmetry is enforced structurally rather than defended stage by stage. The label //! map says exactly which mirrors hold, and it says so with no tolerance and no threshold: -//! a mirror is admitted only when *every* pixel maps to a pixel of the same ink. That test -//! cannot be fooled into snapping a logo the artist drew asymmetric, which is the failure -//! that would matter. Given a mirror, the boundaries pair off, and each pair is replaced by -//! the average of the two and its exact reflection. +//! a mirror is admitted only when *every* pixel maps to a pixel of the same ink. Besides the +//! two mirrors through the centre, the half turn about it is a candidate too, when neither +//! mirror holds (with both, it holds already). Given a mirror, the boundaries pair off. +//! +//! The pixel test sees whole pixels, and a drawing can be symmetric at that resolution and +//! not below it: four colours meeting 0.27 px off the centre leave a label map that is its +//! own half turn. So before a pair is averaged, the refined boundary is asked as well: the +//! paired points of a symmetric drawing differ by the ties the stages broke, a few hundredths +//! of a pixel and within their measured uncertainty, while those of an asymmetric one differ +//! along whole edges ([`AGREEMENT_Z`]). A symmetry the boundary confirms is enforced -- each +//! pair replaced by the average of the two and its exact image -- and one it does not is +//! dropped, which is the failure that would matter: snapping a logo the artist drew +//! asymmetric. //! //! What this does not do is make the *fit* symmetric. Two mirror-paired boundaries with //! exactly mirrored points can still be fitted differently, because the dynamic program @@ -64,15 +73,24 @@ pub enum Mirror { V(i64), /// Horizontal mirror axis: reflects `y` to `k - y`, leaves `x` unchanged. H(i64), + /// Half turn about `(kx/2, ky/2)`: carries `(x, y)` to `(kx − x, ky − y)`. Not a mirror, + /// a rotation, but it pairs pixels and boundaries the same way, so it is handled as one. + Rot180(i64, i64), } impl Mirror { + /// Whether this is a reflection about an axis (`V` or `H`), as opposed to the half turn. + pub fn is_axis(self) -> bool { + matches!(self, Mirror::V(_) | Mirror::H(_)) + } + /// The pixel that pixel `(x, y)` reflects to (possibly outside the image). #[inline] fn pixel(self, x: i64, y: i64) -> (i64, i64) { match self { Mirror::V(k) => (k - x, y), Mirror::H(k) => (x, k - y), + Mirror::Rot180(kx, ky) => (kx - x, ky - y), } } @@ -82,6 +100,7 @@ impl Mirror { match self { Mirror::V(k) => Point::new(k as f64 - p.x, p.y), Mirror::H(k) => Point::new(p.x, k as f64 - p.y), + Mirror::Rot180(kx, ky) => Point::new(kx as f64 - p.x, ky as f64 - p.y), } } } @@ -191,11 +210,19 @@ fn mirrors_of(labels: &[u16], ink: &[usize], w: usize, h: usize) -> Vec // horizontal axis. The loop that used to try every k was pure cost: each off-centre // horizontal candidate matched white margin rows against white margin rows before it // could fail, O(h^2 w) in all -- 1.1 s of a 4 s trace at 2048 px, for nothing. - if holds(Mirror::V(w as i64 - 1)) { - out.push(Mirror::V(w as i64 - 1)); + let (v, hz) = (Mirror::V(w as i64 - 1), Mirror::H(h as i64 - 1)); + let (v_holds, h_holds) = (holds(v), holds(hz)); + if v_holds { + out.push(v); } - if holds(Mirror::H(h as i64 - 1)) { - out.push(Mirror::H(h as i64 - 1)); + if h_holds { + out.push(hz); + } + // The half turn is the two mirrors composed: with both, enforcing them already makes + // every boundary its own half-turn image. With one, it cannot hold (it would make the + // other hold too). So it is tested only when neither mirror holds, at the same centre. + if !v_holds && !h_holds && holds(Mirror::Rot180(w as i64 - 1, h as i64 - 1)) { + out.push(Mirror::Rot180(w as i64 - 1, h as i64 - 1)); } out } @@ -328,21 +355,72 @@ pub fn detect(map: &PlanarMap, labels: &[u16], ink: &[usize]) -> Symmetry { Symmetry { mirrors, partner } } -/// Replace each boundary by the average of itself and its partner's reflection. +/// The 90th percentile of the paired points' disagreement, in standard deviations, above +/// which a symmetry the label map shows is not enforced ([`enforce`]). +/// +/// The label map is symmetric at pixel resolution, and so it can be while the drawing is +/// not: a junction 0.27 px off the image centre (`bench/cases` `junction_quad`) leaves a +/// label map that is exactly its own half turn, and averaging the boundary with its image +/// pulled the junction onto the centre. The refined boundary says which it is. Paired +/// points of a drawing that is symmetric differ only by the ties the stages broke; measured +/// on `bench/cases` (`mirror_v`, `mirror_h`, `mirror_r`, both modes) the 90th percentile is +/// 0.4-1.2 standard deviations, 0.03-0.09 px. On `junction_quad` it is 7.6-8.2, 0.54-0.58 +/// px: whole edges a half pixel from their images. Three standard deviations is what +/// measurement noise reaches and a misplaced edge does not. +pub const AGREEMENT_Z: f64 = 3.0; + +/// How well the refined boundaries agree with `m`: the 90th percentile, over every paired +/// point with a measured uncertainty, of `|p − M(q)| / hypot(σ_p, σ_q)`, with `q` the +/// point `p` pairs with. 0 when no point is paired. +fn disagreement(map: &PlanarMap, m: Mirror, partner: &[Option]) -> f64 { + let mut z: Vec = Vec::new(); + for (k, e) in map.edges.iter().enumerate() { + let Some(pr) = partner.get(k).copied().flatten() else { + continue; + }; + let other = &map.edges[pr.edge]; + let n = e.points.len(); + if other.points.len() != n || e.sigma.len() != n || other.sigma.len() != n { + continue; + } + for i in 0..n { + let j = pr.index(i, n); + let d = e.points[i].dist(m.point(other.points[j])); + z.push(d / e.sigma[i].hypot(other.sigma[j]).max(1e-6)); + } + } + if z.is_empty() { + return 0.0; + } + z.sort_by(f64::total_cmp); + z[((z.len() - 1) as f64 * 0.9).round() as usize] +} + +/// Replace each boundary by the average of itself and its partner's reflection, for every +/// symmetry the refined boundaries confirm; drop the others from `sym`. /// /// Averaging rather than copying one onto the other keeps the result unbiased: the two /// sides carry the same evidence and disagree only by whatever tie was broken between /// them, so the midpoint is the estimate both of them support. /// -/// For each mirror in turn, every paired point becomes `½(p_i + M(q_{π(i)}))`, where `q` -/// is the partner edge as it was before this mirror's pass, `π` the [`Pairing`]'s index -/// map and `M` the reflection; both members of a pair then come out as exact reflections -/// of each other, and a boundary paired with itself becomes symmetric on its own. Returns +/// A symmetry is confirmed when its [`disagreement`] is at most [`AGREEMENT_Z`]; one that +/// is not is left out of `sym`, so the fitter does not reflect its fits either. For each +/// confirmed symmetry in turn, every paired point becomes `½(p_i + M(q_{π(i)}))`, where `q` +/// is the partner edge as it was before this symmetry's pass, `π` the [`Pairing`]'s index +/// map and `M` the reflection; both members of a pair then come out as exact images of +/// each other, and a boundary paired with itself becomes symmetric on its own. Returns /// how many points moved by more than `1e-12` px. -pub fn enforce(map: &mut PlanarMap, sym: &Symmetry) -> usize { +pub fn enforce(map: &mut PlanarMap, sym: &mut Symmetry) -> usize { let mut moved = 0usize; + let mut confirmed = vec![false; sym.mirrors.len()]; for (mi, &m) in sym.mirrors.iter().enumerate() { let partner = &sym.partner[mi]; + let z = disagreement(map, m, partner); + crate::diag!("symmetry", "mirror={m:?} disagreement_p90={z:.3}"); + if z > AGREEMENT_Z { + continue; + } + confirmed[mi] = true; let before: Vec> = map.edges.iter().map(|e| e.points.clone()).collect(); for (k, e) in map.edges.iter_mut().enumerate() { let Some(pr) = partner[k] else { continue }; @@ -363,6 +441,10 @@ pub fn enforce(map: &mut PlanarMap, sym: &Symmetry) -> usize { } } } + let mut keep = confirmed.iter(); + sym.partner.retain(|_| *keep.next().unwrap_or(&false)); + let mut keep = confirmed.iter(); + sym.mirrors.retain(|_| *keep.next().unwrap_or(&false)); moved } @@ -389,13 +471,25 @@ pub fn reflect_path(path: &FittedPath, m: Mirror) -> FittedPath { large_arc, sweep, end, - } => Segment::Arc { - rx, - ry, - phi: -phi, - large_arc, - sweep: !sweep, - end: m.point(end), + } => match m { + Mirror::V(_) | Mirror::H(_) => Segment::Arc { + rx, + ry, + phi: -phi, + large_arc, + sweep: !sweep, + end: m.point(end), + }, + // A rotation keeps handedness: same tilt (an ellipse turned by π is + // itself), same sweep. + Mirror::Rot180(..) => Segment::Arc { + rx, + ry, + phi, + large_arc, + sweep, + end: m.point(end), + }, }, }) .collect(), @@ -417,6 +511,8 @@ pub fn reflect_primitive(pf: &PrimitiveFit, m: Mirror) -> PrimitiveFit { angle: match m { Mirror::V(_) => PI - angle, Mirror::H(_) => -angle, + // A half turn leaves every axis direction where it was. + Mirror::Rot180(..) => angle, }, }, PrimitiveKind::RoundRect { x, y, w, h, rx } => match m { @@ -434,6 +530,13 @@ pub fn reflect_primitive(pf: &PrimitiveFit, m: Mirror) -> PrimitiveFit { h, rx, }, + Mirror::Rot180(kx, ky) => PrimitiveKind::RoundRect { + x: kx as f64 - (x + w), + y: ky as f64 - (y + h), + w, + h, + rx, + }, }, }; PrimitiveFit { @@ -452,44 +555,37 @@ pub fn reflect_primitive(pf: &PrimitiveFit, m: Mirror) -> PrimitiveFit { /// because each was fitted alone, and the middle one straddles the axis. pub fn centre_primitive(pf: &PrimitiveFit, m: Mirror) -> PrimitiveFit { use std::f64::consts::PI; + // The centre a self-symmetric primitive is moved to: onto the axis, or onto the centre + // of a half turn. + let centre = |c: Point| match m { + Mirror::V(k) => Point::new(k as f64 * 0.5, c.y), + Mirror::H(k) => Point::new(c.x, k as f64 * 0.5), + Mirror::Rot180(kx, ky) => Point::new(kx as f64 * 0.5, ky as f64 * 0.5), + }; let kind = match pf.kind { - PrimitiveKind::Circle { c, r } => PrimitiveKind::Circle { - c: match m { - Mirror::V(k) => Point::new(k as f64 * 0.5, c.y), - Mirror::H(k) => Point::new(c.x, k as f64 * 0.5), - }, - r, - }, + PrimitiveKind::Circle { c, r } => PrimitiveKind::Circle { c: centre(c), r }, // An ellipse across a mirror has one axis along it, so the angle is a right angle // away from the mirror's own or along it; snap to whichever it is already nearer. - PrimitiveKind::Ellipse { c, rx, ry, angle } => { - let snapped = (angle / (PI * 0.5)).round() * PI * 0.5; - PrimitiveKind::Ellipse { - c: match m { - Mirror::V(k) => Point::new(k as f64 * 0.5, c.y), - Mirror::H(k) => Point::new(c.x, k as f64 * 0.5), - }, - rx, - ry, - angle: snapped, - } - } - PrimitiveKind::RoundRect { x, y, w, h, rx } => match m { - Mirror::V(k) => PrimitiveKind::RoundRect { - x: (k as f64 - w) * 0.5, - y, - w, - h, - rx, + // Every ellipse centred on a half turn's centre is its own image, at any angle. + PrimitiveKind::Ellipse { c, rx, ry, angle } => PrimitiveKind::Ellipse { + c: centre(c), + rx, + ry, + angle: match m { + Mirror::V(_) | Mirror::H(_) => (angle / (PI * 0.5)).round() * PI * 0.5, + Mirror::Rot180(..) => angle, }, - Mirror::H(k) => PrimitiveKind::RoundRect { - x, - y: (k as f64 - h) * 0.5, + }, + PrimitiveKind::RoundRect { x, y, w, h, rx } => { + let c = centre(Point::new(x + 0.5 * w, y + 0.5 * h)); + PrimitiveKind::RoundRect { + x: c.x - 0.5 * w, + y: c.y - 0.5 * h, w, h, rx, - }, - }, + } + } }; PrimitiveFit { kind, @@ -534,14 +630,14 @@ mod tests { fn averages_a_nudge_back_out() { let (labels, w, h) = bars(); let mut map = planar::build(&labels, w, h, 2); - let sym = detect(&map, &labels, &[0, 1]); + let mut sym = detect(&map, &labels, &[0, 1]); assert!(!sym.is_empty()); let before: Vec> = map.edges.iter().map(|e| e.points.clone()).collect(); // Push every point of one boundary a tenth of a pixel to the right. for p in map.edges[0].points.iter_mut() { p.x += 0.1; } - enforce(&mut map, &sym); + enforce(&mut map, &mut sym); // Each side now carries half the nudge, and the pair is an exact reflection. let m = sym.mirrors[0]; for (k, e) in map.edges.iter().enumerate() { @@ -562,4 +658,113 @@ mod tests { // And the untouched boundaries did not move. assert!(map.edges.len() == before.len()); } + + /// Two blocks of one ink, each the other's half turn about the centre of a 12 x 12 + /// image, and neither mirror image of the other. + fn pinwheel() -> (Vec, usize, usize) { + let (w, h) = (12usize, 12usize); + let mut labels = vec![0u16; w * h]; + for y in 2..5 { + for x in 1..7 { + labels[y * w + x] = 1; + // The half turn of (x, y) is (11 - x, 11 - y). + labels[(11 - y) * w + (11 - x)] = 1; + } + } + (labels, w, h) + } + + #[test] + fn a_half_turn_is_found_when_neither_mirror_holds() { + let (labels, w, h) = pinwheel(); + let map = planar::build(&labels, w, h, 3); + let sym = detect(&map, &labels, &[0, 1, 1]); + assert_eq!(sym.mirrors, vec![Mirror::Rot180(11, 11)]); + // Each block's boundary pairs with the other's. + let paired = (0..map.edges.len()) + .filter(|&e| sym.mirror_of(e).is_some()) + .count(); + assert!(paired > 0); + // With both mirrors (the bars), the half turn is not listed: it holds already. + let (labels, w, h) = bars(); + let map = planar::build(&labels, w, h, 2); + assert!(!detect(&map, &labels, &[0, 1]) + .mirrors + .iter() + .any(|m| matches!(m, Mirror::Rot180(..)))); + } + + #[test] + fn a_symmetry_the_refined_boundary_contradicts_is_dropped_unenforced() { + let (labels, w, h) = pinwheel(); + let mut map = planar::build(&labels, w, h, 3); + let mut sym = detect(&map, &labels, &[0, 1, 1]); + assert!(!sym.is_empty()); + // Measured to 0.05 px, one block's boundary half a pixel off its image: no tie + // explains that, so the half turn is not enforced and leaves `sym`. + for e in map.edges.iter_mut() { + e.sigma.iter_mut().for_each(|s| *s = 0.05); + } + let e0 = sym.mirror_of(1).map(|(_, pr)| pr.edge).unwrap_or(0); + for p in map.edges[e0].points.iter_mut() { + p.x += 0.5; + } + let before: Vec> = map.edges.iter().map(|e| e.points.clone()).collect(); + assert_eq!(enforce(&mut map, &mut sym), 0); + assert!(sym.is_empty()); + let after: Vec> = map.edges.iter().map(|e| e.points.clone()).collect(); + assert_eq!(after, before); + } + + #[test] + fn a_half_turn_carries_paths_and_primitives_onto_their_images() { + use inkvec_fit::primitives::{PrimitiveFit, PrimitiveKind}; + let m = Mirror::Rot180(10, 6); + assert_eq!(m.point(Point::new(1.0, 2.0)), Point::new(9.0, 4.0)); + assert!(!m.is_axis() && Mirror::V(3).is_axis()); + let arc = FittedPath { + start: Point::new(1.0, 1.0), + segments: vec![Segment::Arc { + rx: 2.0, + ry: 1.0, + phi: 0.3, + large_arc: false, + sweep: true, + end: Point::new(3.0, 1.0), + }], + closed: false, + }; + // A rotation keeps handedness: the arc keeps its tilt and its sweep. + match reflect_path(&arc, m).segments[0] { + Segment::Arc { + phi, sweep, end, .. + } => { + assert_eq!((phi, sweep), (0.3, true)); + assert_eq!(end, Point::new(7.0, 5.0)); + } + _ => panic!("an arc stays an arc"), + } + let rect = PrimitiveFit { + kind: PrimitiveKind::RoundRect { + x: 1.0, + y: 1.0, + w: 2.0, + h: 1.0, + rx: 0.5, + }, + chi2: 0.0, + params: 5.0, + }; + match reflect_primitive(&rect, m).kind { + PrimitiveKind::RoundRect { x, y, .. } => assert_eq!((x, y), (7.0, 4.0)), + _ => panic!("a rectangle stays a rectangle"), + } + // Centred on the half turn's centre (5, 3), whatever its size. + match centre_primitive(&rect, m).kind { + PrimitiveKind::RoundRect { x, y, w, h, .. } => { + assert_eq!((x + 0.5 * w, y + 0.5 * h), (5.0, 3.0)) + } + _ => panic!("a rectangle stays a rectangle"), + } + } } diff --git a/docs/BINDINGS.md b/docs/BINDINGS.md index a111fc87..5a79cd70 100644 --- a/docs/BINDINGS.md +++ b/docs/BINDINGS.md @@ -378,8 +378,9 @@ Pure Go: the C ABI compiled to `wasm32-wasip1` by `tools/build_go_wasm.sh`, embe `Options` struct (pointer fields, nil for the default) is generated by `bindings/codegen/golang.py`. One trace runs on one core, about twice the native single-threaded time. The build rewrites the module's floating-point `select`s as integer -ones (`tools/wasm_float_select.py`) around a wazero amd64 compiler bug that returned the wrong -operand. `go get` cannot run a build step, so users get the module from the mirror repository +ones and its integer remainders as divisions (`tools/wasm_wazero_fixes.py`), around a wazero +amd64 compiler bug that returned the wrong operand of a `select` and an arm64 one that +returned a wrong remainder. `go get` cannot run a build step, so users get the module from the mirror repository `github.com/logolabs/inkvec-go`, which `.github/workflows/go.yml` fills from `packages/go` with the compiled `inkvec.wasm` on a `v*` tag; here the `.wasm` is built, not committed. See `packages/go/README.md`. diff --git a/packages/go/README.md b/packages/go/README.md index 2b2e7feb..1ab4d840 100644 --- a/packages/go/README.md +++ b/packages/go/README.md @@ -141,12 +141,15 @@ always byte-identical to a native build or to the browser build (`@logolabs/inkv rounding can tip a near-tie. The cross-language contract (`bindings/contract/cases.json`) records the hashes per target. -The embedded module has its floating-point `select` instructions rewritten as integer ones -(`tools/wasm_float_select.py` in the Inkvec repository): wazero's amd64 compiler, v1.8.0 -through at least v1.12.0, can return the wrong operand from a floating-point `select`, and -without the rewrite traces came out different from what the WebAssembly semantics give (and -from wazero's own interpreter). The test `TestWazeroFloatSelect` reports whether the bug is -still there. +The embedded module is rewritten around two wazero compiler bugs (`tools/wasm_wazero_fixes.py` +in the Inkvec repository), keeping its semantics exactly. Its floating-point `select` +instructions become integer ones: wazero's amd64 compiler, v1.8.0 through at least v1.12.0, +can return the wrong operand from a floating-point `select`, and without the rewrite traces +came out different from what the WebAssembly semantics give (and from wazero's own +interpreter). Its integer remainders become divisions: wazero's arm64 compiler (v1.12.0) can +compute a remainder from a quotient it never stored, which made traces on Apple silicon stop +with an index out of bounds. The tests `TestWazeroFloatSelect` and `TestWazeroRemainder` +report whether the bugs are still there. ## Building from the Inkvec repository diff --git a/packages/go/inkvec_test.go b/packages/go/inkvec_test.go index 2df4335c..7e106697 100644 --- a/packages/go/inkvec_test.go +++ b/packages/go/inkvec_test.go @@ -430,7 +430,7 @@ func TestRetainMemory(t *testing.T) { } // wazero's compiler and its interpreter give the same SVG. The compiler mis-lowers a -// floating-point select on amd64 (tools/wasm_float_select.py rewrites them away at build +// floating-point select on amd64 (tools/wasm_wazero_fixes.py rewrites them away at build // time); without the rewrite this input comes out differently (1296 bytes either way, // different hashes). Both run on wazero's deterministic clocks: the interpreter is slow // enough to reach the tracer's wall-clock caps, which would make its output depend on speed. diff --git a/packages/go/wazero_bug_test.go b/packages/go/wazero_bug_test.go index 599fd7cf..16820866 100644 --- a/packages/go/wazero_bug_test.go +++ b/packages/go/wazero_bug_test.go @@ -12,7 +12,7 @@ import ( // through v1.12.0): f(a, b, x, y) = select(t, x, g(t) < b) with t = x - y, where // g(v) = v > 1 ? v*0.5 : v+3. With the condition false it returns t instead of x. The // module this package embeds has its floating-point selects rewritten as integer ones by -// tools/wasm_float_select.py, which is why it is not affected. +// tools/wasm_wazero_fixes.py, which is why it is not affected. func selectRepro() []byte { f64c := func(f float64) []byte { b := []byte{0x44} @@ -72,8 +72,94 @@ func TestWazeroFloatSelect(t *testing.T) { } if results["compiler"] != 0.8 { t.Logf("wazero's compiler still mis-lowers a float select (returned %v, want 0.8); "+ - "tools/wasm_float_select.py is still needed", results["compiler"]) + "tools/wasm_wazero_fixes.py is still needed", results["compiler"]) } else { t.Logf("wazero's compiler gets the float select right on this platform") } } + +// remainderRepro is a small module wazero's arm64 compiler gets wrong (v1.12.0): f(a, b, n) +// computes forty values from a, then b rem_u n, then returns the remainder (plus the forty +// values times zero, which keeps them live across it). wazero defines a remainder's register +// twice (a division, then a multiply-subtract) around a branch to its division-by-zero exit; +// the register pressure makes it spill the value in between, the spill is stored after the +// second definition, and the multiply-subtract reloads a quotient that was never stored: +// 1000 rem_u 256 comes out as 1000. The module this package embeds has its remainders +// rewritten as divisions by tools/wasm_wazero_fixes.py, which is why it is not affected. +func remainderRepro() []byte { + const live = 40 + uleb := func(v int) []byte { + var o []byte + for { + b := byte(v & 0x7f) + v >>= 7 + if v == 0 { + return append(o, b) + } + o = append(o, b|0x80) + } + } + i32const := func(v int) []byte { // non-negative values below 8192 + if v < 64 { + return []byte{0x41, byte(v)} + } + return []byte{0x41, byte(v&0x7f) | 0x80, byte(v >> 7)} + } + cat := func(parts ...[]byte) []byte { + var o []byte + for _, p := range parts { + o = append(o, p...) + } + return o + } + rem := 3 + live // locals: a, b, n, the live values, the remainder + code := cat([]byte{1}, uleb(live+1), []byte{0x7f}) + for k := range live { // v_k = a*(2k+3) + 7k+1 + code = cat(code, []byte{0x20, 0}, i32const(2*k+3), []byte{0x6c}, i32const(7*k+1), + []byte{0x6a, 0x21}, uleb(3+k)) + } + code = cat(code, []byte{0x20, 1, 0x20, 2, 0x70, 0x21}, uleb(rem)) // rem = b rem_u n + code = cat(code, i32const(0)) + for k := range live { + code = cat(code, []byte{0x20}, uleb(3+k), []byte{0x73}) // xor v_k + } + code = cat(code, i32const(0), []byte{0x6c, 0x20}, uleb(rem), []byte{0x6a, 0x0b}) + section := func(id byte, body []byte) []byte { return cat([]byte{id}, uleb(len(body)), body) } + return cat([]byte{0, 'a', 's', 'm', 1, 0, 0, 0}, + section(1, []byte{1, 0x60, 3, 0x7f, 0x7f, 0x7f, 1, 0x7f}), + section(3, []byte{1, 0}), + section(7, []byte{1, 1, 'f', 0, 0}), + section(10, cat([]byte{1}, uleb(len(code)), code))) +} + +// TestWazeroRemainder reports whether wazero still has the remainder bug the build works +// around; like TestWazeroFloatSelect, it fails only if the interpreter is wrong. +func TestWazeroRemainder(t *testing.T) { + ctx := context.Background() + results := map[string]uint32{} + for name, rc := range map[string]wazero.RuntimeConfig{ + "compiler": wazero.NewRuntimeConfig(), + "interpreter": wazero.NewRuntimeConfigInterpreter(), + } { + r := wazero.NewRuntimeWithConfig(ctx, rc) + defer r.Close(ctx) + m, err := r.Instantiate(ctx, remainderRepro()) + if err != nil { + t.Fatal(err) + } + res, err := m.ExportedFunction("f").Call(ctx, 5, 1000, 256) + if err != nil { + t.Fatal(err) + } + results[name] = uint32(res[0]) + } + if results["interpreter"] != 232 { + t.Fatalf("the interpreter returned %d, want 1000 rem 256 = 232", results["interpreter"]) + } + if results["compiler"] != 232 { + t.Logf("wazero's compiler still mis-lowers an integer remainder (returned %d, want 232); "+ + "tools/wasm_wazero_fixes.py is still needed", results["compiler"]) + } else { + t.Logf("wazero's compiler gets the remainder right on this platform") + } +} diff --git a/tools/build_go_wasm.sh b/tools/build_go_wasm.sh index 901bd6fa..cb482bf5 100755 --- a/tools/build_go_wasm.sh +++ b/tools/build_go_wasm.sh @@ -18,11 +18,12 @@ # cargo the module, without SIMD (see step 3) # wasm-opt -O3, optional: INKVEC_WASM_OPT, else `wasm-opt` on PATH, else the copy # wasm-pack keeps in its cache; without one the module stays unoptimised -# python tools/wasm_float_select.py rewrites every floating-point `select` as an -# integer one, around a wazero compiler bug on amd64 that makes a float -# `select` return the wrong operand (details in that file). It decodes every -# instruction and does not handle SIMD, which measured neither faster nor -# slower under wazero, so the module is built without it. +# python tools/wasm_wazero_fixes.py rewrites every floating-point `select` as an +# integer one and every integer remainder as a division, around two wazero +# compiler bugs: a float `select` can return the wrong operand on amd64, and +# a remainder can come out as garbage on arm64 (details in that file). It +# decodes every instruction and does not handle SIMD, which measured neither +# faster nor slower under wazero, so the module is built without it. # # Prerequisites: rustup target add wasm32-wasip1; Python 3 (PYTHON, else python3 / python). # @@ -83,7 +84,7 @@ else fi PY="${PYTHON:-$(command -v python3 || command -v python)}" -echo "==> $PY tools/wasm_float_select.py" -"$PY" "$ROOT/tools/wasm_float_select.py" "$OPTIMISED" "$OUT" +echo "==> $PY tools/wasm_wazero_fixes.py" +"$PY" "$ROOT/tools/wasm_wazero_fixes.py" "$OPTIMISED" "$OUT" ls -l "$BUILT" "$OUT" diff --git a/tools/wasm_float_select.py b/tools/wasm_wazero_fixes.py similarity index 76% rename from tools/wasm_float_select.py rename to tools/wasm_wazero_fixes.py index 54abe390..be2cf125 100644 --- a/tools/wasm_float_select.py +++ b/tools/wasm_wazero_fixes.py @@ -1,16 +1,20 @@ -"""Rewrite every floating-point `select` in a WebAssembly module as an integer `select`. +"""Rewrite a WebAssembly module around two wazero compiler bugs, keeping its semantics. - python tools/wasm_float_select.py in.wasm out.wasm + python tools/wasm_wazero_fixes.py in.wasm out.wasm -Works around a wazero compiler bug on amd64 (seen in v1.8.0 through v1.12.0, the latest -release). wazero lowers an f32/f64/v128 `select` to a pseudo-instruction, xmmCMov, that it -tells its register allocator *defines* the destination register -- but a conditional move -leaves the destination unchanged when the condition is false, so it also reads it. Under -register pressure (a value live across a call is enough) the allocator gives the destination -a register that never received the false operand, and `select` returns the true operand, or -whatever else sat there, when the condition is false. The integer `select` (cmove) is -modelled correctly. A minimal case, which wazero's interpreter and every other engine run -correctly: +Every floating-point `select` becomes an integer `select`, and every integer remainder +(`rem_u`, `rem_s`) becomes a division, a multiplication and a subtraction. Both bugs are in +wazero's compiler (v1.12.0, the latest release, has both); its interpreter, and every other +engine, run the original module correctly. + +1. Floating-point `select`, on amd64 (seen in v1.8.0 through v1.12.0). wazero lowers an +f32/f64/v128 `select` to a pseudo-instruction, xmmCMov, that it tells its register allocator +*defines* the destination register -- but a conditional move leaves the destination +unchanged when the condition is false, so it also reads it. Under register pressure (a value +live across a call is enough) the allocator gives the destination a register that never +received the false operand, and `select` returns the true operand, or whatever else sat +there, when the condition is false. The integer `select` (cmove) is modelled correctly. A +minimal case, which wazero's interpreter and every other engine run correctly: t = x - y; c = f(t) < b; return select(t, x, c) -- returns t when c is false @@ -22,7 +26,26 @@ select(a, b, c) -> f64.reinterpret_i64(select(i64.reinterpret_f64(a), i64.reinterpret_f64(b), c)) -done in place with two scratch locals per function (the condition and the second operand). +done in place with scratch locals (the condition and the second operand). + +2. Integer remainder, on arm64. wazero lowers `rem` to `udiv rd, rn, rm` (or `sdiv`), a +branch to the division-by-zero exit, and `msub rd, rd, rm, rn` -- two definitions of the +result register. Its register allocator keeps one defining instruction per value and stores +a spilled value after it, which for `rd` is the `msub`. When the exit path's temporary +register forces `rd` out between the two (the allocator evicts the value used furthest +ahead, which a remainder used later is), `msub` reloads the quotient from a stack slot +nothing has written yet, and the remainder comes out as `rn - garbage * rm`. A minimal +case: forty values live across one `i32.rem_u`, and `1000 rem_u 256` returns 1000. In +Inkvec it gave the symmetry pass an index of 4294337216 into a 256-point boundary (macOS, +the Go package's tests). The rewrite, with scratch locals for `a` and `b`: + + rem_u(a, b) -> a - div_u(a, b) * b + rem_s(a, b) -> a - div_s(a, b') * b' with b' = (b == -1 ? 1 : b) + +is exact: division by zero traps either way, and `b'` keeps `rem_s(MIN, -1) = 0` (which +`div_s` would trap on) -- any `a` remainder 1 is 0, as remainder -1 is. A division is +lowered with one definition of its result, so it is not affected. + Finding which `select`s are floating-point needs the operand types, so every function body is decoded and its operand stack typed. The decoder covers what rustc emits for wasm32 without SIMD (MVP, sign extension, saturating truncation, bulk memory, reference types, @@ -134,6 +157,28 @@ def uleb(v: int) -> bytes: # (float type) -> (to-integer reinterpret, back-to-float reinterpret) REINTERPRET = {F64: (0xBD, 0xBF), F32: (0xBC, 0xBE)} +# integer remainder opcode -> (operand type, signed) +REMAINDER = {0x6F: (I32, True), 0x70: (I32, False), 0x81: (I64, True), 0x82: (I64, False)} +# integer type -> (const, eq, div_s, div_u, mul, sub) +INT_OPS = {I32: (0x41, 0x46, 0x6D, 0x6E, 0x6C, 0x6B), I64: (0x42, 0x51, 0x7F, 0x80, 0x7E, 0x7D)} + + +def remainder(t: int, signed: bool, a: int, b: int) -> bytes: + """`rem(a, b)`, its operands on the stack, as `a - div(a, b') * b'` in scratch locals `a` + and `b`; `b'` is `b`, or 1 where a signed remainder's `b` is -1 (see the docstring).""" + const, eq, div_s, div_u, mul, sub = INT_OPS[t] + code = bytearray(b"\x21" + uleb(b) + b"\x22" + uleb(a) + b"\x20" + uleb(a)) + if signed: + # b = select(1, b, b == -1) + code += bytes([const, 0x01]) + b"\x20" + uleb(b) + b"\x20" + uleb(b) + code += bytes([const, 0x7F, eq, 0x1B]) + b"\x22" + uleb(b) + code.append(div_s) + else: + code += b"\x20" + uleb(b) + code.append(div_u) + code += b"\x20" + uleb(b) + bytes([mul, sub]) + return bytes(code) + class Module: def __init__(self, data: bytes): @@ -228,8 +273,9 @@ def block_type(self, r: Reader) -> tuple[tuple[int, ...], tuple[int, ...]]: return self.types[r.s()] -def rewrite_body(m: Module, body: bytes, func_type: tuple) -> tuple[bytes, int]: - """One function body with its float selects rewritten, and how many there were.""" +def rewrite_body(m: Module, body: bytes, func_type: tuple) -> tuple[bytes, int, int]: + """One function body with its float selects and integer remainders rewritten, and how + many of each there were.""" params, results = func_type r = Reader(body) local_groups = [] @@ -241,11 +287,14 @@ def rewrite_body(m: Module, body: bytes, func_type: tuple) -> tuple[bytes, int]: locals_.extend([t] * n) code_start = r.p base = len(locals_) - cond_local, f64_local, f32_local = base, base + 1, base + 2 + # Scratch locals, appended to the function's own: two i32 (a select's condition, or a + # remainder's operands), an f64, an f32 and two i64. + cond_local, f64_local, f32_local = base, base + 2, base + 3 + int_scratch = {I32: (base, base + 1), I64: (base + 4, base + 5)} out = bytearray() copied = code_start # body[copied:r.p] is still to be copied verbatim - rewrites = 0 + selects = remainders = 0 stack: list = [] # control frames: [height, params, results, unreachable] frames = [[0, (), results, False]] @@ -272,7 +321,18 @@ def unreachable(): at = r.p op = d[r.p] r.p += 1 - if op in NUMERIC: + if op in REMAINDER: + # Rewritten in unreachable code too: the opcode gives the types, and the + # replacement is as valid on a polymorphic stack as the original. + t, signed = REMAINDER[op] + pop(t) + pop(t) + stack.append(t) + out += d[copied:at] + out += remainder(t, signed, *int_scratch[t]) + copied = r.p + remainders += 1 + elif op in NUMERIC: pops, pushes = NUMERIC[op] for t in reversed(pops): pop(t) @@ -334,7 +394,7 @@ def unreachable(): out.append(0x1B) out.append(to_float) copied = r.p - rewrites += 1 + selects += 1 elif op == 0x10: p, res = m.types[m.funcs[r.u32()]] for t in reversed(p): @@ -462,14 +522,14 @@ def unreachable(): f"calls are not handled; see the module docstring)") if frames: raise SystemExit("function body ends inside a block") - if not rewrites: - return body, 0 + if not selects and not remainders: + return body, 0, 0 out += d[copied:] - header = bytearray(uleb(len(local_groups) + 3)) + header = bytearray(uleb(len(local_groups) + 4)) for count, t in local_groups: header += uleb(count) + bytes([t]) - header += b"\x01\x7f\x01\x7c\x01\x7d" # the condition, an f64 and an f32 scratch local - return bytes(header) + bytes(out), rewrites + header += b"\x02\x7f\x01\x7c\x01\x7d\x02\x7e" # the scratch locals, as above + return bytes(header) + bytes(out), selects, remainders def main() -> int: @@ -479,7 +539,7 @@ def main() -> int: data = open(sys.argv[1], "rb").read() m = Module(data) out = bytearray(data[:8]) - total = 0 + selects = remainders = 0 n_imported = len(m.funcs) - sum( Reader(data, s).u32() for sid, s, _ in m.sections if sid == 10 ) @@ -496,14 +556,16 @@ def main() -> int: size = r.u32() body = data[r.p : r.p + size] r.p += size - new, k = rewrite_body(m, body, m.types[m.funcs[n_imported + i]]) - total += k + new, k, j = rewrite_body(m, body, m.types[m.funcs[n_imported + i]]) + selects += k + remainders += j bodies += uleb(len(new)) + new out.append(10) out += uleb(len(bodies)) out += bodies open(sys.argv[2], "wb").write(out) - print(f"rewrote {total} floating-point select(s) as integer selects") + print(f"rewrote {selects} floating-point select(s) as integer selects and {remainders} " + f"integer remainder(s) as divisions") return 0