Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

check: fix a missed strobe and a false red flash in the flash rule

James Dimachkie08c4294a7fef
diff --git a/apps/check/src/picture.rs b/apps/check/src/picture.rs
index 28fb8e8e..f500a3f2 100644
--- a/apps/check/src/picture.rs
+++ b/apps/check/src/picture.rs
@@ -165,6 +165,14 @@ const FLASH_W: usize = 192;
const FLASH_MOST: usize = 6;
const FLASH_PAIR_S: f64 = 1.0 + 1e-9;
+/// A pixel in one measure: its value, and whether it can count. Red's value runs on through the saturation line, and a change
+/// counts when either end of it is saturated; a one-level step across the line isn't a jump from zero.
+#[derive(Clone, Copy)]
+struct Px {
+ value: f32,
+ counts: bool,
+}
+
struct Swing {
at: f64,
direction: f32,
@@ -179,8 +187,8 @@ struct Swings {
by: f32,
under: f32,
mixed_counts: bool,
- pivot: Vec<f32>,
- extreme: Vec<f32>,
+ pivot: Vec<Px>,
+ extreme: Vec<Px>,
at: f64,
direction: f32,
recent: std::collections::VecDeque<Swing>,
@@ -192,15 +200,15 @@ impl Swings {
Swings { by, under, mixed_counts, pivot: Vec::new(), extreme: Vec::new(), at: 0.0, direction: 0.0, recent: Default::default(), ends: Vec::new() }
}
- fn moves(&self, a: f32, b: f32, direction: f32) -> bool {
- (b - a) * direction >= self.by && a.min(b) < self.under
+ fn moves(&self, a: Px, b: Px, direction: f32) -> bool {
+ (b.value - a.value) * direction >= self.by && a.value.min(b.value) < self.under && (a.counts || b.counts)
}
- fn changed(&self, earlier: &[f32], later: &[f32], direction: f32) -> bool {
+ fn changed(&self, earlier: &[Px], later: &[Px], direction: f32) -> bool {
earlier.iter().zip(later).filter(|(a, b)| self.moves(**a, **b, direction)).count() * 4 >= earlier.len()
}
- fn push(&mut self, t: f64, px: &[f32]) {
+ fn push(&mut self, t: f64, px: &[Px]) {
if self.pivot.is_empty() {
self.pivot = px.to_vec();
self.extreme = px.to_vec();
@@ -218,17 +226,23 @@ impl Swings {
}
return;
}
- let mean = |v: &[f32]| v.iter().sum::<f32>() / v.len() as f32;
if self.changed(&self.extreme, px, -self.direction) {
self.record();
self.pivot = std::mem::replace(&mut self.extreme, px.to_vec());
self.at = t;
self.direction = -self.direction;
- } else if (mean(px) - mean(&self.extreme)) * self.direction > 0.0 && self.changed(&self.pivot, px, self.direction) {
+ } else if self.changed(&self.pivot, px, self.direction) && self.went_further(px) {
self.extreme = px.to_vec();
}
}
+ /// Whether the pixels swinging from the pivot went further than the saved extreme, judged on those pixels alone: the
+ /// rest of the window can dim by less than counts and still outweigh a patch that's flashing.
+ fn went_further(&self, px: &[Px]) -> bool {
+ let further: f32 = self.pivot.iter().zip(px).zip(&self.extreme).filter(|((p, n), _)| self.moves(**p, **n, self.direction)).map(|((_, n), e)| (n.value - e.value) * self.direction).sum();
+ further > 0.0
+ }
+
fn record(&mut self) {
if !self.changed(&self.pivot, &self.extreme, self.direction) {
return;
@@ -292,9 +306,9 @@ pub(crate) struct FlashScreen {
h: usize,
last: Vec<u8>,
linear: Vec<f32>,
- luma: Vec<f32>,
- red: Vec<f32>,
- cut: Vec<f32>,
+ luma: Vec<Px>,
+ red: Vec<Px>,
+ cut: Vec<Px>,
windows: Vec<(usize, usize, Swings, Swings)>,
}
@@ -332,9 +346,9 @@ impl FlashScreen {
self.red.clear();
for px in rgb.chunks_exact(3) {
let (r, g, b) = (self.linear[px[0] as usize], self.linear[px[1] as usize], self.linear[px[2] as usize]);
- self.luma.push(0.2126 * r + 0.7152 * g + 0.0722 * b);
+ self.luma.push(Px { value: 0.2126 * r + 0.7152 * g + 0.0722 * b, counts: true });
let sum = r + g + b + 1e-9;
- self.red.push(if r / sum >= 0.8 { ((r - g - b) * 320.0).max(0.0) } else { 0.0 });
+ self.red.push(Px { value: ((r - g - b) * 320.0).max(0.0), counts: r / sum >= 0.8 });
}
let (stride, ww, wh) = (self.w * 2, self.w / 3, self.h / 3);
for (x, y, general, saturated) in &mut self.windows {
diff --git a/apps/check/src/tests.rs b/apps/check/src/tests.rs
index c15c4c1a..220e85db 100644
--- a/apps/check/src/tests.rs
+++ b/apps/check/src/tests.rs
@@ -371,3 +371,28 @@ fn a_shape_sliding_past_and_ordinary_cuts_are_not_flashing() {
assert_eq!(flash_of(3, move |f, x, _| if x < f { white } else { black }), None, "one wipe across the frame");
assert_eq!(flash_of(4, move |f, _, _| if (f / 30) % 2 == 0 { white } else { black }), None, "a cut every half second");
}
+
+#[test]
+fn a_patch_strobing_while_its_background_dims_is_still_found() {
+ // a third of a window: 0 then 124 on a 148 background, then the background settles at 124 and the patch cycles 124 and 160.
+ // The background's drop is under the 0.10 that counts, but it outweighs the patch in the window's mean.
+ let found = flash_samples(180, 60.0, |f, _, x, y| {
+ let patch = x < 16 && y < 12;
+ let level = match (patch, f) {
+ (true, 0) => 0,
+ (true, 1) => 124,
+ (true, _) => if (f - 2) * 5 * 2 / 60 % 2 == 0 { 160 } else { 124 },
+ (false, 0 | 1) => 148,
+ (false, _) => 124,
+ };
+ [level, level, level]
+ });
+ assert!(found.is_some(), "the patch flashes five times a second");
+}
+
+#[test]
+fn a_colour_crossing_the_saturation_line_is_not_a_red_flash() {
+ // one level of green moves the pixel across R/(R+G+B) = 0.8 and hardly changes its red at all
+ let found = flash_samples(180, 60.0, |f, _, _, _| if f * 5 * 2 / 60 % 2 == 0 { [100, 49, 0] } else { [100, 50, 0] });
+ assert_eq!(found, None);
+}