Skip to content

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

release-gate: Fix Extract edges and caption font targeting

whatever-industriesf10fda5883c6
diff --git a/crates/gpu/src/fx.wgsl b/crates/gpu/src/fx.wgsl
index 1fe152f83..7d97231f3 100644
--- a/crates/gpu/src/fx.wgsl
+++ b/crates/gpu/src/fx.wgsl
@@ -383,7 +383,12 @@ fn color_op(op: u32, c: vec3<f32>) -> vec3<f32> {
case OP_EXTRACT: {
let lo = u.p0.x; let hi = u.p0.y; let soft = u.p0.z;
let l = linear_to_srgb1(max(luma709(c), 0.0));
- let inside = min(clamp((l - lo) / soft, 0.0, 1.0), clamp((hi - l) / soft, 0.0, 1.0));
+ // Hard cutoffs avoid both division by zero and an artificial narrow fade
+ // that amplifies transfer-function rounding relative to the CPU reference.
+ var inside = select(0.0, 1.0, l > lo && l < hi);
+ if soft > 0.0 {
+ inside = min(clamp((l - lo) / soft, 0.0, 1.0), clamp((hi - l) / soft, 0.0, 1.0));
+ }
return vec3(select(inside, 1.0 - inside, u.i1.x != 0u));
}
case OP_INVERT: {
diff --git a/crates/gpu/src/fx_tests.rs b/crates/gpu/src/fx_tests.rs
index 6cd1a1d9b..31fc55a5a 100644
--- a/crates/gpu/src/fx_tests.rs
+++ b/crates/gpu/src/fx_tests.rs
@@ -126,8 +126,14 @@ fn cases() -> Vec<(&'static str, Vec<(&'static str, ParamValue)>, bool)> {
("gamma_correction", vec![("gamma", fl(-3.0))], false),
("levels", vec![("in_black", fl(20.0)), ("in_white", fl(230.0)), ("out_black", fl(10.0)), ("out_white", fl(240.0)), ("gamma", fl(150.0))], false),
("levels", vec![("in_black", fl(200.0)), ("in_white", fl(0.0)), ("gamma", fl(5.0)), ("out_white", fl(-50.0))], true),
+ ("extract", vec![], true),
+ ("extract", vec![("invert", ParamValue::Bool(true))], true),
("extract", vec![("black", fl(40.0)), ("white", fl(200.0)), ("softness", fl(30.0))], true),
("extract", vec![("black", fl(90.0)), ("white", fl(160.0)), ("invert", ParamValue::Bool(true))], true),
+ ("extract", vec![("black", fl(90.0)), ("white", fl(160.0)), ("softness", fl(1.0))], true),
+ ("extract", vec![("black", fl(40.0)), ("white", fl(200.0)), ("softness", fl(100.0))], true),
+ ("extract", vec![("black", fl(160.0)), ("white", fl(90.0))], true),
+ ("extract", vec![("black", fl(90.0)), ("white", fl(90.0))], true),
("invert", vec![("blend", fl(30.0))], false),
("invert", vec![("channel", ParamValue::Choice(1))], false),
("invert", vec![("channel", ParamValue::Choice(2)), ("blend", fl(70.0))], false),
@@ -329,6 +335,34 @@ fn gpu_effects_match_cpu_exactly() {
assert!(failures.is_empty(), "GPU effects differ from the CPU:\n{}", failures.join("\n"));
}
+#[test]
+fn gpu_extract_zero_softness_has_no_fractional_edge() {
+ let Some((dev, q)) = device() else {
+ eprintln!("no GPU adapter; skipping");
+ return;
+ };
+ let mut c = GpuCompositor::new(&dev, &q);
+ let (w, h) = (67, 41);
+ let px = picture(w, h);
+ let frame = VideoFrame::rgba_f32(w, h, px.clone());
+ let mut masks = Vec::new();
+ for invert in [false, true] {
+ let e = effect("extract", &[("black", fl(90.0)), ("white", fl(160.0)), ("invert", ParamValue::Bool(invert))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w as usize, h as usize).unwrap();
+ let (_, _, gpu) = c.effect_image(&frame, &LayerFx { size: (w, h), decimation: 1, ops: vec![op] }).unwrap();
+ for (src, out) in px.chunks_exact(4).zip(gpu.chunks_exact(4)) {
+ assert_eq!(out[3], src[3], "Extract must preserve alpha");
+ assert!(out[0] == 0.0 || out[0] == out[3], "zero softness produced a fractional edge: {out:?}");
+ assert_eq!(out[0], out[1]);
+ assert_eq!(out[0], out[2]);
+ }
+ masks.push(gpu);
+ }
+ for ((normal, inverted), src) in masks[0].chunks_exact(4).zip(masks[1].chunks_exact(4)).zip(px.chunks_exact(4)) {
+ assert_eq!(normal[0] + inverted[0], src[3], "inversion must complement the premultiplied mask");
+ }
+}
+
/// Chains run in order (and Unsharp keeps its original while blurring, between other blurs).
#[test]
fn gpu_effect_chains_match_cpu() {
diff --git a/crates/render/src/gpufx.rs b/crates/render/src/gpufx.rs
index 572d9627d..00b827fd6 100644
--- a/crates/render/src/gpufx.rs
+++ b/crates/render/src/gpufx.rs
@@ -320,12 +320,17 @@ impl FxOp {
g: 100.0 / f(e, "gamma", cx).max(1.0),
}
}
- "extract" => FxOp::Extract {
- lo: f(e, "black", cx) / 255.0,
- hi: f(e, "white", cx) / 255.0,
- soft: f(e, "softness", cx) / 100.0 * 0.2 + 1e-4,
- invert: b(e, "invert"),
- },
+ "extract" => {
+ let softness = f(e, "softness", cx);
+ FxOp::Extract {
+ lo: f(e, "black", cx) / 255.0,
+ hi: f(e, "white", cx) / 255.0,
+ // Zero softness is a hard cutoff. A tiny substitute fade amplifies
+ // CPU/GPU transfer-function rounding and introduces unwanted grey edges.
+ soft: if softness <= 0.0 { 0.0 } else { softness / 100.0 * 0.2 + 1e-4 },
+ invert: b(e, "invert"),
+ }
+ }
"invert" => FxOp::Invert { channel: choice(e, "channel"), blend: f(e, "blend", cx) / 100.0 },
"posterize" => FxOp::Posterize { n: f(e, "levels", cx).max(2.0) - 1.0 },
"gaussian_blur" | "gaussian_blur_legacy" => {
@@ -730,7 +735,12 @@ impl FxOp {
let (lo, hi, soft, inv) = (*lo, *hi, *soft, *invert);
img.map_rgb(|c, _, _| {
let l = linear_to_srgb(luma709(c[0], c[1], c[2]).max(0.0));
- let inside = ((l - lo) / soft).clamp(0.0, 1.0).min(((hi - l) / soft).clamp(0.0, 1.0));
+ let inside = if soft <= 0.0 {
+ // Match the feathered function's limiting value at the cutoffs.
+ if l > lo && l < hi { 1.0 } else { 0.0 }
+ } else {
+ ((l - lo) / soft).clamp(0.0, 1.0).min(((hi - l) / soft).clamp(0.0, 1.0))
+ };
let v = if inv { 1.0 - inside } else { inside };
[v, v, v]
});
@@ -1086,3 +1096,55 @@ fn resample_cpu(src: &Image, inv: &Affine, rect: [u32; 4]) -> Image {
});
out
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use filmcraft_project::{ParamValue, find_effect};
+ use filmcraft_time::Tick;
+
+ #[test]
+ fn extract_zero_softness_has_no_fractional_edge() {
+ let cx = FxCtx {
+ t: Tick::ZERO,
+ px_scale: 1.0,
+ seconds: 0.0,
+ timecode: "",
+ clip_name: "",
+ project: None,
+ env: None,
+ working: filmcraft_color::WorkingSpace::Rec709,
+ };
+ // Both sides of each cutoff, including samples inside the former artificial fade.
+ let lo = 90.0 / 255.0;
+ let hi = 160.0 / 255.0;
+ let samples = [lo - 0.00005, lo + 0.00005, hi - 0.00005, hi + 0.00005];
+ for softness in [0.0, -1.0] {
+ for invert in [false, true] {
+ for alpha in [1.0, 0.5] {
+ let mut e = find_effect("extract").unwrap().instance();
+ for (key, value) in [("black", 90.0), ("white", 160.0), ("softness", softness)] {
+ e.params.get_mut(key).unwrap().value = ParamValue::Float(value);
+ }
+ e.params.get_mut("invert").unwrap().value = ParamValue::Bool(invert);
+ let mut img = Image {
+ w: 4,
+ h: 1,
+ px: samples
+ .iter()
+ .flat_map(|v| {
+ let c = filmcraft_color::srgb_to_linear(*v) * alpha;
+ [c, c, c, alpha]
+ })
+ .collect(),
+ };
+ FxOp::eval(&e, &cx, 4, 1).unwrap().apply(&mut img);
+ for (pixel, inside) in img.px.chunks_exact(4).zip([false, true, true, false]) {
+ let value = if inside != invert { alpha } else { 0.0 };
+ assert_eq!(pixel, [value, value, value, alpha], "softness={softness}, invert={invert}");
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/crates/ui-egui/src/panels/text.rs b/crates/ui-egui/src/panels/text.rs
index 842000955..9ddf66ac4 100644
--- a/crates/ui-egui/src/panels/text.rs
+++ b/crates/ui-egui/src/panels/text.rs
@@ -402,8 +402,10 @@ fn style_strip(app: &mut FilmcraftApp, ui: &mut egui::Ui, r: Rect, track_idx: us
// names starting with '.' are macOS-private system faces (UI/fallback fonts, some without a space glyph)
for (f, _) in filmcraft_text::families().into_iter().filter(|(f, _)| !f.starts_with('.')) {
let resp = ui.selectable_label(f.eq_ignore_ascii_case(&st.font), &f);
- if ui.is_rect_visible(resp.rect) {
- app.auto.add(&format!("text.captions.style.font.option.{f}"), resp.rect, &f);
+ // An edge row may be only partly visible; automation must click its visible portion.
+ let visible = resp.rect.intersect(ui.clip_rect());
+ if visible.is_positive() {
+ app.auto.add(&format!("text.captions.style.font.option.{f}"), visible, &f);
}
if resp.clicked() {
actions.push(("captions.setStyle".into(), json!({"track": tr.id.0, "font": f})));