Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
feat(canvas): an attack canvas keeps 20 % above the still, and `--shape wide` keeps its own 35 % row
Soohong Kim4c794562d01c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 20dbb17..87878cb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,8 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the- **`video-frames` reads the picture of a clip that carries more**: it takes the first video stream that is not a cover image (`attached_pic`, `stream_index`), and records `codec`, `pix_fmt`, `audio_streams` and `cover_streams`. A ZCRE clip always has an AAC track and an mjpeg cover; the audio is never extracted and the loop outputs carry no sound.- Measured once end to end: one side walk, 720p 3 s, 52 credits, 33.6 s to generate; the clip was 960×960 H.264 `yuv420p` with an AAC track and a cover, and it cut to a periodic 20-frame loop.- **The places that moved or scaled a keyed picture with BICUBIC map it with coverage and colour apart** (`transform_cell`, beside `resize_cell` in `sprite_gen/util/resample.py`): the gait fallback's scaled-back frames (`video-loop --anchor motion-auto` on a walk that grows or shrinks through the clip) and a curated move, scale, rotation or shear baked on a smooth row (`curation.apply_transform`, so `compose`, the GIF, the PNG export and the cycle bake). BICUBIC over premultiplied RGBA has the same negative lobe as LANCZOS, smaller, and drew the same lighter rim and key tint at the edge. On the synthetic outlined figure (`tests/keyed_figure.py`) it left 1,088 to 1,864 pixels with a colour none of the four source pixels under them had when a frame was scaled back by 0.84x to 1.19x, and 840 to 2,102 for a half-pixel move, a 10° rotation, a 0.75x shrink, a shear and a 1.3x grow with a 15° rotation; none are left. Coverage keeps BICUBIC's edge, held to the coverage of the 2 x 2 source pixels the colour mixes from; colour is a BILINEAR mix of premultiplied colour, so it is no longer sharpened. A pixel row's curated transform (`fit.pixel_unfake`, NEAREST and re-snapped) and an identity transform are unchanged. The scene GIF's downscale stays LANCZOS: it scales opaque composited frames, where there is no coverage to divide by. [docs/video-pipeline.md](docs/video-pipeline.md), [docs/loop-review.md](docs/loop-review.md), [docs/curation.md](docs/curation.md), [docs/pixel-unfake.md](docs/pixel-unfake.md).+- **An attack canvas keeps 20 % of its height above the still, not 35 %** (`STATE_CANVAS["attack"]`; 28 % in front and 20 % behind are unchanged). The extra room only made the body a smaller part of the clip, so `video-loop --body-height` scaled it up to the delivery height; with 20 % the body is larger in the clip and is scaled down. A 1024 px square still now pads to 2276×1280 instead of 2800×1575. 20 % still holds a weapon raised about a third of the body above the head: the canvas adds at least a quarter of the still's height above it, and the still's own margin over the crown does the rest (8 % of its height is enough). A still cropped at the crown needs `--headroom 0.26` or more for an overhead swing. At this headroom the 16:9 ratio, not `--lead`, sets the width of a square still's canvas. [docs/video-pipeline.md](docs/video-pipeline.md#1-canvas--the-input-frame-decides-the-output-frame).+- **`--shape wide` keeps 35 % above** (`WIDE_OVERRIDE`, no longer the attack row): it is forced on states whose own row is another shape — `video-set --shape wide` across a batch, or a canvas widened after the subject touched a side edge — and a jump is one of them; 35 % on 16:9 keeps about the room a jump's tall row leaves above the still. A forced-wide canvas is the same as before; an attack in a `--shape wide` batch gets that row, as a cheer or a projectile already did.## v2.20.0 - A keyed picture is scaled without a light rim or a green tint at the edge wherever the engine scales one: a row frame, a sliced sheet, a scene layerdiff --git a/docs/video-pipeline.md b/docs/video-pipeline.mdindex e8c309d..58aa132 100644--- a/docs/video-pipeline.md+++ b/docs/video-pipeline.md@@ -38,13 +38,27 @@ is a property of the motion state, owned by one table (`STATE_CANVAS`):| State | Shape | Ratio | Room | Why ||---|---|---|---|---|| `jump` | tall | 3:4 | 34 % head-room above the still | airborne frames need height |-| `attack` | wide | 16:9 | 35 % above the still, at least 28 % in front (facing side), 20 % behind | weapon swings rise overhead and extend in front; a long weapon drawn back reaches behind |+| `attack` | wide | 16:9 | 20 % above the still, at least 28 % in front (facing side), 20 % behind | a weapon raised overhead needs room above, a swing extends in front, and a long weapon drawn back reaches behind || `projectile` | wide | 16:9 | 34 % in front | the projectile travels away || everything else | square | 1:1 | — | in-place motion fits the still |+The attack's room above is 20 %, not more. On a square or upright still that puts at least+a quarter of the still's height above it, and with the still's own margin over the crown+(8 % of its height is enough) that holds a weapon raised about a third of the body above+the head. More room only made the body a smaller part of the clip, so `video-loop+--body-height` scaled it up to the delivery height; at 20 % it scales down. A still cropped+at the crown gets only the quarter — pass `--headroom 0.26` or more for an overhead swing.+At this headroom the 16:9 ratio, not `--lead`, sets the width of a square still's canvas,+so a smaller lead changes nothing.+`--shape tall|wide|square` overrides the row; `--headroom` / `--lead` / `--trail` tune the room(`--trail` is the empty fraction of the width kept behind the subject, for a weapon drawn-back before the strike); `--facing left` mirrors the wide layout. Headroom is a fraction of the full canvas+back before the strike); `--facing left` mirrors the wide layout. A forced shape is that shape's+own row, not the state's: `--shape tall` is the jump row, and `--shape wide` is a forced-wide+row with 35 % above, 28 % in front and 20 % behind — not the attack row. It lands on states+whose own row is another shape, a jump among them, and 35 % on 16:9 keeps about the room a+jump's tall row leaves above the still, so forcing wide adds width and never takes height+away. 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.@@ -418,7 +432,9 @@ wider than the body (a skirt, a veil, a held object). Both fail `video-frames`'sedge-contact check — the clip was made, the frames were cut, and the run stops at thegate. The state table now routes `cheer`, `wave` and `celebrate` to the wide canvas, and`video-set --shape wide` forces it for every state of a batch when the costume is the-reason. The same `--shape` is what `video-canvas` already took for a single still.+reason. The same `--shape` is what `video-canvas` already took for a single still. The+forced 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 %.## 4. Loop — period first, seam second, then the gait floordiff --git a/sprite_gen/video/canvas.py b/sprite_gen/video/canvas.pyindex edacf6f..5f06eda 100644--- a/sprite_gen/video/canvas.py+++ b/sprite_gen/video/canvas.py@@ -64,7 +64,13 @@ class CanvasProfile:# states fall through to `default`.STATE_CANVAS: dict[str, CanvasProfile] = {"jump": CanvasProfile(SHAPE_TALL, 3 / 4, 0.34, 0.0, 0.0, "airborne frames need head-room; hair clipped at 1:1"),- "attack": CanvasProfile(SHAPE_WIDE, 16 / 9, 0.35, 0.28, 0.2, "weapon swings rise overhead and extend in front; a long weapon drawn back reaches behind"),+ # Head-room 0.20, not more: at least a quarter of the still's height goes above it,+ # which with the still's own margin over the crown holds a weapon raised about a+ # third of the body above the head. More room only shrinks the body in the clip —+ # the loop then scales it up to the delivery height instead of down. Lead and+ # trail stay: for a square or upright still at this head-room the 16:9 ratio, not+ # the lead, sets the width.+ "attack": CanvasProfile(SHAPE_WIDE, 16 / 9, 0.20, 0.28, 0.2, "a weapon raised overhead needs room above, a swing extends in front, and a long weapon drawn back reaches behind"),"projectile": CanvasProfile(SHAPE_WIDE, 16 / 9, 0.0, 0.34, 0.0, "projectile travels away from the body"),# Raised-limb celebrations leave a square frame at the top corners; wide with a# symmetric margin keeps them inside (lead applies in front, the rest pads the back).@@ -73,10 +79,16 @@ STATE_CANVAS: dict[str, CanvasProfile] = {"celebrate": CanvasProfile(SHAPE_WIDE, 16 / 9, 0.0, 0.30, 0.0, "same envelope as cheer"),"default": CanvasProfile(SHAPE_SQUARE, 1.0, 0.0, 0.0, 0.0, "in-place motion fits the still's own frame"),}+# `--shape wide` is not the attack row. It is forced on a state whose own row is+# another shape — `video-set --shape wide` on every state of a batch, or a retry after+# a side-edge contact — and that state may be a jump. 0.35 on 16:9 keeps about the+# room a jump's tall row leaves above the still (0.35/0.65 ≈ 0.34/0.66 of its+# height), so forcing wide adds width and never takes height away.+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")SHAPE_DEFAULTS: dict[str, CanvasProfile] = {SHAPE_SQUARE: STATE_CANVAS["default"],SHAPE_TALL: STATE_CANVAS["jump"],- SHAPE_WIDE: STATE_CANVAS["attack"],+ SHAPE_WIDE: WIDE_OVERRIDE,}CORNER_TOLERANCE = 24 # max per-channel spread across the four corners for a "flat" backgroundFITS = ("state", "tight") # state: the table's room for the motion; tight: nonediff --git a/tests/video/test_attack_headroom.py b/tests/video/test_attack_headroom.pynew file mode 100644index 0000000..d921433--- /dev/null+++ b/tests/video/test_attack_headroom.py@@ -0,0 +1,97 @@+# SPDX-License-Identifier: Apache-2.0+"""The attack canvas keeps 0.20 of its height above the still; `--shape wide` keeps 0.35.++More room above an attack only made the body a smaller part of the clip, so the loop+scaled it up to the delivery height. 0.20 still holds a weapon raised overhead: the+canvas adds at least a quarter of the still's height above it, and the still's own+margin over the crown does the rest. A forced wide canvas is a different case — it+lands on states whose own row is not wide, a jump among them — so it keeps the room+a jump's tall row leaves above the still.+"""+from __future__ import annotations++import pytest+from PIL import Image++from sprite_gen.video import canvas++GREEN = (0, 255, 0)+# An overhead swing, budgeted as a share of the standing body's height above its crown.+OVERHEAD_RISE = 0.35+++def _figure(side: int, top: int, bottom: int) -> Image.Image:+ """A square still on the key with a figure from row `top` (its crown) to row `bottom` (its soles)."""+ img = Image.new("RGB", (side, side), GREEN)+ img.paste((150, 60, 50), (side // 3, top, 2 * side // 3, bottom))+ return img+++def _room_above_crown(padded: Image.Image) -> int:+ """Rows of key above the first row that holds the figure."""+ rgb = padded.convert("RGB")+ for y in range(rgb.height):+ if any(rgb.getpixel((x, y)) != GREEN for x in range(rgb.width)):+ return y+ raise AssertionError("no figure on the canvas")+++def test_attack_row_is_020_above_with_lead_and_trail_unchanged() -> None:+ attack = canvas.profile_for("attack")+ assert (attack.shape, attack.ratio, attack.headroom, attack.lead, attack.trail) == (canvas.SHAPE_WIDE, 16 / 9, 0.20, 0.28, 0.2)++[email protected]("size,facing,expected_canvas,expected_offset", [+ ((1024, 1024), "right", [2276, 1280], [455, 256]),+ ((1024, 1024), "left", [2276, 1280], [2276 - 1024 - 455, 256]),+ ((120, 160), "right", [356, 200], [71, 40]),+])+def test_attack_canvas_size_and_placement(size, facing, expected_canvas, expected_offset) -> None:+ still = Image.new("RGB", size, GREEN)+ still.paste((150, 60, 50), (size[0] // 3, size[1] // 4, 2 * size[0] // 3, size[1]))+ padded, report = canvas.pad_canvas(still, canvas.profile_for("attack"), facing=facing)+ assert report["canvas"] == expected_canvas and report["offset"] == expected_offset+ assert report["headroom"] == 0.20 and padded.size == tuple(expected_canvas)+ w, h = expected_canvas+ # head-room is the canvas fraction above the still; 16:9 sets both sides+ assert report["offset"][1] == round(h * 0.20) and abs(w / h - 16 / 9) < 0.01+++def test_lead_does_not_set_the_width_of_a_square_attack_canvas() -> None:+ still = _figure(1024, 80, 1000)+ base, _ = canvas.pad_canvas(still, canvas.profile_for("attack"))+ for lead in (0.18, 0.28):+ padded, _ = canvas.pad_canvas(still, canvas.profile_for("attack"), lead=lead)+ assert padded.tobytes() == base.tobytes()++[email protected]("crown_margin", [0.08, 0.19])+def test_a_raised_weapon_fits_above_the_crown(crown_margin: float) -> None:+ """0.08 is about the smallest margin over the crown that still holds the swing; 0.19 is the layout guide's crown line."""+ side = 1024+ top, bottom = round(side * crown_margin), side+ padded, _ = canvas.pad_canvas(_figure(side, top, bottom), canvas.profile_for("attack"))+ body = bottom - top+ assert _room_above_crown(padded) >= OVERHEAD_RISE * body+++def test_a_still_cropped_at_the_crown_needs_more_headroom() -> None:+ """With no margin over the crown, 0.20 gives a quarter of the body; the swing needs `--headroom 0.26`."""+ side = 1024+ still = _figure(side, 0, side)+ short, _ = canvas.pad_canvas(still, canvas.profile_for("attack"))+ assert _room_above_crown(short) == side // 4 < OVERHEAD_RISE * side+ raised, _ = canvas.pad_canvas(still, canvas.profile_for("attack"), headroom=0.26)+ assert _room_above_crown(raised) >= OVERHEAD_RISE * side+++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+ still = _figure(1024, 120, 1000)+ tall, tall_rep = canvas.pad_canvas(still, canvas.profile_for("jump"))+ wide, wide_rep = canvas.pad_canvas(still, forced)+ assert wide_rep["offset"][1] >= tall_rep["offset"][1] # room above the still+ assert wide_rep["canvas"] == [2800, 1575] # the forced-wide canvas is the same as before the attack row moveddiff --git a/tests/video/test_video_pipeline.py b/tests/video/test_video_pipeline.pyindex fdf35d9..f94519b 100644--- a/tests/video/test_video_pipeline.py+++ b/tests/video/test_video_pipeline.py@@ -82,7 +82,7 @@ def test_attack_canvas_reserves_overhead_room_and_reports_placement(tmp_path: Paassert image.crop((x, y, x + 120, y + 160)).tobytes() == Image.open(still).tobytes()assert image.crop((0, 0, image.width, y)).getextrema() == ((0, 0), (255, 255), (0, 0))assert json.loads(report.read_text()) == rep- assert rep["why"] == "weapon swings rise overhead and extend in front; a long weapon drawn back reaches behind"+ assert rep["why"] == canvas_mod.STATE_CANVAS["attack"].why# Explicit zeros still give the pre-headroom, pre-trail wide layout.zero, zero_rep = canvas_mod.pad_canvas(Image.open(still), canvas_mod.profile_for("attack"), headroom=0, trail=0)assert zero.size == (284, 160) and zero_rep["offset"] == [0, 0]