mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-02 14:38:21 -06:00
Render: frame_position no longer panics on a zero or negative frame rate
A project file can carry footage with a frame rate of den 0; frame_position divided by zero while rendering it. Treat non-positive rates as having no frame to land between.
This commit is contained in:
@@ -80,6 +80,10 @@ pub(crate) fn frame_blend_mode(ctx: &EvalCtx, layer: &Layer) -> FrameBlend {
|
||||
/// fraction (0..1) of the way to the next frame. Exact (integer tick arithmetic), so frames that
|
||||
/// line up exactly report a fraction of 0.
|
||||
pub fn frame_position(t: Tick, rate: FrameRate) -> (i64, f64) {
|
||||
// A project file can carry a zero or negative rate; there are no frames to land between.
|
||||
if rate.num <= 0 || rate.den <= 0 {
|
||||
return (0, 0.0);
|
||||
}
|
||||
let num = t.0 as i128 * rate.num as i128;
|
||||
let den = TICKS_PER_SECOND as i128 * rate.den as i128;
|
||||
(num.div_euclid(den) as i64, num.rem_euclid(den) as f64 / den as f64)
|
||||
|
||||
@@ -167,3 +167,10 @@ fn time_stretched_precomp_frame_mixes() {
|
||||
let img = crate::render_frame(&p, cid, FrameRate::FPS_30.tick_of(1), 1.0);
|
||||
assert!(img.get(12, 10)[3] < 1e-3 && (img.get(17, 10)[3] - 1.0).abs() < 1e-3 && (img.get(22, 10)[3] - 1.0).abs() < 1e-3 && img.get(27, 10)[3] < 1e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_position_survives_degenerate_rates() {
|
||||
for rate in [FrameRate { num: 24, den: 0 }, FrameRate { num: 0, den: 1 }, FrameRate { num: -24, den: 1 }] {
|
||||
assert_eq!(frame_position(Tick(1_000_000), rate), (0, 0.0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user