Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix(cycle-align): a frame made between two source frames that opened a transparent hole inside its silhouette neither source frame has there gives way to the nearer source frame — rife.hole reads the made frame's pixels under solid that the outside cannot reach without crossing its solid body, with no pixel under solid in either source frame within 4 px (HOLE_REACH), in regions at least 5 px thick (HOLE_THICK), over its solid pixels; the fault hole (interpolation_quality.HOLE_WARN 0) is the nearer source frame under --between auto and a named frame kept under rife; every smear row records hole and the warning says it (a gap through a limb); made frames without such a hole, and holes a source frame has (an arm's gap kept or moved a few px), keep their bytes; the jump repair, eased retime, overlapped wrap, video-loop-compare and video-loop-repair do not read it
Soohong Kime168b3308ef3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5ff1f49..6667587 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,14 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the- Output changes: on a synthetic walk away (15.0 % smaller over 73 frames, feet 20 px higher) cut on frames 24 to 47 with `--anchor body`, projection reproduces 24 of 24 cells. Restoring smeared cell 9 (25 pixels) yields verdict `improved` and recovers the cell byte for byte: 119 px tall with feet on row 129 (held loop cells run 119 to 120 px tall, feet on rows 129 to 130; cut with `--size-hold off`, cell 9 is 111 px tall on row 120, and the loop spans 107 to 113 px tall, feet on rows 116 to 122). In 3.1.0 this request ended `unknown`, `source-processing-unsupported` with no candidate. For six synthetic cuts (174 files), `video-loop` matches 3.1.0 except `producer` and `size_hold.cut_frames`. For five non-held cuts (110 files), `video-loop-repair` matches 3.1.0 except `measurement_engine` and candidate report digests.- **Tests**: `tests/video/test_held_cut_restoration.py` checks that the synthetic walk away cut on frames 24 to 47 with `--anchor body` restores smeared cell 9 byte for byte with verdict `improved`, matching first-cell height and row while the filmed source frame is under 0.95 height and 8 px or more higher; checks exact cell reproduction; checks that reports lacking `cut_frames` stay `source-processing-unsupported`; checks that four malformed records exit nonzero with no candidate; checks that a 1 % scale alteration triggers `source-processing-receipt-mismatch`; and checks that `frame_hold` round-trips through JSON to match `undo_scale` with and without feet held.+**Upgrading from 3.1.0: `video-cycle-align` now gives way to the nearer source frame on made frames with enclosed limb holes under `--between auto`, changing only alignment outputs with such holes while made frames without them and all outputs from `video-loop`, `video-loop-compare`, and `video-loop-repair` keep their bytes.**++- **New: `video-cycle-align` now reads a new `hole` fault on frames RIFE makes between source frames and gives way to the nearer source frame.** On every frame RIFE makes between two source frames, `video-cycle-align` reads a new fault, `hole` ("a hole through a limb"). Under `--between auto` (the default) a made frame with this fault gives way to the nearer source frame, like the other faults (`smear`, `outline`, `ghost`); under `--between rife` the frame is kept and named in the warnings. `video-set --align-cycles auto` runs the same alignment. RIFE runs a frame's colour and its coverage as two separate runs. Where a step opens a gap between the legs in one source frame and not the other, the coverage run can carry that gap into a leg the colour run keeps whole: the made frame shows a transparent band through the leg with the leg's fill on both sides. Nothing is added (no `smear`), there is no part coverage (no `ghost`), and the leg's own fill is the only line round the band. The fault `outline` needs both outline readings: the dark line (`outline_loss`) counts a fill darker than 70/255 (dark tights, a boot) as outline, and the rank reading (`outline_shift`) reads the band's edge as lost only where it is 50/255 lighter than the source frames' lines at the same rank, which light lines elsewhere on the art (fur, hair drawn without a dark line) can hide; so such a frame passes every fault and is delivered. The reading `rife.hole(made, a, b)` measures pixels under solid (alpha under 0.5) that the outside of the picture cannot reach without crossing its solid body (enclosed holes), taking those with no pixel under solid in either source frame within 4 px (`rife.HOLE_REACH`, a square neighbourhood), in regions at least 5 px thick (`rife.HOLE_THICK`, opened by a 5 px square, so a pinhole or an antialiased crevice a pixel or two wide is not counted), as a fraction of the made frame's solid pixels, or 0 where the frame has no solid body. The fault `hole` is any reading over 0 (`interpolation_quality.HOLE_WARN`, 0.0). A gap that either source frame has there (for example between an arm and the body, or between the legs a step parts), kept or moved up to 4 px by the flow, is that frame's and is not a hole. A band cut through a leg's edge, reaching the outside, is not an enclosed hole and is left to the outline readings as before.+- Report fields: each row of a loop's `smear` list in the `video-cycle-align` report (and in `cycle_align` in `strip.json`) gains `hole`. A frame given way for it carries `faults: ["hole"]` and `method: "nearest"`, and gets a line in `warnings` with these words: "opened a transparent hole inside its outline over 2.69 % of its body, at least 5 px thick, where neither source frame beside it has one, a gap through a limb, so the nearer source frame was taken there (--between auto)". The `--between` help says the same.+- Python callers: new `rife.hole(made, a, b)`, `rife.HOLE_REACH` (4), and `rife.HOLE_THICK` (5); new `interpolation_quality.HOLE_WARN` (0.0); `interpolation_quality.faults(measure)` returns `"hole"` where the measure carries a `hole` reading over `HOLE_WARN`, and measures without a `hole` key are judged as before.+- Output changes: an alignment whose made frame has an enclosed hole 5 px thick or more that neither source frame beside it has within 4 px now delivers the nearer source frame at that cell instead of the made frame, and counts it in `replaced` and `nearest_at` rather than `made_by_rife` and `made_at`. Made frames without such a hole, and frames whose holes the source frames have, are delivered as before with the same bytes; the report gains `hole` on every made frame. Synthetic numbers (the figure of the tests below): the frame between with a band 8 px wide and 40 px tall through its far leg reads `outline_loss` 0, `outline_shift` 11.3 % (so no `outline` fault, which needs both over 5 %), `ghost` 0, and `hole` 2.69 %; in 3.1.0 it is kept, now the nearer source frame is taken. The frame drawn between, the same with the arm's gap moved 3 px, and the same with a pinhole 3 px wide through the leg read `hole` 0 and are kept. The jump repair of `video-loop`, its eased retime and overlapped wrap, `video-loop-compare`, and `video-loop-repair` do not read `hole`; their measures and outputs are as in 3.1.0. A `--between-cache` directory written by 3.1.0 is not taken from by this version: the code of `rife.py` is part of what a kept frame is keyed by, so those frames are made again.+- **Tests**: new `tests/video/test_inbetween_hole.py`, synthetic only: a 160 by 200 px figure of rectangles outlined in black, legs filled darker than 70/255, the near leg drawn over the far one, an arm joined to the body above and below with an 8 px gap between them in every frame; one source frame with its legs side by side, the other with a gap between them. It checks that the band through the far leg reads as a hole over exactly its own 320 pixels as a share of the solid body, and the drawn frame, the arm's gap, that gap moved 3 px, a 3 px pinhole and an empty frame read 0; that the readings before this one pass the band (`outline_loss` 0, `outline_shift` over 5 %, `dark_excess` below 0, `ghost` 0, `partial_excess` 0, no fault) and that with `hole` the fault is `["hole"]`; that resampling the two frames to four under `--between auto` takes the nearer source frame at both made cells (cells 1 and 3) with the fault named in the warning words, while `--between rife` keeps them named; and that the clean frame and the frame with the moved arm gap are delivered as the very frame made, nothing given way.+## v3.1.0 - Walk and run loop gates for held animation and seam pops, size hold for fixed cuts, talking face layer and default chroma decontamination**Upgrading from 3.0.0: Fixed walk or run cuts and clips whose feet climb or sink now hold size and feet before cutting, changing bytes when size changes by 1 % or more.** Application scan numbers change for held clips, and held fixed cuts are not restored by `video-loop-repair`. Loops cut with `--size-hold off`, clips changing size by under 1 %, held `motion-auto` cuts on one row, and non-walk cuts keep their bytes. The report records `size_hold` on fixed walk or run cuts, `size_hold.feet` when size is held, `size_hold.anchor_regions` under `--anchor motion`, and `size_hold.at_cut` on fixed cuts; `producer` names the new version.diff --git a/docs/loop-repair.md b/docs/loop-repair.mdindex f23bd93..fb6aed6 100644--- a/docs/loop-repair.md+++ b/docs/loop-repair.md@@ -1282,7 +1282,8 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-- **Smear and melt, per made frame**: `cycle_align.smear` lists every frame RIFE made, with its`method` — `rife` (kept) or `nearest` (a source frame beside it taken instead) — its `faults`,its own ghost reading (`ghost`, below; `null` where the screen does not read the loop), its- cross-fade reading and what to `look` at (`crossfade`, below — named, never judged), and what+ cross-fade reading and what to `look` at (`crossfade`, below — named, never judged), the holes+ it opened inside its silhouette that neither source frame has there (`hole`, below), and whatit has that neither source frame beside it has (`rife.smear`):- `dark_excess`: dark pixels (luma under 70/255) inside the body beyond the darker neighbour'scount, as a fraction of its solid pixels — a black smear raises it, a dark part that only@@ -1474,6 +1475,41 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-blends their guesses part-covered, which the augmentation, combining them inside the binary, doesnot. Nor can the binary's own augmented calls be read for it: a call with `-x` from a to b and onefrom b to a agree where the single passes do not.+- **A hole through a limb, per made frame** (`rife.hole`): RIFE runs a frame's colour and its+ coverage apart (section 1), and where a step opens a gap between the legs in one source frame and+ not the other, the coverage run can carry that gap into a leg the colour run keeps whole: a+ transparent band through the leg, its fill on both sides. Nothing is added, no part coverage is+ left to read, and the leg's own fill is all the line round the band — a fill darker than 70/255+ (dark tights, a boot) is outline to the dark line, and one lighter than that is lost rank by rank+ only where it is 50/255 lighter than the source frames' lines at the same rank, which light lines+ elsewhere on the art (fur, hair drawn without a dark line) hide; the fault `outline` needs both.+ Neither reading is about a hole.+ - `hole`: of the made frame's pixels under solid (alpha under 0.5) that the outside of the+ picture cannot reach without crossing its solid body, those with no pixel under solid in either+ source frame within **4 px** (`rife.HOLE_REACH`, a square) — a gap between an arm and the body+ that they have, kept or moved a little by the flow, is theirs — in regions at least **5 px**+ thick (`rife.HOLE_THICK`, opened by a square that wide: a pinhole or an antialiased crevice a+ pixel or two wide opens away), as a fraction of its solid pixels.+ - Over **0** (`interpolation_quality.HOLE_WARN`) the frame has a hole through a limb: the fault+ `hole`, and `auto` takes the nearer source frame there, with a line in `warnings` ("… opened a+ transparent hole inside its outline over 2.69 % of its body, at least 5 px thick, where neither+ source frame beside it has one — a gap through a limb, so the nearer source frame was taken+ there"); `rife` keeps it, named. A hole that thick which neither source frame has near it is one+ whatever its size: a gap a step opens between the legs lies where the source frame that shows it+ open has it, and is that frame's; a gap the flow opens through a lock of hair is read the same+ way as one through a leg.+ - What it does not read: a band cut through a leg's edge rather than inside it reaches the+ outside and is no hole (the outline rules read its edge as before); a hole within 4 px of a+ gap a source frame has is read as that gap moved.+ - Read only on the frames the alignment makes (`hole` in each `smear` row). The jump repair, the+ eased retime and the overlapped wrap (section 2) do not read it, and neither do the versioned+ measures of `video-loop-compare` and `video-loop-repair`.+ - On the synthetic figure of `tests/video/test_inbetween_hole.py` — rectangles outlined in black,+ legs filled darker than 70/255, one source frame with its legs side by side and the other with a+ gap between them — the frame between with a band 8 px wide through its far leg reads+ `outline_loss` 0 and `outline_shift` 11.3 %, no fault before this reading, and `hole` 2.69 %;+ the frame drawn between, the same with the arm's gap 3 px on, and the same with a pinhole 3 px+ wide through the leg read 0.- **What `auto` costs**: a frame replaced shows the nearer source frame, so the motion theresteps as filmed, up to half a frame off its time, and the loop shows fewer distinct drawings asecond than `rife` — but no melted one. A clip drawn on twos has a long step between drawingsdiff --git a/sprite_gen/video/align.py b/sprite_gen/video/align.pyindex ea1f707..1c6f522 100644--- a/sprite_gen/video/align.py+++ b/sprite_gen/video/align.py@@ -7,7 +7,8 @@ to be the same number of frames and to start on the same step. So every loop ofresampled to the set's median length L*: frame k of the new loop is the source loop at timek·L/L* (cyclic, offset 0), so a time that lands on a source frame takes that frame as filmedand only the times between two frames are made by RIFE — each kept unless it smeared, lost its-outline or carries a ghost, where the nearer source frame is taken instead (`--between auto`, the+outline, carries a ghost or opened a hole through a limb that neither source frame beside it has,+where the nearer source frame is taken instead (`--between auto`, thedefault; `rife` keeps every made frame and warns, `nearest` makes none); a made frame kept thatcross-fades two drawings inside its silhouette is named to look at. Every frame delivered,filmed or made, passes the ghost screen (`interpolation_quality.ghost_screen`): a filmed frame half@@ -237,7 +238,8 @@ def resample(frames: list[Image.Image], length: int, interpolate: rife_mod.Inter[i, j], "t": t}` for one made between two.A time between two source frames is made by `interpolate`, with what it added and the outline it- lost measured (`rife.smear`), its own ghost read (`rife.ghost`), and judged (`faults`); `between`+ lost measured (`rife.smear`), its own ghost read (`rife.ghost`), the holes it opened inside its+ silhouette that neither source frame has there (`rife.hole`), and judged (`faults`); `between`auto keeps a made frame with no fault and takes the nearer source frame where it has one, rifekeeps every made frame, nearest makes none and takes the nearer source frame (the motion keeps thefilmed frames at up to half a frame off their time). Every made frame is listed in `smear` with@@ -293,7 +295,7 @@ def resample(frames: list[Image.Image], length: int, interpolate: rife_mod.Intermade = interpolate(a, b, frac)measure = {**rife_mod.smear(made, a, b), "outline_shift": rife_mod.outline_shift(made, a, b),"ghost": rife_mod.ghost(made) if screen["reads"] else None,- "crossfade": rife_mod.crossfade(made, a, b, frac)}+ "crossfade": rife_mod.crossfade(made, a, b, frac), "hole": rife_mod.hole(made, a, b)}wrong = faults(measure)keep = between == "rife" or not wrongtaken = ({"between": [i % count, (i + 1) % count], "t": round(frac, 4)} if keep@@ -747,7 +749,10 @@ def _fault_line(name: str, m: dict[str, Any], ghost: dict[str, Any] | None = Non*([f"lost its outline on {100 * m['outline_loss']:.2f} % of its edge beyond either source frame beside it"+ ("" if m["outline_shift"] is None else f", {100 * m['outline_shift']:.2f} % lighter than theirs at the same rank")+ " — a melted or ghost limb"] if "outline" in m["faults"] else []),- *([f"carries {_ghost_words(m['ghost'])} — a ghost"] if "ghost" in m["faults"] else [])]+ *([f"carries {_ghost_words(m['ghost'])} — a ghost"] if "ghost" in m["faults"] else []),+ *([f"opened a transparent hole inside its outline over {100 * m['hole']:.2f} % of its body, at least "+ f"{rife_mod.HOLE_THICK} px thick, where neither source frame beside it has one — a gap through a limb"]+ if "hole" in m["faults"] else [])]if m["method"] == "nearest":return (f"{name}: frame {m['at']}: RIFE's frame " + "; ".join(what)+ (", so the nearer source frame was taken there (--between auto)" if ghost is None else@@ -1023,7 +1028,8 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:"its gait floor is the shortest cycle the screen looks for")parser.add_argument("--between", choices=BETWEEN, default=DEFAULT_BETWEEN,help="a time between two source frames: auto (default) makes that frame with RIFE and keeps it unless it smeared, "- "lost its outline (a melted limb) or carries a ghost (a part-covered band), where the nearer source frame is "+ "lost its outline (a melted limb), carries a ghost (a part-covered band) or opened a hole through a limb "+ "(a transparent gap inside its outline neither source frame beside it has), where the nearer source frame is ""taken and named in the warnings; rife keeps every made frame and warns on those; nearest takes the nearer ""source frame — nothing made, no RIFE needed, the motion up to half a frame off its time. Every made frame is ""measured in the report (`smear`); under auto and nearest a filmed ghost frame gives way to a clean source "diff --git a/sprite_gen/video/interpolation_quality.py b/sprite_gen/video/interpolation_quality.pyindex a36a587..397b5bd 100644--- a/sprite_gen/video/interpolation_quality.py+++ b/sprite_gen/video/interpolation_quality.py@@ -1,11 +1,11 @@# SPDX-License-Identifier: Apache-2.0"""The shared acceptance policy for the frames repair and cycle alignment deliver.-Measurements live in ``rife`` (``smear``, ``outline_shift``, ``ghost``, ``crossfade``, ``torn``); these are the existing-alignment bounds, also applied before replacing a filmed middle frame during jump repair, the ghost screen+Measurements live in ``rife`` (``smear``, ``outline_shift``, ``ghost``, ``crossfade``, ``torn``, ``hole``); these are the+existing alignment bounds, also applied before replacing a filmed middle frame during jump repair, the ghost screenevery frame a loop path delivers passes, filmed or made, the clean frame a filmed ghost gives way-to, and the cross-fade a made frame is named for, kept; and the torn part a frame of the overlapped wrap-is refused for (docs/loop-repair.md section 4).+to, and the cross-fade a made frame is named for, kept; the torn part a frame of the overlapped wrap+is refused for; and the hole through a limb a frame the alignment makes gives way for (docs/loop-repair.md section 4)."""from collections.abc import Container@@ -32,6 +32,11 @@ CROSSFADE_LOOK = 0.015# legs melted into one shape, a part doubled) and the frames seen as clean. Read only where a frame is read with its# passes — the overlapped wrap's frames (docs/loop-repair.md sections 2 and 4).TORN_WARN = 0.21+# A made frame whose `rife.hole` is over this has a hole through a limb: a transparent hole inside its silhouette at+# least rife.HOLE_THICK pixels thick that neither neighbour has there. Any is one: a gap a step opens between two legs+# lies where the neighbour that shows it open has it, and is theirs. Read only where the measure carries it — the+# frames the cycle alignment makes (docs/loop-repair.md section 4).+HOLE_WARN = 0.0def faults(measure: dict[str, float | None]) -> list[str]:@@ -45,13 +50,16 @@ def faults(measure: dict[str, float | None]) -> list[str]:Where it is None (art painted without a line) and in the versioned measures of`video-loop-compare` and `video-loop-repair`, which carry `rife.smear` alone, the dark linejudges alone. A part torn where the measure carries how far the frame's passes disagree- (`torn`, the overlapped wrap's frames: `rife.passes`, `rife.torn`)."""+ (`torn`, the overlapped wrap's frames: `rife.passes`, `rife.torn`). A hole through a limb where+ it carries the holes the frame opened that neither neighbour has (`hole`, the cycle alignment's+ frames: `rife.hole`)."""shift = measure.get("outline_shift")outline = measure["outline_loss"] if shift is None else min(measure["outline_loss"], shift)return ([*(["smear"] if measure["dark_excess"] > SMEAR_WARN else []),*(["outline"] if outline > OUTLINE_WARN else []),*(["ghost"] if (measure.get("ghost") or 0.0) > GHOST_WARN else []),- *(["torn"] if (measure.get("torn") or 0.0) > TORN_WARN else [])])+ *(["torn"] if (measure.get("torn") or 0.0) > TORN_WARN else []),+ *(["hole"] if (measure.get("hole") or 0.0) > HOLE_WARN else [])])def looks(measure: dict[str, float | None]) -> list[str]:diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.pyindex 9c60ab5..7b39e34 100644--- a/sprite_gen/video/rife.py+++ b/sprite_gen/video/rife.py@@ -14,7 +14,9 @@ the flow failed and melted a limb into the fill; `outline_shift` reads that edgeneighbours' rank by rank, so a mid-tone line the flow only softened is not read as lost — where theneighbours are outlined (`outlined`), and not on art painted without a line; `ghost` reads a band ofpart coverage on a frame alone, filmed or made; `crossfade` reads two drawings cross-faded inside a-whole silhouette, a reading to look at, not a fault (docs/loop-repair.md section 4). `passes` runs+whole silhouette, a reading to look at, not a fault; `hole` reads a transparent hole a made frame opened+inside its silhouette that neither neighbour has there, where the two runs disagree inside a limb+(docs/loop-repair.md section 4). `passes` runsthe sixteen single passes the binary's augmentation combines inside one call, one at a time, and`torn` reads where they disagree: a small part RIFE drags apart beside a large one moving far.@@ -58,6 +60,7 @@ import PILfrom PIL import Image, ImageFilter, PngImagePluginfrom sprite_gen._deps import np+from sprite_gen.video.frames import _components, _nearBINARY = "rife-ncnn-vulkan"MODEL = "rife-v4.6"@@ -99,6 +102,12 @@ OUTLINED = 0.75# antialiased edge or a keyed strand of hair is a pixel or two wide and erodes away; the band a video# model leaves between two drawings, or a flow carries from it, does not.GHOST_THICK = 5+# `hole`: a pixel under solid (alpha under 0.5) that the outside of the picture cannot reach without crossing the solid+# body is a hole; a made frame's is its neighbours' where either of them is under solid within HOLE_REACH pixels of it+# (a gap between an arm and the body, kept or moved a little by the flow), and counts where it makes a region at least+# HOLE_THICK pixels thick: a pinhole a pixel or two wide, an antialiased crevice, is opened away.+HOLE_REACH = 4+HOLE_THICK = 5# `crossfade`: an edge's local range is read over this many pixels, wider than RIFE's blur, so a# softened edge keeps its height and a cross-faded one does not; strong edges are counted in windows# this share of the body's height; a window that kept under CROSSFADE_KEEP of its source frames'@@ -457,6 +466,34 @@ def ghost(frame: Image.Image) -> float:return round(float(thick.sum()) / max(1, int((alpha >= 0.5).sum())), 5)+def _enclosed(solid: np.ndarray) -> np.ndarray:+ """The pixels off `solid` that the outside of the picture cannot reach without crossing it (8-connected)."""+ ys, starts, stops, pieces, _ = _components(~solid, diagonal=True)+ h, w = solid.shape+ inside = ~np.isin(pieces, pieces[(ys == 0) | (ys == h - 1) | (starts == 0) | (stops == w)])+ out = np.zeros_like(solid)+ for y, x0, x1 in zip(ys[inside], starts[inside], stops[inside]):+ out[y, x0:x1] = True+ return out+++def hole(made: Image.Image, a: Image.Image, b: Image.Image) -> float:+ """A hole a made frame opened inside its silhouette that neither neighbour has there: of its pixels under solid+ that the outside cannot reach without crossing its solid body, those with no pixel under solid in either neighbour+ within HOLE_REACH, in regions at least HOLE_THICK pixels thick (opened by a square that wide), as a fraction of its+ solid pixels. The coverage run can carry a gap one neighbour has between two legs into a leg the colour run keeps+ whole: the frame shows the leg's fill on both sides of a transparent band, with no line round it, and nothing+ added and no part coverage to read. A gap a neighbour has there — between an arm and the body, kept or moved a+ little by the flow — is theirs. 0 where the frame has no solid body (docs/loop-repair.md section 4)."""+ m, fa, fb = (np.asarray(f.convert("RGBA").getchannel("A"), dtype=np.float32) / 255.0 for f in (made, a, b))+ solid = m >= 0.5+ if not solid.any():+ return 0.0+ opened = _enclosed(solid) & ~_near((fa < 0.5) | (fb < 0.5), HOLE_REACH)+ thick = _mask(_mask(opened, HOLE_THICK, ImageFilter.MinFilter), HOLE_THICK, ImageFilter.MaxFilter)+ return round(float(thick.sum()) / int(solid.sum()), 5)++def _local_range(x: np.ndarray) -> np.ndarray:"""Per pixel, the largest over the channels of max - min within CROSSFADE_RANGE pixels."""out = np.zeros(x.shape[:2], dtype=np.float32)diff --git a/tests/video/test_inbetween_hole.py b/tests/video/test_inbetween_hole.pynew file mode 100644index 0000000..918310f--- /dev/null+++ b/tests/video/test_inbetween_hole.py@@ -0,0 +1,109 @@+# SPDX-License-Identifier: Apache-2.0+"""A hole through a limb (docs/loop-repair.md section 4): `rife.hole` reads the transparent holes a made frame opened+inside its silhouette that neither source frame beside it has there, and the cycle alignment gives such a frame way to+the nearer source frame (`--between auto`). RIFE runs a frame's colour and its coverage apart, and the coverage run can+carry the gap one source frame has between two legs into a leg the colour run keeps whole: a transparent band through+the leg with its fill on both sides. Nothing is added, no part coverage is left, and around the band the leg's own fill+is all the line there is — the outline readings judge an outline, not a hole.++The fixtures are synthetic: a figure of rectangles outlined in black, legs filled dark (under the dark line, 70/255,+as dark tights or boots are), the near leg over the far one; the first source frame with its legs side by side, the+second with a gap between them, as a step opens it; a gap between the arm and the body in every frame."""++from __future__ import annotations++import numpy as np+from PIL import Image, ImageDraw++from sprite_gen.video import align, rife+from sprite_gen.video.interpolation_quality import faults++W, H = 160, 200+LINE, BODY = (0, 0, 0, 255), (250, 240, 235, 255)+FAR, NEAR = (70, 52, 40, 255), (78, 58, 46, 255) # luma 56 and 62: under the dark line, over the black one by 50/255 and more+SIDE_BY_SIDE, APART, BETWEEN = (50, 80), (45, 85), (47, 82) # the far and the near leg's left column+BAND = (58, 120, 66, 160) # inside the far leg between, 8 px wide: 8 px or more from anything under solid in either source+++def _box(d: ImageDraw.ImageDraw, x0: int, y0: int, x1: int, y1: int, fill: tuple[int, ...]) -> None:+ d.rectangle([x0, y0, x1 - 1, y1 - 1], fill=LINE)+ d.rectangle([x0 + 2, y0 + 2, x1 - 3, y1 - 3], fill=fill)+++def _figure(legs: tuple[int, int], *, arm_gap: int = 0) -> Image.Image:+ """A body over two legs 30 px wide, the far leg from column legs[0], the near from legs[1] drawn over it; an arm+ on the right joined to the body above and below, the gap between them 8 px wide, `arm_gap` px further right."""+ im = Image.new("RGBA", (W, H), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ far, near = legs+ _box(d, far, 96, far + 30, 180, FAR)+ _box(d, near, 96, near + 30, 185, NEAR)+ x = 128 + arm_gap+ _box(d, x, 30, x + 12, 90, BODY)+ for y in (30, 80):+ _box(d, 110, y, x + 12, y + 10, BODY)+ _box(d, 40, 20, 120, 100, BODY)+ d.rectangle([118, 32, 119, 37], fill=BODY) # the arm's joins with no line across them+ d.rectangle([118, 82, 119, 87], fill=BODY)+ return im+++def _holed(frame: Image.Image, box: tuple[int, int, int, int]) -> Image.Image:+ out = frame.copy()+ ImageDraw.Draw(out).rectangle([box[0], box[1], box[2] - 1, box[3] - 1], fill=(0, 0, 0, 0))+ return out+++def _measure(made: Image.Image, a: Image.Image, b: Image.Image) -> dict[str, float | None]:+ return {**rife.smear(made, a, b), "outline_shift": rife.outline_shift(made, a, b), "ghost": rife.ghost(made)}+++def test_a_hole_through_a_leg_is_read_and_a_gap_the_sources_have_is_not():+ """The band reads as a hole over its own pixels; the frame drawn between, the arm's gap all three frames have, and+ that gap moved a few pixels (within rife.HOLE_REACH of theirs) read none. A pinhole thinner than rife.HOLE_THICK is+ opened away."""+ a, b, clean = _figure(SIDE_BY_SIDE), _figure(APART), _figure(BETWEEN)+ alpha = lambda im: np.asarray(im)[..., 3]+ x0, y0, x1, y1 = BAND+ assert (alpha(a)[y0 - 8:y1 + 8, x0 - 8:x1 + 8] == 255).all() and (alpha(b)[y0 - 8:y1 + 8, x0 - 8:x1 + 8] == 255).all()+ holed = _holed(clean, BAND)+ assert rife.hole(holed, a, b) == round((x1 - x0) * (y1 - y0) / int((alpha(holed) >= 128).sum()), 5)+ assert rife.hole(clean, a, b) == 0+ assert (alpha(clean)[40:80, 120:128] == 0).all() # the arm's gap, enclosed+ assert rife.hole(_figure(BETWEEN, arm_gap=3), a, b) == 0+ assert rife.hole(_holed(clean, (60, 130, 63, 133)), a, b) == 0+ assert rife.hole(Image.new("RGBA", (W, H)), a, b) == 0+++def test_the_outline_readings_pass_a_hole_through_a_dark_leg():+ """Around the band the leg's fill is darker than the dark line, so the dark line counts it as outline; read rank by+ rank against black lines it is lost — but the fault `outline` needs both. Nothing dark is added and nothing is part+ covered: the readings before the hole pass it."""+ a, b = _figure(SIDE_BY_SIDE), _figure(APART)+ m = _measure(_holed(_figure(BETWEEN), BAND), a, b)+ assert m["outline_loss"] <= 0 and m["outline_shift"] > 0.05 and m["dark_excess"] < 0+ assert m["ghost"] == 0 and m["partial_excess"] <= 0 and faults(m) == []+ assert faults({**m, "hole": rife.hole(_holed(_figure(BETWEEN), BAND), a, b)}) == ["hole"]+++def test_alignment_takes_the_nearer_frame_for_a_hole_through_a_leg():+ """Resampled from two frames to four (times 0, .5, 1, 1.5), both frames made at t 0.5 carry the band: `--between+ auto` takes the nearer source frame at each and names the fault in the report's words; `rife` keeps them, named."""+ a, b = _figure(SIDE_BY_SIDE), _figure(APART)+ holed = _holed(_figure(BETWEEN), BAND)+ out, facts = align.resample([a, b], 4, lambda x, y, t: holed)+ assert facts["made_at"] == [] and facts["nearest_at"] == [1, 3] and out[1] is b and out[3] is a+ assert all(m["method"] == "nearest" and m["faults"] == ["hole"] and m["hole"] > 0 for m in facts["smear"])+ assert "a gap through a limb" in align._fault_line("walk-front", facts["smear"][0])+ out, facts = align.resample([a, b], 4, lambda x, y, t: holed, between="rife")+ assert facts["made_at"] == [1, 3] and out[1] is holed and all(m["faults"] == ["hole"] for m in facts["smear"])+++def test_alignment_keeps_a_clean_frame_and_one_with_the_gap_the_sources_have():+ """The frame drawn between, and the same with the arm's gap a few pixels on, are kept as made: the same frame+ delivered, nothing given way."""+ a, b = _figure(SIDE_BY_SIDE), _figure(APART)+ for made in (_figure(BETWEEN), _figure(BETWEEN, arm_gap=3)):+ out, facts = align.resample([a, b], 4, lambda x, y, t: made)+ assert facts["made_at"] == [1, 3] and facts["nearest_at"] == [] and out[1] is made and out[3] is made+ assert all(m["faults"] == [] and m["hole"] == 0 for m in facts["smear"])