diff --git a/crates/codecs/src/codecs/jpeg.rs b/crates/codecs/src/codecs/jpeg.rs index 19e524a18f2..3cece9af7cd 100644 --- a/crates/codecs/src/codecs/jpeg.rs +++ b/crates/codecs/src/codecs/jpeg.rs @@ -248,15 +248,12 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result Result Option> { + // The pixels are written as they are shown: never let a viewer rotate them again. + let seg = [EXIF_HEADER, upright_exif(exif).as_ref()].concat(); + (seg.len() <= MAX_SEGMENT).then_some(seg) +} + +/// The XMP APP1 segment. When the packet doesn't fit in one segment, the layered-document +/// properties are left out; `None` when it still doesn't fit. +pub(crate) fn xmp_segment(xmp: &str) -> Option> { + let xmp = upright_xmp(xmp); + let seg = |x: &str| [XMP_HEADER, x.as_bytes()].concat(); + if XMP_HEADER.len() + xmp.len() <= MAX_SEGMENT { + return Some(seg(&xmp)); + } + let trimmed = LAYERED_ONLY_XMP.iter().fold(xmp.into_owned(), |x, name| remove_element(&x, name)); + (XMP_HEADER.len() + trimmed.len() <= MAX_SEGMENT).then(|| seg(&trimmed)) +} + +/// `xmp` without its `…` and `` elements. Unbalanced +/// markup is kept as is. +fn remove_element(xmp: &str, name: &str) -> String { + let local = format!(":{name}"); + let mut out = String::with_capacity(xmp.len()); + let mut rest = xmp; + while let Some(j) = rest.find(&local) { + let head = rest.get(..j).unwrap_or_default(); + let prefix_len = head.bytes().rev().take_while(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')).count(); + let lt = j - prefix_len; + let after = rest.get(j + local.len()..).unwrap_or_default(); + // An opening tag of exactly this name (not a closing tag, an attribute or a longer name). + let opening = prefix_len > 0 && lt > 0 && rest.get(lt - 1..lt) == Some("<"); + if !opening || !after.starts_with(['>', '/', ' ', '\t', '\r', '\n']) { + out.push_str(rest.get(..j + local.len()).unwrap_or_default()); + rest = after; + continue; + } + let Some(gt) = after.find('>') else { break }; + let end = if after.get(..gt).is_some_and(|s| s.ends_with('/')) { + gt + 1 + } else { + let close = format!("", rest.get(lt..j).unwrap_or_default()); + let Some(c) = after.find(&close) else { break }; + c + close.len() + }; + out.push_str(rest.get(..lt - 1).unwrap_or_default()); + rest = after.get(end..).unwrap_or_default(); + } + out.push_str(rest); + out +} diff --git a/crates/codecs/src/fidelity.rs b/crates/codecs/src/fidelity.rs index e49bd33774e..50a386e2196 100644 --- a/crates/codecs/src/fidelity.rs +++ b/crates/codecs/src/fidelity.rs @@ -41,6 +41,12 @@ pub enum FidelityWarning { XmpDropped, DpiDropped, TextDropped, + /// Metadata larger than the format can hold in one piece (`what`: "EXIF", "XMP") will be + /// dropped. + MetadataTooLarge { + what: &'static str, + bytes: usize, + }, /// Image exceeds the format's maximum size; encoding will fail. DimensionsExceeded { max_width: u32, @@ -86,6 +92,7 @@ impl fmt::Display for FidelityWarning { XmpDropped => write!(f, "XMP not supported; it will be dropped"), DpiDropped => write!(f, "resolution (DPI) not supported; it will be dropped"), TextDropped => write!(f, "text metadata not supported; it will be dropped"), + MetadataTooLarge { what, bytes } => write!(f, "{what} ({bytes} bytes) is too large for the format; it will be dropped"), DimensionsExceeded { max_width, max_height } => { write!(f, "image exceeds the format maximum of {max_width}x{max_height}") } @@ -219,6 +226,14 @@ pub fn fidelity_warnings_with(image: &Image, format: Format, opts: &EncodeOption if m.xmp.is_some() && !c.xmp { w.push(W::XmpDropped); } + if format == Format::Jpeg { + if let Some(exif) = m.exif.as_deref().filter(|e| crate::codecs::jpeg::exif_segment(e).is_none()) { + w.push(W::MetadataTooLarge { what: "EXIF", bytes: exif.len() }); + } + if let Some(xmp) = m.xmp.as_deref().filter(|x| crate::codecs::jpeg::xmp_segment(x).is_none()) { + w.push(W::MetadataTooLarge { what: "XMP", bytes: xmp.len() }); + } + } if m.dpi.is_some() && !c.dpi { w.push(W::DpiDropped); } diff --git a/crates/codecs/tests/metadata.rs b/crates/codecs/tests/metadata.rs index 22a5347f5ca..b3bc61c06a3 100644 --- a/crates/codecs/tests/metadata.rs +++ b/crates/codecs/tests/metadata.rs @@ -155,6 +155,45 @@ fn jpeg_multi_segment_icc() { } } +/// A layered document's XMP can list every document ever placed in it and the text of every type +/// layer: far more than the 64 KB one JPEG segment holds. The JPEG is still written, with that +/// bookkeeping left out of the XMP; metadata too large even then is dropped with a warning. +#[test] +fn jpeg_with_oversized_metadata_still_encodes() { + let ancestors: String = (0..3000).map(|i| format!("xmp.did:{i:032x}")).collect(); + let texts: String = (0..400) + .map(|i| format!("Layer {i}Words on layer {i}")) + .collect(); + let xmp = format!( + "\n{ancestors}{texts}" + ); + assert!(xmp.len() > 120_000, "{}", xmp.len()); + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut img = test_image(ChannelLayout::Rgb, sample); + img.meta.xmp = Some(xmp.clone()); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap().meta.xmp.unwrap(); + assert!(back.contains("xmp:CreatorTool=\"synthetic\"") && back.ends_with(""), "{sample:?}"); + assert!(!back.contains("DocumentAncestors>") && !back.contains("TextLayers"), "{sample:?}"); + assert!(back.contains(""), "{sample:?}: a longer name is not the element"); + assert!(!fidelity_warnings(&img, Format::Jpeg).iter().any(|w| matches!(w, FidelityWarning::MetadataTooLarge { .. }))); + // Formats without a segment limit keep the packet whole. + assert_eq!(decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap().meta.xmp.as_deref(), Some(xmp.as_str())); + } + // Still too large: the JPEG is written without it. + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + let big = format!("", "x".repeat(70_000)); + let mut exif = sample_exif(); + exif.resize(70_000, 0); + img.meta.xmp = Some(big.clone()); + img.meta.exif = Some(exif); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.xmp.is_none() && back.meta.exif.is_none()); + let w = fidelity_warnings(&img, Format::Jpeg); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "XMP", bytes: big.len() }), "{w:?}"); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "EXIF", bytes: 70_000 }), "{w:?}"); +} + #[test] fn embed_flags_disable_metadata() { let img = rich_image(ChannelLayout::Rgb);