use_image/
aspect_ratio.rs1use crate::size::ImageSize;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5pub struct ImageAspectRatio {
6 pub width: u32,
7 pub height: u32,
8}
9
10impl ImageAspectRatio {
11 #[must_use]
13 pub const fn new(width: u32, height: u32) -> Self {
14 Self { width, height }
15 }
16}
17
18fn gcd(mut left: u32, mut right: u32) -> u32 {
19 while right != 0 {
20 let remainder = left % right;
21 left = right;
22 right = remainder;
23 }
24
25 left
26}
27
28#[must_use]
30pub fn aspect_ratio(size: ImageSize) -> ImageAspectRatio {
31 let divisor = gcd(size.width, size.height);
32
33 if divisor == 0 {
34 return ImageAspectRatio::new(size.width, size.height);
35 }
36
37 ImageAspectRatio::new(size.width / divisor, size.height / divisor)
38}
39
40#[must_use]
42pub fn aspect_ratio_f64(size: ImageSize) -> Option<f64> {
43 if size.height == 0 {
44 return None;
45 }
46
47 Some(f64::from(size.width) / f64::from(size.height))
48}