Skip to content

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

Custom filter: in CMYK a positive offset brightens (#7076) (#7077)

David Mann13bbf0eefe4f
diff --git a/crates/algo/src/other2.rs b/crates/algo/src/other2.rs
index b5a16f06e..61ccfc003 100644
--- a/crates/algo/src/other2.rs
+++ b/crates/algo/src/other2.rs
@@ -8,7 +8,8 @@ use crate::image::Image;
use crate::{Ctx, HsbModel};
/// Custom: `Σ kernel·pixel / scale + offset/255` per colour channel (alpha
-/// kept). Kernels shorter than 25 entries are zero-padded.
+/// kept). Kernels shorter than 25 entries are zero-padded. CMYK samples are
+/// ink, so there it runs on `1 - ink` and a positive offset brightens.
pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f32, offset: f32) -> Vec<f32> {
let n = src.ch;
let cc = ncol(ctx, n);
@@ -16,6 +17,7 @@ pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f
(0..25).filter_map(|i| kernel.get(i).copied().filter(|v| *v != 0.0).map(|v| (i as i32 % 5 - 2, i as i32 / 5 - 2, v))).collect();
let s = if scale == 0.0 { 1.0 } else { scale };
let off = offset / 255.0;
+ let sub = crate::fxutil::subtractive(ctx);
let mut res = src.crop(out);
if k.is_empty() {
return res;
@@ -23,8 +25,10 @@ pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f
for (i, px) in res.chunks_exact_mut(n).enumerate() {
let (x, y) = xy(out, i);
for (c, v) in px.iter_mut().enumerate().take(cc) {
- let sum: f32 = k.iter().map(|&(dx, dy, w)| src.get(x + dx, y + dy, c) * w).sum();
- *v = sum / s + off;
+ // CMYK: convolve brightness (1 - ink), then back to ink.
+ let lum = |v: f32| if sub { 1.0 - v } else { v };
+ let sum: f32 = k.iter().map(|&(dx, dy, w)| lum(src.get(x + dx, y + dy, c)) * w).sum();
+ *v = lum(sum / s + off);
}
}
res
diff --git a/crates/algo/src/tests_ext.rs b/crates/algo/src/tests_ext.rs
index be97a76cb..7c28485f1 100644
--- a/crates/algo/src/tests_ext.rs
+++ b/crates/algo/src/tests_ext.rs
@@ -637,6 +637,28 @@ fn custom_kernel_shifts_and_offsets() {
assert!((off.pixel(5, 5)[0] - 0.4).abs() < 1e-5);
}
+/// CMYK samples are ink: Custom's positive offset brightens (removes ink), as in Photoshop
+/// (identity, scale 1, offset +50: white stays white, C=100% becomes C≈80%). Alpha is untouched.
+/// Photoshop's CMYK depths; float is unclamped on both polarities (RGB white + offset exceeds 1).
+#[test]
+fn custom_offset_brightens_cmyk() {
+ let mut id = vec![0.0; 25];
+ id[12] = 1.0;
+ let p = FilterParams::Custom { kernel: id, scale: 1.0, offset: 50.0 };
+ for depth in [SampleType::U8, SampleType::U16] {
+ let f = PixelFormat::new(ColorMode::Cmyk, depth, true);
+ let mut s = Surface::new(f);
+ s.fill_rect(R, &[0.0, 0.0, 0.0, 0.0, 0.6]);
+ let w = run(&s, &p).pixel(5, 5);
+ assert!(w[..4].iter().all(|v| v.abs() < 1e-6) && (w[4] - 0.6).abs() < 0.01, "{depth:?} white: {w:?}");
+ let mut s = Surface::new(f);
+ s.fill_rect(R, &[1.0, 0.5, 0.0, 0.0, 1.0]);
+ let c = run(&s, &p).pixel(5, 5);
+ let want = [1.0 - 50.0 / 255.0, 0.5 - 50.0 / 255.0, 0.0, 0.0, 1.0];
+ assert!(c.iter().zip(want).all(|(a, b)| (a - b).abs() < 0.01), "{depth:?} C100: {c:?}");
+ }
+}
+
#[test]
fn hsb_filter_encodes_and_decodes() {
let s = flat(SampleType::F32, R, [1.0, 0.0, 0.0, 1.0]);