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 hole a made frame shows is its source frames' own where either has a pixel under solid within 4 px of where the hole's pixel lay at that frame's time, read in place, carried with the whole figure, and carried with what lies round each piece of hole left — rife.hole(made, a, b, t) measures each motion on the two source frames alone (a bilateral block match of premultiplied colour and coverage over a box, coarse to fine, up to rife.HOLE_TRAVEL 0.2 of the body's height, the coarsest level at most rife.HOLE_COARSE 24 px each way), never on the made frame or RIFE's flow; a gap both source frames have carried 24 or 32 px with the body, a gap one has half open while the figure moves 30 px and a hole carried by a part moving 32 px over a body that stays are kept as made with their bytes, while the band through a leg and a band through a moving body are still given way; a frame with no hole in place is read as before
Soohong Kim7aa1e1185eaf
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6667587..009dd4e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -15,11 +15,11 @@ All notable public changes to `sprite-gen` are recorded here. Versions track the**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.+- **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, t)` measures pixels under solid (alpha under 0.5) that the outside of the picture cannot reach without crossing its solid body (enclosed holes). A pixel of a made frame's hole is the source frames' own when either source frame is under solid within 4 px (`rife.HOLE_REACH`, a square neighbourhood) of where that pixel lay at that frame's time, read three ways: in place (the background, still, and a gap kept, or opened by a step where the frame that shows it open has it); carried with the whole figure along the motion the two source frames show for the whole figure; and carried with what lies round the hole along the motion the two source frames show round each piece of hole the first two leave. Where a made frame sits at fraction `t` of the way from source frame a to source frame b, a pixel of it lay in a at `t` of the motion back and lies in b at `1 - t` of it on (rounded so the two are the whole motion apart). Motions are measured only from the two source frames, never from the made frame and never from RIFE's own flow. Each pixel of a box in made frame coordinates is read in a where it lay and in b where it lies for a candidate motion; the motion whose mean absolute difference of premultiplied colour and coverage over the box is least is the motion there, with ties going to the smaller motion. The whole figure's motion is read over the box round all three frames' solid bodies. Each remaining piece's motion is read over the box round the piece, widened each way by `max(t, 1 - t)` of the farthest motion followed, so the box holds a gap the source frames carried there. A motion is followed up to 20 % of the body's height each way (`rife.HOLE_TRAVEL`, 0.2). The search is coarse to fine: the whole range is searched on a level pooled in square blocks of a power of two, the smallest power for which the range is at most 24 px each way (`rife.HOLE_COARSE`, 24), then within 2 px of twice the coarser motion on each finer level, down to the pixel. Motions are measured only on frames that keep a hole after the in-place reading; a frame with no hole in place is read as before. The 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) are taken after each step, 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 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`; the line says the frame opened a transparent hole inside its outline over the given share of its body, at least 5 px thick, where neither source frame beside it has one, a gap through a limb, and that the nearer source frame was taken there. The `--between` help says the same.+- Python callers: new `rife.hole(made, a, b, t)`, `rife.HOLE_TRAVEL` (0.2), `rife.HOLE_COARSE` (24), `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, in place or carried by the motions above, 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 in place or carried, are delivered as in 3.1.0 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: figure 1 (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) 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. Figure 2 (a body 120 by 180 px of one colour with a gap 8 by 40 px through it, on a 240 by 220 px canvas, moved whole from one source frame to the next by 0, 4, 8, 12, 24 and 32 px, across and diagonally) checks that at every move it is resampled to four with both made cells delivered as the very frame made, no fault, and reads `hole` 0, each source frame moved half the way giving the frame between byte for byte. Figure 3 (a body 90 px wide over two legs 24 px wide, each standing in a boot that reaches under the other leg, so a gap a step opens between the legs is closed by the boots below; in one source frame the legs touch, in the other they are parted), still and moving 30 px between source frames, is kept the same way and reads 0; and moving with an 8 by 40 px band through its body, it reads exactly its 320 pixels as a share of the solid body, in either order of the source frames, with both made cells given way to the nearer source frame and `hole` among their faults. Figure 4 (a body 200 by 180 px of one colour that stays, with a part on it 40 px square and a hole 14 px square through the part, the part moving 32 px from one source frame to the next) is kept the same way and reads 0.## 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 decontaminationdiff --git a/docs/loop-repair.md b/docs/loop-repair.mdindex fb6aed6..1235d76 100644--- a/docs/loop-repair.md+++ b/docs/loop-repair.md@@ -1479,37 +1479,73 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-coverage apart (section 1), and where a step opens a gap between the legs in one source frame andnot the other, the coverage run can carry that gap into a leg the colour run keeps whole: atransparent 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+ 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 rankonly where it is 50/255 lighter than the source frames' lines at the same rank, which light lineselsewhere 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.+ picture cannot reach without crossing its solid body (enclosed holes), those with no pixel+ under solid in either source frame within **4 px** (`rife.HOLE_REACH`, a square neighbourhood)+ of where that pixel lay at that frame's time, read three ways: in place (the background,+ still, and a gap kept, or opened by a step where the frame showing it open has it); carried+ with the whole figure along the motion the two source frames show for the whole figure; and+ carried with what lies round the hole along the motion the two source frames show round each+ piece of hole the first two leave. Where the made frame sits at fraction `t` from source frame a+ to source frame b, a pixel of it lay in a at `t` of the motion back and lies in b at `1 - t` of+ it on (rounded so the two are the whole motion apart). Motions are measured only from the two+ source frames, never from the made frame or RIFE's flow: for each candidate motion over a box+ in made frame coordinates, each pixel is read in a where it lay and in b where it lies, taking+ the motion with least mean absolute difference of premultiplied colour and coverage (ties+ going to smaller motion). The whole figure's motion is read over the box round all three+ frames' solid bodies. Each remaining piece's motion is read over the box round the piece,+ widened each way by `max(t, 1 - t)` of the farthest motion followed. A motion is followed up to+ 20 % of the body's height each way (`rife.HOLE_TRAVEL`, 0.2), searched coarse to fine: pooled+ in square blocks of a power of two where the range is at most 24 px each way+ (`rife.HOLE_COARSE`, 24), then within 2 px of twice the coarser motion on each finer level down+ to the pixel. Motions are measured only on frames keeping a hole after the in-place reading.+ 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) are taken after each step, 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.+ `hole`, and `auto` takes the nearer source frame there, with a line in `warnings` (it says the+ frame opened a transparent hole inside its outline over the given share of its body, at least+ 5 px thick, where neither source frame beside it has one, a gap through a limb, and that the+ nearer source frame was taken there); `rife` keeps it, named. A gap both source frames have,+ carried by the body's motion, lies in the frame between on the way from where one source frame+ has it to where the other has it; moved far enough it lies 5 px and more from where either+ source frame has it in place, and a reading in place alone would take it for a hole and give+ a correct frame way. A gap one source frame has, half open in the frame between while the figure+ moves, lies beside where that frame has it in the same way. Following the motions keeps those+ gaps as the source frames' own; 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.+ outside and is no hole (the outline readings read its edge as before); a hole within 4 px of a+ gap a source frame has, in place or where the motion read carries it, is read as that gap. A+ motion farther than 20 % of the body's height each way is not followed: a gap both source frames+ have, carried farther between them, reads as a hole. One motion is read per box: the whole+ figure's, then one per piece of hole. Where parts in the box move different ways, the motion read+ is the one that agrees best over the box; a hole lying where that motion carries a gap either+ source frame has is read as that gap, and a gap carried by a part that the rest of the box+ outweighs can read as a hole.+ - Read only on the frames the alignment makes (`hole` in each `smear` row). The jump repair of+ `video-loop`, its eased retime and 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 figures of `tests/video/test_inbetween_hole.py`: figure 1 (rectangles outlined+ in black, legs filled darker than 70/255, one source frame with legs side by side and the other+ with a gap between them) has the frame between with a band 8 px wide and 40 px tall through its+ far leg read `outline_loss` 0, `outline_shift` 11.3 %, `ghost` 0, and `hole` 2.69 % (its own 320+ pixels as a share of the body); whole figure motion is 0 and the band box shows 5 px, neither+ showing a gap where the band lay, so the band is read whole. The drawn frame, the arm gap moved+ 3 px, a pinhole 3 px wide, and an empty frame read `hole` 0. In figure 2 (a 120 by 180 px body+ with an 8 by 40 px gap, moved 0 to 32 px across and diagonally), the gap from 24 px on lies+ 5 px and more from where either source frame has it in place (1.50 % in place alone), but whole+ figure motion carries it: `hole` 0. In figure 3 (a body 90 px wide on legs in boots closing the+ gap below), a half-open gap when moving 30 px lies 8 px and more beside where the parted frame+ has it (300 px in place), but whole figure motion carries it: `hole` 0; with an 8 by 40 px band+ cut through the moving body it reads `hole` 2.88 % (320 pixels) and `faults: ["outline", "hole"]`.+ In figure 4 (a 200 by 180 px still body with a 40 px square part and 14 px square hole moving+ 32 px), whole figure motion is 0, but motion round the hole is 32 px, carrying it: `hole` 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 1c6f522..07d20d0 100644--- a/sprite_gen/video/align.py+++ b/sprite_gen/video/align.py@@ -239,7 +239,8 @@ def resample(frames: list[Image.Image], length: int, interpolate: rife_mod.InterA time between two source frames is made by `interpolate`, with what it added and the outline itlost 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`+ silhouette that neither source frame has there, in place or carried by the motion the two show+ (`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@@ -295,7 +296,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), "hole": rife_mod.hole(made, a, b)}+ "crossfade": rife_mod.crossfade(made, a, b, frac), "hole": rife_mod.hole(made, a, b, frac)}wrong = faults(measure)keep = between == "rife" or not wrongtaken = ({"between": [i % count, (i + 1) % count], "t": round(frac, 4)} if keepdiff --git a/sprite_gen/video/interpolation_quality.py b/sprite_gen/video/interpolation_quality.pyindex 397b5bd..ee61428 100644--- a/sprite_gen/video/interpolation_quality.py+++ b/sprite_gen/video/interpolation_quality.py@@ -33,8 +33,9 @@ CROSSFADE_LOOK = 0.015# 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+# least rife.HOLE_THICK pixels thick that neither neighbour has there, in place or carried by the motion the two show.+# Any is one: a gap a step opens between two legs lies where the neighbour that shows it open has it, and a gap the two+# have moved with the body lies on its way between them; both are theirs. Read only where the measure carries it — the# frames the cycle alignment makes (docs/loop-repair.md section 4).HOLE_WARN = 0.0diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.pyindex 7b39e34..197eb3e 100644--- a/sprite_gen/video/rife.py+++ b/sprite_gen/video/rife.py@@ -15,8 +15,8 @@ neighbours' rank by rank, so a mid-tone line the flow only softened is not readneighbours 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 awhole 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` runs+inside its silhouette that neither neighbour has there, in place or carried by the motion the two show,+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.@@ -103,11 +103,17 @@ OUTLINED = 0.75# 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.+# body is a hole; a made frame's is its neighbours' where either of them is under solid within HOLE_REACH pixels of where+# it lay at that neighbour's time — in place, carried with the whole figure, or carried with what lies round it (a gap+# between an arm and the body or between two legs, kept, opened by a step or moved with them) — 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. Each+# motion is the one the two neighbours alone show (`_travel`), never the flow's, so a frame the flow got wrong is not+# judged by the flow that made it; it is followed up to HOLE_TRAVEL of the body's height, searched whole on the level+# pooled coarsely enough that this reach is at most HOLE_COARSE pixels each way, then refined down to the pixel.HOLE_REACH = 4HOLE_THICK = 5+HOLE_TRAVEL = 0.2+HOLE_COARSE = 24# `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'@@ -477,21 +483,132 @@ def _enclosed(solid: np.ndarray) -> np.ndarray: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)."""+def _opened(mask: np.ndarray, size: int) -> np.ndarray:+ """`mask` opened by a square `size` pixels wide: what is thinner than that goes."""+ return _mask(_mask(mask, size, ImageFilter.MinFilter), size, ImageFilter.MaxFilter)+++def _premultiplied(frame: Image.Image) -> np.ndarray:+ """An RGBA frame as its colour premultiplied by its coverage and the coverage (0..1, H x W x 4)."""+ x = np.asarray(frame.convert("RGBA"), dtype=np.float32) / 255.0+ return np.concatenate([x[..., :3] * x[..., 3:], x[..., 3:]], axis=-1)+++def _pooled(x: np.ndarray, f: int) -> np.ndarray:+ """`x` (H x W x C) averaged over blocks f pixels on a side, padded with zeros to whole blocks."""+ if f == 1:+ return x+ x = np.pad(x, ((0, -x.shape[0] % f), (0, -x.shape[1] % f), (0, 0)))+ return x.reshape(x.shape[0] // f, f, x.shape[1] // f, f, x.shape[2]).mean(axis=(1, 3))+++def _apart(v: int, t: float) -> tuple[int, int]:+ """Along one axis, for a motion of v pixels from a to b: where a pixel of the frame at fraction t lay in a and lies+ in b, from it — t of the motion back and 1 - t of it on, rounded so the two are v apart."""+ back = int(np.floor(t * v + 0.5))+ return -back, v - back+++def _match(pa: np.ndarray, pb: np.ndarray, t: float, box: tuple[int, int, int, int], centre: tuple[int, int],+ radius: int) -> tuple[int, int]:+ """The motion (rows, columns) from a to b, within `radius` of `centre` each way, under which the two agree best over+ `box` (y0, y1, x0, x1) of the frame at fraction t: each pixel of the box read in a where it lay and in b where it lies+ (`_apart`), the mean absolute difference of their premultiplied colour and coverage (`_premultiplied`), a tie to the+ smaller motion. Past the picture both are empty."""+ y0, y1, x0, x1 = box+ pad = max(abs(centre[0]), abs(centre[1])) + radius + 1+ h, w = y1 - y0, x1 - x0+ crops = []+ for x in (pa, pb):+ crop = np.zeros((h + 2 * pad, w + 2 * pad, x.shape[2]), dtype=x.dtype)+ sy0, sy1, sx0, sx1 = max(0, y0 - pad), min(x.shape[0], y1 + pad), max(0, x0 - pad), min(x.shape[1], x1 + pad)+ crop[sy0 - y0 + pad:sy1 - y0 + pad, sx0 - x0 + pad:sx1 - x0 + pad] = x[sy0:sy1, sx0:sx1]+ crops.append(crop)+ best: tuple[float, int, int, int] | None = None+ for vy in range(centre[0] - radius, centre[0] + radius + 1):+ ay, by = _apart(vy, t)+ for vx in range(centre[1] - radius, centre[1] + radius + 1):+ ax, bx = _apart(vx, t)+ cost = float(np.abs(crops[0][pad + ay:pad + ay + h, pad + ax:pad + ax + w]+ - crops[1][pad + by:pad + by + h, pad + bx:pad + bx + w]).mean(dtype=np.float64))+ key = (cost, vy * vy + vx * vx, vy, vx)+ if best is None or key < best:+ best = key+ return best[2], best[3]+++def _travel(pa: np.ndarray, pb: np.ndarray, t: float, box: tuple[int, int, int, int], reach: int) -> tuple[int, int]:+ """The motion from a to b over `box` of the frame at fraction t (`_match`), up to `reach` pixels each way: searched+ whole on the level pooled in blocks of the smallest power of two that brings the reach to at most HOLE_COARSE pixels+ each way, then within two pixels of twice the coarser motion on each level down to the pixel."""+ f = 1+ while reach > HOLE_COARSE * f:+ f *= 2+ v, radius = (0, 0), -(-reach // f)+ y0, y1, x0, x1 = box+ while True:+ qa, qb = _pooled(pa, f), _pooled(pb, f)+ v = _match(qa, qb, t, (y0 // f, min(qa.shape[0], -(-y1 // f)), x0 // f, min(qa.shape[1], -(-x1 // f))), v, radius)+ if f == 1:+ return v+ f, v, radius = f // 2, (2 * v[0], 2 * v[1]), 2+++def _moved(mask: np.ndarray, dy: int, dx: int) -> np.ndarray:+ """`mask` moved dy rows down and dx columns right; what comes in is empty."""+ h, w = mask.shape+ out = np.zeros_like(mask)+ if abs(dy) < h and abs(dx) < w:+ out[max(0, dy):h + min(0, dy), max(0, dx):w + min(0, dx)] = mask[max(0, -dy):h - max(0, dy), max(0, -dx):w - max(0, dx)]+ return out+++def _bounds(mask: np.ndarray, margin: int = 0) -> tuple[int, int, int, int]:+ """The rows and columns `mask` spans (y0, y1, x0, x1), `margin` further each way inside the picture."""+ ys, xs = np.nonzero(mask)+ return (max(0, int(ys.min()) - margin), min(mask.shape[0], int(ys.max()) + 1 + margin),+ max(0, int(xs.min()) - margin), min(mask.shape[1], int(xs.max()) + 1 + margin))+++def hole(made: Image.Image, a: Image.Image, b: Image.Image, t: float) -> float:+ """A hole a made frame at fraction `t` from `a` to `b` 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 where neither+ neighbour is under solid within HOLE_REACH of where the pixel lay at that neighbour's time — in place, carried with+ the whole figure, or carried with what lies round the piece of hole — in regions at least HOLE_THICK pixels thick+ (opened by a square that wide), as a fraction of its solid pixels. Each motion is the one the two neighbours alone+ show (`_travel`, followed up to HOLE_TRAVEL of the body's height): over the box round all three frames' solid bodies,+ then over the box round each piece of hole that one leaves, widened each way by as far as the piece can have lain in+ the farther neighbour (max(t, 1 - t) of that reach), so the box holds a gap the neighbours carried there.+ 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 — and wherever the band's pixels lay, both neighbours show the leg there. A gap the neighbours have+ — between an arm and the body or between two legs, kept, opened by a step, or moved with the body or the arm — 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.5if 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)+ near_a, near_b = _near(fa < 0.5, HOLE_REACH), _near(fb < 0.5, HOLE_REACH)+ left = _opened(_enclosed(solid) & ~near_a & ~near_b, HOLE_THICK)+ if left.any():+ pa, pb = _premultiplied(a), _premultiplied(b)+ rows = np.nonzero(solid.any(axis=1))[0]+ reach = max(1, int(round(HOLE_TRAVEL * int(rows[-1] - rows[0] + 1))))++ def theirs(v: tuple[int, int]) -> np.ndarray:+ (ay, by), (ax, bx) = _apart(v[0], t), _apart(v[1], t)+ return _moved(near_a, -ay, -ax) | _moved(near_b, -by, -bx)++ left = _opened(left & ~theirs(_travel(pa, pb, t, _bounds(solid | (fa >= 0.5) | (fb >= 0.5)), reach)), HOLE_THICK)+ ys, starts, stops, pieces, _ = _components(left, diagonal=True)+ margin = int(np.ceil(max(t, 1 - t) * reach))+ for piece in np.unique(pieces):+ part = np.zeros_like(left)+ for y, x0, x1 in zip(ys[pieces == piece], starts[pieces == piece], stops[pieces == piece]):+ part[y, x0:x1] = True+ left &= ~(part & theirs(_travel(pa, pb, t, _bounds(part, margin), reach)))+ left = _opened(left, HOLE_THICK)+ return round(float(left.sum()) / int(solid.sum()), 5)def _local_range(x: np.ndarray) -> np.ndarray:diff --git a/tests/video/test_inbetween_hole.py b/tests/video/test_inbetween_hole.pyindex 918310f..ec50a2d 100644--- a/tests/video/test_inbetween_hole.py+++ b/tests/video/test_inbetween_hole.py@@ -1,14 +1,18 @@# 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.+inside its silhouette that neither source frame beside it has there, in place or carried by the motion the two show, 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. A gap the source frames have is theirs wherever the motion between them carries it: kept, moved with the body or+with a part of it, or opened by a step between two legs.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."""+second with a gap between them, as a step opens it; a gap between the arm and the body in every frame. Then a body with+a gap through it, moved whole; a figure whose legs part above boots that close the gap below, still and moving; and a+body that stays with a part on it, a hole through the part, moved."""from __future__ import annotations@@ -23,6 +27,7 @@ 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 moreSIDE_BY_SIDE, APART, BETWEEN = (50, 80), (45, 85), (47, 82) # the far and the near leg's left columnBAND = (58, 120, 66, 160) # inside the far leg between, 8 px wide: 8 px or more from anything under solid in either source+MOVES = (0, 4, 8, 12, 24, 32) # px the body with a gap moves from one source frame to the nextdef _box(d: ImageDraw.ImageDraw, x0: int, y0: int, x1: int, y1: int, fill: tuple[int, ...]) -> None:@@ -48,6 +53,42 @@ def _figure(legs: tuple[int, int], *, arm_gap: int = 0) -> Image.Image:return im+def _body(dx: int = 0, dy: int = 0) -> Image.Image:+ """A body 120 x 180 px of one colour with a gap 8 x 40 px through it, on a 240 x 220 px canvas, moved dx columns+ right and dy rows down."""+ im = Image.new("RGBA", (240, 220), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ d.rectangle([40 + dx, 20 + dy, 159 + dx, 199 + dy], fill=(45, 55, 60, 255))+ d.rectangle([120 + dx, 60 + dy, 127 + dx, 99 + dy], fill=(0, 0, 0, 0))+ return im+++def _parting(x: int, far: int, near: int) -> Image.Image:+ """A body 90 px wide from column x over two legs 24 px wide, the far leg from column x + far and the near from+ x + near, each standing in a boot that reaches under the other leg: legs side by side touch, and a gap a step opens+ between them is closed by the boots below."""+ im = Image.new("RGBA", (200, 200), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ fx, nx = x + far, x + near+ _box(d, fx, 90, fx + 24, 170, FAR)+ _box(d, fx, 160, nx + 12, 180, (120, 80, 50, 255))+ _box(d, nx, 90, nx + 24, 172, NEAR)+ _box(d, nx - 6, 162, nx + 30, 184, (130, 90, 60, 255))+ _box(d, x, 20, x + 90, 100, BODY)+ return im+++def _ring(dx: int) -> Image.Image:+ """A body 200 x 180 px of one colour that stays, with a part on it 40 px square, a hole 14 px square through the part,+ the part moved dx columns right."""+ im = Image.new("RGBA", (260, 220), (0, 0, 0, 0))+ d = ImageDraw.Draw(im)+ d.rectangle([30, 20, 229, 199], fill=(45, 55, 60, 255))+ d.rectangle([100 + dx, 80, 139 + dx, 119], fill=(150, 90, 60, 255))+ d.rectangle([113 + dx, 93, 126 + dx, 106], fill=(0, 0, 0, 0))+ 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))@@ -58,6 +99,24 @@ def _measure(made: Image.Image, a: Image.Image, b: Image.Image) -> dict[str, floreturn {**rife.smear(made, a, b), "outline_shift": rife.outline_shift(made, a, b), "ghost": rife.ghost(made)}+def _shifted(frame: Image.Image, dx: int, dy: int) -> Image.Image:+ out = Image.new("RGBA", frame.size, (0, 0, 0, 0))+ out.paste(frame, (dx, dy))+ return out+++# What the alignment does with a frame made between two source frames that it keeps as made: both frames made at t 0.5+# (times .5 and 1.5 of four) delivered as the very frame made, nothing given way, no fault.+KEPT = {"made_at": [1, 3], "nearest_at": [], "faults": [[], []], "delivered": True}+++def _resampled(a: Image.Image, made: Image.Image, b: Image.Image) -> dict[str, object]:+ """What the alignment does resampling a and b to four frames, both frames made at t 0.5 being `made`."""+ out, facts = align.resample([a, b], 4, lambda x, y, t: made)+ return {"made_at": facts["made_at"], "nearest_at": facts["nearest_at"], "faults": [m["faults"] for m in facts["smear"]],+ "delivered": out[1] is made and out[3] is 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, andthat gap moved a few pixels (within rife.HOLE_REACH of theirs) read none. A pinhole thinner than rife.HOLE_THICK is@@ -67,12 +126,12 @@ def test_a_hole_through_a_leg_is_read_and_a_gap_the_sources_have_is_not():x0, y0, x1, y1 = BANDassert (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 rife.hole(holed, a, b, 0.5) == round((x1 - x0) * (y1 - y0) / int((alpha(holed) >= 128).sum()), 5)+ assert rife.hole(clean, a, b, 0.5) == 0assert (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+ assert rife.hole(_figure(BETWEEN, arm_gap=3), a, b, 0.5) == 0+ assert rife.hole(_holed(clean, (60, 130, 63, 133)), a, b, 0.5) == 0+ assert rife.hole(Image.new("RGBA", (W, H)), a, b, 0.5) == 0def test_the_outline_readings_pass_a_hole_through_a_dark_leg():@@ -83,7 +142,7 @@ def test_the_outline_readings_pass_a_hole_through_a_dark_leg():m = _measure(_holed(_figure(BETWEEN), BAND), a, b)assert m["outline_loss"] <= 0 and m["outline_shift"] > 0.05 and m["dark_excess"] < 0assert m["ghost"] == 0 and m["partial_excess"] <= 0 and faults(m) == []- assert faults({**m, "hole": rife.hole(_holed(_figure(BETWEEN), BAND), a, b)}) == ["hole"]+ assert faults({**m, "hole": rife.hole(_holed(_figure(BETWEEN), BAND), a, b, 0.5)}) == ["hole"]def test_alignment_takes_the_nearer_frame_for_a_hole_through_a_leg():@@ -107,3 +166,56 @@ def test_alignment_keeps_a_clean_frame_and_one_with_the_gap_the_sources_have():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 madeassert all(m["faults"] == [] and m["hole"] == 0 for m in facts["smear"])+++def test_a_gap_both_source_frames_have_moved_with_the_body_is_theirs():+ """The body with its gap moved whole from one source frame to the next, across and diagonally, by each of MOVES, the+ frame between drawn halfway: each source frame moved half the way onto it gives it byte for byte. From 24 px on, the+ gap lies 5 px and more from where either source frame has it, but on the way the body moves, so it is theirs: every+ frame is kept as made, and reads no hole."""+ seen = {}+ for move in MOVES:+ for down in (0, 1):+ dx, dy = move // 2, down * move // 2+ a, made, b = _body(-dx, -dy), _body(), _body(dx, dy)+ assert _shifted(a, dx, dy).tobytes() == made.tobytes() == _shifted(b, -dx, -dy).tobytes()+ seen[move, "diagonally" if down else "across"] = (a, made, b, _resampled(a, made, b))+ assert {k: v[3] for k, v in seen.items() if v[3] != KEPT} == {}+ assert {k: rife.hole(made, a, b, 0.5) for k, (a, made, b, _) in seen.items()} == {k: 0.0 for k in seen}+++def test_a_gap_one_source_frame_has_half_open_between_is_theirs():+ """Legs side by side in one source frame, touching, and parted in the other; the frame between drawn with them half+ parted. The gap there is the parted frame's, opened halfway: with the figure still, inside where that frame has it;+ with the figure moving 30 px, 8 px and more beside it, where the figure carries it. Both are kept as made."""+ seen = {}+ for name, (xa, fa, na), (xb, fb, nb) in (("still", (40, 10, 34), (40, 4, 40)), ("moving", (20, 10, 34), (50, 10, 50))):+ a, b = _parting(xa, fa, na), _parting(xb, fb, nb)+ x, far, near = (xa + xb) // 2, (fa + fb) // 2, (na + nb) // 2+ made = _parting(x, far, near)+ assert na == fa + 24 and (np.asarray(made)[110:150, x + far + 24:x + near, 3] == 0).all()+ seen[name] = (a, made, b, _resampled(a, made, b))+ assert {k: v[3] for k, v in seen.items() if v[3] != KEPT} == {}+ assert {k: rife.hole(made, a, b, 0.5) for k, (a, made, b, _) in seen.items()} == {"still": 0.0, "moving": 0.0}+++def test_a_gap_moved_with_a_part_while_the_body_stays_is_theirs():+ """A part with a hole through it moves 32 px across a body that stays, the frame between drawn with the part+ halfway: the hole lies 5 px and more from where either source frame has it, on the way the part moves. The body+ outweighs the part in the whole figure, which stays; the hole is carried with what lies round it, and the frame is+ kept as made."""+ a, made, b = _ring(-16), _ring(0), _ring(16)+ assert _resampled(a, made, b) == KEPT and rife.hole(made, a, b, 0.5) == 0+++def test_a_hole_through_a_moving_body_is_still_read():+ """The parting figure moving 30 px, the frame between with a band 8 px wide through its body: wherever its pixels+ lay, in place or carried, both source frames show the body there, so the band reads as a hole over its own pixels+ and the frame gives way to the nearer source frame. (The body's fill is light, so its edge round the band also reads+ as an outline lost.)"""+ a, b = _parting(20, 10, 34), _parting(50, 10, 50)+ holed = _holed(_parting(35, 10, 42), (70, 40, 78, 80))+ 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("hole" in m["faults"] for m in facts["smear"])+ assert rife.hole(holed, a, b, 0.5) == rife.hole(holed, b, a, 0.5) == round(8 * 40 / int((np.asarray(holed)[..., 3] >= 128).sum()), 5)