Skip to content

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

Merge remote-tracking branch 'origin/main' into fix/tiff-alpha-compression

Finn Newick3d7a021d490d
diff --cc crates/codecs/src/codecs/tiff.rs
index 9cae4e1c1,9528fec9c..7fceb0065
--- a/crates/codecs/src/codecs/tiff.rs
+++ b/crates/codecs/src/codecs/tiff.rs
@@@ -543,16 -548,8 +550,16 @@@ fn decode_ifd(f: &File<'_>, dir: &Ifd,
5 => 4,
_ => 1,
};
- let alpha_at = extra.iter().position(|&e| e == 1 || e == 2).filter(|&a| (color + a as u64) < spp);
- let all_samples = palette.is_none() && matches!(photometric, 2 | 5 | 8 | 9) && alpha_at.is_some_and(|a| a > 0);
+ // With no extra sample marked as transparency, the first unspecified one (ExtraSamples 0) is
+ // decoded as the alpha and flagged as an alpha channel: Photoshop writes a document's alpha
+ // channel that way, and dropping it lost the channel (#5301).
+ let marked = extra.iter().position(|&e| e == 1 || e == 2);
+ let paletted = palette.is_some();
+ let channel_alpha = marked.is_none() && !paletted && extra.first() == Some(&0);
+ let alpha_at = marked.or(channel_alpha.then_some(0)).filter(|&a| (color + a as u64) < spp);
- // The `tiff` crate returns an RGB or CMYK alpha only when the first extra sample is marked as
- // one, so an alpha channel (unspecified) is read through every sample too.
- let all_samples = palette.is_none() && matches!(photometric, 2 | 5) && alpha_at.is_some_and(|a| a > 0 || channel_alpha);
++ // The `tiff` crate returns an RGB, CMYK or Lab alpha only when the first extra sample is marked
++ // as one, so an alpha channel (unspecified) is read through every sample too.
++ let all_samples = palette.is_none() && matches!(photometric, 2 | 5 | 8 | 9) && alpha_at.is_some_and(|a| a > 0 || channel_alpha);
let mut patches: Vec<(u64, Vec<u8>)> = Vec::new();
let encode_u = |v: u64, len: usize| -> Vec<u8> {
let b = if f.le { v.to_le_bytes() } else { v.to_be_bytes() };
@@@ -686,8 -689,10 +699,11 @@@
if tally.decoded == 0 && tally.missing > 0 {
return Err(err("the image data is missing (cut off or past the end of the file)"));
}
+ if lab {
+ convert_ab(&mut data, out_layout.channels(), out, photometric == 8, false);
+ }
let mut img = Image::from_raw(w, h, out_layout, out, data)?;
+ img.meta.alpha_is_channel = channel_alpha && out_layout.has_alpha();
if tally.missing > 0 {
img.warnings.push(DecodeWarning::Truncated { format: F });
}