Skip to content

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

fix(io): keep variable noise GdFl fills as fill layers

Elvis33LE716d1e24c977
diff --git a/crates/io/src/blocks.rs b/crates/io/src/blocks.rs
index f356c1320..4cad23ce4 100644
--- a/crates/io/src/blocks.rs
+++ b/crates/io/src/blocks.rs
@@ -446,12 +446,17 @@ pub fn fill_from_desc(key: &[u8; 4], d: &Descriptor) -> Option<Fill> {
opacity_stops.clear();
}
if stops.is_empty() {
- if let Some(grad) = gradient.filter(|g| enum_of(g, "GrdF") == Some(b"ClNs")) {
- let color = constant_rgb_noise_color(grad)?;
- stops = vec![(0.0, color), (1.0, color)];
- } else {
- stops = vec![(0.0, Color::BLACK), (1.0, Color::WHITE)];
- }
+ // A provably constant-RGB noise gradient renders one exact colour. Any other
+ // noise gradient (variable ranges, transparency, other models) has no stops to
+ // read: keep the placeholder ramp rather than failing the whole fill — the
+ // layer stays a fill layer, Photoshop's own pixels ride along as `fill_cache`,
+ // and the raw `GdFl` block survives the round trip byte for byte. Failing
+ // here would import the layer as pixels and drop `GdFl` on save (#6272).
+ stops = gradient
+ .filter(|g| enum_of(g, "GrdF") == Some(b"ClNs"))
+ .and_then(constant_rgb_noise_color)
+ .map(|color| vec![(0.0, color), (1.0, color)])
+ .unwrap_or_else(|| vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]);
}
let pct = |o: &Descriptor, k: &str| num(o.get(k)).map_or(0.0, |v| v as f32 / 100.0);
let offset = get_desc(d, "Ofst").map_or((0.0, 0.0), |o| (pct(o, "Hrzn"), pct(o, "Vrtc")));
@@ -811,9 +816,15 @@ mod tests {
}
#[test]
- fn variable_or_transparent_noise_fill_is_not_fabricated_as_a_black_to_white_ramp() {
+ fn variable_or_transparent_noise_stays_a_fill_with_the_placeholder_ramp() {
+ use photocraft_doc::Fill;
+ // Photoshop's default Noise gradient (min 0, max 100) and a transparent variant: no
+ // exact colour is provable, but the fill must parse — the layer keeps its type, its
+ // stored pixels (`fill_cache`) and its raw `GdFl` block. Only the uncached preview
+ // shows the placeholder ramp.
for descriptor in [noise_gradient_fill([0, 0, 0, 0], [100, 100, 100, 100], false), noise_gradient_fill([50, 25, 0, 0], [50, 25, 0, 100], true)] {
- assert!(fill_from_desc(b"GdFl", &descriptor).is_none());
+ let Some(Fill::Gradient { stops, .. }) = fill_from_desc(b"GdFl", &descriptor) else { panic!("variable noise gradient must stay a fill layer") };
+ assert_eq!(stops, vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]);
}
}
diff --git a/crates/io/tests/psd_noise_fill_roundtrip.rs b/crates/io/tests/psd_noise_fill_roundtrip.rs
new file mode 100644
index 000000000..e8fb38b88
--- /dev/null
+++ b/crates/io/tests/psd_noise_fill_roundtrip.rs
@@ -0,0 +1,54 @@
+//! #6272: a Photoshop noise `GdFl` gradient that is not provably constant-RGB still opens as
+//! a fill layer — the placeholder ramp stands in where the model has no stops, Photoshop's
+//! own pixels ride along as the fill cache — and its raw `GdFl` block survives a re-save byte
+//! for byte. Before the fix, `fill_from_desc` returned `None` for those gradients: the layer
+//! imported as pixels and the export dropped the `GdFl` (`RASTER_STALE_KEYS`), so opening and
+//! re-saving a file silently turned the fill layer into a plain pixel layer.
+
+use photocraft_doc::{Fill, LayerContent};
+use photocraft_io::*;
+use photocraft_psd::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor};
+use photocraft_psd::testgen;
+use photocraft_psd::{Compression, LayerRecord, PsdFile, TaggedBlock, Version};
+
+/// Photoshop's default Noise gradient (RGB, min 0/0/0, max 100/100/100) as `GdFl` block data.
+fn default_noise_gdfl() -> Vec<u8> {
+ let values = |v: [i32; 4]| Value::List(v.into_iter().map(Value::Integer).collect());
+ let gradient = Descriptor::new("Grdn")
+ .with("GrdF", Value::Enumerated { type_id: Id::new("GrdF"), value: Id::new("ClNs") })
+ .with("ClrS", Value::Enumerated { type_id: Id::new("ClrS"), value: Id::new("RGBC") })
+ .with("ShTr", Value::Boolean(false))
+ .with("VctC", Value::Boolean(true))
+ .with("RndS", Value::Integer(9052369))
+ .with("Smth", Value::Integer(2048))
+ .with("Mnm ", values([0, 0, 0, 0]))
+ .with("Mxm ", values([100, 100, 100, 100]));
+ let d = Descriptor::new("null")
+ .with("Grad", Value::Descriptor(gradient))
+ .with("Angl", Value::UnitFloat { unit: *b"#Ang", value: 0.0 })
+ .with("Type", Value::Enumerated { type_id: Id::new("Type"), value: Id::new("CstS") });
+ VersionedDescriptor::new(d).to_bytes()
+}
+
+#[test]
+fn variable_noise_gradient_stays_a_fill_layer_and_keeps_its_gdfl() {
+ let _ = UnicodeString::new_nul("x");
+ let mut f = testgen::small(Version::Psd, Compression::Raw);
+ let gdfl = default_noise_gdfl();
+ f.layers_mut().push(LayerRecord {
+ name: b"noise fill".to_vec(),
+ blocks: vec![TaggedBlock::unicode_name("noise fill"), TaggedBlock::new(*b"GdFl", gdfl.clone())],
+ ..Default::default()
+ });
+ for b in f.layers().last().unwrap().blocks.iter() {
+ b.check_structure().unwrap();
+ }
+ let bytes = f.to_bytes().unwrap();
+ let imp = import("in.psd", &bytes).unwrap();
+ let l = imp.document.layers.iter().find(|l| l.name == "noise fill").unwrap();
+ assert!(matches!(l.content, LayerContent::Fill(Fill::Gradient { .. })), "a variable noise GdFl opens as a gradient fill layer, not pixels");
+ let out = export(&imp.document, "out.psd", &ExportOptions::default()).unwrap().bytes;
+ let back = PsdFile::from_bytes(&out).unwrap();
+ let rec = back.layers().iter().find(|l| l.block(b"GdFl").is_some()).expect("the re-saved layer carries its GdFl");
+ assert_eq!(rec.block(b"GdFl").unwrap().data, gdfl, "the raw GdFl survives the round trip byte for byte");
+}