1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::fmt;
use std::fmt::Display;

/// Encapsulation of the TIFF type that represents a signed rational number
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct IRational {
	pub numerator: i32,
	pub denominator: i32,
}

impl IRational {
	/// Floating point value (numerator divided by denominator)
	pub fn value(&self) -> f64 {
		(self.numerator as f64) / (self.denominator as f64)
	}
}

impl Display for IRational {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}/{}", self.numerator, self.denominator)
	}
}

#[derive(Copy, Clone, Debug, PartialEq)]
/// Encapsulation of the TIFF type that represents an unsigned rational number
pub struct URational {
	pub numerator: u32,
	pub denominator: u32,
}

impl URational {
	/// Floating point value (numerator divided by denominator)
	pub fn value(&self) -> f64 {
		f64::from(self.numerator) / f64::from(self.denominator)
	}
}

impl Display for URational {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}/{}", self.numerator, self.denominator)
	}
}