Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video-loop): an eased stretch is read again on the frames made, and one that hurries the rest of the body is refused
Soohong Kim18d949357780
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 813003c..a0bc1ba 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,8 +4,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased-- **New: `video-loop --repair auto` (and `on`) eases a part that rushes on its way — the frames either side of the rush are made again at times that spread it, the body's own steps kept.** A part can keep its way and still jump: a tail that runs short of the cycle rushes on at the wrap, covering in one step what it covers in eight elsewhere, and each frame there lies near enough the line between its neighbours that the flow reading lets it pass, or takes the frame at the wrap out and leaves the rush over two steps. `repair.surges` reads the speed on the flow reading's own path and windows: a run of one or two steps whose mean step is at least 3.0 usual steps and 3.0 times the mean of the three steps either side, the part going on the way it came through it (a step that turns back is a frame flung, the flow repair's). After the flow groups, the worst rush is eased over three or four drawings either side, made from the frames as filmed (a frame the flow made inside is made again, `supersedes`) and kept between two frames as filmed, never leaving more than half the loop made with the rounds before it (as much as `video-loop-compare` reads back as the repair's): `repair.ease_times` gives every frame a time, on a grid of a twentieth of a step, where the part's step changes from one step to the next as little as it can — from the speed it came in with to the speed it goes on with — while the foot line's centre and the body's mass (`loop.foot_centre`, `loop.body_centre`) and the lowest solid row move in a step no further than the stretch's largest step as it played and half a pixel, and every other window of the flow reading no further than its own largest step there; within 2 % of that, the timing nearest the frames' own. How far the part has gone is the path of its coverage's centre in the windows that read the rush, which grows as far as the part moves where a blurred distance stops growing. RIFE makes each drawing between the frames as filmed either side of its time, one call a drawing, at most nine, outside the three of the flow groups; the stretch is taken only where no frame made has an interpolation fault, no body measure moves a pixel past the stretch's largest step, the part's largest step falls, no frame is left further off the flow and over its limit, and no new rush appears in the rest of the body (`rejected_for`). On the synthetic walker the tail a tenth of a cycle short — under the flow reading's limit — goes from 5.8 usual steps into the wrap to at most 2.7; a quarter short, after the flow repair, from 7.2 to 5.0 and still read a rush, as far as the legs allow; ordinary walks and a flung frame read no rush. A loop with no rush costs one more reading and no call. `jump_repair` carries `surge`, `surge_after`, `ease_skipped`, `ease_calls` and the round `why: ease`, `replaced` its frames, and the summary `eased`; without RIFE the rush is cut as filmed with a warning line, and `--repair on` refuses ([loop-repair](docs/loop-repair.md) section 2, "The eased retime").-- **Tests** — `tests/video/test_loop_ease.py` (a tail that rushes on at the wrap after the flow repair is eased over three or four drawings either side, in play order, made only from frames as filmed — the flow's frame at the wrap made again — every frame outside as it was, the part's largest step down by a quarter or more and the body's feet, mass and ground within the stretch's own steps and a pixel; a rush under the flow reading's limit eased, none left; ordinary walks, drawn every frame and on twos, have no rush and nothing is made; a frame flung against the way is not a rush; on twos each drawing made once and shown on both its frames; a made frame pushed sideways refuses the stretch for drift, and one with its outline melted for a fault, the loop left as the flow left it; without an interpolator the rush named and nothing made; the same frames whichever way the body faces; a fourteen-frame loop eased three a side where four would leave more than half of it made; the search keeps every frame at its own time where the part moves evenly, and spreads a rush as far as the body's bound lets it; `video-loop` playing the frames made in a loop as long, a rush without RIFE cut as filmed with a warning line, `--repair on` without RIFE refused), and `tests/video/test_loop_flow.py`'s walker draws its dark parts whole on asking (`dark_whole`), so that a frame drawn between two phases has no more dark pixels than they have.+- **New: `video-loop --repair auto` (and `on`) eases a part that rushes on its way — the frames either side of the rush are made again at times that spread it, the body's own steps kept.** A part can keep its way and still jump: a tail that runs short of the cycle rushes on at the wrap, covering in one step what it covers in eight elsewhere, and each frame there lies near enough the line between its neighbours that the flow reading lets it pass, or takes the frame at the wrap out and leaves the rush over two steps. `repair.surges` reads the speed on the flow reading's own path and windows: a run of one or two steps whose mean step is at least 3.0 usual steps and 3.0 times the mean of the three steps either side, the part going on the way it came through it (a step that turns back is a frame flung, the flow repair's). After the flow groups, the worst rush is eased over three or four drawings either side, made from the frames as filmed (a frame the flow made inside is made again, `supersedes`) and kept between two frames as filmed, never leaving more than half the loop made with the rounds before it (as much as `video-loop-compare` reads back as the repair's): `repair.ease_times` gives every frame a time, on a grid of a twentieth of a step, where the part's step changes from one step to the next as little as it can — from the speed it came in with to the speed it goes on with — while the foot line's centre and the body's mass (`loop.foot_centre`, `loop.body_centre`) and the lowest solid row move in a step no further than the stretch's largest step as it played and half a pixel, and every other window of the flow reading no further than its own largest step there; within 2 % of that, the timing nearest the frames' own. How far the part has gone is the path of its coverage's centre in the windows that read the rush, which grows as far as the part moves where a blurred distance stops growing. RIFE makes each drawing between the frames as filmed either side of its time, one call a drawing, at most nine, outside the three of the flow groups; the stretch is taken only where no frame made has an interpolation fault, no body measure moves a pixel past the stretch's largest step, the part's largest step falls, no frame is left further off the flow and over its limit, no new rush appears in the rest of the body, and no window of the rest of the body steps further on the frames made than its own largest step over the stretch as it played (`rejected_for`, `the rest faster`: the search reads the rest of the body on the line between the frames as filmed, and the frames made, which go the body's own way between them, step further where a part moves far between frames). On the synthetic walker drawn over 24 frames a cycle, the tail a quarter short goes, after the flow repair, from 5.3 usual steps into the wrap to 3.4 with no rush left; over 16 frames, 15 % short and under the flow reading's limit, from 3.6 to 1.7; over 36 frames, where its legs, arm and head move far between frames, every stretch is refused, the rest of the body 1.5 % to 19 % past its own largest step on the frames made; ordinary walks and a flung frame read no rush. A loop with no rush costs one more reading and no call. `jump_repair` carries `surge`, `surge_after`, `ease_skipped`, `ease_calls` and the round `why: ease` (its `rest`: per window the rush is not read in, `step_max_before`, `step_max_after` and `faster`), `replaced` its frames, and the summary `eased`; without RIFE the rush is cut as filmed with a warning line, and `--repair on` refuses ([loop-repair](docs/loop-repair.md) section 2, "The eased retime").+- **Tests** — `tests/video/test_loop_ease.py` (a tail that rushes on at the wrap after the flow repair is eased over three or four drawings either side, in play order, made only from frames as filmed — the flow's frame at the wrap made again — every frame outside as it was, the part's largest step down by a quarter or more and the body's feet, mass and ground within the stretch's own steps and a pixel, and every window of the rest of the body within its own largest step, read again on the loop as it plays; a rush under the flow reading's limit eased, none left; over 36 frames, the stretch whose frames made step further in the arm, the head and the legs than they did there refused for that alone, the loop as the flow left it; ordinary walks, drawn every frame and on twos, have no rush and nothing is made; a frame flung against the way is not a rush; on twos each drawing made once and shown on both its frames; a made frame pushed sideways refuses the stretch for drift, and one with its outline melted for a fault, the loop left as the flow left it; without an interpolator the rush named and nothing made; the same frames whichever way the body faces; a fourteen-frame loop eased three a side where four would leave more than half of it made; the search keeps every frame at its own time where the part moves evenly, and spreads a rush as far as the body's bound lets it; `video-loop` playing the frames made in a loop as long, a rush without RIFE cut as filmed with a warning line, `--repair on` without RIFE refused), and `tests/video/test_loop_flow.py`'s walker draws its dark parts whole on asking (`dark_whole`), so that a frame drawn between two phases has no more dark pixels than they have.- **Behavior change: a frame the jump repair or the cycle alignment makes between outlined drawings 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 rank reading is read only between outlined drawings: `rife.outlined(frame)` is the share of a frame's edge whose line, the darkest solid pixel within 2 px, is darker than its fill, its colour from twice the bleed's depth inside (`rife.LINE_DEPTH`), by more than 50/255, and both neighbours must read at least 75 % (`rife.OUTLINED`); elsewhere `outline_shift` is `null` and the dark line judges alone, as before. Art painted without a line has no line to lose, and a part the flow melts there — a boot washed pale — can come out lighter by less than 50/255 at every rank: read rank by rank it would be kept and delivered, where refused it is replaced by the nearer filmed frame. On the walker of the tests painted without a line (outlined on 4 % of its edge), the drawing between with its near boot washed 40 tones lighter reads `outline_loss` 7.8 % and, rank by rank, 0: refused. The 75 % is set on frames made between drawn walks and judged by eye so that no frame seen as broken that the dark line alone refused is kept. The jump repair's proposals and the alignment's `smear` rows carry `outline_shift`, and the alignment's warning line names it where it was read; `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, and so does a measure whose rank reading was not read; art outlined near-black reads the same on both; a frame with no edge reads 0 and neighbours with none are not read; 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; on the walker painted without a line, a boot washed 40 tones lighter is refused by the jump repair and replaced by the alignment, and the two-tone and black walkers read outlined on all their edge; through RIFE, a short step of two-tone legs keeps its outline and legs crossing far lose it on both).- **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").diff --git a/docs/loop-repair.md b/docs/loop-repair.mdindex 5c2ab42..4376cac 100644--- a/docs/loop-repair.md+++ b/docs/loop-repair.md@@ -486,7 +486,8 @@ forward and keep, in every step,the ground it stands on (`repair.drift_points`) — within the stretch's largest step as it played,and half a pixel (`DRIFT_PIXEL` − `EASE_MARGIN`), drawn between the frames as filmed, and- every other window of the flow reading — the rest of the body — within its own largest step in- the stretch: no leg or arm hurried past what it did there,+ the stretch, read on the line between the states of the frames as filmed: no leg or arm hurried+ past what it did there (read again on the frames made, below),`repair.ease_times` takes the one whose largest change of the part's step from one step to the next,from the step into the stretch to the step out of it, is least (`accel`): the part speeds up and@@ -507,21 +508,38 @@ on the frames made:| the part's largest step over the stretch falls (`part`) | `no gain` || no frame of the stretch is read further off the flow than before and at or over its limit (`off_max`) | `off the flow` || no rush is read in the stretch in a window the part does not rush in: the rest of the body was hurried | `a new rush` |+| no window the part does not rush in steps further than its own largest step over the stretch as it played (`rest`: `windows`, `step_max_before`, `step_max_after`, in the usual steps of the loop the flow groups left, and `faster`) | `the rest faster` |A refused stretch keeps the loop as the flow groups left it; the loop keeps its length and everyframe outside the stretch is as it was.+**The rest of the body, read again.** The search reads each window of the rest of the body on the+line between the states of the frames as filmed. The frames made go the body's own way between+them, and where a part moves far between two frames — a leg through its swing, two legs crossing —+its window steps further than that line says, most where a step is stretched across a frame as+filmed. So the bound is read again on the frames made, on the flow reading's own steps, and a+stretch that goes past it in any window is refused whole: nothing is allowed over. Better readings+from the frames as filmed alone were weighed and none holds. Against the stand-in's frames made at+every time of the search on the 36-frame walker, over the steps a search step can take, a step each+reading allows is made as much as 1.48 times the bound on the line, 1.39 times along the window's+path through the frames, 1.35 times along it with a smoothly changing speed and 1.31 times with each+step lengthened by the circle through its bend; and the stand-in's own speed integrated, the ceiling+of every reading by speed, eases none of those walkers drawn every frame. Searching again with the+frames made added to the frames as filmed does not settle either: after five tries those walkers+were still refused.+| On the synthetic walker (the stand-in in-between of `tests/video/test_loop_ease.py`) | Part's largest step | Frames made ||---|---|---|-| the tail 5 % of a cycle short | 3.8 → 2.6 | 8, at four a side, 7 calls |-| 8 % | 4.9 → 2.6 | 8, 8 calls |-| a tenth | 5.8 → 2.7, no rush left | 8, 8 calls |-| a quarter, after the flow repair | 7.2 → 5.0, still read a rush (3.7) | 9, 9 calls |-| a quarter, on twos | 3.4 → 1.6 (on its drawings) | 8 drawings, 16 frames, 8 calls |--A quarter of a cycle is too far to spread out of sight over four drawings a side without hurrying-the legs past their own largest step: the rush falls by three tenths and is left, named in `surge_after`.-On a Mac's GPU a call is a second or so; on Modal's CPU with `-x -z` twelve to thirty seconds a+| 24 frames a cycle, the tail a quarter short, after the flow repair at the wrap | 5.3 → 3.4, no rush left | 8, at four a side, 8 calls |+| 16 frames, 15 % short (under the flow reading's limit) | 3.6 → 1.7, no rush left | 6, at three a side, 5 calls |+| 16 frames, a fifth short | 3.1 → 1.5, no rush left | 6, 6 calls |+| 14 frames, 15 % short | 3.1 → 1.5, no rush left | 6, 6 calls |+| 48 frames on twos, a quarter short, after the flow repair | 5.3 → 3.4 (on its drawings), no rush left | 8 drawings, 16 frames, 8 calls |+| 36 frames, 5 % to 30 % short, drawn every frame or on twos | refused: the rest of the body 1.5 % to 19 % past its own largest step on the frames made | 6 to 9 calls, none kept |++Over 36 frames a cycle the stand-in's legs, arm and head move far between frames, and every stretch+there is refused: the loop keeps the flow repair, `surge_after` names the rush, and the calls the+stretch made are spent. On a Mac's GPU a call is a second or so; on Modal's CPU with `-x -z` twelve to thirty seconds a280 × 520 frame (section 1), so a loop that rushes costs two to four and a half minutes more, and aloop that does not, nothing.@@ -534,8 +552,9 @@ given way, refused, or flung against the way`, `no timing keeps the body in bounstretch in play order), `sources` (the frames as filmed it was made from), `supersedes`, `span`(frames from anchor to anchor), `made` (per drawing: `frames`, `time` and `own` in frames from thefirst anchor, `from`, `t`, `proposal`, `faults`, `look`), `accel` (`before`, `chosen`), `part`-(`windows`, `step_max_before`, `step_max_after`), `drift`, `off_max`, `calls`, `faults`, `look` and-`outcome`, and `replaced` or `rejected_for`. The record's `replaced` holds the frames it changed, the+(`windows`, `step_max_before`, `step_max_after`), `rest` (`windows`, `step_max_before`,+`step_max_after`, `faster`), `drift`, `off_max`, `calls`, `faults`, `look` and `outcome`, and+`replaced` or `rejected_for`. The record's `replaced` holds the frames it changed, theseam gate reads the rendered cells, and the summary names them (`eased`). Without RIFE the rush iscut as filmed with a warning line (`why`: "RIFE not installed — the rush cut as filmed"), and`--repair on` refuses.diff --git a/sprite_gen/video/repair.py b/sprite_gen/video/repair.pyindex 0c4bed5..f768da2 100644--- a/sprite_gen/video/repair.py+++ b/sprite_gen/video/repair.py@@ -17,7 +17,8 @@ A part can also keep its way and still rush: a tail that runs short of the cycleit covers in eight elsewhere. The surge reading (`surges`) reads that speed; the frames either side of the rush, threeor four drawings each way, are made again by RIFE at times chosen so the part's speed rises and falls gradually(`ease_times`), while the body's feet, mass and ground line move no further in a step, nor the rest of the body-faster, than they did in that stretch (docs/loop-repair.md section 2, "The eased retime").+faster, than they did in that stretch — read again on the frames made, which go the body's own way between the+frames as filmed (docs/loop-repair.md section 2, "The eased retime").Measured on the loop as it plays — cyclic, the last frame followed by the first — and insidethe union box of the body over the whole loop, the way the strip cells are cut, so a frame's@@ -577,7 +578,8 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interpinto = np.array([float(np.linalg.norm(pl[stretch[0]] - pl[(stretch[0] - 1) % n])) for pl in places])after = np.array([float(np.linalg.norm(pl[(stretch[-1] + 1) % n] - pl[stretch[-1]])) for pl in places])own = list(range(1, T))- # the rest of the body, window by window, moves in no step further than it did in a step of the stretch+ # the rest of the body, window by window, moves in no step further than it did in a step of the stretch —+ # read here on the line between the frames as filmed, and on the frames made once they are maderest = [w for w in range(len(flow["windows"])) if w not in W]allowed = np.ones((len(grid), len(grid)), dtype=bool)for w in rest:@@ -592,9 +594,9 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interpif result is None:why = "no timing keeps the body in bound"continue- plans.append({"surge": s, "k": k, "stretch": stretch, "sources": sources, "W": W, "progress": progress,- "grid": grid, "bound": bound, "into": into, "after": after, "result": result,- "accel_before": _part_accel(progress, [float(t) for t in own], grid, into, after)})+ plans.append({"surge": s, "k": k, "stretch": stretch, "sources": sources, "W": W, "rest": rest,+ "progress": progress, "grid": grid, "bound": bound, "into": into, "after": after,+ "result": result, "accel_before": _part_accel(progress, [float(t) for t in own], grid, into, after)})if plans:breakif not plans:@@ -654,9 +656,15 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interp# a rush the stretch did not have, in a window the part does not rush in: the retime hurried the rest of the bodyrushes = [x for x in _surges(after_flow)if {(x["d"] + i) % n for i in range(x["r"] + 1)} & inside and set(x["W"]) - set(W)]+ # the rest of the body on the frames made, window by window: no step of the stretch further than its largest as it+ # played. The search reads it between the frames as filmed; the frames made go the body's own way between them.+ rest = plan["rest"]+ rest_before = flow["step"][stretch[:-1]][:, rest].max(axis=0) if rest else np.zeros(0)+ rest_after = after_flow["step"][stretch[:-1]][:, rest].max(axis=0) if rest else np.zeros(0)+ faster = [w for w, b, a in zip(rest, rest_before, rest_after) if a > b + 1e-9]rejected = ((["fault"] if wrong else []) + (["drift"] if over else []) + (["no gain"] if part_after >= part_before else [])+ (["off the flow"] if off_after >= FLOW_LIMIT and off_after > off_before else [])- + (["a new rush"] if rushes else []))+ + (["a new rush"] if rushes else []) + (["the rest faster"] if faster else []))changed = [t for d in stretch[1:-1] for t in runs[d] if new[t] is not out[t]]round_: dict[str, Any] = {"why": "ease", "surge": _said_surge(s, flow), "width": plan["k"], "anchors": [first[stretch[0]], first[stretch[-1]]],@@ -666,6 +674,10 @@ def _ease(frames: list[Image.Image], out: list[Image.Image], interpolate: Interp"accel": {"before": round(plan["accel_before"], 4), "chosen": round(plan["result"]["accel"], 4)},"part": {"windows": [[round(v, 4) for v in flow["fractions"][w]] for w in W],"step_max_before": round(part_before, 4), "step_max_after": round(part_after, 4)},+ "rest": {"windows": [[round(v, 4) for v in flow["fractions"][w]] for w in rest],+ "step_max_before": [round(float(v), 4) for v in rest_before / flow["scale"][rest]],+ "step_max_after": [round(float(v), 4) for v in rest_after / flow["scale"][rest]],+ "faster": [[round(v, 4) for v in flow["fractions"][w]] for w in faster]},"drift": {"measures": list(DRIFT_MEASURES), "bound": [round(float(v), 3) for v in plan["bound"]],"moved": [round(float(v), 3) for v in moved], "over": over},"off_max": {"before": round(off_before, 4), "after": round(off_after, 4)}, "calls": calls, "faults": wrong,@@ -746,7 +758,8 @@ def repair_jumps(frames: list[Image.Image], interpolate: Interpolate | None, *,as filmed (a frame the flow made inside it is made again, `supersedes`), each drawing at the time `ease_times`chooses and shown on every frame it was; the stretch is taken only where no made frame has a fault, no bodymeasure moves further than the stretch did and a pixel (`drift`), the part's largest step falls (`part`), no- frame is left further off the flow than the limit and before, and no new rush appears in the rest of the body+ frame is left further off the flow than the limit and before, no new rush appears in the rest of the body, and+ no window of the rest of the body steps further than its own largest step over the stretch as it played (`rest`)(`rejected_for` names what failed). Its calls are its own, outside `max_frames` (`ease_calls`). `surge_after`is the reading of the loop as it now plays. `facing` reads nothing here: it is recorded for the jump guard of`video-loop-compare` (`restoration.jump`)."""diff --git a/tests/video/test_loop_ease.py b/tests/video/test_loop_ease.pyindex 8583566..3a8025e 100644--- a/tests/video/test_loop_ease.py+++ b/tests/video/test_loop_ease.py@@ -2,13 +2,18 @@"""The eased retime (docs/loop-repair.md section 2, "The eased retime"): where a part rushes — a run of one ortwo steps far faster than the steps around it, going the way it was going — the frames either side are madeagain at times the search chooses, so the part's speed rises and falls gradually; the body's feet, mass and-ground line move no further in a step than they did anywhere in that stretch, the loop keeps its length, and-every frame outside the stretch is the video's own. A frame flung against the way is the flow repair's, not-a rush; an ordinary walk has none.+ground line move no further in a step than they did anywhere in that stretch, nor any window of the rest of the+body further than its own largest step there, read on the frames made; the loop keeps its length, and every+frame outside the stretch is the video's own. A frame flung against the way is the flow repair's, not a rush;+an ordinary walk has none.The loops are the walker of tests/video/test_loop_flow.py, whose tail can run short of the cycle so that itrushes on at the wrap (`lag`). The interpolator is a stand-in that draws the walker between two frames the way-an optical-flow in-between does: the body at the time between them, the tail moved on between its two places."""+an optical-flow in-between does: the body at the time between them, the tail moved on between its two places.+Drawn over 36 frames a cycle, its legs, arm and head move far between frames, and the stand-in's frames made+there, the body's own way, step further than the line between the frames as filmed the search reads them on:+every such stretch is refused on the frames made. The walkers eased here are drawn over 14 to 48 frames, where+the frames made keep the rest of the body within its own steps."""from __future__ import annotations@@ -25,6 +30,8 @@ from tests.video.test_loop_flow import N, _against, _frame, _loopfrom tests.video.test_loop_repair import _keyed, _no_rifeLAG = 0.25 # the tail a quarter of a cycle short: the flow repair takes one frame at the wrap, and a rush is left+WRAP = 24 # frames a cycle of the walker eased after the flow repair's frame at the wrap+SHORT, SHORT_LAG = 16, 0.15 # a walker whose tail rushes at the wrap under the flow reading's limitdef _walk(n: int = N, *, lag: float = 0.0, hold: int = 1, flung: dict[int, float] | None = None) -> list[Image.Image]:@@ -81,45 +88,89 @@ def _ease(record: dict) -> dict:return round_+def _rest(frames: list[Image.Image], before: list[Image.Image], after: list[Image.Image], round_: dict+ ) -> tuple[np.ndarray, np.ndarray]:+ """Every window the rush is not read in, its largest step over the stretch on the loop `before` the ease and on the+ loop `after` it: the flow reading's own steps, on the union box of `frames` (the loop as filmed) and the drawings of+ `before`."""+ box = repair.union_box(frames)+ flow = repair._flow(before, box)+ then = repair._flow(after, box, (flow["runs"], flow["hold"]))+ first = [run[0] for run in flow["runs"]]+ stretch = [first.index(k) for k in (round_["anchors"][0], *(d[0] for d in round_["drawings"]), round_["anchors"][1])]+ part = [w for w, fraction in enumerate(flow["fractions"]) if [round(v, 4) for v in fraction] in round_["part"]["windows"]]+ rest = [w for w in range(len(flow["windows"])) if w not in part]+ return flow["step"][stretch[:-1]][:, rest].max(axis=0), then["step"][stretch[:-1]][:, rest].max(axis=0)+++def _kept_rest(frames: list[Image.Image], flow_only: list[Image.Image], out: list[Image.Image], round_: dict) -> None:+ """The stretch taken kept every window the rush is not read in within the largest step it took over the stretch as+ it played, read again here on the loop as it now plays, and the round says so (`rest`)."""+ before, after = _rest(frames, flow_only, out, round_)+ assert np.all(after <= before + 1e-9)+ assert round_["rest"]["faster"] == [] and len(round_["rest"]["windows"]) == len(before)+ assert all(a <= b + 1e-4 for a, b in zip(round_["rest"]["step_max_after"], round_["rest"]["step_max_before"]))++def test_a_tail_that_rushes_on_at_the_wrap_is_eased_over_the_frames_either_side():- frames = _walk(lag=LAG)- flow_only, before = repair.repair_jumps(frames, Lerp(frames, lag=LAG), ease=False)+ n = WRAP+ frames = _walk(n, lag=LAG)+ flow_only, before = repair.repair_jumps(frames, Lerp(frames, lag=LAG, n=n), ease=False)assert [r["why"] for r in before["rounds"]] == ["flow"] # the flow takes one frame at the wrap, and leaves a rushassert before["surge"] is not None and before["surge_after"] is not None- lerp = Lerp(frames, lag=LAG)+ lerp = Lerp(frames, lag=LAG, n=n)out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)assert round_["outcome"] == "accepted" and round_["width"] in repair.EASE_WIDTHSsegment = [*[round_["anchors"][0]], *round_["frames"], round_["anchors"][1]]- assert [(k - segment[0]) % N for k in segment] == list(range(len(segment))) # one stretch, in play order- assert {N - 1, 0} <= set(round_["frames"]) and len(round_["frames"]) == 2 * round_["width"] + round_["surge"]["steps"] - 1- assert len(out) == N and all(out[k] is frames[k] for k in range(N) if k not in round_["frames"])+ assert [(k - segment[0]) % n for k in segment] == list(range(len(segment))) # one stretch, in play order+ assert {n - 1, 0} <= set(round_["frames"]) and len(round_["frames"]) == 2 * round_["width"] + round_["surge"]["steps"] - 1+ assert len(out) == n and all(out[k] is frames[k] for k in range(n) if k not in round_["frames"])# every frame is made from two frames as filmed, never from a frame made — the flow's frame at the wrap is made againflow_frames = set(before["rounds"][0]["frames"])assert flow_frames <= set(round_["frames"]) and round_["supersedes"] == sorted(flow_frames)assert all(not {a, b} & flow_frames for a, b, _ in lerp.calls)- # the rush is spread: the part's largest step over the stretch falls by a quarter or more, and its speed changes- # less from step to step. A quarter of a cycle is too far to spread out of sight over the stretch without hurrying- # the legs, and the legs may go no faster than they did there.+ # the rush is spread: the part's largest step over the stretch falls by a quarter or more, its speed changes less+ # from step to step, and no rush is leftassert round_["part"]["step_max_after"] <= 3 / 4 * round_["part"]["step_max_before"]- assert round_["accel"]["chosen"] < round_["accel"]["before"] / 2+ assert round_["accel"]["chosen"] < round_["accel"]["before"] / 2 and record["surge_after"] is None# no drift: the feet, the body's mass and the ground line move no further in a step than they did there, to within- # the pixel they are read on+ # the pixel they are read on; and the rest of the body no fasterassert np.all(_drift(out, segment) <= _drift(flow_only, segment) + repair.DRIFT_PIXEL)+ _kept_rest(frames, flow_only, out, round_)changed = {k for k in round_["frames"] if out[k] is not frames[k]}assert round_["replaced"] == sorted(changed, key=round_["frames"].index) and len(changed) >= len(round_["frames"]) - 2assert set(record["replaced"]) == flow_frames | changeddef test_a_rush_the_flow_reading_lets_pass_is_eased_too():- # A tail a tenth of a cycle short reads under the flow's limit: no frame is taken out, and it still rushes.- frames = _walk(lag=0.1)+ # A tail 15 % of a cycle short over 16 frames reads under the flow's limit: no frame is taken out, and it still rushes.+ frames = _walk(SHORT, lag=SHORT_LAG)assert repair.flow_targets(frames)["groups"] == []- out, record = repair.repair_jumps(frames, Lerp(frames, lag=0.1))+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT), ease=False)+ out, record = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT))round_ = _ease(record)assert round_["outcome"] == "accepted" and round_["supersedes"] == []assert record["surge"]["ratio"] >= repair.SURGE_RATIO and record["surge_after"] is None- assert len(out) == N and all(out[k] is frames[k] for k in range(N) if k not in round_["frames"])+ assert len(out) == SHORT and all(out[k] is frames[k] for k in range(SHORT) if k not in round_["frames"])+ _kept_rest(frames, flow_only, out, round_)+++def test_a_stretch_whose_frames_made_hurry_the_rest_of_the_body_is_refused():+ # Over 36 frames the legs, the arm and the head move far between frames. Easing a tail a tenth of a cycle short gives+ # the rush more time and the steps around it less; the search reads the rest of the body on the line between the+ # frames as filmed and finds times that keep it, and the frames made, which go the body's own way between them, step+ # further there (a validator's counterexample on 9dd5050: the arm's window 1.736 to 2.030, the stretch taken). Read+ # on the frames made, the stretch is refused for that alone, and the loop stays as the flow left it.+ frames = _walk(lag=0.1)+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=0.1), ease=False)+ out, record = repair.repair_jumps(frames, Lerp(frames, lag=0.1))+ round_ = _ease(record)+ assert round_["outcome"] == "rejected" and round_["rejected_for"] == ["the rest faster"]+ faster = [i for i, w in enumerate(round_["rest"]["windows"]) if w in round_["rest"]["faster"]]+ assert faster and all(round_["rest"]["step_max_after"][i] > round_["rest"]["step_max_before"][i] for i in faster)+ assert [f.tobytes() for f in out] == [f.tobytes() for f in flow_only] and record["replaced"] == []+ assert record["surge_after"] is not None and "replaced" not in round_def test_an_ordinary_walk_has_no_rush_and_nothing_is_made():@@ -136,20 +187,24 @@ def test_a_frame_flung_against_the_way_is_not_a_rush():def test_on_twos_each_drawing_is_made_once_and_shown_for_both_its_frames():- frames = _walk(lag=LAG, hold=2)- lerp = Lerp(frames, lag=LAG, hold=2)+ n = 48 # 24 drawings on twos+ frames = _walk(n, lag=LAG, hold=2)+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG, hold=2, n=n), ease=False)+ lerp = Lerp(frames, lag=LAG, hold=2, n=n)out, record = repair.repair_jumps(frames, lerp)round_ = _ease(record)assert record["hold"] == 2 and round_["outcome"] == "accepted"assert all(len(d) == 2 and out[d[0]] is out[d[1]] for d in round_["drawings"])assert len(lerp.calls) <= len(round_["drawings"]) + len(record["rounds"][0]["drawings"])- assert len(out) == N and all(out[k] is frames[k] for k in range(N) if k not in round_["frames"])+ assert len(out) == n and all(out[k] is frames[k] for k in range(n) if k not in round_["frames"])+ _kept_rest(frames, flow_only, out, round_)def test_a_made_frame_that_moves_the_body_further_than_the_stretch_did_refuses_the_ease():- frames = _walk(lag=LAG)- flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG), ease=False)- lerp = Lerp(frames, lag=LAG)+ n = WRAP+ frames = _walk(n, lag=LAG)+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG, n=n), ease=False)+ lerp = Lerp(frames, lag=LAG, n=n)def pushed(a: Image.Image, b: Image.Image, t: float) -> Image.Image:made = lerp(a, b, t)@@ -167,9 +222,10 @@ def test_a_made_frame_that_moves_the_body_further_than_the_stretch_did_refuses_tdef test_a_made_frame_at_fault_refuses_the_ease_and_the_loop_stays_as_the_flow_left_it():- frames = _walk(lag=LAG)- flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG), ease=False)- lerp = Lerp(frames, lag=LAG)+ n = WRAP+ frames = _walk(n, lag=LAG)+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=LAG, n=n), ease=False)+ lerp = Lerp(frames, lag=LAG, n=n)def melted(a: Image.Image, b: Image.Image, t: float) -> Image.Image:made = lerp(a, b, t)@@ -194,17 +250,18 @@ def test_without_an_interpolator_a_rush_is_named_and_nothing_is_made():def test_it_eases_the_same_frames_whichever_way_the_body_faces():- frames = _walk(lag=0.1)+ frames = _walk(SHORT, lag=SHORT_LAG)mirrored = [ImageOps.mirror(f) for f in frames]- lerp = Lerp(frames, lag=0.1)+ lerp = Lerp(frames, lag=SHORT_LAG, n=SHORT)index = {id(m): k for k, m in enumerate(mirrored)}def mirror_lerp(a: Image.Image, b: Image.Image, t: float) -> Image.Image:return ImageOps.mirror(lerp(frames[index[id(a)]], frames[index[id(b)]], t))- _, record = repair.repair_jumps(frames, Lerp(frames, lag=0.1))+ _, record = repair.repair_jumps(frames, Lerp(frames, lag=SHORT_LAG, n=SHORT))_, flipped = repair.repair_jumps(mirrored, mirror_lerp)assert _ease(flipped)["frames"] == _ease(record)["frames"] and _ease(flipped)["outcome"] == "accepted"+ assert _ease(record)["outcome"] == "accepted"# --- the search (repair.ease_times) -------------------------------------------------------------------------------@@ -255,19 +312,19 @@ class NearestLerp(Lerp):def _cut(tmp_path, frames: list[Image.Image], **kw):return loop_mod.run_loop(_keyed(tmp_path, frames), tmp_path / "out", fps=24.0, state="walk", min_len=None, max_len=None,n_out=None, seam_max=1000.0, name="w", report_path=tmp_path / "w.json", cycle_mode="fixed",- start=0, length=N, anchor="none", **kw)+ start=0, length=len(frames), anchor="none", **kw)def test_video_loop_eases_the_rush_and_plays_the_frames_made(tmp_path, capsys):- frames = _walk(lag=0.1)- lerp = NearestLerp(frames, lag=0.1)+ frames = _walk(SHORT, lag=SHORT_LAG)+ lerp = NearestLerp(frames, lag=SHORT_LAG, n=SHORT)rep = _cut(tmp_path, frames, interpolate=lerp)jr = rep["jump_repair"]round_ = _ease(jr)assert round_["outcome"] == "accepted" and jr["applied"] is True and jr["surge_after"] is Noneassert set(round_["replaced"]) <= set(jr["replaced"]) and rep["seam_measurement"] == "rendered-cells"cells = sorted((tmp_path / "out" / "cycle").glob("frame-*.png"))- assert len(cells) == N # the loop keeps its length+ assert len(cells) == SHORT # the loop keeps its lengthmade = {k: m for m in round_["made"] if m["proposal"] is not None for k in m["frames"]}assert made and len(made) == len(lerp.made)for k, image in zip(sorted(made, key=round_["frames"].index), lerp.made):@@ -298,8 +355,10 @@ def test_a_stretch_never_leaves_more_than_half_the_loop_made(monkeypatch):frames = _walk(14, lag=0.15)monkeypatch.setattr(repair, "MADE_SHARE", 1.0)_, unbound = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14))- assert _ease(unbound)["width"] == 4 and len(unbound["replaced"]) == 8+ assert _ease(unbound)["width"] == 4 and len(_ease(unbound)["frames"]) == 8monkeypatch.setattr(repair, "MADE_SHARE", 0.5)- _, record = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14))+ flow_only, _ = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14), ease=False)+ out, record = repair.repair_jumps(frames, Lerp(frames, lag=0.15, n=14))round_ = _ease(record)assert round_["outcome"] == "accepted" and round_["width"] == 3 and len(record["replaced"]) <= 14 // 2+ _kept_rest(frames, flow_only, out, round_)