Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(video): video-canvas --shape wide gives a walk or run no head-room
Soohong Kim6a9b98dfc777
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d31c7c5..722b974 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,9 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the## Unreleased+- **Output change: `video-canvas --shape wide` gives a walk or run no head-room.** A forced-wide canvas gave every state a jump's 35 % above the still, and above a square still that head-room set the width as well (2800 × 1575 for 1024 × 1024, 1216 columns in front of the still); a walker that small in its frame is one a video model may reframe. A walk or run forced wide now has a row of its own (`canvas.GAIT_WIDE`, read through `STATE_SHAPE_CANVAS`): 28 % in front, 20 % behind, nothing above — 1969 × 1108 for a 1024 × 1024 still. A jump, and every other state forced wide, keeps the forced-wide row (`WIDE_OVERRIDE`) and its canvas byte for byte; `--headroom` still sets the room above. [docs/video-pipeline.md](docs/video-pipeline.md#1-canvas--the-input-frame-decides-the-output-frame).+- **`tests/video/test_edge_speck_lead_in.py`**: a walk or run forced wide is 1969 × 1108 with nothing above, a jump still 2800 × 1575.+- **Fix: `video-loop` leaves a walk's lead-in out of the search.** A video model may reframe a small subject in a clip's first frames — the body grows or shrinks by a third or more in under half a second, then walks at its new size. Searched with those frames, the motion analysis read the reframing as the clip's motion and lost the walk's repeat, and the gait fallback, scaling back by a line through the clip, read the step as steady growth and shrank the walk. A walk or run's sizes are now read one cycle on (`gait_fallback.cycle_drift`, the size hold's model) and the frames from the first on that are off it by 10 % or more (`LEAD_IN_MIN`) are its lead-in (`gait_fallback.lead_in`); `--cycle auto` and `periodic` — the size hold, the motion analysis, the cycle search and the gait fallback — read the clip after it as a clip of its own, window included. Every frame number the report gives is the clip's own: `cycle.start`, each candidate's `start` and `context_pair_range`, a refusal's windows too, so `--cycle fixed --start` taken from them cuts the frames they name. Every walk and run report gains `lead_in` (`frames`, `height_change`, `first_off`, `min`, `method`, `search_from`); a clip too short to read its size one cycle on has none (`why`). A cut the caller names (`fixed`, `pinned`) is the caller's frames. `cycle.coverage` counts the frames the search read. The gait fallback now scales back by the size read one cycle on, as the hold does, so a first pose settling into the walk no longer hides a body that grows from it (`gait_fallback.scale_drift` gains `method: one-cycle-on` in the report); a clip that reaches the fallback with 3 % or more of change is scaled by that model now, and its pixels and the drift its refusal quotes can differ in the last places. A walk or run with no lead-in that the first search cuts is cut as before, its strip, GIF, WebP, cycle frames and `strip.json` byte for byte. [docs/video-pipeline.md](docs/video-pipeline.md#4-loop--period-first-seam-second-then-the-gait-floor), [docs/loop-review.md](docs/loop-review.md#the-gait-fallback-a-slow-walk-or-a-walk-toward-the-camera).- **Fix: after a lead-in, `--body-height` reads the standing height on the cut's first frame.** The clip's first frame is the still's pose as filmed, but a walk that opens on a lead-in films it at another scale: read there, the standing height scaled the walk up into the cell cap. Such a cut now reads it on its own first frame, and `strip.json` says so (`body_ref: cut-first-frame`, `body_ref_frame`, the clip frame); `video-cycle-align` rebuilds such a strip at the same height. A clip with no lead-in reads its first frame as before, and its `strip.json` is as before. [docs/video-pipeline.md](docs/video-pipeline.md#4-loop--period-first-seam-second-then-the-gait-floor).- **`tests/video/test_edge_speck_lead_in.py`**: a walker filmed at 0.63 of its size and reframed over 9 frames: a lead-in of 5 frames, found no cycle before, now cuts its 24-frame walk after it, every frame number its own; too slow a walk after the same lead-in is refused with windows that all start after it; a fixed cut there reads its standing height on its own first frame, and `video-cycle-align` keeps it. A walker that settles from its first pose and grows 5 % reads 2.8 % on a line and was refused; read one cycle on it is scaled back and cut.diff --git a/docs/video-pipeline.md b/docs/video-pipeline.mdindex a811fe0..d7b03ae 100644--- a/docs/video-pipeline.md+++ b/docs/video-pipeline.md@@ -61,7 +61,12 @@ row with 35 % above, 28 % in front and 20 % behind — not the attack row. It lawhose own row is another shape, a jump among them, and 35 % on 16:9 keeps about the room ajump's tall row leaves above a square or upright still. A still wider than it is tall keepsless above it under forced wide than under the tall row (a 3:2 still about three fifths),-because there the wide canvas's height follows the still's width. Headroom is a fraction of the full canvas+because there the wide canvas's height follows the still's width. A walk or run forced wide+has a row of its own (`STATE_SHAPE_CANVAS`): the same 28 % in front and 20 % behind, and nothing+above — its feet stay on the ground. Above a square still the jump's 35 % set the width as well+(1575 rows on 16:9 are 2800 columns, 1216 of them in front of the still), and a walker that small+in its frame is one a video model may reframe in its first frames (section 4, "A lead-in"). A+square still forced wide as a walk is 1969 × 1108. Headroom is a fraction of the full canvasheight; wide canvases grow both dimensions to preserve their ratio without shrinkingthe still. A still whose corners are not one flat colouris refused — a non-flat background cannot be extended without guessing.@@ -676,7 +681,8 @@ gate. The state table now routes `cheer`, `wave` and `celebrate` to the wide can`video-set --shape wide` forces it for every state of a batch when the costume is thereason. The same `--shape` is what `video-canvas` already took for a single still. Theforced canvas is the forced-wide row (35 % above), so a jump in that batch keeps its-head-room; an attack in it gets that row too, not its own 20 %.+head-room; an attack in it gets that row too, not its own 20 %. A walk or run gets its own wide+row, with nothing above (section 1).## 4. Loop — period first, seam second, then the gait floordiff --git a/sprite_gen/video/canvas.py b/sprite_gen/video/canvas.pyindex a5f3ecb..8cddfc8 100644--- a/sprite_gen/video/canvas.py+++ b/sprite_gen/video/canvas.py@@ -6,7 +6,8 @@ image-to-video (2026-09-08 실측: a `3:4` request still returned 960x960). So tcanvas is decided HERE, on the still: a jump needs head-room above (tall), anattack needs room above and in front, a projectile needs room in front (wide), everything else stays square.The state -> canvas table below is the single owner of that rule; `--shape`-overrides it per call.+overrides it per call, with the state's own row for that shape where it has one (a walk or run+forced wide gets no head-room).`--fit tight` is the other way to frame: no room is added for the motion. Theempty rows above and below the subject are dropped (a little headroom stays),@@ -87,6 +88,17 @@ STATE_CANVAS: dict[str, CanvasProfile] = {# row would give it (a 3:2 still about three fifths), because the wide canvas's# height follows the still's width there.WIDE_OVERRIDE = CanvasProfile(SHAPE_WIDE, 16 / 9, 0.35, 0.28, 0.2, "forced wide: room in front and behind, and a jump's head-room above, whatever the state")+# A walk or run forced wide keeps the room in front and behind and gets none above: its feet do+# not leave the ground. Above a square still the jump's head-room set the canvas's width too —+# 1575 rows on 16:9 are 2800 columns, 1216 of them in front of the still — and a subject that+# small in its frame is one a video model may reframe in its first frames (`video-loop`'s+# lead-in). The same 16:9 with no head-room gives a square still 1969 x 1108.+GAIT_WIDE = CanvasProfile(SHAPE_WIDE, 16 / 9, 0.0, 0.28, 0.2, "forced wide for a walk or run: room in front and behind, none above — its feet stay on the ground")+# A state's own row for a forced shape, read before the shape's default (`profile_for`).+STATE_SHAPE_CANVAS: dict[tuple[str, str], CanvasProfile] = {+ ("walk", SHAPE_WIDE): GAIT_WIDE,+ ("run", SHAPE_WIDE): GAIT_WIDE,+}SHAPE_DEFAULTS: dict[str, CanvasProfile] = {SHAPE_SQUARE: STATE_CANVAS["default"],SHAPE_TALL: STATE_CANVAS["jump"],@@ -102,12 +114,13 @@ KEYS = ("auto", "green", "magenta", "white") # auto: the corners decide; white:def profile_for(state: str | None, shape: str | None = None) -> CanvasProfile:- """Resolve the canvas profile: an explicit `shape` wins, else the state's row, else default."""+ """Resolve the canvas profile: an explicit `shape` wins — the state's own row for that shape+ (`STATE_SHAPE_CANVAS`), else the shape's default — else the state's row, else default."""+ key = (state or "").strip().lower()if shape is not None:if shape not in SHAPES:raise SystemExit(f"video-canvas: unknown --shape {shape!r}; expected one of {', '.join(SHAPES)}")- return SHAPE_DEFAULTS[shape]- key = (state or "").strip().lower()+ return STATE_SHAPE_CANVAS.get((key, shape), SHAPE_DEFAULTS[shape])return STATE_CANVAS.get(key, STATE_CANVAS["default"])diff --git a/tests/video/test_attack_headroom.py b/tests/video/test_attack_headroom.pyindex d921433..26a61a5 100644--- a/tests/video/test_attack_headroom.py+++ b/tests/video/test_attack_headroom.py@@ -89,7 +89,7 @@ def test_forced_wide_keeps_035_and_never_takes_height_from_a_jump() -> None:forced = canvas.profile_for("jump", shape="wide")assert forced is canvas.WIDE_OVERRIDE and forced is not canvas.STATE_CANVAS["attack"]assert (forced.shape, forced.headroom, forced.lead, forced.trail) == (canvas.SHAPE_WIDE, 0.35, 0.28, 0.2)- assert canvas.profile_for("walk", shape="wide") is forced+ assert canvas.profile_for("idle", shape="wide") is forced # a walk or run has its own wide row (no head-room)still = _figure(1024, 120, 1000)tall, tall_rep = canvas.pad_canvas(still, canvas.profile_for("jump"))wide, wide_rep = canvas.pad_canvas(still, forced)diff --git a/tests/video/test_edge_speck_lead_in.py b/tests/video/test_edge_speck_lead_in.pyindex 8eefbd0..9460e59 100644--- a/tests/video/test_edge_speck_lead_in.py+++ b/tests/video/test_edge_speck_lead_in.py@@ -1,12 +1,13 @@# SPDX-License-Identifier: Apache-2.0-"""A fleck far from the body is not the subject touching the edge, and a clip's first frames that reframe a-small subject are not part of its walk.+"""A fleck far from the body is not the subject touching the edge, a clip's first frames that reframe a+small subject are not part of its walk, and a walk forced wide gets no room above.* `video-frames` erases a speck — a small piece far from the body — before the edge check reads the frame.* `video-loop` reads a walk's lead-in (the first frames off the walk's size) and searches the clip after it;what it finds, and what it refuses, is said in the clip's own frame numbers. The gait fallback scales backby the size read one cycle on, as the hold does. A cut after a lead-in reads the standing height on itsown first frame.+* `video-canvas --shape wide` gives a walk or run room in front and behind and none above."""from __future__ import annotations@@ -220,3 +221,37 @@ def test_a_cut_of_a_clip_with_no_lead_in_reads_its_first_frame_as_before(tmp_patassert out["lead_in"]["frames"] == 0assert meta["body_ref"] == "first-frame" and "body_ref_frame" not in metaassert meta["body_src_h"] == _height(keyed / "000.png")+++# --- a walk forced wide ---------------------------------------------------------------------------------------+++def test_a_walk_or_run_forced_wide_gets_no_room_above():+ still = Image.new("RGB", (1024, 1024), (0, 255, 0))+ still.paste((180, 60, 30), (400, 100, 600, 1000))+ for state in ("walk", "run"):+ profile = canvas.profile_for(state, shape="wide")+ assert profile is canvas.GAIT_WIDE and (profile.headroom, profile.lead, profile.trail) == (0.0, 0.28, 0.2)+ _, report = canvas.pad_canvas(still, profile)+ # 16:9 over the still's own height, the room in front and behind as forced wide gives any state.+ assert report["canvas"] == [1969, 1108] and report["offset"] == [394, 84]+ assert report["canvas"][0] == round(1024 / (1 - 0.28 - 0.2))+ # A jump, or a state with no wide row of its own, keeps the jump's head-room.+ for state in ("jump", "idle", None):+ assert canvas.profile_for(state, shape="wide") is canvas.WIDE_OVERRIDE+ _, jump = canvas.pad_canvas(still, canvas.profile_for("jump", shape="wide"))+ assert jump["canvas"] == [2800, 1575]+ # Unforced, a walk is square as before.+ assert canvas.profile_for("walk") is canvas.STATE_CANVAS["default"]+ assert math.isclose(canvas.GAIT_WIDE.ratio, 16 / 9)++[email protected]("state", ["walk", "run"])+def test_video_canvas_wide_for_a_gait_from_the_command_line(tmp_path, state):+ still = tmp_path / "still.png"+ image = Image.new("RGB", (512, 512), (0, 255, 0))+ image.paste((180, 60, 30), (200, 50, 300, 500))+ image.save(still)+ payload = canvas.run_canvas(still, tmp_path / "c.png", state=state, shape="wide", facing="right", headroom=None,+ lead=None, report_path=None)+ assert payload["headroom"] == 0.0 and payload["canvas"] == [985, 554] and payload["why"] == canvas.GAIT_WIDE.why