Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Video: Interpret Footage and New Video Layer from File reject an invalid frame rate (#5657)

David Mannf31fb26fe83f
diff --git a/crates/engine/src/timeline_cmds.rs b/crates/engine/src/timeline_cmds.rs
index 197c8e9cc..3121c5dab 100644
--- a/crates/engine/src/timeline_cmds.rs
+++ b/crates/engine/src/timeline_cmds.rs
@@ -59,7 +59,7 @@ fn duration_param(cmd: &str, p: &Value) -> Result<Option<usize>> {
}
/// The optional `fps` param, rejected unless it's a finite number in `(0, MAX_FPS]` (#1009).
-fn fps_param(cmd: &str, p: &Value) -> Result<Option<f32>> {
+pub(crate) fn fps_param(cmd: &str, p: &Value) -> Result<Option<f32>> {
let Some(v) = p.get("fps").filter(|v| !v.is_null()) else { return Ok(None) };
v.as_f64()
.filter(|f| f.is_finite() && *f > 0.0 && *f <= f64::from(MAX_FPS))
diff --git a/crates/engine/src/video_cmds.rs b/crates/engine/src/video_cmds.rs
index 2a9f8ae8b..301f6d12b 100644
--- a/crates/engine/src/video_cmds.rs
+++ b/crates/engine/src/video_cmds.rs
@@ -297,7 +297,7 @@ fn new_from_file(s: &mut Session, p: &Value) -> Result<Value> {
let fmt = s.active().ok_or(EngineError::NoDocument)?.doc.pixel_format();
let Footage { frames, fps: footage_fps } = load_frames(&path, fmt)?;
// An explicit `fps` wins, then the rate the footage implies, then 30.
- let fps = p.get("fps").and_then(Value::as_f64).map(|f| f as f32).or(footage_fps).unwrap_or(30.0);
+ let fps = crate::timeline_cmds::fps_param("layer.videoLayers.newVideoLayerFromFile", p)?.or(footage_fps).unwrap_or(30.0);
let n = frames.len();
let id = s.edit("New Video Layer from File", |doc, active| {
store(doc);
@@ -337,10 +337,10 @@ fn replace_footage(s: &mut Session, p: &Value) -> Result<Value> {
}
fn interpret_footage(s: &mut Session, p: &Value) -> Result<Value> {
- let fps = p.get("fps").and_then(Value::as_f64);
+ let fps = crate::timeline_cmds::fps_param("layer.videoLayers.interpretFootage", p)?;
edit_video(s, "Interpret Footage", move |v, _| {
if let Some(f) = fps {
- v.fps = f as f32;
+ v.fps = f;
}
Ok(())
})
diff --git a/crates/engine/src/video_cmds/tests.rs b/crates/engine/src/video_cmds/tests.rs
index 22d639d49..8c8673af9 100644
--- a/crates/engine/src/video_cmds/tests.rs
+++ b/crates/engine/src/video_cmds/tests.rs
@@ -330,6 +330,32 @@ fn a_still_gif_is_one_frame_at_the_default_rate() {
assert_eq!(s.active().unwrap().doc.layer(id).unwrap().video.as_ref().unwrap().fps, 10.0);
}
+/// Interpret Footage and New Video Layer from File reject an fps that isn't a finite number in
+/// `(0, MAX_FPS]` and keep the layer as it was (#5657). 1e39 used to become an infinite rate the
+/// next `.pcraft` save refused.
+#[test]
+fn an_invalid_frame_rate_is_an_error_and_changes_nothing() {
+ let mut s = session();
+ let id = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap());
+ let fps = |s: &Session| s.active().unwrap().doc.layer(id).unwrap().video.as_ref().unwrap().fps;
+ assert_eq!(fps(&s), 30.0);
+ for bad in [json!(0), json!(-1), json!(1e39), json!("x")] {
+ assert!(s.execute("layer.videoLayers.interpretFootage", json!({"fps": bad})).is_err(), "fps {bad}");
+ assert_eq!(fps(&s), 30.0, "fps {bad}");
+ }
+ s.execute("layer.videoLayers.interpretFootage", json!({"fps": 24})).unwrap();
+ assert_eq!(fps(&s), 24.0);
+
+ let dir = tmpdir("gifin-badfps");
+ let path = format!("{dir}/anim.gif");
+ write_gif(&path, 8, 8, &[([255, 0, 0], 10), ([0, 255, 0], 10)]);
+ let layers = s.active().unwrap().doc.layer_count();
+ for bad in [json!(0), json!(-1), json!(1e39), json!("x")] {
+ assert!(s.execute("layer.videoLayers.newVideoLayerFromFile", json!({"path": path, "fps": bad})).is_err(), "fps {bad}");
+ assert_eq!(s.active().unwrap().doc.layer_count(), layers, "fps {bad}");
+ }
+}
+
#[test]
fn video_frames_to_layers_takes_every_gif_frame() {
let dir = tmpdir("gifin-ftl");