From dff16c1ad15fe5e161d616f1ad8910bfd64c9435 Mon Sep 17 00:00:00 2001 From: Lcoding15 <125505497+Lcoding15@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:56:13 +0100 Subject: [PATCH] fix(export): PNG-24 embeds the profile only with Embed Color Profile on (#13798) --- crates/engine/src/cmd/web_cmds/optimizer.rs | 7 ++ .../src/cmd/web_cmds/png_embed_icc_tests.rs | 67 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 crates/engine/src/cmd/web_cmds/png_embed_icc_tests.rs diff --git a/crates/engine/src/cmd/web_cmds/optimizer.rs b/crates/engine/src/cmd/web_cmds/optimizer.rs index 2186f3b70a..73ac0325f8 100644 --- a/crates/engine/src/cmd/web_cmds/optimizer.rs +++ b/crates/engine/src/cmd/web_cmds/optimizer.rs @@ -155,6 +155,9 @@ pub fn optimize( if preview { look = rgba(&data, layout); } + // The profile only with Embed Color Profile on, as the WebP and JPEG branches do: Quick + // Export, which never sets it, writes no iCCP chunk (#13798). + let icc = icc.filter(|_| st.embed_icc); let img = photocraft_codecs::Image::from_u8(ww, hh, layout, data).map_err(e)?.with_icc(icc.map(<[u8]>::to_vec)).with_meta(meta()); let opts = photocraft_codecs::EncodeOptions { png_interlaced: st.interlaced, @@ -428,3 +431,7 @@ fn quantize_rows(px: &mut [[f32; 4]], w: usize, pal: &[[u8; 3]], dither: Dither, #[cfg(target_arch = "wasm32")] quantize_band(px, w, 0, pal, dither, amount, t) } + +#[cfg(test)] +#[path = "png_embed_icc_tests.rs"] +mod png_embed_icc_tests; diff --git a/crates/engine/src/cmd/web_cmds/png_embed_icc_tests.rs b/crates/engine/src/cmd/web_cmds/png_embed_icc_tests.rs new file mode 100644 index 0000000000..f295d8409a --- /dev/null +++ b/crates/engine/src/cmd/web_cmds/png_embed_icc_tests.rs @@ -0,0 +1,67 @@ +//! #13798: a Quick Export PNG carries no ICC profile, with Convert to sRGB on or off, and Save for +//! Web's PNG-24 embeds one only with Embed Color Profile on. The pixels are as before. + +use serde_json::json; + +use crate::Session; + +fn tmp(name: &str) -> String { + let d = std::env::temp_dir().join(format!("photocraft-png-icc-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d.to_string_lossy().into_owned() +} + +/// A red document with a wide-gamut profile (Adobe RGB) assigned. +fn tagged() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 20, "height": 10, "background": "#c81e1e"})).unwrap(); + s.execute("edit.assignProfile", json!({"profile": "adobe-rgb-compat"})).unwrap(); + assert!(s.active().unwrap().doc.icc_profile.is_some(), "the document has a profile"); + s +} + +/// The PNG's chunk types, in order. +fn chunks(bytes: &[u8]) -> Vec { + assert_eq!(bytes.get(..8), Some(b"\x89PNG\r\n\x1a\n".as_slice()), "a PNG"); + let mut out = Vec::new(); + let mut at = 8; + while let Some(head) = bytes.get(at..at + 8) { + let len = u32::from_be_bytes([head[0], head[1], head[2], head[3]]) as usize; + out.push(String::from_utf8_lossy(&head[4..8]).into_owned()); + at += 12 + len; + } + out +} + +fn pixels(path: &str) -> Vec { + let img = photocraft_codecs::decode(&std::fs::read(path).unwrap()).unwrap(); + img.convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8).data().to_vec() +} + +#[test] +fn a_quick_export_png_embeds_no_profile_with_convert_to_srgb_on_or_off() { + let dir = tmp("quick"); + let mut s = tagged(); + for convert in [true, false] { + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "png", "metadata": "none", "convertToSrgb": convert})).unwrap(); + let path = format!("{dir}/convert-{convert}.png"); + s.execute("file.export.quickExport", json!({"path": path})).unwrap(); + let found = chunks(&std::fs::read(&path).unwrap()); + assert!(!found.iter().any(|c| c == "iCCP"), "convert {convert}: {found:?}"); + assert!(photocraft_codecs::decode(&std::fs::read(&path).unwrap()).unwrap().icc.is_none(), "convert {convert}"); + } + s.execute("prefs.reset", json!({"path": "export"})).unwrap(); +} + +#[test] +fn save_for_web_png24_embeds_the_profile_only_when_asked_and_keeps_its_pixels() { + let dir = tmp("sfw"); + let mut s = tagged(); + let (on, off) = (format!("{dir}/on.png"), format!("{dir}/off.png")); + s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "embedIcc": true, "path": on})).unwrap(); + s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "embedIcc": false, "path": off})).unwrap(); + assert!(chunks(&std::fs::read(&on).unwrap()).iter().any(|c| c == "iCCP"), "Embed Color Profile on"); + assert!(!chunks(&std::fs::read(&off).unwrap()).iter().any(|c| c == "iCCP"), "Embed Color Profile off"); + assert_eq!(pixels(&on), pixels(&off), "only the profile differs"); +}