Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video-loop): a made frame loses its outline only where its edge is also lighter than its neighbours' rank by rank
Soohong Kim282d8a04112d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e771fb6..7e5f5b8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased+- **Behavior change: a frame the jump repair or the cycle alignment makes loses its outline only where it is lighter than its neighbours' edge rank by rank, too.** The fault `outline` read the made frame's edge against one dark line (a solid pixel under 70/255 within 2 px, `outline_loss` over 5 %), so art outlined in a mid tone — a brown or a painted line whose tone lies about 70/255 — had its line on the near side of the line in the drawings and on the far side wherever the flow softened it by a few tones: every frame made between two such drawings read as melted, and the repair and `--between auto` refused it. `rife.outline_shift(made, a, b)` reads the same edge, each pixel by how dark the darkest solid pixel within 2 px of it is, in order, against the lighter neighbour's edge at the same rank, and counts it where it is lighter by more than 50/255 (`rife.OUTLINE_SHIFT`). The fault `outline` now needs both over 5 %: a softened line is lighter by less at every rank and is kept, whatever tone the art outlines in; a line gone to the fill beside it is lighter by its whole contrast and is lost. Art outlined near-black reads the same on both, and art outlined lighter than 70/255 is still not judged by the outline. On the two-tone walker of the tests, the drawing between with its line 20 to 45 tones lighter reads `outline_loss` 57 % and `outline_shift` 0 (refused before, kept now); with its legs' line turned to fill, 24 % and 48 % (refused). A line less than 50/255 darker than the fill it melts into is not read as lost. The jump repair's proposals and the alignment's `smear` rows carry `outline_shift`, and the alignment's warning line names it; `video-loop-compare` and `video-loop-repair` keep their versioned measures, `rife.smear` alone, and judge the outline on the dark line as before ([loop-repair](docs/loop-repair.md) section 4).+- **Tests** — `tests/video/test_outline_shift.py` (a mid-tone line drawn lighter is kept and one gone to fill is lost; the versioned measures judge on the dark line alone; art outlined near-black reads the same on both; a frame with no edge reads 0; the jump repair takes a lighter line and refuses one gone to fill; the alignment keeps the one and takes the nearer frame for the other; through RIFE, a short step of two-tone legs keeps its outline and legs crossing far lose it on both), `tests/video/test_cycle_align.py` (the stand-in melt takes the dark foot into a pale fill: into the body's blue, 42/255 lighter, it is no further than a softened line).- **Behavior change: every RIFE call runs the binary's test-time augmentation, `-x -z` (`rife.TTA`).** `-x` runs the pair as its eight flips and transposes and `-z` both ways in time, and the results are averaged, so a frame made from a to b is the one made from b to a. Neither rife-ncnn-vulkan nor Practical-RIFE has released a model since 2024; the model stays `rife-v4.6`. On the synthetic pairs of `tests/video/test_rife.py`, `-x -z` is the one setting of the four (none, `-x`, `-z`, both) that makes legs crossing far the same both ways with no ghost, and it keeps the most of a short step's outline. On the Mac's GPU it adds a tenth to half a second a frame; on Modal's CPU it costs 15 to 19 times a plain call's CPU — 12 to 30 s a 280 × 520 frame on two cores, 44 to 61 s a 960 × 960 frame. `SPRITE_GEN_RIFE_TTA` names another (`-x`, `-z`, both, or empty for none; anything else is refused before a frame is asked for), for a CPU-only machine that aligns sets. The interpolator a report names (`jump_repair.interpolator`, the alignment's `interpolator`) carries `tta`. A call past 600 s (120 s before) is stopped and reported as a RIFE that failed, where it used to end in Python's own timeout ([loop-repair](docs/loop-repair.md) section 1, "Which setting").- **Tests** — `tests/video/test_rife.py` (every call asks for `-x -z` and never `-g`; `SPRITE_GEN_RIFE_TTA` names either mode, both or none, and anything else is refused; a call past its time is a RIFE that failed; the real-RIFE pairs read with the augmentation: legs crossing far melt the same both ways and leave no ghost, a short step stays outlined, legs walking through each other add no dark where they cross; a refused repair names its faults whole, not by the first of a sorted list), `tests/conftest.py` (every test runs the engine's own augmentation, whatever the calling shell sets).- **New: the flow reading of a walk loop (`repair.flow_scores`, `repair.flow_targets`) — the frames that leave the way a part is going, by direction and speed, not by the size of a step.** Each frame is a point of the loop's path, its coverage and shading blurred by 3 % of the body height, read in nine windows of the union box: how far it lies off the straight line between its neighbours (the second difference, the acceleration), in that window's usual steps. A clip filmed on twos or threes is read on its drawings, the hold `held.measure`'s. `flow_targets` takes the worst frame at or over 3.0 first and names the fewest frames around it — one, then two — whose straight in-between brings every frame it touches under 3.0, never made from a frame taken before; what two cannot bring under is `unfixed`. On the synthetic walks an ordinary loop reads at most 1.65 where the jump score names 8 to 18 steps, and a tail flung 5 % of the body height against its sway reads 2.8 to 4.1. The methods weighed beside it — interpolation error with RIFE, optical flow, a periodic fit, a Kalman predictor, Savitzky–Golay, a Hampel test — and their sources are in docs/loop-repair.md section 2, "The flow reading".diff --git a/docs/loop-repair.md b/docs/loop-repair.mdindex e6f920d..7ad41af 100644--- a/docs/loop-repair.md+++ b/docs/loop-repair.md@@ -282,8 +282,9 @@ delivered as filmed, named (section 4).The flow is read again after each group, on the union box of the loop as filmed; a refused group isnever tried again in that run, and nothing is made from or beside it. The circular last/firstadjacency rule and the call budget hold across the wrap. The shared `interpolation_quality` policy-is the same one used by cycle alignment: `dark_excess > 0.001` or-`outline_loss > 0.05` rejects a proposal, and so does a proposal that is a ghost on its own+is the same one used by cycle alignment: `dark_excess > 0.001`, or `outline_loss` and+`outline_shift` both over 0.05 (an outline lost by the dark line and lighter than the neighbours'+edge rank by rank, section 4), rejects a proposal, and so does a proposal that is a ghost on its owncoverage (`ghost` over 0.003, read where the loop's ghost screen reads it, section 4). An emptyproposal is also rejected. One made frame at fault refuses its whole group. A proposal thatcross-fades two drawings inside its silhouette (`crossfade` over 0.015, section 4) is taken all the@@ -793,8 +794,32 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-counts), the share with no dark solid pixel within 2 px, beyond the less outlinedneighbour's, as a fraction of its edge. Legs that crossed too far for the flow melt into oneshape of fill and lose their outline where they meet the air; a limb left as a ghost has none.- Over 5 % it is the fault `outline`. A step the flow follows keeps its outline; a figure drawn- without outlines loses none against neighbours that have none, and is not judged by it.+ Over 5 % it is the fault `outline` — where `outline_shift` is over 5 % too (below). A step the+ flow follows keeps its outline; a figure drawn without outlines loses none against neighbours+ that have none, and is not judged by it.+ - `outline_shift` (`rife.outline_shift`): the same edge read against the neighbours rank by rank —+ each pixel of the frame's coverage edge by how dark the darkest solid pixel within 2 px of it is,+ in order, against the lighter neighbour's edge at the same rank, counted where it is lighter by+ more than **50/255** (`rife.OUTLINE_SHIFT`), as a fraction of its edge. The fault `outline` needs+ both readings over 5 %. The dark line alone reads an art outlined in a mid tone — a brown or a+ painted line whose tone lies about 70/255 — as lost wherever the flow softened it: a line a few+ tones lighter falls on the far side of 70/255 all along the figure, and a frame made between two+ such drawings reads as melted however cleanly the flow followed them. Rank by rank the softened line is lighter by less than 50/255+ and is kept, whatever tone the art outlines in; a line gone to the fill beside it is lighter by the+ line's whole contrast and is lost. Art outlined near-black reads the same on both, and the fault+ is the dark line's as before; art outlined lighter than 70/255 is still not judged by the outline+ (the cross-fade reading below names what it hides). On the synthetic walker of+ `tests/video/test_outline_shift.py`, outlined in two tones either side of 70/255 (luma 57 and 77),+ the drawing between with its line drawn 20 to 45 tones lighter reads `outline_loss` 57 % and+ `outline_shift` 0; the same drawing with its legs' line turned to fill reads 24 % and 48 %.+ The 50/255 is a reference, like the 5 %, not a measured optimum: it lies between the lift a+ softened line takes and the lift of a line gone to fill, and a frame near either is worth a look.+ What it does not see: a line less than 50/255 darker than the fill it melts into (a dark grey+ shoe on a mid blue) is no further from its neighbours' than a softened line, and is not read as+ lost.+ The jump repair (section 2) and the alignment read it on every frame they make; the versioned+ measures of `video-loop-compare` and `video-loop-repair` do not, and judge the outline on the+ dark line alone, as before.- `partial_excess`: part-covered pixels beyond the more ragged neighbour's, as a fraction ofits solid pixels. Reported, not judged: a melted frame and a clean one read alike on it, and aframe made beside a filmed ghost carries the ghost without adding to it — the ghost screendiff --git a/sprite_gen/video/align.py b/sprite_gen/video/align.pyindex c44996f..8e289c7 100644--- a/sprite_gen/video/align.py+++ b/sprite_gen/video/align.py@@ -284,7 +284,8 @@ def resample(frames: list[Image.Image], length: int, interpolate: rife_mod.Interraise ValueError(f"frame {k} of {length} falls between source frames and no interpolator is available")a, b = frames[i % count], frames[(i + 1) % count]made = interpolate(a, b, frac)- measure = {**rife_mod.smear(made, a, b), "ghost": rife_mod.ghost(made) if screen["reads"] else None,+ measure = {**rife_mod.smear(made, a, b), "outline_shift": rife_mod.outline_shift(made, a, b),+ "ghost": rife_mod.ghost(made) if screen["reads"] else None,"crossfade": rife_mod.crossfade(made, a, b, frac)}wrong = faults(measure)keep = between == "rife" or not wrong@@ -737,8 +738,9 @@ def _fault_line(name: str, m: dict[str, Any], ghost: dict[str, Any] | None = Nonother a filmed ghost too (`_ghost_line` names both)."""what = [*([f"has {100 * m['dark_excess']:.2f} % more dark pixels inside the body than either source frame beside it — a smear"]if "smear" in m["faults"] else []),- *([f"lost its outline on {100 * m['outline_loss']:.2f} % of its edge beyond either source frame beside it — "- "a melted or ghost limb"] if "outline" in m["faults"] else []),+ *([f"lost its outline on {100 * m['outline_loss']:.2f} % of its edge beyond either source frame beside it, "+ f"{100 * m['outline_shift']:.2f} % lighter than theirs at the same rank — a melted or ghost limb"]+ if "outline" in m["faults"] else []),*([f"carries {_ghost_words(m['ghost'])} — a ghost"] if "ghost" in m["faults"] else [])]if m["method"] == "nearest":return (f"{name}: frame {m['at']}: RIFE's frame " + "; ".join(what)diff --git a/sprite_gen/video/interpolation_quality.py b/sprite_gen/video/interpolation_quality.pyindex 1d43be8..6f51fb5 100644--- a/sprite_gen/video/interpolation_quality.py+++ b/sprite_gen/video/interpolation_quality.py@@ -1,7 +1,7 @@# SPDX-License-Identifier: Apache-2.0"""The shared acceptance policy for the frames repair and cycle alignment deliver.-Measurements live in ``rife`` (``smear``, ``ghost``, ``crossfade``); these are the existing alignment+Measurements live in ``rife`` (``smear``, ``outline_shift``, ``ghost``, ``crossfade``); these are the existing alignmentbounds, also applied before replacing a filmed middle frame during jump repair, the ghost screenevery frame a loop path delivers passes, filmed or made, the clean frame a filmed ghost gives wayto, and the cross-fade a made frame is named for, kept (docs/loop-repair.md section 4).@@ -31,9 +31,15 @@ CROSSFADE_LOOK = 0.015def faults(measure: dict[str, float | None]) -> list[str]:"""Smear inside the body or outline loss beyond both neighbours, and a ghost on the frame'sown coverage where the measure carries its reading (`ghost`; None where the screen does not- read the loop, `ghost_screen`)."""+ read the loop, `ghost_screen`). Where the measure also carries `outline_shift` (the frames the+ jump repair and the cycle alignment make), an outline is lost only where both readings say so:+ a line outlined in a mid tone, which the dark line (`rife.DARK_LUMA`) reads as lost wherever the+ flow softened it, is kept unless its edge went lighter than the neighbours' by more than+ `rife.OUTLINE_SHIFT` (docs/loop-repair.md section 4). The versioned measures of+ `video-loop-compare` and `video-loop-repair` carry `rife.smear` alone and judge as before."""+ outline = min(measure["outline_loss"], measure.get("outline_shift", measure["outline_loss"]))return ([*(["smear"] if measure["dark_excess"] > SMEAR_WARN else []),- *(["outline"] if measure["outline_loss"] > OUTLINE_WARN else []),+ *(["outline"] if outline > OUTLINE_WARN else []),*(["ghost"] if (measure.get("ghost") or 0.0) > GHOST_WARN else [])])diff --git a/sprite_gen/video/repair.py b/sprite_gen/video/repair.pyindex 7c041e0..53d6df5 100644--- a/sprite_gen/video/repair.py+++ b/sprite_gen/video/repair.py@@ -31,7 +31,7 @@ from PIL import Imagefrom sprite_gen._deps import npfrom sprite_gen.video import held as held_mod-from sprite_gen.video.rife import Interpolate, crossfade, ghost, smear+from sprite_gen.video.rife import Interpolate, crossfade, ghost, outline_shift, smearfrom sprite_gen.video.interpolation_quality import clean_beside, faults, ghost_screen, looks# Interpolation calls at most per loop, a drawing made one call, including rejected proposals. A loop that needs more is@@ -354,7 +354,8 @@ def _give_way(frames: list[Image.Image], j: int, ghosts: dict[int, float], intermade = interpolate(frames[a], frames[b], 0.5)if made.mode != "RGBA" or made.size != frames[j].size:raise ValueError("repair interpolator must return an RGBA frame of the original size")- measure = {**smear(made, frames[a], frames[b]), "ghost": ghost(made), "crossfade": crossfade(made, frames[a], frames[b], 0.5)}+ measure = {**smear(made, frames[a], frames[b]), "outline_shift": outline_shift(made, frames[a], frames[b]),+ "ghost": ghost(made), "crossfade": crossfade(made, frames[a], frames[b], 0.5)}wrong = faults(measure)if not made.getchannel("A").getbbox():wrong.append("empty")@@ -445,7 +446,8 @@ def repair_jumps(frames: list[Image.Image], interpolate: Interpolate | None, *,frame = interpolate(a, b, t)if frame.mode != "RGBA" or frame.size != out[drawing[0]].size:raise ValueError("repair interpolator must return an RGBA frame of the original size")- measure = {**smear(frame, a, b), "ghost": ghost(frame) if reads else None, "crossfade": crossfade(frame, a, b, t)}+ measure = {**smear(frame, a, b), "outline_shift": outline_shift(frame, a, b),+ "ghost": ghost(frame) if reads else None, "crossfade": crossfade(frame, a, b, t)}wrong = faults(measure) + ([] if frame.getchannel("A").getbbox() else ["empty"])made.append((drawing, frame, {"frames": drawing, "t": round(t, 4), "proposal": measure, "faults": wrong, "look": looks(measure),diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.pyindex f8fa9dd..44e85d3 100644--- a/sprite_gen/video/rife.py+++ b/sprite_gen/video/rife.py@@ -10,9 +10,11 @@ pushed out from inside its outline (`bleed`): a disagreement there reads as theblack a frame premultiplied over black put under it (2.24 and before: a black smear betweencrossing legs). Inside the coverage the colour is the frame's own, outline included.`smear` measures what a made frame has that neither neighbour has, and the outline it lost where-the flow failed and melted a limb into the fill; `ghost` reads a band of part coverage on a frame-alone, filmed or made; `crossfade` reads two drawings cross-faded inside a whole silhouette, a-reading to look at, not a fault (docs/loop-repair.md section 4).+the flow failed and melted a limb into the fill; `outline_shift` reads that edge against the+neighbours' rank by rank, so a mid-tone line the flow only softened is not read as lost; `ghost`+reads a band of part coverage on a frame alone, filmed or made; `crossfade` reads two drawings+cross-faded inside a whole silhouette, a reading to look at, not a fault (docs/loop-repair.md+section 4).The binary's own CPU path (`-g -1`) returns a wrong frame with rife-v4.6 on both macOS andLinux (measured 2026-10-03: mean error 39 against 3.3 through Vulkan), so it is never passed;@@ -66,6 +68,10 @@ PARTIAL_ALPHA = (0.1, 0.9)# when a dark solid pixel lies within this many pixels of it. A drawn outline sits on the edge, under# an antialiased pixel or two, whatever the frame's size.OUTLINE_REACH = 2+# `outline_shift`: a pixel of the made frame's edge is lost where its darkest solid pixel within OUTLINE_REACH is+# lighter than the lighter neighbour's at the same rank by more than this (luma, 0..1). A line the flow keeps+# and softens comes out lighter by less; a line gone to the fill beside it, by the line's own contrast.+OUTLINE_SHIFT = 50 / 255# `ghost`: part-covered pixels count where they make a region at least this many pixels thick. An# antialiased edge or a keyed strand of hair is a pixel or two wide and erodes away; the band a video# model leaves between two drawings, or a flow carries from it, does not.@@ -265,6 +271,36 @@ def smear(made: Image.Image, a: Image.Image, b: Image.Image) -> dict[str, float]"outline_loss": round((um - max(ua, ub)) / max(1, em), 5)}+def _edge_darkness(frame: Image.Image) -> np.ndarray:+ """Per pixel of the coverage edge (alpha from PARTIAL_ALPHA's floor, as `smear` reads it), the luma of the+ darkest solid pixel within OUTLINE_REACH of it, 1 where there is none."""+ x = np.asarray(frame.convert("RGBA"), dtype=np.float32) / 255.0+ covered = x[..., 3] >= PARTIAL_ALPHA[0]+ edge = covered & ~_mask(covered, 3, ImageFilter.MinFilter)+ luma = x[..., 0] * 0.299 + x[..., 1] * 0.587 + x[..., 2] * 0.114+ near = np.pad(np.where(x[..., 3] >= 0.5, luma, 1.0), OUTLINE_REACH, constant_values=1.0)+ width = 2 * OUTLINE_REACH + 1+ for axis in (0, 1): # a square window's minimum, one axis at a time+ near = np.lib.stride_tricks.sliding_window_view(near, width, axis=axis).min(axis=-1)+ return near[edge]+++def outline_shift(made: Image.Image, a: Image.Image, b: Image.Image) -> float:+ """The edge of a made frame that lost its line, read against its two neighbours rank by rank: each pixel+ of its coverage edge by how dark the darkest solid pixel within OUTLINE_REACH is, in order, against the+ lighter of the two neighbours' edges at the same rank (their quantiles), and counted where it is lighter+ by more than OUTLINE_SHIFT — as a fraction of its edge. A line drawn in a mid tone and softened by the+ flow reads lighter by less at every rank and is not counted, whatever tone the art outlines in; a line+ gone to the fill beside it (legs melted into one shape, a pale ghost limb) is. 0 where nothing is lighter+ (docs/loop-repair.md section 4)."""+ dm, da, db = (_edge_darkness(f) for f in (made, a, b))+ if not (dm.size and da.size and db.size):+ return 0.0+ rank = (np.arange(dm.size) + 0.5) / dm.size+ lighter = np.maximum(np.quantile(da, rank), np.quantile(db, rank))+ return round(float(np.mean(np.sort(dm) > lighter + OUTLINE_SHIFT)), 5)++def ghost(frame: Image.Image) -> float:"""Part-covered pixels (PARTIAL_ALPHA) inside regions at least GHOST_THICK pixels thick, as afraction of the frame's solid pixels: a ghost band, read on the frame's own coverage and notdiff --git a/tests/video/test_cycle_align.py b/tests/video/test_cycle_align.pyindex 97425ff..d0c0f5e 100644--- a/tests/video/test_cycle_align.py+++ b/tests/video/test_cycle_align.py@@ -374,10 +374,12 @@ def test_a_made_frame_darker_than_both_neighbours_is_named_in_the_warnings(tmp_pdef _melt(a: Image.Image, b: Image.Image, t: float) -> Image.Image:"""A stand-in RIFE that lost the dark foot: the frame between drawn whole, its dark grey taken- into the body's blue — the outline around the foot gone, nothing darker added."""+ into a pale fill — the outline around the foot gone, nothing darker added. (Taken into the body's+ blue, 42/255 lighter than the grey, it would be no further from the grey than a softened line and+ not read as lost, `rife.OUTLINE_SHIFT`.)"""made = np.asarray(Image.blend(a, b, t)).copy()made[..., 3] = np.where(made[..., 3] >= 64, 255, 0)- made[(made[..., 3] > 0) & (made[..., :3].max(axis=-1) < 130)] = (90, 90, 200, 255)+ made[(made[..., 3] > 0) & (made[..., :3].max(axis=-1) < 130)] = (200, 200, 235, 255)return Image.fromarray(made, "RGBA")diff --git a/tests/video/test_outline_shift.py b/tests/video/test_outline_shift.pynew file mode 100644index 0000000..3da13cf--- /dev/null+++ b/tests/video/test_outline_shift.py@@ -0,0 +1,148 @@+# SPDX-License-Identifier: Apache-2.0+"""The outline read rank by rank (docs/loop-repair.md section 4): `rife.outline_shift` reads a made frame's+coverage edge against its two neighbours' at the same rank, and the fault `outline` of the frames the jump+repair and the cycle alignment make needs it over the line as well as `outline_loss`. A line outlined in a+mid tone, either side of the dark line (70/255), and drawn a little lighter by the flow is kept; a line gone+to the fill is lost, whatever tone the art outlines in. The versioned measures, which carry `rife.smear`+alone, judge the outline on the dark line as before.++The fixtures are synthetic: the walker of tests/video/test_rife.py twice the size, outlined in bands of two+browns either side of 70/255 (luma 57 and 77), the frame between drawn with its line lighter or its legs'+line turned to fill. The one real-RIFE case runs where RIFE is installed."""++from __future__ import annotations++import numpy as np+import pytest+from PIL import Image, ImageDraw++from sprite_gen.video import align, repair, rife+from sprite_gen.video.interpolation_quality import OUTLINE_WARN, faults+from tests.video.test_loop_repair import forced_flow_group+from tests.video.test_rife import _walker++TONES = ((80, 50, 35), (105, 68, 48)) # luma 57 and 77: the dark line falls between them+FAR, NEAR = ((20, -20), (-5, 5)), ((10, -10), (4, -4))+++def _two_tone(front: float, back: float, *, lift: int = 0, s: int = 2) -> Image.Image:+ """A body over two legs swung from the hip (degrees, 0 straight down), each a capsule outlined 2·s px,+ the line in bands 6·s px tall of the two TONES, each `lift` lighter; the far leg shaded."""+ w, h = 128 * s, 192 * s+ im = Image.new("RGBA", (w, h), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ line = (1, 2, 3, 255) # a placeholder, coloured by band below+ hip, length, r = (w // 2, int(h * 0.55)), int(h * 0.36), int(w * 0.09)+ for angle, fill in ((back, (215, 205, 200, 255)), (front, (250, 240, 235, 255))):+ a = np.radians(angle)+ foot = (hip[0] + length * np.sin(a), hip[1] + length * np.cos(a))+ for radius, colour in ((r, line), (r - 2 * s, fill)):+ d.line([hip, foot], fill=colour, width=2 * radius)+ d.ellipse([foot[0] - radius, foot[1] - radius, foot[0] + radius, foot[1] + radius], fill=colour)+ body = [w * 0.25, h * 0.15, w * 0.75, h * 0.62]+ d.ellipse(body, fill=line)+ d.ellipse([body[0] + 2 * s, body[1] + 2 * s, body[2] - 2 * s, body[3] - 2 * s], fill=(250, 240, 235, 255))+ x = np.asarray(im).copy()+ on_line = (x == line).all(axis=-1)+ band = (np.arange(h)[:, None] // (6 * s) % 2).repeat(w, axis=1)+ x[on_line, :3] = np.minimum(np.asarray(TONES)[band[on_line]] + lift, 255)+ return Image.fromarray(x, "RGBA")+++def _legs_to_fill(frame: Image.Image, below: float = 0.57) -> Image.Image:+ """`frame` with the line of everything below this share of its height turned to the leg's fill."""+ x = np.asarray(frame).copy()+ legs = x[int(below * x.shape[0]):]+ line = (legs[..., 3] > 0) & (legs[..., :3] @ np.array([0.299, 0.587, 0.114]) < 140)+ legs[line] = (240, 230, 225, 255)+ return Image.fromarray(x, "RGBA")+++def _measure(made: Image.Image, a: Image.Image, b: Image.Image) -> dict[str, float]:+ return {**rife.smear(made, a, b), "outline_shift": rife.outline_shift(made, a, b)}+++def test_a_mid_tone_line_drawn_lighter_is_kept_and_one_gone_to_fill_is_lost():+ """Rank by rank the lighter line is no further from its neighbours' than 50/255, so nothing is lost; the+ dark line reads half the edge and more as lost, the line being now all on its far side. The legs' line+ gone to fill is lost on both."""+ a, b = (_two_tone(*p) for p in FAR)+ drawn = _two_tone(8, -8)+ assert rife.outline_shift(drawn, a, b) == 0 and faults(_measure(drawn, a, b)) == []+ for lift in (20, 45):+ lighter = _two_tone(8, -8, lift=lift)+ m = _measure(lighter, a, b)+ assert m["outline_loss"] > 0.5 and m["outline_shift"] == 0 and faults(m) == []+ melted = _measure(_legs_to_fill(drawn), a, b)+ assert melted["outline_loss"] > 0.18 and melted["outline_shift"] > 0.4 and faults(melted) == ["outline"]+++def test_the_versioned_measures_judge_the_outline_on_the_dark_line_alone():+ """`rife.smear` alone — what `video-loop-compare` and `video-loop-repair` read — carries no+ `outline_shift`, and the lighter line is the fault `outline` there, as before."""+ a, b = (_two_tone(*p) for p in FAR)+ assert faults(rife.smear(_two_tone(8, -8, lift=20), a, b)) == ["outline"]+ assert faults({"dark_excess": 0.0, "outline_loss": 0.3, "outline_shift": 0.0}) == []+ assert faults({"dark_excess": 0.0, "outline_loss": 0.3, "outline_shift": 0.3}) == ["outline"]+ assert faults({"dark_excess": 0.0, "outline_loss": 0.0, "outline_shift": 0.3}) == []+++def test_art_outlined_near_black_reads_the_same_on_both():+ """The walker outlined in black: its melted frame is lost on both readings, its drawing between on+ neither, and a black line drawn lighter but still under the dark line is lost on neither."""+ a, b = _walker(20, -20), _walker(-5, 5)+ melted = np.asarray(_walker(8, -8)).copy()+ legs = melted[110:, :]+ legs[(legs[..., 3] > 0) & (legs[..., :3].max(axis=-1) < 70)] = (240, 230, 225, 255)+ m = _measure(Image.fromarray(melted, "RGBA"), a, b)+ assert m["outline_loss"] > OUTLINE_WARN and m["outline_shift"] > OUTLINE_WARN and faults(m) == ["outline"]+ assert faults(_measure(_walker(8, -8), a, b)) == []+ grey = np.asarray(_walker(8, -8)).copy()+ grey[(grey[..., 3] > 0) & (grey[..., :3].max(axis=-1) < 70), :3] = 60+ m = _measure(Image.fromarray(grey, "RGBA"), a, b)+ assert m["outline_loss"] <= 0.01 and m["outline_shift"] == 0+++def test_a_frame_with_no_edge_and_neighbours_without_one_read_zero():+ blank = Image.new("RGBA", (64, 64), (0, 0, 0, 0))+ a, b = (_two_tone(*p, s=1) for p in FAR)+ assert rife.outline_shift(blank, a, b) == 0 and rife.outline_shift(a, blank, blank) == 0+++def test_jump_repair_takes_a_lighter_line_and_refuses_one_gone_to_fill(monkeypatch):+ """A flow group whose RIFE frame keeps a mid-tone line, drawn lighter, is accepted, its proposal+ carrying both readings; the same frame with its legs' line gone to fill is refused."""+ frames = [_two_tone(20, -20), _two_tone(30, -30), _two_tone(-5, 5), _two_tone(10, -10)]+ lighter = _two_tone(8, -8, lift=30)+ forced_flow_group(monkeypatch, [1], (0, 2), [0.5])+ out, record = repair.repair_jumps(frames, lambda x, y, t: lighter)+ made = record["rounds"][0]["made"][0]["proposal"]+ assert record["rounds"][0]["outcome"] == "accepted" and out[1] is lighter+ assert made["outline_loss"] > OUTLINE_WARN and made["outline_shift"] == 0+ melted = _legs_to_fill(_two_tone(8, -8))+ out, record = repair.repair_jumps(frames, lambda x, y, t: melted)+ assert record["rounds"][0]["outcome"] == "rejected" and record["rounds"][0]["faults"] == ["outline"]+ assert out[1] is frames[1]+++def test_alignment_keeps_a_lighter_line_and_takes_the_nearer_frame_for_one_gone_to_fill():+ a, b = (_two_tone(*p) for p in FAR)+ lighter, melted = _two_tone(8, -8, lift=30), _legs_to_fill(_two_tone(8, -8))+ out, facts = align.resample([a, b], 4, lambda x, y, t: lighter) # times 0, .5, 1, 1.5: both made at t=0.5+ assert facts["made_at"] == [1, 3] and all(m["faults"] == [] and m["outline_shift"] == 0 for m in facts["smear"])+ out, facts = align.resample([a, b], 4, lambda x, y, t: melted)+ assert facts["nearest_at"] == [1, 3] and all(m["faults"] == ["outline"] for m in facts["smear"])++[email protected]_rife+def test_real_rife_keeps_a_mid_tone_line_through_a_short_step_and_loses_it_crossing_far():+ """Through RIFE, the two-tone legs of a short step come out outlined on the rank reading at every+ fraction; legs crossing far melt, and the fault is read on both. At the size of the real-RIFE cases+ of tests/video/test_rife.py, where the 2 px line is thick enough for the flow to melt."""+ interpolate = rife.Rife()+ near = tuple(_two_tone(*p, s=1) for p in NEAR)+ for t in (0.25, 0.5, 0.75):+ assert "outline" not in faults(_measure(interpolate(*near, t), *near))+ far = tuple(_two_tone(*p, s=1) for p in FAR)+ m = _measure(interpolate(*far, 0.5), *far)+ assert m["outline_loss"] > OUTLINE_WARN and m["outline_shift"] > OUTLINE_WARN and "outline" in faults(m)