diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index f5a9ce50af..6421de5045 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -439,8 +439,15 @@ pub fn webp_is_animated(bytes: &[u8]) -> bool { /// DPX elements and FITS HDU payloads. /// Pixels are not decoded; derived TIFF thumbnails do not require preservation here. /// Other formats return false; this does not establish preservation of other format-specific data. +/// A format whose codec this build leaves out is refused with [`CodecError::Unsupported`]. pub fn has_unretained_source_data(bytes: &[u8]) -> Result { - match detect(bytes) { + let detected = detect(bytes); + if let Some(format) = detected + && let Some(feature) = format::missing_feature(format) + { + return Err(CodecError::feature_missing(format, feature)); + } + match detected { Some(Format::Gif) => via_image::gif_has_more_frames(bytes).ok_or_else(|| CodecError::malformed(Format::Gif, "cannot inspect every frame")), Some(Format::Png) => png::has_additional_images(bytes), Some(Format::WebP) => gated!("webp", Format::WebP, Ok(webp_is_animated(bytes))), diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs index e9f9edc3f0..9acda8469e 100644 --- a/crates/image-codecs/src/raw_metadata.rs +++ b/crates/image-codecs/src/raw_metadata.rs @@ -273,7 +273,6 @@ pub fn replace_webp_exif(webp: &mut Vec, exif: &[u8]) -> Result<(), CodecErr mod tests { use super::*; const EXIF: &[u8] = b"II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; - #[cfg(feature = "webp")] #[test] fn replacements_match_previous_code_on_mutated_containers() { fn old_png(png: &mut Vec, exif: &[u8]) { @@ -324,7 +323,12 @@ mod tests { } let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); image.meta.exif = Some(EXIF.to_vec()); - for format in [Format::Jpeg, Format::Png, Format::WebP] { + for format in [ + Format::Jpeg, + Format::Png, + #[cfg(feature = "webp")] + Format::WebP, + ] { let encoded = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); for at in 0..encoded.len() { for value in [0, 255] { @@ -360,12 +364,16 @@ mod tests { } } - #[cfg(feature = "webp")] #[test] fn explicit_replacements_preserve_raw_orientation_in_real_encodings() { let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); image.meta.exif = Some(EXIF.to_vec()); - for format in [Format::Jpeg, Format::Png, Format::WebP] { + for format in [ + Format::Jpeg, + Format::Png, + #[cfg(feature = "webp")] + Format::WebP, + ] { let mut bytes = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); let normal = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); assert_eq!(normal.meta.exif.as_deref().map(crate::exif_orientation), Some(1), "default {format:?}"); diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index 07d5678a2e..070d300e61 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -620,8 +620,8 @@ fn sun_raster_disabled_by_default() { assert!(encode(&image, Format::SunRaster, &EncodeOptions::default()).is_err()); } -/// A codec the build leaves out still has its files detected, and reading or writing one is -/// refused with the missing feature named. +/// A codec the build leaves out still has its files detected, and reading, writing or inspecting +/// one is refused with the missing feature named. #[test] fn formats_left_out_of_the_build_are_detected_and_refused_by_name() { let mut dicom = vec![0u8; 128]; @@ -653,6 +653,7 @@ fn formats_left_out_of_the_build_are_detected_and_refused_by_name() { assert!(!caps(format).read, "{feature}"); assert!(names_feature(decode(&bytes).map(drop), format, feature), "{feature}"); assert!(names_feature(probe_size(&bytes).map(drop), format, feature), "{feature}"); + assert!(names_feature(has_unretained_source_data(&bytes).map(drop), format, feature), "{feature}"); } } } diff --git a/crates/image-codecs/tests/it/resolution.rs b/crates/image-codecs/tests/it/resolution.rs index 6cbcd9c4e9..41ee95f0fc 100644 --- a/crates/image-codecs/tests/it/resolution.rs +++ b/crates/image-codecs/tests/it/resolution.rs @@ -405,12 +405,16 @@ fn jpeg_export_writes_one_resolution_everywhere() { } } -#[cfg(feature = "webp")] #[test] fn other_formats_take_the_exif_resolution_when_they_have_none() { let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); img.meta.exif = Some(camera_exif(&LE)); - for fmt in [Format::WebP, Format::Png] { + let formats = [ + #[cfg(feature = "webp")] + Format::WebP, + Format::Png, + ]; + for fmt in formats { // Written at 240 with the 300 ppi EXIF: the EXIF follows the image. img.meta.dpi = Some((240.0, 240.0)); let out = encode(&img, fmt, &EncodeOptions::default()).unwrap(); @@ -497,7 +501,6 @@ fn exif_with_thumbnail(e: &Exif) -> Vec { v } -#[cfg(feature = "webp")] fn next_ifd(e: &Exif, exif: &[u8]) -> u32 { let at = 8 + 2 + 4 * 12; let b: [u8; 4] = exif[at..at + 4].try_into().unwrap(); @@ -506,7 +509,6 @@ fn next_ifd(e: &Exif, exif: &[u8]) -> u32 { /// #2147 / #2373: an export must not carry the source's EXIF thumbnail (IFD1). Finder, Android /// and upload pickers show it instead of the image, so an edited photo kept its old thumbnail. -#[cfg(feature = "webp")] #[test] fn exports_drop_the_source_exif_thumbnail() { for e in [&LE, &BE] { @@ -536,7 +538,12 @@ fn exports_drop_the_source_exif_thumbnail() { img.meta.exif = Some(exif.clone()); let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); assert_eq!(next_ifd(e, &exif_of(&jpeg)), 0, "JPEG"); - for format in [Format::Png, Format::WebP] { + let formats = [ + Format::Png, + #[cfg(feature = "webp")] + Format::WebP, + ]; + for format in formats { let back = decode(&encode(&img, format, &EncodeOptions::default()).unwrap()).unwrap(); let got = back.meta.exif.unwrap_or_else(|| panic!("{format:?} kept no EXIF")); let body = got.strip_prefix(b"Exif\0\0").unwrap_or(&got); diff --git a/crates/image-codecs/tests/it/roundtrip.rs b/crates/image-codecs/tests/it/roundtrip.rs index f58af9acb2..73be35d97b 100644 --- a/crates/image-codecs/tests/it/roundtrip.rs +++ b/crates/image-codecs/tests/it/roundtrip.rs @@ -155,12 +155,18 @@ mod hdr { // --------------------------------------------------------------------------- /// Float formats must keep HDR values (> 1, and negative where allowed). -#[cfg(all(feature = "exr", feature = "pnm"))] #[test] fn hdr_values_survive_float_formats() { let vals: Vec = (0..16 * 8 * 3).map(|i| (i as f32 - 40.0) * 0.37).collect(); let img = Image::from_f32(16, 8, ChannelLayout::Rgb, &vals).unwrap(); - for f in [Format::Tiff, Format::OpenExr, Format::Pnm] { + let formats = [ + Format::Tiff, + #[cfg(feature = "exr")] + Format::OpenExr, + #[cfg(feature = "pnm")] + Format::Pnm, + ]; + for f in formats { let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); assert_eq!(back.to_f32_samples().unwrap(), vals, "{f:?}"); }