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 frame between that shows a picture beside it again is not taken, in the held fill and at --out-fps
Soohong Kimb2b624f9080b
diff --git a/sprite_gen/video/held_fill.py b/sprite_gen/video/held_fill.pyindex 5880d7e..1275880 100644--- a/sprite_gen/video/held_fill.py+++ b/sprite_gen/video/held_fill.py@@ -29,15 +29,18 @@ its held-drawings reading (`held.measure_cycle`, which `video-cycle-align` readsthat long, but a run cut short at the cut's first or last cell: a clip carried to a higher rate by repeating framesreads a hold too (24 fps carried to 60 shows each frame two or three times), its runs two and three cells by turns, anda clip that stalls now and then reads none. A frame made-is judged as the frames the alignment makes are (`align.measure_made`, `faults`), and for a part standing still on-both sides of it (`still_kept`: a foot planted, a hand on a ledge), which must stand where it stood; a frame refused is-made again at another fraction of RETIME_T within half a cell of its own time, else the run is kept as filmed and says-why. A run is made whole or kept whole: half made it would step back.--`upsample` gives a cut at a rate above its own (`--out-fps`) its frames between, at their own times, never a frame-shown again; a cell where both frames beside its time show one picture (a hold kept) is that picture. `at_times` makes-a cut's cells at any times so: `upsample`'s, and the air a single play's air retime gives a new length-(`root_motion.retime`, docs/video-pipeline.md "Root motion")."""+is judged as the frames the alignment makes are (`align.measure_made`, `faults`), for a part standing still on both+sides of it (`still_kept`: a foot planted, a hand on a ledge), which must stand where it stood, and for a picture of its+own (`same_picture`, `held`): one that shows the picture of either frame it is made between, or of the cell before it,+is no frame between but a held cell again. A frame refused is made again at another fraction of RETIME_T within half a+cell of its own time, else the run is kept as filmed and says why. A run is made whole or kept whole: half made it would+step back.++`upsample` gives a cut at a rate above its own (`--out-fps`) its frames between, at their own times, judged as the+fill's are, never a frame shown again; a cell where both frames beside its time show one picture (a hold kept) is that+picture, and one no frame made is taken for is the nearer frame, named. `at_times` makes a cut's cells at any times+so: `upsample`'s, and the air a single play's air retime gives a new length (`root_motion.retime`,+docs/video-pipeline.md "Root motion")."""from __future__ import annotations@@ -93,6 +96,11 @@ def same_steps(frames: Sequence[Image.Image]) -> list[bool]:for k in range(len(frames) - 1)]+def same_picture(frame: Image.Image, others: Sequence[Image.Image]) -> bool:+ """Whether `frame` shows the same picture as one of `others`: each pair read as `same_steps` reads a step."""+ return any(same_steps([other, frame])[0] for other in others)++def runs(same: Sequence[bool]) -> list[dict[str, int]]:"""Each run of cells showing one picture: its first cell and how many (two or more). Not read across a wrap."""found, k = [], 0@@ -152,28 +160,34 @@ def still_kept(frame: Image.Image, a: Image.Image, b: Image.Image) -> dict[str,return {"reads": True, "still_px": int(still.sum()), "kept": round(least, 4), "at_row": at, "window_px": win}-def judge(frame: Image.Image, a: Image.Image, b: Image.Image, t: float, *, reads: bool) -> tuple[dict[str, Any], list[str]]:+def judge(frame: Image.Image, a: Image.Image, b: Image.Image, t: float, *, reads: bool,+ before: Image.Image | None = None) -> tuple[dict[str, Any], list[str]]:"""A frame made between `a` and `b` at `t`: the alignment's measure and faults (`align.measure_made`, `faults`),- and `contact` where a part standing still on both sides does not stand where it stood (`still_kept`)."""+ `contact` where a part standing still on both sides does not stand where it stood (`still_kept`), and `held` where+ it shows the same picture as `a`, `b` or the cell delivered `before` it (`same_picture`): no frame between, but a+ held cell again."""from sprite_gen.video.align import measure_made # align imports loop, which imports this modulemeasure = measure_made(frame, a, b, t, reads=reads)contact = still_kept(frame, a, b)- wrong = faults(measure) + (["contact"] if contact["reads"] and contact["kept"] < STILL_KEPT_MIN else [])+ held = same_picture(frame, (a, b) if before is None else (a, b, before))+ wrong = (faults(measure) + (["contact"] if contact["reads"] and contact["kept"] < STILL_KEPT_MIN else [])+ + (["held"] if held else []))return {**measure, "contact": contact}, wrongdef _made(interpolate: rife_mod.Interpolate, a: Image.Image, b: Image.Image, t: float, window: tuple[float, float], *,- reads: bool) -> tuple[Image.Image | None, dict[str, Any]]:+ reads: bool, before: Image.Image | None = None) -> tuple[Image.Image | None, dict[str, Any]]:"""The frame between `a` and `b` at `t`, or at the fraction of RETIME_T nearest it inside `window` that is made- with no fault and, made only off its time, with its ghost band under RETIME_WINDOW_MAX in every window (align's- bar for a frame made again at another fraction); None and what each try was refused for, where none is."""+ with no fault (`judge`, `before` the cell delivered before it, None where that is `a`) and, made only off its time,+ with its ghost band under RETIME_WINDOW_MAX in every window (align's bar for a frame made again at another+ fraction); None and what each try was refused for, where none is."""from sprite_gen.video.align import RETIME_T, RETIME_WINDOW_MAXtries = []for at in [t] + sorted((r for r in RETIME_T if window[0] < r < window[1] and abs(r - t) > 1e-3), key=lambda r: (abs(r - t), r)):frame = interpolate(a, b, at)- measure, wrong = judge(frame, a, b, at, reads=reads)+ measure, wrong = judge(frame, a, b, at, reads=reads, before=before)seen = at != t and (measure.get("ghost_window") or 0.0) >= RETIME_WINDOW_MAXtries.append({"t": round(at, 4), "faults": wrong, **({"window_over": RETIME_WINDOW_MAX} if seen and not wrong else {})})if not wrong and not seen:@@ -244,7 +258,8 @@ def fill(frames: list[Image.Image], interpolate: rife_mod.Interpolate | None, *,for c in cells:t = (c - a) / (b - a)half = 0.5 / (b - a)- frame, how = _made(interpolate, frames[a], frames[b % n], t, (t - half, t + half), reads=reads)+ frame, how = _made(interpolate, frames[a], frames[b % n], t, (t - half, t + half), reads=reads,+ before=taken[-1][1] if taken else None)tried.append({"cell": c, "between": [a, b % n], **how})if frame is None:break@@ -307,7 +322,8 @@ def at_times(frames: list[Image.Image], interpolate: rife_mod.Interpolate, at: Scontinuet = tau - ahalf = (spacing if isinstance(spacing, (int, float)) else spacing[k]) / 2- frame, how = _made(interpolate, frames[a], frames[(a + 1) % n], t, (t - half, t + half), reads=reads)+ frame, how = _made(interpolate, frames[a], frames[(a + 1) % n], t, (t - half, t + half), reads=reads,+ before=out[-1] if out else None)if frame is None:out.append(frames[near % n])shown_again.append(k)diff --git a/tests/video/test_held_fill.py b/tests/video/test_held_fill.pyindex ca123f7..10a3b25 100644--- a/tests/video/test_held_fill.py+++ b/tests/video/test_held_fill.py@@ -156,6 +156,30 @@ def test_a_stalled_walk_has_its_held_cells_made_between_the_poses_beside_them(tmassert rep["seam_measurement"] == "rendered-cells"+def test_an_interpolator_that_gives_the_pose_back_leaves_the_stalled_walk_as_filmed_and_says_so(tmp_path, capsys):+ # No frame between at all: each frame it gives is the frame before, a held cell itself. Tried again at other+ # fractions, never taken: the cut is delivered as filmed, its record and its warnings say both runs are still held.+ calls: list[float] = []++ def stuck(a, b, t):+ calls.append(t)+ return a++ rep = _run(tmp_path, shown_again(walk(), STALLED), interpolate=stuck, **STALLED_CUT)+ assert rep["status"] == "passed"+ record = rep["held_fill"]+ assert record["made"] == [] and "interpolator" not in record+ runs = record["before"]["held_runs"]+ assert [(r["start"], r["length"], r["filled"]) for r in runs] == [(5, 2, False), (14, 3, False)]+ assert all("held" in r["faults"] and len(r["tries"][0]["tries"]) > 1 for r in runs)+ assert len(calls) == sum(len(r["tries"][0]["tries"]) for r in runs)+ assert (record["cells"], record["poses"], record["held_runs"]) == (+ N + 3, N, [{"start": 5, "length": 2}, {"start": 14, "length": 3}])+ assert _pictures(Path(rep["out_dir"]), "m") == STALLED_OFF+ err = capsys.readouterr().err+ assert "held cells 5-6 kept as filmed" in err and "held cells 14-16 kept as filmed" in err++def test_fill_held_off_cuts_the_stalled_walk_as_before_and_still_names_its_runs(tmp_path):rep = _run(tmp_path, shown_again(walk(), STALLED), fill_held="off", **STALLED_CUT)assert _pictures(Path(rep["out_dir"]), "m") == STALLED_OFF@@ -238,7 +262,13 @@ def test_a_once_cut_and_a_one_shot_have_their_held_cells_made_too(tmp_path):record = rep["held_fill"]start = rep["cycle"]["start"]assert [(r["start"], r["filled"]) for r in record["before"]["held_runs"] if r["filled"]] == [(20 - start, True)]- assert between.calls == [(20.0, 21.0, 0.5)]+ # Half way, and a twentieth either side, the body has sunk less than a pixel: each such frame shows frame 20 again+ # and is refused (`held`). The cell is made at the nearest fraction where the body has moved.+ assert between.calls == [(20.0, 21.0, t) for t in (0.5, 0.45, 0.55, 0.4, 0.6)]+ made = next(r for r in record["frames"] if r["cell"] == 21 - start)+ assert (made["t"], made["from_t"]) == (0.6, 0.5)+ assert [t["faults"] for t in made["tries"]] == [["held"]] * 4 + [[]]+ assert all(r["start"] != 20 - start for r in record["held_runs"]) # read again, the cell made is a pose of its ownhopped = shown_again(hop(), {18: 2})rep = _run(tmp_path, hopped, "hop", interpolate=lambda a, b, t: Image.blend(a, b, t), state="jump", cycle_mode="one-shot")assert any(r["start"] + rep["cycle"]["start"] == 18 for r in rep["held_fill"]["before"]["held_runs"])@@ -291,6 +321,38 @@ def test_a_frame_between_that_cross_fades_two_poses_is_not_taken():assert all(not r["filled"] and "ghost" in r["faults"] for r in record["before"]["held_runs"])[email protected]("given", ["the frame before", "the frame after"])+def test_a_frame_between_that_shows_a_pose_beside_it_again_is_not_taken(given):+ frames = shown_again(walk(), STALLED)[: N + 3]+ calls: list[float] = []++ def back(a, b, t):+ calls.append(t)+ return a if given == "the frame before" else b++ out, record = held_fill.fill(frames, back, ring=True)+ assert record["made"] == [] and [o.tobytes() for o in out] == [f.tobytes() for f in frames]+ runs = record["before"]["held_runs"]+ assert all(not r["filled"] and r["faults"] == ["held"] for r in runs)+ # Each run's first cell tried at its own time, then at the other fractions within half a cell of it.+ assert all(len(r["tries"][0]["tries"]) > 1 and all(t["faults"] == ["held"] for t in r["tries"][0]["tries"]) for r in runs)+ assert (record["poses"], record["held_runs"]) == (N, [{"start": 5, "length": 2}, {"start": 14, "length": 3}])+++def test_a_run_whose_frames_between_show_one_picture_twice_is_kept_as_filmed():+ # An interpolator that draws the time half way whatever it is asked: a run of two is made, a run of three would+ # show its one frame between on both its cells.+ frames = shown_again(walk(), STALLED)[: N + 3]+ between = Between(walk_at, range(N + 6), period=N)+ out, record = held_fill.fill(frames, lambda a, b, t: between(a, b, 0.5), ring=True)+ runs = record["before"]["held_runs"]+ assert [(r["start"], r["filled"]) for r in runs] == [(5, True), (14, False)]+ assert runs[1]["faults"] == ["held"] and [row["cell"] for row in runs[1]["tries"]] == [15, 16]+ assert record["made"] == [6] and out[6].tobytes() == walk_at(5.5).tobytes()+ assert [o.tobytes() for o in out[14:17]] == [f.tobytes() for f in frames[14:17]]+ assert record["held_runs"] == [{"start": 14, "length": 3}]++def test_the_opening_and_the_closing_pose_of_a_cut_played_once_are_kept():frames = _planted([0, 0, 1, 2, 3, 4, 4])calls: list = []@@ -371,6 +433,27 @@ def test_a_one_shot_asked_for_at_60_fps_keeps_its_first_and_last_frame(tmp_path)assert record["out_fps"]["applied"] is True+def test_a_cell_at_a_higher_rate_whose_frame_between_shows_a_frame_again_is_named_shown_again():+ # Five frames at 24 fps given at 60: eleven cells, three on a frame's time. An interpolator that gives back the frame+ # before makes none of the eight between: each is the nearer frame, named.+ frames = walk()[:5]+ out, record = held_fill.upsample(frames, lambda a, b, t: a, ring=False, fps=24.0, out_fps=60.0)+ assert record["made"] == 0 and record["shown_again"] == [1, 2, 3, 4, 6, 7, 8, 9]+ assert all(t["faults"] == ["held"] for m in record["frames"] for t in m["tries"])+ assert [o.tobytes() for o in out] == [frames[round(k * 0.4)].tobytes() for k in range(11)]+++def test_two_cells_at_a_higher_rate_between_one_pair_are_not_one_picture():+ # Given at 60, the cells at 0.4 and 0.8 of a step both lie between its two frames. An interpolator that draws the+ # time half way whatever it is asked makes the first; the second would show it again, and is the nearer frame.+ frames = walk()[:5]+ between = Between(walk_at, range(5))+ out, record = held_fill.upsample(frames, lambda a, b, t: between(a, b, 0.5), ring=False, fps=24.0, out_fps=60.0)+ assert record["shown_again"] == [2, 4, 7, 9] and record["made"] == 4+ assert out[1].tobytes() == walk_at(0.5).tobytes() and out[2].tobytes() == frames[1].tobytes()+ assert out[3].tobytes() == walk_at(1.5).tobytes() and out[4].tobytes() == frames[2].tobytes()++def test_no_rife_delivers_a_clip_asked_for_at_a_higher_rate_at_its_own(tmp_path, monkeypatch, capsys):_no_rife(monkeypatch, tmp_path)rep = _run(tmp_path, walk(), "walk60", out_fps=60.0, state="walk", cycle_mode="fixed", start=0, length=N,