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): a hole both source frames have, carried by a part over a patterned body that stays, is theirs where one motion within reach carries it in both and fits the made frame's coverage round it — for each piece the three readings leave, rife.hole judges every motion within rife.HOLE_TRAVEL: each source frame, moved by its share, must hold rife.HOLE_CARRY 0.9 of the piece within 4 px of its transparency and cover the band 5 to 8 px round it as the made frame does on all but rife.HOLE_AGREE 0.05, every motion counted at once by FFT correlation; the made frame judges motions and never measures one, RIFE's flow is never read; the validator's 24 px part moved 32 px over 3 px stripes, a grid of parts over patterned bodies and an arm moving 40 px beside a block-patterned torso are kept as made with their bytes, while a gap only one source frame opens on such a part, plain body or patterned, is given way, as the band through a leg is; a frame the three readings clear reads as before

Soohong Kim63a5eefb6cc4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 009dd4e..a16efbb 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 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.
+- **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 fourth reading runs only on a frame that still keeps a piece of hole after the third reading, and only on the pieces the first three leave (a frame the first three readings leave no piece of hole on reads what they give), added because one motion per box lets a patterned body that stays outweigh a small part moving over it. Over the box round the part's hole the body's stillness (or a motion that matches the stripes to themselves) fits better than the part's motion, so the third reading alone would miss a hole both source frames have and the part carries, and give a correct frame way. The fourth reading judges every motion in that range (every whole-pixel motion from minus to plus the reach on each axis) instead of one motion per box, reading a piece as the source frames' where both source frames carry it by one motion within the same reach of 20 % of the body's height each way (`rife.HOLE_TRAVEL`). For a motion, each source frame is moved by its share of it, and each moved source frame must (a) have at least 90 % of the piece (`rife.HOLE_CARRY`, 0.9) within 4 px (`rife.HOLE_REACH`) of its own pixels under solid, and (b) cover the band round the piece, the pixels from 5 to 8 px out from it (from `rife.HOLE_REACH` + 1 to twice `rife.HOLE_REACH`, a square neighbourhood), as the made frame covers it (solid meaning alpha 0.5 and over), differing on at most 5 % of the band (`rife.HOLE_AGREE`, 0.05). Of the motions that pass for both source frames, the one whose worse source frame differs least over the band is taken (then the one that carries more of the piece, then the smaller motion); the piece's pixels within 4 px of where either source frame has pixels under solid along that motion are the source frames', as in the other readings. The made frame is used only to judge each motion by its own coverage round its own hole; it is never used to measure a motion, RIFE's flow is never read, and the counts for every motion are computed at once per source frame by FFT correlation, rounded to whole counts. 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.
+- 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_CARRY` (0.9), and `rife.HOLE_AGREE` (0.05); 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. A frame whose hole both source frames have, carried by a part over a patterned body that stays, 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 first three readings leave the hole whole, 100 pixels or 0.28 % of the body (the whole figure's motion is 0 and the box round the hole fits a motion that lays the stripes on themselves); the fourth carries it: `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), the first three readings leave a hole in 59 of these 105 frames (0.16 % to 0.47 %); moved 16, 32 and 48 px across and diagonally at t 0.25 and 0.75 over the 3 px stripes at contrast 40, none. With the fourth reading every one reads `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 over the box round it the torso's stillness fits better than the arm's motion: the first three readings read 0.28 %, 0.55 % and 0.28 % at t 0.25, 0.5 and 0.75. The fourth carries it: `hole` 0, 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 the first readings do not carry, 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"]`. 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"]`.
## 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 1235d76..fa6a917 100644
--- a/docs/loop-repair.md
+++ b/docs/loop-repair.md
@@ -1503,10 +1503,27 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-
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.
+ to the pixel. A fourth reading is run only on a frame that still keeps a piece of hole after the
+ third reading, and only on those pieces (a frame keeping no piece of hole after the third
+ reading has the value the first three readings give): a piece is the source frames' where both
+ source frames carry it by one motion within the same reach (20 % of the body's height each way,
+ `rife.HOLE_TRAVEL`). Every motion in that range is judged (every whole-pixel motion from minus
+ to plus the reach on each axis), not one motion per box. Each source frame is moved by its share
+ of the motion, as above. Each moved source frame must (a) have at least 90 % of the piece
+ (`rife.HOLE_CARRY`, 0.9) within 4 px (`rife.HOLE_REACH`) of its own pixels under solid, and (b)
+ cover the band round the piece, the pixels from 5 to 8 px out from it (from `rife.HOLE_REACH` + 1
+ to twice `rife.HOLE_REACH`, a square neighbourhood), as the made frame covers it (solid meaning
+ alpha 0.5 and over), differing on at most 5 % of the band (`rife.HOLE_AGREE`, 0.05). Of the
+ motions that pass for both source frames, the one whose worse source frame differs least over the
+ band is taken (then the one that carries more of the piece, then the smaller motion); the piece's
+ pixels within 4 px of where either source frame has pixels under solid along that motion are the
+ source frames', as in the other readings. The made frame is used only to judge each motion, by
+ its own coverage round its own hole; it is never used to measure a motion, and RIFE's flow is
+ never read. The counts for every motion are computed at once per source frame by FFT correlation,
+ rounded to whole counts. 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` (it says the
frame opened a transparent hole inside its outline over the given share of its body, at least
@@ -1516,18 +1533,35 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-
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.
+ moves, lies beside where that frame has it in the same way. A hole both source frames have,
+ carried by a part over a patterned body that stays, is kept by the fourth reading: one motion
+ per box lets a patterned body that stays outweigh a small part moving over it (over the box round
+ the part's hole the body's stillness, or a motion that matches the stripes to themselves, fits
+ better than the part's motion, so the third reading alone would miss such a hole and give a
+ correct frame way), whereas the fourth reading judges every motion, and the part's motion carries
+ the hole in both source frames with the made frame covered round it as they are. 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 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.
+ gap a source frame has, in place or where the first three readings' motion 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. In the first three
+ readings, 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 is left to the fourth reading, which
+ keeps it only where both source frames carry it and the made frame is covered round it as they
+ are. The fourth reading keeps only what both source frames carry. A gap only one source frame
+ opens (closed in the other, half open in the frame between), on a part that moves over a body
+ that stays, far enough that the in-place reading misses it, is given way to the nearer source
+ frame, whether the body is plain or patterned: the whole figure stays; the box round the gap
+ fits a motion that does not carry it (on a plain body, a motion that lays the box on the body
+ away from the part; on a patterned body, the body's stillness); and no one motion carries it in
+ both source frames. The reading cannot tell such a gap from a band the coverage run carried out
+ of one source frame's gap into a limb: at one motion both are the same shape (one source frame
+ open, the made frame narrower), so both are given way. A half-open gap the whole figure carries
+ (figure 3) is kept by the second reading.
- 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`.
@@ -1543,9 +1577,39 @@ sprite-gen video-cycle-align --loop-dir set/front-walk/loop --loop-dir set/side-
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.
+ 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. In 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 first three readings leave
+ the hole whole, 100 pixels or 0.28 % of the body (the whole figure's motion is 0 and the box
+ round the hole fits a motion that lays the stripes on themselves); the fourth carries it:
+ `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), the first three readings
+ leave a hole in 59 of these 105 frames (0.16 % to 0.47 %); moved 16, 32 and 48 px across and
+ diagonally at `t` 0.25 and 0.75 over the 3 px stripes at contrast 40, none. With the fourth
+ reading every one reads `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. In 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 over the box
+ round it the torso's stillness fits better than the arm's motion: the first three readings read
+ 0.28 %, 0.55 % and 0.28 % at `t` 0.25, 0.5 and 0.75. The fourth carries it: `hole` 0, and
+ resampled to eight every made frame is kept, byte for byte. 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"]`.
- **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 197eb3e..c0d0931 100644
--- a/sprite_gen/video/rife.py
+++ b/sprite_gen/video/rife.py
@@ -15,8 +15,9 @@ neighbours' rank by rank, so a mid-tone line the flow only softened is not read
neighbours are outlined (`outlined`), and not on art painted without a line; `ghost` reads a band of
part coverage on a frame alone, filmed or made; `crossfade` reads two drawings cross-faded inside a
whole silhouette, a reading to look at, not a fault; `hole` reads a transparent hole a made frame opened
-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` runs
+inside its silhouette that neither neighbour has there, in place or carried by a motion the two show —
+the whole figure's, the one round the hole, or any that carries it in both and fits the made frame's
+coverage round it — where the two runs disagree inside a limb (docs/loop-repair.md section 4). `passes` runs
the 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.
@@ -60,6 +61,7 @@ import PIL
from PIL import Image, ImageFilter, PngImagePlugin
from sprite_gen._deps import np
+from sprite_gen.video.auto_motion import _correlate
from sprite_gen.video.frames import _components, _near
BINARY = "rife-ncnn-vulkan"
@@ -114,6 +116,15 @@ HOLE_REACH = 4
HOLE_THICK = 5
HOLE_TRAVEL = 0.2
HOLE_COARSE = 24
+# A piece of hole those motions leave is the neighbours' where both carry it by one motion within that reach, every motion
+# judged (`_carried`): one motion per box lets a patterned body that stays outweigh a small part moving over it, its hole
+# carried with it. Each neighbour, moved by its share of the motion, has HOLE_CARRY of the piece within HOLE_REACH of its
+# transparency and covers the band HOLE_REACH + 1 to 2 HOLE_REACH pixels round the piece as the made frame covers it, all
+# but HOLE_AGREE of the band; the made frame says only which motion fits round its own hole. A gap one neighbour alone
+# has fits no motion in both, so it stays: a band the coverage run carried out of one neighbour's gap into a limb, and as
+# well a gap only one neighbour opens, narrower in the made frame, on a part the first motions do not follow.
+HOLE_CARRY = 0.9
+HOLE_AGREE = 0.05
# `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'
@@ -502,6 +513,15 @@ def _pooled(x: np.ndarray, f: int) -> np.ndarray:
return x.reshape(x.shape[0] // f, f, x.shape[1] // f, f, x.shape[2]).mean(axis=(1, 3))
+def _padded(x: np.ndarray, box: tuple[int, int, int, int], pad: int) -> np.ndarray:
+ """`x` over `box` (y0, y1, x0, x1) and `pad` pixels round it each way; past the picture, zero."""
+ y0, y1, x0, x1 = box
+ out = np.zeros((y1 - y0 + 2 * pad, x1 - x0 + 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)
+ out[sy0 - y0 + pad:sy1 - y0 + pad, sx0 - x0 + pad:sx1 - x0 + pad] = x[sy0:sy1, sx0:sx1]
+ return out
+
+
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."""
@@ -518,12 +538,7 @@ def _match(pa: np.ndarray, pb: np.ndarray, t: float, box: tuple[int, int, int, i
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)
+ crops = [_padded(x, box, pad) for x in (pa, pb)]
best: tuple[float, int, int, int] | None = None
for vy in range(centre[0] - radius, centre[0] + radius + 1):
ay, by = _apart(vy, t)
@@ -570,6 +585,59 @@ def _bounds(mask: np.ndarray, margin: int = 0) -> tuple[int, int, int, int]:
max(0, int(xs.min()) - margin), min(mask.shape[1], int(xs.max()) + 1 + margin))
+def _pieces(mask: np.ndarray) -> list[np.ndarray]:
+ """The 8-connected pieces of `mask`, each a mask of its own."""
+ ys, starts, stops, pieces, _ = _components(mask, diagonal=True)
+ out = []
+ for piece in np.unique(pieces):
+ part = np.zeros_like(mask)
+ for y, x0, x1 in zip(ys[pieces == piece], starts[pieces == piece], stops[pieces == piece]):
+ part[y, x0:x1] = True
+ out.append(part)
+ return out
+
+
+def _alike(box: tuple[int, int, int, int], part: np.ndarray, band: np.ndarray, solid: np.ndarray, near: np.ndarray,
+ covered: np.ndarray, offsets: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
+ """One neighbour moved by each pair of `offsets` (rows, columns: where a pixel of the made frame lay in it), as a grid
+ over the two: the share of `part` within HOLE_REACH of its transparency (`near`), and the share of `band` where its
+ coverage (`covered`) is not the made frame's (`solid`). `part`, `band` and `solid` are the made frame's over `box`;
+ `near` and `covered` the neighbour's whole picture, past which it is neither. Each count is one correlation over
+ every offset at once (`_correlate`), rounded to the whole number it is."""
+ k = int(np.abs(offsets).max())
+ near_k, covered_k = (_padded(x, box, k).astype(np.float64) for x in (near, covered))
+ p, on, off = (x.astype(np.float64) for x in (part, band & solid, band & ~solid))
+ n = 2 * k + 1
+ carried = np.rint(_correlate(near_k, p, n, n))
+ differs = on.sum() - np.rint(_correlate(covered_k, on, n, n)) + np.rint(_correlate(covered_k, off, n, n))
+ at = np.ix_(offsets + k, offsets + k)
+ return carried[at] / p.sum(), differs[at] / max(1.0, on.sum() + off.sum())
+
+
+def _carried(part: np.ndarray, solid: np.ndarray, a: tuple[np.ndarray, np.ndarray], b: tuple[np.ndarray, np.ndarray],
+ t: float, reach: int) -> tuple[int, int] | None:
+ """The motion (rows, columns) within `reach` each way by which both neighbours carry `part`, a piece of the hole of a
+ made frame at fraction `t` whose solid pixels are `solid`; each neighbour given as its pixels within HOLE_REACH of its
+ transparency and its solid pixels. Moved by its share of the motion (`_apart`), each neighbour has HOLE_CARRY of the
+ piece within HOLE_REACH of its transparency and covers the band HOLE_REACH + 1 to 2 HOLE_REACH pixels round the
+ piece as the made frame does, all but HOLE_AGREE of it (`_alike`). Of those motions, the one whose worse neighbour
+ differs least over the band, then carries more of the piece, then is the smallest; None where there is none."""
+ box = _bounds(part, 2 * HOLE_REACH)
+ window = (slice(box[0], box[1]), slice(box[2], box[3]))
+ inside = part[window]
+ band = _near(inside, 2 * HOLE_REACH) & ~_near(inside, HOLE_REACH)
+ v = np.arange(-reach, reach + 1)
+ lay, lie = np.array([_apart(int(x), t) for x in v]).T # per motion along an axis, where a pixel lay in a and lies in b
+ (ca, da), (cb, db) = (_alike(box, inside, band, solid[window], near, covered, offsets)
+ for (near, covered), offsets in ((a, lay), (b, lie)))
+ carry, differs = np.minimum(ca, cb), np.maximum(da, db)
+ iy, ix = np.nonzero((carry >= HOLE_CARRY) & (differs <= HOLE_AGREE))
+ if iy.size == 0:
+ return None
+ k = np.lexsort((v[iy] ** 2 + v[ix] ** 2, -carry[iy, ix], differs[iy, ix]))[0]
+ return int(v[iy[k]]), int(v[ix[k]])
+
+
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
@@ -578,7 +646,9 @@ def hole(made: Image.Image, a: Image.Image, b: Image.Image, t: float) -> float:
(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 farther neighbour (max(t, 1 - t) of that reach), so the box holds a gap the neighbours carried there. A piece
+ those leave is theirs where both carry it by any one motion within that reach that the made frame's coverage round
+ the piece fits (`_carried`): a part moving over a patterned body that stays, which outweighs it in the box.
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
@@ -600,14 +670,16 @@ def hole(made: Image.Image, a: Image.Image, b: Image.Image, t: float) -> float:
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
+ for part in _pieces(left):
left &= ~(part & theirs(_travel(pa, pb, t, _bounds(part, margin), reach)))
left = _opened(left, HOLE_THICK)
+ if left.any():
+ for part in _pieces(left):
+ v = _carried(part, solid, (near_a, fa >= 0.5), (near_b, fb >= 0.5), t, reach)
+ if v is not None:
+ left &= ~(part & theirs(v))
+ left = _opened(left, HOLE_THICK)
return round(float(left.sum()) / int(solid.sum()), 5)
diff --git a/tests/video/test_inbetween_hole.py b/tests/video/test_inbetween_hole.py
index ec50a2d..548eb02 100644
--- a/tests/video/test_inbetween_hole.py
+++ b/tests/video/test_inbetween_hole.py
@@ -6,17 +6,24 @@ its coverage apart, and the coverage run can carry the gap one source frame has
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.
+with a part of it, or opened by a step between two legs — and a hole both source frames have, carried by a part over a
+patterned body that stays, is theirs too, though the body's stillness fits the box round it better than the part's
+motion. A gap only one source frame opens, on such a part, is given way: the reading does not tell it 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
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."""
+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; and
+a torso patterned in blocks with an arm beside it, the gap between them moving with the arm."""
from __future__ import annotations
+import itertools
+
import numpy as np
+import pytest
from PIL import Image, ImageDraw
from sprite_gen.video import align, rife
@@ -28,6 +35,13 @@ FAR, NEAR = (70, 52, 40, 255), (78, 58, 46, 255) # luma 56 and 62: under the da
SIDE_BY_SIDE, APART, BETWEEN = (50, 80), (45, 85), (47, 82) # the far and the near leg's left column
BAND = (58, 120, 66, 160) # inside the far leg between, 8 px wide: 8 px or more from anything under solid in either source
MOVES = (0, 4, 8, 12, 24, 32) # px the body with a gap moves from one source frame to the next
+DIM, PART = (45, 55, 60), (150, 90, 60) # the patterned body's darker colour (lighter by its contrast) and the part's
+PATTERNS = ((3, 6, "v"), (2, 4, "v"), (6, 12, "v"), (3, 6, "h"), (4, 8, "c")) # stripes wide, their period, which way
+CONTRASTS = (15, 40, 120)
+# A part's side, its hole's, the px it moves and whether diagonally: the moves at t 0.5 where one motion per box gives the
+# body's stillness the part's hole on some pattern above.
+OUTWEIGHED = (((24, 10), 32, 0), ((24, 10), 32, 1), ((24, 10), 48, 0), ((24, 10), 48, 1), ((40, 17), 32, 0),
+ ((40, 17), 32, 1), ((60, 25), 32, 1))
def _box(d: ImageDraw.ImageDraw, x0: int, y0: int, x1: int, y1: int, fill: tuple[int, ...]) -> None:
@@ -89,6 +103,70 @@ def _ring(dx: int) -> Image.Image:
return im
+def _patterned(d: ImageDraw.ImageDraw, box: tuple[int, int, int, int], stripe: int, period: int, way: str,
+ contrast: int) -> None:
+ """`box` (x0, y0, x1, y1, inclusive) in DIM with stripes `stripe` px wide every `period` px, up and down ("v") or
+ across ("h"), or checks `stripe` px square ("c"), or blocks `stripe` px square each lighter by its own whole number up
+ to `contrast` from one fixed sequence ("n"); the stripes and checks lighter by `contrast`."""
+ x0, y0, x1, y1 = box
+ d.rectangle(box, fill=DIM + (255,))
+ light = tuple(min(255, c + contrast) for c in DIM) + (255,)
+ if way == "v":
+ for x in range(x0, x1 + 1, period):
+ d.rectangle((x, y0, min(x1, x + stripe - 1), y1), fill=light)
+ elif way == "h":
+ for y in range(y0, y1 + 1, period):
+ d.rectangle((x0, y, x1, min(y1, y + stripe - 1)), fill=light)
+ elif way == "c":
+ for y in range(y0, y1 + 1, stripe):
+ for x in range(x0 + (y - y0) // stripe % 2 * stripe, x1 + 1, 2 * stripe):
+ d.rectangle((x, y, min(x1, x + stripe - 1), min(y1, y + stripe - 1)), fill=light)
+ else:
+ state = 11
+ for y in range(y0, y1 + 1, stripe):
+ for x in range(x0, x1 + 1, stripe):
+ state = (state * 1103515245 + 12345) % 2 ** 31
+ lift = (state >> 16) % (contrast + 1)
+ d.rectangle((x, y, min(x1, x + stripe - 1), min(y1, y + stripe - 1)),
+ fill=tuple(min(255, c + lift) for c in DIM) + (255,))
+
+
+def _over(dx: int, dy: int, *, part: int = 24, gap: int = 10, pattern: tuple[int, int, str] = (3, 6, "v"),
+ contrast: int = 40, size: tuple[int, int] = (300, 300), body: tuple[int, int, int, int] = (30, 20, 269, 279),
+ at: tuple[int, int] = (120, 110)) -> Image.Image:
+ """A patterned body that stays (`_patterned`; 240 x 260 px on a 300 x 300 px canvas), with a part `part` px square on
+ it at `at`, a hole `gap` px square through the part's middle (none where 0), the part moved dx columns right and dy
+ rows down."""
+ im = Image.new("RGBA", size, (0, 0, 0, 0))
+ d = ImageDraw.Draw(im)
+ _patterned(d, body, *pattern, contrast)
+ x0, y0 = at[0] + dx, at[1] + dy
+ d.rectangle((x0, y0, x0 + part - 1, y0 + part - 1), fill=PART + (255,))
+ if gap:
+ o = (part - gap) // 2
+ d.rectangle((x0 + o, y0 + o, x0 + o + gap - 1, y0 + o + gap - 1), fill=(0, 0, 0, 0))
+ return im
+
+
+def _validators(dx: int) -> Image.Image:
+ """The checkpoint validator's frame: a body 200 x 180 px with stripes 3 px wide every 6 px on a 260 x 220 px canvas,
+ a part 24 px square with a hole 10 px square through it, the part moved dx columns right."""
+ return _over(dx, 0, size=(260, 220), body=(30, 20, 229, 199), at=(100, 80))
+
+
+def _arm(dy: int) -> Image.Image:
+ """A torso that stays, 120 x 260 px, patterned in blocks 4 px square at contrast 200; an arm beside its right edge,
+ the upper arm and the forearm lying along that edge and joined by the arm's outer side, so the gap between arm and
+ torso, 16 x 12 px, has the torso's edge on its left and the arm on its other sides; the arm reaches 48 px above and
+ 52 px below the gap and is moved dy rows down."""
+ im = Image.new("RGBA", (300, 300), (0, 0, 0, 0))
+ d = ImageDraw.Draw(im)
+ _patterned(d, (40, 20, 159, 279), 4, 0, "n", 200)
+ for x0, y0, x1, y1 in ((150, 60, 191, 111), (176, 60, 191, 175), (150, 124, 191, 175)):
+ d.rectangle((x0, y0 + dy, x1, y1 + dy), fill=PART + (255,))
+ 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))
@@ -117,6 +195,26 @@ def _resampled(a: Image.Image, made: Image.Image, b: Image.Image) -> dict[str, o
"delivered": out[1] is made and out[3] is made}
+# Resampled to eight, the frames made at t 0.25, 0.5 and 0.75 each way all kept as made.
+QUARTERS = {"made_at": [1, 2, 3, 5, 6, 7], "nearest_at": [], "faults": [[]] * 6, "delivered": True}
+
+
+def _quarters(draw) -> dict[str, object]:
+ """What the alignment does resampling draw(0) and draw(1) to eight frames, each frame between them (t 0.25, 0.5 and
+ 0.75 from the first to the second and back) being draw(s) at its place s from the first."""
+ a, b, made = draw(0.0), draw(1.0), {}
+
+ def interpolate(x: Image.Image, y: Image.Image, t: float) -> Image.Image:
+ s = t if x is a else 1 - t
+ if s not in made:
+ made[s] = draw(s)
+ return made[s]
+
+ out, facts = align.resample([a, b], 8, interpolate)
+ return {"made_at": facts["made_at"], "nearest_at": facts["nearest_at"], "faults": [m["faults"] for m in facts["smear"]],
+ "delivered": all(out[k] is made[k / 4 if k < 4 else 2 - k / 4] for k in (1, 2, 3, 5, 6, 7))}
+
+
def test_a_hole_through_a_leg_is_read_and_a_gap_the_sources_have_is_not():
"""The band reads as a hole over its own pixels; the frame drawn between, the arm's gap all three frames have, and
that gap moved a few pixels (within rife.HOLE_REACH of theirs) read none. A pinhole thinner than rife.HOLE_THICK is
@@ -219,3 +317,61 @@ def test_a_hole_through_a_moving_body_is_still_read():
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)
+
+
+def test_a_hole_a_part_carries_over_a_patterned_body_that_stays_is_kept_as_made():
+ """A part with a hole through it moves over a striped body that stays, each frame between drawn exactly at its
+ place: the hole is both source frames', carried with the part. Over the box round it the body's stillness fits
+ better than the part's motion, which alone misses it; every motion within reach is judged, and the part's carries
+ the hole in both source frames with the frame between covered round it as they are. The checkpoint validator's own
+ frame (its 24 px part moved 32 px), resampled to four, and parts of 24, 40 and 60 px moved diagonally 32 px (and the
+ 24 px part 48 px) over stripes at contrast 120, resampled to eight, keep every frame made, byte for byte."""
+ assert _resampled(_validators(-16), _validators(0), _validators(16)) == KEPT
+ for (part, gap), move in (((24, 10), 32), ((24, 10), 48), ((40, 17), 32), ((60, 25), 32)):
+
+ def draw(s: float, move: int = move, part: int = part, gap: int = gap) -> Image.Image:
+ dx = round(s * move) - move // 2
+ return _over(dx, dx, part=part, gap=gap, contrast=120)
+
+ assert _quarters(draw) == QUARTERS
+
+
+def test_every_pattern_contrast_and_move_reads_no_hole_where_a_part_carries_it():
+ """The same over every pattern and contrast at each move in OUTWEIGHED (t 0.5), and the three parts moved 16, 32 and
+ 48 px across and diagonally over the validator's stripes at t 0.25 and 0.75: every frame between reads no hole."""
+ seen = {}
+ for pattern, contrast, ((part, gap), move, down) in itertools.product(PATTERNS, CONTRASTS, OUTWEIGHED):
+ a, made, b = (_over(dx, down * dx, part=part, gap=gap, pattern=pattern, contrast=contrast)
+ for dx in (-move // 2, 0, move // 2))
+ seen[pattern, contrast, part, move, down, 0.5] = rife.hole(made, a, b, 0.5)
+ for (part, gap), move, down, t in itertools.product(((24, 10), (40, 17), (60, 25)), (16, 32, 48), (0, 1), (0.25, 0.75)):
+ back = int(np.floor(t * move + 0.5))
+ a, made, b = (_over(dx, down * dx, part=part, gap=gap) for dx in (-back, 0, move - back))
+ seen["stripes", 40, part, move, down, t] = rife.hole(made, a, b, t)
+ assert {k: v for k, v in seen.items() if v} == {}
+
+
+def test_the_gap_between_an_arm_and_a_patterned_torso_that_stays_is_theirs():
+ """An arm moves 40 px down beside a torso that stays, patterned in blocks: the gap between them, carried with the arm,
+ lies where the arm covers it in the other source frame, and over the box round it the torso's stillness fits better
+ than the arm's motion. Both source frames carry it on the arm's way: at t 0.25, 0.5 and 0.75 the frames between read
+ no hole and are kept as made."""
+ assert {t: rife.hole(_arm(0), _arm(-int(np.floor(t * 40 + 0.5))), _arm(40 - int(np.floor(t * 40 + 0.5))), t)
+ for t in (0.25, 0.5, 0.75)} == {0.25: 0.0, 0.5: 0.0, 0.75: 0.0}
+ assert _quarters(lambda s: _arm(round(s * 40) - 20)) == QUARTERS
+
+
+def test_a_gap_only_one_source_frame_opens_on_a_part_over_a_body_that_stays_is_given_way():
+ """The boundary: a part 30 px square moves 32 px over a body that stays, a hole 14 px square open through it in the
+ first source frame and closed in the second, half open (8 px) in the frame between. The hole lies 5 px and more from
+ where the first frame has it in place; the whole figure stays; the box round it fits a motion that does not carry it
+ (over a plain body, one that lays the box on the body away from the part; over the validator's stripes, the body's
+ stillness); and no motion carries it in both source frames — as none carries a band the coverage run took out of one
+ source frame's gap into a limb. Plain or striped, the frame gives way to the nearer source frame."""
+ for contrast in (0, 40):
+ a, made, b = (_over(dx, 0, part=30, gap=gap, contrast=contrast) for dx, gap in ((-16, 14), (0, 8), (16, 0)))
+ out, facts = align.resample([a, b], 4, lambda x, y, t: made)
+ assert facts["made_at"] == [] and facts["nearest_at"] == [1, 3] and out[1] is b and out[3] is a
+ assert all(m["faults"] == ["hole"] for m in facts["smear"])
+ assert rife.hole(made, a, b, 0.5) == round(8 * 8 / int((np.asarray(made)[..., 3] >= 128).sum()), 5)