diff --git a/crates/effects/src/pixel/tests.rs b/crates/effects/src/pixel/tests.rs index f2f66b2dd4..eff3af66ed 100644 --- a/crates/effects/src/pixel/tests.rs +++ b/crates/effects/src/pixel/tests.rs @@ -714,14 +714,16 @@ fn smart_blur_normal_mode_keeps_its_pixels() { /// Smart Blur at radius 1 averages each pixel with the neighbours whose channels, alpha included, /// all lie within the threshold: grays 100 and 110 average to 105 each, 200 is 90 levels from 110 -/// and stays, and the half-covered pixel next to it differs in alpha and stays too. +/// and stays, and the two grays 100 after it have the same color but alpha 255 and 128, so neither +/// takes the other. #[test] fn smart_blur_averages_only_the_neighbours_within_the_threshold() { - let (w, h) = (4, 1); + let (w, h) = (5, 1); let src = image(w, h, |x, _| match x { 0 => [100, 100, 100, 255], 1 => [110, 110, 110, 255], 2 => [200, 200, 200, 255], + 3 => [100, 100, 100, 255], _ => [100, 100, 100, 128], }); let mut d = src.clone(); @@ -730,6 +732,104 @@ fn smart_blur_averages_only_the_neighbours_within_the_threshold() { assert_eq!(at(&d, w, 1, 0), [105, 105, 105, 255]); assert_eq!(at(&d, w, 2, 0), [200, 200, 200, 255]); assert_eq!(at(&d, w, 3, 0), at(&src, w, 3, 0)); + assert_eq!(at(&d, w, 4, 0), at(&src, w, 4, 0)); +} + +/// Smart Blur at radius 12 reads its neighbours from a box-blurred copy. Next to a step from gray +/// 100 to gray 200 that copy holds values between them, so at threshold 50 the pixel on the dark +/// side of the step takes in some lighter gray and the pixel on the light side some darker gray, +/// while pixels more than 15 pixels from the step keep their gray. Medium quality takes in less of +/// the other gray than low, and at threshold 30 the pixels next to the step keep their gray. +#[test] +fn smart_blur_at_a_wide_radius_reads_the_blurred_copy_next_to_an_edge() { + let (w, h) = (60, 40); + let src = image(w, h, |x, _| grey(if x < 30 { 100 } else { 200 })); + let mut d = src.clone(); + fx("blur.smart", json!({"radius": 12, "threshold": 50, "quality": "low"})).apply(&mut d, w, h, &space(w, h)); + let dark = at(&d, w, 29, 20)[0]; + let light = at(&d, w, 30, 20)[0]; + assert!((101..=150).contains(&dark), "the dark side takes in some lighter gray: {dark}"); + assert!((150..=199).contains(&light), "the light side takes in some darker gray: {light}"); + assert_eq!(at(&d, w, 10, 20), grey(100)); + assert_eq!(at(&d, w, 50, 20), grey(200)); + // Medium quality spreads 7 samples instead of 5, read from a copy blurred over 2 pixels instead + // of 3, so less of the other gray reaches the pixels next to the step. + let mut medium = src.clone(); + fx("blur.smart", json!({"radius": 12, "threshold": 50, "quality": "medium"})).apply(&mut medium, w, h, &space(w, h)); + let (medium_dark, medium_light) = (at(&medium, w, 29, 20)[0], at(&medium, w, 30, 20)[0]); + assert!(100 < medium_dark && medium_dark < dark, "medium {medium_dark}, low {dark}"); + assert!(light < medium_light && medium_light < 200, "medium {medium_light}, low {light}"); + // At threshold 30 the blurred values next to the step are too far from both grays, and the + // pixels on either side keep their gray. + let mut sharp = src.clone(); + fx("blur.smart", json!({"radius": 12, "threshold": 30, "quality": "low"})).apply(&mut sharp, w, h, &space(w, h)); + assert_eq!(at(&sharp, w, 29, 20), grey(100)); + assert_eq!(at(&sharp, w, 30, 20), grey(200)); +} + +/// Smart Blur at radius 12 and low quality samples offsets 0, 6 and 12 pixels along each axis, +/// in both directions, within distance 12.5. A one-column gray-200 stripe at x = 30 on gray 100 +/// becomes (6 * 100 + 200) / 7, about 114.29, in the copy box-blurred with radius 3. For the +/// gray-100 pixel at (24, 20), only the three samples at x = 30 have blurred values above 100. +/// Their own value 200 is outside threshold 30, so they are excluded and the result stays 100. +/// Including them would average ten samples of 100 with three of about 114.29 and round to 103. +#[test] +fn smart_blur_at_a_wide_radius_rejects_a_stripes_own_value_outside_the_threshold() { + let (w, h) = (60, 40); + let src = image(w, h, |x, _| grey(if x == 30 { 200 } else { 100 })); + let mut d = src.clone(); + fx("blur.smart", json!({"radius": 12, "threshold": 30, "quality": "low"})).apply(&mut d, w, h, &space(w, h)); + assert_eq!(at(&d, w, 24, 20), grey(100)); +} + +/// Smart Blur at radius 2 reaches two pixels each way: grays 100, 110 and 120 lie within 20 levels +/// of each other and all average to 110 (at radius 1 the ends would give 105 and 115), 200 and 210 +/// average to 205, and 60 is too far from both groups and stays. +#[test] +fn smart_blur_at_radius_two_averages_the_neighbours_two_pixels_away() { + let (w, h) = (6, 1); + let src = image(w, h, |x, _| grey([100, 110, 120, 200, 210, 60][x])); + let mut d = src.clone(); + fx("blur.smart", json!({"radius": 2, "threshold": 20, "quality": "high"})).apply(&mut d, w, h, &space(w, h)); + let want = image(w, h, |x, _| grey([110, 110, 110, 205, 205, 60][x])); + assert_eq!(d, want); +} + +/// Smart Blur at radius 2 and high quality reads the original pixels within distance 2.5. +/// The center at (2, 2) is gray 100, one neighbor is gray 120, and the rest are gray 200, +/// outside threshold 20. A neighbor at (4, 2) is two pixels away, so the center averages +/// (100 + 120) / 2 = 110. A neighbor at (4, 4) is sqrt(8), about 2.83 pixels away, so it is +/// excluded and the center stays 100. Reading the whole square would give 110 in both cases. +#[test] +fn smart_blur_at_radius_two_keeps_the_neighbours_inside_the_disc() { + let (w, h) = (5, 5); + for ((nx, ny), want) in [((4, 2), 110), ((4, 4), 100)] { + let src = image(w, h, |x, y| match (x, y) { + (2, 2) => grey(100), + p if p == (nx, ny) => grey(120), + _ => grey(200), + }); + let mut d = src.clone(); + fx("blur.smart", json!({"radius": 2, "threshold": 20, "quality": "high"})).apply(&mut d, w, h, &space(w, h)); + assert_eq!(at(&d, w, 2, 2), grey(want), "neighbor at ({nx}, {ny})"); + } +} + +/// Smart Blur's default radius 3 and medium quality (7 samples) read every integer offset +/// in the 7 by 7 square without prefiltering, keeping offsets within distance 3.5. +/// The center at (3, 3) is gray 100, its neighbor at (6, 3) is gray 120, and the rest are +/// gray 200, outside the default threshold 25. The center averages (100 + 120) / 2 = 110. +/// A search limited to two pixels would leave the center at 100. +#[test] +fn smart_blur_default_radius_averages_the_neighbour_three_pixels_away() { + let (w, h) = (7, 7); + let mut d = image(w, h, |x, y| match (x, y) { + (3, 3) => grey(100), + (6, 3) => grey(120), + _ => grey(200), + }); + fx("blur.smart", json!({})).apply(&mut d, w, h, &space(w, h)); + assert_eq!(at(&d, w, 3, 3), grey(110)); } /// Smart Blur's test image: grays 56 and 64 in a checkerboard for x < 3 and 66 and 74 for x in 3..6