Skip to content

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

Flat export: a Lab document with one layer isn't reported as flattened (#4853)

Gert Pellin - GPEcb6ccaafc4ef
diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs
index 1778e5d06..c75418da7 100644
--- a/crates/io/src/flat.rs
+++ b/crates/io/src/flat.rs
@@ -312,8 +312,10 @@ fn document_to_image_for(doc: &Document, warnings: &mut Vec<String>, cmyk_target
let canvas = doc.bounds();
let fmt = doc.pixel_format();
let n = (w as usize) * (h as usize);
- // Lab has no flat-format layout here: Lab documents always go through the composite.
- let native = single_layer(doc).filter(|_| fmt.mode != ColorMode::Lab);
+ // Lab has no flat-format layout here: Lab documents always go through the composite, which
+ // flattens nothing when there is a single plain layer.
+ let single = single_layer(doc);
+ let native = single.filter(|_| fmt.mode != ColorMode::Lab);
let mut icc = doc.icc_profile.as_ref().map(|i| i.to_vec());
let img = if let Some(s) = native {
// Native path: keep model and depth. The surface's encoded samples are the codec's raw
@@ -340,8 +342,10 @@ fn document_to_image_for(doc: &Document, warnings: &mut Vec<String>, cmyk_target
}
Image::from_raw(w, h, layout, csample(fmt.sample), data)?
} else {
- let count = doc.layer_count();
- warnings.push(format!("{count} layer(s) flattened; layers, masks and blend modes are not kept"));
+ if single.is_none() {
+ let count = doc.layer_count();
+ warnings.push(format!("{count} layer(s) flattened; layers, masks and blend modes are not kept"));
+ }
let cmyk = fmt.mode == ColorMode::Cmyk && cmyk_target;
// Only a CMYK target needs the document's profile; RGB-only targets keep the compositor's
// own handling (built-in coated CMYK when the embedded profile is unusable).
diff --git a/crates/io/tests/flat.rs b/crates/io/tests/flat.rs
index 35a5e5ad4..f57041a72 100644
--- a/crates/io/tests/flat.rs
+++ b/crates/io/tests/flat.rs
@@ -963,3 +963,20 @@ fn tga_default_bits_follow_the_alpha_channels() {
assert_eq!(tga_default_bits(&with_alpha_channel(d, "Alpha 1")), TgaBits::Bits32);
assert_eq!((TgaBits::from_bits(24), TgaBits::from_bits(32), TgaBits::from_bits(16)), (Some(TgaBits::Bits24), Some(TgaBits::Bits32), None));
}
+
+#[test]
+fn a_lab_document_with_one_layer_is_not_reported_as_flattened() {
+ // Lab always goes through the sRGB composite, but a lone layer loses nothing to it.
+ let d = single(ColorMode::Lab, SampleType::U8, false);
+ for ext in ["tif", "png", "jpg"] {
+ let r = export(&d, ext, &ExportOptions::default()).unwrap();
+ assert!(r.warnings.iter().all(|w| !w.contains("flattened")), "{ext}: {:?}", r.warnings);
+ assert!(r.warnings.iter().any(|w| w.contains("Lab composite written as sRGB")), "{ext}: {:?}", r.warnings);
+ }
+ // Two layers are flattened, and say so.
+ let mut two = single(ColorMode::Lab, SampleType::U8, false);
+ let fmt = two.pixel_format();
+ two.layers.push(raster("Top", fmt, two.bounds(), 5, true));
+ let r = export(&two, "tif", &ExportOptions::default()).unwrap();
+ assert!(r.warnings.iter().any(|w| w.contains("2 layer(s) flattened")), "{:?}", r.warnings);
+}