From 6f1164e034fc4f04214ce7f9014a4fe46a4b50ba Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 24 Oct 2026 22:40:40 -0600 Subject: [PATCH] fix(io, engine): Embed Color Profile writes sRGB for an untagged document (#9325) An untagged document (a new one in the default working space, or a file opened without a profile) is treated as sRGB (sGray for grayscale), but Export As, Save As and Save for Web wrote no profile for it, so viewers had to guess. Photoshop embeds sRGB IEC61966-2.1 for both a new and a "Don't Color Manage" document when the profile is embedded. A flat export to a format that carries profiles now tags an untagged image with the profile assumed for its mode; the codec still leaves it out when embedding is off, and formats without profiles are unchanged. Save for Web does the same when Embed Color Profile is on. Tests that relied on untagged exports carrying no profile (an EXIF ColorSpace round trip, the Export As proxy-estimate premise, an Affinity preview fixture, a WebP lossy check) now say so explicitly. Co-Authored-By: Claude Opus 5.5 --- crates/automation/src/rpc.rs | 3 +- crates/engine/src/color_cmds.rs | 5 ++- crates/engine/src/web_cmds.rs | 5 ++- crates/engine/src/web_cmds/tests.rs | 18 +++++++++++ crates/engine/tests/it/affinity.rs | 5 ++- crates/io/src/flat.rs | 50 +++++++++++++++++++++++++++++ crates/ui-egui/src/export_dialog.rs | 9 +++++- 7 files changed, 90 insertions(+), 5 deletions(-) diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index b98094eb5d6..74eb51be43b 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -662,7 +662,8 @@ mod tests { } h.handle("doc.save", json!({"path": path.to_string_lossy(), "quality": 85})).unwrap(); let bytes = std::fs::read(&path).unwrap(); - assert_eq!(bytes.get(12..16), Some(&b"VP8 "[..]), "a WebP saved with a quality is lossy"); + // The lossy chunk; an extended WebP (one carrying a colour profile) puts `VP8X` first. + assert!(bytes.windows(4).any(|c| c == b"VP8 ") && !bytes.windows(4).any(|c| c == b"VP8L"), "a WebP saved with a quality is lossy"); std::fs::remove_dir_all(dir).unwrap(); } diff --git a/crates/engine/src/color_cmds.rs b/crates/engine/src/color_cmds.rs index 9e9c0aafd35..b443441a182 100644 --- a/crates/engine/src/color_cmds.rs +++ b/crates/engine/src/color_cmds.rs @@ -1675,7 +1675,10 @@ mod settings_tests { fn exif_srgb_jpeg_round_trip() { let mut s = Session::new(); s.execute("edit.colorSettings", json!({"workingRgb": "display-p3"})).unwrap(); - let r = photocraft_io::export(&untagged_camera_file(Some(1)), "x.jpg", &photocraft_io::ExportOptions::default()).unwrap(); + // Embed Color Profile off: an untagged document is otherwise tagged sRGB on export (#9325). + let mut opts = photocraft_io::ExportOptions::default(); + opts.encode.embed_icc = false; + let r = photocraft_io::export(&untagged_camera_file(Some(1)), "x.jpg", &opts).unwrap(); let back = photocraft_io::import("x.jpg", &r.bytes).unwrap().document; assert!(back.icc_profile.is_none(), "no ICC profile in the file"); let (_, r) = s.open_document(back, None); diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rs index 3738aa6ca3a..5e269432551 100644 --- a/crates/engine/src/web_cmds.rs +++ b/crates/engine/src/web_cmds.rs @@ -900,7 +900,10 @@ fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u let (wdoc, sx, sy) = web_document(&doc, p, &st)?; let buf = photocraft_compose::flatten(&wdoc); let bw = wdoc.size.width as usize; - let icc = wdoc.icc_profile.as_deref().map(|v| v.as_slice()); + // An untagged document is treated as sRGB (see [`web_document`]); with Embed Color Profile + // on, the file says so, as Photoshop's does (#9325). + let srgb = (st.embed_icc && wdoc.icc_profile.is_none()).then(|| photocraft_cms::Builtin::Srgb.profile().to_bytes().to_vec()); + let icc = wdoc.icc_profile.as_deref().map(|v| v.as_slice()).or(srgb.as_deref()); let xmp = web_xmp(&doc, st.metadata); let exif = web_exif(&doc, st.metadata); let dpi = wdoc.resolution_dpi; diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rs index 38fa82c188f..42e7d36aa52 100644 --- a/crates/engine/src/web_cmds/tests.rs +++ b/crates/engine/src/web_cmds/tests.rs @@ -1163,3 +1163,21 @@ fn quick_export_exr_preserves_linear_hdr_and_alpha() { s.execute("file.export.quickExport", json!({"path": format!("{dir}/explicit.exr")})).unwrap(); assert_eq!(decode(&format!("{dir}/explicit.exr")).dimensions(), (2, 1)); } + +/// #9325: an untagged document is treated as sRGB, and with Embed Color Profile on the file +/// says so, as Photoshop's Save for Web does; off, it still carries none. +#[test] +fn embed_color_profile_tags_an_untagged_document_srgb() { + let mut s = session(8); + assert!(s.active().unwrap().doc.icc_profile.is_none(), "a new document is untagged"); + let dir = tmp("assumed-icc"); + let profile = |path: &str| decode(path).icc.map(|b| photocraft_cms::Profile::parse(&b).unwrap().description); + for (format, ext) in [("jpeg", "jpg"), ("png24", "png")] { + let on = format!("{dir}/on.{ext}"); + s.execute("file.export.saveForWebLegacy", json!({"format": format, "embedIcc": true, "path": on})).unwrap(); + assert_eq!(profile(&on).as_deref(), Some(photocraft_cms::Builtin::Srgb.description()), "{format}"); + let off = format!("{dir}/off.{ext}"); + s.execute("file.export.saveForWebLegacy", json!({"format": format, "embedIcc": false, "path": off})).unwrap(); + assert_eq!(profile(&off), None, "{format}"); + } +} diff --git a/crates/engine/tests/it/affinity.rs b/crates/engine/tests/it/affinity.rs index 2d56fbf9863..06c4bf4e66c 100644 --- a/crates/engine/tests/it/affinity.rs +++ b/crates/engine/tests/it/affinity.rs @@ -12,7 +12,10 @@ use serde_json::json; fn fixture() -> Vec { let mut src = Session::new(); src.execute("file.new", json!({"width": 2, "height": 1, "background": "#dc283c"})).unwrap(); - let png = photocraft_io::export(&src.active().unwrap().doc, "preview.png", &Default::default()).unwrap().bytes; + // Affinity's previews carry no colour profile. + let mut opts = photocraft_io::ExportOptions::default(); + opts.encode.embed_icc = false; + let png = photocraft_io::export(&src.active().unwrap().doc, "preview.png", &opts).unwrap().bytes; // Observed v12 preview envelope, without a native object graph. let mut bytes = vec![0; 72]; bytes[..4].copy_from_slice(b"\x00\xffKA"); diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index afff94853d1..9833ebfcb31 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -664,6 +664,16 @@ pub(crate) fn export_flat_with(doc: &Document, format: Format, opts: &ExportOpti img = gray; warnings.push(format!("colours converted to sGray; {format:?} can't embed the document's colour profile")); } + } else if img.icc.is_none() { + // An untagged document is in the profile assumed for its mode (sRGB, sGray), which is + // what Photoshop embeds for it (#9325): a JPEG or PNG with Embed Color Profile then says + // so instead of leaving viewers to guess. The codec still leaves it out when embedding + // is off. + if img.layout().is_rgb() { + img.icc = Some(Builtin::Srgb.profile().to_bytes().to_vec()); + } else if img.layout().is_gray() { + img.icc = Some(Builtin::SGray.profile().to_bytes().to_vec()); + } } if format == Format::Tga { img = tga_image(doc, img, opts.tga_bits, &mut warnings)?; @@ -1037,3 +1047,43 @@ mod cmyk_band_tests { assert_eq!(first[3], 255); } } + +#[cfg(test)] +mod assumed_profile_tests { + use super::*; + use photocraft_cms::{Builtin, Profile}; + use photocraft_color::Color; + + fn doc(mode: ColorMode) -> Document { + Document::with_background("t", Size::new(16, 12), mode, SampleType::U8, Color::rgba(0.78, 0.39, 0.2, 1.0)) + } + + fn embedded(r: &ExportResult) -> Option { + codecs::decode(&r.bytes).unwrap().icc.map(|b| Profile::parse(&b).unwrap().description) + } + + /// #9325: an untagged document is in the profile assumed for its mode, and Photoshop embeds + /// that (sRGB IEC61966-2.1 for a new or untagged RGB document) when the profile is embedded. + #[test] + fn untagged_documents_embed_the_profile_they_are_assumed_to_be_in() { + let opts = ExportOptions::default(); + assert!(opts.encode.embed_icc); + for ext in ["jpg", "png", "tif", "webp"] { + let r = crate::export(&doc(ColorMode::Rgb), ext, &opts).unwrap(); + assert_eq!(embedded(&r).as_deref(), Some(Builtin::Srgb.description()), "{ext}"); + } + let r = crate::export(&doc(ColorMode::Grayscale), "png", &opts).unwrap(); + assert_eq!(embedded(&r).as_deref(), Some(Builtin::SGray.description())); + // Embed Color Profile off: still none. + let mut off = ExportOptions::default(); + off.encode.embed_icc = false; + assert_eq!(embedded(&crate::export(&doc(ColorMode::Rgb), "jpg", &off).unwrap()), None); + // A format without profiles gets no warning about one. + let r = crate::export(&doc(ColorMode::Rgb), "bmp", &opts).unwrap(); + assert!(!r.warnings.iter().any(|w| w.contains("profile")), "{:?}", r.warnings); + // A tagged document keeps its own profile. + let mut p3 = doc(ColorMode::Rgb); + p3.icc_profile = Some(Arc::new(Builtin::DisplayP3.profile().to_bytes().to_vec())); + assert_eq!(embedded(&crate::export(&p3, "jpg", &opts).unwrap()).as_deref(), Some(Builtin::DisplayP3.description())); + } +} diff --git a/crates/ui-egui/src/export_dialog.rs b/crates/ui-egui/src/export_dialog.rs index 29ee74b1aa3..c687156e150 100644 --- a/crates/ui-egui/src/export_dialog.rs +++ b/crates/ui-egui/src/export_dialog.rs @@ -1416,7 +1416,14 @@ mod tests { } } let doc = Arc::new(doc); - let f = Map::from_iter([("format".into(), json!("jpg")), ("scale".into(), json!(100)), ("quality".into(), json!(32))]); + // Without the embedded profile, whose fixed size the proxy would scale with the pixels + // (the untagged document is tagged sRGB on export, #9325). + let f = Map::from_iter([ + ("format".into(), json!("jpg")), + ("scale".into(), json!(100)), + ("quality".into(), json!(32)), + ("embedProfile".into(), json!(false)), + ]); let actual = photocraft_io::export(&export_document(&doc, &f, None).unwrap(), "x.jpg", &settings(&f).io_options()).unwrap().bytes.len() as u64; let proxy = preview_proxy(&doc, &f).unwrap(); let old = estimate_native(&doc, &proxy, &f).unwrap();