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(cycle-align): the pairs a made frame's holes take are the set that pairs the most of them, not the cheapest first — rife._taken searches each group of triples sharing holes exactly: every hole the in-place and whole-figure readings give the source frames must pair, and of the sets that pair them all the one pairing the most of the rest, then of the least summed cost, is taken (rife._searched, depth first from the hole with the fewest free triples, cut where it can pair no more than the best found, ending at a set pairing every hole; rife.HOLE_PAIR_STEPS 5000 steps a group, past which none of its holes pairs); where no set pairs every one of those, one is a hole more than the source frames have and none of the rest pairs; the fourth checkpoint validator's two holes 12 px apart on a curved way, and three and four such holes, are kept as made, while one hole more before the row, the row's last hole shut in one source frame and a column whose middle hole one source frame shuts are given way

Soohong Kim230090cc2135
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57fc4de..5cfc675 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, 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 in place (the background, still, and a gap kept, or opened by a step where the frame that shows it open has it) and carried with the whole figure along the motion the two source frames show for the whole figure. 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). The motion is measured only from the two source frames, never from the made frame and never from RIFE's own flow: over the box round all three frames' solid bodies, each pixel 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, ties going to the smaller motion. It is followed up to 20 % of the body's height each way (`rife.HOLE_TRAVEL`, 0.2), coarse to fine: the whole range on a level pooled in square blocks of a power of two, the smallest 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. A hole those two readings leave is the source frames' own, whatever carried it (a part moving over a body that stays, plain or patterned, turning, growing, sheared or swinging), where it is one hole of each source frame. A hole is an 8-connected piece of a frame's enclosed pixels: the made frame's count where they keep a pixel opened by a 5 px square, a source frame's where they keep one opened by a 3 px square (`rife.HOLE_SOURCE_THICK`, 3). Each is read for its area, its centroid, its shape (the logarithms of its pixels' two principal second moments, each plus 1/12, over its area, which turning and growing keep) and the colour round it (the mean premultiplied colour of the frame's solid pixels 5 to 8 px out). The two source frames' holes: areas within 4 times of each other (`rife.HOLE_PAIR_AREA`, 4.0), shapes within 2 times on each moment (`rife.HOLE_PAIR_SHAPE`, 2.0), colours within 0.12 on each channel (`rife.HOLE_COLOUR`, 0.12), centroids at most sqrt(2) times the reach and 4 px apart. The made frame's hole: area within a quarter of their range (`rife.HOLE_AREA`, 0.25), shape within 1.3 times of theirs (`rife.HOLE_SHAPE`, 1.3), colour within 0.12 of each, and centroid within 4 px and 0.1 of their distance (`rife.HOLE_PATH`, 0.1) of the point at `t` on the line between theirs. Pairs are taken one to one, each source frame's hole explaining one hole of the made frame: the made frame's holes the first two readings already give the source frames first, then the rest, each the alikest and nearest first. So a hole the made frame keeps where it was explains no other, and in a row of alike holes moving, each takes its own two. The made frame is never used to measure a motion, and RIFE's flow is never read. The motion is read only on frames that keep a hole after the in-place reading, and the pairing only on frames that keep one after the whole figure's motion; a frame with no hole in place is read with no further cost. 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.
+- **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 in place (the background, still, and a gap kept, or opened by a step where the frame that shows it open has it) and carried with the whole figure along the motion the two source frames show for the whole figure. 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). The motion is measured only from the two source frames, never from the made frame and never from RIFE's own flow: over the box round all three frames' solid bodies, each pixel 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, ties going to the smaller motion. It is followed up to 20 % of the body's height each way (`rife.HOLE_TRAVEL`, 0.2), coarse to fine: the whole range on a level pooled in square blocks of a power of two, the smallest 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. A hole those two readings leave is the source frames' own, whatever carried it (a part moving over a body that stays, plain or patterned, turning, growing, sheared or swinging), where it is one hole of each source frame. A hole is an 8-connected piece of a frame's enclosed pixels: the made frame's count where they keep a pixel opened by a 5 px square, a source frame's where they keep one opened by a 3 px square (`rife.HOLE_SOURCE_THICK`, 3). Each is read for its area, its centroid, its shape (the logarithms of its pixels' two principal second moments, each plus 1/12, over its area, which turning and growing keep) and the colour round it (the mean premultiplied colour of the frame's solid pixels 5 to 8 px out). The two source frames' holes: areas within 4 times of each other (`rife.HOLE_PAIR_AREA`, 4.0), shapes within 2 times on each moment (`rife.HOLE_PAIR_SHAPE`, 2.0), colours within 0.12 on each channel (`rife.HOLE_COLOUR`, 0.12), centroids at most sqrt(2) times the reach and 4 px apart. The made frame's hole: area within a quarter of their range (`rife.HOLE_AREA`, 0.25), shape within 1.3 times of theirs (`rife.HOLE_SHAPE`, 1.3), colour within 0.12 of each, and centroid within 4 px and 0.1 of their distance (`rife.HOLE_PATH`, 0.1) of the point at `t` on the line between theirs. Pairs are taken one to one: each source frame's hole explains one hole of the made frame, and no hole of any frame is in two pairs taken. Every hole the first two readings already give that has a pair must be paired: of sets pairing those holes, the set taken pairs the most of the other holes, and of those, the one with least summed cost; so a hole kept where it was explains no other, and alike holes moving together each keep their own two. Where no set pairs every one of the holes the first two readings give, one of them is a hole more than the source frames have, and none of the other holes in that group is paired. An exact search finds the set within groups of pairs sharing a hole, taking at most 5000 steps per group (`rife.HOLE_PAIR_STEPS`, 5000), past which a group pairs none of its holes and the frame gives way to the nearer source frame. The made frame is never used to measure a motion, and RIFE's flow is never read. The motion is read only on frames that keep a hole after the in-place reading, and the pairing only on frames that keep one after the whole figure's motion; a frame with no hole in place is read with no further cost. 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), `rife.HOLE_THICK` (5), `rife.HOLE_SOURCE_THICK` (3), `rife.HOLE_PAIR_AREA` (4.0), `rife.HOLE_PAIR_SHAPE` (2.0), `rife.HOLE_AREA` (0.25), `rife.HOLE_SHAPE` (1.3), `rife.HOLE_PATH` (0.1), and `rife.HOLE_COLOUR` (0.12); 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, carried by the whole figure's motion, or paired with one hole of each source frame as 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, carried or paired, 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. A frame whose hole both source frames have, carried by a part (moving over a body that stays, plain or patterned, turning, growing, sheared or swinging within reach), is delivered as in 3.1.0 with the same bytes. Figure 5: a body that stays, patterned, with a part on it and a hole through the part's middle, the part moved from one source frame to the next, each frame between drawn exactly at its place. (i) The checkpoint validator's frame: a body 200 by 180 px with stripes 3 px wide every 6 px (a lighter colour by 40 levels), on a 260 by 220 px canvas, a part 24 px square with a hole 10 px square, moved 32 px across: the in-place reading leaves the hole whole, 100 pixels or 0.28 % of the body, and the whole figure's motion is 0; it is one hole of each source frame, alike and on its way, so it is paired: `hole` 0, and both made cells are kept as made, byte for byte. (ii) A body 240 by 260 px on a 300 by 300 px canvas in five patterns (stripes 3, 2 and 6 px wide every 6, 4 and 12 px running up and down; stripes 3 px wide every 6 px running across; checks 4 px square) at contrasts 15, 40 and 120, parts 24, 40 and 60 px square with holes 10, 17 and 25 px square: moved 32 and 48 px across and diagonally at t 0.5 (the moves listed in the test), these frames, and the same three parts moved 16, 32 and 48 px across and diagonally at t 0.25 and 0.75 over the 3 px stripes at contrast 40, every one read `hole` 0. Resampled to eight frames (t 0.25, 0.5 and 0.75 each way), parts of 24, 40 and 60 px moved 32 px diagonally and the 24 px part moved 48 px over stripes at contrast 120 keep every made frame, byte for byte. Figure 6: a torso 120 by 260 px that stays, patterned in blocks 4 px square each lighter by its own whole number up to 200 levels, with an arm beside its right edge: the upper arm and the forearm lie along that edge and are joined by the arm's outer side, so the gap between arm and torso, 16 by 12 px, has the torso's edge on its left and the arm on its other three sides; the arm reaches 48 px above and 52 px below the gap and moves 40 px down from one source frame to the next. The gap lies where the arm covers it in the other source frame, so the in-place reading leaves it, and the whole figure's motion is 0; it is one gap of each source frame, alike and on the arm's way, so it is paired: `hole` 0 at t 0.25, 0.5 and 0.75, and resampled to eight every made frame is kept, byte for byte. A frame with a gap only one source frame opens, on a part moving over a body that stays, plain or patterned, which neither the in-place reading nor the whole figure's motion carries and which has no hole in the other source frame to pair with, gives way to the nearer source frame where 3.1.0 kept it. The boundary, pinned by a test: a part 30 px square moving 32 px across a body that stays, a hole 14 px square open through it in one source frame and closed in the other, 8 px square in the frame between (half open): over a plain body and over the 3 px stripes alike it reads `hole` 0.10 % (its 64 pixels) and both made cells are given way to the nearer source frame, with `faults: ["hole"]`. Figure 7: the plate of the tests (a body 240 by 260 px that stays, striped or plain, with a plate on it and a gap through the plate, 11 by 31 px, each frame between drawn exactly at its place). The third checkpoint validator's frame, the plate turning 60 degrees as it moves 48 px over the stripes, reads `hole` 0 and is delivered as in 3.1.0, both made cells byte for byte, resampled to four and to eight; so are the plate turned 90 degrees, grown 2 times and sheared by 0.6 as it moves 48 px, and swung 45 degrees, over the stripes, and 125 such frames read `hole` 0. A frame between with a hole unlike both source frames' (the gap drawn 16 by 40 px: `hole` 1.04 %; drawn 18 by 18 px: 0.52 %; the plate growing 2.2 times as it moves 48 px, the source frames' gaps more than 4 times apart in area: 0.55 %), off its way (the plate drawn 12 px across it: 0.55 %; drawn where it is at t 0.8 in a frame made at t 0.5: 0.30 %), or out of reach (the plate swung 60 degrees, its centre moving 100 px, where 77.5 px is let through on this body: 0.55 %; the plate moving 80 px: 0.55 %) gives way to the nearer source frame with `hole` among its faults. Figure 8: the plate 120 by 40 px with a row of four holes 8 px square, 24 px apart, moving 32 px down: the frame between drawn exactly, and with the row a pixel taller, read `hole` 0 and are delivered as in 3.1.0; with one hole more (midway between two, between the last two, past the end, or one of the row moved to a midway) it reads 0.10 % and gives way to the nearer source frame. 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. Figure 5 (a body that stays, patterned, with a part on it and a hole through the part's middle, the part moved from one source frame to the next, each frame between drawn exactly at its place: the checkpoint validator's frame with a body 200 by 180 px with stripes 3 px wide every 6 px, a lighter colour by 40 levels, on a 260 by 220 px canvas, a part 24 px square with a hole 10 px square, moved 32 px across; and a body 240 by 260 px on a 300 by 300 px canvas in five patterns, stripes 3, 2 and 6 px wide every 6, 4 and 12 px running up and down, stripes 3 px wide every 6 px running across, and checks 4 px square, at contrasts 15, 40 and 120, parts 24, 40 and 60 px square with holes 10, 17 and 25 px square): figure 5's validator frame resampled to four, and its parts of 24, 40 and 60 px moved 32 px diagonally and the 24 px part moved 48 px over stripes at contrast 120 resampled to eight, keep every made frame as made; the wide grid of figure 5 (every pattern and contrast at each listed move at t 0.5, and the three parts moved 16, 32 and 48 px at t 0.25 and 0.75) reads `hole` 0 and is marked slow, so it runs with `pytest --full`. Figure 6 (a torso 120 by 260 px that stays, patterned in blocks 4 px square each lighter by its own whole number up to 200 levels, with an arm beside its right edge: the upper arm and the forearm lie along that edge and are joined by the arm's outer side, so the gap between arm and torso, 16 by 12 px, has the torso's edge on its left and the arm on its other three sides; the arm reaches 48 px above and 52 px below the gap and moves 40 px down from one source frame to the next) reads `hole` 0 at t 0.25, 0.5 and 0.75 and keeps every made frame resampled to eight. The boundary test (a part 30 px square moving 32 px across a body that stays, a hole 14 px square open through it in one source frame and closed in the other, 8 px square in the frame between) checks that the half-open gap on the moving part, over a plain body and over the stripes, reads its 64 pixels as a share of the body and gives both made cells way to the nearer source frame with `faults: ["hole"]`. Figure 7: a body 240 by 260 px on a 300 by 300 px canvas that stays, in stripes 3 px wide every 6 px (lighter by 40 levels) or plain, with a plate on it (|u| < 24 and |v| < 28 px in its own axes) and a gap through the plate (|u| < 6, |v| < 16), each frame between drawn exactly at its place in one motion: the plate's centre moving across as the plate turns, grows or is sheared about it, or the plate swinging from a point 100 px above its centre. One test checks that the third checkpoint validator's frame (the plate turning 60 degrees as it moves 48 px over the stripes), resampled to four, keeps both made frames as made, and resampled to eight (t 0.25, 0.5 and 0.75 each way) every one; that the plate turned 90 degrees, grown 2 times and sheared by 0.6 as it moves 48 px, and swung 45 degrees, resampled to four over the stripes, keep both made frames; and that over the plain body each of these reads `hole` 0. A test marked slow (it runs with `pytest --full`) checks that 125 frames read `hole` 0: the plate turned 0 to 90 degrees, grown 1.1 to 2 times or sheared by 0.15 to 0.6 as it moves 0, 24 or 48 px, or swung 10 to 45 degrees, over both bodies at t 0.5, and turned as it moves over the stripes at t 0.25. A boundary test checks that, on the plate over either body, a frame between with its gap drawn 16 by 40 px or 18 by 18 px, with the plate drawn 12 px across its way or where it is at t 0.8 in a frame made at t 0.5, with the plate growing 2.2 times as it moves 48 px (the source frames' gaps more than 4 times apart in area), with the plate moving 80 px, and with the plate swung 60 degrees, gives both made cells way to the nearer source frame with `hole` among their faults. Figure 8: the same body with a plate 120 by 40 px on it and a row of four holes 8 px square, 24 px apart, through the plate. Its test checks that, the plate moving 32 px down over either body, the frame between drawn exactly and the same with the row a pixel taller keep both made frames as made; that with one hole more midway between two of the row's, between the last two, past the row's end, or with one of the row's moved 12 px to a midway, both made cells give way to the nearer source frame with `hole` among their faults; and that, the plate standing still, the row drawn a pixel taller keeps both made frames, while with one hole more midway as well both give way.
+- Python callers: new `rife.hole(made, a, b, t)`, `rife.HOLE_TRAVEL` (0.2), `rife.HOLE_COARSE` (24), `rife.HOLE_REACH` (4), `rife.HOLE_THICK` (5), `rife.HOLE_SOURCE_THICK` (3), `rife.HOLE_PAIR_AREA` (4.0), `rife.HOLE_PAIR_SHAPE` (2.0), `rife.HOLE_AREA` (0.25), `rife.HOLE_SHAPE` (1.3), `rife.HOLE_PATH` (0.1), `rife.HOLE_COLOUR` (0.12), and `rife.HOLE_PAIR_STEPS` (5000); 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, carried by the whole figure's motion, or paired with one hole of each source frame as 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, carried or paired, 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. A frame whose hole both source frames have, carried by a part (moving over a body that stays, plain or patterned, turning, growing, sheared or swinging within reach), is delivered as in 3.1.0 with the same bytes. Figure 5: a body that stays, patterned, with a part on it and a hole through the part's middle, the part moved from one source frame to the next, each frame between drawn exactly at its place. (i) The checkpoint validator's frame: a body 200 by 180 px with stripes 3 px wide every 6 px (a lighter colour by 40 levels), on a 260 by 220 px canvas, a part 24 px square with a hole 10 px square, moved 32 px across: the in-place reading leaves the hole whole, 100 pixels or 0.28 % of the body, and the whole figure's motion is 0; it is one hole of each source frame, alike and on its way, so it is paired: `hole` 0, and both made cells are kept as made, byte for byte. (ii) A body 240 by 260 px on a 300 by 300 px canvas in five patterns (stripes 3, 2 and 6 px wide every 6, 4 and 12 px running up and down; stripes 3 px wide every 6 px running across; checks 4 px square) at contrasts 15, 40 and 120, parts 24, 40 and 60 px square with holes 10, 17 and 25 px square: moved 32 and 48 px across and diagonally at t 0.5 (the moves listed in the test), these frames, and the same three parts moved 16, 32 and 48 px across and diagonally at t 0.25 and 0.75 over the 3 px stripes at contrast 40, every one read `hole` 0. Resampled to eight frames (t 0.25, 0.5 and 0.75 each way), parts of 24, 40 and 60 px moved 32 px diagonally and the 24 px part moved 48 px over stripes at contrast 120 keep every made frame, byte for byte. Figure 6: a torso 120 by 260 px that stays, patterned in blocks 4 px square each lighter by its own whole number up to 200 levels, with an arm beside its right edge: the upper arm and the forearm lie along that edge and are joined by the arm's outer side, so the gap between arm and torso, 16 by 12 px, has the torso's edge on its left and the arm on its other three sides; the arm reaches 48 px above and 52 px below the gap and moves 40 px down from one source frame to the next. The gap lies where the arm covers it in the other source frame, so the in-place reading leaves it, and the whole figure's motion is 0; it is one gap of each source frame, alike and on the arm's way, so it is paired: `hole` 0 at t 0.25, 0.5 and 0.75, and resampled to eight every made frame is kept, byte for byte. A frame with a gap only one source frame opens, on a part moving over a body that stays, plain or patterned, which neither the in-place reading nor the whole figure's motion carries and which has no hole in the other source frame to pair with, gives way to the nearer source frame where 3.1.0 kept it. The boundary, pinned by a test: a part 30 px square moving 32 px across a body that stays, a hole 14 px square open through it in one source frame and closed in the other, 8 px square in the frame between (half open): over a plain body and over the 3 px stripes alike it reads `hole` 0.10 % (its 64 pixels) and both made cells are given way to the nearer source frame, with `faults: ["hole"]`. Figure 7: the plate of the tests (a body 240 by 260 px that stays, striped or plain, with a plate on it and a gap through the plate, 11 by 31 px, each frame between drawn exactly at its place). The third checkpoint validator's frame, the plate turning 60 degrees as it moves 48 px over the stripes, reads `hole` 0 and is delivered as in 3.1.0, both made cells byte for byte, resampled to four and to eight; so are the plate turned 90 degrees, grown 2 times and sheared by 0.6 as it moves 48 px, and swung 45 degrees, over the stripes, and 125 such frames read `hole` 0. A frame between with a hole unlike both source frames' (the gap drawn 16 by 40 px: `hole` 1.04 %; drawn 18 by 18 px: 0.52 %; the plate growing 2.2 times as it moves 48 px, the source frames' gaps more than 4 times apart in area: 0.55 %), off its way (the plate drawn 12 px across it: 0.55 %; drawn where it is at t 0.8 in a frame made at t 0.5: 0.30 %), or out of reach (the plate swung 60 degrees, its centre moving 100 px, where 77.5 px is let through on this body: 0.55 %; the plate moving 80 px: 0.55 %) gives way to the nearer source frame with `hole` among its faults. Figure 8: the plate 120 by 40 px with a row of four holes 8 px square, 24 px apart, moving 32 px down: the frame between drawn exactly, and with the row a pixel taller, read `hole` 0 and are delivered as in 3.1.0; with one hole more (midway between two, between the last two, past the end, or one of the row moved to a midway) it reads 0.10 % and gives way to the nearer source frame. Figure 9: on the same striped body, a plate with a row of two, three or four holes 8 px square, 12 px apart, moving on a curved way, int(40 t + 20 t (1 - t)) px across and int(32 t) px down at time t, the plate moving with the holes or staying while they move through it (with two holes it is the fourth checkpoint validator's frame): the frame between drawn exactly reads `hole` 0 and is delivered as in 3.1.0, both made cells byte for byte, for two, three and four holes, the plate moving or staying; with one hole more before the row, or with the row's last hole shut in the second source frame, it reads 0.10 % and gives way to the nearer source frame. Figure 10: on the same striped body, a plate 40 by 90 px with a column of three holes 8 px square, 24 px apart, moving 32 px down, the middle hole shut in the second source frame: the frame between drawn exactly (one hole more than the second source frame has) reads `hole` 0.10 % and gives way to the nearer source frame, where 3.1.0 delivered it. 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. Figure 5 (a body that stays, patterned, with a part on it and a hole through the part's middle, the part moved from one source frame to the next, each frame between drawn exactly at its place: the checkpoint validator's frame with a body 200 by 180 px with stripes 3 px wide every 6 px, a lighter colour by 40 levels, on a 260 by 220 px canvas, a part 24 px square with a hole 10 px square, moved 32 px across; and a body 240 by 260 px on a 300 by 300 px canvas in five patterns, stripes 3, 2 and 6 px wide every 6, 4 and 12 px running up and down, stripes 3 px wide every 6 px running across, and checks 4 px square, at contrasts 15, 40 and 120, parts 24, 40 and 60 px square with holes 10, 17 and 25 px square): figure 5's validator frame resampled to four, and its parts of 24, 40 and 60 px moved 32 px diagonally and the 24 px part moved 48 px over stripes at contrast 120 resampled to eight, keep every made frame as made; the wide grid of figure 5 (every pattern and contrast at each listed move at t 0.5, and the three parts moved 16, 32 and 48 px at t 0.25 and 0.75) reads `hole` 0 and is marked slow, so it runs with `pytest --full`. Figure 6 (a torso 120 by 260 px that stays, patterned in blocks 4 px square each lighter by its own whole number up to 200 levels, with an arm beside its right edge: the upper arm and the forearm lie along that edge and are joined by the arm's outer side, so the gap between arm and torso, 16 by 12 px, has the torso's edge on its left and the arm on its other three sides; the arm reaches 48 px above and 52 px below the gap and moves 40 px down from one source frame to the next) reads `hole` 0 at t 0.25, 0.5 and 0.75 and keeps every made frame resampled to eight. The boundary test (a part 30 px square moving 32 px across a body that stays, a hole 14 px square open through it in one source frame and closed in the other, 8 px square in the frame between) checks that the half-open gap on the moving part, over a plain body and over the stripes, reads its 64 pixels as a share of the body and gives both made cells way to the nearer source frame with `faults: ["hole"]`. Figure 7: a body 240 by 260 px on a 300 by 300 px canvas that stays, in stripes 3 px wide every 6 px (lighter by 40 levels) or plain, with a plate on it (|u| < 24 and |v| < 28 px in its own axes) and a gap through the plate (|u| < 6, |v| < 16), each frame between drawn exactly at its place in one motion: the plate's centre moving across as the plate turns, grows or is sheared about it, or the plate swinging from a point 100 px above its centre. One test checks that the third checkpoint validator's frame (the plate turning 60 degrees as it moves 48 px over the stripes), resampled to four, keeps both made frames as made, and resampled to eight (t 0.25, 0.5 and 0.75 each way) every one; that the plate turned 90 degrees, grown 2 times and sheared by 0.6 as it moves 48 px, and swung 45 degrees, resampled to four over the stripes, keep both made frames; and that over the plain body each of these reads `hole` 0. A test marked slow (it runs with `pytest --full`) checks that 125 frames read `hole` 0: the plate turned 0 to 90 degrees, grown 1.1 to 2 times or sheared by 0.15 to 0.6 as it moves 0, 24 or 48 px, or swung 10 to 45 degrees, over both bodies at t 0.5, and turned as it moves over the stripes at t 0.25. A boundary test checks that, on the plate over either body, a frame between with its gap drawn 16 by 40 px or 18 by 18 px, with the plate drawn 12 px across its way or where it is at t 0.8 in a frame made at t 0.5, with the plate growing 2.2 times as it moves 48 px (the source frames' gaps more than 4 times apart in area), with the plate moving 80 px, and with the plate swung 60 degrees, gives both made cells way to the nearer source frame with `hole` among their faults. Figure 8: the same body with a plate 120 by 40 px on it and a row of four holes 8 px square, 24 px apart, through the plate. Its test checks that, the plate moving 32 px down over either body, the frame between drawn exactly and the same with the row a pixel taller keep both made frames as made; that with one hole more midway between two of the row's, between the last two, past the row's end, or with one of the row's moved 12 px to a midway, both made cells give way to the nearer source frame with `hole` among their faults; and that, the plate standing still, the row drawn a pixel taller keeps both made frames, while with one hole more midway as well both give way. Figure 9: the striped body (240 by 260 px that stays, stripes 3 px wide every 6 px) with a plate on it and a row of two, three or four holes 8 px square, 12 px apart, through the plate, moving on a curved way from one source frame to the next, int(40 t + 20 t (1 - t)) px across and int(32 t) px down at time t; the plate moves with the holes or stays while they move through it. With two holes it is the fourth checkpoint validator's frame. One test checks that, resampled to four, the frame between drawn exactly keeps both made frames as made, for two, three and four holes, the plate moving or staying. Figure 10: the same striped body with a plate 40 by 90 px on it and a column of three holes 8 px square, 24 px apart, through the plate, the plate moving 32 px down from one source frame to the next. A boundary test checks that, with two and four holes, the plate moving or staying, a frame between with one hole more before the row, and a frame between whose second source frame has the row's last hole shut, give both made cells way to the nearer source frame with `hole` among their faults; that the frame between of figure 10, with the middle hole shut in the second source frame, gives both made cells way to the nearer source frame with `hole` among their faults; and that with `rife.HOLE_PAIR_STEPS` set to one step, the frame between of two holes gives both made cells way as well.
## 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
diff --git a/docs/loop-repair.md b/docs/loop-repair.md
index 2c9e812..38cc4d7 100644
--- a/docs/loop-repair.md
+++ b/docs/loop-repair.md
@@ -1519,20 +1519,35 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-
(`rife.HOLE_SHAPE`, 1.3), the colour round it within 0.12 of each, and its centroid within 4 px
plus 0.1 of their distance (`rife.HOLE_PATH`, 0.1) of the point at `t` on the line from the
centroid of a's hole to that of b's. Pairs are taken one to one: each source frame's hole
- explains one hole of the made frame. The made frame's holes the first two readings already give
- the source frames are paired first, then the rest; within each, the alikest and nearest first,
- by the sum of: the logarithm of how far the made frame's hole's area is out of the range, its
- distance from the point at `t` over what it may be, how far its shape is out, the colour
- differences of the made frame's hole and of the two source frames' holes each over 0.12, and the
- two source frames' holes' distance over what it may be; ties go by the made frame's hole, then
- a's, then b's, in reading order. So a hole the made frame keeps where it was explains no other
- hole, and in a row of alike holes moving, each hole of the made frame takes the two its own way
- brings rather than a hole of one source frame and the next but one of the other, whose midpoint
- lies as near. The made frame is never used to measure a motion and RIFE's flow is never read.
- The motion and the pairing are read only on frames keeping a hole after the in-place reading,
- and the pairing only on frames keeping one after the second 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.
+ explains one hole of the made frame. No hole of any frame is in two pairs taken; every hole of
+ the made frame that the first two readings (in place, and carried with the whole figure) already
+ give the source frames, and that has a pair at all, must be paired too. Of all sets pairing
+ those holes, the set taken pairs the most of the other holes; of those, the one with the least
+ summed cost. The same frames always give the same set. The cost of a pair is the sum of: the
+ logarithm of how far the made frame's hole's area is out of the range of the two source frames'
+ holes' areas, its distance from the point at `t` over what it may be, how far its shape is out,
+ the colour differences of the made frame's hole and of the two source frames' holes each over
+ 0.12, and the two source frames' holes' distance over what it may be. So a hole the made frame
+ keeps where it was explains no other hole, since it must be paired itself; and alike holes
+ moving together each keep their own two holes, however near their ways run. Pairs that share a
+ hole form a group, searched apart by an exact depth first search from the hole with the fewest
+ pairs still free, trying each pair, cheapest first (or leaving a hole not given by the first two
+ readings unpaired); groups whose holes the first two readings already give are not searched,
+ branches leaving one of those holes unpaired or unable to beat the best set found are cut, and
+ the search stops at a set pairing every hole. At most 5000 steps are taken per group
+ (`rife.HOLE_PAIR_STEPS`, 5000), past which a group pairs none of its holes and the frame gives
+ way to the nearer source frame; a row or grid of alike holes that all pair takes about a step
+ per hole (a grid of 100 holes, 101 to 106 steps), and on the synthetic frames only grids of 16
+ to 100 holes, 16 px apart, with one hole shut in one source frame, run past it, which give way
+ in any case. Where no set pairs every hole the first two readings give, one of the first two
+ readings' holes is then a hole more than the source frames have, which those readings took for
+ theirs because it lies within 4 px of where another hole is; then none of the other holes in
+ that group is paired (figure 10). The made frame is never used to measure a motion and RIFE's
+ flow is never read. The motion and the pairing are read only on frames keeping a hole after the
+ in-place reading, and the pairing only on frames keeping one after the second 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` (it says the
frame opened a transparent hole inside its outline over the given share of its body, at least 5
@@ -1662,15 +1677,46 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-
24 px apart, the plate moving 32 px down from one source frame to the next), the frame between
drawn exactly reads `hole` 0 and is kept as made; so is the frame between with the row drawn a
pixel taller, where a hole of one source frame and the next but one of the other have their
- midpoint as near each of the made frame's holes as its own two: the nearer pair is taken first,
- so each hole takes its own two. Drawn with one hole more, midway between two of the row's holes,
- or between the last two, or past the row's end, or with one of the row's holes moved 12 px to a
- midway, the frame between reads `hole` 0.10 % (the 64 pixels of one hole) and gives way to the
- nearer source frame: each source frame's hole explains one hole of the made frame. Standing
- still, with the row drawn a pixel taller in the frame between, it is kept; with one hole more
- midway as well, it reads 0.10 % and gives way: the in-place reading gives the row's holes to the
- source frames, and they are paired first, so the source frames' holes are taken by the holes
- they are.
+ midpoint as near each of the made frame's holes as its own two: the pairing takes the set that
+ pairs the most holes, so each hole keeps its own two. Drawn with one hole more, midway between
+ two of the row's holes, or between the last two, or past the row's end, or with one of the row's
+ holes moved 12 px to a midway, the frame between reads `hole` 0.10 % (the 64 pixels of one hole)
+ and gives way to the nearer source frame: each source frame's hole explains one hole of the made
+ frame. Standing still, with the row drawn a pixel taller in the frame between, it is kept; with
+ one hole more midway as well, it reads 0.10 % and gives way: the in-place reading gives the
+ row's holes to the source frames, and they are paired first, so the source frames' holes are
+ taken by the holes they are. In figure 9 (the same body with a plate on it and a row of two,
+ three or four holes 8 px square through the plate, 12 px apart, moving on a curved way from one
+ source frame (t 0) to the next (t 1): int(40 t + 20 t (1 - t)) px across and int(32 t) px down
+ at time t, the plate moving with the holes, or staying while the holes move through it; with two
+ holes it is the fourth checkpoint validator's frame), the frame between is drawn exactly, at t
+ 0.5: each of its holes lies 5 px beside the midpoint of where the two source frames have that
+ hole; 4 px and 0.1 of their distance (51 px), 9.1 px in all, are let through. The first hole
+ lies 1 px from the midpoint of the second hole in the first source frame and the first hole in
+ the second source frame: that pair of source holes is the cheaper for it, and taken, it would
+ leave the second hole with nothing to pair with. The set of pairs taken pairs the most holes, so
+ each hole keeps its own two: the frame between reads `hole` 0, and resampled to four frames,
+ both made cells are kept, byte for byte. This holds for two, three and four holes, the plate
+ moving or staying (with three and four holes, each hole but the last is likewise cheaper with
+ the next one's hole in the first source frame). Boundaries, pinned by a test, with two and four
+ holes, the plate moving or staying: with one hole more before the row, on the first hole's way
+ (it could take the first hole's two, but then the first and the second cannot both keep theirs:
+ each source frame's holes pair as many of the made frame's holes as they number, and no more),
+ the frame between reads `hole` 0.10 % (the 64 pixels of one hole) and gives way to the nearer
+ source frame; with the row's last hole shut in the second source frame (it has nothing there to
+ pair with), it reads 0.10 % and gives way. With the search limited to one step
+ (`rife.HOLE_PAIR_STEPS` set to 1), no set is found within it, so neither hole of the two-hole
+ frame is paired: it reads `hole` 0.21 % (both holes, 128 pixels) and gives way. In figure 10
+ (the same body with a plate 40 by 90 px on it and a column of three holes 8 px square through
+ the plate, 24 px apart, moving 32 px down, the middle hole shut in the second source frame), the
+ frame between is drawn at t 0.5, its holes 16 px down. Each lies against the next hole down in
+ the first source frame and the hole before it in the second: the in-place reading gives the
+ first and middle holes to the source frames, leaves of the first only a strip 4 px high, which
+ does not count, and leaves the last hole whole. The first hole pairs only with its own two and
+ the middle only with the first source frame's last hole and the second's first; sharing that
+ hole, no set pairs both, so one is a hole more than the second source frame has, none of the
+ other holes is paired, and the frame between reads `hole` 0.10 % and gives way to the nearer
+ source frame.
- **What `auto` costs**: a frame replaced shows the nearer source frame, so the motion there
steps as filmed, up to half a frame off its time, and the loop shows fewer distinct drawings a
second than `rife` — but no melted one. A clip drawn on twos has a long step between drawings
diff --git a/sprite_gen/video/rife.py b/sprite_gen/video/rife.py
index c01a192..2043919 100644
--- a/sprite_gen/video/rife.py
+++ b/sprite_gen/video/rife.py
@@ -124,9 +124,14 @@ HOLE_COARSE = 24
# and no farther apart than the figure's motion reaches, diagonally, and HOLE_REACH. The made frame's: its area within
# HOLE_AREA of their range, its shape within HOLE_SHAPE times of it, the colour round it within HOLE_COLOUR of each, and
# its centroid within HOLE_REACH and HOLE_PATH of their distance of the point at its time on the line from one to the
-# other. Each neighbour's hole explains one of the made frame's, the alikest and nearest taken first, and the made frame's
-# holes the first two readings give the neighbours before the rest: a neighbour's hole the made frame keeps where it was
-# explains no other.
+# other. Each neighbour's hole explains one of the made frame's: of the sets of such triples that can stand together
+# and pair every one of the made frame's holes the first two readings give the neighbours, the one taken pairs the most
+# of the rest, then is the alikest and nearest (`_taken`) — a neighbour's hole the made frame keeps where it was
+# explains no other, and alike holes moving together each keep their own two, however near their ways run. Where no
+# set pairs all of those, one of them is a hole more than the neighbours have that the first two readings took for
+# theirs, and none of the rest pairs. An exact search finds the set, each group of triples sharing holes apart, in at
+# most HOLE_PAIR_STEPS steps (a row or grid of alike holes that all pair takes about a step a hole); a group whose
+# search runs past them pairs none of its holes.
HOLE_SOURCE_THICK = 3
HOLE_PAIR_AREA = 4.0
HOLE_PAIR_SHAPE = 2.0
@@ -134,6 +139,7 @@ HOLE_AREA = 0.25
HOLE_SHAPE = 1.3
HOLE_PATH = 0.1
HOLE_COLOUR = 0.12
+HOLE_PAIR_STEPS = 5000
# `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'
@@ -624,19 +630,18 @@ def _holes(pre: np.ndarray, thick: int) -> tuple[list[tuple[tuple[slice, slice],
def _paired(made: np.ndarray, a: np.ndarray, b: np.ndarray, t: float, reach: int, first: np.ndarray) -> list[int]:
- """Which of the made frame's holes (rows of `_holes`; the frame at fraction `t` from `a` to `b`, the neighbours'
- holes) are one hole of each neighbour's. A triple is one of the made frame's holes and one of each neighbour's where
- the neighbours' two have areas within HOLE_PAIR_AREA times of each other, shapes within HOLE_PAIR_SHAPE times (on
- each moment) and the colours round them within HOLE_COLOUR (on each channel), their centroids at most sqrt(2)
- `reach` + HOLE_REACH apart (`reach`: how far the figure's motion is followed each way); and the made frame's has its
- area within HOLE_AREA of their range, its shape within HOLE_SHAPE times of theirs on each moment, the colour round it
- within HOLE_COLOUR of each, and its centroid within HOLE_REACH + HOLE_PATH of their distance of the point at `t` on
- the line from theirs in a to theirs in b. Triples are taken one to one: those of the holes `first` names before the
- rest, the alikest and nearest first — the logarithm of how far its area is out of their range, its distance from that
+ """Which of the made frame's holes outside `first` (rows of `_holes`; the frame at fraction `t` from `a` to `b`, the
+ neighbours' holes) are one hole of each neighbour's. A triple is one of the made frame's holes and one
+ of each neighbour's where the neighbours' two have areas within HOLE_PAIR_AREA times of each other, shapes within
+ HOLE_PAIR_SHAPE times (on each moment) and the colours round them within HOLE_COLOUR (on each channel), their
+ centroids at most sqrt(2) `reach` + HOLE_REACH apart (`reach`: how far the figure's motion is followed each way);
+ and the made frame's has its area within HOLE_AREA of their range, its shape within HOLE_SHAPE times of theirs on
+ each moment, the colour round it within HOLE_COLOUR of each, and its centroid within HOLE_REACH + HOLE_PATH of
+ their distance of the point at `t` on the line from theirs in a to theirs in b. Its cost, how far the three are
+ from alike and near: the logarithm of how far the made frame's area is out of their range, its distance from that
point over what it may be, how far its shape is out of theirs, the two colour differences over HOLE_COLOUR and the
- neighbours' two holes' distance over what it may be, summed — then by the made frame's hole, then a's, then b's, in
- order: of a row of alike holes moving, each of the made frame's takes the two its own way brings, not a hole of one
- neighbour and the next but one of the other, whose midpoint is as near."""
+ neighbours' two holes' distance over what it may be, summed. The triples taken are one to one and pair every hole
+ `first` names (`_taken`)."""
if not (len(made) and len(a) and len(b)):
return []
A, B = a[:, None], b[None, :]
@@ -659,13 +664,90 @@ def _paired(made: np.ndarray, a: np.ndarray, b: np.ndarray, t: float, reach: int
cost = np.abs(np.log(area)) + path + shape + alike / HOLE_COLOUR + colour / HOLE_COLOUR + near
for p in np.nonzero((area >= 1 - HOLE_AREA) & (area <= 1 + HOLE_AREA) & (path <= 1)
& (shape <= np.log(HOLE_SHAPE)) & (alike <= HOLE_COLOUR))[0]:
- triples.append((not first[i], float(cost[p]), i, int(j[p]), int(k[p])))
- used: tuple[set[int], set[int], set[int]] = (set(), set(), set())
- for _, _, *triple in sorted(triples):
- if all(x not in seen for x, seen in zip(triple, used)):
- for x, seen in zip(triple, used):
- seen.add(x)
- return sorted(used[0])
+ triples.append((i, int(j[p]), int(k[p]), float(cost[p])))
+ return _taken(triples, [bool(x) for x in first])
+
+
+def _taken(triples: list[tuple[int, int, int, float]], first: list[bool]) -> list[int]:
+ """The made frame's holes outside `first` that the triples taken pair, one to one: no hole of any frame in two of
+ them. Each hole `first` names, the neighbours' already, is one hole of each neighbour's too: of the sets of
+ `triples` (the made frame's hole, a's, b's and the cost; `_paired`) that can stand together and pair every one of
+ those, the one taken pairs the most of the rest, then has the least summed cost. So a neighbour's hole the made
+ frame keeps where it was explains no other, and alike holes moving together each keep their own two, however near
+ their ways run. Triples sharing a hole, directly or through others, make one group, read apart from the rest
+ (`_searched`); a group with no hole outside `first` changes nothing and is not read."""
+ owner: dict[tuple[int, int], tuple[int, int]] = {}
+
+ def root(x: tuple[int, int]) -> tuple[int, int]:
+ while owner.get(x, x) != x:
+ x = owner[x]
+ return x
+
+ for i, j, k, _ in triples:
+ for x in ((1, j), (2, k)):
+ if (r := root(x)) != (s := root((0, i))):
+ owner[r] = s
+ groups: dict[tuple[int, int], dict[int, list[tuple[float, int, int]]]] = {}
+ for i, j, k, cost in sorted(triples, key=lambda x: (x[3], x[1], x[2], x[0])):
+ groups.setdefault(root((0, i)), {}).setdefault(i, []).append((cost, 1 << j, 1 << k))
+ taken: list[int] = []
+ for cands in groups.values():
+ if not all(first[i] for i in cands):
+ taken += _searched(cands, first)
+ return sorted(taken)
+
+
+def _searched(cands: dict[int, list[tuple[float, int, int]]], first: list[bool]) -> list[int]:
+ """Of one group of triples (each of its made frame's holes with its triples, cheapest first: the cost, and a's hole
+ and b's as bits; `_taken`), the holes outside `first` the set taken pairs, by an exact search, depth first: from the
+ made frame's hole with the fewest triples still free (holding no hole a triple taken holds), each of those,
+ cheapest first, then, for a hole outside `first`, the hole left unpaired. The most a branch could still pair: each
+ hole left with a free triple, and in each neighbour no more than the holes the free triples hold there. A branch is
+ cut where that leaves a hole `first` names unpaired, or pairs fewer of the rest than the best set found, or as many
+ with no less cost than the best's, even were each hole of `first` and each of as many of the rest as it takes, the
+ cheapest, paired by its cheapest free triple. The search ends at a set pairing every hole, since every such set
+ pairs the same holes. Where no set pairs every hole `first` names, one of them is a hole more than the neighbours
+ have that the first two readings took for theirs, and none of the rest is paired; nor where the search runs past
+ HOLE_PAIR_STEPS steps (docs/loop-repair.md section 4)."""
+ owed = sum(first[i] for i in cands)
+ best: tuple[int, float, tuple[int, ...]] = (-1, 0.0, ())
+ stack = [(tuple(sorted(cands)), 0, 0, 0, 0, 0.0, ())]
+ for _ in range(HOLE_PAIR_STEPS):
+ if not stack:
+ break
+ holes, used_a, used_b, got_first, got_rest, cost, pairs = stack.pop()
+ free, cheapest, held = {}, ([], []), [0, 0, 0, 0] # held: a's and b's holes as bits, `first`'s, the rest's
+ for i in holes:
+ if f := [x for x in cands[i] if not (used_a & x[1] or used_b & x[2])]:
+ free[i] = f
+ side = 2 * (not first[i])
+ cheapest[side // 2].append(f[0][0])
+ for _, ja, kb in f:
+ held[side] |= ja
+ held[side + 1] |= kb
+ in_a, in_b = (held[0] | held[2]).bit_count(), (held[1] | held[3]).bit_count()
+ more_first = min(len(cheapest[0]), held[0].bit_count(), held[1].bit_count(), in_a, in_b)
+ more_rest = min(len(cheapest[1]), held[2].bit_count(), held[3].bit_count(), in_a - more_first,
+ in_b - more_first)
+ could = got_rest + more_rest
+ if got_first + more_first < owed or could < best[0] or could == best[0] and (
+ cost + sum(cheapest[0]) + sum(sorted(cheapest[1])[:more_rest]) >= best[1]):
+ continue
+ if not free:
+ best = (got_rest, cost, pairs)
+ if got_first + got_rest == len(cands):
+ break
+ continue
+ h = min(free, key=lambda i: (len(free[i]), i))
+ others = tuple(i for i in free if i != h)
+ if not first[h]:
+ stack.append((others, used_a, used_b, got_first, got_rest, cost, pairs))
+ stack.extend((others, used_a | ja, used_b | kb, got_first + first[h], got_rest + (not first[h]), cost + c,
+ pairs + (h,)) for c, ja, kb in reversed(free[h]))
+ else:
+ if stack:
+ return []
+ return [i for i in best[2] if not first[i]]
def hole(made: Image.Image, a: Image.Image, b: Image.Image, t: float) -> float:
diff --git a/tests/video/test_inbetween_hole.py b/tests/video/test_inbetween_hole.py
index 5b0fc23..5ace4c8 100644
--- a/tests/video/test_inbetween_hole.py
+++ b/tests/video/test_inbetween_hole.py
@@ -9,9 +9,11 @@ frames' own where either has a pixel under solid within 4 px of where it lay, in
motion — a gap kept, moved with the body, or opened by a step between two legs — or where it is, whatever carried it,
one hole of each source frame: alike in area, in shape and in the colour round it, and on its way between theirs. So a
hole a part carries over a body that stays, turned, grown, sheared or swung, is theirs. Each source frame's hole is one
-hole of the frame between: a hole more than they have is given way, and so is a hole unlike theirs, off its way or
-farther than the figure's motion is followed, and a gap only one source frame opens on a part the figure's motion does
-not carry, which the reading does not tell from a band the coverage run carried out of one source frame's gap.
+hole of the frame between: the holes the first two readings give them must all pair, and of the sets that pair them
+the one taken pairs the most of the rest, so alike holes whose ways run near each other each keep their own two. A hole
+more than they have is given way, and so is a hole unlike theirs, off its way or farther than the figure's motion is
+followed, and a gap only one source frame opens on a part the figure's motion does not carry, which the reading does
+not tell from a band the coverage run carried out of one source frame's gap.
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
@@ -19,7 +21,8 @@ second with a gap between them, as a step opens it; a gap between the arm and th
a gap through it, moved whole; a figure whose legs part above boots that close the gap below, still and moving; a body
that stays with a part on it, a hole through the part, moved; the same over a body patterned in stripes or checks; a
torso patterned in blocks with an arm beside it, the gap between them moving with the arm; a plate with a gap through it,
-turned, grown, sheared or swung over a body that stays; and a plate with a row of holes."""
+turned, grown, sheared or swung over a body that stays; a plate with a row of holes; a row of holes moving on a curved
+way; and a column of holes."""
from __future__ import annotations
@@ -223,6 +226,39 @@ def _row(t: float, *, plain: bool = False, d: int = 0, dy: int = 0, more: tuple[
return Image.fromarray(rgba, "RGBA")
+def _way(t: float, n: int = 2, *, still: bool = False, more: tuple[int, ...] = (), shut: tuple[int, ...] = ()) -> Image.Image:
+ """The body that stays (`_still`) with a plate on it and a row of n holes 8 px square through it, 12 px apart, moving
+ on a curved way from t 0 to t 1, int(40 t + 20 t (1 - t)) px across and int(32 t) px down — at t 0.5 each hole lies
+ 5 px beside the midpoint of where the source frames have it — drawn at t: the plate moving with them, or `still` and
+ wide enough for their whole way; holes 8 px square more at the places in `more` (-1 before the first), and the row's
+ holes at the places in `shut` left solid. With two holes it is the fourth checkpoint validator's frame."""
+ rgba = _still(False)
+ dx, dy = int(40 * t + 20 * t * (1 - t)), int(32 * t)
+ if still:
+ rgba[95:185, 85:200] = PART + (255,)
+ else:
+ rgba[90 + dy:160 + dy, 85 + dx:150 + 12 * (n - 2) + dx] = PART + (255,)
+ for place in [p for p in range(n) if p not in shut] + list(more):
+ x, y = 110 + 12 * place + dx, 124 + dy
+ rgba[y - 4:y + 4, x - 4:x + 4] = 0
+ return Image.fromarray(rgba, "RGBA")
+
+
+def _column(t: float, *, shut: tuple[int, ...] = ()) -> Image.Image:
+ """The body that stays (`_still`) with a plate 40 x 90 px on it and a column of three holes 8 px square through it,
+ 24 px apart, the plate moving 32 px down from t 0 to t 1, drawn at t; the holes at the places in `shut` left solid.
+ At t 0.5 each hole lies against the next hole of the first source frame and the one before it of the second: the
+ in-place reading gives the middle hole those two, and leaves of the first and the last a strip thinner than
+ rife.HOLE_THICK."""
+ rgba = _still(False)
+ cy = int(round(140 + 32 * (t - 0.5)))
+ rgba[cy - 45:cy + 45, 130:170] = PART + (255,)
+ for place in (p for p in range(3) if p not in shut):
+ y = cy + 24 * (place - 1)
+ rgba[y - 4:y + 4, 146:154] = 0
+ return Image.fromarray(rgba, "RGBA")
+
+
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))
@@ -514,12 +550,12 @@ def test_a_hole_unlike_the_source_frames_or_off_its_way_or_out_of_reach_is_given
def test_each_hole_of_a_source_frame_is_one_hole_of_the_frame_between():
"""A plate with a row of four holes (`_row`) moves 32 px down over a body that stays, plain or striped: the frame
between drawn exactly is kept as made, and so is one with the row drawn a pixel taller, where a hole of one source
- frame and the next but one of the other have their midpoint as near each hole as its own two: the nearer pair is
- taken first. Drawn with one hole more midway between two of the row's — where one source frame's hole and the next
- one of the other would put it — or past the row's end, or with one of the row's moved 12 px to a midway, it gives way
- to the nearer source frame: each source frame's hole is one hole of the frame between, and each of the row's is taken
- by the hole it is. Standing still, the row drawn a pixel taller: the in-place reading gives the row its source frames'
- holes, which are paired with it before the hole more, so one more hole midway still gives way."""
+ frame and the next but one of the other have their midpoint as near each hole as its own two: only with its own two
+ is every hole of the row paired. Drawn with one hole more midway between two of the row's — where one source frame's
+ hole and the next one of the other would put it — or past the row's end, or with one of the row's moved 12 px to a
+ midway, it gives way to the nearer source frame: each source frame's hole is one hole of the frame between. Standing
+ still, the row drawn a pixel taller: the in-place reading gives the row its source frames' holes, and every hole of
+ the row must pair, which leaves one more hole midway none, so it still gives way."""
seen = {}
for plain in (False, True):
a, b = _row(0.0, plain=plain, dy=32), _row(1.0, plain=plain, dy=32)
@@ -534,3 +570,40 @@ def test_each_hole_of_a_source_frame_is_one_hole_of_the_frame_between():
seen["still, a pixel taller", plain] = _resampled(still, _row(0.5, plain=plain, taller=1), still) == KEPT
seen["still, a pixel taller, one more midway", plain] = _given_way(still, _row(0.5, plain=plain, taller=1, more=(0,)), still)
assert seen == {k: True for k in seen}
+
+
+def test_alike_holes_whose_ways_run_near_each_keep_their_own_two():
+ """The fourth checkpoint validator's frame (`_way`): two alike holes 12 px apart move on a curved way over a body
+ that stays, with the plate or through a plate that stays, and the frame between is drawn exactly, each hole 5 px
+ beside the midpoint of its own two (4 px and 0.1 of their 51 px are let through). The first hole lies 1 px from the
+ midpoint of the second's hole in the first source frame and its own in the second: that triple is the cheaper, and
+ taken first it would leave the second hole nothing to pair with. The set of triples taken pairs the most holes, so
+ each hole keeps its own two: resampled to four, both frames made are kept, byte for byte, and so are three and four
+ such holes, each but the last cheapest with the next one's hole in the first source frame."""
+ seen = {(n, still): _resampled(*(_way(s, n, still=still) for s in (0.0, 0.5, 1.0))) for n in (2, 3, 4)
+ for still in (False, True)}
+ assert seen == {k: KEPT for k in seen}
+
+
+def test_holes_no_set_of_pairs_explains_together_give_way(monkeypatch):
+ """The boundaries on the same way (`_way`), two and four holes, the plate moving or still. One hole more before the
+ row, on the first hole's way: it could take the first hole's two, but the first and the second cannot then both
+ keep theirs — each source frame's holes pair as many of the made frame's as they number and no more. And the row's
+ last hole shut in the second source frame: it has nothing there to pair with. Both give way to the nearer source
+ frame with `hole` among their faults. So does a column of three holes moving down (`_column`), its middle hole shut
+ in the second source frame: the in-place reading gives the frame between's first and middle holes to the source
+ frames, but no set of pairs pairs both — the middle one's only pair, the holes it lies against, holds the second
+ source frame's first hole, the first one's own — so one of them is a hole more than the second source frame has,
+ and the last hole, which its own two would pair, is not paired. Searched no further than rife.HOLE_PAIR_STEPS
+ steps, here one, the frame of the test above gives way too: a group of triples whose search runs past it pairs none
+ of its holes."""
+ seen = {}
+ for n, still in itertools.product((2, 4), (False, True)):
+ seen["one more before", n, still] = _given_way(_way(0.0, n, still=still), _way(0.5, n, still=still, more=(-1,)),
+ _way(1.0, n, still=still))
+ seen["the last only in the first", n, still] = _given_way(_way(0.0, n, still=still), _way(0.5, n, still=still),
+ _way(1.0, n, still=still, shut=(n - 1,)))
+ seen["a column, its middle hole shut in the second"] = _given_way(_column(0.0), _column(0.5), _column(1.0, shut=(1,)))
+ monkeypatch.setattr(rife, "HOLE_PAIR_STEPS", 1)
+ seen["searched one step"] = _given_way(*(_way(s) for s in (0.0, 0.5, 1.0)))
+ assert seen == {k: True for k in seen}