Skip to main content

use_image/
orientation.rs

1use core::fmt;
2
3use crate::size::ImageSize;
4
5/// Common image orientation buckets.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
7pub enum ImageOrientation {
8    Landscape,
9    Portrait,
10    Square,
11    #[default]
12    Unknown,
13}
14
15impl fmt::Display for ImageOrientation {
16    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
17        formatter.write_str(match self {
18            Self::Landscape => "landscape",
19            Self::Portrait => "portrait",
20            Self::Square => "square",
21            Self::Unknown => "unknown",
22        })
23    }
24}
25
26/// Detects the orientation for a size.
27#[must_use]
28pub const fn orientation(size: ImageSize) -> ImageOrientation {
29    if size.width == 0 || size.height == 0 {
30        ImageOrientation::Unknown
31    } else if size.width > size.height {
32        ImageOrientation::Landscape
33    } else if size.height > size.width {
34        ImageOrientation::Portrait
35    } else {
36        ImageOrientation::Square
37    }
38}