Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Minimum / Maximum: CMYK spreads paper white with Maximum, ink with Minimum (#7074) (#7075)

David Mannaa8621788eb9
diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs
index ac4b3b1fc..6cefbbd4c 100644
--- a/crates/algo/src/lib.rs
+++ b/crates/algo/src/lib.rs
@@ -745,8 +745,8 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec<f
FilterParams::AddNoise { amount, distribution, monochromatic, seed } => noise::add(src, out, ctx, *amount, *distribution, *monochromatic, *seed),
FilterParams::Median { radius } => noise::median(src, out, *radius, None),
FilterParams::DustAndScratches { radius, threshold } => noise::median(src, out, *radius, Some(*threshold)),
- FilterParams::Minimum { radius, preserve } => other::min_max_preserve(src, out, *radius, false, *preserve),
- FilterParams::Maximum { radius, preserve } => other::min_max_preserve(src, out, *radius, true, *preserve),
+ FilterParams::Minimum { radius, preserve } => min_max_ink(src, out, ctx, *radius, false, *preserve),
+ FilterParams::Maximum { radius, preserve } => min_max_ink(src, out, ctx, *radius, true, *preserve),
FilterParams::Offset { horizontal, vertical, undefined } => other::offset(src, out, ctx, *horizontal, *vertical, *undefined),
FilterParams::Mosaic { cell_size } => stylize::mosaic(src, out, ctx, *cell_size),
FilterParams::Emboss { angle, height, amount } => stylize::emboss(src, out, ctx, *angle, *height, *amount),
@@ -910,6 +910,23 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec<f
}
}
+/// Minimum / Maximum in the document's polarity. CMYK samples are ink, so there Maximum spreads
+/// paper white and Minimum spreads ink (as Photoshop does): the colour channels run inverted,
+/// alpha keeps its usual polarity.
+fn min_max_ink(src: &Image, out: Rect, ctx: &Ctx, radius: f32, max: bool, preserve: Preserve) -> Vec<f32> {
+ if !fxutil::subtractive(ctx) {
+ return other::min_max_preserve(src, out, radius, max, preserve);
+ }
+ let n = src.ch.max(1);
+ let cc = if ctx.alpha { n - 1 } else { n };
+ let flip = |data: &mut [f32]| data.chunks_exact_mut(n).for_each(|px| px.iter_mut().take(cc).for_each(|v| *v = 1.0 - *v));
+ let mut inv = src.clone();
+ flip(&mut inv.data);
+ let mut res = other::min_max_preserve(&inv, out, radius, max, preserve);
+ flip(&mut res);
+ res
+}
+
/// Output tile size used by [`apply`].
pub const TILE: i32 = 256;
diff --git a/crates/algo/src/tests.rs b/crates/algo/src/tests.rs
index 06beabebb..b7fc92113 100644
--- a/crates/algo/src/tests.rs
+++ b/crates/algo/src/tests.rs
@@ -269,6 +269,32 @@ fn minimum_maximum_spread() {
assert!((mn.pixel(10, 10)[0] - 0.5).abs() < 1e-6);
}
+/// CMYK samples are ink, so Maximum spreads paper white and Minimum spreads ink, as Photoshop
+/// does: Maximum r 1 removes a 1-px C 100% line on white, Minimum widens it to 3 px. Alpha keeps
+/// the usual polarity (Maximum grows opaque areas, Minimum grows transparent ones).
+#[test]
+fn minimum_maximum_treat_cmyk_as_ink() {
+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let mut s = Surface::new(PixelFormat::new(ColorMode::Cmyk, depth, true));
+ s.fill_rect(R, &[0.0, 0.0, 0.0, 0.0, 1.0]);
+ s.fill_rect(Rect::new(20, 0, 21, 30), &[1.0, 0.0, 0.0, 0.0, 1.0]);
+ s.write_pixel(5, 5, &[0.0, 0.0, 0.0, 0.0, 0.0]);
+ for preserve in [Preserve::Squareness, Preserve::Roundness] {
+ let mx = run(&s, &FilterParams::Maximum { radius: 1.0, preserve });
+ for x in 17..24 {
+ assert!(mx.pixel(x, 15)[0].abs() < 1e-6, "{depth:?} {preserve:?} Maximum C at x {x}: {:?}", mx.pixel(x, 15));
+ }
+ assert!((mx.pixel(5, 5)[4] - 1.0).abs() < 1e-6, "{depth:?} {preserve:?} Maximum grows alpha");
+ let mn = run(&s, &FilterParams::Minimum { radius: 1.0, preserve });
+ for x in 17..24 {
+ let want = if (19..=21).contains(&x) { 1.0 } else { 0.0 };
+ assert!((mn.pixel(x, 15)[0] - want).abs() < 1e-6, "{depth:?} {preserve:?} Minimum C at x {x}: {:?}", mn.pixel(x, 15));
+ }
+ assert!(mn.pixel(6, 5)[4].abs() < 1e-6, "{depth:?} {preserve:?} Minimum shrinks alpha");
+ }
+ }
+}
+
#[test]
fn maximum_roundness_grows_a_point_into_a_disc() {
// One white pixel on black: Maximum spreads it over the structuring element itself.