From be446f89d3eabe78d60dc3a1fed92c1d27b994e7 Mon Sep 17 00:00:00 2001 From: LloydNicholson <34131158+LloydNicholson@users.noreply.github.com> Date: Fri, 9 Oct 2026 21:43:14 +0200 Subject: [PATCH] Transparent PNG, TIFF and other flat images open as Layer 0, not Background (#1913) A flat image with transparency (alpha channel or tRNS) opens as a normal, unlocked Layer 0, as in Photoshop; opaque images still open as the locked Background. A lone Layer 0 saves back to PNG natively. Co-Authored-By: Claude Opus 5.5 --- apps/photocraft-cli/tests/cli.rs | 3 +- crates/io/src/flat.rs | 17 ++++++--- crates/io/src/lib.rs | 5 ++- crates/io/tests/flat.rs | 64 ++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/apps/photocraft-cli/tests/cli.rs b/apps/photocraft-cli/tests/cli.rs index 0f2a339c39a..b71c0f9aae3 100644 --- a/apps/photocraft-cli/tests/cli.rs +++ b/apps/photocraft-cli/tests/cli.rs @@ -136,7 +136,8 @@ fn info_prints_layer_tree_json() { let v: Value = serde_json::from_str(&out).unwrap(); assert_eq!(v["width"], 10); assert_eq!(v["height"], 6); - assert_eq!(v["layers"][0]["name"], "Background"); + // An RGBA PNG opens as a normal "Layer 0", not the locked Background (as in Photoshop). + assert_eq!(v["layers"][0]["name"], "Layer 0"); assert!(v.get("history").is_none()); let (out, _) = ok(bin().arg("info").arg(&a).arg("--compact")); assert_eq!(out.trim().lines().count(), 1); diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index db68035fc4e..8d0921c8c0c 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -51,7 +51,8 @@ pub fn import_tiff_page(name: &str, bytes: &[u8], page: Option) -> Result image_to_document(name, &img.oriented(orientation)?) } -/// A decoded flat image as a single-layer document. +/// A decoded flat image as a single-layer document: a locked "Background" when it is opaque, a +/// normal "Layer 0" when it has transparency. pub(crate) fn image_to_document(name: &str, img: &Image) -> Result { // What the decoder noticed (frames or pages left out, data ending early) comes first. let mut warnings: Vec = img.warnings.iter().map(ToString::to_string).collect(); @@ -87,12 +88,18 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result Image::from_u8(w, h, layout, (0..n * ch).map(|i| if (i + 1) % ch == 0 && i < ch * 5 { 0 } else { 200 }).collect()), + _ => Image::from_u16(w, h, layout, &(0..n * ch).map(|i| if (i + 1) % ch == 0 && i < ch * 5 { 0 } else { 50_000 }).collect::>()), + } + .unwrap(); + photocraft_codecs::encode(&img, format, &EncodeOptions::default()).unwrap() + }; + let cases: Vec<(&str, Vec, bool)> = vec![ + ("a.png", enc(ChannelLayout::Rgba, CS::U8, Format::Png), true), + ("a16.png", enc(ChannelLayout::Rgba, CS::U16, Format::Png), true), + ("ga.png", enc(ChannelLayout::GrayA, CS::U8, Format::Png), true), + ("a.tif", enc(ChannelLayout::Rgba, CS::U8, Format::Tiff), true), + ("ga.tif", enc(ChannelLayout::GrayA, CS::U16, Format::Tiff), true), + ("pal.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], Some(0)).unwrap(), true), + ("pal_opaque.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], None).unwrap(), false), + ("rgb.png", enc(ChannelLayout::Rgb, CS::U8, Format::Png), false), + ("g.png", enc(ChannelLayout::Gray, CS::U16, Format::Png), false), + ("rgb.tif", enc(ChannelLayout::Rgb, CS::U8, Format::Tiff), false), + ("rgb.jpg", enc(ChannelLayout::Rgb, CS::U8, Format::Jpeg), false), + ]; + for (name, bytes, transparent) in cases { + let d = import(name, &bytes).unwrap_or_else(|e| panic!("{name}: {e}")).document; + assert_opens_as(name, &d, transparent); + // Photoshop numbers the next new layer after "Layer 0" as "Layer 1". + assert_eq!(d.next_layer_name("Layer"), "Layer 1", "{name}"); + } +} + +#[test] +fn layer_0_saves_back_to_png_and_reopens_as_layer_0() { let r = export(&single(ColorMode::Rgb, SampleType::U8, true), "png", &ExportOptions::default()).unwrap(); let d = import("a.png", &r.bytes).unwrap().document; - assert!(!d.layers[0].locks.transparency); + assert_opens_as("first open", &d, true); + let again = export(&d, "b.png", &ExportOptions::default()).unwrap(); + assert!(again.warnings.iter().all(|w| !w.contains("flattened")), "a lone Layer 0 is written natively: {:?}", again.warnings); + let back = import("b.png", &again.bytes).unwrap().document; + assert_opens_as("reopened", &back, true); + pixels_eq(&d, &back, 0.0); } #[test]