PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Assign Profile and Convert to Profile: Preview, as in Photoshop; Black Point Compensation spelled out (#6825)
Tom Inglingb3c8a2a7f3c7
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 69ed5ebcd..15a5db617 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -1838,6 +1838,7 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &eif !fresh && !app.filter_preview_worker.busy() {let doc = app.session.documents().get(idx)?.doc.clone();let documents: Vec<_> = app.session.documents().iter().map(|d| d.doc.clone()).collect();+ let color = app.session.color.settings.clone();let task = request.clone();if let Err(error) = app.filter_preview_worker.start(request.clone(), ctx.clone(), move |cancel| {let t0 = crate::gpu_canvas::now_ms();@@ -1850,6 +1851,7 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &eSome(cancel),task.target,&documents,+ &color,).map(|doc| {let buffer = photocraft_compose::flatten(&doc);@@ -1888,12 +1890,14 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &eNone,request.target,&documents,+ &app.session.color.settings,).map(std::sync::Arc::new);if let Some(r) = &result {let buf = photocraft_compose::flatten(r);let t1 = crate::gpu_canvas::now_ms();- let (display, _) = canvas_display(app, &doc, None);+ // The preview's own profile: Assign and Convert to Profile change it (see `publish_filter_preview`).+ let (display, _) = canvas_display(app, r, None);if let Some(gpu) = app.gpu.as_ref() {gpu.upload_buffer_full(key, &texture_buffer(display.as_deref(), &buf), doc.depth);}@@ -1929,7 +1933,9 @@ fn publish_filter_preview(let result = computed.and_then(|(result, buffer)| {let t0 = crate::gpu_canvas::now_ms();let doc = app.session.documents().get(idx)?.doc.clone();- let (display, _) = canvas_display(app, &doc, None);+ // Shown through the preview's own colour management, as the result will be: Assign and+ // Convert to Profile change the profile (and Convert the mode), not just the pixels.+ let (display, _) = canvas_display(app, &result, None);// A GPU texture is an optimisation: headless/CPU tests must still see the preview.if let Some(gpu) = app.gpu.as_ref() {gpu.upload_buffer_full(request.doc.0 ^ (1u64 << 61), &texture_buffer(display.as_deref(), &buffer), doc.depth);@@ -1940,6 +1946,16 @@ fn publish_filter_preview(app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request, result, committing: false });}+/// The document whose colour management the canvas texture under `key` is shown through: a filter+/// preview's own result (it keeps the document's id, so Proof Colors still apply), else `doc`.+/// Assign Profile changes only the profile, so through `doc`'s its preview looked unchanged.+fn filter_preview_display_doc(app: &PhotocraftApp, doc: &std::sync::Arc<Document>, key: u64) -> std::sync::Arc<Document> {+ if key != doc.id.0 ^ (1u64 << 61) {+ return doc.clone();+ }+ app.filter_preview.as_ref().and_then(|p| p.result.clone()).filter(|r| r.id == doc.id).unwrap_or_else(|| doc.clone())+}+/// After a filter dialog's OK: `Some(shown)` while its preview stands in for the background job/// that commits it (the document unchanged and the job still running, for this document's views),/// `None` otherwise (other documents' views included). A job that lands, fails or is cancelled@@ -3061,6 +3077,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:on_gpu = true;app.navigator_gpu.remove(&doc.id);let original_size = [doc.size.width.div_ceil(k), doc.size.height.div_ceil(k)];+ let shown = filter_preview_display_doc(app, &doc, key);let params = crate::gpu_canvas::ViewParams {doc: key,doc_size: preview_size,@@ -3077,7 +3094,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:},pixel_grid: false,view_key: egui::Id::new(("pc-canvas-proxy", ctx.viewport_id(), idx)).value(),- display: sync_display_lut(app, &doc, key, output),+ display: sync_display_lut(app, &shown, key, output),output: output.unwrap_or(0),hdr: hdr_preview(app, &doc),offset: [0.0, 0.0],@@ -6308,13 +6325,21 @@ mod tests {app.filter_preview_worker.start(key.clone(), ctx.clone(), move |_| {wait.recv_timeout(std::time::Duration::from_secs(60)).expect("the test releases the job");- let result =- crate::filter_dialog::preview_document_for_target(&doc, key.active, &key.command, &key.params, key.k, None, key.target, &documents).map(- |d| {- let buffer = photocraft_compose::flatten(&d);- (std::sync::Arc::new(d), buffer)- },- );+ let result = crate::filter_dialog::preview_document_for_target(+ &doc,+ key.active,+ &key.command,+ &key.params,+ key.k,+ None,+ key.target,+ &documents,+ &Default::default(),+ )+ .map(|d| {+ let buffer = photocraft_compose::flatten(&d);+ (std::sync::Arc::new(d), buffer)+ });crate::filter_preview_worker::Computed { result, ms: 0.0 }}).expect("the worker starts");@@ -8690,6 +8715,64 @@ mod rotation_preview_isolation_tests {}}+ /// Assign and Convert to Profile have Photoshop's Preview. Assign changes only the profile, so+ /// its preview must be shown through the profile it assigns (through the document's own it+ /// looked unchanged); Convert keeps the look of in-gamut colours. Their `working` is the+ /// working space from Color Settings, not the built-in default.+ #[test]+ fn profile_dialogs_preview_through_the_profile_they_choose() {+ let mut session = photocraft_engine::Session::new();+ session.color.settings.monitor_profile = "srgb".into();+ session.color.settings.working_rgb = "display-p3".into();+ session.execute("file.new", json!({"width": 4, "height": 4})).unwrap();+ session.execute("edit.assignProfile", json!({"profile": "srgb"})).unwrap();+ session+ .edit("Fixture", |doc, _| {+ let layer = doc.layers.last_mut().unwrap();+ for y in 0..4 {+ for x in 0..4 {+ layer.surface_mut().unwrap().write_pixel(x, y, &[0.6, 0.35, 0.25, 1.0]);+ }+ }+ Ok(())+ })+ .unwrap();+ let mut app = PhotocraftApp::new(session, crate::Services::default());+ let shown_pixels = |app: &PhotocraftApp, doc: &Document| {+ let (display, _) = canvas_display(app, doc, None);+ display_image(display.as_deref(), &photocraft_compose::flatten(doc)).pixels[0]+ };+ let original = app.session.active().unwrap().doc.clone();+ let before = shown_pixels(&app, &original);+ let preview = |app: &mut PhotocraftApp, command: &str, profile: &str| {+ crate::filter_dialog::open(app, command).unwrap();+ let fields = &mut app.ui.dialogs.last_mut().unwrap().fields;+ assert_eq!(fields.get("__preview"), Some(&json!(true)), "{command} has a Preview checkbox, on");+ fields.insert("profile".into(), json!(profile));+ ensure_filter_preview(app, 0, &egui::Context::default()).expect("a preview");+ let result = app.filter_preview.as_ref().unwrap().result.clone().unwrap();+ let key = original.id.0 ^ (1u64 << 61);+ assert_eq!(filter_preview_display_doc(app, &original, key).icc_profile, result.icc_profile, "the canvas shows it through its own profile");+ assert_eq!(app.session.active().unwrap().doc.icc_profile, original.icc_profile, "previewing changes nothing");+ app.ui.dialogs.clear();+ app.filter_preview = None;+ result+ };+ let description = |doc: &Document| photocraft_engine::color_cmds::profile_from_bytes(doc.icc_profile.as_ref().unwrap()).unwrap().description.clone();++ let assigned = preview(&mut app, "edit.assignProfile", "prophoto-compat");+ assert_eq!(description(&assigned), photocraft_cms::Builtin::ProPhotoCompat.description());+ let shown = shown_pixels(&app, &assigned);+ assert!(shown.r().abs_diff(before.r()) > 10 || shown.b().abs_diff(before.b()) > 10, "the assigned profile shows: {before:?} → {shown:?}");++ let converted = preview(&mut app, "edit.convertToProfile", "working");+ assert_eq!(description(&converted), photocraft_cms::Builtin::DisplayP3.description(), "`working` is Color Settings' working space");+ let shown = shown_pixels(&app, &converted);+ for (a, b) in [(shown.r(), before.r()), (shown.g(), before.g()), (shown.b(), before.b())] {+ assert!(a.abs_diff(b) <= 2, "converting keeps the look: {before:?} → {shown:?}");+ }+ }+/// A CPU-only app must be able to generate the actual rotation preview without a/// GPU adapter; checking the result also verifies Preview off and document integrity.#[test]diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rsindex fa7cb9524..8cf7f3519 100644--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@ -7,6 +7,7 @@use std::sync::Arc;use photocraft_doc::Document;+use photocraft_engine::color_cmds::ColorSettings;use serde_json::{Map, Value, json};use crate::PhotocraftApp;@@ -279,6 +280,9 @@ pub const PREVIEWED: &[&str] = &["layer.matting.colorDecontaminate","layer.layerStyle.scaleEffects","type.warpText",+ // The preview shows the image through the chosen profile, as Photoshop's Preview does.+ "edit.assignProfile",+ "edit.convertToProfile",];pub fn has_dialog(command: &str) -> bool {@@ -293,8 +297,6 @@ pub fn has_dialog(command: &str) -> bool {command,"image.trim"| "select.refineEdge"- | "edit.assignProfile"- | "edit.convertToProfile"| "view.proofSetup"| "layer.layerStyle.globalLight"| "image.mode.colorTable"@@ -556,6 +558,10 @@ pub(crate) fn source_label(key: &str) -> String {if key == "localized" {return "Localized Color Clusters".into();}+ // Convert to Profile's, Proof Setup's…: spelled out, as in Photoshop (not "Bpc").+ if key == "bpc" {+ return "Black Point Compensation".into();+ }// A name that already reads as text ("iPhone 14 Pro Max", "Web 1920") is not camelCase.if key.contains(' ') {return key.to_string();@@ -875,12 +881,23 @@ pub fn preview_document_with(k: u32,cancel: Option<&photocraft_engine::jobs::JobCtx>,) -> Option<Document> {- preview_document_for_target(doc, active, command, params, k, cancel, photocraft_engine::channel_cmds::ChannelTarget::Composite, &[])+ preview_document_for_target(+ doc,+ active,+ command,+ params,+ k,+ cancel,+ photocraft_engine::channel_cmds::ChannelTarget::Composite,+ &[],+ &ColorSettings::default(),+ )}/// Run the preview with the same pixel target as the final command, including individual channels./// `documents` are the open documents in session order: document params (Match Color's Source,-/// Displace's map) index into them.+/// Displace's map) index into them. `color` is the app's Edit › Color Settings: Assign and Convert+/// to Profile's `working` is the working space chosen there, not the built-in default.#[allow(clippy::too_many_arguments)]pub(crate) fn preview_document_for_target(doc: &Document,@@ -891,6 +908,7 @@ pub(crate) fn preview_document_for_target(cancel: Option<&photocraft_engine::jobs::JobCtx>,target: photocraft_engine::channel_cmds::ChannelTarget,documents: &[Arc<Document>],+ color: &ColorSettings,) -> Option<Document> {if command == crate::select_mask_view::COMMAND {return crate::select_mask_view::preview_document(doc, active, params, k, cancel);@@ -900,6 +918,7 @@ pub(crate) fn preview_document_for_target(let (run_k, reduce_k) = if command == "type.warpText" { (1, k) } else { (k, 1) };let proxy = crate::proxy::proxy_document(doc, run_k);let mut s = photocraft_engine::Session::new();+ s.color.settings = color.clone();s.set_inline_job_ctx(cancel.cloned());let doc_id = proxy.id;s.add_document(proxy, None);@@ -1050,6 +1069,16 @@ mod tests {assert_eq!(source_label("centerX"), "Center X");}+ /// Convert to Profile's and Proof Setup's `bpc` read "Bpc": spelled out, and translated.+ #[test]+ fn black_point_compensation_is_spelled_out() {+ assert_eq!(source_label("bpc"), "Black Point Compensation");+ // English is the source: it has no catalog.+ for lang in crate::i18n::Lang::all().filter(|l| l.code() != "en") {+ assert!(crate::i18n::has(lang, "Black Point Compensation"), "{lang:?}");+ }+ }+fn key() -> FilterPreviewKey {FilterPreviewKey {doc: photocraft_doc::DocId(7),@@ -1569,6 +1598,7 @@ mod tests {None,photocraft_engine::channel_cmds::ChannelTarget::Composite,&documents,+ &ColorSettings::default(),).unwrap_or_else(|| panic!("{label}: no preview"));let got = preview.layers[0].surface().unwrap().pixel(24, 16);@@ -1584,6 +1614,7 @@ mod tests {None,photocraft_engine::channel_cmds::ChannelTarget::Composite,&documents,+ &ColorSettings::default(),).unwrap();let px = preview.layers[0].surface().unwrap().pixel(12, 8);diff --git a/crates/ui-egui/src/filter_preview_ui.rs b/crates/ui-egui/src/filter_preview_ui.rsindex 27f530980..e4afdb756 100644--- a/crates/ui-egui/src/filter_preview_ui.rs+++ b/crates/ui-egui/src/filter_preview_ui.rs@@ -329,6 +329,7 @@ mod tests {None,ChannelTarget::Alpha(0),&[],+ &Default::default(),).unwrap();let ctx = egui::Context::default();