PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Video layers persist in .pcraft (frame stacks round-trip)
Brandon Thomasd549c60a2486
diff --git a/crates/format/src/convert.rs b/crates/format/src/convert.rsindex 7abf1f750..bace2596f 100644--- a/crates/format/src/convert.rs+++ b/crates/format/src/convert.rs@@ -104,6 +104,15 @@ fn opt_blob(b: &Option<Arc<Vec<u8>>>, sink: &mut dyn Sink) -> Option<Hash> {b.as_ref().map(|b| sink.blob(b))}+fn video_m(v: &photocraft_doc::VideoData, sink: &mut dyn Sink) -> crate::manifest::VideoDataM {+ crate::manifest::VideoDataM {+ frames: v.frames.iter().map(|f| surface_m(f, sink)).collect(),+ source: v.source.clone(),+ fps: v.fps,+ show_altered: v.show_altered,+ }+}+fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM {let content = match &l.content {LayerContent::Raster(s) => ContentM::Raster {@@ -202,6 +211,7 @@ fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM {}),link_group: l.link_group,excluded_channels: l.excluded_channels,+ video: l.video.as_ref().map(|v| video_m(v, sink)),}}@@ -330,6 +340,11 @@ pub(crate) struct Loader<'a> {}impl Loader<'_> {+ fn video(&mut self, m: &crate::manifest::VideoDataM) -> Result<photocraft_doc::VideoData> {+ let frames = m.frames.iter().map(|f| self.surface(f)).collect::<Result<Vec<_>>>()?;+ Ok(photocraft_doc::VideoData { frames, source: m.source.clone(), fps: m.fps, show_altered: m.show_altered })+ }+fn surface(&mut self, m: &SurfaceM) -> Result<Surface> {let f = m.format;let mut dp = unhex(&m.default)?;@@ -552,7 +567,7 @@ impl Loader<'_> {fill_cache,link_group: m.link_group,excluded_channels: m.excluded_channels,- video: None,+ video: m.video.as_ref().map(|v| self.video(v)).transpose()?,})}diff --git a/crates/format/src/manifest.rs b/crates/format/src/manifest.rsindex 9a22aea0b..ab0d05707 100644--- a/crates/format/src/manifest.rs+++ b/crates/format/src/manifest.rs@@ -162,6 +162,15 @@ pub struct SurfaceM {pub tiles: Vec<TileRef>,}+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]+pub struct VideoDataM {+ pub frames: Vec<SurfaceM>,+ pub source: photocraft_doc::VideoSource,+ pub fps: f32,+ #[serde(default)]+ pub show_altered: bool,+}+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct MaskM {pub surface: SurfaceM,@@ -212,6 +221,8 @@ pub struct LayerM {/// Advanced Blending channels left out (bit per colour channel; 0 = all blend).#[serde(default, skip_serializing_if = "is_zero_u32")]pub excluded_channels: u32,+ #[serde(default)]+ pub video: Option<VideoDataM>,}#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]diff --git a/crates/format/tests/roundtrip.rs b/crates/format/tests/roundtrip.rsindex 6445ec1e0..5d34dde62 100644--- a/crates/format/tests/roundtrip.rs+++ b/crates/format/tests/roundtrip.rs@@ -317,3 +317,38 @@ fn channel_restrictions_and_bevel_elements_roundtrip() {assert_eq!(back, doc);assert_eq!(back.layers[0].excluded_channels, 0b101);}++#[test]+fn video_layer_frames_survive_roundtrip() {+ use photocraft_doc::{Timeline, VideoData, VideoSource};+ use photocraft_geom::Rect;+ let mut doc = Document::new("Vid", photocraft_geom::Size { width: 8, height: 6 }, ColorMode::Rgb, SampleType::U8);+ doc.timeline = Some(Timeline::new(3, 24.0));+ let fmt = doc.pixel_format();+ let mut l = Layer::raster("Clip", fmt);+ let mut frames = Vec::new();+ for i in 0..3u8 {+ let mut s = photocraft_raster::Surface::new(fmt);+ let v = i as f32 / 2.0;+ s.fill_rect(Rect::new(0, 0, 8, 6), &photocraft_raster::from_rgba(&fmt, [v, 0.0, 1.0 - v, 1.0]));+ frames.push(s);+ }+ let mut vd = VideoData::new(frames, 24.0);+ vd.source = VideoSource::File { path: "/tmp/seq".into() };+ vd.show_altered = false;+ l.video = Some(vd);+ doc.layers.push(l);++ let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap();+ let back = load_from_bytes(&bytes).unwrap();+ let v = back.layers.last().unwrap().video.as_ref().expect("video survived");+ assert_eq!(v.frames.len(), 3);+ assert_eq!(v.fps, 24.0);+ assert!(!v.show_altered);+ assert_eq!(v.source, VideoSource::File { path: "/tmp/seq".into() });+ assert_eq!(back.timeline.as_ref().unwrap().duration, 3);+ // A middle frame's pixel survived.+ let mut px = [[0.0f32; 4]; 1];+ v.frames[2].read_rgba_into(Rect::from_xywh(1, 1, 1, 1), &mut px);+ assert!(px[0][0] > 0.9 && px[0][2] < 0.1, "frame 2 is reddish: {:?}", px[0]);+}