Merge pull request #1686 from storytold/codex/exif-ifd0-orientation

Expose compatible first-IFD orientation lookup
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 06:47:48 +01:00
commit 791561953d
3 files changed
+20 -2

No files matched your search

+11 -1
View File
@@ -51,8 +51,18 @@ fn pos(v: Option<f64>) -> Option<f64> {
/// the Exif's tag 700 states; a viewer displays a photo by the Exif's alone, so that one can't tell how a photo
/// is stored.
pub(crate) fn ifd0_orientation(bytes: &[u8]) -> Option<Orientation> {
ifd0_orientation_value(bytes).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif)
}
fn ifd0_orientation_value(bytes: &[u8]) -> Option<u16> {
let tiff = Tiff::parse_with(strip_exif_header(bytes), &ParseOptions::default()).ok()?;
tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION)).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif)
tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION))
}
/// Read the first IFD's orientation, treating out-of-range values as normal.
/// Missing or malformed values return `None`.
pub fn ifd0_orientation_compat(bytes: &[u8]) -> Option<Orientation> {
ifd0_orientation_value(bytes).map(Orientation::from_exif)
}
/// The ISO an Exif IFD states beyond the 16-bit ISOSpeedRatings: RecommendedExposureIndex, else
+1 -1
View File
@@ -40,7 +40,7 @@ mod xmp_merge;
pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks};
pub use datetime::{DatePrecision, DateTime};
pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif};
pub use exif::{from_tiff, ifd0_orientation_compat, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif};
pub use focus::FocusPoint;
pub use fuji::{FujiRecipe, FujiRow, might_hold_fuji_recipe, read_recipe_file, recipe_from_bytes, recipe_from_exif};
pub use geom::{Orientation, Point, Rect};
@@ -0,0 +1,8 @@
#[test]
fn missing_and_invalid_values_remain_distinct() {
let mut data = b"II*\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0".to_vec();
assert_eq!(craft_metadata::ifd0_orientation_compat(&data).map(|o| o.to_exif()), Some(6));
data[18] = 0;
assert_eq!(craft_metadata::ifd0_orientation_compat(&data).map(|o| o.to_exif()), Some(1));
assert_eq!(craft_metadata::ifd0_orientation_compat(b"invalid"), None);
}