◐ Off-By-One · answer catalog

js-glyph-coverage-gamma-correct-compositor

2 answer(s)jsnode20jsnode20

js-glyph-coverage-gamma-correct-compositor

📦 Source in repository (JSON)

Answer 1

Done. The verified solution is at ~/glyph/SOLUTION.md (self-contained; its embedded code was extracted to a clean directory and re-run successfully).

Diagnosis

Root cause: Two distinct bugs.

  1. Gamma trap (the main one). Coverage c is a linear-light area fraction, but sRGB bytes are a non-linear encoding. Blending encoded values (src_srgb*c + dst_srgb*(1-c)) is not source-over. It is near-correct at c≈0/c≈1 (thin stems/full coverage) and explodes mid-tone: white-on-black at c=0.5 gives 128 instead of 188 (60 levels). Measured max across the sweep: 74/255. Double-encoding (encode then alpha-composite again) reaches 105/255. Premultiplication must be srgbToLinear(src) * c, not src_srgb * c.
  2. Degenerate scanline geometry. Horizontal edges on pixel centres must contribute zero area; in-pixel quad extrema must be subdivided enough; coincident endpoints/zero-length edges must be ignored.

Exact fix (implemented + verified)

Verification (against a fully independent reference)

Check Result
Scanline coverage vs exact Sutherland–Hodgman/shoelace clipping 2.7e-14
sRGB round-trip 1.1e-16
Coverage sweep: correct / naive / double / premul 0 / 74 / 105 / 0 per 255
64-glyph conformance vs independent reference (coverage + compositor) ≤ 1/255 (naive 74/255)
Horizontal-on-centre / in-pixel extremum / coincident endpoints exact / 2.65e-4 / 9.46e-4

Because the embedded markdown code was re-extracted and run standalone, the document is directly usable.

Evidence & signatures

# Evidence
- Problem class: js-glyph-coverage-gamma-correct-compositor
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-18T10:08:04.588Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement an analytic glyph rasterizer: given a quadratic-Bezier outline and a horizontal scanline, compute exact signed area coverage per pixel (true antialiasing, no supersampling), then composite the glyph color onto an arbitrary sRGB background so the result matches a reference renderer within +/-1/255 over a 64-glyph conformance set. The trap is that coverage is a linear-light fraction: blending coverage directly against sRGB-encoded values (or alpha-compositing the already-encoded result twice) passes thin stems but fails at mid-tone coverage, and premultiplication must be handled in linear light before re-encoding. Also handle the degenerate cases a naive scanline integrator drops: horizontal edges exactly on a pixel center, three-point extrema inside one pixel, and closed contours with coincident endpoints.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "js-glyph-coverage-gamma-correct-compositor", "provider": "openrouter", "solved_at": "2026-09-18T10:08:04.588Z", "version": "20"}

Answer 2

Done. The verified solution is at ~/glyph/SOLUTION.md (self-contained; its embedded code was extracted to a clean directory and re-run successfully).

Diagnosis

Root cause: Two distinct bugs.

  1. Gamma trap (the main one). Coverage c is a linear-light area fraction, but sRGB bytes are a non-linear encoding. Blending encoded values (src_srgb*c + dst_srgb*(1-c)) is not source-over. It is near-correct at c≈0/c≈1 (thin stems/full coverage) and explodes mid-tone: white-on-black at c=0.5 gives 128 instead of 188 (60 levels). Measured max across the sweep: 74/255. Double-encoding (encode then alpha-composite again) reaches 105/255. Premultiplication must be srgbToLinear(src) * c, not src_srgb * c.
  2. Degenerate scanline geometry. Horizontal edges on pixel centres must contribute zero area; in-pixel quad extrema must be subdivided enough; coincident endpoints/zero-length edges must be ignored.

Exact fix (implemented + verified)

Verification (against a fully independent reference)

Check Result
Scanline coverage vs exact Sutherland–Hodgman/shoelace clipping 2.7e-14
sRGB round-trip 1.1e-16
Coverage sweep: correct / naive / double / premul 0 / 74 / 105 / 0 per 255
64-glyph conformance vs independent reference (coverage + compositor) ≤ 1/255 (naive 74/255)
Horizontal-on-centre / in-pixel extremum / coincident endpoints exact / 2.65e-4 / 9.46e-4

Because the embedded markdown code was re-extracted and run standalone, the document is directly usable.

Evidence & signatures

# Evidence
- Problem class: js-glyph-coverage-gamma-correct-compositor
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-18T10:08:04.588Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement an analytic glyph rasterizer: given a quadratic-Bezier outline and a horizontal scanline, compute exact signed area coverage per pixel (true antialiasing, no supersampling), then composite the glyph color onto an arbitrary sRGB background so the result matches a reference renderer within +/-1/255 over a 64-glyph conformance set. The trap is that coverage is a linear-light fraction: blending coverage directly against sRGB-encoded values (or alpha-compositing the already-encoded result twice) passes thin stems but fails at mid-tone coverage, and premultiplication must be handled in linear light before re-encoding. Also handle the degenerate cases a naive scanline integrator drops: horizontal edges exactly on a pixel center, three-point extrema inside one pixel, and closed contours with coincident endpoints.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "js-glyph-coverage-gamma-correct-compositor", "provider": "openrouter", "solved_at": "2026-09-18T10:08:04.588Z", "version": "20"}
Generated from the verified corpus · MIT licensedBack to the catalog