Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Merge origin/main into #1527: the Normal-blend replace moves into adjustment_layer (after #1327)

Brandon Thomas0cfc02a93381
diff --cc crates/render/src/lib.rs
index 0854cf757,9c227fa9b..1136585a2
--- a/crates/render/src/lib.rs
+++ b/crates/render/src/lib.rs
@@@ -219,43 -227,11 +227,16 @@@ pub(crate) fn render_seq_tracks(project
if !opts.effects {
continue;
}
- let mt = item.effect_time_at(t);
- let mut adjusted = canvas.snapshot();
- let cx = effects::FxCtx {
- t: mt,
- px_scale: opts.scale,
- seconds: (t - item.start).seconds(),
- timecode: &tc,
- clip_name: &item.name,
- project: Some(project),
- env: None,
- working: seq.settings.color.working,
- };
- for e in item.effects.iter().filter(|e| e.def().is_some_and(|d| !d.intrinsic)) {
- mask::apply_effect(&mut adjusted, e, &cx);
- }
- let (op, bl) = opacity_blend(item, mt);
- // Adjustment layer opacity (and opacity masks, in sequence pixels) weigh adjusted against original.
- let mut weight = Image::filled(w, h, [op; 4]);
- mask::apply_opacity_masks(&mut weight, item, mt, opts.scale);
- // Motion moves / scales the adjustment layer's frame: it only applies inside it.
- if let Some(region) = adjustment_region(seq, item, project, mt, opts.scale, w, h) {
- let cov: Vec<f32> = region.px.as_chunks::<4>().0.iter().map(|p| p[3]).collect();
- mask::scale_by(&mut weight, &cov);
- }
- let cov: Vec<f32> = weight.px.as_chunks::<4>().0.iter().map(|p| p[3]).collect();
- let mut out = canvas.snapshot();
- let mut adj = adjusted;
+ let below = canvas.snapshot();
+ let (adj, bl) = adjustment_layer(project, seq, item, t, opts, &tc, &below);
- let mut out = below;
- blend::composite(&mut out, &adj, 1.0, bl);
- canvas.set(out);
+ if bl == Blend::Normal {
- // The adjusted picture replaces the original instead of being drawn over it: what
- // Transform or Basic 3D leave empty shows nothing of the untransformed picture (#1524).
- mask::mix(&mut adj, &out, &cov);
++ // The adjusted picture already replaces the original (see `adjustment_layer`).
+ canvas.set(adj);
+ } else {
- mask::scale_by(&mut adj, &cov);
++ let mut out = below;
+ blend::composite(&mut out, &adj, 1.0, bl);
+ canvas.set(out);
+ }
continue;
}
match item_layer_ex(project, seq, item, t, opts, sources, &tc, true) {
@@@ -288,6 -264,43 +269,51 @@@
canvas
}
+ fn is_adjustment(project: &Project, item: &TrackItem) -> bool {
+ project.item(item.item).is_some_and(|pi| matches!(pi.kind, ItemKind::AdjustmentLayer { .. }))
+ }
+
+ /// An adjustment layer as a layer over `below` (the composite of the tracks under it): `below`
+ /// with the layer's effects, faded by its opacity and opacity masks and limited to its
+ /// (Motion-transformed) frame, and the blend mode it mixes with.
+ fn adjustment_layer(project: &Project, seq: &Sequence, item: &TrackItem, t: Tick, opts: RenderOptions, tc: &str, below: &Image) -> (Image, Blend) {
+ let (w, h) = (below.w, below.h);
+ let mt = item.effect_time_at(t);
+ let mut adj = below.clone();
+ let cx = effects::FxCtx {
+ t: mt,
+ px_scale: opts.scale,
+ seconds: (t - item.start).seconds(),
+ timecode: tc,
+ clip_name: &item.name,
+ project: Some(project),
+ env: None,
+ frame_rate: seq.settings.frame_rate.as_f64(),
+ working: seq.settings.color.working,
+ };
+ for e in item.effects.iter().filter(|e| e.def().is_some_and(|d| !d.intrinsic)) {
+ mask::apply_effect(&mut adj, e, &cx);
+ }
+ let (op, bl) = opacity_blend(item, mt);
- // Adjustment layer opacity (and opacity masks, in sequence pixels) mix adjusted over original.
- adj.scale_alpha(op);
- mask::apply_opacity_masks(&mut adj, item, mt, opts.scale);
++ // Adjustment layer opacity (and opacity masks, in sequence pixels) weigh adjusted against original.
++ let mut weight = Image::filled(w, h, [op; 4]);
++ mask::apply_opacity_masks(&mut weight, item, mt, opts.scale);
+ // Motion moves / scales the adjustment layer's frame: it only applies inside it.
+ if let Some(region) = adjustment_region(seq, item, project, mt, opts.scale, w, h) {
+ let cov: Vec<f32> = region.px.as_chunks::<4>().0.iter().map(|p| p[3]).collect();
++ mask::scale_by(&mut weight, &cov);
++ }
++ let cov: Vec<f32> = weight.px.as_chunks::<4>().0.iter().map(|p| p[3]).collect();
++ if bl == Blend::Normal {
++ // The adjusted picture replaces the original instead of being drawn over it: what
++ // Transform or Basic 3D leave empty shows nothing of the untransformed picture (#1524).
++ mask::mix(&mut adj, below, &cov);
++ } else {
+ mask::scale_by(&mut adj, &cov);
+ }
+ (adj, bl)
+ }
+
/// Coverage of an adjustment layer's (Motion-transformed) frame in output pixels, or `None` when
/// it covers the whole output.
fn adjustment_region(seq: &Sequence, item: &TrackItem, project: &Project, mt: Tick, scale: f32, w: usize, h: usize) -> Option<Image> {