pub mod policy;
#[cfg(feature = "exif")]
pub mod exif;
#[cfg(feature = "exif")]
pub mod orientation;
pub mod riff;
pub mod sink;
#[derive(Default, Clone)]
pub struct ImageMetadata {
pub exif: Option<Vec<u8>>,
pub icc: Option<Vec<u8>>,
pub xmp: Option<Vec<u8>>,
pub exif_applied_orientation: bool,
}
const EXIF_HEADER: &[u8] = b"Exif\0\0";
#[must_use]
pub fn normalize_exif_payload(raw: Vec<u8>) -> Option<Vec<u8>> {
let payload = if raw.len() >= EXIF_HEADER.len() && raw[..EXIF_HEADER.len()] == *EXIF_HEADER {
raw[EXIF_HEADER.len()..].to_vec()
} else {
raw
};
if payload.is_empty() {
None
} else {
Some(payload)
}
}
#[must_use]
pub fn exif_for_jxl(tiff_payload: &[u8]) -> Vec<u8> {
let mut payload = Vec::with_capacity(8 + tiff_payload.len());
payload.extend_from_slice(&4u32.to_be_bytes());
payload.extend_from_slice(&[0, 0, 0, 0]);
payload.extend_from_slice(tiff_payload);
payload
}
#[cfg(feature = "exif")]
#[must_use]
pub fn resolve(policy: &policy::ExifPolicy, meta: &ImageMetadata) -> Option<Vec<u8>> {
exif::resolve(policy, meta)
}
#[cfg(not(feature = "exif"))]
#[must_use]
pub fn resolve(_policy: &policy::ExifPolicy, _meta: &ImageMetadata) -> Option<Vec<u8>> {
None
}
#[cfg(feature = "exif")]
#[must_use]
pub fn extract_exif_fallback(
path: &std::path::Path,
source_format: &crate::format::ImageFormat,
) -> Option<Vec<u8>> {
use crate::format::ImageFormat;
let bytes = std::fs::read(path).ok()?;
match source_format {
ImageFormat::Webp => riff::extract_exif_payload(&bytes),
ImageFormat::Tiff => {
let parsed = exif::parse_with_container(&bytes)?;
match exif::serialize_all(&parsed) {
Ok(payload) if !payload.is_empty() => Some(payload),
_ => normalize_exif_payload(bytes),
}
}
_ => None,
}
}
#[cfg(not(feature = "exif"))]
#[must_use]
pub fn extract_exif_fallback(
_path: &std::path::Path,
_source_format: &crate::format::ImageFormat,
) -> Option<Vec<u8>> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalization_strips_marker_prefix_and_empties() {
let tiff = b"II*\0\x08\0\0\0\0\0".to_vec();
let mut prefixed = EXIF_HEADER.to_vec();
prefixed.extend_from_slice(&tiff);
assert_eq!(
normalize_exif_payload(prefixed).as_deref(),
Some(tiff.as_slice())
);
assert_eq!(
normalize_exif_payload(tiff.clone()).as_deref(),
Some(tiff.as_slice())
);
assert_eq!(normalize_exif_payload(Vec::new()), None);
assert_eq!(normalize_exif_payload(EXIF_HEADER.to_vec()), None);
assert_eq!(
normalize_exif_payload(b"Exif".to_vec()).as_deref(),
Some(&b"Exif"[..])
);
}
#[test]
fn jxl_payload_uses_offset_prefix_convention() {
let tiff = b"II*\0\x08\0\0\0";
let payload = exif_for_jxl(tiff);
assert_eq!(&payload[..4], &4u32.to_be_bytes());
assert_eq!(&payload[4..8], &[0, 0, 0, 0]);
assert_eq!(&payload[8..], tiff);
let offset = u32::from_be_bytes(payload[..4].try_into().expect("4 bytes")) as usize;
assert_eq!(&payload[4 + offset..], tiff);
}
}