//! EXIF orientation, read from the TIFF block that JPEG, WebP and PNG all carry. //! //! The TIFF block is bare in WebP's `EXIF` chunk and in PNG's `eXIf` chunk (PNG 1.5 //! and libwebp both define it that way, and `image` reads it that way). JPEG's APP1 //! segment puts an `Exif\0\0` header in front, which the JPEG reader strips. Only //! IFD0's orientation tag (0x0112) is read, which is the one the EXIF spec defines. /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a /// readable TIFF or carries no valid orientation entry. pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option { let big_endian = match tiff.get(..2)? { b"II" => false, b"MM" => true, _ => return None, }; let u16_at = |at: usize| crate::bytes::u16_at(tiff, at, big_endian); let u32_at = |at: usize| crate::bytes::u32_at(tiff, at, big_endian); if u16_at(2)? != 42 { return None; } let ifd = u32_at(4)? as usize; let count = usize::from(u16_at(ifd)?); for i in 0..count { let entry = ifd + 2 + i * 12; // Tag 0x0112, type SHORT (3), one value: the value sits in the first two bytes of the field. if u16_at(entry)? == 0x0112 && u16_at(entry + 2)? == 3 && u32_at(entry + 4)? == 1 { let value = u16_at(entry + 8)?; return (1..=8).contains(&value).then_some(value as u8); } } None }