diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs index 8379e54ec47..453446642cc 100644 --- a/crates/compose/src/adjust.rs +++ b/crates/compose/src/adjust.rs @@ -204,10 +204,19 @@ fn apply_depth_mode(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, dept Adjustment::ChannelMixer { cmyk: Some(rows), monochrome, .. } => { // A CMYK mixer outside a CMYK document: on the screen colour's separation. use photocraft_color::convert::{cmyk_to_rgb, rgb_to_cmyk}; + // In a 32-bit document the change is added as a difference of two round trips, as + // [`tone::tone_in_space`] does, so samples past 0..1 the separation clips keep their + // magnitude (an identity mixer leaves an HDR highlight as it is). + let float = depth == Some(SampleType::F32); map_rgb(buf, |c| { - let mut ink = rgb_to_cmyk(c); - mix_inks(rows, *monochrome, &mut ink); - cmyk_to_rgb(ink) + let ink = rgb_to_cmyk(c); + let mut mixed = ink; + mix_inks(rows, *monochrome, &mut mixed); + if !float { + return cmyk_to_rgb(mixed); + } + let (before, after) = (cmyk_to_rgb(ink), cmyk_to_rgb(mixed)); + std::array::from_fn(|i| c[i] + after[i] - before[i]) }) } Adjustment::ChannelMixer { matrix, monochrome, .. } if depth == Some(SampleType::F32) => { diff --git a/crates/compose/src/adjust/tone_tests.rs b/crates/compose/src/adjust/tone_tests.rs index 5d49d8b869d..d189f95a010 100644 --- a/crates/compose/src/adjust/tone_tests.rs +++ b/crates/compose/src/adjust/tone_tests.rs @@ -900,9 +900,19 @@ fn cmyk_channel_mixer_mixes_the_inks() { more_cyan[0][4] = 0.3; let more_cyan = Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: false, cmyk: Some(more_cyan) }; - let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), c); - apply_in(&more_cyan, &mut b, Transfer::Srgb, Some(SampleType::U8), ColorMode::Cmyk); - assert!(b.px[0][0] < round_trip[0] - 0.05, "more cyan ink takes red away: {:?}", b.px[0]); + for mode in [ColorMode::Cmyk, ColorMode::Rgb] { + let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), c); + apply_in(&more_cyan, &mut b, Transfer::Srgb, Some(SampleType::U8), mode); + assert!(b.px[0][0] < round_trip[0] - 0.05, "{mode:?}: more cyan ink takes red away: {:?}", b.px[0]); + } + // In a 32-bit document an identity mixer keeps samples past 0..1, and an edit still applies. + let hdr = [2.0, 0.5, 0.25, 1.0]; + let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), hdr); + apply_in(&mixer, &mut b, Transfer::Gamma(1.0), Some(SampleType::F32), ColorMode::Rgb); + assert!((0..3).all(|i| (b.px[0][i] - hdr[i]).abs() < 1e-5), "identity keeps the HDR highlight: {:?}", b.px[0]); + let mut b = Buffer::filled(crate::Rect::new(0, 0, 1, 1), hdr); + apply_in(&more_cyan, &mut b, Transfer::Gamma(1.0), Some(SampleType::F32), ColorMode::Rgb); + assert!(b.px[0][0] > 1.0 && b.px[0][0] < hdr[0] - 0.05, "more cyan takes red away and keeps it HDR: {:?}", b.px[0]); } /// Photoshop's own CMYK Channel Mixer (photoshop corpus cmyk16 channel-mixer.psd, made with diff --git a/crates/io/src/adjust_map.rs b/crates/io/src/adjust_map.rs index b7b61dcb6f3..75b5e209f33 100644 --- a/crates/io/src/adjust_map.rs +++ b/crates/io/src/adjust_map.rs @@ -168,7 +168,7 @@ pub fn unreadable_reason(key: &[u8; 4], data: &[u8]) -> Option { /// composite record first, then one per document channel (see [`Channels`]). pub fn parse(key: &[u8; 4], data: &[u8], cged: Option<&[u8]>, channels: Channels) -> Adjustment { // A CMYK document's mixer has Photoshop's four ink outputs. - if key == b"mixr" && channels == Channels::Cmyk { + if key == b"mixr" && channels == Channels::Cmyk && !legacy_rgb_mixer(data) { return parse_cmyk_mixer(data).unwrap_or_else(|| unsupported(key, data)); } // In other modes a mixer using the fourth (black) source has no RGB meaning: keep it raw. @@ -682,6 +682,17 @@ fn parse_channel_mixer(d: &[u8]) -> Option { /// image has no black ink). A monochrome block may carry only its first record (the mix the /// black ink gets); no Photoshop sample of one exists yet, and an unedited one is saved back as /// it was read. +/// A Channel Mixer block PhotoCraft wrote in its RGB layout before CMYK mixers were read +/// ([`encode::channel_mixer`]): its fourth record repeats the first and no record uses the black +/// source. Read as CMYK rows, that fourth record would set the black ink to the cyan mix +/// (`K' = C`), so such a block in a CMYK document keeps the RGB reading it had. A CMYK block +/// whose black output is exactly the cyan mix with no black source would read the same way. +fn legacy_rgb_mixer(d: &[u8]) -> bool { + let record = |r: usize| d.get(4 + r * 10..4 + (r + 1) * 10); + let no_black = (0..4).all(|r| record(r).and_then(|rec| rec.get(6..8)) == Some(&[0, 0][..])); + d.len() == 44 && no_black && record(0).is_some() && record(0) == record(3) +} + fn parse_cmyk_mixer(d: &[u8]) -> Option { if be16(d, 0)? != 1 { return None; diff --git a/crates/io/src/adjust_map/tests.rs b/crates/io/src/adjust_map/tests.rs index aeab9b149ad..8415d9702cc 100644 --- a/crates/io/src/adjust_map/tests.rs +++ b/crates/io/src/adjust_map/tests.rs @@ -472,6 +472,15 @@ fn malformed_channel_mixers_are_unsupported() { ); let ours = write(&Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: false, cmyk: None }); assert!(matches!(parse(b"mixr", &ours[0].1, None, Channels::Other), Adjustment::ChannelMixer { .. })); + // Our RGB layout in a CMYK document (written before CMYK mixers were read: its fourth record + // repeats the red row) keeps its RGB reading: read as ink rows, the black ink became the cyan mix. + let legacy = [[0.8, 0.2, 0.0, 0.05], [0.1, 0.9, 0.0, 0.0], [0.0, 0.3, 0.7, 0.0]]; + let block = write(&Adjustment::ChannelMixer { matrix: legacy, monochrome: false, cmyk: None }); + assert!( + matches!(parse(b"mixr", &block[0].1, None, Channels::Cmyk), Adjustment::ChannelMixer { cmyk: None, matrix, .. } if matrix == legacy), + "{:?}", + parse(b"mixr", &block[0].1, None, Channels::Cmyk) + ); assert!(matches!(parse(b"mixr", &[0, 1, 0, 0, 0, 100], None, Channels::Rgb), Adjustment::Unsupported { .. })); } diff --git a/crates/ui-egui/src/dialogs/color_settings_presets_tests.rs b/crates/ui-egui/src/dialogs/color_settings_presets_tests.rs index 9ef25414fe0..6453ad76278 100644 --- a/crates/ui-egui/src/dialogs/color_settings_presets_tests.rs +++ b/crates/ui-egui/src/dialogs/color_settings_presets_tests.rs @@ -214,6 +214,31 @@ fn a_control_set_before_the_first_pass_shows_custom() { } } +#[test] +fn reset_set_before_the_first_pass_shows_custom() { + let name = "North America General Purpose 2"; + let stored = preset_setup(&ColorSettings::default(), name, "srgb").unwrap(); + for (value, expected) in [(json!(true), CUSTOM_SETTINGS), (json!(false), name)] { + let mut app = app_with(stored.clone()); + let id = open_dialog(&mut app, 0); + dialog_set(&mut app, id, "reset", value.clone()); + assert_eq!(fields(&app, id)["preset"], expected, "reset {value}"); + } +} + +#[test] +fn monitor_color_follows_a_monitor_profile_set_before_the_first_pass() { + let stored = preset_setup(&ColorSettings { monitor_profile: "srgb".into(), ..ColorSettings::default() }, "Monitor Color", "srgb").unwrap(); + let mut app = app_with(stored); + let id = open_dialog(&mut app, 0); + dialog_set(&mut app, id, "monitorProfile", json!("display-p3")); + let f = fields(&app, id); + assert_eq!((&f["preset"], &f["workingRgb"]), (&json!("Monitor Color"), &json!("display-p3")), "loaded at once"); + crate::dialogs::testing::confirm(&mut app, id).unwrap(); + let c = &app.session.color().settings; + assert_eq!((c.preset.as_str(), c.working_rgb.as_str()), ("Monitor Color", "display-p3")); +} + #[test] fn a_setup_set_before_the_first_pass_loads_too() { let expected = preset_setup(&ColorSettings::default(), "Europe Prepress 3", "srgb").unwrap(); diff --git a/crates/ui-egui/src/dialogs/filter_dialog/color_settings_presets.rs b/crates/ui-egui/src/dialogs/filter_dialog/color_settings_presets.rs index 05cd3cec0b8..c58fce05406 100644 --- a/crates/ui-egui/src/dialogs/filter_dialog/color_settings_presets.rs +++ b/crates/ui-egui/src/dialogs/filter_dialog/color_settings_presets.rs @@ -80,8 +80,14 @@ pub(crate) fn reconcile(app: &mut PhotocraftApp, ctx: &egui::Context) { set_hidden(app, APPLIED, json!(CUSTOM_SETTINGS)); return; } + let base = tracked(&f); + // A monitor profile set before the first pass moves Monitor Color's working RGB too. + if shown == MONITOR_COLOR && monitor_rgb_moved(app, &f, &base) { + load(app, ctx, &f, &shown); + return; + } set_hidden(app, APPLIED, json!(shown)); - set_hidden(app, BASE, Value::Object(tracked(&f))); + set_hidden(app, BASE, Value::Object(base)); return; }; if shown != applied { @@ -137,10 +143,12 @@ fn load(app: &mut PhotocraftApp, ctx: &egui::Context, f: &Map, na set_hidden(app, BASE, Value::Object(base)); } -/// A tracked control no longer shows the stored Color Settings' value. +/// A tracked control no longer shows the stored Color Settings' value (for Reset, which the +/// settings don't hold, its opening value: off). fn differs_from_stored(app: &PhotocraftApp, f: &Map) -> bool { let Ok(Value::Object(stored)) = serde_json::to_value(&app.session.color().settings) else { return false }; - tracked(f).iter().any(|(k, v)| stored.get(k).is_some_and(|s| !same(s, v))) + let opening = |k: &str| stored.get(k).cloned().or_else(|| (k == "reset").then_some(Value::Bool(false))); + tracked(f).iter().any(|(k, v)| opening(k).is_some_and(|s| !same(&s, v))) } /// The working RGB Monitor Color gave (in `base`) is no longer the one the shown monitor profile gives.