Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

The brushes paint against a tinted cutout, and a smart stroke stays put

Jareer54edda0dfb2e
diff --git a/engine/crates/concat-export/src/flatten.rs b/engine/crates/concat-export/src/flatten.rs
index 44cd85ff..9b65a0b5 100644
--- a/engine/crates/concat-export/src/flatten.rs
+++ b/engine/crates/concat-export/src/flatten.rs
@@ -95,6 +95,7 @@ pub fn flatten_timeline_in(
has_audio: Some(false),
cutout: None,
mask_dir: String::new(),
+ highlighted: false,
});
}
@@ -173,6 +174,7 @@ pub fn flatten_timeline_in(
}
_ => String::new(),
},
+ highlighted: false,
})
})
.collect()
diff --git a/engine/crates/concat-export/src/lib.rs b/engine/crates/concat-export/src/lib.rs
index e16b17c1..faa20979 100644
--- a/engine/crates/concat-export/src/lib.rs
+++ b/engine/crates/concat-export/src/lib.rs
@@ -191,6 +191,10 @@ pub struct ExportClip {
/// leaves the picture whole.
#[serde(default)]
pub cutout: Option<Cutout>,
+ /// Draw the cutout tinted over the whole picture instead of cutting
+ /// it: the view while the clip's brushes are in use. Preview only.
+ #[serde(default)]
+ pub highlighted: bool,
/// Where this clip's media has its masks - see `concat_vision::mask_dir`
/// - or empty when the flattener had no project folder to name it by.
#[serde(default)]
@@ -657,6 +661,10 @@ struct Source<F> {
passes: Vec<ShaderPass>,
}
+/// The tint a highlighted cutout wears: a teal that no footage is likely
+/// to be, and the colour the brushes themselves are drawn in.
+const HIGHLIGHT: [u8; 3] = [0, 196, 204];
+
/// A cutout as the frame loop runs it: the masks, what to paint on them,
/// and how a decoded pixel finds its place in the source.
struct CutoutJob {
@@ -705,6 +713,17 @@ impl CutoutJob {
concat_vision::cut(&mut out, &mask, &self.mapping);
Some(out)
}
+
+ /// The frame whole, with what the cutout keeps tinted over it: the
+ /// painting view. `None` as for `cut`.
+ fn highlight(&self, frame: &Frame, source_time: Rational) -> Option<Frame> {
+ let mask = self
+ .store
+ .resolved(source_time.as_f64(), &self.cutout, self.aspect)?;
+ let mut out = frame.clone();
+ concat_vision::highlight(&mut out, &mask, &self.mapping, HIGHLIGHT);
+ Some(out)
+ }
}
/// The fraction-to-pixel placement of one composited layer: fitted and
@@ -770,6 +789,7 @@ fn render_picture(
pre_chains,
passes,
cutouts,
+ highlight: _,
} = build_timeline(request, rate, visible, gpu);
let mut encoder = Encoder::create(
@@ -969,6 +989,8 @@ struct BuiltTimeline {
pre_chains: HashMap<ClipId, String>,
/// The clip's shader passes, on a GPU renderer.
passes: HashMap<ClipId, Vec<ShaderPass>>,
+ /// The clip whose cutout is drawn tinted rather than cut, if one is.
+ highlight: Option<ClipId>,
/// The layers: treatments over the stack, by span.
treatments: Vec<Treatment>,
/// The clips whose background a mask takes away.
@@ -1108,6 +1130,7 @@ fn build_timeline(
let mut pre_chains: HashMap<ClipId, String> = HashMap::new();
let mut passes: HashMap<ClipId, Vec<ShaderPass>> = HashMap::new();
let mut cutouts: HashMap<ClipId, CutoutJob> = HashMap::new();
+ let mut highlight: Option<ClipId> = None;
let lanes = visible.iter().map(|clip| clip.track).max().unwrap_or(0) + 1;
let tracks: Vec<_> = (0..lanes)
@@ -1194,6 +1217,9 @@ fn build_timeline(
if let Some(job) = CutoutJob::of(clip) {
cutouts.insert(id, job);
}
+ if clip.highlighted {
+ highlight = Some(id);
+ }
}
}
@@ -1207,6 +1233,7 @@ fn build_timeline(
pre_chains,
passes,
cutouts,
+ highlight,
}
}
@@ -1469,6 +1496,7 @@ pub fn preview_sources(
pre_chains,
passes,
cutouts,
+ highlight,
} = preview_timeline(request, rate, gpu);
let time = quantise(request.time, rate);
let plan = plan_frame(&timeline, time);
@@ -1494,11 +1522,15 @@ pub fn preview_sources(
pre,
) {
Ok(frame) => {
- let frame = match cutouts
- .get(&layer.clip)
- .and_then(|job| job.cut(&frame, layer.source_time))
- {
- Some(cut) => std::sync::Arc::new(cut),
+ let highlighted = highlight == Some(layer.clip);
+ let frame = match cutouts.get(&layer.clip).and_then(|job| {
+ if highlighted {
+ job.highlight(&frame, layer.source_time)
+ } else {
+ job.cut(&frame, layer.source_time)
+ }
+ }) {
+ Some(drawn) => std::sync::Arc::new(drawn),
None => frame,
};
sources.push(Source {
@@ -1731,6 +1763,7 @@ mod tests {
has_audio: None,
cutout: None,
mask_dir: String::new(),
+ highlighted: false,
}
}
diff --git a/engine/crates/concat-host/src/titles.rs b/engine/crates/concat-host/src/titles.rs
index 3c7bd670..e809cdc1 100644
--- a/engine/crates/concat-host/src/titles.rs
+++ b/engine/crates/concat-host/src/titles.rs
@@ -136,6 +136,7 @@ impl Titles {
has_audio: Some(false),
cutout: None,
mask_dir: String::new(),
+ highlighted: false,
},
block,
});
diff --git a/engine/crates/concat-vision/src/apply.rs b/engine/crates/concat-vision/src/apply.rs
index d97f7a2e..86d7c416 100644
--- a/engine/crates/concat-vision/src/apply.rs
+++ b/engine/crates/concat-vision/src/apply.rs
@@ -109,10 +109,52 @@ pub fn cut(frame: &mut Frame, mask: &Mask, mapping: &Mapping) {
});
}
+/// The frame with what the mask keeps tinted `colour`, and the rest as
+/// shot: the view a person paints against, where the picture stays whole
+/// and the cutout is drawn over it rather than taken out of it. The same
+/// mapping as [`cut`], so the tint lies exactly where the cut would.
+pub fn highlight(frame: &mut Frame, mask: &Mask, mapping: &Mapping, colour: [u8; 3]) {
+ let width = frame.width() as usize;
+ let height = frame.height() as usize;
+ if width == 0 || height == 0 {
+ return;
+ }
+ let pixels = frame.pixels_mut();
+ let row_bytes = width * BYTES_PER_PIXEL;
+ for (y, row) in pixels.chunks_exact_mut(row_bytes).enumerate() {
+ let fy = (y as f32 + 0.5) / height as f32;
+ for (x, pixel) in row.chunks_exact_mut(BYTES_PER_PIXEL).enumerate() {
+ let fx = (x as f32 + 0.5) / width as f32;
+ let (u, v) = mapping.source_of(fx, fy);
+ // Half way to the colour where the mask is sure, less where
+ // it is not: the tint reads as the mask's own edge.
+ let mix = mask.sample(u, v) * 0.55;
+ for c in 0..3 {
+ pixel[c] = (f32::from(pixel[c]) * (1.0 - mix) + f32::from(colour[c]) * mix + 0.5)
+ as u8;
+ }
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn a_highlight_tints_the_kept_half_and_leaves_the_rest() {
+ let mut frame = Frame::black(4, 2);
+ frame.fill([200, 200, 200, 255]);
+ let mut mask = Mask::filled(2, 1, 0);
+ mask.bytes_mut()[1] = 255;
+ highlight(&mut frame, &mask, &Mapping::default(), [0, 200, 200]);
+ let left = frame.pixel(0, 0).unwrap();
+ let right = frame.pixel(3, 0).unwrap();
+ assert_eq!(left, [200, 200, 200, 255]);
+ assert!(right[0] < 120 && right[1] == 200, "{right:?}");
+ assert_eq!(right[3], 255);
+ }
+
fn left_half_kept() -> Mask {
let mut mask = Mask::filled(8, 8, 0);
for y in 0..8 {
diff --git a/engine/crates/concat-vision/src/lib.rs b/engine/crates/concat-vision/src/lib.rs
index 05830bf0..9b28879a 100644
--- a/engine/crates/concat-vision/src/lib.rs
+++ b/engine/crates/concat-vision/src/lib.rs
@@ -51,7 +51,7 @@ pub mod segment;
pub mod store;
pub mod strokes;
-pub use apply::{Mapping, cut};
+pub use apply::{Mapping, cut, highlight};
#[cfg(feature = "infer")]
pub use brush::{Brush, Embedding};
pub use mask::Mask;
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index 177dd3e2..0981c25d 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -726,6 +726,10 @@ pub struct Studio {
cutout_jobs: HashMap<String, (bool, f32)>,
/// The smart stroke being read, by the same name, while one is.
region_job: Option<String>,
+ /// The last smart stroke as the stage drew it, kept on screen from the
+ /// release until the model has read what was under it, so the paint
+ /// does not vanish before its answer arrives.
+ pending_stroke: Option<(String, f32, bool)>,
}
// ── conversions between the document and the window ─────────────────────
@@ -1149,6 +1153,7 @@ impl Studio {
painting: true,
cutout_jobs: HashMap::new(),
region_job: None,
+ pending_stroke: None,
host,
};
studio.settings.language = studio
@@ -1513,6 +1518,18 @@ impl Studio {
};
let width = ((f64::from(width) * scale).round() as u32).max(2) & !1;
let height = ((f64::from(height) * scale).round() as u32).max(2) & !1;
+ // While the brushes are out, the clip being painted is drawn with
+ // its cutout tinted over the whole picture rather than cut, so a
+ // stroke shows what it grabbed against what it left.
+ if self.painting
+ && let Some(target) = self.paint_target()
+ && let Some(media) = self.project().media_by_id(&target.media_id)
+ && let Some(flat) = clips.iter_mut().find(|flat| {
+ flat.path == media.path && (flat.start - target.start).abs() < 1e-6
+ })
+ {
+ flat.highlighted = true;
+ }
let spec = FrameSpec {
time: f64::from(self.playhead),
width,
@@ -3386,6 +3403,9 @@ impl Studio {
/// transform command per picture, batched when there are several.
pub fn stage_released(&mut self) {
self.stage_guides.clear();
+ let overlay = matches!(self.gesture, Gesture::Paint { .. })
+ .then(|| self.stroke_overlay())
+ .filter(|(path, _, _)| !path.is_empty());
let gesture = std::mem::replace(&mut self.gesture, Gesture::None);
let touched: Vec<String> = match gesture {
Gesture::StageMove { origins, .. } => {
@@ -3403,15 +3423,18 @@ impl Studio {
..
} => {
// The stroke becomes one command, and one undo step; a
- // smart stroke then has its thing read from the frame.
+ // smart stroke then has its thing read from the frame, and
+ // stays drawn until it has been.
+ let stroke = model::Stroke {
+ tool,
+ size,
+ points: points.clone(),
+ at: Some(at),
+ };
+ self.pending_stroke = stroke.is_smart().then_some(overlay).flatten();
self.apply(Command::AddCutoutStroke {
clip_id: clip,
- stroke: model::Stroke {
- tool,
- size,
- points,
- at: Some(at),
- },
+ stroke,
});
self.ensure_regions();
return;
@@ -3661,6 +3684,12 @@ impl Studio {
pub fn cutout_painting(&mut self, on: bool) {
self.painting = on;
+ if !on {
+ self.pending_stroke = None;
+ }
+ // The monitor's view changes with the brushes: tinted while they
+ // are out, cut when they are put away.
+ self.request_preview();
}
/// The picture a press on the stage would paint: the one selected clip,
@@ -3729,7 +3758,10 @@ impl Studio {
..
} = &self.gesture
else {
- return (String::new(), 0.0, false);
+ return self
+ .pending_stroke
+ .clone()
+ .unwrap_or((String::new(), 0.0, false));
};
let Some((first, rest)) = screen.split_first() else {
return (String::new(), 0.0, false);
@@ -5299,6 +5331,7 @@ impl Studio {
|studio, _, _, (key, result)| {
studio.region_job = None;
studio.cutout_jobs.remove(&key);
+ studio.pending_stroke = None;
match result {
Ok(()) => {
studio.request_preview();