@@ -64,6 +64,19 @@ impl Clone for Frame {
64
64
}
65
65
66
66
/// The delay of a frame relative to the previous one.
67
+ ///
68
+ /// The ratio is reduced on construction which means equality comparisons is reliable even when
69
+ /// mixing different bases. Note however that there is an upper limit to the delays that can be
70
+ /// represented exactly when using [`Self::from_saturating_duration`] which depends on the
71
+ /// granularity of the interval.
72
+ ///
73
+ /// ```
74
+ /// use image::Delay;
75
+ /// let delay_10ms = Delay::from_numer_denom_ms(10, 1);
76
+ /// let delay_10000us = Delay::from_numer_denom_ms(10_000, 1_000);
77
+ ///
78
+ /// assert_eq!(delay_10ms, delay_10000us);
79
+ /// ```
67
80
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd ) ]
68
81
pub struct Delay {
69
82
ratio : Ratio ,
@@ -304,6 +317,14 @@ impl From<Delay> for Duration {
304
317
}
305
318
}
306
319
320
+ #[ inline]
321
+ const fn gcd ( mut a : u32 , mut b : u32 ) -> u32 {
322
+ while b != 0 {
323
+ ( a, b) = ( b, a. rem_euclid ( b) ) ;
324
+ }
325
+ a
326
+ }
327
+
307
328
#[ derive( Copy , Clone , Debug ) ]
308
329
pub ( crate ) struct Ratio {
309
330
numer : u32 ,
@@ -314,9 +335,10 @@ impl Ratio {
314
335
#[ inline]
315
336
pub ( crate ) fn new ( numerator : u32 , denominator : u32 ) -> Self {
316
337
assert_ne ! ( denominator, 0 ) ;
338
+ let divisor = gcd ( numerator, denominator) ;
317
339
Self {
318
- numer : numerator,
319
- denom : denominator,
340
+ numer : numerator / divisor ,
341
+ denom : denominator / divisor ,
320
342
}
321
343
}
322
344
0 commit comments