jpeg: share strict EXIF orientation framing

This commit is contained in:
Dylan M. Taylor
2026-11-22 11:04:24 -05:00
parent 1507e63e6a
commit e084f8c1f6
+52
View File
@@ -74,6 +74,14 @@ fn be16(b: &[u8], at: usize) -> Option<u16> {
/// `segment_span` covers the `0xFF` marker byte through the end of the segment payload.
pub fn walk_jpeg_segments<'a>(
bytes: &'a [u8],
visit: impl FnMut(u8, Range<usize>, &'a [u8]) -> Result<JpegVisit, JpegProbeError>,
) -> Result<(), JpegProbeError> {
walk_segments(bytes, false, visit)
}
fn walk_segments<'a>(
bytes: &'a [u8],
stop_at_non_marker: bool,
mut visit: impl FnMut(u8, Range<usize>, &'a [u8]) -> Result<JpegVisit, JpegProbeError>,
) -> Result<(), JpegProbeError> {
if bytes.len() < 4 || bytes[0] != 0xFF || bytes[1] != 0xD8 {
@@ -82,6 +90,9 @@ pub fn walk_jpeg_segments<'a>(
let mut i = 2usize;
while let Some(&byte) = bytes.get(i) {
if byte != 0xFF {
if stop_at_non_marker {
break;
}
i += 1;
continue;
}
@@ -115,6 +126,26 @@ pub fn walk_jpeg_segments<'a>(
Ok(())
}
/// EXIF orientation from the first complete APP1 segment prefixed with `Exif\0\0`.
///
/// Scans only contiguous JPEG header markers before SOS or EOI. Returns 1 if that
/// segment has no readable orientation, or if none is found before the header
/// ends or becomes unreadable. This deliberately does not resynchronize past
/// non-marker bytes as [`walk_jpeg_segments`] does.
pub fn strict_exif_orientation(bytes: &[u8]) -> u16 {
let mut orientation = 1;
let _ = walk_segments(bytes, true, |marker, _, segment| {
if marker == 0xE1
&& let Some(exif) = segment.strip_prefix(b"Exif\0\0")
{
orientation = crate::exif_orientation(exif);
return Ok(JpegVisit::Stop);
}
Ok(JpegVisit::Continue)
});
orientation
}
/// Parse JFIF APP0 density `(x_dpi, y_dpi)` when unit is inches (1) or centimetres (2) and both
/// axes are positive.
pub fn jfif_dpi(seg: &[u8]) -> Option<(f64, f64)> {
@@ -193,6 +224,27 @@ pub fn probe_jpeg(bytes: &[u8]) -> Result<JpegProbe, JpegProbeError> {
mod tests {
use super::*;
#[test]
fn strict_orientation_reads_the_first_exif_app1_before_the_scan() {
const ORIENTATION_6: &[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";
let mut header = Vec::from(b"\xFF\xD8\xFF\xE1\0\x05XMP".as_slice());
let app1: &[u8] = b"\xFF\xFF\xE1\0\x22Exif\0\0";
header.extend_from_slice(app1);
header.extend_from_slice(ORIENTATION_6);
assert_eq!(strict_exif_orientation(&header), 6);
let mut interrupted = vec![0xff, 0xd8, 0x01];
interrupted.extend_from_slice(&header[2..]);
assert_eq!(strict_exif_orientation(&interrupted), 1);
let mut after_scan = Vec::from(b"\xFF\xD8\xFF\xDA".as_slice());
after_scan.extend_from_slice(&header[2..]);
assert_eq!(strict_exif_orientation(&after_scan), 1);
let mut untagged_first = Vec::from(b"\xFF\xD8\xFF\xE1\0\x16Exif\0\0II\x2a\0\x08\0\0\0\0\0\0\0\0\0".as_slice());
untagged_first.extend_from_slice(&header[2..]);
assert_eq!(strict_exif_orientation(&untagged_first), 1);
}
#[test]
fn jpeg_probe_preserves_error_diagnostics() {
assert_eq!(probe_jpeg(b"\x89PNG").unwrap_err(), JpegProbeError::NotJpeg);