Ridge: near-cover check measures a notch against 8 columns each side; scrim kept over the settled still

The body-end check compared each column only with its neighbour, so an early
dissolve that eased over a few columns passed it. It now refuses a column whose
body ends more than 40 px short of the deepest end within 8 columns either side.
The old Vienna bake (12e733dc9f09) fails it at column 1251 (42 px); the committed
rebake (f25b2314a20c) passes in all six frames. Mutants added for the window.

The night scrim was dropped when the scene settled to a still frame; it is now
rebuilt over the canvas after the planes are painted (gradient with planes, flat
without), with a test for both.

Recovered from Nibu's uncommitted work at the 2026-11-17 brake.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nibu 2026-11-17 15:49:14 +01:00
commit 24e1a1c3e2
6 changed files with 66 additions and 30 deletions

View file

@ -27,7 +27,8 @@ LUMA_SKY = 0.5 # the flat mid-grey sky the planes are co
LUMA_COVER = 0.5 # composite coverage (1 - prod(1 - alpha)) at or above which a pixel is measured
NEAR_CREST_ALPHA = 64 # alpha (0..255) at which a near-plane pixel counts as the ridge's body: the crest of a column is its first such row
NEAR_BODY_ALPHA = 40 # alpha at which a near-plane pixel still counts as body when finding where a column's body ends
NEAR_END_STEP_PX = 24 # the most the row where the body ends may differ between neighbouring columns: a step is where the near plane dissolves early over a few columns (Vienna had 55 px at x 1252)
NEAR_END_DROP_PX = 40 # how far the row where the body ends may fall short of the deepest end within NEAR_END_WINDOW columns: a notch where the near plane dissolves early over a few columns (Vienna had 60 at x 1252-1280; natural terrain 22)
NEAR_END_WINDOW = 8
NEAR_HOLE_ALPHA = 24 # alpha below which a pixel under the crest is a hole (the sky would show through the ground in front of the eye)
SHEET_SKY = (0.80, 0.86, 0.94) # the flat sky of the contact sheet
ATTRIBUTION = dict(
@ -104,14 +105,15 @@ def luma_range(decoded):
return float(y.min()), float(y.max())
def check_near_cover(near, horizon_row):
"""Refuse a near plane (RGBA uint8) that does not cover the ground in front of the eye: every column must have a crest (an alpha >= NEAR_CREST_ALPHA row), and from the crest down to the horizon row no pixel may be a hole (alpha < NEAR_HOLE_ALPHA), and the row where the body ends may not step more than NEAR_END_STEP_PX between neighbouring columns.
"""Refuse a near plane (RGBA uint8) that does not cover the ground in front of the eye: every column must have a crest (an alpha >= NEAR_CREST_ALPHA row), and from the crest down to the horizon row no pixel may be a hole (alpha < NEAR_HOLE_ALPHA), and the row where the body ends may not fall more than NEAR_END_DROP_PX short of the deepest end within NEAR_END_WINDOW columns.
A transparent column, or a wedge of holes under a crest, shows the sky through the foreground; the farther planes may leave columns empty (a place with no ridge at that distance), the nearest may not."""
a = near[..., 3]; h, w = a.shape; crest = np.argmax(a >= NEAR_CREST_ALPHA, axis=0); none = ~(a >= NEAR_CREST_ALPHA).any(0)
if none.any(): raise ValueError(f'near plane: {int(none.sum())} fully transparent columns, first at x={int(np.argmax(none))}')
rows = np.arange(h)[:, None]; hole = (a < NEAR_HOLE_ALPHA) & (rows >= crest[None, :]) & (rows < int(horizon_row))
if hole.any(): x = int(np.argmax(hole.any(0))); raise ValueError(f'near plane: {int(hole.any(0).sum())} columns have a hole between the crest and the horizon, first at x={x} row {int(np.argmax(hole[:, x]))}')
body = a >= NEAR_BODY_ALPHA; end = h - 1 - np.argmax(body[::-1], axis=0); step = np.abs(np.diff(end)) # the body's last row, per column: the dissolve below the horizon has no edge of its own
if (step > NEAR_END_STEP_PX).any(): x = int(np.argmax(step > NEAR_END_STEP_PX)); raise ValueError(f'near plane: the body ends at row {int(end[x])} in column {x} and row {int(end[x + 1])} in column {x + 1}, a step over {NEAR_END_STEP_PX} px')
body = a >= NEAR_BODY_ALPHA; end = h - 1 - np.argmax(body[::-1], axis=0) # the body's last row, per column: the dissolve below the horizon has no edge of its own
deepest = np.lib.stride_tricks.sliding_window_view(np.pad(end, NEAR_END_WINDOW, mode='edge'), 2 * NEAR_END_WINDOW + 1).max(-1); notch = deepest - end
if (notch > NEAR_END_DROP_PX).any(): x = int(np.argmax(notch > NEAR_END_DROP_PX)); raise ValueError(f'near plane: the body ends {int(notch[x])} px short of its neighbours in column {x} (row {int(end[x])}), a notch over {NEAR_END_DROP_PX} px')
def check_budget(frame_bytes, plane_w, plane_h):
"""Refuse a bake the runtime could not hold: any two adjacent frames (night with dawn too) above PAIR_BYTES_MAX, or the planes of a frame pair decoding above DECODE_BYTES_MAX."""

View file

@ -91,7 +91,8 @@ M = {
'near foot step not eased (the widest foot in the margin dropped)': ('ridge.py', 'return np.maximum(blurw(slide_max(foot, NEAR_FOOT_MARGIN_PX), NEAR_FOOT_MARGIN_PX / 2.0), foot)', 'return foot'),
'near foot widest-in-margin without the easing': ('ridge.py', 'blurw(slide_max(foot, NEAR_FOOT_MARGIN_PX), NEAR_FOOT_MARGIN_PX / 2.0)', 'slide_max(foot, NEAR_FOOT_MARGIN_PX)'),
'near foot margin of one column': ('ridge.py', 'NEAR_FOOT_MARGIN_PX = 48', 'NEAR_FOOT_MARGIN_PX = 1'),
'near cover allows any step in where the body ends': ('export.py', 'NEAR_END_STEP_PX = 24 ', 'NEAR_END_STEP_PX = 600 '),
'near cover allows any notch in where the body ends': ('export.py', 'NEAR_END_DROP_PX = 40 ', 'NEAR_END_DROP_PX = 600 '),
'near cover looks at no neighbour (window of zero)': ('export.py', 'NEAR_END_WINDOW = 8', 'NEAR_END_WINDOW = 0'),
'near cover measures the body end at alpha above any pixel': ('export.py', 'NEAR_BODY_ALPHA = 40 ', 'NEAR_BODY_ALPHA = 256 '),
'near cover looks below the horizon too': ('export.py', '& (rows < int(horizon_row))', '& (rows < h)'),
'near cover crest threshold above any alpha': ('export.py', 'NEAR_CREST_ALPHA = 64 ', 'NEAR_CREST_ALPHA = 256 '),

View file

@ -461,9 +461,9 @@ def test_near_foot_reaches_the_horizon_and_a_step_in_it_is_eased_not_cut():
assert np.abs(np.diff(f)).max() < 8.0, np.abs(np.diff(f)).max()
assert ridge.near_foot(np.full(400, 1.0), np.full(400, 5.0)).min() >= 5.0 * ridge.PXDEG_V / ridge.FADE_AT - 1e-6
ok = np.zeros((60, 40, 4), np.uint8); ok[10:, :, 3] = 200; export.check_near_cover(ok, 30.0)
early = ok.copy(); early[25:, 30:, 3] = 0
try: export.check_near_cover(early, 20.0)
except ValueError as e: assert 'step' in str(e), str(e)
early = ok.copy(); early[15:, 30:, 3] = 0
try: export.check_near_cover(early, 12.0)
except ValueError as e: assert 'notch' in str(e), str(e)
else: assert False, 'a near plane whose body ends early in a few columns was accepted'
def test_write_bake_refuses_a_near_plane_with_a_wedge_and_lets_the_other_planes_have_empty_columns():
"""The refusal is at the writer, on the decoded near plane of every frame; the far and mid planes may have transparent columns (a place with no ridge at that distance in part of the window)."""