PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Minimum/Maximum (Squareness): running extremes, bit-identical (#630)
Armaan Sandhu09b657257827
diff --git a/crates/algo/src/other.rs b/crates/algo/src/other.rsindex de8c3aec8..1db629a70 100644--- a/crates/algo/src/other.rs+++ b/crates/algo/src/other.rs@@ -76,39 +76,41 @@ pub(crate) fn running_extreme(v: &[f32], k: usize, max: bool, out: &mut Vec<f32>out.extend((0..=n - k).map(|i| pick(suf[i], pre[i + k - 1])));}-/// Minimum (spreads dark/transparent areas) or Maximum over a square.+/// Minimum (spreads dark/transparent areas) or Maximum over a square. Separable (rows, then+/// columns), each pass a [`running_extreme`], so the cost per pixel does not grow with the radius.pub(crate) fn min_max(src: &Image, out: Rect, radius: f32, max: bool) -> Vec<f32> {let n = src.ch;let r = radius.max(0.0).round() as i32;if r == 0 {return src.crop(out);}- // Separable: rows then columns.+ let k = (2 * r + 1) as usize;+ // Rows: the extreme over x - r..=x + r, for every row the column pass needs.let win = Rect::new(out.x0, out.y0 - r, out.x1, out.y1 + r);- let ww = win.width() as usize;- let mut tmp = vec![0.0f32; ww * win.height() as usize * n];+ let (ww, wh) = (win.width() as usize, win.height() as usize);+ let mut tmp = vec![0.0f32; ww * wh * n];+ let (mut line, mut ext) = (Vec::new(), Vec::new());for y in win.y0..win.y1 {- for x in win.x0..win.x1 {- for c in 0..n {- let mut m = if max { f32::MIN } else { f32::MAX };- for xx in x - r..=x + r {- let v = src.get(xx, y, c);- m = if max { m.max(v) } else { m.min(v) };- }- tmp[((y - win.y0) as usize * ww + (x - win.x0) as usize) * n + c] = m;+ let o = (y - win.y0) as usize * ww * n;+ for c in 0..n {+ line.clear();+ line.extend((win.x0 - r..win.x1 + r).map(|x| src.get(x, y, c)));+ running_extreme(&line, k, max, &mut ext);+ for (x, v) in ext.iter().enumerate() {+ tmp[o + x * n + c] = *v;}}}- let mut res = Vec::with_capacity(out.width() as usize * out.height() as usize * n);- for y in out.y0..out.y1 {- for x in out.x0..out.x1 {- for c in 0..n {- let mut m = if max { f32::MIN } else { f32::MAX };- for yy in y - r..=y + r {- let v = tmp[((yy - win.y0) as usize * ww + (x - win.x0) as usize) * n + c];- m = if max { m.max(v) } else { m.min(v) };- }- res.push(m);+ // Columns: the extreme over y - r..=y + r of the row results.+ let (ow, oh) = (out.width() as usize, out.height() as usize);+ let mut res = vec![0.0f32; ow * oh * n];+ for x in 0..ow {+ for c in 0..n {+ line.clear();+ line.extend((0..wh).map(|y| tmp[(y * ww + x) * n + c]));+ running_extreme(&line, k, max, &mut ext);+ for (y, v) in ext.iter().enumerate() {+ res[(y * ow + x) * n + c] = *v;}}}@@ -137,3 +139,50 @@ pub(crate) fn offset(src: &Image, out: Rect, ctx: &Ctx, dx: i32, dy: i32, undefi}res}++#[cfg(test)]+mod tests {+ use super::*;++ /// The square window scanned directly, the reference for [`min_max`].+ fn min_max_direct(src: &Image, out: Rect, r: i32, max: bool) -> Vec<f32> {+ let mut res = Vec::new();+ for y in out.y0..out.y1 {+ for x in out.x0..out.x1 {+ for c in 0..src.ch {+ let mut m = if max { f32::MIN } else { f32::MAX };+ for yy in y - r..=y + r {+ for xx in x - r..=x + r {+ let v = src.get(xx, yy, c);+ m = if max { m.max(v) } else { m.min(v) };+ }+ }+ res.push(m);+ }+ }+ }+ res+ }++ #[test]+ fn square_min_max_matches_the_direct_window_exactly() {+ let src_rect = Rect::new(-12, -9, 37, 30);+ let mut img = Image::new(src_rect, 4);+ let mut s = 0x2545_f491u32;+ for v in img.data.iter_mut() {+ s ^= s << 13;+ s ^= s >> 17;+ s ^= s << 5;+ *v = (s % 1000) as f32 / 999.0;+ }+ // Includes windows wider than the output and output reaching past the source.+ for out in [Rect::new(0, 0, 25, 21), Rect::new(-14, -3, 3, 40)] {+ for r in [1, 2, 3, 7, 20] {+ for max in [false, true] {+ let fast = min_max(&img, out, r as f32, max);+ assert_eq!(fast, min_max_direct(&img, out, r, max), "r {r} max {max} out {out:?}");+ }+ }+ }+ }+}