#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Orientation {
#[default]
Normal,
FlipH,
Rotate180,
FlipV,
Transpose,
Rotate90,
Transverse,
Rotate270,
}
impl Orientation {
pub fn to_exif(self) -> u8 {
match self {
Orientation::Normal => 1,
Orientation::FlipH => 2,
Orientation::Rotate180 => 3,
Orientation::FlipV => 4,
Orientation::Transpose => 5,
Orientation::Rotate90 => 6,
Orientation::Transverse => 7,
Orientation::Rotate270 => 8,
}
}
pub fn from_exif(value: u8) -> Option<Self> {
Some(match value {
1 => Orientation::Normal,
2 => Orientation::FlipH,
3 => Orientation::Rotate180,
4 => Orientation::FlipV,
5 => Orientation::Transpose,
6 => Orientation::Rotate90,
7 => Orientation::Transverse,
8 => Orientation::Rotate270,
_ => return None,
})
}
pub(crate) fn to_u3(self) -> u64 {
(self.to_exif() - 1) as u64
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exif_roundtrip() {
for v in 1u8..=8 {
let o = Orientation::from_exif(v).unwrap();
assert_eq!(o.to_exif(), v);
assert_eq!(o.to_u3(), (v - 1) as u64);
}
assert_eq!(Orientation::from_exif(0), None);
assert_eq!(Orientation::from_exif(9), None);
}
#[test]
fn default_is_normal() {
assert_eq!(Orientation::default(), Orientation::Normal);
assert_eq!(Orientation::default().to_exif(), 1);
}
}