Skip to content

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

CMYK: Invert, Posterize and Threshold on the inks; 16-bit Lab Invert keeps greys neutral (#3930)

Timothy Dalybecc27ac1c72
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs
index 79085151d..4366ab0a0 100644
--- a/crates/compose/src/adjust.rs
+++ b/crates/compose/src/adjust.rs
@@ -249,11 +249,18 @@ fn legacy_brightness_contrast(brightness: f32, contrast: f32) -> impl Fn(f32) ->
}
/// Applies an adjustment in a document of colour `mode`: [`apply_depth`], except that in Lab
-/// documents the adjustments Photoshop evaluates on the L, a and b channels do so (see
-/// [`lab_channel_adjustment`]).
+/// and CMYK documents the adjustments Photoshop evaluates on the document's own channels do so
+/// (see [`lab_channel_adjustment`] and [`cmyk_channel_adjustment`]).
pub fn apply_in(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, depth: Option<SampleType>, mode: ColorMode) {
if mode == ColorMode::Lab && lab_channel_adjustment(adj) {
- apply_lab(adj, buf);
+ apply_lab(adj, buf, depth);
+ } else if mode == ColorMode::Cmyk && cmyk_channel_adjustment(adj) {
+ use photocraft_color::convert::{cmyk_to_rgb, rgb_to_cmyk};
+ map_rgb(buf, |c| {
+ let mut ink = rgb_to_cmyk(c);
+ stored_channels(adj, mode, depth, &mut ink, rgb_to_gray(c));
+ cmyk_to_rgb(ink)
+ });
} else {
apply_depth(adj, buf, transfer, depth);
}
@@ -274,6 +281,51 @@ pub fn lab_channel_adjustment(adj: &Adjustment) -> bool {
)
}
+/// Whether a CMYK document evaluates `adj` on its inks rather than on the screen colour: Invert
+/// and Posterize (each ink, black included, so paper inverts to 400% ink) and Threshold (paper or
+/// 100% black ink alone). Photoshop corpus cmyk8 and cmyk16 invert, posterize-4 and threshold-128
+/// follow these rules on the files' own inks on every pixel; here the inks are the screen colour
+/// separated through the document's CMYK profile, so neutrals carry the black that profile
+/// generates rather than the file's.
+pub fn cmyk_channel_adjustment(adj: &Adjustment) -> bool {
+ stored_channel_adjustment(adj)
+}
+
+/// Whether [`stored_channels`] evaluates `adj`: Invert, Posterize and Threshold.
+pub fn stored_channel_adjustment(adj: &Adjustment) -> bool {
+ matches!(adj, Adjustment::Invert | Adjustment::Posterize { .. } | Adjustment::Threshold { .. })
+}
+
+/// Invert, Posterize or Threshold on one pixel's own channels in a Lab or CMYK document of
+/// `depth` (`None`: unknown, treated as 8-bit): `ch` holds L, a, b normalised as [`lab_norm`]
+/// gives them, or the C, M, Y, K inks (0: none); `gray` is the pixel's luma, which CMYK Threshold
+/// compares. Lab Invert turns 8-bit bytes `v` into `255 - v` (a becomes -1 - a) and mirrors a and
+/// b around neutral at 16 bits (photoshop corpus lab16 invert.psd: neutral stays neutral).
+pub fn stored_channels(adj: &Adjustment, mode: ColorMode, depth: Option<SampleType>, ch: &mut [f32], gray: f32) {
+ let lab = mode == ColorMode::Lab;
+ match adj {
+ Adjustment::Invert => {
+ let mirror = if lab && depth.is_some_and(|d| d != SampleType::U8) { 256.0 / 255.0 } else { 1.0 };
+ for (i, v) in ch.iter_mut().enumerate() {
+ *v = if i > 0 { mirror } else { 1.0 } - v.clamp(0.0, 1.0);
+ }
+ }
+ Adjustment::Posterize { levels } => ch.iter_mut().for_each(|v| *v = posterize(*v, *levels)),
+ Adjustment::Threshold { level } => {
+ // On the 8-bit grid, like the RGB threshold.
+ let t = (level * 255.0).round();
+ let out = if lab {
+ let white = ch.first().is_some_and(|l| (l.clamp(0.0, 1.0) * 255.0).round() >= t);
+ [if white { 1.0 } else { 0.0 }, 128.0 / 255.0, 128.0 / 255.0, 0.0]
+ } else {
+ [0.0, 0.0, 0.0, if (gray * 255.0).round() < t { 1.0 } else { 0.0 }]
+ };
+ ch.iter_mut().zip(out).for_each(|(v, o)| *v = o);
+ }
+ _ => {}
+ }
+}
+
/// Encoded sRGB → Lab normalised the way Photoshop's channels hold it: L/100, (a+128)/255,
/// (b+128)/255.
fn lab_norm(c: [f32; 3]) -> [f32; 3] {
@@ -286,12 +338,16 @@ fn lab_denorm(n: [f32; 3]) -> [f32; 3] {
lab_to_srgb([n[0] * 100.0, n[1] * 255.0 - 128.0, n[2] * 255.0 - 128.0])
}
-/// The Lab evaluation of a [`lab_channel_adjustment`] (no-op for any other adjustment).
-fn apply_lab(adj: &Adjustment, buf: &mut Buffer) {
+/// The Lab evaluation of a [`lab_channel_adjustment`] in a document of `depth` (no-op for any
+/// other adjustment).
+fn apply_lab(adj: &Adjustment, buf: &mut Buffer, depth: Option<SampleType>) {
match adj {
- // n → 1 - n on every channel: L → 100 - L and a, b mirrored about neutral.
- Adjustment::Invert => map_rgb(buf, |c| lab_denorm(lab_norm(c).map(|v| 1.0 - v.clamp(0.0, 1.0)))),
- Adjustment::Posterize { levels } => map_rgb(buf, |c| lab_denorm(lab_norm(c).map(|v| posterize(v, *levels)))),
+ // L → 100 - L and a, b mirrored about neutral; each channel posterized (see `stored_channels`).
+ Adjustment::Invert | Adjustment::Posterize { .. } => map_rgb(buf, |c| {
+ let mut n = lab_norm(c);
+ stored_channels(adj, ColorMode::Lab, depth, &mut n, 0.0);
+ lab_denorm(n)
+ }),
Adjustment::Threshold { level } => {
// Split on L alone (on the 8-bit grid); the result is neutral black or white.
let t = (level * 255.0).round();
@@ -1489,6 +1545,41 @@ mod tone_tests {
assert!(!near(srgb_to_lab([rgb.px[0][0], rgb.px[0][1], rgb.px[0][2]]), got, 1.0));
}
+ #[test]
+ fn sixteen_bit_lab_invert_keeps_greys_neutral() {
+ // 16-bit Lab Invert mirrors a and b around neutral (photoshop corpus lab16 invert.psd);
+ // 8-bit bytes land one step off it.
+ let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), [0.0, 0.0, 0.0, 1.0]);
+ let c = lab(40.0, 0.0, 0.0);
+ b.px[0] = [c[0], c[1], c[2], 1.0];
+ apply_in(&Adjustment::Invert, &mut b, Transfer::Srgb, Some(SampleType::U16), ColorMode::Lab);
+ let got = srgb_to_lab([b.px[0][0], b.px[0][1], b.px[0][2]]);
+ assert!(near(got, [60.0, 0.0, 0.0], 0.05), "{got:?}");
+ assert!(near(in_lab(&Adjustment::Invert, c), [60.0, -1.0, -1.0], 0.1));
+ }
+
+ /// `adj` applied in a CMYK document to one encoded sRGB colour.
+ fn in_cmyk(adj: &Adjustment, c: [f32; 3]) -> [f32; 3] {
+ let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), [c[0], c[1], c[2], 1.0]);
+ apply_in(adj, &mut b, Transfer::Srgb, Some(SampleType::U8), ColorMode::Cmyk);
+ [b.px[0][0], b.px[0][1], b.px[0][2]]
+ }
+
+ #[test]
+ fn cmyk_invert_posterize_and_threshold_work_on_the_inks() {
+ use photocraft_color::convert::{cmyk_to_rgb, rgb_to_cmyk};
+ // Paper inverts to all four inks at 100%, not to RGB black (photoshop corpus cmyk8 invert.psd).
+ assert!(near(in_cmyk(&Adjustment::Invert, [1.0; 3]), cmyk_to_rgb([1.0; 4]), 2e-3));
+ let red = [0.9, 0.2, 0.1];
+ assert!(near(in_cmyk(&Adjustment::Invert, red), cmyk_to_rgb(rgb_to_cmyk(red).map(|v| 1.0 - v)), 2e-3));
+ let two = cmyk_to_rgb(rgb_to_cmyk(red).map(|v| if v >= 0.5 { 1.0 } else { 0.0 }));
+ assert!(near(in_cmyk(&Adjustment::Posterize { levels: 2 }, red), two, 2e-3));
+ // Threshold's black is the black ink alone (threshold-128.psd), its white the paper.
+ let t = Adjustment::Threshold { level: 0.5 };
+ assert!(near(in_cmyk(&t, [0.2; 3]), cmyk_to_rgb([0.0, 0.0, 0.0, 1.0]), 2e-3));
+ assert!(near(in_cmyk(&t, [0.8; 3]), cmyk_to_rgb([0.0; 4]), 2e-3));
+ }
+
#[test]
fn lab_posterize_steps_l_a_and_b() {
let got = in_lab(&Adjustment::Posterize { levels: 2 }, lab(70.0, 20.0, -30.0));
@@ -1547,15 +1638,18 @@ mod tone_tests {
let (mut a, mut b) = (Buffer::filled(crate::Rect::new(0, 0, 1, 1), c), Buffer::filled(crate::Rect::new(0, 0, 1, 1), c));
apply_in(adj, &mut a, Transfer::Srgb, None, mode);
apply_depth(adj, &mut b, Transfer::Srgb, None);
- assert_eq!(a.px == b.px, mode != ColorMode::Lab || !lab_channel_adjustment(adj), "{adj:?} in {mode:?}");
+ let own = (mode == ColorMode::Lab && lab_channel_adjustment(adj)) || (mode == ColorMode::Cmyk && cmyk_channel_adjustment(adj));
+ assert_eq!(a.px == b.px, !own, "{adj:?} in {mode:?}");
}
}
// Transparent pixels are left alone and odd values never panic.
let mut t = Buffer::filled(crate::Rect::new(0, 0, 2, 1), [f32::NAN, 2.0, -1.0, 0.0]);
for adj in lab_only.iter().chain([&Adjustment::GradientMap { stops: vec![], reverse: true, dither: true }]) {
- apply_in(adj, &mut t, Transfer::Srgb, None, ColorMode::Lab);
- let mut odd = Buffer::filled(crate::Rect::new(0, 0, 1, 1), [f32::NAN, f32::INFINITY, -3.0, 1.0]);
- apply_in(adj, &mut odd, Transfer::Srgb, None, ColorMode::Lab);
+ for mode in [ColorMode::Lab, ColorMode::Cmyk] {
+ apply_in(adj, &mut t, Transfer::Srgb, None, mode);
+ let mut odd = Buffer::filled(crate::Rect::new(0, 0, 1, 1), [f32::NAN, f32::INFINITY, -3.0, 1.0]);
+ apply_in(adj, &mut odd, Transfer::Srgb, Some(SampleType::U16), mode);
+ }
}
assert!(t.px[0][1] == 2.0);
}
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index c13a423ca..5193c2198 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -592,6 +592,20 @@ fn render_is_tile_independent() {
}
}
+/// A Lab Invert layer in a 16-bit document keeps a grey neutral (the compositor passes the
+/// document's depth to `adjust::apply_in`).
+#[test]
+fn a_16_bit_lab_invert_layer_keeps_greys_neutral() {
+ let mut d = Document::new("lab", Size::new(2, 2), ColorMode::Lab, SampleType::U16);
+ let mut bg = Layer::raster("bg", d.pixel_format());
+ bg.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 2, 2), &[0.25, 128.0 / 255.0, 128.0 / 255.0, 1.0]);
+ d.layers.push(bg);
+ d.layers.push(Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert)));
+ let p = px(&d, 1, 1);
+ let lab = photocraft_color::convert::srgb_to_lab([p[0], p[1], p[2]]);
+ assert!((lab[0] - 75.0).abs() < 0.1 && lab[1].abs() < 0.05 && lab[2].abs() < 0.05, "{lab:?}");
+}
+
#[test]
fn cmyk_document_renders_via_rgb() {
let mut d = Document::new("c", Size::new(2, 2), ColorMode::Cmyk, SampleType::U8);
@@ -2388,7 +2402,9 @@ fn lab_documents_evaluate_invert_threshold_and_gradient_map_on_lab() {
};
let near = |got: [f32; 3], want: [f32; 3], tol: f32| got.iter().zip(want).all(|(g, w)| (g - w).abs() <= tol);
let inv = render(Adjustment::Invert);
- assert!(near(inv, [70.0, -13.0, 17.0], 1.0), "{depth:?}: {inv:?}");
+ // 8-bit bytes become 255 - v (a, b one step past the mirror); 16 bits mirror them exactly.
+ let want = if depth == SampleType::U8 { [70.0, -13.0, 17.0] } else { [70.0, -12.0, 18.0] };
+ assert!(near(inv, want, 0.5), "{depth:?}: {inv:?}");
let th = render(Adjustment::Threshold { level: 0.25 });
assert!(near(th, [100.0, 0.0, 0.0], 0.5), "{depth:?}: L 30 is above level 64: {th:?}");
let gm = render(Adjustment::GradientMap { stops: vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])], reverse: false, dither: false });
@@ -2416,10 +2432,10 @@ const MUTED: [f32; 3] = [0.40, 0.42, 0.60];
fn lab_documents_invert_and_posterize_l_a_and_b() {
let lab = photocraft_color::convert::srgb_to_lab(MUTED);
for depth in SampleType::ALL {
- // Invert: every normalised channel n -> 1 - n, so L -> 100 - L and a, b -> their opposites
- // (-a - 1 on the 0..255 grid).
+ // Invert: L -> 100 - L and a, b -> their opposites (-a - 1 on the 8-bit 0..255 grid).
let inv = lab_doc_adjusted(MUTED, Adjustment::Invert, depth);
- near3(inv, [100.0 - lab[0], -lab[1] - 1.0, -lab[2] - 1.0], 1.0, &format!("{depth:?} invert"));
+ let step = if depth == SampleType::U8 { 1.0 } else { 0.0 };
+ near3(inv, [100.0 - lab[0], -lab[1] - step, -lab[2] - step], 1.0, &format!("{depth:?} invert"));
// An RGB invert would land elsewhere: lightness 100 - L is the Lab rule.
let rgb_inv = photocraft_color::convert::srgb_to_lab(MUTED.map(|v| 1.0 - v));
assert!((rgb_inv[0] - inv[0]).abs() > 2.0, "{depth:?}: {rgb_inv:?} vs {inv:?}");
diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rs
index f176315ca..cde7fa41d 100644
--- a/crates/engine/src/pixels.rs
+++ b/crates/engine/src/pixels.rs
@@ -91,6 +91,9 @@ pub fn adjust_mask(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface
/// `s`'s pixels over `r` (untouched ones included) with `adj` applied through the selection, encoded.
fn adjusted(s: &Surface, r: Rect, adj: &Adjustment, selection: Option<&Surface>, mode: photocraft_color::ColorMode) -> Vec<f32> {
let fmt = s.format();
+ if fmt.mode == mode && matches!(mode, photocraft_color::ColorMode::Lab | photocraft_color::ColorMode::Cmyk) && adjust::stored_channel_adjustment(adj) {
+ return adjusted_channels(s, r, adj, selection);
+ }
let n = fmt.channels();
let raw = s.read_region(r);
let mut buf = Buffer { rect: r, px: raw.chunks_exact(n).map(|p| to_rgba(&fmt, p)).collect() };
@@ -102,6 +105,38 @@ fn adjusted(s: &Surface, r: Rect, adj: &Adjustment, selection: Option<&Surface>,
mix_through_selection(&fmt, r, &orig.px, &buf.px, selection)
}
+/// [`adjusted`] for an adjustment Photoshop applies to a Lab or CMYK layer's own channels
+/// (`adjust::stored_channels`): exact on the stored values, with no trip through RGB, and mixed
+/// through the selection on those values (alpha untouched).
+fn adjusted_channels(s: &Surface, r: Rect, adj: &Adjustment, selection: Option<&Surface>) -> Vec<f32> {
+ let fmt = s.format();
+ let (n, colors) = (fmt.channels(), fmt.mode.color_channels());
+ let mut raw = s.read_region(r);
+ let w = (r.width() as usize).max(1);
+ let mut coverage = vec![1.0f32; w];
+ for (j, row) in raw.chunks_mut(w * n).enumerate() {
+ if let Some(sel) = selection {
+ sel.sample_row_strided(r.y0.saturating_add(j as i32), r.x0, 1, 0, &mut coverage);
+ }
+ for (px, &k) in row.chunks_exact_mut(n).zip(&coverage) {
+ if k <= 0.0 {
+ continue;
+ }
+ let p = to_rgba(&fmt, px);
+ let gray = photocraft_color::convert::rgb_to_gray([p[0], p[1], p[2]]);
+ let Some(ch) = px.get_mut(..colors) else { continue };
+ let mut out = [0.0f32; 4];
+ let Some(out) = out.get_mut(..colors) else { continue };
+ out.copy_from_slice(ch);
+ adjust::stored_channels(adj, fmt.mode, Some(fmt.sample), out, gray);
+ for (v, o) in ch.iter_mut().zip(out.iter()) {
+ *v += (o - *v) * k;
+ }
+ }
+ }
+ raw
+}
+
/// `edited` mixed back over `orig` (straight RGBA over `r`, row-major) by the selection's
/// coverage, encoded in `fmt`: what a destructive edit writes back through a selection.
pub(crate) fn mix_through_selection(
diff --git a/crates/engine/src/pixels_tests.rs b/crates/engine/src/pixels_tests.rs
index 4a9e577a3..965544307 100644
--- a/crates/engine/src/pixels_tests.rs
+++ b/crates/engine/src/pixels_tests.rs
@@ -242,7 +242,8 @@ fn benchmark_clear_large_selection() {
}
}
-/// Image › Adjustments › Invert in a Lab document mirrors L, a and b, at every depth.
+/// Image › Adjustments › Invert in a Lab document mirrors L, a and b, at every depth: 8-bit bytes
+/// become 255 - v (a lands one step past the mirror), 16 bits mirror a and b around neutral.
#[test]
fn invert_in_a_lab_document_mirrors_l_a_and_b() {
use photocraft_color::convert::{lab_to_srgb, srgb_to_lab};
@@ -256,7 +257,8 @@ fn invert_in_a_lab_document_mirrors_l_a_and_b() {
let px = s.read_region(Rect::new(1, 1, 2, 2));
let rgba = photocraft_raster::to_rgba(&fmt, &px);
let got = srgb_to_lab([rgba[0], rgba[1], rgba[2]]);
- assert!(got.iter().zip([70.0, -13.0, 17.0]).all(|(g, w)| (g - w).abs() < 1.0), "{depth:?}: {got:?}");
+ let want = if depth == SampleType::U8 { [70.0, -13.0, 17.0] } else { [70.0, -12.0, 18.0] };
+ assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() < 0.5), "{depth:?}: {got:?}");
}
}
@@ -296,3 +298,54 @@ fn mixing_through_a_selection_matches_sampling_it_per_pixel() {
assert_eq!(mix_through_selection(&fmt, Rect::new(0, 0, 1, 1), &[[0.0; 4]], &px, None), enc[..m].to_vec());
assert!(mix_through_selection(&fmt, Rect::EMPTY, &[], &[], None).is_empty());
}
+
+/// One `fmt` pixel holding `ch`, after `adj` through an optional `k`-strength selection.
+fn adjusted_pixel(fmt: PixelFormat, ch: &[f32], adj: &Adjustment, k: Option<f32>) -> Vec<f32> {
+ let unit = Rect::new(0, 0, 1, 1);
+ let mut s = Surface::new(fmt);
+ s.write_region(unit, ch);
+ let mut sel = Surface::new(PixelFormat::GRAY8);
+ if let Some(k) = k {
+ sel.write_region(unit, &[k]);
+ }
+ adjust_surface(&mut s, adj, k.map(|_| &sel), fmt.mode);
+ s.read_region(unit)
+}
+
+fn assert_levels(got: &[f32], want_255: &[f32], what: &str) {
+ let want: Vec<f32> = want_255.iter().map(|v| v / 255.0).collect();
+ assert!(got.iter().zip(&want).all(|(g, w)| (g - w).abs() < 1e-4), "{what}: got {:?}, want {want_255:?}", got.iter().map(|v| v * 255.0).collect::<Vec<_>>());
+}
+
+/// Image › Adjustments › Invert, Posterize and Threshold change a Lab or CMYK layer's stored
+/// channels exactly as Photoshop's do (photoshop corpus lab8, lab16, cmyk8 and cmyk16 invert,
+/// posterize-4 and threshold-128 follow these rules on every pixel), with no trip through RGB.
+#[test]
+fn invert_posterize_and_threshold_change_lab_and_cmyk_channels_as_stored() {
+ let f = |v: [f32; 4]| v.map(|x| x / 255.0);
+ let cmyk = PixelFormat::new(ColorMode::Cmyk, SampleType::U8, true);
+ let ink = [51.0, 102.0, 153.0, 26.0, 255.0].map(|x| x / 255.0);
+ assert_levels(&adjusted_pixel(cmyk, &ink, &Adjustment::Invert, None), &[204.0, 153.0, 102.0, 229.0, 255.0], "CMYK invert");
+ assert_levels(&adjusted_pixel(cmyk, &ink, &Adjustment::Posterize { levels: 4 }, None), &[0.0, 85.0, 170.0, 0.0, 255.0], "CMYK posterize");
+ // Threshold: the black ink alone, or paper.
+ let dark = [200.0, 180.0, 180.0, 200.0, 255.0].map(|x| x / 255.0);
+ assert_levels(&adjusted_pixel(cmyk, &dark, &Adjustment::Threshold { level: 0.5 }, None), &[0.0, 0.0, 0.0, 255.0, 255.0], "CMYK threshold dark");
+ let paper = [0.0, 0.0, 0.0, 0.0, 1.0];
+ assert_levels(&adjusted_pixel(cmyk, &paper, &Adjustment::Threshold { level: 0.5 }, None), &[0.0, 0.0, 0.0, 0.0, 255.0], "CMYK threshold paper");
+ // A half-strength selection mixes the stored inks.
+ let half = adjusted_pixel(cmyk, &paper, &Adjustment::Invert, Some(0.5));
+ assert!(half.iter().take(4).all(|v| (v - 0.5).abs() < 0.01) && half[4] == 1.0, "{half:?}");
+
+ let lab8 = PixelFormat::new(ColorMode::Lab, SampleType::U8, true);
+ let lab16 = PixelFormat::new(ColorMode::Lab, SampleType::U16, true);
+ let px = f([77.0, 128.0, 200.0, 255.0]);
+ assert_levels(&adjusted_pixel(lab8, &px, &Adjustment::Invert, None), &[178.0, 127.0, 55.0, 255.0], "Lab 8-bit invert");
+ assert_levels(&adjusted_pixel(lab16, &px, &Adjustment::Invert, None), &[178.0, 128.0, 56.0, 255.0], "Lab 16-bit invert");
+ // A neutral grey posterizes warm: a and b step to +42.
+ let grey = f([128.0, 128.0, 128.0, 255.0]);
+ assert_levels(&adjusted_pixel(lab8, &grey, &Adjustment::Posterize { levels: 4 }, None), &[170.0, 170.0, 170.0, 255.0], "Lab posterize");
+ // Threshold compares L alone and keeps black and white neutral.
+ let level = Adjustment::Threshold { level: 128.0 / 255.0 };
+ assert_levels(&adjusted_pixel(lab8, &f([128.0, 30.0, 220.0, 255.0]), &level, None), &[255.0, 128.0, 128.0, 255.0], "Lab threshold light");
+ assert_levels(&adjusted_pixel(lab16, &f([127.0, 30.0, 220.0, 255.0]), &level, None), &[0.0, 128.0, 128.0, 255.0], "Lab threshold dark");
+}
diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs
index 52c36f14d..60a57b83b 100644
--- a/crates/gpu/src/plan.rs
+++ b/crates/gpu/src/plan.rs
@@ -1528,13 +1528,18 @@ fn to_row(t: &[f32]) -> [f32; 4096] {
/// Whether the adjustment kernel can evaluate `adj` in a document of colour `mode`: Levels and
/// Curves on CMYK ink or Lab channels convert through ICC profiles per pixel, and so do the
-/// adjustments a Lab document evaluates on L, a and b (`photocraft_compose::adjust::lab_channel_adjustment`),
-/// which only the CPU does.
+/// adjustments a Lab document evaluates on L, a and b (`photocraft_compose::adjust::lab_channel_adjustment`)
+/// and a CMYK document on its inks (`cmyk_channel_adjustment`), which only the CPU does.
pub fn adjustment_on_gpu(adj: &Adjustment, mode: photocraft_color::ColorMode) -> bool {
+ use photocraft_compose::adjust::{cmyk_channel_adjustment, lab_channel_adjustment};
let tone_in_space =
matches!(adj, Adjustment::Levels { space: ToneSpace::Cmyk | ToneSpace::Lab, .. } | Adjustment::Curves { space: ToneSpace::Cmyk | ToneSpace::Lab, .. });
- let lab_channels = mode == photocraft_color::ColorMode::Lab && photocraft_compose::adjust::lab_channel_adjustment(adj);
- !tone_in_space && !lab_channels
+ let own_channels = match mode {
+ photocraft_color::ColorMode::Lab => lab_channel_adjustment(adj),
+ photocraft_color::ColorMode::Cmyk => cmyk_channel_adjustment(adj),
+ _ => false,
+ };
+ !tone_in_space && !own_channels
}
/// A tone transfer as the shader's `t_decode`/`t_encode` exponent (0: the sRGB curve).
@@ -1761,9 +1766,12 @@ mod tests {
];
for adj in &lab_rules {
assert!(!adjustment_on_gpu(adj, ColorMode::Lab), "{adj:?}");
- for mode in [ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Grayscale] {
+ for mode in [ColorMode::Rgb, ColorMode::Grayscale] {
assert!(adjustment_on_gpu(adj, mode), "{adj:?} in {mode:?}");
}
+ // CMYK documents invert, posterize and threshold their inks, on the CPU.
+ let inks = matches!(adj, Adjustment::Invert | Adjustment::Posterize { .. } | Adjustment::Threshold { .. });
+ assert_eq!(adjustment_on_gpu(adj, ColorMode::Cmyk), !inks, "{adj:?} in CMYK");
}
// Screen-colour adjustments stay on the GPU in Lab documents.
assert!(adjustment_on_gpu(&Adjustment::Exposure { exposure: 1.0, offset: 0.0, gamma: 1.0 }, ColorMode::Lab));
diff --git a/crates/io/tests/corpus.rs b/crates/io/tests/corpus.rs
index d45956dba..776f27f89 100644
--- a/crates/io/tests/corpus.rs
+++ b/crates/io/tests/corpus.rs
@@ -156,7 +156,7 @@ const PSD_TOOLS: Source = Source { label: "psd-tools corpus", dir: "corpus/psd-t
/// Our Photoshop-authored oracles (256 files; https://github.com/storytold/photocraft-corpus),
/// grouped by feature (`smart-filters`, `effects`, `text`, `adjustments/<mode><bits>`).
-const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 133, roundtrip_floor: 258 };
+const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 145, roundtrip_floor: 258 };
/// The corpus directory; a missing corpus fails the test.
fn locate(src: &Source) -> PathBuf {
diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md
index daa8827c3..d2c9d8ccd 100644
--- a/docs/file-format-parity.md
+++ b/docs/file-format-parity.md
@@ -1,6 +1,6 @@
# File-format parity
-> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-12 · **Change:** minor (`.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026
+> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-12 · **Change:** minor (CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026
Every format Photoshop 2026 (27.11.0) reads or writes, with PhotoCraft's support, fidelity and
tests. Photoshop's list comes from its `Info.plist` `CFBundleDocumentTypes`, the PiPL read/write
@@ -21,7 +21,7 @@ XL are missing (gap G10). Remaining: 300–500 h including PSD fidelity.
| Format | Photoshop | PhotoCraft read | PhotoCraft write | Fidelity and tests |
|---|---|---|---|---|
-| PSD | R/W | yes | yes | 1/8/16/32-bit; Bitmap, Gray, Indexed, RGB, CMYK, Multichannel, Lab, Duotone; layers, groups, masks, vector masks, adjustment and fill layers, effects, text, shapes, smart objects (SoLd/lnk2; CS2 PlLd/lnkD read with their warp, kept as is or re-saved as SoLd once edited), smart filters (14 mapped, rest verbatim), slices, comps, Advanced Blending. Oracle floors: Photoshop-authored 133/256, io 146/170, psd-tools 236/309; round trip ≥ 169/170, 308/309, 258/256. Known failures: #1281, #2469 (#2374, text layers, closed 2026-10-10; #2465, CS2 `PlLd`-only smart objects, fixed 2026-10-12) |
+| PSD | R/W | yes | yes | 1/8/16/32-bit; Bitmap, Gray, Indexed, RGB, CMYK, Multichannel, Lab, Duotone; layers, groups, masks, vector masks, adjustment and fill layers, effects, text, shapes, smart objects (SoLd/lnk2; CS2 PlLd/lnkD read with their warp, kept as is or re-saved as SoLd once edited), smart filters (14 mapped, rest verbatim), slices, comps, Advanced Blending. Oracle floors: Photoshop-authored 145/258, io 146/170, psd-tools 236/309; round trip ≥ 169/170, 308/309, 258/256. Known failures: #1281, #2469 (#2374, text layers, closed 2026-10-10; #2465, CS2 `PlLd`-only smart objects, fixed 2026-10-12) |
| PSB | R/W | yes | yes | Auto-switches above 2 GB (REL-212-2); 4–5 GB fixtures generated (FILE-216-2); > 4 GB fails on Windows (#375) |
| Cloud PSD (.psdc) | R/W | no | no | Out of scope (Adobe cloud) |
| TIFF | R/W | yes | yes | 8/16/32f, Gray/RGB/CMYK, BigTIFF, every IFD, banded parallel decode; layered TIFF both byte orders (FILE-215-7: extra alpha channels not written) |
@@ -145,6 +145,7 @@ remaining gaps in #1606 stay tracked with per-sample corpus ceilings.
| 2026-10-12 | minor | `.atn`: 112 events mapped (Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, points, Bitmap) |
| 2026-10-12 | minor | DDS textures open, read-only (#1327) |
| 2026-10-12 | minor | #2465 fixed: Photoshop CS2 placed layers (`PlLd` only, file in `lnkD`) open as editable smart objects and save their edits |
+| 2026-10-12 | minor | CMYK Invert, Posterize and Threshold work on the inks and 16-bit Lab Invert mirrors a and b around neutral (oracle bad pixels: CMYK 71–77% → 9–10%, 37% → 1.6%; lab16 invert 52% → 1.2%); Photoshop-oracle floor 133 → 145 of 258 |
| 2026-10-12 | minor | DPX read and write (#1049); Cineon still missing |
| 2026-10-12 | minor | Photoshop Actions `.atn`: read and write, 90 events mapped, the rest kept verbatim (AUTO-219-2) |
| 2026-10-12 | minor | Presets table: Contours `.shc` read and write through the Contour Editor (FILE-215-15); `.csh` and `.tpl` stay missing |
diff --git a/docs/gaps.md b/docs/gaps.md
index 71169ab22..65102e2d0 100644
--- a/docs/gaps.md
+++ b/docs/gaps.md
@@ -1,6 +1,6 @@
# Where PhotoCraft falls short of Photoshop
-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed) · **Target:** Adobe Photoshop 2026
+> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (G1: CMYK Invert, Posterize and Threshold on the inks, 16-bit Lab Invert; Photoshop-oracle floor 145/258; G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed) · **Target:** Adobe Photoshop 2026
Every known shortfall against Photoshop 2026 (27.11.0), one entry each, ranked by user impact
on the way to beta. This is the work list agents pick from: take the highest-ranked entry that
@@ -43,7 +43,7 @@ what an agent can't finish alone.
- **Missing:** PSDs we write are not always accepted by Photoshop, and Photoshop-authored files
don't always render as Photoshop renders them.
- **Evidence:** corpus floors (`crates/io/tests/corpus.rs`, scorecard): Photoshop-authored
- oracles 133/256 (52%), io corpus 146/170 (86%), psd-tools 236/309 (76%); round trips ~100%
+ oracles 145/258 (56%; CMYK Invert, Posterize and Threshold on the inks 2026-10-12), io corpus 146/170 (86%), psd-tools 236/309 (76%); round trips ~100%
within the corpus. Smart-filter re-render matches 5/30. User reports: a PSD with text layers
re-saved unmodified would not reopen (#2374, closed 2026-10-10), Photoshop 26.6 cannot reopen our files (#1281), "PSD not
opening in Photoshop" (#2469), a CS2-era smart object unavailable (#2465, fixed 2026-10-12), PSB smart object
@@ -256,6 +256,7 @@ what an agent can't finish alone.
| 2026-10-12 | minor | G11: `.atn` import maps 112 events (Select and Mask, Spatter, Ripple, Crystallize, Pattern Fill, mask targeting, Step Backward, points, Bitmap) |
| 2026-10-12 | minor | G9: toolbar tool shortcut editing (#4086); the remaining workspace gaps stay open |
| 2026-10-12 | minor | G1: #2465 (CS2 `PlLd`-only smart objects) marked fixed |
+| 2026-10-12 | minor | G1: CMYK Invert, Posterize and Threshold work on the inks, 16-bit Lab Invert keeps greys neutral; Photoshop-oracle floor 133 → 145 of 258 (145 already passed on main) |
| 2026-10-12 | minor | G5: spring-loaded tool letters done (#2672) |
| 2026-10-12 | minor | G10: DPX read and write (#1049); Cineon remains open |
| 2026-10-12 | minor | G4: checkbox-only Brush Settings rows preserve the open controls and Smoothing settings (#4283); no readiness percentage or estimate changed |
diff --git a/docs/scorecard.md b/docs/scorecard.md
index 6741f981e..1ad3f3aa3 100644
--- a/docs/scorecard.md
+++ b/docs/scorecard.md
@@ -7,7 +7,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
| Area | Done | Partial | Missing | Key numbers |
|---|---:|---:|---:|---|
| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 17 not run, 5 not measurable yet (baseline 2026-10-05) |
-| [File compatibility](#file-compatibility) | 6 | 13 | 8 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 |
+| [File compatibility](#file-compatibility) | 6 | 13 | 8 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 145/256, round trip ≥ 258/256 |
| [Tools](#tools) | 28 | 14 | 1 | |
| [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 26 of 156 (target 0) |
| [Type](#type) | 4 | 3 | 2 | |
@@ -94,7 +94,7 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
|---|---|---:|---:|---:|
| io corpus | `corpus/psd` | 170 | 146 | 169 |
| psd-tools corpus | `corpus/psd-tools` | 309 | 236 | 308 |
-| photoshop oracles | `corpus/photoshop` | 256 | 133 | 258 |
+| photoshop oracles | `corpus/photoshop` | 256 | 145 | 258 |
| Id | Target | Status | Issue | Note |
|---|---|---|---|---|