Skip to content

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

Deep EXR: write the flattened pixel by layout, not a fixed RGBA quadruple (#1021)

Elvis33LE96413cff457f
diff --git a/crates/codecs/src/codecs/deep_exr.rs b/crates/codecs/src/codecs/deep_exr.rs
index 3deb2264f..574e13c40 100644
--- a/crates/codecs/src/codecs/deep_exr.rs
+++ b/crates/codecs/src/codecs/deep_exr.rs
@@ -550,17 +550,20 @@ pub(crate) fn flatten(deep: &DeepImage) -> Result<Image, CodecError> {
// Un-premultiply into straight alpha (the deep colours are premultiplied).
let straight = |c: f32| if a_clamped > 1e-6 { c / a_clamped } else { 0.0 };
let base = p * nc;
- for (k, v) in [straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), a_clamped].into_iter().enumerate() {
+ // Written by layout: RGB(A), gray(+A) — a fixed RGBA quadruple would put a luminance
+ // copy into a GrayA image's alpha slot.
+ // A fixed array plus a length: this runs once per pixel, so no allocation.
+ let (vals, len) = match (rgb, nc) {
+ (true, 4) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), a_clamped], 4),
+ (true, _) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), 0.0], 3),
+ (false, 2) => ([straight(cacc[0]), a_clamped, 0.0, 0.0], 2),
+ (false, _) => ([straight(cacc[0]), 0.0, 0.0, 0.0], 1),
+ };
+ for (k, v) in vals.into_iter().take(len).enumerate() {
if let Some(slot) = out.get_mut(base + k) {
*slot = v;
}
}
- if !rgb {
- // Luminance: keep only channel 0, and the alpha behind it.
- if let Some(slot) = out.get_mut(base) {
- *slot = straight(cacc[0]);
- }
- }
}
let mut img = Image::from_f32(deep.width, deep.height, layout, &out)?;
img.warnings.push(DecodeWarning::DeepFlattened { format: F, max_samples_per_pixel: max_samples });
diff --git a/crates/codecs/tests/deep_exr.rs b/crates/codecs/tests/deep_exr.rs
index 4f1c59819..f15f1099e 100644
--- a/crates/codecs/tests/deep_exr.rs
+++ b/crates/codecs/tests/deep_exr.rs
@@ -496,6 +496,41 @@ fn without_alpha_front_sample_is_opaque() {
assert!((px[0] - 0.4).abs() < 1e-6 && (px[1] - 0.2).abs() < 1e-6 && (px[2] - 0.9).abs() < 1e-6);
}
+/// Grayscale deep files (Y + Z) flatten to opaque gray, Y + A + Z keeps its alpha: the alpha
+/// must land in the GrayA alpha slot, not a luminance copy (that was the bug).
+#[test]
+fn grayscale_deep_keeps_its_alpha() {
+ let channels = [Chan { name: "A", ty: 2 }, Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }];
+ let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| {
+ match ci {
+ 0 => vec![0.5], // A
+ 1 => vec![0.5], // Y, premultiplied: straight 1.0 × 0.5
+ _ => vec![p as f32], // Z
+ }
+ });
+ let img = decode(&bytes).expect("decodes");
+ assert_eq!(img.layout(), photocraft_codecs::ChannelLayout::GrayA);
+ let px = img.to_f32_samples().expect("f32");
+ for (p, s) in px.chunks(2).enumerate() {
+ let (y, a) = (s[0], s[1]);
+ assert!((a - 0.5).abs() < 1e-6, "pixel {p}: alpha {a}, want 0.5");
+ assert!((y - 1.0).abs() < 1e-6, "pixel {p}: luminance {y}, want the un-premultiplied 1.0");
+ }
+
+ // Without an alpha channel the same file is opaque gray.
+ let channels = [Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }];
+ let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| match ci {
+ 0 => vec![0.4],
+ _ => vec![p as f32],
+ });
+ let img = decode(&bytes).expect("decodes");
+ assert_eq!(img.layout(), photocraft_codecs::ChannelLayout::Gray);
+ let px = img.to_f32_samples().expect("f32");
+ for (p, v) in px.iter().enumerate() {
+ assert!((v - 0.4).abs() < 1e-6, "pixel {p}: {v}, want 0.4");
+ }
+}
+
/// decode_deep returns the counts and channels in file order.
#[test]
fn decode_deep_structure() {