PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Render Video: GIF frame delay follows timelines under 1 fps (#7045)
David Mann7ebaa94a821a
diff --git a/crates/engine/src/video_cmds.rs b/crates/engine/src/video_cmds.rsindex 6fb9ab457..a4ed4f840 100644--- a/crates/engine/src/video_cmds.rs+++ b/crates/engine/src/video_cmds.rs@@ -424,7 +424,9 @@ pub fn render_video_with(if frames > crate::timeline_cmds::MAX_DURATION {return Err(EngineError::Other(format!("the timeline is {frames} frames; Render Video is limited to {} frames", crate::timeline_cmds::MAX_DURATION)));}- let fps = base.timeline.as_ref().map_or(30.0, |t| t.fps).max(1.0);+ // Only the GIF delay reads the rate (frames come from the duration); rates under 1 fps are+ // valid timelines, so only an unusable loaded rate falls back to 30.+ let fps = base.timeline.as_ref().map(|t| t.fps).filter(|f| f.is_finite() && *f > 0.0).unwrap_or(30.0);let stem = base.name.rsplit_once('.').map_or(base.name.as_str(), |(a, _)| a).to_string();let fmt = base.pixel_format();let bounds = base.bounds();diff --git a/crates/engine/src/video_cmds/tests.rs b/crates/engine/src/video_cmds/tests.rsindex c16c7f8e3..d9d8e451e 100644--- a/crates/engine/src/video_cmds/tests.rs+++ b/crates/engine/src/video_cmds/tests.rs@@ -151,6 +151,39 @@ fn a_fully_transparent_animated_gif_frame_encodes() {assert!(f.indices.iter().all(|&i| Some(i) == f.transparent));}+/// The GIF frame delays (centiseconds, from each Graphic Control Extension) of a Render Video.+fn rendered_gif_delays(s: &Session) -> Vec<u16> {+ let mut gif = Vec::new();+ render_video_with(+ s,+ &json!({"format": "gif"}),+ |_, bytes| {+ gif = bytes.to_vec();+ Ok(())+ },+ |_, _| true,+ )+ .unwrap();+ gif.windows(6).filter(|w| w[..3] == [0x21, 0xF9, 0x04]).map(|w| u16::from_le_bytes([w[4], w[5]])).collect()+}++#[test]+fn animated_gif_delay_follows_timelines_under_one_fps() {+ // 0.5 fps is a valid timeline rate; its frames last 2 s, not the 1 s a 1 fps floor gave.+ let mut s = session();+ s.execute("timeline.create", json!({"duration": 2, "fps": 0.5})).unwrap();+ assert_eq!(rendered_gif_delays(&s), [200, 200]);+ // A loaded timeline with an unusable rate falls back to 30 fps (3 cs), as with no timeline.+ let mut doc = (*s.active().unwrap().doc).clone();+ doc.timeline = Some(Timeline::new(2, f32::NAN));+ {+ let st = s.active_mut().unwrap();+ st.doc = std::sync::Arc::new(doc);+ st.revision += 1;+ }+ assert_eq!(rendered_gif_delays(&s), [3, 3]);+}+#[test]fn inspect_reports_video_layer() {let dir = tmpdir("vinsp");