img_core/exif.rs#
Source: lib/img_core/exif.rs
exif -- TIFF IFD reader (EXIF blocks; shared with the TIFF codec) and EXIF helpers
[src/img/exif.s]
Tifr::open(data) -> Ok(reader) / Err(ERR_TRUNC / ERR_HDR)
tr.visit(off) -> 0 / ERR_LIMIT record an IFD (a cycle or TIFR_MAXIFD IFDs: limit)
tr.count(off) -> entries (clamped to what fits) / ERR_TRUNC
tr.ifd(off) -> entries / ERR_* visit + count
tr.entry(off, i) -> Ok(TEnt) / Err(ERR_HDR unknown type / ERR_TRUNC)
tr.find(off, tag) -> Some(TEnt) first entry with the tag (bad entries skipped)
tr.get(te, i) -> u64 integer value i (signed types sign-extended,
RATIONAL: numerator, FLOAT / DOUBLE: raw bits)
tr.rational(te, i) -> (num, den) RATIONAL / SRATIONAL value i (others: value / 1)
tr.next(off) -> offset of the next IFD (0 = none)
exif_read(blob) -> (0 / ERR_*, ExifInfo) orientation, resolution, colour space
exif_orientation(blob) -> 1..8
exif_set_orientation(blob, o) -> bool patch the IFD0 Orientation SHORT in place
exif_copy_orient1(blob) -> Vec copy with orientation 1
The reader works on a TIFF structure in memory: "II*\0" / "MM\0*" (classic, 4-byte
offsets, 12-byte entries) or BigTIFF "II+\0" / "MM\0+" (8-byte offsets, 20-byte
entries); every read is bounds-checked, value spans must lie inside the data.
TIFR_MAXIFD: usize = 1024 // IFDs per reader (chain + sub-IFDs), cycles detected
TIFR_MAXDEPTH: u32 = 3 // sub-IFD nesting callers must respect
struct TEntone decoded IFD entry (TENT)tag: u32typ: u32-- TIFF field type 1..18count: u64-- number of valuesvoff: u64-- offset of the values in the TIFF structurebytes: u64-- count * type size (inside the data)
struct Tifr<'a>a TIFF IFD reader (TIFR)data: &'a [u8]be: bool-- "MM"big: bool-- BigTIFFifd0: u64-- offset of the first IFD
impl<'a> Tifr<'a>
fn open(data: &'a [u8]) -> Result<Tifr<'a>, i32>start reading the TIFF structure: byte order, classic / BigTIFF, first IFD offset [tifr_open]fn visit(&mut self, off: u64) -> i32record that the IFD at off is being read: ERR_LIMIT when it was read before or TIFR_MAXIFD IFDs were read [tifr_visit]fn count(&self, off: u64) -> i64entries of the IFD at off, clamped to the entries that fit, or ERR_TRUNC [tifr_count]fn ifd(&mut self, off: u64) -> i64visit + count [tifr_ifd]fn entry(&self, off: u64, i: u64) -> Result<TEnt, i32>entry i (i < count) of the IFD at off; the value span is checked to lie inside the data [tifr_entry]fn find(&self, off: u64, tag: u32) -> Option<TEnt>the first entry of the IFD at off with the given tag (entries that fail entry() are skipped) [tifr_find]fn get(&self, te: &TEnt, i: u64) -> u64value i of an entry as an integer: BYTE / ASCII / UNDEFINED / SHORT / LONG / IFD / LONG8 / IFD8 zero-extended, SBYTE / SSHORT / SLONG / SLONG8 sign-extended, RATIONAL: numerator, FLOAT / DOUBLE: raw bits; 0 when i >= count [tifr_get]fn rational(&self, te: &TEnt, i: u64) -> (u64, u64)RATIONAL value i as (numerator, denominator) (SRATIONAL: sign-extended); other types give value / 1; i >= count: 0 / 0 [tifr_rational]fn next(&self, off: u64) -> u64offset of the IFD following the one at off (0 = none, or it lies outside the data) [tifr_next]struct ExifInfowhat exif_read finds (EXIFINFO)orient: u32-- 1..8 (1 when absent / invalid)unit: u32-- ResolutionUnit (1 none, 2 inch, 3 cm; 0 absent)dpix: u32-- 16.16 dpi (0 = unknown)dpiy: u32cspace: u32-- ColorSpace (1 = sRGB, 0xFFFF uncalibrated, 0 absent)orioff: u32-- offset of the orientation SHORT in the blob (0 = none)
impl ExifInfo
fn to_bytes(&self) -> [u8; 24]the 24-byte EXIFINFO recordfn exif_read(blob: &[u8]) -> (i32, ExifInfo)fill an ExifInfo from an EXIF TIFF structure: IFD0 Orientation, X / YResolution, ResolutionUnit (default inch), the Exif IFD (ColorSpace; the Interop IFD is visited), then the IFD chain is walked until its end, a cycle or the limit (errors there are ignored) -> (0 / ERR_TRUNC / ERR_HDR / ERR_LIMIT, info) [exif_read]fn exif_orientation(blob: &[u8]) -> u32the IFD0 orientation of an EXIF TIFF structure (1 when missing, invalid or unreadable) [exif_orientation]fn exif_set_orientation(blob: &mut [u8], orient: u32) -> booloverwrite the IFD0 Orientation SHORT in place (in the blob's byte order) -> true patched / false no orientation tag [exif_set_orientation]fn exif_copy_orient1(blob: &[u8]) -> Vec<u8>a copy of an EXIF block with its orientation set to 1 (for pixels rotated already); empty for an empty blob [exif_copy_orient1]