Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
test: the safe area is judged in one place - safe_area_fit's tiers (past the physical cap warns, past the safe area within it is informational, nothing fails), the pixel snap and pixel-draw's G8 both call it and say what it answers, the snap's lines as they were; G8 never fails a candidate and the prompt gives the safe area as a size target a pose may go past
Soohong Kim2181e2a6b8c9
diff --git a/tests/frames/test_safe_area_fit.py b/tests/frames/test_safe_area_fit.pynew file mode 100644index 0000000..7d79c21--- /dev/null+++ b/tests/frames/test_safe_area_fit.py@@ -0,0 +1,141 @@+# SPDX-License-Identifier: Apache-2.0+"""The safe-area judgment is one function, `frames.extract.safe_area_fit` (maintainer 2026-07-14): a+frame past the physical cap - the cell's width, its height less one margin - is a reroll candidate (a+warning); one past the safe area but within the cap is informational; nothing fails. The pixel snap of+`extract` and `pixel-draw`'s G8 both call it: what it answers is what both say, and the snap's two+lines read as they always have."""+from __future__ import annotations++import json+import random++import pytest+from PIL import Image++import sprite_gen.frames.extract as extract_module+from sprite_gen.frames.extract import safe_area_fit+from sprite_gen.gen import pixel_grid as pg+from sprite_gen.gen.pixel_gates import evaluate++MAGENTA = (255, 0, 255)+INK, CLEAR = (40, 30, 46, 255), (0, 0, 0, 0)++[email protected]("size,over,soft", [+ ((26, 26), [], []), # the safe area itself+ ((27, 26), [], [0]), # a column past it+ ((26, 27), [], [0]), # a row past it+ ((32, 29), [], [0]), # the physical cap itself: the cell's width, its height less one margin+ ((32, 30), [0], []), # a row past the cap+ ((33, 20), [0], []), # wider than the cell+])+def test_the_tiers_of_a_32_px_cell(size, over, soft):+ assert safe_area_fit([size], 32, 32, 3, 3) == {"cap": [32, 29], "safe": [26, 26], "over": over, "soft": soft}+++def test_the_tiers_count_logical_pixels_and_each_margin_on_its_own():+ """A 64 px cell at two cell pixels a logical pixel, margins 6 across and 4 down: cap 32x30, safe area 26x28."""+ fit = safe_area_fit([(26, 28), (27, 28), (26, 29), (32, 30), (32, 31)], 64, 64, 6, 4, scale=2)+ assert fit == {"cap": [32, 30], "safe": [26, 28], "over": [4], "soft": [1, 2, 3]}+++def _logical_art(width: int, height: int, seed: int) -> Image.Image:+ rng = random.Random(seed)+ art = Image.new("RGB", (width, height), MAGENTA)+ for y in range(height):+ for x in range(width):+ if rng.random() < 0.55:+ art.putpixel((x, y), (rng.randrange(30, 220), rng.randrange(30, 220), rng.randrange(30, 220)))+ return art+++def _snap_run(root, cell: int, margin: int):+ """Two frames of 20x36 logical pixels drawn at a pitch of 8, snapped 1:1 into a `cell` px cell."""+ run_dir = root / "run"+ (run_dir / "raw").mkdir(parents=True)+ pitch, gap = 8, 40+ frame = _logical_art(20, 36, seed=7).resize((20 * pitch, 36 * pitch), Image.Resampling.NEAREST)+ strip = Image.new("RGB", (frame.width * 2 + gap * 3, frame.height + gap * 2), MAGENTA)+ strip.paste(frame, (gap, gap))+ strip.paste(frame, (frame.width + gap * 2, gap))+ strip.save(run_dir / "raw" / "walk.png")+ request = {+ "version": 1,+ "kind": "sprite-gen-request",+ "engine": "component-row",+ "character": {"id": "safebot", "description": "safe area fixture", "base_image": None},+ "cell": {"shape": "square", "width": cell, "height": cell, "safe_margin_x": margin, "safe_margin_y": margin,+ "size": cell, "safe_margin": margin},+ "chroma_key": {"name": "magenta", "hex": "#FF00FF", "rgb": [255, 0, 255], "selection": "fallback"},+ "states": {"walk": {"frames": 2, "fps": 8, "loop": True, "action": "synthetic safe area fixture"}},+ "fit": {"pixel_unfake": True},+ }+ (run_dir / "sprite-request.json").write_text(json.dumps(request, indent=2) + "\n", encoding="utf-8")+ return run_dir+++def _warnings(run_dir) -> list[str]:+ return json.loads((run_dir / "frames" / "frames-manifest.json").read_text(encoding="utf-8"))["warnings"]+++def calls(monkeypatch):+ """Every call to the judgment, with what it answered."""+ real, seen = extract_module.safe_area_fit, []++ def recording(*args, **kwargs):+ seen.append((args, kwargs, real(*args, **kwargs)))+ return seen[-1][2]++ monkeypatch.setattr(extract_module, "safe_area_fit", recording)+ return seen+++def test_the_snap_notes_a_frame_past_the_safe_area_in_its_own_words(tmp_path, calls):+ run_dir = _snap_run(tmp_path, 48, 8) # safe area 32x32, cap 48x40: 36 rows are past one, within the other+ assert extract_module.run(run_dir=run_dir) == 0+ ((args, kwargs, fit),) = calls+ assert args == ([(20, 36), (20, 36)], 48, 48, 8, 8, 1) and kwargs == {}+ assert fit == {"cap": [48, 40], "safe": [32, 32], "over": [], "soft": [0, 1]}+ warnings = _warnings(run_dir)+ assert ("walk: frames exceed the safe area 32x32 but fit within the margin zone (informational, no action): "+ "0:20x36, 1:20x36") in warnings+ assert not any("physical cap" in line for line in warnings)+++def test_the_snap_warns_a_frame_past_the_physical_cap_in_its_own_words(tmp_path, calls):+ run_dir = _snap_run(tmp_path, 40, 6) # cap 40x34: 36 rows are past it+ assert extract_module.run(run_dir=run_dir) == 0+ ((args, _, fit),) = calls+ assert args[1:] == (40, 40, 6, 6, 1) and fit["over"] == [0, 1] and fit["soft"] == []+ warnings = _warnings(run_dir)+ assert ("walk: native logical exceeds the physical cap 40x34 — capped frames (reroll candidates): "+ "0:20x36, 1:20x36") in warnings+ assert not any("margin zone" in line for line in warnings)+++def test_pixel_draw_asks_it_with_the_canvas_and_the_engines_margin(calls):+ frame = [[INK if 3 <= y <= 28 and 8 <= x <= 23 else CLEAR for x in range(32)] for y in range(32)]+ gates = evaluate({"f": frame}, kind="sprite", rule=pg.palette_rule("free"), walk_cycle=False)+ ((args, kwargs, fit),) = calls+ assert args == ([(16, 26)], 32, 32, 3, 3) and kwargs == {}+ assert gates["G8"]["physical_cap"] == fit["cap"] and gates["G8"]["safe_area"] == fit["safe"]+++def test_both_paths_say_what_it_answers(tmp_path, monkeypatch):+ """Answer that every frame is past the safe area: the snap and G8 both say so of frames well inside it."""+ real = extract_module.safe_area_fit++ def past(sizes, *args, **kwargs):+ return {**real(sizes, *args, **kwargs), "over": [], "soft": list(range(len(sizes)))}++ monkeypatch.setattr(extract_module, "safe_area_fit", past)+ small = [[INK if 19 <= y <= 28 and 11 <= x <= 20 else CLEAR for x in range(32)] for y in range(32)]+ gates = evaluate({"f": small}, kind="sprite", rule=pg.palette_rule("free"), walk_cycle=False)+ assert gates["ok"] and gates["warnings"] == []+ assert gates["info"] == ["G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, "+ "no action): f:10x10"]+ run_dir = _snap_run(tmp_path, 96, 8) # safe area 80x80+ assert extract_module.run(run_dir=run_dir) == 0+ assert ("walk: frames exceed the safe area 80x80 but fit within the margin zone (informational, no action): "+ "0:20x36, 1:20x36") in _warnings(run_dir)diff --git a/tests/gen/test_pixel_draw.py b/tests/gen/test_pixel_draw.pyindex 52d02cd..9e337a8 100644--- a/tests/gen/test_pixel_draw.py+++ b/tests/gen/test_pixel_draw.py@@ -144,7 +144,8 @@ def test_one_candidate_is_drawn_checked_and_written(tmp_path, claude, capsys):(candidate,) = report["candidates"]assert candidate["status"] == "passed" and candidate["G1"] == {"ok": True, "issues": [], "recovered": []}assert candidate["gates"]["ok"] and candidate["files"]["grid"] == "candidates/c1/grid.txt"- assert candidate["gates"]["G8"]["ok"] and candidate["warnings"] == []+ assert not candidate["gates"]["G8"]["warn"] and candidate["warnings"] == [] and candidate["info"] == []+ assert report["warnings"] == [] and report["info"] == []grid, _ = pg.parse_answer(SPRITE16, "chargrid", 16, 16, ("sprite",))assert (out / "grid.txt").read_text(encoding="utf-8") == pg.to_text(grid)assert (out / "frame-sprite.png").read_bytes() == pg.png_bytes(pg.frame_pixels(grid, "sprite"))@@ -495,7 +496,7 @@ def test_prompts_say_the_kind_and_the_format():assert "Palette: at most 8 colors plus transparent." in tile and "Background: transparent" not in tileassert "```pixels\nPALETTE\n. transparent" in tile and "exactly 32 lines of exactly 32 characters" in tilesprite = pixel_draw.generation_prompt(request(kind="sprite", frames=("down", "up"), subject="a knight"))- assert "stand on row 29 in every frame, and nothing of the body goes below it." in sprite+ assert "stand on row 29 in every frame." in sprite and "goes below it" not in spriteassert "Background: transparent." in spriteassert "Frames in this order: down, up." in sprite@@ -548,64 +549,100 @@ def pixel_request(**changes) -> pixel_draw.PixelRequest:@pytest.mark.parametrize("size,margin,inner,first,last", [(16, 1, 14, 2, 15), (24, 2, 20, 3, 22), (32, 3, 26, 4, 29),(48, 4, 40, 5, 44)])def test_the_prompt_says_the_safe_area_in_pixels(size, margin, inner, first, last):- """The engine's safe area (prepare's 9.4% margin, floored), in numbers the model can count."""+ """The engine's safe area (prepare's 9.4% margin, floored), in numbers the model can count - a+ target for the body's size that a pose may go past, as the image path keeps it (frames/extract.py)."""sprite = pixel_draw.generation_prompt(pixel_request(size=size))assert "fill most of the canvas" not in sprite and "1-2 pixel margin" not in sprite- assert (f"Safe area: keep a {margin}-pixel margin on every side and draw the subject's body inside the central "- f"{inner}x{inner} pixels: rows {first}-{last} and columns {first}-{last}, counting rows and columns "- "from 1.") in sprite- assert f"the outermost rows and columns (rows 1 and {size}, columns 1 and {size})" in sprite.lower()- assert (f"Floor: row {last}, the bottom row of the safe area, is the ground. The feet (or the lowest point the "- f"subject rests on) stand on row {last}, and nothing of the body goes below it.") in sprite- if margin > 1:- assert (f"Only a thin detail (a weapon tip, a strand of hair, an effect) may reach into the margin, at most "- f"{margin - 1} pixel{'s' if margin > 2 else ''} past the safe area;") in sprite- else:- assert "at this size nothing reaches past the safe area." in sprite+ assert (f"Safe area: draw the subject's body about the size of the central {inner}x{inner} pixels - rows "+ f"{first}-{last} and columns {first}-{last}, counting rows and columns from 1 - inside a {margin}-pixel "+ "margin on every side. That is a target for its average size, not a wall: when a pose or a detail needs "+ "the room (a raised weapon, a long blade, an effect, a wide stance), it may reach past the safe area "+ "into the margin, up to the canvas edge. Keep the four corner pixels transparent.") in sprite+ assert (f"Floor: row {last}, the bottom row of the safe area, is the ground: the feet (or the lowest point the "+ f"subject rests on) stand on row {last}.") in sprite+ for absolute in ("outermost rows and columns", "stay transparent", "Only a thin detail", "nothing reaches past",+ "goes below it", "keep a"):+ assert absolute not in sprite, absolutedef test_the_prompt_keeps_every_frame_on_the_floor_row_and_icons_off_it():walk = pixel_draw.generation_prompt(pixel_request(frames=("walk1", "walk2", "walk3", "walk4"), walk_cycle=True))- assert "stand on row 29 in every frame, and nothing of the body goes below it." in walk+ assert "stand on row 29 in every frame." in walkassert "the same bottom row" not in walkicon = pixel_draw.generation_prompt(pixel_request(kind="icon", frames=("icon",)))- assert "draw the icon inside the central 26x26 pixels: rows 4-29 and columns 4-29" in icon+ assert "draw the icon about the size of the central 26x26 pixels - rows 4-29 and columns 4-29" in iconassert "Floor:" not in icontile = pixel_draw.generation_prompt(pixel_request(kind="tile", frames=("tile",)))assert "Safe area" not in tile and "Floor:" not in tiletiny = pixel_draw.generation_prompt(pixel_request(size=8))assert ("Safe area: at 8x8 there is no margin; the subject may use the whole canvas, with the four corner pixels ""transparent.") in tiny- assert "Floor: row 8, the bottom row of the safe area, is the ground." in tiny+ assert "Floor: row 8, the bottom row of the safe area, is the ground: the feet" in tinydef test_the_prompt_takes_the_margin_from_prepare(monkeypatch):from sprite_gen.gen import preparemonkeypatch.setattr(prepare, "DEFAULT_SAFE_MARGIN_RATIO", 0.125)prompt = pixel_draw.generation_prompt(pixel_request())- assert "keep a 4-pixel margin on every side" in prompt and "the central 24x24 pixels: rows 5-28" in prompt+ assert "inside a 4-pixel margin on every side" in prompt and "the central 24x24 pixels - rows 5-28" in promptdef test_the_pick_knows_the_margin_is_meant():pick = pixel_draw.pick_prompt(pixel_request(), ["A", "B"])- assert ("The request was: a knight Canvas 32x32 pixels per frame, the body kept inside the central 26x26 with a "- "3-pixel margin.") in pick+ assert ("The request was: a knight Canvas 32x32 pixels per frame, the body about the size of the central 26x26, "+ "inside a 3-pixel margin a pose may reach into.") in picktile = pixel_draw.pick_prompt(pixel_request(kind="tile", frames=("tile",), subject="grass"), ["A", "B"])assert "The request was: grass Canvas 32x32 pixels per frame." in tile-def test_an_edited_grid_goes_through_the_safe_area_gate(tmp_path):+def test_an_edited_grid_goes_through_the_safe_area_check(tmp_path, capsys):grid, _ = pg.parse_answer(FULL16, "chargrid", 16, 16, ("front",))full = tmp_path / "full.txt"full.write_text(pg.to_text(grid), encoding="utf-8") # drawn under the old prompt: rows 1 and 16 are used- with pytest.raises(SystemExit, match=r"full.txt fails G8"):- pixel_draw.main(["--grid", str(full), "--out-dir", str(tmp_path / "full")])+ assert pixel_draw.main(["--grid", str(full), "--out-dir", str(tmp_path / "full")]) == 0report = report_of(tmp_path / "full")- assert report["status"] == "failed" and report["gates"]["G8"]["frames"]["front"]["edge_pixels"] == 14+ assert json.loads(capsys.readouterr().out) == report+ assert report["status"] == "ok" and report["gates"]["G8"]["over_cap"] == ["front"] and report["info"] == []+ assert report["warnings"] == ["G8: frames exceed the physical cap 16x15 (reroll candidates): front:14x16",+ "G8 the feet are not on the floor row (the safe area's bottom row): front 1 px below it"]inside = tmp_path / "inside.txt"rows = ["." * 16] + ["...." + "a" * 8 + "...."] * 14 + ["." * 16]inside.write_text("PALETTE\n. transparent\na #2a1f2e\nFRAME front\n" + "\n".join(rows) + "\nEND\n", encoding="utf-8")assert pixel_draw.main(["--grid", str(inside), "--out-dir", str(tmp_path / "inside")]) == 0report = report_of(tmp_path / "inside")- assert report["status"] == "ok" and report["warnings"] == []+ assert report["status"] == "ok" and report["warnings"] == [] and report["info"] == []assert report["gates"]["G8"]["floor_row"] == 14 and report["gates"]["G8"]["safe_area"] == [14, 14]+++def staff_answer() -> str:+ """A 32 px sprite whose staff reaches the canvas's top edge: 17x29, past the 26x26 safe area and within+ the 32x29 cap, the feet on the floor row."""+ rows = []+ for y in range(32):+ row = ["."] * 32+ if 3 <= y <= 28:+ row[8:24] = ["a"] * 16+ if y <= 28:+ row[24] = "b"+ rows.append("".join(row))+ return "```pixels\nPALETTE\n. transparent\na #2a1f2e\nb #8a5a30\nFRAME sprite\n" + "\n".join(rows) + "\nEND\n```"++[email protected]("answer,size,frame,warnings,info", [+ (staff_answer(), "32", "sprite", [],+ ["G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, no action): sprite:17x29"]),+ (FULL16, "16", "front", ["G8: frames exceed the physical cap 16x15 (reroll candidates): front:14x16",+ "G8 the feet are not on the floor row (the safe area's bottom row): front 1 px below it"], []),+], ids=["staff-to-the-edge", "knight-on-the-edge-rows"])+def test_one_candidate_past_the_safe_area_is_the_pick(tmp_path, claude, answer, size, frame, warnings, info):+ """One candidate, as a caller that buys one drawing asks: a staff on the canvas edge, or a knight+ from helmet to boots on its edge rows, passes - its info lines and warnings in the report."""+ claude({"c1-gen1": reply(answer)})+ out = tmp_path / "run"+ assert draw(out, "--subject", "a figure", "--size", size, "--frames", frame, "--candidates", "1") == 0+ report = report_of(out)+ (candidate,) = report["candidates"]+ assert report["status"] == "ok" and report["chosen"] == "c1" and candidate["status"] == "passed"+ assert report["pick"]["by"] == "only" and (out / f"frame-{frame}.png").is_file()+ assert report["warnings"] == candidate["warnings"] == warnings+ assert report["info"] == candidate["info"] == infodiff --git a/tests/gen/test_pixel_gates.py b/tests/gen/test_pixel_gates.pyindex 4d51efb..e48ba01 100644--- a/tests/gen/test_pixel_gates.py+++ b/tests/gen/test_pixel_gates.py@@ -1,8 +1,11 @@# SPDX-License-Identifier: Apache-2.0"""Gates G2..G6 and G8 of `pixel-draw` on recorded answers (tests/fixtures/pixel_draw) and on grids-made to break exactly one rule. G2, G3, G6 and G8's canvas edge fail a candidate; G4, G5, the walk-check and G8's body and floor checks only warn. The `*-safe-area-pass` answers were drawn under the-safe-area prompt; the other recorded sprites when the prompt asked to fill the canvas."""+made to break exactly one rule. G2, G3 and G6 fail a candidate; G4, G5 and the walk check only warn.+G8 never fails: its judgment is the image path's, `frames.extract.safe_area_fit`+(tests/frames/test_safe_area_fit.py) - a frame past the physical cap warns, one past the safe area+within the cap is an info line - and a sprite whose feet are off the floor row warns. The+`*-safe-area-pass` answers were drawn under the safe-area prompt; the other recorded sprites when the+prompt asked to fill the canvas."""from __future__ import annotationsfrom pathlib import Path@@ -36,7 +39,7 @@ def blob(size: int = 8, top: int = 2, bottom: int = 5, fill=INK):def test_recorded_sprite_passes_every_gate():gates = evaluate(frames_of("sprite-16-safe-area-pass.txt", 16, ("sprite",)), kind="sprite", rule=FREE16,walk_cycle=False)- assert gates["ok"] and gates["failed"] == [] and gates["warnings"] == []+ assert gates["ok"] and gates["failed"] == [] and gates["warnings"] == [] and gates["info"] == []assert gates["G2"]["colors_used"] <= 16assert gates["G3"]["frames"]["sprite"]["corners_transparent"]assert gates["G5"]["isolated_pixels"] == 0 and "G6" not in gates and "G4" not in gates@@ -45,7 +48,7 @@ def test_recorded_sprite_passes_every_gate():def test_recorded_tile_is_opaque_and_repeats():gates = evaluate(frames_of("tile-16-pass.txt", 16, ("tile",)), kind="tile", rule=FREE16, walk_cycle=False)- assert gates["ok"] and gates["G3"]["frames"]["tile"]["opaque_fraction"] == 1.0+ assert gates["ok"] and gates["G3"]["frames"]["tile"]["opaque_fraction"] == 1.0 and gates["info"] == []assert gates["G4"]["seam_ratio"] <= 1.5 and not gates["G4"]["warn"]@@ -94,7 +97,12 @@ def test_recorded_four_directions_stand_on_one_line():gates = evaluate(frames_of("fourdir-16-pass.txt", 16, FOURDIR), kind="sprite", rule=FREE16, walk_cycle=False)assert gates["G6"]["ok"] and gates["G6"]["identical_pairs"] == []assert len(set(gates["G6"]["baselines"].values())) == 1 and "walk" not in gates["G6"]- assert gates["failed"] == ["G8"] # drawn to fill the canvas: the plumes on row 1, the boots on row 16+ # drawn to fill the canvas: plume to boots 16 rows, where a 16 px cell has 15 above its bottom margin+ assert gates["ok"] and gates["failed"] == [] and gates["info"] == []+ assert gates["warnings"] == [+ "G8: frames exceed the physical cap 16x15 (reroll candidates): down:12x16, up:12x16, left:10x16, right:10x16",+ "G8 the feet are not on the floor row (the safe area's bottom row): down 1 px below it, up 1 px below it, "+ "left 1 px below it, right 1 px below it"]def test_recorded_walk_passes_with_the_weak_leg_warning():@@ -102,7 +110,7 @@ def test_recorded_walk_passes_with_the_weak_leg_warning():g6 = gates["G6"]assert gates["ok"] and g6["ok"] and g6["height_spread"] == 1 # the passing pose is a pixel higherassert g6["walk"]["warn"] and g6["walk"]["ratio"] < 0.4- assert [w[:30] for w in gates["warnings"]] == ["G6 weak leg alternation: the c"]+ assert [w[:30] for w in gates["warnings"]] == ["G6 weak leg alternation: the c"] and gates["info"] == []assert set(g6["baselines"].values()) == {gates["G8"]["floor_row"]} == {28} # row 29 of 32, counted from 1@@ -158,52 +166,80 @@ def test_the_safe_area_follows_prepares_ratio(monkeypatch):def test_g8_passes_a_body_inside_the_safe_area():gates = evaluate({"f": figure()}, kind="sprite", rule=FREE16, walk_cycle=False)g8 = gates["G8"]- assert gates["ok"] and gates["warnings"] == []- assert g8["ok"] and g8["margin"] == [3, 3] and g8["safe_area"] == [26, 26] and g8["floor_row"] == 28- frame = g8["frames"]["f"]- assert frame["edge_pixels"] == 0 and frame["body_box"] == [8, 3, 23, 28] and frame["bbox"] == [8, 3, 23, 28]- assert frame["overflow"] == {"left": 0, "top": 0, "right": 0, "bottom": 0}+ assert gates["ok"] and gates["warnings"] == [] and gates["info"] == [] and not g8["warn"]+ assert g8["margin"] == [3, 3] and g8["safe_area"] == [26, 26] and g8["physical_cap"] == [32, 29]+ assert g8["floor_row"] == 28 and g8["over_cap"] == [] and g8["past_safe_area"] == []+ assert g8["frames"]["f"] == {"bbox": [8, 3, 23, 28], "size": [16, 26], "body_box": [8, 3, 23, 28]}-def test_g8_fails_a_pixel_on_the_canvas_edge():- for x, y in ((31, 15), (15, 0), (0, 20), (12, 31)):- frame = figure()- frame[y][x] = INK- gates = evaluate({"f": frame}, kind="sprite", rule=FREE16, walk_cycle=False)- assert not gates["ok"] and gates["failed"] == ["G8"], (x, y)- assert gates["G8"]["frames"]["f"]["edge_pixels"] == 1 and gates["G8"]["edge_pixels"] == 1- standing_on_the_edge = paint(canvas(), 8, 6, 23, 31)- gates = evaluate({"f": standing_on_the_edge}, kind="icon", rule=FREE16, walk_cycle=False)- assert gates["failed"] == ["G8"] and gates["G8"]["frames"]["f"]["edge_pixels"] == 16[email protected]("stroke,info", [+ ((24, 15, 31, 15), []), # a blade to the right edge: 24 columns, within the safe area's width+ ((0, 20, 7, 20), []), # an arm to the left edge+ ((15, 0, 15, 2), ["f:16x29"]), # a staff to the top edge: 29 rows, past the safe area, within the cap+ ((12, 29, 12, 31), ["f:16x29"]), # a tail to the bottom edge, the feet still on the floor row+])+def test_g8_never_fails_a_pose_that_reaches_the_canvas_edge(stroke, info):+ """The safe area is a target for the body's size, not a wall: a pose may reach the canvas edge."""+ frame = paint(figure(), *stroke)+ gates = evaluate({"f": frame}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["failed"] == [] and gates["warnings"] == []+ assert gates["info"] == [f"G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, "+ f"no action): {where}" for where in info]def test_g8_lets_a_thin_tip_into_the_margin():frame = paint(figure(), 24, 15, 30, 15) # a blade from the body's right side to 2 px past the safe areagates = evaluate({"f": frame}, kind="sprite", rule=FREE16, walk_cycle=False)measure = gates["G8"]["frames"]["f"]- assert gates["ok"] and gates["warnings"] == []+ assert gates["ok"] and gates["warnings"] == [] and gates["info"] == []assert measure["bbox"] == [8, 3, 30, 28] and measure["body_box"] == [8, 3, 23, 28]-def test_g8_warns_when_the_body_leaves_the_safe_area_by_more_than_a_pixel():- one_over = paint(canvas(), 2, 3, 20, 28)- assert evaluate({"f": one_over}, kind="sprite", rule=FREE16, walk_cycle=False)["warnings"] == []- two_over = paint(canvas(), 1, 2, 20, 28)- gates = evaluate({"f": two_over}, kind="sprite", rule=FREE16, walk_cycle=False)- assert gates["ok"] and gates["G8"]["frames"]["f"]["overflow"] == {"left": 2, "top": 1, "right": 0, "bottom": 0}- assert gates["warnings"] == ["G8 the body reaches past the 26x26 safe area by more than 1 px "- "(f: 2 px left) — only a thin detail may go into the 3 px margin"]+def test_g8_notes_a_frame_past_the_safe_area_within_the_physical_cap():+ """Size, not place: what the image path judges is how big the frame is against the cell."""+ for inside in (paint(canvas(), 3, 3, 28, 28), paint(canvas(), 1, 3, 20, 28)): # 26x26, and 20x26 off-centre+ gates = evaluate({"f": inside}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["warnings"] == [] and gates["info"] == []+ wide = paint(canvas(), 3, 3, 29, 28) # 27 columns+ whole = paint(canvas(), 0, 3, 31, 28) # the canvas's width, edge to edge+ staff = paint(figure(), 20, 0, 20, 2) # a staff to the top edge: 29 rows, the cap+ gates = evaluate({"a": wide, "b": whole, "c": staff}, kind="icon", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["failed"] == [] and gates["warnings"] == [] and not gates["G8"]["warn"]+ assert gates["G8"]["past_safe_area"] == ["a", "b", "c"]+ assert gates["info"] == ["G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, "+ "no action): a:27x26, b:32x26, c:16x29"]+++def test_g8_warns_a_frame_past_the_physical_cap():+ """The cap is the canvas's width and its height less the margin under the floor row: 32x29 at 32 px."""+ over = paint(figure(), 20, 0, 20, 29) # a staff from the top edge to a row under the floor row: 30 rows+ for kind in ("sprite", "icon"): # the same judgment for both+ gates = evaluate({"f": over}, kind=kind, rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["failed"] == [] and gates["G8"]["warn"] and gates["info"] == []+ assert gates["G8"]["over_cap"] == ["f"] and gates["G8"]["frames"]["f"]["body_box"][3] == 28+ assert gates["warnings"] == ["G8: frames exceed the physical cap 32x29 (reroll candidates): f:16x30"]+++def test_g8_never_fails_a_candidate():+ """A plus across the whole canvas: past the cap and off the floor row - two warnings, and it passes."""+ plus = paint(paint(canvas(), 15, 0, 16, 31), 0, 15, 31, 16)+ gates = evaluate({"f": plus}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert gates["ok"] and gates["failed"] == [] and "ok" not in gates["G8"] and gates["info"] == []+ assert gates["warnings"] == ["G8: frames exceed the physical cap 32x29 (reroll candidates): f:32x32",+ "G8 the feet are not on the floor row (the safe area's bottom row): f 12 px above it"]def test_g8_warns_a_sprite_off_the_floor_row():floating = paint(canvas(), 8, 3, 23, 26)gates = evaluate({"a": floating, "b": paint(canvas(), 9, 3, 23, 26)}, kind="sprite", rule=FREE16, walk_cycle=False)- assert gates["ok"] and gates["G6"]["ok"]+ assert gates["ok"] and gates["G6"]["ok"] and gates["G8"]["warn"] and gates["info"] == []assert gates["warnings"] == ["G8 the feet are not on the floor row (the safe area's bottom row): ""a 2 px above it, b 2 px above it"]- below = paint(canvas(), 8, 3, 23, 29)- assert evaluate({"f": below}, kind="sprite", rule=FREE16, walk_cycle=False)["warnings"] == [+ below = evaluate({"f": paint(canvas(), 8, 3, 23, 29)}, kind="sprite", rule=FREE16, walk_cycle=False)+ assert below["ok"] and below["warnings"] == ["G8 the feet are not on the floor row (the safe area's bottom row): f 1 px below it"]+ assert below["info"] == ["G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, "+ "no action): f:16x27"]icon = evaluate({"f": floating}, kind="icon", rule=FREE16, walk_cycle=False)assert icon["warnings"] == [] and icon["G8"]["floor_row"] is None@@ -215,22 +251,23 @@ def test_g8_reads_the_floor_from_the_body_not_a_tip_below_it():assert gates["G8"]["frames"]["f"]["bbox"][3] == 30 and gates["G8"]["frames"]["f"]["body_box"][3] == 28-def test_g8_has_no_edge_to_keep_below_a_one_pixel_margin():+def test_g8_below_a_one_pixel_margin_the_safe_area_and_the_cap_are_the_canvas():plus = paint(paint(canvas(8), 3, 0, 4, 7), 0, 3, 7, 4) # touches every side, corners cleargates = evaluate({"f": plus}, kind="icon", rule=FREE16, walk_cycle=False)- assert gates["ok"] and gates["G8"]["margin"] == [0, 0] and gates["G8"]["edge_pixels"] == 0+ assert gates["ok"] and gates["warnings"] == [] and gates["info"] == []+ assert gates["G8"]["margin"] == [0, 0] and gates["G8"]["safe_area"] == [8, 8] and gates["G8"]["physical_cap"] == [8, 8]def test_g8_leaves_tiles_alone():gates = evaluate({"tile": [[GRASS] * 32 for _ in range(32)]}, kind="tile", rule=FREE16, walk_cycle=False)- assert gates["ok"] and "G8" not in gates+ assert gates["ok"] and "G8" not in gates and gates["info"] == []def test_a_recorded_blade_reaches_into_the_margin_and_the_body_stays_inside():frames = frames_of("attack-32-safe-area-pass.txt", 32, ("attack1", "attack2", "attack3", "attack4"))gates = evaluate(frames, kind="sprite", rule=FREE16, walk_cycle=False)measures = gates["G8"]["frames"]- assert gates["ok"] and gates["warnings"] == []+ assert gates["ok"] and gates["warnings"] == [] and gates["info"] == []assert measures["attack1"]["bbox"][0] == 2 and measures["attack3"]["bbox"][2] == 29 # the blade, 1 px past itassert all(3 <= m["body_box"][0] and m["body_box"][2] <= 28 and m["body_box"][3] == 28 for m in measures.values())@@ -247,15 +284,20 @@ def test_g6_measures_the_frames_inside_the_safe_area():def test_a_recorded_staff_and_spark_in_the_margin_pass():"""Drawn under the safe-area prompt: the staff pulled back reaches 2 px into the top margin and the- spark 1 px, the figure is the same height in every frame."""+ spark 1 px, the figure is the same height in every frame. The two frames they make taller or wider+ than the safe area are an info line."""frames = frames_of("attack-32-margin-detail.txt", 32, ("attack1", "attack2", "attack3", "attack4"))gates = evaluate(frames, kind="sprite", rule=FREE16, walk_cycle=False)assert gates["ok"] and gates["warnings"] == [] and gates["G6"]["height_spread"] == 0+ assert gates["info"] == ["G8: frames exceed the safe area 26x26 but fit within the margin zone (informational, "+ "no action): attack2:19x28, attack3:26x27"]assert [m["bbox"][1] for m in gates["G8"]["frames"].values()] == [3, 1, 2, 3]-def test_recorded_answers_from_the_full_canvas_prompt_fail_g8():+def test_recorded_answers_from_the_full_canvas_prompt_pass_g8_with_warnings():"""Drawn when the prompt asked to fill the canvas: the knight's helmet is on row 1 and its boots- on row 16, the 16 px canvas's own edge rows."""+ on row 16, the 16 px canvas's own edge rows - 16 rows, where 15 stand above the bottom margin."""gates = evaluate(frames_of("sprite-16-pass.txt", 16, ("front",)), kind="sprite", rule=FREE16, walk_cycle=False)- assert gates["failed"] == ["G8"] and gates["G8"]["frames"]["front"]["edge_pixels"] == 14+ assert gates["ok"] and gates["failed"] == [] and gates["info"] == []+ assert gates["warnings"] == ["G8: frames exceed the physical cap 16x15 (reroll candidates): front:14x16",+ "G8 the feet are not on the floor row (the safe area's bottom row): front 1 px below it"]