Skip to content

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

fix(io): a layered TIFF export warns that retained EXIF will be dropped (#1545) (#1597)

kubuntugamer967dcfd32479
diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs
index 1172eb7d9..fd143bdd5 100644
--- a/crates/io/src/tiff_layers.rs
+++ b/crates/io/src/tiff_layers.rs
@@ -220,7 +220,10 @@ pub(crate) fn export_layered(doc: &Document, opts: &ExportOptions) -> Result<Exp
let mut img = Image::from_raw(w, h, layout, sample, data)?;
img.icc = doc.icc_profile.as_ref().map(|i| i.to_vec());
img.meta = codecs::Metadata {
- exif: doc.metadata.exif.as_ref().map(|e| e.to_vec()),
+ // EXIF is deliberately not set here: the TIFF codec has no EXIF directory, so the tags
+ // above carry it as resource 1058 and the importer reads them back from there. Handing the
+ // codec a second copy in `meta.exif` bought nothing and made the export claim it would
+ // drop metadata it had in fact kept (#1545).
xmp: doc.metadata.xmp.clone().filter(|_| opts.xmp == crate::XmpEmbed::All),
dpi: Some((doc.resolution_dpi, doc.resolution_dpi)),
photoshop_resources: Some(resources),
diff --git a/crates/io/tests/tiff_layers.rs b/crates/io/tests/tiff_layers.rs
index 7a8b09da9..b893cfca0 100644
--- a/crates/io/tests/tiff_layers.rs
+++ b/crates/io/tests/tiff_layers.rs
@@ -107,6 +107,44 @@ fn every_compression_keeps_the_layers() {
}
}
+/// The Artist tag's text, and the bytes every EXIF assertion looks for.
+const ARTIST: &[u8] = b"fixture-artist";
+
+/// A minimal little-endian EXIF (TIFF) block with a single Artist tag and no Orientation, so
+/// `upright_exif` passes it through untouched.
+fn exif() -> Vec<u8> {
+ let mut artist = ARTIST.to_vec();
+ artist.push(0); // ASCII NUL terminator
+ let mut v = b"II*\0\x08\0\0\0".to_vec(); // TIFF header, first IFD at 8
+ v.extend_from_slice(&1u16.to_le_bytes()); // one entry
+ v.extend_from_slice(&0x013bu16.to_le_bytes()); // Artist
+ v.extend_from_slice(&2u16.to_le_bytes()); // ASCII
+ v.extend_from_slice(&(artist.len() as u32).to_le_bytes());
+ v.extend_from_slice(&26u32.to_le_bytes()); // value offset: 8 + 2 + 12 + 4
+ v.extend_from_slice(&0u32.to_le_bytes()); // no next IFD
+ v.extend_from_slice(&artist);
+ v
+}
+
+/// #1545: a layered TIFF carries EXIF as Photoshop resource 1058 and reads it back, so it must
+/// not report a loss it never makes. A flat TIFF really does lose the tags and still says so.
+#[test]
+fn layered_tiff_keeps_exif_without_claiming_a_loss() {
+ let mut d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS);
+ d.metadata.exif = Some(std::sync::Arc::new(exif()));
+
+ let r = tiff(&d, true);
+ assert!(!r.warnings.iter().any(|w| w.contains("EXIF")), "a layered TIFF keeps EXIF, so it must not claim to drop it: {:?}", r.warnings);
+ // ...and the tags really are in the file, so the missing warning is a true absence of loss.
+ let back = import("x.tif", &r.bytes).unwrap().document;
+ let kept = back.metadata.exif.as_deref().expect("EXIF survives the layered export");
+ assert!(kept.windows(ARTIST.len()).any(|w| w == ARTIST), "the Artist tag did not survive: {kept:?}");
+
+ // The control from the issue: the flat path drops it for real, and keeps reporting that.
+ let flat = tiff(&d, false);
+ assert!(flat.warnings.iter().any(|w| w.contains("EXIF")), "a flat TIFF really does lose EXIF: {:?}", flat.warnings);
+}
+
#[test]
fn discard_layers_writes_a_flat_file() {
let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS);