Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

fix(video-sway): --region-over and --region-membrane draw by one rule: the wear wears the crease in with the rest and, laid over the figure, the region reaches down to it again in every cell (crease_reach); the move draws the air and where the region lifts, the layer the band alone; --hold-mask pixels the region mask covers stay the region's on that path; the membrane is solved by passes from a cos² spread; and --region-membrane-unpin, --region-clear-alpha, --region-floor-beside and --region-own-outline shape the membrane, off by default

Soohong Kim9e5fdef0a812
diff --git a/sprite_gen/video/sway.py b/sprite_gen/video/sway.py
index 9279316..3e4d90d 100644
--- a/sprite_gen/video/sway.py
+++ b/sprite_gen/video/sway.py
@@ -150,17 +150,19 @@ and the upper yield's. The ellipses still say each region's gain and where `--ba
pixels take a held one is read from the others, and walked back only where they are half its weight, which the rim keeps
from happening. The record holds the mask, its carry in every cell and a ledger per cell (`region_ledger`). Not with
`--band`, a region's flat share, `--stretch-floor` nor `--hold-mask` (but under `--region-over`). The crease is the
-picture's own edge: `--region-shrink` wears the mask in everywhere else and leaves its lower edge on the crease
-(`crease_kept`).
+picture's own edge: under `--region-over` the region reaches down to it again in every cell where the wear lifted it
+(`crease_reach`).
`--region-over` (with `--region-mask`; off by default, and off nothing of it is read or written) lays the region over the
figure under it (`layer_sets`, `lay_over`): the held pixels under its lower edge, solid or not — in its columns and the
air's reach past them — are no anchor of the rim, so the lower edge moves with the rest, and the band of them within the
air's reach of it moves too and is drawn over: the region's own pixels (with its soft edge, which goes with it beside the
figure too, and its outline against the air) read at the move and laid by their alpha over the band's own pixel, which
-keeps its bytes where they do not land, and over the air, which shows them and never the figure beside it; where the
-region lifts, its pixels read the figure under it. `--hold-mask` names the pixels it
-leaves out (an arm), carried as a band's are: never the region's, never drawn over, an anchor of the rim. `--region-shear
+keeps its bytes where they do not land; the air and where the region lifts are drawn by the move, the figure under it
+showing there. With `--region-membrane` what lies under the lower edge within its reach moves as a membrane between
+the region's move and none (`membrane_move`), drawn by that move. `--hold-mask` names the pixels it
+leaves out (an arm), carried as a band's are: where the mask does not cover them never the region's, never drawn over, an
+anchor of the rim; where it does, the region keeps them and they move with it. `--region-shear
S` (off by default) widens the rim so a move one way rises across it the other way by at most S px a px (`shear_floor`):
the fold rule reads each way by the move along it alone, and a move down rose all at once across a rim sized for a small
move across. With either the ledger reads the region's edges too: the steps the drawn move takes (`move_steps`), the
@@ -264,8 +266,9 @@ REGION_AIR = 2 # px past the move's reach (8-neighbour steps) round the region
CREASE_UP, CREASE_DOWN = 5, 3 # rows above and below a region's lowest pixel its lower edge is looked for (`--region-crease`)
CREASE_SPAN = 2 # columns either way the crease's rows are taken the median over
# A region laid over the figure under it (`--region-over`): what lies under its lower edge no anchor of the rim, the band of
-# it within the air's reach drawn over by the region's own pixels by their alpha and kept where they do not land.
-OVER_POLICY = "under-edge-free/band-and-air-laid-by-alpha/air-outline-with/1"
+# it within the air's reach drawn over by the region's own pixels by their alpha and kept where they do not land; the air
+# and where it lifts drawn by the move; with `--region-crease` its lower edge reaching down to the crease in every cell.
+OVER_POLICY = "under-edge-free/band-laid-by-alpha/air-and-lift-by-move/crease-reach/2"
# Laid over with `--region-over-solid`: of what lies under its lower edge only the solid pixels are free of the rim and laid
# over, the rest stays and holds the rim, and its soft edge goes with it even where it touches the figure under it.
OVER_SOLID_POLICY = "under-edge-solid-free/soft-edge-with/1"
@@ -273,9 +276,11 @@ OVER_SOLID_POLICY = "under-edge-solid-free/soft-edge-with/1"
# edge) hold the rim round, the wider of its two widths each way; the rest of the anchors hold it as ever.
RIM_ROUND_POLICY = "beside-above-floor/round-wider-width/1"
# Laid over the figure under it, a membrane (`--region-membrane N`): what lies under its lower edge within N + 1 px of it
-# moves, each pixel's move the mean of its figure 4-neighbours', between the region's own and none on the rest of the figure.
-MEMBRANE_POLICY = "under-edge-within-n/figure-4-mean/conjugate-gradients/1"
-MEMBRANE_TOL = 1e-9 # px: the membrane is solved until the 4-neighbour means are met to within this
+# moves, starting at the region's nearest move falling as cos² over N + 1 px, then each pixel's move the mean of its figure
+# 4-neighbours', between the region's own and none on the rest of the figure, pass after pass.
+MEMBRANE_POLICY = "under-edge-within-n/spread-cos2-start/figure-4-mean-passes/2"
+MEMBRANE_STEP = 1e-4 # px: the membrane's passes stop at the first that moves no pixel this far
+MEMBRANE_PASSES = 1200 # the most passes the membrane takes
TIP_COLS = 10 # the region's columns at its tip the ledger reads the tip's shape and move over (`tip_shape`)
TIP_AIR = 8 # px of the air round the tip's columns and rows the tip's window takes
TIP_RING = 3 # px round the source's outline against the air the tip's shape is read over
@@ -1307,15 +1312,16 @@ def read_ask(regions: list[tuple[float, ...]], *, gain: float | None = None, fre
region_mask: Path | None = None, region_rim: float | None = None, region_shrink: int | None = None,
region_crease: bool = False, region_over: bool = False, region_shear: float | None = None,
region_membrane: int | None = None, region_over_solid: bool = False, region_rim_round: bool = False,
- verb: str = VERB) -> Ask:
+ region_membrane_unpin: bool = False, region_clear_alpha: int | None = None,
+ region_floor_beside: int | None = None, region_own_outline: int | None = None, verb: str = VERB) -> Ask:
"""A sway's ask, read and checked. A number left out (None) is its default: the damped mass's gain, freq and zeta,
or, on the breath (`breathe`), the breath's gain and lag. `band` (px or AUTO; None: off) moves each region as one
mass, `band_margin` and `band_up` shaping it, and `hold_mask` (a PNG or a folder of them; None: off) holds pixels
the band moves round. `region_mask` (a PNG or a folder of them; None: off) gives the region as a mask instead,
`region_rim`, `region_shrink`, `region_crease`, `region_over` (with `hold_mask` the pixels its layer leaves out,
- `region_over_solid` which pixels under it it lays over, `region_membrane` the figure under it a membrane moves and
- `region_rim_round` the rim held round beside it),
- `region_shear` and `band_up` shaping it."""
+ `region_over_solid` which pixels under it it lays over, `region_membrane` the figure under it a membrane moves and,
+ with it, `region_membrane_unpin`, `region_clear_alpha`, `region_floor_beside` and `region_own_outline`, and
+ `region_rim_round` the rim held round beside it), `region_shear` and `band_up` shaping it."""
# The numbers go into the record, which is JSON, and into the cells' arithmetic: a numpy number from a
# caller (an int64 has no JSON form) is read as the float the command line gives, so every caller of the
# same numbers writes the same strip and record.
@@ -1354,7 +1360,9 @@ def read_ask(regions: list[tuple[float, ...]], *, gain: float | None = None, fre
if region_mask is not None:
masked = _read_region_mask(regions, region_mask, region_rim, region_shrink, region_crease, band, band_margin, band_up,
stretch_floor, hold_mask, verb, over=region_over, shear=region_shear,
- membrane=region_membrane, over_solid=region_over_solid, rim_round=region_rim_round)
+ membrane=region_membrane, over_solid=region_over_solid, rim_round=region_rim_round,
+ unpin=region_membrane_unpin, clear_alpha=region_clear_alpha,
+ floor_beside=region_floor_beside, own_outline=region_own_outline)
return Ask(regions, gain, freq, zeta, on_fold, None if read_on is None else str(read_on), stretch_floor, gain_across,
breath, mask=masked)
if region_rim is not None or region_shrink is not None or region_crease:
@@ -1362,6 +1370,9 @@ def read_ask(regions: list[tuple[float, ...]], *, gain: float | None = None, fre
if region_over or region_shear is not None or region_membrane is not None or region_over_solid or region_rim_round:
raise SystemExit(f"{verb}: --region-over, --region-over-solid, --region-rim-round, --region-shear and --region-membrane "
"shape a region mask; they need --region-mask")
+ if region_membrane_unpin or region_clear_alpha is not None or region_floor_beside is not None or region_own_outline is not None:
+ raise SystemExit(f"{verb}: --region-membrane-unpin, --region-clear-alpha, --region-floor-beside and --region-own-outline "
+ "shape a region mask's membrane; they need --region-mask")
shaped = _read_band(regions, band, band_margin, band_up, stretch_floor, verb)
if hold_mask is not None and shaped is None:
raise SystemExit(f"{verb}: --hold-mask keeps pixels as they are while a band moves its regions round them; it needs --band")
@@ -1395,10 +1406,19 @@ def _read_region_mask(regions: list[tuple[float, ...]], path: Path, rim: float |
band: float | str | None, margin: float | str | None, up: tuple[float, float | str] | str | None,
stretch_floor: float | None, hold_mask: Path | None, verb: str, *, over: bool = False,
shear: float | None = None, membrane: int | None = None, over_solid: bool = False,
- rim_round: bool = False) -> RegionMask:
+ rim_round: bool = False, unpin: bool = False, clear_alpha: int | None = None,
+ floor_beside: int | None = None, own_outline: int | None = None) -> RegionMask:
"""The region mask asked (`--region-mask`, `--region-rim`, `--region-shrink`, `--region-crease`, `--region-over` and,
with it, `--hold-mask`, `--region-over-solid`, `--region-rim-round` and `--region-membrane`, `--region-shear`,
- `--band-up`), checked as the command line checks it, and its PNGs read."""
+ `--band-up`; the membrane's `--region-membrane-unpin`, `--region-clear-alpha`, `--region-floor-beside` and
+ `--region-own-outline`, which shape nothing without one), checked as the command line checks it, and its PNGs read."""
+
+ def whole(given: object) -> int: # a whole number as given; -1 where it is none (a NaN, an infinity, not a number)
+ try:
+ return int(given) if not isinstance(given, bool) and float(given) == int(given) else -1
+ except (TypeError, ValueError, OverflowError):
+ return -1
+
if band is not None:
raise SystemExit(f"{verb}: --region-mask moves the region's own pixels and holds every visible pixel round it, the weight "
"falling over a rim inside it; --band moves an ellipse over a band outside it: give one of them")
@@ -1432,11 +1452,7 @@ def _read_region_mask(regions: list[tuple[float, ...]], path: Path, rim: float |
if membrane is not None:
if not over:
raise SystemExit(f"{verb}: --region-membrane moves the figure under a region laid over it; it needs --region-over")
- given = membrane
- try:
- membrane = int(given) if not isinstance(given, bool) and float(given) == int(given) else -1
- except (TypeError, ValueError, OverflowError): # a NaN, an infinity, not a number
- membrane = -1
+ given, membrane = membrane, whole(membrane)
if membrane < 0:
raise SystemExit(f"{verb}: --region-membrane is how far under the region's lower edge the membrane reaches, a "
f"whole number of px past the first, 0 or more; got {given!r}")
@@ -1444,19 +1460,27 @@ def _read_region_mask(regions: list[tuple[float, ...]], path: Path, rim: float |
if not rim >= 1 or not math.isfinite(rim): # a NaN is not
raise SystemExit(f"{verb}: --region-rim is how many times the fold rule's least band the rim starts at, 1 or more; got {rim:g}")
asked = SHRINK_DEFAULT if shrink is None else shrink
- try:
- shrink = int(asked) if not isinstance(asked, bool) and float(asked) == int(asked) else -1
- except (TypeError, ValueError, OverflowError): # a NaN, an infinity, not a number
- shrink = -1
+ shrink = whole(asked)
if shrink < 0:
raise SystemExit(f"{verb}: --region-shrink is the px the mask is worn in by, a whole number, 0 or more; got {asked!r}")
+ if clear_alpha is not None:
+ given, clear_alpha = clear_alpha, whole(clear_alpha)
+ if not 1 <= clear_alpha < ALPHA_SOLID:
+ raise SystemExit(f"{verb}: --region-clear-alpha is the alpha under which a pixel is read as clear, a whole number "
+ f"from 1 to {ALPHA_SOLID - 1} (under a solid pixel's); got {given!r}")
+ for flag, given in (("--region-floor-beside", floor_beside), ("--region-own-outline", own_outline)):
+ if given is not None and whole(given) < 0:
+ raise SystemExit(f"{verb}: {flag} is px, a whole number, 0 or more; got {given!r}")
+ floor_beside = None if floor_beside is None else whole(floor_beside)
+ own_outline = None if own_outline is None else whole(own_outline)
try:
shaped = None if up is None else parse_band_up(up if isinstance(up, str) else ",".join(str(v) for v in up))
except argparse.ArgumentTypeError as exc:
raise SystemExit(f"{verb}: {exc}") from exc
masks, digests, per_cell = _read_masks(path, "--region-mask", verb)
return RegionMask(masks, digests, per_cell, rim, shrink, bool(crease), shaped, bool(over), shear,
- None if hold_mask is None else read_hold(hold_mask, verb), membrane, bool(over_solid), bool(rim_round))
+ None if hold_mask is None else read_hold(hold_mask, verb), membrane, bool(over_solid), bool(rim_round),
+ bool(unpin), clear_alpha, floor_beside or 0, own_outline or 0)
@dataclass(frozen=True, eq=False)
@@ -1489,7 +1513,9 @@ def read_request(loop_dir: Path, regions: list[tuple[float, ...]], *, gain: floa
hold_mask: Path | None = None, region_mask: Path | None = None, region_rim: float | None = None,
region_shrink: int | None = None, region_crease: bool = False, region_over: bool = False,
region_shear: float | None = None, region_membrane: int | None = None,
- region_over_solid: bool = False, region_rim_round: bool = False, read_on_cut: Path | None = None) -> Request:
+ region_over_solid: bool = False, region_rim_round: bool = False, region_membrane_unpin: bool = False,
+ region_clear_alpha: int | None = None, region_floor_beside: int | None = None,
+ region_own_outline: int | None = None, read_on_cut: Path | None = None) -> Request:
loop_dir = loop_dir.expanduser().resolve()
metas = sorted(loop_dir.glob("*.strip.json"))
if len(metas) != 1:
@@ -1505,7 +1531,8 @@ def read_request(loop_dir: Path, regions: list[tuple[float, ...]], *, gain: floa
hold_mask=hold_mask, region_mask=region_mask, region_rim=region_rim, region_shrink=region_shrink,
region_crease=region_crease, region_over=region_over, region_shear=region_shear,
region_membrane=region_membrane, region_over_solid=region_over_solid, region_rim_round=region_rim_round,
- verb=verb)
+ region_membrane_unpin=region_membrane_unpin, region_clear_alpha=region_clear_alpha,
+ region_floor_beside=region_floor_beside, region_own_outline=region_own_outline, verb=verb)
if read_on_cut is None:
return Request(loop_dir, name, meta_path, meta_bytes, meta, ask)
return Request(loop_dir, name, meta_path, meta_bytes, meta, ask, read_cut(read_on_cut, ask.read_on, verb),
@@ -1638,14 +1665,31 @@ class Answer:
way, from the held pixels beside the region), and with `--band-up` the lesser of that and each region's upper
yield, the ellipse carried by its own pixels as a band carries it (`Masked.up`); 0 elsewhere. Laid over the figure
under it (`--region-over`, `layer_sets`), what lies under its lower edge is no anchor of the rim and the band of it
- moves too, the pixels `--hold-mask` names left out of the region and of the band."""
+ moves too, the pixels `--hold-mask` names left out of its soft edge and of the band (the region keeps its own); with
+ `--region-crease` the region reaches down to the crease carried into the cell (`crease_reach`). With a membrane
+ (`--region-membrane`) the region grows over the outline its mask leaves out (`--region-own-outline`, `own_outline`),
+ the lower edge beside it is set by the lowest pixel near each side (`--region-floor-beside`), what the membrane
+ moves is no anchor of the rim (`--region-membrane-unpin`), and a pixel under `--region-clear-alpha` is read as
+ clear (`RegionMask.alpha_of`)."""
assert self.mask is not None
masked = self.mask
- alpha = np.asarray(self.cells[k].getchannel("A"))
- region = masked.region_in(k, alpha >= ALPHA_SOLID)
+ cell = self.cells[k]
+ solid = np.asarray(cell.getchannel("A")) >= ALPHA_SOLID
+ alpha = masked.asked.alpha_of(cell)
+ region = masked.region_in(k, solid)
if masked.asked.over:
left_out = masked.left_out_in(k)
- sets = layer_sets(region & ~left_out, alpha, masked.air, left_out, masked.asked.over_solid)
+ if masked.creases and masked.creases[0] and masked.carried is not None:
+ px = masked.carried[k]["px"]
+ region = crease_reach(region, solid, masked.creases[0]["rows"],
+ (px[0] - masked.base_shift[0], px[1] - masked.base_shift[1]))
+ membrane = masked.asked.membrane is not None
+ if membrane and masked.asked.own_outline:
+ region = own_outline(region, solid, alpha == 0, left_out, masked.asked.own_outline)
+ sets = layer_sets(region, alpha, masked.air, left_out, masked.asked.over_solid,
+ masked.asked.floor_beside if membrane else 0)
+ if membrane and masked.asked.unpin:
+ sets["anchors"] = sets["anchors"] & ~membrane_sets(sets, masked.asked.membrane)
anchors, moving = sets["anchors"], sets["region"] | sets["soft"] | sets["air"] | sets["band"]
sets["left_out"] = region & left_out
else:
@@ -1677,10 +1721,10 @@ class Answer:
def masked_cell(self, k: int) -> tuple[Image.Image, dict[str, Any]]:
"""Under a region mask, cell k drawn by its move (`mask_field`) as a hold draws it (`draw_held`), every read its
whole move and no held pixel read; what it moved and held, the move, and how each pixel was read. Laid over the
- figure under it (`--region-over`), the band, the air and what a lift uncovers are drawn as the layer lands on them
- (`lay_over`), not read by `draw_held`: the held pixels are then those it keeps, the move 0 on them; with a membrane
- (`--region-membrane`), what lies under its lower edge within its reach moves by it (`membrane_sets`,
- `membrane_move`) and is drawn by `draw_held`, never laid over. Kept in `Masked.drawn`."""
+ figure under it (`--region-over`), the band is drawn as the layer lands on it (`lay_over`), not read by
+ `draw_held`, which draws the air and where the region lifts by the move: the held pixels are then those it keeps,
+ the move 0 on them; with a membrane (`--region-membrane`), what lies under its lower edge within its reach moves by
+ it (`membrane_sets`, `membrane_move`) and is drawn by `draw_held`, never laid over. Kept in `Masked.drawn`."""
assert self.mask is not None
cell = self.cells[k]
w, h = cell.size
@@ -1696,26 +1740,23 @@ class Answer:
| ({} if self.mask.asked.membrane is None else {"membrane": still > 0})
elif self.mask.asked.over:
band, free = sets["band"], np.zeros((h, w), bool)
- if self.mask.asked.membrane is not None:
+ reach = self.mask.asked.membrane
+ if reach is not None:
# The membrane under the lower edge: its move hangs between the region's own (its soft edge and its outline
# over the band too) and none on the rest of the figure, and it is drawn by that move, never laid over.
- free = membrane_sets(sets, self.mask.asked.membrane)
- own = sets["region"] | sets["soft"] | sets["edge"]
- free, free_x, free_y = membrane_move(free, np.asarray(cell.getchannel("A")) > 0,
- (np.where(own, field[1], 0.0), np.where(own, field[2], 0.0)))
- field = (field[0] | free, np.where(free, free_x, field[1]), np.where(free, free_y, field[2]))
+ free = membrane_sets(sets, reach)
+ field = (field[0] | free, *membrane_move(free, self.mask.asked.alpha_of(cell) > 0, sets["region"] | sets["soft"],
+ sets["edge"], (field[1], field[2]), reach))
band = band & ~free
- layered = band | sets["air"] | uncovered(sets, field[2])
- # The band, the air and what a lift uncovers are the layer's to draw (`lay_over`, every pixel of them): no read
- # of `draw_held`'s is taken there.
- drawn, read = draw_held(cell, (field[0] & ~layered, field[1], field[2]), sets["held"] & ~sets["band"] & ~free,
+ # The air and where the region lifts are drawn by the move (`draw_held`, the band under it read there), the band
+ # by the layer (`lay_over`, every pixel of it): no read of `draw_held`'s is taken on the band.
+ drawn, read = draw_held(cell, (field[0] & ~band, field[1], field[2]), sets["held"] & ~sets["band"] & ~free,
np.zeros((h, w), bool), short=False)
drawn, laid, over, lifted = lay_over(cell, drawn, field, {**sets, "band": band})
kept = band & ~laid
field = (field[0], np.where(kept, 0.0, field[1]), np.where(kept, 0.0, field[2]))
- # There the read is the layer's: its whole move, none where the band keeps its bytes.
- read = {**read, "read_x": np.where(layered, field[1], read["read_x"]),
- "read_y": np.where(layered, field[2], read["read_y"])}
+ # On the band the read is the layer's: its whole move, none where the band keeps its bytes.
+ read = {**read, "read_x": np.where(band, field[1], read["read_x"]), "read_y": np.where(band, field[2], read["read_y"])}
# What it keeps: the held pixels less the band the layer lands on, the outline that goes with it and what the
# membrane moves; what no read takes: the held pixels but the band, which the region reads where it lifts.
sets = {**sets, "held": sets["held"] & ~laid & ~free, "unread": sets["held"] & ~sets["band"] & ~free, "over": over,
@@ -1794,8 +1835,8 @@ class Answer:
the cells that kept the carry of the cell before and why (None and none where a mask is given for each cell); the
upper yield's bands and carry (`--band-up`; None without it); and the ledger (`region_ledger`). The rim `--region-
shear` asks at least (`rim.shear_px`) and the layer (`over`: how deep its band reaches, the membrane under it
- (`--region-membrane`: how far it reaches and how it is solved), and the pixels `--hold-mask` leaves out, carried as a
- hold mask is) only where they are asked."""
+ (`--region-membrane`: how far it reaches and how its passes stop), and the pixels `--hold-mask` leaves out, carried
+ as a hold mask is) only where they are asked."""
assert self.mask is not None
masked = self.mask
across, down, slope, least = masked.rim
@@ -1816,7 +1857,8 @@ class Answer:
} | ({} if not masked.asked.over else {"over": {"policy": OVER_POLICY, "band_px": masked.air} | (
{} if not masked.asked.over_solid else {"solid": OVER_SOLID_POLICY}) | (
{} if masked.asked.membrane is None else
- {"membrane": {"policy": MEMBRANE_POLICY, "reach_px": masked.asked.membrane + 1, "tol_px": MEMBRANE_TOL}}) | {
+ {"membrane": {"policy": MEMBRANE_POLICY, "reach_px": masked.asked.membrane + 1, "step_px": MEMBRANE_STEP,
+ "passes_most": MEMBRANE_PASSES}}) | {
"left_out": None if left_out is None else {
"grow_px": None if left_out.asked.per_cell else HOLD_GROW,
"pieces": [{"px": int(piece.sum()), "box": _box(piece)} for piece in left_out.pieces],
@@ -2654,7 +2696,11 @@ class RegionMask:
(`--region-shear`, px a px; None: the rim by the move along it alone), how far under its lower edge a membrane
moves the figure under it (`--region-membrane`, px; None: none, the layer slides over it), whether only the solid
pixels under it are laid over (`--region-over-solid`), and whether the held pixels beside it hold the rim round
- (`--region-rim-round`). The record names the last six only where they are asked."""
+ (`--region-rim-round`); with a membrane, whether what it moves is no anchor of the rim (`--region-membrane-unpin`),
+ the alpha under which a pixel is read as clear (`--region-clear-alpha`; None: 0's alone), how far into the region's
+ columns from each side the lowest pixel sets the lower edge beside it (`--region-floor-beside`, px; 0: the nearest
+ column's), and how far the region grows over the outline its mask leaves out (`--region-own-outline`, px; 0: none).
+ The record names the last ten only where they are asked, the membrane's four only with one."""
masks: list[np.ndarray]
sha256: list[str]
@@ -2669,6 +2715,10 @@ class RegionMask:
membrane: int | None = None
over_solid: bool = False
rim_round: bool = False
+ unpin: bool = False
+ clear_alpha: int | None = None
+ floor_beside: int = 0
+ own_outline: int = 0
def asked(self) -> dict[str, Any]:
return {"per_cell": self.per_cell, "sha256": self.sha256 if self.per_cell else self.sha256[0], "rim": self.rim,
@@ -2678,7 +2728,22 @@ class RegionMask:
| ({} if not self.over_solid else {"over_solid": True}) \
| ({} if not self.rim_round else {"rim_round": True}) \
| ({} if self.shear is None else {"shear": self.shear}) \
- | ({} if self.membrane is None else {"membrane_px": self.membrane})
+ | ({} if self.membrane is None else {"membrane_px": self.membrane} | self.membrane_asked())
+
+ def alpha_of(self, cell: Image.Image) -> np.ndarray:
+ """The cell's alpha as the sets and a membrane read it: under `--region-clear-alpha` (with a membrane) 0, clear;
+ the cell's bytes are its own."""
+ alpha = np.asarray(cell.getchannel("A"))
+ return alpha if self.membrane is None or self.clear_alpha is None else np.where(alpha < self.clear_alpha, 0, alpha)
+
+ def membrane_asked(self) -> dict[str, Any]:
+ """The membrane's own keys as the record names them: those asked, with a membrane; none without one."""
+ if self.membrane is None:
+ return {}
+ return ({} if not self.unpin else {"membrane_unpin": True}) \
+ | ({} if self.clear_alpha is None else {"clear_alpha": self.clear_alpha}) \
+ | ({} if not self.floor_beside else {"floor_beside_px": self.floor_beside}) \
+ | ({} if not self.own_outline else {"own_outline_px": self.own_outline})
def edges(self) -> bool:
"""Whether the ledger reads the region's edges (`still_strip`, `tip_shape`, `move_steps`): with `--region-over`
@@ -2775,16 +2840,27 @@ def crease_of(cell: Image.Image, mask: np.ndarray) -> tuple[np.ndarray, dict[str
return out, {"rows": {str(x): y for x, y in sorted(rows.items())}, "free_columns": free}
-def crease_kept(trimmed: np.ndarray, worn: np.ndarray, crease: dict[str, Any]) -> np.ndarray:
- """`worn` — the mask ended at its crease (`trimmed`, `crease_of`) and worn in (`--region-shrink`) — with its lower edge
- back on the crease: in each column the crease ended (`crease`'s rows), where the worn mask holds a pixel, the trimmed
- mask's pixels from its lowest one down to the crease's row. The crease is the picture's own edge, not one drawn a
- little large, so the wear leaves it where it is; it still wears the mask in everywhere else."""
- out = worn.copy()
- for x, y in ((int(x), int(y)) for x, y in crease["rows"].items()):
+def crease_reach(region: np.ndarray, solid: np.ndarray, rows: dict[str, int], shift: tuple[float, float]) -> np.ndarray:
+ """`region` — a cell's, its mask carried `shift` from the cell it was drawn on — reaching down to its crease again
+ where the wear lifted it (`--region-over` with `--region-crease`): the crease's rows (`crease_of`, columns and rows on
+ that cell; a column between two of them takes the nearer's row, the left one's on a tie) carried by `shift` (the column
+ rounded to the nearest, half to even), and in each column the crease reaches, where the region holds a pixel, the cell's
+ solid pixels from its lowest one down to the crease's row. The crease is the picture's own edge, not one drawn a little
+ large, so the wear leaves it where it is; it still wears the mask in everywhere else."""
+ h, w = region.shape
+ columns = sorted(int(x) for x in rows)
+ if not columns:
+ return region.copy()
+ line: dict[int, float] = {}
+ for x in range(max(0, columns[0]), min(w, columns[-1] + 1)):
+ at = int(round(x + shift[0]))
+ if 0 <= at < w:
+ line[at] = rows[str(min(columns, key=lambda q: abs(q - x)))] + shift[1]
+ out = region.copy()
+ for x, y in line.items():
if out[:, x].any():
- lowest = int(np.nonzero(out[:, x])[0].max())
- out[lowest + 1:y + 1, x] |= trimmed[lowest + 1:y + 1, x]
+ lowest, crease = int(np.nonzero(out[:, x])[0].max()), int(math.floor(y))
+ out[lowest + 1:crease + 1, x] |= solid[lowest + 1:crease + 1, x]
return out
@@ -2886,10 +2962,11 @@ def region_sets(region: np.ndarray, alpha: np.ndarray, air: int) -> tuple[np.nda
def layer_sets(region: np.ndarray, alpha: np.ndarray, air: int, left_out: np.ndarray,
- solid_only: bool = False) -> dict[str, np.ndarray]:
+ solid_only: bool = False, beside: int = 0) -> dict[str, np.ndarray]:
"""What a region laid over the figure under it moves, draws over and holds in a cell (`--region-over`; `alpha`: the
- cell's alpha, `air`: the move's reach and REGION_AIR px, `left_out`: the pixels `--hold-mask` names there, none of
- them the region's; `solid_only`: `--region-over-solid`, the changes it makes said where they fall):
+ cell's alpha, `air`: the move's reach and REGION_AIR px, `left_out`: the pixels `--hold-mask` names there, which the
+ region keeps where it holds them and which are left out of all else; `solid_only`: `--region-over-solid`, the changes
+ it makes said where they fall; `beside`: `--region-floor-beside`, the lower edge beside its columns):
- `soft`: its soft edge, the pixels not opaque within REGION_SOFT 4-neighbour steps of it, beside another solid pixel
or not (left beside the figure under it, it would stay behind as a ghost of its outline), none left out; under its
lower edge (`floor`) only those touching no solid pixel outside it (8-neighbour): one that does is the figure's own
@@ -2908,7 +2985,8 @@ def layer_sets(region: np.ndarray, alpha: np.ndarray, air: int, left_out: np.nda
(4-neighbour), within 2 px of it, touching no other solid pixel outside it (4-neighbour) — which go with the layer and
leave the air behind; an outline pixel that touches the figure under it or a pixel left out is the figure's, kept;
- `anchors`: the held pixels the rim falls to: all but those under its lower edge;
- - `floor`: per column, the first row under its lower edge (that of its nearest column), the cell's height where none."""
+ - `floor`: per column, the first row under its lower edge (that of its nearest column; with `beside` N, past its
+ columns that of the lowest pixel of its columns within N of that side), the cell's height where none."""
solid = alpha >= ALPHA_SOLID
h, w = region.shape
under = np.zeros((h, w), bool)
@@ -2916,8 +2994,11 @@ def layer_sets(region: np.ndarray, alpha: np.ndarray, air: int, left_out: np.nda
xs = np.nonzero(region.any(axis=0))[0]
if len(xs):
lowest = {int(x): int(np.nonzero(region[:, x])[0].max()) for x in xs}
- for x in range(max(0, int(xs.min()) - air), min(w, int(xs.max()) + air + 1)):
- floor[x] = lowest[min(lowest, key=lambda q: abs(q - x))] + 1
+ left, right = int(xs.min()), int(xs.max())
+ sides = (max(lowest[q] for q in lowest if q <= left + beside), max(lowest[q] for q in lowest if q >= right - beside))
+ for x in range(max(0, left - air), min(w, right + air + 1)):
+ floor[x] = (sides[0] if beside and x < left else sides[1] if beside and x > right
+ else lowest[min(lowest, key=lambda q: abs(q - x))]) + 1
under[floor[x]:, x] = True
soft = (alpha > 0) & ~solid & _grow4(region, REGION_SOFT) & ~region & ~left_out
if not solid_only:
@@ -2943,84 +3024,98 @@ def membrane_sets(sets: dict[str, np.ndarray], reach: int) -> np.ndarray:
return sets["below"] & ~sets["edge"] & (rim_distance(sets["region"] | sets["soft"], reach + 1, reach + 1) < 1)
-def membrane_move(free: np.ndarray, figure: np.ndarray, fixed: tuple[np.ndarray, np.ndarray]) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
- """The move of the pixels `free` as a membrane stretched between the moves `fixed` gives every other pixel of the figure
- (`figure`: the pixels not clear; `fixed` across and down, read off them alone): each free pixel's move the mean of its
- 4 neighbours' that are the figure, the air none of them — the point the 4-neighbour mean comes to when it is taken over
- and over, solved straight (conjugate gradients, to MEMBRANE_TOL px). A piece of them (4-neighbour) that no figure pixel
- outside it touches has nothing to hang between and is left out. The free pixels kept, and the move across and down on
- them (0 elsewhere)."""
+def own_outline(region: np.ndarray, solid: np.ndarray, clear: np.ndarray, left_out: np.ndarray, steps: int) -> np.ndarray:
+ """`region` grown over the outline its mask leaves out (`--region-own-outline G`, with a membrane): G 4-neighbour steps
+ through the cell's solid pixels within G 4-neighbour steps of a clear one (`clear`), never under its lower edge's line
+ (in every column, under the lowest pixel of the region's nearest column as it stands before), never a pixel
+ `--hold-mask` leaves out (`left_out`)."""
+ h, w = region.shape
+ xs = np.nonzero(region.any(axis=0))[0]
+ if not len(xs):
+ return region.copy()
+ lowest = {int(x): int(np.nonzero(region[:, x])[0].max()) for x in xs}
+ under = np.arange(h)[:, None] > np.array([lowest[min(lowest, key=lambda q: abs(q - x))] for x in range(w)])[None, :]
+ zone = solid & ~left_out & ~under & ~region & _grow4(clear, steps)
+ grown = region.copy()
+ for _ in range(steps):
+ grown = grown | (_grow4(grown, 1) & zone)
+ return grown
+
+
+def membrane_move(free: np.ndarray, figure: np.ndarray, own: np.ndarray, outline: np.ndarray,
+ move: tuple[np.ndarray, np.ndarray], reach: int) -> tuple[np.ndarray, np.ndarray]:
+ """The move of the pixels `free` as a membrane stretched between the region's own move (`move`, across and down, on
+ `own` — the region and its soft edge — and on its outline over the band, `outline`) and none on the rest of the figure
+ (`figure`: the pixels not clear), the air none of them. Each free pixel starts at the move of the nearest pixels of
+ `own` (`_spread`, over reach + 1 steps; its own move where none reaches it), falling as cos² of its distance to them
+ over reach + 1 px (`rim_distance`), and is then the mean of its 4 neighbours' that are the figure, all of them at
+ once, pass after pass, until a pass moves none of them MEMBRANE_STEP px or more (MEMBRANE_PASSES at most). The move
+ across and down: the membrane's on the free pixels, `move`'s elsewhere."""
h, w = free.shape
- four = ((-1, 0), (1, 0), (0, -1), (0, 1))
-
- def toward(a: np.ndarray, oy: int, ox: int) -> np.ndarray: # each pixel's neighbour (oy, ox) away; past the edge, 0
- p = np.pad(a, 1)
- return p[1 + oy:1 + oy + h, 1 + ox:1 + ox + w]
-
- holding = figure & ~free
- kept = free & np.logical_or.reduce([toward(holding, oy, ox) for oy, ox in four])
- while True: # every free pixel joined to one that touches the rest of the figure
- grown = kept | (_grow4(kept, 1) & free)
- if (grown == kept).all():
- break
- kept = grown
- moves = (np.zeros((h, w)), np.zeros((h, w)))
- if not kept.any():
- return kept, *moves
- ys, xs = np.nonzero(kept)
+ falls = np.cos(0.5 * math.pi * rim_distance(own, reach + 1, reach + 1)) ** 2
+ start = [np.where(free, _spread(way, own, free, reach + 1) * falls, way) for way in move]
+ if not free.any():
+ return start[0], start[1]
+ fixed = own | outline
+ ux, uy = (np.where(free, way, np.where(fixed, way, 0.0)) for way in start)
+ # The passes read only the free pixels and their neighbours: the box round them, a px past it.
+ ys, xs = np.nonzero(free)
y0, y1, x0, x1 = max(int(ys.min()) - 1, 0), min(int(ys.max()) + 2, h), max(int(xs.min()) - 1, 0), min(int(xs.max()) + 2, w)
- box, inside = (slice(y0, y1), slice(x0, x1)), kept[y0:y1, x0:x1]
+ box = (slice(y0, y1), slice(x0, x1))
+ inside, bx, by = free[box], ux[box], uy[box]
bh, bw = inside.shape
- # The figure's 4 neighbours of each pixel of the box, the cell's own past it.
- fig = np.pad(figure, 1)[y0:y1 + 2, x0:x1 + 2]
- near = [fig[1 + oy:1 + oy + bh, 1 + ox:1 + ox + bw] for oy, ox in four]
- count = np.sum(near, axis=0).astype(np.float64)
+ four = ((-1, 0), (1, 0), (0, -1), (0, 1))
+ near = np.pad(figure, 1)[y0:y1 + 2, x0:x1 + 2]
+ near = [near[1 + oy:1 + oy + bh, 1 + ox:1 + ox + bw] for oy, ox in four]
+ count = np.maximum(sum(n.astype(float) for n in near), 1.0)
- def apply(u: np.ndarray) -> np.ndarray: # count·u less the free neighbours' u, on the free pixels
+ def mean(u: np.ndarray) -> np.ndarray: # each pixel's figure 4-neighbours' mean
p = np.pad(u, 1)
- return np.where(inside, count * u - sum(n * p[1 + oy:1 + oy + bh, 1 + ox:1 + ox + bw] for n, (oy, ox) in zip(near, four)), 0.0)
-
- for way, given in enumerate(fixed):
- held_at = np.pad(np.where(holding, given, 0.0), 1)[y0:y1 + 2, x0:x1 + 2]
- b = np.where(inside, sum(n * held_at[1 + oy:1 + oy + bh, 1 + ox:1 + ox + bw] for n, (oy, ox) in zip(near, four)), 0.0)
- u, r = np.zeros((bh, bw)), b.copy()
- p, rr = r.copy(), float((r * r).sum())
- for _ in range(2 * int(inside.sum()) + 8):
- if math.sqrt(rr) <= MEMBRANE_TOL:
- break
- ap = apply(p)
- step = rr / float((p * ap).sum())
- u += step * p
- r -= step * ap
- rr, before = float((r * r).sum()), rr
- p = r + (rr / before) * p
- moves[way][box] = np.where(inside, u, 0.0)
- return kept, *moves
-
+ return sum(np.where(n, p[1 + oy:1 + oy + bh, 1 + ox:1 + ox + bw], 0.0) for n, (oy, ox) in zip(near, four)) / count
-def uncovered(sets: dict[str, np.ndarray], move_y: np.ndarray) -> np.ndarray:
- """The pixels of a region laid over the figure under it, and of its soft edge, that its lift may uncover (`layer_sets`):
- where it moves up, those whose own column, read as far under them as the move, reaches past its last row (into the
- rows under its lower edge, `floor`). There the layer is laid over that figure as it lies in the pixel's own column
- (`lay_over`)."""
- yy = np.mgrid[0:move_y.shape[0], 0:move_y.shape[1]][0]
- return (sets["region"] | sets["soft"]) & (move_y < 0) & (yy - move_y > sets["floor"][None, :] - 1)
+ for _ in range(MEMBRANE_PASSES):
+ nx, ny = np.where(inside, mean(bx), bx), np.where(inside, mean(by), by)
+ done = max(float(np.abs(nx - bx).max()), float(np.abs(ny - by).max())) < MEMBRANE_STEP
+ bx, by = nx, ny
+ if done:
+ break
+ ux, uy = ux.copy(), uy.copy()
+ ux[box], uy[box] = bx, by
+ return np.where(free, ux, start[0]), np.where(free, uy, start[1])
+
+
+def _spread(value: np.ndarray, start: np.ndarray, into: np.ndarray, steps: int) -> np.ndarray:
+ """`value` carried from the pixels `start` into those of `into`, nearest first: `steps` + 1 times over, each pixel of
+ `into` not yet reached beside one that is (8-neighbour) takes the mean of their values; one never reached keeps its
+ own."""
+ h, w = value.shape
+ out, got = value.copy(), start.copy()
+ for _ in range(steps + 1):
+ total, count = np.zeros((h, w)), np.zeros((h, w))
+ pv, pg = np.pad(out, 1), np.pad(got, 1)
+ for oy in (-1, 0, 1):
+ for ox in (-1, 0, 1):
+ if oy or ox:
+ near = pg[1 + oy:1 + oy + h, 1 + ox:1 + ox + w]
+ total += np.where(near, pv[1 + oy:1 + oy + h, 1 + ox:1 + ox + w], 0.0)
+ count += near
+ new = into & ~got & (count > 0)
+ if not new.any():
+ break
+ out, got = np.where(new, total / np.maximum(count, 1), out), got | new
+ return out
def lay_over(cell: Image.Image, drawn: Image.Image, field: tuple[np.ndarray, np.ndarray, np.ndarray],
sets: dict[str, np.ndarray]) -> tuple[Image.Image, np.ndarray, np.ndarray, np.ndarray]:
- """`drawn` — the region and its soft edge moved by `field` round the held pixels but the band (`draw_held`) — with the
- region laid over the band and the air (`--region-over`, `layer_sets`): the region's own pixels (it, its soft edge and
- its outline against the air, premultiplied) read at the move, over what they leave of the band's own pixel (the air,
- under the outline). Where the layer lands on a band pixel (alpha over 0), and on its outline, that pixel is the layer;
- where it does not, the band pixel keeps its bytes. Where it lands on the air (a drawn alpha of 1 or more), that pixel
- is the layer alone, and the air keeps its bytes where it does not: the air shows the region moved into it, never the
- figure beside it. Where the region lifts off the figure under it (`uncovered`), the layer is laid over that figure read
- in the pixel's own column as far under it as the move, never above its first row under the lower edge (the cell as cut
- under the lower edge, a piece `--hold-mask` leaves out too, drawn up, never moved sideways: the figure's side under the
- region stays where it is, and what the lift uncovers over it is as opaque as it is). Also the pixels of the band the
- layer lands on (`laid`), the band's it covers half or more (`over`, its outline not counted) and the region's pixels
- under which the figure shows half or more, by its alpha, where its layer does not (`lifted`)."""
+ """`drawn` — the cell moved by `field` round the held pixels but the band (`draw_held`: the air and where the region
+ lifts off the figure under it are read as the move reads them, the band under it showing there) — with the region laid
+ over the band (`--region-over`, `layer_sets`): the region's own pixels (it, its soft edge and its outline against the
+ air, premultiplied) read at the move, over what they leave of the band's own pixel (the air, under the outline). Where
+ the layer lands on a band pixel (alpha over 0), and on its outline, that pixel is the layer; where it does not, the
+ band pixel keeps its bytes. Also the pixels of the band the layer lands on (`laid`), the band's it covers half or more
+ (`over`, its outline not counted) and the region's pixels whose read falls under half on it and its soft edge and half
+ or more on the figure under its lower edge (`lifted`)."""
as_cut = np.asarray(cell.convert("RGBA"))
src = as_cut.astype(np.float64)
out = np.asarray(drawn.convert("RGBA")).astype(np.float64)
@@ -3028,7 +3123,7 @@ def lay_over(cell: Image.Image, drawn: Image.Image, field: tuple[np.ndarray, np.
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
_, move_x, move_y = field
sx, sy = xx - move_x, yy - move_y
- region, soft, band, edge, air = sets["region"], sets["soft"], sets["band"], sets["edge"], sets["air"]
+ region, soft, band, edge = sets["region"], sets["soft"], sets["band"], sets["edge"]
pm = src.copy()
pm[..., :3] *= pm[..., 3:4] / 255
layer = pm * (region | soft | edge)[..., None]
@@ -3038,29 +3133,12 @@ def lay_over(cell: Image.Image, drawn: Image.Image, field: tuple[np.ndarray, np.
both = read + (1 - read[..., 3:4] / 255) * pm * (~edge)[..., None]
alpha = np.clip(both[..., 3:4], 0, 255)
both[..., :3] = np.where(alpha > 0, both[..., :3] * 255 / np.maximum(alpha, 1e-3), 0)
- landed = air & (np.round(read[..., 3]) > 0)
- out[laid | landed] = both[laid | landed]
- # Uncovered by a lift, the figure under the region in the pixel's own column, read down as far as the move.
- shown = uncovered(sets, move_y)
- share = np.zeros((h, w))
- if shown.any():
- ys, xs = np.nonzero(shown)
- depth = np.maximum(ys - move_y[ys, xs].astype(np.float64), sets["floor"][xs]) # never above its first row
- row = np.clip(np.floor(depth).astype(int), 0, h - 2)
- fy = np.clip(depth - row, 0, 1)
- figure = pm * (~(region | soft))[..., None] # the cell as cut under the lower edge, a piece left out too
- below = (1 - fy)[:, None] * figure[row, xs] + fy[:, None] * figure[row + 1, xs]
- on = read[ys, xs] + (1 - read[ys, xs, 3:4] / 255) * below
- a = np.clip(on[:, 3:4], 0, 255)
- on[:, :3] = np.where(a > 0, on[:, :3] * 255 / np.maximum(a, 1e-3), 0)
- out[ys, xs] = on
- share[ys, xs] = below[:, 3] / 255
+ out[laid] = both[laid]
out = np.clip(np.round(out), 0, 255).astype(np.uint8)
- out[(laid | landed | shown) & (out[..., 3] == 0)] = 0 # clear, no colour: as `draw_held` writes a pixel it clears
- kept = (band & ~laid) | (air & ~landed)
+ kept = band & ~laid
out[kept] = as_cut[kept]
ours = np.clip(_bilinear((region | soft).astype(np.float64), sx, sy), 0, 1)
- lifted = region & shown & (ours < 0.5) & (share >= 0.5)
+ lifted = region & (ours < 0.5) & (_bilinear(sets["under"].astype(np.float64), sx, sy) >= 0.5)
return Image.fromarray(out, "RGBA"), laid, over, lifted
@@ -3181,8 +3259,7 @@ def _masked(answer: Answer, up: Banded | None, whole: tuple[np.ndarray, np.ndarr
def shaped(given: np.ndarray, cell: Image.Image) -> tuple[np.ndarray, np.ndarray, dict[str, Any] | None]:
trimmed, crease = crease_of(cell, given) if mask.crease else (given, None)
- worn = _shrink4(trimmed, mask.shrink)
- return trimmed, worn if crease is None else crease_kept(trimmed, worn, crease), crease
+ return trimmed, _shrink4(trimmed, mask.shrink), crease
if mask.per_cell:
made = [shaped(given, cell) for given, cell in zip(mask.masks, cells)]
@@ -3236,11 +3313,13 @@ def region_ledger(answer: Answer) -> dict[str, Any]:
pixels but the band), and where (weight, x, y; None where no read takes one), and `read_held_half_px`: reads that put
half or more on them;
- `renormalised_px`, `walked_px`: reads that took a held pixel and were read from the others, and those walked back
- (`draw_held`), which the rim keeps at 0.
+ (`draw_held`), which the rim keeps at 0 — laid over the figure under it, but in the air, which is read too: a read
+ there may take the held figure beside it past the band.
Laid over the figure under it (`--region-over`, `layer_sets`, `lay_over`), also:
- `band_px`, `over_px`, `edge_px`, `lifted_px`, `left_out_px`: the band under its lower edge, the band's pixels the
- layer covers half or more, the outline that goes with the layer, the region's pixels that show the figure under it
- where it lifts, and the mask's pixels `--hold-mask` leaves out; `cover_depth_px`: how deep the layer covers the band
+ layer covers half or more, the outline that goes with the layer, the region's pixels whose read falls on the figure
+ under it where it lifts (`lay_over`), and the mask's pixels `--hold-mask` names, which the region keeps;
+ `cover_depth_px`: how deep the layer covers the band
(`cover_depth`); the held pixels are those the drawn cell keeps, the band the layer does not land on among them;
- `edge_folded_px`: folds within a px (4-neighbour) of a held pixel, where the layer stops and the band keeps its
bytes — the layer's edge, not the move's, so they are counted apart and the rest is read as ever.
@@ -3446,14 +3525,18 @@ def sway_loop(loop_dir: Path, regions: list[tuple[float, ...]], *, gain: float |
region_mask: Path | None = None, region_rim: float | None = None, region_shrink: int | None = None,
region_crease: bool = False, region_over: bool = False, region_shear: float | None = None,
region_membrane: int | None = None, region_over_solid: bool = False, region_rim_round: bool = False,
+ region_membrane_unpin: bool = False, region_clear_alpha: int | None = None,
+ region_floor_beside: int | None = None, region_own_outline: int | None = None,
read_on_cut: Path | None = None, verb: str = VERB) -> dict[str, Any]:
request = read_request(loop_dir, regions, gain=gain, freq=freq, zeta=zeta, on_fold=on_fold, read_on=read_on,
stretch_floor=stretch_floor, breathe=breathe, lag=lag, gain_across=gain_across, band=band,
band_margin=band_margin, band_up=band_up, hold_mask=hold_mask, region_mask=region_mask,
region_rim=region_rim, region_shrink=region_shrink, region_crease=region_crease,
region_over=region_over, region_shear=region_shear, region_membrane=region_membrane,
- region_over_solid=region_over_solid, region_rim_round=region_rim_round, read_on_cut=read_on_cut,
- verb=verb)
+ region_over_solid=region_over_solid, region_rim_round=region_rim_round,
+ region_membrane_unpin=region_membrane_unpin, region_clear_alpha=region_clear_alpha,
+ region_floor_beside=region_floor_beside, region_own_outline=region_own_outline,
+ read_on_cut=read_on_cut, verb=verb)
loop_dir, name, meta_path, meta = request.loop_dir, request.name, request.meta_path, request.meta
strip_path = request.strip_path
source = request.source_path
@@ -3509,7 +3592,9 @@ def sway_frames(frames: Path, out: Path, regions: list[tuple[float, ...]], *, fp
hold_mask: Path | None = None, region_mask: Path | None = None, region_rim: float | None = None,
region_shrink: int | None = None, region_crease: bool = False, region_over: bool = False,
region_shear: float | None = None, region_membrane: int | None = None, region_over_solid: bool = False,
- region_rim_round: bool = False, read_on_cut: Path | None = None, verb: str = VERB) -> dict[str, Any]:
+ region_rim_round: bool = False, region_membrane_unpin: bool = False, region_clear_alpha: int | None = None,
+ region_floor_beside: int | None = None, region_own_outline: int | None = None,
+ read_on_cut: Path | None = None, verb: str = VERB) -> dict[str, Any]:
"""The sway of a folder of a cycle's frames — a loop's `cycle/`, or what `sprite-gen breathe` wrote from it — at their
own size and count, past the strip's caps on cells and width: the same frames under the same names, the regions
(in frame pixels) moved, and `sway.json` beside them, written whole into a new folder (`frames.folder`); the frames
@@ -3525,7 +3610,8 @@ def sway_frames(frames: Path, out: Path, regions: list[tuple[float, ...]], *, fp
hold_mask=hold_mask, region_mask=region_mask, region_rim=region_rim, region_shrink=region_shrink,
region_crease=region_crease, region_over=region_over, region_shear=region_shear,
region_membrane=region_membrane, region_over_solid=region_over_solid, region_rim_round=region_rim_round,
- verb=verb)
+ region_membrane_unpin=region_membrane_unpin, region_clear_alpha=region_clear_alpha,
+ region_floor_beside=region_floor_beside, region_own_outline=region_own_outline, verb=verb)
if ask.read_on is not None or read_on_cut is not None:
raise SystemExit(f"{verb}: --read-on names the strip as cut that the regions were read on; frames are read as they are")
if ask.breath is not None and fps is not None:
@@ -3683,15 +3769,18 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
help=f"with --region-mask: before it is worn in, end the mask at the crease under it — in each column whose "
f"lowest pixel lies on more of the figure, the darkest solid pixel {CREASE_UP} px above to {CREASE_DOWN} "
f"below it, the median over {2 * CREASE_SPAN + 1} columns; a column over the air is left as it is. "
- "--region-shrink then leaves that edge on the crease. Off by default")
+ "--region-shrink then wears it in; with --region-over the region reaches down to the crease again in "
+ "every cell. Off by default")
parser.add_argument("--region-over", action="store_true",
help="with --region-mask: lay the region over the figure under it — what lies under its lower edge (in its "
f"columns and the move's reach and {REGION_AIR} px past them) is no anchor of the rim, so the lower "
"edge moves with the rest, and the band of it within as far is drawn over by the region's own pixels "
- "by their alpha and keeps its bytes where they do not land; where the region lifts, its pixels show the "
- "figure under it. Its soft edge goes with it beside the figure too, and its outline against the air "
- "with the layer. --hold-mask names pixels it leaves out (an arm): never the region's, never drawn "
- "over, an anchor of the rim. Off by default")
+ "by their alpha and keeps its bytes where they do not land; the air and where the region lifts are "
+ "drawn by the move, the figure under it showing there. Its soft edge goes with it beside the figure "
+ "too, and its outline against the air with the layer; with --region-crease it reaches down to the "
+ "crease in every cell. --hold-mask names pixels it leaves out (an arm): where the mask does not cover "
+ "them never the region's, never drawn over, an anchor of the rim; where it does, they move with the "
+ "region. Off by default")
parser.add_argument("--region-over-solid", action="store_true",
help="with --region-over: of what lies under the region's lower edge only the solid pixels (alpha "
f"{ALPHA_SOLID} or more) are no anchor of the rim and drawn over; the rest of the figure there, not "
@@ -3713,6 +3802,22 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
"moves as a membrane, each pixel's move the mean of its 4 neighbours' that are the figure, between the "
"region's own and none on the rest of the figure, and is drawn by that move instead of laid over: the "
"figure under the region stretches with it instead of the region sliding over it. Off by default")
+ parser.add_argument("--region-membrane-unpin", action="store_true",
+ help="with --region-membrane: what the membrane moves is no anchor of the rim either, so the region's "
+ "move does not fall to none at its first rows under the lower edge. Nothing without a membrane. "
+ "Off by default")
+ parser.add_argument("--region-clear-alpha", type=int, default=None, metavar="A",
+ help=f"with --region-membrane: a pixel under alpha A (1 to {ALPHA_SOLID - 1}) is read as clear by what "
+ "the region moves and holds and by the membrane, its bytes kept: a speck of dust beside the figure "
+ "holds nothing. Nothing without a membrane. No default")
+ parser.add_argument("--region-floor-beside", type=int, default=None, metavar="N",
+ help="with --region-membrane: beside the region's columns its lower edge is the lowest pixel of its "
+ "columns within N px of that side, not that of its nearest column alone. Nothing without a membrane. "
+ "No default")
+ parser.add_argument("--region-own-outline", type=int, default=None, metavar="G",
+ help="with --region-membrane: the region grows G 4-neighbour steps over the cell's solid pixels within G "
+ "px of a clear one, above its lower edge's line and never what --hold-mask leaves out: the outline its "
+ "mask, carried into the cell, leaves out moves with it. Nothing without a membrane. No default")
parser.add_argument("--board", type=Path, help="write a before/after picture at the frames where the part sits lowest and highest")
@@ -3757,7 +3862,9 @@ def run(**kwargs: object) -> int:
"region_crease": bool(kwargs.get("region_crease")), "region_over": bool(kwargs.get("region_over")),
"region_shear": kwargs.get("region_shear"), "region_membrane": kwargs.get("region_membrane"),
"region_over_solid": bool(kwargs.get("region_over_solid")), "region_rim_round": bool(kwargs.get("region_rim_round")),
- "read_on_cut": kwargs.get("read_on_cut")}
+ "region_membrane_unpin": bool(kwargs.get("region_membrane_unpin")),
+ "region_clear_alpha": kwargs.get("region_clear_alpha"), "region_floor_beside": kwargs.get("region_floor_beside"),
+ "region_own_outline": kwargs.get("region_own_outline"), "read_on_cut": kwargs.get("read_on_cut")}
if kwargs.get("frames") is not None:
if kwargs.get("out") is None:
raise SystemExit(f"{verb}: --frames needs --out, a new folder for the swayed frames")
diff --git a/tests/video/test_follow_inspect.py b/tests/video/test_follow_inspect.py
index 0e4d5a9..b2576ae 100644
--- a/tests/video/test_follow_inspect.py
+++ b/tests/video/test_follow_inspect.py
@@ -457,11 +457,12 @@ def test_the_command_takes_what_video_follow_takes(tmp_path, capsys):
# region's own pixels the inspection does not read, the pixels a band leaves as they are (`--hold-mask`), and the
# region given as a mask (`--region-mask`), laid over the figure under it (`--region-over`, its solid pixels alone
# with `--region-over-solid`, its rim round beside it with `--region-rim-round`) with a membrane under it
- # (`--region-membrane`) and its rim widened to the shear (`--region-shear`), and the record of another cut of the clip
- # the regions were read on (`--read-on-cut`)
+ # (`--region-membrane`, with its four keys) and its rim widened to the shear (`--region-shear`), and the record of
+ # another cut of the clip the regions were read on (`--read-on-cut`)
own = {'board', 'frames', 'out', 'fps', 'reference', 'band', 'band_margin', 'band_up', 'hold_mask', 'region_mask',
'region_rim', 'region_shrink', 'region_crease', 'region_over', 'region_over_solid', 'region_rim_round',
- 'region_shear', 'region_membrane', 'read_on_cut'}
+ 'region_shear', 'region_membrane', 'region_membrane_unpin', 'region_clear_alpha', 'region_floor_beside',
+ 'region_own_outline', 'read_on_cut'}
assert set(inspected)-set(followed) == {'out_dir', 'scale', 'columns'} and set(followed)-set(inspected) == own
assert {key: inspected[key] for key in followed if key not in own} == {key: value for key, value in followed.items() if key not in own}
capsys.readouterr()
diff --git a/tests/video/test_sway_region.py b/tests/video/test_sway_region.py
index 31121a6..389b89b 100644
--- a/tests/video/test_sway_region.py
+++ b/tests/video/test_sway_region.py
@@ -437,7 +437,7 @@ def test_the_crease_ends_the_mask_on_the_darkest_row_under_it_before_it_is_worn_
over_air = [x for x in range(W) if part0[:, x].any() and x > 85]
assert record['region_mask']['crease'] == [{'rows': {str(x): BOTTOM+1 for x in over_torso}, 'free_columns': len(over_air)}]
worn = sway.sway_loop(strip_dir(tmp_path/'worn', part_figure), [PART], region_mask=mask, region_crease=True)
- assert worn['region_mask']['mask']['box'][3] == BOTTOM + 1 # the crease: the wear leaves it where it is
+ assert worn['region_mask']['mask']['box'][3] == BOTTOM + 1 - 2 # the crease, then worn in by 2
assert worn['region_mask']['mask']['carried_by_px'] == int(part0.sum()) + len(over_torso)
diff --git a/tests/video/test_sway_region_over.py b/tests/video/test_sway_region_over.py
index cb98754..2a61cbe 100644
--- a/tests/video/test_sway_region_over.py
+++ b/tests/video/test_sway_region_over.py
@@ -1,21 +1,29 @@
-"""`video-sway --region-mask` with `--region-over`, `--region-over-solid`, `--region-rim-round`, `--region-membrane`,
-`--region-shear`, and the crease the wear leaves where it is.
+"""`video-sway --region-mask` with `--region-over`, `--region-over-solid`, `--region-rim-round`, `--region-membrane` and
+its four keys, `--region-shear`, and the crease the wear wears in and the layer reaches down to again.
`--region-over` lays the region over the figure under it: what lies under its lower edge is no anchor of the rim, so the
lower edge moves with the rest; the band of it within the air's reach is drawn over by the region's own pixels by their
-alpha and keeps its bytes where they do not land; where the region lifts, its pixels show the figure under it; its
-outline against the air goes with it and leaves the air behind, its outline against the figure stays; `--hold-mask` names
-the pixels it leaves out (an arm), never the region's, never drawn over. `--region-over-solid` lays it over the solid
-pixels under its lower edge alone: the rest there stays and holds the rim, and its soft edge goes with it beside the figure
-under it too. `--region-rim-round` holds the rim round beside it, so below the corner where the figure beside it stops over
-what lies under its lower edge the move rises down as gently as across. `--region-membrane N` moves what lies under its
-lower edge within N + 1 px of it, none of what it leaves out, as a membrane between the region's move and none on the rest
-of the figure, drawn by that move instead of laid over. `--region-shear S` widens the rim so a move one way rises across it
-the other way by at most S px a px. With either, the ledger reads the steps the drawn move takes, the strip a region drawn
-short of its side leaves standing, and its tip. `--region-crease` with `--region-shrink`: the wear
-leaves the lower edge on the crease. Synthetic figures only: the region test's body and its striped part over the torso's
-right side and past it over the air, a dark crease under it on the torso; an arm block beside the part's lower left; a dark
-outline under the part over the air; a piece of the arm's colour right under the part's lower edge.
+alpha and keeps its bytes where they do not land; the air and where the region lifts are drawn by the move, the figure
+under it read up there; its outline against the air goes with it and leaves the air behind, its outline against the
+figure stays; `--hold-mask` names the pixels it leaves out (an arm): those the region mask covers stay the region's and
+move with it, the rest are never moved and never drawn over (on a band, every one of them is held). `--region-over-solid`
+lays it over the solid pixels under its lower edge alone: the rest there stays and holds the rim, and its soft edge goes
+with it beside the figure under it too. `--region-rim-round` holds the rim round beside it, so below the corner where the
+figure beside it stops over what lies under its lower edge the move rises down as gently as across. `--region-membrane N`
+moves what lies under its lower edge within N + 1 px of it, none of what it leaves out, as a membrane between the
+region's move and none on the rest of the figure (each pixel the mean of its figure neighbours' to within a pass's
+MEMBRANE_STEP), drawn by that move instead of laid over; with it, `--region-membrane-unpin` makes what the membrane moves
+no anchor of the rim, `--region-clear-alpha A` reads a pixel under alpha A as clear, `--region-floor-beside N` sets the
+lower edge beside the region by its lowest pixel within N columns of each side, and `--region-own-outline G` grows the
+region G px over the outline its mask leaves out; without it they shape nothing. `--region-shear S` widens the rim so a
+move one way rises across it the other way by at most S px a px. With either, the ledger reads the steps the drawn move
+takes, the strip a region drawn short of its side leaves standing, and its tip. `--region-crease` with `--region-shrink`:
+the wear wears the crease in with the rest of the mask, and laid over the figure the region reaches down to the crease
+again in every cell. Synthetic figures only: the region test's body and its striped part over the torso's right side and
+past it over the air, a dark crease under it on the torso; an arm block beside the part's lower left; a dark outline
+under the part over the air; a piece of the arm's colour right under the part's lower edge, or inside the part; a few
+pixels not opaque and a near-clear speck under the part's lower edge over the air; a pixel or two of the part's outline
+over the air past its mask, in a row that changes from cell to cell.
Each case first reads what a sway writes with the new keys (its cells, its record), so on a release without them the
same input fails on what comes out.
@@ -111,6 +119,20 @@ def share_of(answer, k, where):
return float(np.hypot(field[1], field[2])[where].mean()/offset)
+def reads_wholly_on(field, where, any_tap=False):
+ """The pixels whose read at the move (`field`: covered, across, down) takes its four taps on `where` (with `any_tap`,
+ one at least of those it weighs)."""
+ yy, xx = np.mgrid[0:H, 0:W]
+ rx, ry = xx-field[1], yy-field[2]
+ x0, y0 = np.clip(np.floor(rx).astype(int), 0, W-2), np.clip(np.floor(ry).astype(int), 0, H-2)
+ fx, fy = rx-np.floor(rx), ry-np.floor(ry)
+ taps = [(where[y0+oy, x0+ox], weight > 0) for oy, ox, weight in
+ ((0, 0, (1-fx)*(1-fy)), (0, 1, fx*(1-fy)), (1, 0, (1-fx)*fy), (1, 1, fx*fy))]
+ if any_tap:
+ return np.logical_or.reduce([on & weighed for on, weighed in taps])
+ return np.logical_and.reduce([on for on, _ in taps])
+
+
RISE = 8 # rows over the lower rows the part's own move is read for them to be set against
@@ -138,7 +160,7 @@ def test_without_the_new_keys_nothing_of_them_is_read_or_written(tmp_path):
# ---------------------------------------------------------------- the crease the wear leaves where it is
-def test_the_wear_leaves_the_lower_edge_on_the_crease_and_wears_in_the_rest(tmp_path):
+def test_the_wear_wears_the_crease_in_and_laid_over_the_region_reaches_down_to_it(tmp_path):
mask = mask_png(tmp_path/'mask.png', part_at(0))
loop_dir = strip_dir(tmp_path/'loop', part_figure)
record = sway.sway_loop(loop_dir, [PART], region_mask=mask, region_crease=True)
@@ -148,11 +170,11 @@ def test_the_wear_leaves_the_lower_edge_on_the_crease_and_wears_in_the_rest(tmp_
over_air = [x for x in range(W) if part[:, x].any() and x > 85]
lowest = {x: int(np.nonzero(worn[:, x])[0].max()) for x in range(W) if worn[:, x].any()}
kept = [x for x in on_torso if x in lowest]
- assert kept and all(lowest[x] == BOTTOM+1 for x in kept) # on the crease, as crease_of ended it
+ assert kept and all(lowest[x] == BOTTOM+1-2 for x in kept) # the crease crease_of ended it on, worn in by 2
assert all(lowest[x] == BOTTOM-2 for x in over_air if x in lowest) # over the air worn in by 2 as ever
xs = sorted(lowest)
assert xs[0] == min(on_torso)+2 and xs[-1] == max(over_air)-2 # the sides worn in by 2 as ever
- assert record['region_mask']['mask']['box'][3] == BOTTOM+1 and record['region_mask']['crease'][0]['rows'] == \
+ assert record['region_mask']['mask']['box'][3] == BOTTOM+1-2 and record['region_mask']['crease'][0]['rows'] == \
{str(x): BOTTOM+1 for x in on_torso}
# Nothing under the crease moves: the torso keeps its bytes there in every cell.
source, moved = cells_of(strip_dir(tmp_path/'source', part_figure)), cells_of(loop_dir)
@@ -160,6 +182,14 @@ def test_the_wear_leaves_the_lower_edge_on_the_crease_and_wears_in_the_rest(tmp_
for k in range(N):
under = (source[k][..., 3] > 0) & (yy > BOTTOM+1+body_sway(k)[1]) & ~part_at(k)
assert np.array_equal(moved[k][under], source[k][under]), k
+ # Laid over the figure, the region reaches down to the crease carried into each cell again: in every column of the
+ # crease the worn region keeps, its lowest pixel is the crease's row.
+ over = answer_of(strip_dir(tmp_path/'over', part_figure), [PART], region_mask=mask, region_crease=True, region_over=True)
+ for k in range(N):
+ sx, sy = body_sway(k)
+ region = over.mask_field(k, *np.mgrid[0:H, 0:W][::-1].astype(np.float32))[1]['region']
+ assert all(int(np.nonzero(region[:, x+sx])[0].max()) == BOTTOM+1+sy for x in kept), k
+ assert all(int(np.nonzero(region[:, x+sx])[0].max()) == BOTTOM-2+sy for x in over_air if x in lowest), k
# ---------------------------------------------------------------- the layer
@@ -200,13 +230,19 @@ def test_the_band_is_drawn_over_where_the_layer_lands_and_keeps_its_bytes_elsewh
answer = answer_of(strip_dir(tmp_path/'again', part_figure), [PART], **kw)
source, moved = cells_of(strip_dir(tmp_path/'source', part_figure)), cells_of(loop_dir)
air = math.ceil(answer.reach)+2
- covered = 0
+ covered = aired = 0
+ walked = []
for k in range(N):
drawn, read = answer.masked_cell(k)
assert np.array_equal(np.asarray(drawn), moved[k]), k
band, laid = read['band'], read['laid']
- # the layer draws the band and the air; no read of the move's is taken there
- assert not (read['readers'] & (band | read['air'])).any(), k
+ # the layer draws the band, no read of the move's taken there; the move draws the air
+ assert not (read['readers'] & band).any(), k
+ aired += int((read['readers'] & read['air']).sum())
+ # a read of the air's that takes the held figure beside it (the torso's soft side past the band) is walked back,
+ # as `draw_held` walks any: in the air alone, never on the band
+ assert not (read['walked'] & ~read['air']).any() and not ((read['walked'] | read['renormalised']) & band).any(), k
+ walked.append(int(read['walked'].sum()))
changed = np.any(moved[k] != source[k], axis=-1)
visible = source[k][..., 3] > 0
# Outside the region, its soft edge, the air and the band nothing changes; in the band only where the layer lands.
@@ -220,10 +256,11 @@ def test_the_band_is_drawn_over_where_the_layer_lands_and_keeps_its_bytes_elsewh
assert len(ys) and not (band & ~visible).any(), k
assert np.nonzero(band & (source[k][..., 3] >= 128))[0].min() >= BOTTOM+2+body_sway(k)[1], k
assert ys.min() >= BOTTOM+1+body_sway(k)[1] and ys.max() <= BOTTOM+1+body_sway(k)[1]+sway.REGION_SOFT+air, k
- assert covered > 0
+ assert covered > 0 and aired > 0
ledger = record['region_mask']['ledger']
assert ledger['cover_depth_px_max'] <= air and ledger['over_px_max'] > 0 and ledger['held_changed_px_max'] == 0
- assert ledger['folded_px_max'] == 0 and min(ledger['jacobian_min']) > 0 and ledger['walked_px_max'] == 0
+ assert ledger['folded_px_max'] == 0 and min(ledger['jacobian_min']) > 0
+ assert ledger['walked_px'] == walked == [0]*9 + [4, 9, 6] + [0]*12
def test_where_the_part_lifts_it_shows_the_figure_under_it(tmp_path):
@@ -233,36 +270,45 @@ def test_where_the_part_lifts_it_shows_the_figure_under_it(tmp_path):
record = sway.sway_loop(loop_dir, [PART], **kw)
answer = answer_of(strip_dir(tmp_path/'again', part_figure), [PART], **kw)
source, moved = cells_of(strip_dir(tmp_path/'source', part_figure)), cells_of(loop_dir)
+ yy, xx = np.mgrid[0:H, 0:W]
cells = lifting(answer)
assert cells
- shown = 0
+ shown, thin, notched = 0, {}, []
for k in cells:
_, read = answer.masked_cell(k)
lifted = read['lifted']
assert lifted.any(), k
sx, sy = body_sway(k)
- side = np.mgrid[0:H, 0:W][1] == 86+sx # the torso's soft side
- # what the lift uncovers over an opaque figure stays opaque, the row read between the part's last row and the
- # figure's first too: the figure is drawn up from its first row under the lower edge, never from the part's
- floor = read['floor']
- under_opaque = source[k][np.minimum(floor, H-1), np.arange(W), 3] == 255
- opaque = sway.uncovered(read, read['field'][2]) & under_opaque[None, :] & (source[k][..., 3] == 255)
+ side = xx == 86+sx # the torso's soft side
+ # what the lift uncovers is read at its whole move as any read (`draw_held`), none of it short: its alpha is the
+ # source's read there
+ alpha = np.clip(np.round(sway._bilinear(source[k][..., 3].astype(np.float64), xx-read['read_x'], yy-read['read_y'])),
+ 0, 255)
+ assert not (lifted & (read['renormalised'] | read['walked'])).any(), k
+ assert np.array_equal(moved[k][lifted][:, 3], alpha[lifted]), k
+ # where that read lies wholly on opaque pixels, it stays opaque; the torso's and the crease's red, none of the
+ # stripes' (200 or more)
+ opaque = lifted & reads_wholly_on(read['field'], source[k][..., 3] == 255)
assert opaque.any(), k
assert (moved[k][opaque][:, 3] == 255).all(), (k, sorted(set(moved[k][opaque][:, 3].tolist())))
- # the torso and the crease read up into the part's lower rows, each in its own column: no hole (the torso's soft
- # side shows as soft as it is), the torso's and the crease's red, none of the stripes' (200 or more)
- assert (moved[k][lifted & ~side][:, 3] >= 128).all(), k
- assert (moved[k][lifted & side][:, 3] >= SOFT-1).all(), k
assert float(moved[k][lifted][:, 0].mean()) < 100, k
- # the torso's soft side under the part stays where it is as the part lifts off it, no notch: in the rows the part
- # leaves, it is as soft as it was under them
+ # beside the torso's soft side the read takes that side and the air past it: one column of the part's, within
+ # 2 px of that side, as soft as that read
+ under = lifted & ~side & (moved[k][..., 3] < 128)
+ assert len(set(xx[under].tolist())) <= 1 and (abs(xx[under]-(86+sx)) <= 2).all(), k
+ thin[k] = int(under.sum())
+ # in the rows the part leaves, its column over the torso's soft side reads at the move too: where the part moves
+ # toward the torso that read takes the air past the side, and the side shows thinner than it was
rows = slice(BOTTOM+1+sy+math.floor(answer.dy[k]), BOTTOM+1+sy)
- assert (moved[k][rows, 86+sx, 3] >= SOFT-1).all(), k
+ if not (moved[k][rows, 86+sx, 3] >= SOFT-1).all():
+ notched.append(k)
shown += int(lifted.sum())
+ assert thin == {0: 7, 1: 0, 2: 0, 9: 2, 10: 4, 11: 6, 12: 0, 13: 0, 14: 0, 21: 3, 22: 5, 23: 7}
+ assert notched == [k for k in cells if answer.dx[k] < 0] == [0, 1, 13, 14, 21, 22, 23]
assert shown > 0 and record['region_mask']['ledger']['lifted_px_max'] > 0
-def test_where_the_part_lifts_off_a_piece_left_out_it_shows_that_piece(tmp_path):
+def test_where_the_part_lifts_off_a_piece_left_out_no_hole_opens_and_the_piece_keeps_its_bytes(tmp_path):
mask = mask_png(tmp_path/'mask.png', part_at(0))
kw = dict(region_mask=mask, region_shrink=0, region_over=True, hold_mask=arm_masks(tmp_path/'piece', under_at))
loop_dir = strip_dir(tmp_path/'loop', under_figure)
@@ -271,16 +317,18 @@ def test_where_the_part_lifts_off_a_piece_left_out_it_shows_that_piece(tmp_path)
source, moved = cells_of(strip_dir(tmp_path/'source', under_figure)), cells_of(loop_dir)
cells = lifting(answer)
assert cells
+ took_px = 0
for k in cells:
_, read = answer.masked_cell(k)
piece = under_at(k)
- # the piece left out under the lower edge is the figure under it there: what the lift uncovers over it stays
- # opaque, the piece drawn up, and the piece keeps its bytes
- over_piece = piece[np.minimum(read['floor'], H-1), np.arange(W)]
- opaque = sway.uncovered(read, read['field'][2]) & over_piece[None, :] & (source[k][..., 3] == 255)
- assert opaque.any(), k
- assert (moved[k][opaque][:, 3] == 255).all(), (k, sorted(set(moved[k][opaque][:, 3].tolist())))
+ # the piece left out under the lower edge is held: a read of the part's that would take it is read from its other
+ # pixels (`draw_held`), none of them where the part lifts, so no hole opens; the piece keeps its bytes
+ took = read['field'][0] & reads_wholly_on(read['field'], piece, any_tap=True) & ~piece
+ assert not (took & read['lifted']).any() and not (took & ~read['renormalised']).any(), k
+ assert (moved[k][took][:, 3] == 255).all(), (k, sorted(set(moved[k][took][:, 3].tolist())))
assert np.array_equal(moved[k][piece], source[k][piece]), k
+ took_px += int(took.sum())
+ assert took_px == 10*len(cells)
def test_its_outline_against_the_air_goes_with_it_and_against_the_figure_stays(tmp_path):
@@ -315,16 +363,50 @@ def test_its_outline_against_the_air_goes_with_it_and_against_the_figure_stays(t
assert np.array_equal(moved[k][crease], source[k][crease]), k
-def test_the_hold_mask_leaves_the_arm_out_of_the_region_and_the_band(tmp_path):
- over_arm = part_at(0) | (arm_at(0) & (np.mgrid[0:H, 0:W][0] >= 86)) # the mask runs onto the arm's lower rows
- mask = mask_png(tmp_path/'mask.png', over_arm)
- source = cells_of(strip_dir(tmp_path/'source', arm_figure))
- for name, hold in (('folder', arm_masks(tmp_path/'arms')), ('one', mask_png(tmp_path/'arm.png', arm_at(0)))):
- loop_dir = strip_dir(tmp_path/name, arm_figure)
- record = sway.sway_loop(loop_dir, [PART], region_mask=mask, region_shrink=0, region_over=True, hold_mask=hold)
+INSIDE_BOX = (58, 82, 66, 90) # x0, y0, x1, y1 in cell 0, last included: a piece of the arm's colour inside the part
+
+
+def inside_at(k):
+ """A piece of the arm's colour inside the part's lower half in cell k, carried with the body."""
+ sx, sy = body_sway(k)
+ yy, xx = np.mgrid[0:H, 0:W]
+ x0, y0, x1, y1 = INSIDE_BOX
+ return (xx >= x0+sx) & (xx <= x1+sx) & (yy >= y0+sy) & (yy <= y1+sy)
+
+
+def inside_figure(k, n=N):
+ """The arm figure with a piece of the arm's colour inside the part too."""
+ a = np.asarray(arm_figure(k, n)).copy()
+ a[inside_at(k)] = ARM
+ return Image.fromarray(a, 'RGBA')
+
+
+def test_laid_over_the_figure_the_pixels_the_hold_mask_names_under_the_region_mask_move_with_it(tmp_path):
+ mask = mask_png(tmp_path/'mask.png', part_at(0))
+ source = cells_of(strip_dir(tmp_path/'source', inside_figure))
+ both = arm_masks(tmp_path/'both', lambda k: inside_at(k) | arm_at(k))
+ for name, hold in (('folder', both), ('one', mask_png(tmp_path/'both.png', inside_at(0) | arm_at(0)))):
+ kw = dict(region_mask=mask, region_shrink=0, region_over=True, hold_mask=hold)
+ loop_dir = strip_dir(tmp_path/name, inside_figure)
+ record = sway.sway_loop(loop_dir, [PART], **kw)
+ answer = answer_of(strip_dir(tmp_path/f'{name}-again', inside_figure), [PART], **kw)
moved = cells_of(loop_dir)
- for k in range(N): # the arm keeps its bytes in every cell
- assert np.array_equal(moved[k][arm_at(k)], source[k][arm_at(k)]), (name, k)
+ carried = 0
+ for k in range(N):
+ drawn, read = answer.masked_cell(k)
+ assert np.array_equal(np.asarray(drawn), moved[k]), (name, k)
+ piece = inside_at(k)
+ # The piece the region mask covers is the region's: no anchor of the rim, never left out of it.
+ assert np.array_equal(read['region'] & piece, piece) and np.array_equal(read['left_out'] & piece, piece), (name, k)
+ assert not (read['anchors'] & piece).any(), (name, k)
+ if read['field'] is None:
+ continue
+ # It moves with the part: each pixel of the part whose read takes the piece wholly is the piece's colour, and
+ # some of them lie where it was not.
+ on_piece = read['region'] & reads_wholly_on(read['field'], piece)
+ assert (moved[k][on_piece] == ARM).all(), (name, k)
+ carried += int((on_piece & ~piece).sum())
+ assert carried > 0, name
masked = record['region_mask']
assert masked['ledger']['left_out_px_max'] > 0 and masked['asked']['hold']['per_cell'] == (name == 'folder')
left_out = masked['over']['left_out']
@@ -332,12 +414,30 @@ def test_the_hold_mask_leaves_the_arm_out_of_the_region_and_the_band(tmp_path):
assert left_out['carry'] is None and left_out['grow_px'] is None
else:
assert left_out['grow_px'] == 3 and len(left_out['carry']['cells']) == N and left_out['lay'] == []
- assert left_out['pieces'] == [{'px': int(arm_at(0).sum()), 'box': [42, 78, 49, 104]}]
- # Without it the arm's lower rows are the region's, and under its lower edge the band: they move.
- loop_dir = strip_dir(tmp_path/'no-hold', arm_figure)
- sway.sway_loop(loop_dir, [PART], region_mask=mask, region_shrink=0, region_over=True)
+ assert sorted(p['px'] for p in left_out['pieces']) == sorted([int(inside_at(0).sum()), int(arm_at(0).sum())])
+
+
+def test_the_pixels_the_hold_mask_names_outside_the_region_mask_and_on_a_band_keep_their_bytes(tmp_path):
+ over_arm = part_at(0) | (arm_at(0) & (np.mgrid[0:H, 0:W][0] >= 86)) # the mask runs onto the arm's lower rows
+ mask = mask_png(tmp_path/'mask.png', over_arm)
+ source = cells_of(strip_dir(tmp_path/'source', inside_figure))
+ yy = np.mgrid[0:H, 0:W][0]
+ loop_dir = strip_dir(tmp_path/'over', inside_figure)
+ sway.sway_loop(loop_dir, [PART], region_mask=mask, region_shrink=0, region_over=True, hold_mask=arm_masks(tmp_path/'arms'))
moved = cells_of(loop_dir)
- assert any(not np.array_equal(moved[k][arm_at(k)], source[k][arm_at(k)]) for k in range(N))
+ for k in range(N): # laid over the figure: the arm's rows the mask does not cover keep their bytes in every cell
+ out = arm_at(k) & (yy < 86+body_sway(k)[1])
+ assert np.array_equal(moved[k][out], source[k][out]), k
+ # On a band (no region mask, so none laid over) every pixel the hold mask names keeps its bytes, the piece inside the
+ # part too, while the part moves round them.
+ both = arm_masks(tmp_path/'both', lambda k: inside_at(k) | arm_at(k))
+ band_dir = strip_dir(tmp_path/'band', inside_figure)
+ sway.sway_loop(band_dir, [PART], band='auto', hold_mask=both)
+ banded = cells_of(band_dir)
+ for k in range(N):
+ held = inside_at(k) | arm_at(k)
+ assert np.array_equal(banded[k][held], source[k][held]), k
+ assert any(np.any(banded[k][part_at(k) & ~inside_at(k)] != source[k][part_at(k) & ~inside_at(k)]) for k in range(N))
# ---------------------------------------------------------------- the shear
@@ -413,9 +513,10 @@ def test_laid_over_the_solid_pixels_alone_the_rest_under_it_stays_and_its_soft_e
GENTLE = 0.9 # the gain of a move of about 2.2 px, the size a membrane a few px long is for: the default's 6.2 px folds it
-def figure_mean_gap(read, alpha):
- """How far each membrane pixel's move is from the mean of its figure 4-neighbours' (the region's own move on it, its
- soft edge and its outline over the band; none on the rest of the figure), the most of it, across and down."""
+def membrane_pass(read, alpha):
+ """How far one more of the membrane's passes would move its pixels, the most of it, across and down: each pixel's
+ move set against the mean of its figure 4-neighbours' (the region's own move on it, its soft edge and its outline over
+ the band; none on the rest of the figure)."""
free, field = read['membrane'], read['field']
own = read['region'] | read['soft'] | read['edge']
figure = alpha > 0
@@ -425,7 +526,7 @@ def figure_mean_gap(read, alpha):
pu, pf = np.pad(u, 1), np.pad(figure, 1)
total = sum(pf[1+oy:1+oy+H, 1+ox:1+ox+W]*pu[1+oy:1+oy+H, 1+ox:1+ox+W] for oy, ox in ((-1, 0), (1, 0), (0, -1), (0, 1)))
count = sum(pf[1+oy:1+oy+H, 1+ox:1+ox+W].astype(float) for oy, ox in ((-1, 0), (1, 0), (0, -1), (0, 1)))
- gaps.append(float(np.abs(count*u-total)[free].max()))
+ gaps.append(float(np.abs(u-total/np.maximum(count, 1))[free].max()))
return max(gaps)
@@ -466,8 +567,9 @@ def test_the_membrane_under_the_lower_edge_hangs_between_the_part_and_the_rest(t
assert free.any() and not (free & (yy <= BOTTOM+sy)).any() and not (free & read['edge']).any(), k
assert not (free & (sway.rim_distance(read['region'] | read['soft'], reach+1, reach+1) >= 1)).any(), k
assert (free & (source[k][..., 3] > 0)).sum() == free.sum(), k
- # Each pixel's move the mean of its figure 4-neighbours', the part's own and none on the rest of the figure.
- assert figure_mean_gap(read, source[k][..., 3]) <= 1e-6, k
+ # Each pixel's move the mean of its figure 4-neighbours', the part's own and none on the rest of the figure, to
+ # within what one more pass would move it: less than the step the passes stop at.
+ assert membrane_pass(read, source[k][..., 3]) < sway.MEMBRANE_STEP, k
if math.hypot(answer.dx[k], answer.dy[k]) < 0.5:
continue
# The figure under the part stretches with it: the move steps down from the part's last row to the row under it
@@ -482,7 +584,8 @@ def test_the_membrane_under_the_lower_edge_hangs_between_the_part_and_the_rest(t
assert read_cells > 0
masked = record['region_mask']
assert masked['asked']['membrane_px'] == reach
- assert masked['over']['membrane'] == {'policy': sway.MEMBRANE_POLICY, 'reach_px': reach+1, 'tol_px': sway.MEMBRANE_TOL}
+ assert masked['over']['membrane'] == {'policy': sway.MEMBRANE_POLICY, 'reach_px': reach+1, 'step_px': sway.MEMBRANE_STEP,
+ 'passes_most': sway.MEMBRANE_PASSES}
ledger = masked['ledger']
assert len(ledger['membrane_px']) == N and max(ledger['membrane_px']) > 0
most = [entry for entry in ledger['membrane_most'] if entry is not None]
@@ -515,8 +618,15 @@ def test_the_layer_without_the_membrane_is_as_it_was(tmp_path):
off = sway.sway_loop(asked_off, [PART], region_membrane=None, **kw)
assert (as_ever/'walk.strip.png').read_bytes() == (asked_off/'walk.strip.png').read_bytes()
assert off['region_mask'] == record['region_mask']
+ # The membrane's four keys shape nothing without it: the same bytes and the same record, none of them named.
+ keyed_dir = strip_dir(tmp_path/'keyed', arm_figure)
+ keyed = sway.sway_loop(keyed_dir, [PART], region_membrane_unpin=True, region_clear_alpha=8, region_floor_beside=12,
+ region_own_outline=2, **kw)
+ assert (as_ever/'walk.strip.png').read_bytes() == (keyed_dir/'walk.strip.png').read_bytes()
+ assert keyed['region_mask'] == record['region_mask']
masked = record['region_mask']
assert 'membrane_px' not in masked['asked'] and 'membrane' not in masked['over'] and 'solid' not in masked['over']
+ assert not {'membrane_unpin', 'clear_alpha', 'floor_beside_px', 'own_outline_px'} & set(masked['asked'])
assert not {'membrane_px', 'membrane_most', 'membrane_folded_px'} & set(masked['ledger'])
_, read = answer_of(strip_dir(tmp_path/'again', arm_figure), [PART], **kw).masked_cell(0)
assert 'membrane' not in read
@@ -536,6 +646,157 @@ def test_a_membrane_too_short_for_the_move_is_refused_by_its_name(tmp_path):
assert ledger['membrane_folded_px'] == [0]*N and ledger['membrane_folded_px_max'] == 0
+# ---------------------------------------------------------------- the membrane's keys
+
+HAZE_COLUMNS = range(88, 92) # in cell 0: under the part's lower edge where it hangs over the air
+SPECK = (90, BOTTOM+6) # x, y in cell 0: a near-clear speck under it
+
+
+def hem_figure(k, n=N):
+ """The part over the air with a few pixels not opaque right under its lower edge there, and a near-clear speck."""
+ sx, sy = body_sway(k)
+ a = np.asarray(part_figure(k, n)).copy()
+ for x in HAZE_COLUMNS: # past the part's soft edge (REGION_SOFT px), within the membrane's reach
+ a[BOTTOM+3+sy, x+sx] = (*OUTLINE[:3], 60)
+ a[BOTTOM+4+sy, x+sx] = (*OUTLINE[:3], 30)
+ a[SPECK[1]+sy, SPECK[0]+sx] = (*OUTLINE[:3], 3)
+ return Image.fromarray(a, 'RGBA')
+
+
+BUMP_ROWS = (79, 83, 87, 81, 85, 89) # in cell 0: the row a pixel of the part's outline over the air stands past its mask
+
+
+def bump_at(k):
+ """The outline's pixels past the part's mask in cell k: one beside its side over the air, two in the odd cells, in a
+ row that changes from cell to cell."""
+ sx, sy = body_sway(k)
+ row = BUMP_ROWS[k % len(BUMP_ROWS)]
+ out = np.zeros((H, W), bool)
+ out[row+sy:row+sy+1+k % 2, 93+sx] = True
+ return out
+
+
+def bump_figure(k, n=N):
+ a = np.asarray(part_figure(k, n)).copy()
+ a[bump_at(k)] = OUTLINE
+ return Image.fromarray(a, 'RGBA')
+
+
+def last_crossing(alpha):
+ """Where `alpha` (0 to 1 along a line) last falls through 0.5, subpixel; None where it never does."""
+ falls = np.nonzero((alpha[:-1] >= 0.5) & (alpha[1:] < 0.5))[0]
+ if not len(falls):
+ return None
+ i = int(falls[-1])
+ return i+float((alpha[i]-0.5)/(alpha[i]-alpha[i+1]))
+
+
+def edge_lags(drawn, cut, move, columns):
+ """Per column, the lower edge's row drawn (where its alpha last falls through 0.5 going down) less the same row of the
+ cut moved whole by `move` (its alpha read bilinear at x - dx, y - dy)."""
+ alpha, rows = cut[..., 3]/255, np.arange(H, dtype=float)
+ return [last_crossing(drawn[:, x, 3]/255)-last_crossing(sway._bilinear(alpha, np.full(H, x-move[0]), rows-move[1]))
+ for x in columns]
+
+
+def side_lags(drawn, cut, moves):
+ """Per row, the side's column drawn (where its alpha last falls through 0.5 going right) less the same row's of the cut
+ moved whole by the move that row is meant to take (`moves`: row -> (dx, dy))."""
+ alpha, columns = cut[..., 3]/255, np.arange(W, dtype=float)
+ return {y: last_crossing(drawn[y, :, 3]/255)-last_crossing(sway._bilinear(alpha, columns-mx, np.full(W, y-my)))
+ for y, (mx, my) in moves.items()}
+
+
+def hem_lags(tmp_path, name, **kw):
+ """The most |lag| of the lower edge over the air (`edge_lags`, the part's own move) per cell that moves 0.5 px or more
+ down or up, a hem figure swayed as asked."""
+ loop_dir = strip_dir(tmp_path/name, hem_figure)
+ record = sway.sway_loop(loop_dir, [PART], **kw)
+ source, moved = cells_of(strip_dir(tmp_path/f'{name}-source', hem_figure)), cells_of(loop_dir)
+ lags = {k: max(abs(v) for v in edge_lags(moved[k], source[k], (record['dx_px'][k], record['dy_px'][k]),
+ [x+body_sway(k)[0] for x in HAZE_COLUMNS]))
+ for k in range(N) if abs(record['dy_px'][k]) >= 0.5}
+ return lags, record
+
+
+def bump_lags(tmp_path, name, **kw):
+ """Per cell that moves 0.5 px or more across, the most |lag| of the part's side over the air (`side_lags`, each row
+ meant to take the move the part without the outline past its mask takes at its side's last pixel there) and its row,
+ a bump figure swayed as asked; and the answer."""
+ loop_dir = strip_dir(tmp_path/name, bump_figure)
+ sway.sway_loop(loop_dir, [PART], **kw)
+ answer = answer_of(strip_dir(tmp_path/f'{name}-again', bump_figure), [PART], **kw)
+ clean = answer_of(strip_dir(tmp_path/f'{name}-clean', part_figure), [PART], **kw)
+ source, moved = cells_of(strip_dir(tmp_path/f'{name}-source', bump_figure)), cells_of(loop_dir)
+ yy, xx = np.mgrid[0:H, 0:W].astype(np.float32)
+ out = {}
+ for k in range(N):
+ if abs(answer.dx[k]) < 0.5:
+ continue
+ sx, sy = body_sway(k)
+ field, _ = clean.mask_field(k, xx, yy)
+ lags = side_lags(moved[k], source[k], {y+sy: (float(field[1][y+sy, 92+sx]), float(field[2][y+sy, 92+sx]))
+ for y in range(78, BOTTOM-1)})
+ row = max(lags, key=lambda y: abs(lags[y]))
+ out[k] = (abs(lags[row]), row-sy)
+ return out, answer
+
+
+def test_with_the_membrane_unpinned_the_lower_edge_over_the_air_moves_with_the_part_over_what_is_not_opaque(tmp_path):
+ mask = mask_png(tmp_path/'mask.png', part_at(0))
+ kw = dict(gain=GENTLE, region_mask=mask, region_shrink=0, region_over=True, region_over_solid=True, region_membrane=6,
+ region_shear=0.35)
+ keyed, record = hem_lags(tmp_path, 'keyed', region_membrane_unpin=True, region_clear_alpha=8, **kw)
+ pinned, _ = hem_lags(tmp_path, 'pinned', **kw)
+ # Laid over the solid pixels alone, the pixels not opaque under the lower edge are the membrane's and, but for the key,
+ # anchors of the rim too: the lower edge over them stays short of the part's move. Unpinned, it moves with it.
+ assert keyed and max(keyed.values()) <= 0.25, keyed
+ assert max(pinned.values()) >= 0.5, pinned
+ asked = record['region_mask']['asked']
+ assert asked['membrane_unpin'] is True and asked['clear_alpha'] == 8 and 'floor_beside_px' not in asked
+ # Under the clear alpha the speck is clear: neither held nor the membrane's.
+ answer = answer_of(strip_dir(tmp_path/'again', hem_figure), [PART], region_membrane_unpin=True, region_clear_alpha=8, **kw)
+ plain = answer_of(strip_dir(tmp_path/'plain', hem_figure), [PART], **kw)
+ for k in range(N):
+ sx, sy = body_sway(k)
+ speck = (SPECK[1]+sy, SPECK[0]+sx)
+ _, read = answer.masked_cell(k)
+ assert not read['held'][speck] and not read['membrane'][speck], k
+ _, as_cut = plain.masked_cell(k)
+ assert as_cut['membrane'][speck], k
+
+
+def test_grown_over_its_own_outline_the_side_over_the_air_moves_by_its_rows_share(tmp_path):
+ mask = mask_png(tmp_path/'mask.png', part_at(0))
+ kw = dict(gain=GENTLE, band_up=(0.4, 'auto'), region_mask=mask, region_shrink=0, region_over=True, region_membrane=6)
+ grown, answer = bump_lags(tmp_path, 'grown', region_own_outline=2, **kw)
+ held, as_cut = bump_lags(tmp_path, 'held', **kw)
+ # Held, the outline's pixel past the mask stands where it is and pins the rim: the side bends at its row, a row that
+ # changes from cell to cell. Grown over, it is the part's and the side moves by each row's share of the move.
+ assert grown and max(lag for lag, _ in grown.values()) <= 0.25, grown
+ assert max(lag for lag, _ in held.values()) >= 0.5 and len({row for lag, row in held.values() if lag >= 0.25}) > 1, held
+ for k in range(N):
+ _, read = answer.masked_cell(k)
+ _, kept = as_cut.masked_cell(k)
+ assert np.array_equal(read['region'] & bump_at(k), bump_at(k)) and not (read['anchors'] & bump_at(k)).any(), k
+ assert np.array_equal(kept['anchors'] & bump_at(k), bump_at(k)), k
+ assert answer.mask.asked.own_outline == 2
+
+
+def test_the_floor_beside_the_region_is_its_lowest_pixel_within_n_columns_of_that_side():
+ region = np.zeros((40, 30), bool)
+ for x in range(10, 20):
+ region[5:20+(x-10), x] = True # the lower edge falls 1 px a column to the right
+ alpha = np.full((40, 30), 255, np.uint8)
+ none = np.zeros((40, 30), bool)
+ nearest = sway.layer_sets(region, alpha, 3, none)['floor']
+ beside = sway.layer_sets(region, alpha, 3, none, beside=4)['floor']
+ lowest = {x: 20+(x-10) for x in range(10, 20)} # one under each column's lowest pixel
+ assert [int(v) for v in nearest[7:10]] == [lowest[10]]*3 and [int(v) for v in nearest[20:23]] == [lowest[19]]*3
+ assert [int(v) for v in beside[7:10]] == [lowest[14]]*3 and [int(v) for v in beside[20:23]] == [lowest[19]]*3
+ assert np.array_equal(beside[10:20], nearest[10:20]) and (beside[:7] == 40).all() and (beside[23:] == 40).all()
+
+
# ---------------------------------------------------------------- the rim held round beside it
def corner_step(read, columns, rows):
@@ -682,6 +943,17 @@ def test_what_the_new_keys_are_refused_for_leaves_the_loop_as_it_was(tmp_path):
(['--region-membrane', '6'], r'they need --region-mask$'),
(['--region-mask', str(mask), '--region-membrane', '6'], r'--region-membrane moves the figure under a region laid over it; it needs --region-over$'),
(['--region-mask', str(mask), '--region-over', '--region-membrane', '-1'], r'0 or more; got -1$'),
+ (['--region-membrane-unpin'], r"--region-membrane-unpin, --region-clear-alpha, --region-floor-beside and "
+ r"--region-own-outline shape a region mask's membrane; they need --region-mask$"),
+ (['--region-clear-alpha', '8'], r"membrane; they need --region-mask$"),
+ (['--region-floor-beside', '12'], r"membrane; they need --region-mask$"),
+ (['--region-own-outline', '2'], r"membrane; they need --region-mask$"),
+ (['--region-mask', str(mask), '--region-over', '--region-membrane', '6', '--region-clear-alpha', '0'],
+ r"--region-clear-alpha is the alpha under which a pixel is read as clear, a whole number from 1 to 127 \(under a "
+ r"solid pixel's\); got 0$"),
+ (['--region-mask', str(mask), '--region-clear-alpha', '128'], r'from 1 to 127 \(under a solid pixel\'s\); got 128$'),
+ (['--region-mask', str(mask), '--region-floor-beside', '-1'], r'--region-floor-beside is px, a whole number, 0 or more; got -1$'),
+ (['--region-mask', str(mask), '--region-own-outline', '-2'], r'--region-own-outline is px, a whole number, 0 or more; got -2$'),
(['--region-mask', str(mask), '--region-shear', '0'], r'--region-shear is the most a move one way may rise across'),
(['--region-mask', str(mask), '--region-shear', '-1'], r'over 0; got -1$'),
(['--region-mask', str(mask), '--region-shear', 'nan'], r'over 0; got nan$'),