Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
test(logo): a hole cut to nothing in a see-through patch opens no way out of the letter — one or four squares of the key in a mint patch walled in by a navy letter on green are still found inside, the glow's reach stays a letter's rim, and the logo is refused at the matte; a hole glued to the letter's edge is inside sealed and outside once one pixel joins it to the key round the letter; big hollow O and A letters, plain or with a glow pooled round a clear counter, stay clear (red)
Soohong Kima9c16f06916e
diff --git a/tests/frames/test_logo_matte.py b/tests/frames/test_logo_matte.pyindex dddefc9..5968649 100644--- a/tests/frames/test_logo_matte.py+++ b/tests/frames/test_logo_matte.py@@ -13,8 +13,8 @@ import numpy as npimport pytestfrom PIL import Image, ImageFilter-from logo_lettering import (BUTTER, GREEN_PAINTED, MAGENTA_PAINTED, MINT, SKY, key_excess, lettering, over, quilt,- truth_over)+from logo_lettering import (BUTTER, GREEN_PAINTED, MAGENTA_PAINTED, MINT, SKY, hollow_letters, key_excess, lettering,+ over, quilt, truth_over, walled_patch)from sprite_gen.frames import logo_matte@@ -269,6 +269,69 @@ def test_a_glow_is_not_see_through_inside(item, key) -> None:assert not logo_matte.see_through_inside(cut)["wide"]+KEY_ISLAND = (96, 156, 9) # the end validator's counterexample: a 9x9 square of the key in the middle of the patch++[email protected]("holes", [+ (),+ (KEY_ISLAND,),+ (KEY_ISLAND, (70, 105, 5), (65, 130, 4), (120, 205, 15)), # one more on its own, two glued to the patch's wall+], ids=["no-hole", "one-hole", "four-holes"])+def test_a_hole_cut_to_nothing_opens_no_way_out_of_the_letter(holes) -> None:+ # the mint patch walled in by the navy letter is cut see-through on green and the squares of the key in it are cut+ # to nothing: walled in by the letter, joined to no edge of the picture, so the patch round them is still inside+ # and the glow's reach (no glow here) stays a letter's rim+ cut, _ = logo_matte.matte(walled_patch(holes=holes), key="green")+ alpha = np.asarray(cut)[..., 3]+ for y, x, side in holes:+ assert (alpha[y:y + side, x:x + side] == 0).all()+ patch = alpha[65:135, 100:220]+ see = int(((patch > 0) & (patch < logo_matte.INSIDE_ALPHA8)).sum())+ assert see >= 8400 - sum(side * side for *_, side in holes)+ inside = logo_matte.see_through_inside(cut)+ assert inside["wide"] and inside["glow_reach_px"] == logo_matte.INSIDE_EDGE_PX+ assert inside["px"] >= 0.85 * see+++def test_a_hole_joined_to_the_outside_is_outside_however_narrow() -> None:+ # the patch fills the letter but for its 4 px outline, and a hole is glued to the letter's edge: one pixel of navy+ # between it and the key round the letter. Sealed, the hole is walled in and the patch round it is inside; with+ # that one pixel of the key, the hole is joined to the outside and the patch has a way out through it+ patch, holes = (24, 176, 24, 296), ((96, 21, 9),)+ sealed, _ = logo_matte.matte(walled_patch(patch=patch, holes=holes), key="green")+ opened, _ = logo_matte.matte(walled_patch(patch=patch, holes=holes, opening=(100, 20)), key="green")+ assert np.asarray(opened)[100, 19:30, 3].max() == 0 # nothing from the outside into the hole+ assert logo_matte.see_through_inside(sealed)["wide"]+ found = logo_matte.see_through_inside(opened)+ assert found["px"] == 0 and not found["wide"]+++def test_big_letters_with_counters_are_not_see_through_inside() -> None:+ # an O's counter and an A's are transparent and walled in by the letters, so not outside; nothing see-through lies+ # in them past the letters' antialiased rim, so nothing is inside+ cut, _ = logo_matte.matte(lettering(shapes=hollow_letters, size=(640, 320), glow=None, key=GREEN_PAINTED).image,+ key="green")+ alpha = np.asarray(cut)[..., 3]+ assert alpha[160, 179] == 0 and alpha[150, 460] == 0 # the middle of the O's counter and of the A's+ inside = logo_matte.see_through_inside(cut)+ assert inside["px"] == 0 and not inside["wide"]++[email protected]("item, key", [+ (lambda: lettering(shapes=hollow_letters, size=(640, 320)), "magenta"),+ (lambda: lettering(shapes=hollow_letters, size=(640, 320), fill=(232, 182, 52), outline=(20, 24, 40),+ glow=(40, 230, 255), glow_radius=6.0, key=GREEN_PAINTED), "green"),+ (lambda: lettering(shapes=hollow_letters, size=(640, 320), fill=(255, 214, 40), glow=(255, 120, 20),+ glow_radius=5.0, glow_strength=0.7, key=GREEN_PAINTED), "green"),+], ids=["white-glow", "outlined-cyan-glow-green", "orange-glow-green"])+def test_a_glow_pooled_in_a_big_counter_is_not_see_through_inside(item, key) -> None:+ # the glow pools round a counter whose middle stays transparent, walled in by the letter: it reaches no farther+ # from the letter there than beside it+ cut, _ = logo_matte.matte(item().image, key=key)+ assert np.asarray(cut)[160, 179, 3] == 0 # the middle of the O's counter+ assert not logo_matte.see_through_inside(cut)["wide"]++def test_the_key_is_read_off_the_lettering_box_not_the_scene() -> None:mood = Image.new("RGB", (100, 50), (40, 200, 60)) # a forest scene ...for x in range(40, 60):diff --git a/tests/gen/test_logo.py b/tests/gen/test_logo.pyindex 13e20e7..1d4efc4 100644--- a/tests/gen/test_logo.py+++ b/tests/gen/test_logo.py@@ -17,7 +17,7 @@ import numpy as npimport pytestfrom PIL import Image-from logo_lettering import GREEN_PAINTED, MAGENTA_PAINTED, lettering, quilt+from logo_lettering import GREEN_PAINTED, MAGENTA_PAINTED, lettering, quilt, walled_patchfrom sprite_gen import genfrom sprite_gen.frames import logo_mattefrom sprite_gen.gen import base as gen_base@@ -422,6 +422,24 @@ def test_see_through_inside_the_letters_is_refused_and_kept_aside(tmp_path, fakeassert inside["pct_of_visible"] >= logo_matte.INSIDE_MAX_PCT and inside["wide"][email protected]("holes", [(), ((96, 156, 9),)], ids=["no-hole", "key-island"])+def test_a_hole_cut_in_a_see_through_patch_does_not_let_it_through(tmp_path, fake, holes) -> None:+ # the end validator's counterexample: a navy letter on green with a mint patch walled in it, with or without a 9x9+ # square of the key in the patch's middle; the square is cut to nothing and opens no way out of the letter+ provider = fake(reads=(["I"],))+ out = tmp_path / "logo"+ out.mkdir()+ walled_patch(holes=holes).save(out / "lettering.png")+ with pytest.raises(SystemExit, match="see-through inside"):+ _run(tmp_path, text="I", lettering=out / "lettering.png")+ assert provider.looked == [] # refused before the text check+ assert not (out / "logo.png").exists() and (out / "logo.rejected.png").is_file()+ report = _report(tmp_path)+ assert report["status"] == "refused" and report["refused_at"] == "matte"+ inside = report["logo"]["see_through_inside"]+ assert inside["wide"] and inside["glow_reach_px"] == logo_matte.INSIDE_EDGE_PX++def test_a_glowing_title_is_not_see_through_inside(tmp_path, fake) -> None:fake()assert _run(tmp_path) == 0diff --git a/tests/logo_lettering.py b/tests/logo_lettering.pyindex d09104d..15a3475 100644--- a/tests/logo_lettering.py+++ b/tests/logo_lettering.py@@ -9,6 +9,7 @@ key is exact alpha blending, so the truth (coverage and colour of every pixel) ifrom __future__ import annotations+from collections.abc import Callablefrom dataclasses import dataclassimport numpy as np@@ -49,6 +50,34 @@ def _letter_mask(size: tuple[int, int], *, pixel: bool = False) -> Image.Image:return mask+def hollow_letters(size: tuple[int, int], *, pixel: bool = False) -> Image.Image:+ """Two big 'letters' whose counters the key shows through, as a soft or hard mask: an O ring, and an A whose+ crossbar closes its counter and whose legs stand open below it."""+ width, height = size+ s = 4 if not pixel else 1+ mask = Image.new("L", (width * s, height * s), 0)+ draw = ImageDraw.Draw(mask)+ top, bottom = int(height * 0.12) * s, int(height * 0.88) * s+ tall = bottom - top+ stroke = max(4, int(height * 0.12)) * s+ cx, cy, r = int(width * 0.28) * s, (top + bottom) // 2, tall // 2+ draw.ellipse((cx - r, cy - r, cx + r, cy + r), fill=255)+ draw.ellipse((cx - r + stroke, cy - r + stroke, cx + r - stroke, cy + r - stroke), fill=0)+ ax, half = int(width * 0.72) * s, tall * 55 // 100+ draw.polygon([(ax, top), (ax + half, bottom), (ax - half, bottom)], fill=255)++ def leg(y: float, side: int) -> float: # a leg's inner edge at row y: its outer edge moved a stroke inward+ return ax + side * (half * (y - top) / tall - stroke)++ apex, bar_top, bar_bottom = top + stroke * tall / half, top + tall * 55 // 100, top + tall * 70 // 100+ draw.polygon([(ax, apex), (leg(bar_top, 1), bar_top), (leg(bar_top, -1), bar_top)], fill=0)+ draw.polygon([(leg(bar_bottom, -1), bar_bottom), (leg(bar_bottom, 1), bar_bottom),+ (leg(bottom + s, 1), bottom + s), (leg(bottom + s, -1), bottom + s)], fill=0)+ if s > 1:+ mask = mask.resize((width, height), Image.Resampling.LANCZOS)+ return mask++def lettering(*,size: tuple[int, int] = (320, 160),@@ -62,14 +91,15 @@ def lettering(noise: float = 2.0,pixel: bool = False,seed: int = 7,+ shapes: Callable[..., Image.Image] = _letter_mask,) -> Lettering:width, height = size- mask = np.asarray(_letter_mask(size, pixel=pixel), dtype=np.float64) / 255.0+ mask = np.asarray(shapes(size, pixel=pixel), dtype=np.float64) / 255.0body = maskcolour = np.broadcast_to(np.array(fill, dtype=np.float64), (height, width, 3)).copy()if outline is not None:- grown = np.asarray(_letter_mask(size, pixel=pixel).filter(ImageFilter.MaxFilter(7)), dtype=np.float64) / 255.0- inner = np.asarray(_letter_mask(size, pixel=pixel).filter(ImageFilter.MinFilter(3)), dtype=np.float64) / 255.0+ grown = np.asarray(shapes(size, pixel=pixel).filter(ImageFilter.MaxFilter(7)), dtype=np.float64) / 255.0+ inner = np.asarray(shapes(size, pixel=pixel).filter(ImageFilter.MinFilter(3)), dtype=np.float64) / 255.0body = grown# outline ink where the grown mask covers and the eroded fill does notcolour = inner[..., None] * np.array(fill, dtype=np.float64) + (1 - inner[..., None]) * np.array(outline, dtype=np.float64)@@ -125,6 +155,26 @@ def quilt(*, size: tuple[int, int] = (320, 160), key: tuple[int, int, int] = GREreturn Image.fromarray(np.uint8(np.clip(np.round(rgb), 0, 255)), "RGB"), mint+NAVY, KEY_GREEN = (30, 34, 70), (0, 255, 0)+++def walled_patch(*, patch: tuple[int, int, int, int] = (65, 135, 100, 220), holes: tuple[tuple[int, int, int], ...] = (),+ opening: tuple[int, int] | None = None) -> Image.Image:+ """One navy letter block (rows 20-180, columns 20-300 of a 200x320 picture) on pure green, flat and without+ noise, with a mint patch walled in by it (`patch`: top, bottom, left, right), which the matte cuts see-through on+ green. `holes` (top, left, side) are squares of the key painted into the letter, which it cuts to nothing;+ `opening` (row, column) is one pixel of the key. The default patch is the end validator's counterexample."""+ rgb = np.full((200, 320, 3), KEY_GREEN, dtype=np.uint8)+ rgb[20:180, 20:300] = NAVY+ top, bottom, left, right = patch+ rgb[top:bottom, left:right] = MINT+ for y, x, side in holes:+ rgb[y:y + side, x:x + side] = KEY_GREEN+ if opening is not None:+ rgb[opening] = KEY_GREEN+ return Image.fromarray(rgb, "RGB")++def key_excess(rgb: np.ndarray, key: str = "magenta") -> np.ndarray:"""How much of the key's hue a colour carries: min(R,B)-G for magenta, G-max(R,B) for green."""rgb = np.asarray(rgb, dtype=np.float64)