mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Share compatible first-IFD orientation lookup
This commit is contained in:
1 parent
1fb21f0487
commit
8920871fe4
3 files changed
+20
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -40,7 +40,7 @@ mod xmp_merge;
|
||||
|
||||
pub use container::{Embedded, embedded, 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);
|
||||
}
|
||||
Reference in new issue
Block a user