Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
test: video-stop leaves the loop where the clip reaches it, plays the start back to the first frame, takes the drift out and refuses a jump or another motion (red)
Soohong Kim7a612d927332
diff --git a/tests/video/test_stop.py b/tests/video/test_stop.pynew file mode 100644index 0000000..540649d--- /dev/null+++ b/tests/video/test_stop.py@@ -0,0 +1,296 @@+# SPDX-License-Identifier: Apache-2.0+"""`video-stop`: a run that stops, cut from the clip's own start played backwards — on synthetic runners.++A synthetic clip opens on a standing runner, sets it off (its stride grows over a few frames) and runs it+in place; `video-loop --cycle fixed` cuts a later cycle. The stop must leave the loop where the clip+reaches it, play the start backwards to the clip's first frame without sliding, and refuse a start that+reaches the loop by a jump or holds another motion before it.+"""+from __future__ import annotations++import json+import math+from pathlib import Path++import numpy as np+import pytest+from PIL import Image, ImageDraw++from sprite_gen.compose import game_pack+from sprite_gen.util.resample import resize_cell+from sprite_gen.video import evidence, loop, stop++SS = 4 # drawn this many times larger and scaled down, so a slow swing moves by sub-pixels+W, H = 240, 260+GROUND = 236+PERIOD = 16+REST = 6+SET_OFF = 8+++def runner(*, run: float, phase: float, dx: float = 0.0, dy: float = 0.0, arm_up: float = 0.0, crouch: float = 0.0,+ ghost: bool = False) -> Image.Image:+ """A side-view runner on GROUND: head, torso, two legs of different colours and an arm. `run` takes it from standing+ (0: upright, legs together, arm down) to running (1: leaning in, hips low, legs and arm swinging at `phase`, the body+ bobbing with the stride); `arm_up` raises the arm and `crouch` lowers the body instead. `ghost` lays a part-covered+ copy of the front leg a little behind it, the band a video model leaves between two drawings."""+ big = Image.new("RGBA", (W * SS, H * SS))+ d = ImageDraw.Draw(big)+ amp = run+ cx = 110 + dx+ bob = 5 * amp * abs(math.sin(phase)) + 30 * crouch + 10 * run+ hip = GROUND - 78 + bob + dy+ shoulder = hip - 64+ lean = 14 * run++ def line(p, q, width, fill):+ d.line([tuple(round(v * SS) for v in p), tuple(round(v * SS) for v in q)], fill=fill, width=round(width * SS))++ def foot(sign):+ swing = sign * amp * 34 * math.sin(phase)+ lift = amp * 16 * max(0.0, sign * math.cos(phase))+ return (cx + swing, GROUND + dy - lift - 2)++ if ghost:+ line((cx - 8, hip), (foot(1)[0] - 14, foot(1)[1]), 12, (240, 200, 40, 110))+ line((cx, hip), foot(-1), 12, (30, 60, 90, 255))+ line((cx, hip), foot(1), 12, (240, 200, 40, 255))+ d.polygon([tuple(round(v * SS) for v in p) for p in ((cx - 16 + lean, shoulder), (cx + 16 + lean, shoulder),+ (cx + 16, hip + 4), (cx - 16, hip + 4))], fill=(20, 90, 180, 255))+ d.ellipse(tuple(round(v * SS) for v in (cx - 15 + lean, shoulder - 34, cx + 15 + lean, shoulder - 4)), fill=(230, 180, 140, 255))+ if arm_up:+ hand = (cx + 20, shoulder - 30 - 30 * arm_up)+ else:+ angle = amp * 0.9 * math.sin(phase + math.pi)+ hand = (cx + 52 * math.sin(angle), shoulder + 52 * math.cos(angle))+ line((cx + lean, shoulder + 6), (hand[0] + lean, hand[1]), 9, (200, 40, 40, 255))+ return big.convert("RGBa").resize((W, H), Image.Resampling.BOX).convert("RGBA")+++def clip(tmp_path: Path, *, cycles: int = 3, drift: tuple[float, float] = (0.0, 0.0), other: int = 0,+ instant: bool = False, ghost_at: int | None = None) -> tuple[Path, int]:+ """Keyed frames of a clip: REST standing frames, a setting-off of SET_OFF frames (none when `instant`: the stride+ is there at once), `other` frames of another motion (arm raised and waved, legs crouched, no stride), then the+ run, `cycles` cycles of PERIOD frames. `drift` moves the body across the frame while it sets off. Returns the+ folder and the first frame of the run."""+ frames = [runner(run=0.0, phase=0.0) for _ in range(REST)]+ ramp = 0 if instant else SET_OFF+ for k in range(other):+ frames.append(runner(run=0.0, phase=0.0, arm_up=0.5 + 0.5 * abs(math.sin(k * math.pi / 5)), crouch=0.4 + 0.3 * math.sin(k * 0.7)))++ def setting_off(k: int, ghost: bool = False) -> Image.Image:+ t = (k + 1) / (ramp + 1)+ return runner(run=t, phase=2 * math.pi * k / PERIOD, dx=drift[0] * t, dy=drift[1] * t, ghost=ghost)++ frames += [setting_off(k) for k in range(ramp)]+ run_from = len(frames)+ for k in range(cycles * PERIOD):+ frames.append(runner(run=1.0, phase=2 * math.pi * (ramp + k) / PERIOD, dx=drift[0], dy=drift[1]))+ if ghost_at is not None:+ frames[ghost_at] = setting_off(ghost_at - REST - other, ghost=True)+ keyed = tmp_path / "keyed"+ keyed.mkdir()+ for i, im in enumerate(frames):+ im.save(keyed / f"frame-{i + 1:04d}.png")+ return keyed, run_from+++def cut(tmp_path: Path, keyed: Path, start: int, *, name: str = "run") -> Path:+ """The loop as video-loop cuts it: one cycle from `start`, as filmed."""+ loop.run_loop(keyed, tmp_path / "loop", fps=24.0, state="run", min_len=None, max_len=None, n_out=None, seam_max=1000.0,+ name=name, report_path=None, cycle_mode="fixed", start=start, length=PERIOD, anchor="none", repair="off")+ return tmp_path / "loop" / f"{name}.strip.json"+++def cells_of(meta_path: Path) -> tuple[dict, list[Image.Image]]:+ meta = json.loads(meta_path.read_text())+ strip = Image.open(meta_path.with_name(meta_path.name[:-5] + ".png")).convert("RGBA")+ return meta, [strip.crop((k * meta["w"], 0, (k + 1) * meta["w"], meta["h"])) for k in range(meta["frames"])]+++def hip_x(im: Image.Image) -> float:+ """Where the synthetic runner's body is across: the middle of its torso's lowest rows, over the hip the legs swing from."""+ a = np.asarray(im.convert("RGBA")).astype(int)+ torso = (np.abs(a[..., 0] - 20) < 8) & (np.abs(a[..., 1] - 90) < 8) & (np.abs(a[..., 2] - 180) < 8) & (a[..., 3] == 255)+ ys, xs = np.nonzero(torso)+ low = ys >= ys.max() - 6+ return float(xs[low].mean())+++def make(tmp_path: Path, keyed: Path, meta: Path, **kwargs) -> dict:+ return stop.make_stop(keyed, meta, tmp_path / "stop", **kwargs)+++def test_stop_plays_the_start_backwards_from_the_loop_to_the_first_frame(tmp_path):+ keyed, run_from = clip(tmp_path)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ out = make(tmp_path, keyed, meta)+ assert out["status"] == "passed"+ shown = out["shown"]+ # It reaches the loop where the run starts, and plays back down through the setting-off to the still.+ assert run_from - 2 <= out["entry"]["frame"] <= run_from+ assert shown == sorted(shown, reverse=True) and shown[-1] == 0+ assert all(j > out["rest"]["end"] for j in shown[:-1]) and out["rest"]["end"] >= REST - 1+ # The cell it leaves is the loop's cell at the entry's phase: the run is cut a cycle on, so the same phase.+ assert out["entry"]["from_cell"] == (out["entry"]["frame"] - (run_from + PERIOD)) % PERIOD+ assert out["seam"]["steps"] <= stop.JUMP_RATIO and out["run_up"]["cycles"] <= stop.RUN_UP_MAX_CYCLES+ # The last cell is the clip's first frame — the still as filmed — at the loop's scale, pixel for pixel.+ smeta, cells = cells_of(tmp_path / "stop" / "stop.strip.json")+ lmeta = json.loads(meta.read_text())+ first = Image.open(sorted(keyed.glob("*.png"))[0]).convert("RGBA")+ first = resize_cell(first, (round(first.width * lmeta["scale"]), round(first.height * lmeta["scale"])))+ assert cells[-1].crop(cells[-1].getbbox()).tobytes() == first.crop(first.getbbox()).tobytes()+ # The one-shot contract other single plays keep: plays once and holds the still, sized as its strip.+ assert (smeta["loop"], smeta["hold_last"], smeta["kind"], smeta["end_frame"]) == (False, True, "stop", len(shown) - 1)+ assert smeta["frames"] == len(shown) and smeta["source_cut"]["frames"] == shown+ assert smeta["from_loop"]["from_cell"] == out["entry"]["from_cell"] and smeta["body_h"] == lmeta["body_h"]+ assert smeta["source_cut"]["source"]["sha256"] == lmeta["source_cut"]["source"]["sha256"]+ assert out["source"]["same_as_loop"] is True+ with Image.open(tmp_path / "stop" / "stop.gif") as gif:+ assert gif.n_frames == len(shown) and gif.info.get("loop") is None+ report = json.loads((tmp_path / "stop" / "stop.stop.report.json").read_text())+ assert report["status"] == "passed" and report["shown"] == shown+++def test_stop_does_not_slide_when_the_body_drifts_as_it_sets_off(tmp_path):+ """A clip whose body moves 36 px across and 18 px up while it sets off, beside the same clip filmed in place: played+ back as filmed, the first would slide that far into the still. Its stop is the in-place clip's, the drift taken out+ to the pixel: the body stays over the loop's place, the still on the loop's ground."""+ runs = {}+ for label, drift in (("in-place", (0.0, 0.0)), ("drifts", (36.0, -18.0))):+ (tmp_path / label).mkdir()+ keyed, run_from = clip(tmp_path / label, drift=drift)+ meta = cut(tmp_path / label, keyed, run_from + PERIOD)+ out = make(tmp_path / label, keyed, meta)+ smeta, cells = cells_of(tmp_path / label / "stop" / "stop.strip.json")+ lmeta, lcells = cells_of(meta)+ ox, oy = smeta["loop_cell_origin"]+ hips = [hip_x(c) - ox for c in cells] # in the loop cell's own pixels+ # From the loop's last cell into the stop the body does not jump; the still stands on the loop's ground.+ assert abs(hip_x(lcells[out["entry"]["from_cell"]]) - hips[0]) < 3.0+ assert out["drift"]["ends_on_ground"] is True+ assert stop._box(cells[-1])[3] - 1 == max(stop._box(c)[3] - 1 for c in lcells) + oy+ # What moves the hips is the standing pose laid by its coverage on a run that leans 14 px: a few pixels.+ assert max(hips) - min(hips) < 9.0+ runs[label] = (out, hips)+ (still, still_hips), (drifts, drift_hips) = runs["in-place"], runs["drifts"]+ # The still lands where the in-place clip's does, the whole drift taken out; the frames between take a straight+ # share of it per step, and this drift was not straight over them (none on the last rest frames): a tenth of it left.+ taken = [b - a for a, b in zip(still["drift"]["clip_px"], drifts["drift"]["clip_px"])]+ assert taken == [36, -18] and still_hips[-1] == drift_hips[-1]+ assert max(abs(a - b) for a, b in zip(still_hips, drift_hips)) <= 0.1 * 36 + 0.5+++def test_stop_refuses_a_start_that_holds_another_motion(tmp_path):+ """Two and a half cycles of another motion (an arm waved high, the body crouched) between the still and the run:+ the stop would play it backwards. Refused by name, the report written; an explicit --run-up-max lets it through."""+ keyed, run_from = clip(tmp_path, other=2 * PERIOD + PERIOD // 2)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ with pytest.raises(SystemExit, match="another motion"):+ make(tmp_path, keyed, meta)+ report = json.loads((tmp_path / "stop" / "stop.stop.report.json").read_text())+ assert report["status"] == "failed" and report["run_up"]["cycles"] > stop.RUN_UP_MAX_CYCLES+ assert not (tmp_path / "stop" / "stop.strip.png").exists()+ out = make(tmp_path, keyed, meta, run_up_max=4.0)+ assert out["status"] == "passed" and out["run_up_max_cycles"] == 4.0+++def test_stop_refuses_a_seam_past_the_jump_line(tmp_path):+ """A clip that runs at once from the still reaches the loop by a jump: the stop would leave the run for the+ standing pose in one frame."""+ keyed, run_from = clip(tmp_path, instant=True)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ with pytest.raises(SystemExit, match="jump line"):+ make(tmp_path, keyed, meta)+ report = json.loads((tmp_path / "stop" / "stop.stop.report.json").read_text())+ assert report["status"] == "failed" and report["seam"]["steps"] > stop.JUMP_RATIO+++def test_stop_leaves_an_aligned_loop_from_the_cell_nearest_the_entry(tmp_path):+ """A loop turned to start elsewhere (as video-cycle-align turns one): the cell left from follows the pose, not the index."""+ keyed, run_from = clip(tmp_path)+ meta_path = cut(tmp_path, keyed, run_from + PERIOD)+ meta, cells = cells_of(meta_path)+ turn = 5+ turned = cells[turn:] + cells[:turn]+ strip = Image.new("RGBA", (meta["w"] * len(turned), meta["h"]))+ for k, c in enumerate(turned):+ strip.paste(c, (k * meta["w"], 0))+ aligned = tmp_path / "loop" / "run.aligned.strip.json"+ strip.save(aligned.with_name("run.aligned.strip.png"))+ aligned.write_text(json.dumps(meta))+ plain = make(tmp_path, keyed, meta_path, name="plain")+ turned_out = make(tmp_path, keyed, aligned, name="turned")+ assert turned_out["entry"]["frame"] == plain["entry"]["frame"]+ assert turned_out["entry"]["from_cell"] == (plain["entry"]["from_cell"] - turn) % PERIOD+ assert turned_out["shown"] == plain["shown"]+++def test_stop_speed_and_frame_cap(tmp_path):+ keyed, run_from = clip(tmp_path)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ once = make(tmp_path, keyed, meta, name="once")+ twice = make(tmp_path, keyed, meta, name="twice", speed=2)+ played = once["shown"]+ assert twice["shown"] == played[::2] + ([] if played[::2][-1] == 0 else [0])+ capped = make(tmp_path, keyed, meta, name="capped", max_frames=4)+ assert len(capped["shown"]) == 4 and capped["shown"][0] == played[0] and capped["shown"][-1] == 0+ assert capped["strip"]["frames"] == 4 and capped["strip"]["delay_ms"] == once["strip"]["delay_ms"]+ with pytest.raises(SystemExit, match="--speed"):+ make(tmp_path, keyed, meta, name="x", speed=3)+ with pytest.raises(SystemExit, match="--max-frames"):+ make(tmp_path, keyed, meta, name="x", max_frames=1)+++def test_stop_names_a_filmed_ghost_and_keeps_it(tmp_path):+ keyed, run_from = clip(tmp_path, ghost_at=REST + SET_OFF - 3)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ out = make(tmp_path, keyed, meta)+ assert out["status"] == "passed" and out["ghost_screen"]["reads"] is True+ named = [g["clip_frame"] for g in out["ghost_screen"]["ghosts"]]+ assert named == [REST + SET_OFF - 3]+ assert REST + SET_OFF - 3 in out["shown"]+++def test_stop_refuses_a_loop_it_cannot_leave(tmp_path):+ keyed, run_from = clip(tmp_path, cycles=2)+ meta_path = cut(tmp_path, keyed, run_from + PERIOD)+ meta = json.loads(meta_path.read_text())+ for change, match in (({"state": "idle"}, "walk or run"), ({"loop": False}, "plays once"), ({"source_cut": None}, "source cut")):+ bad = tmp_path / "loop" / "bad.strip.json"+ bad.with_name("bad.strip.png").write_bytes(meta_path.with_name("run.strip.png").read_bytes())+ bad.write_text(json.dumps({**meta, **change}))+ with pytest.raises(SystemExit, match=match):+ make(tmp_path, keyed, bad, name="bad")+ few = tmp_path / "few"+ few.mkdir()+ for f in sorted(keyed.glob("*.png"))[:run_from]:+ (few / f.name).write_bytes(f.read_bytes())+ with pytest.raises(SystemExit, match="holds"):+ make(tmp_path, few, meta_path, name="few")+++def test_game_pack_packs_the_stop_to_play_once(tmp_path):+ keyed, run_from = clip(tmp_path, cycles=2)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ out = make(tmp_path, keyed, meta)+ request = tmp_path / "game.json"+ request.write_text(json.dumps({"version": 1, "assets": [+ {"id": "hero_run_side", "source": str(meta), "display": {"body_height": 56}},+ {"id": "hero_stop_side", "source": str(tmp_path / "stop" / "stop.strip.json"), "display": {"body_height": 56}}]}))+ packed = game_pack.pack(request, tmp_path / "pack")+ run_anim, stop_anim = packed["animations"]["hero_run_side"], packed["animations"]["hero_stop_side"]+ assert run_anim["loop"] is True+ assert (stop_anim["loop"], stop_anim["hold_last"]) == (False, True) and len(stop_anim["frames"]) == len(out["shown"])+ # Drawn at their pivots, the loop's cell lies where the stop's sidecar says it does: the stop stands where the run did.+ f = stop_anim["size"]["w"] / out["strip"]["w"]+ origin = out["strip"]["loop_cell_origin"]+ for axis in (0, 1):+ assert abs(stop_anim["anchor"][axis] - origin[axis] * f - run_anim["anchor"][axis]) <= 1.0+++def test_stop_source_identity_is_the_loop_cut_from(tmp_path):+ keyed, run_from = clip(tmp_path, cycles=2)+ meta = cut(tmp_path, keyed, run_from + PERIOD)+ files = sorted(keyed.glob("*.png"))+ assert json.loads(meta.read_text())["source_cut"]["source"] == evidence.source_identity(files, 24.0)