Skip to content

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

GPU7: Lumetri Color (basic, creative, vignette) on the GPU compositor (#30)

Manuel Camacho029e9bb04097
diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs
index 424457e21..d16be17dd 100644
--- a/crates/gpu/src/fx.rs
+++ b/crates/gpu/src/fx.rs
@@ -51,6 +51,7 @@ const OP_VFLIP: u32 = 27;
const OP_MIRROR: u32 = 28;
const OP_OFFSET: u32 = 29;
const OP_VIDEO_LIMITER: u32 = 30;
+const OP_LUMETRI: u32 = 31;
type Target = (wgpu::Texture, wgpu::TextureView);
@@ -87,7 +88,7 @@ struct Step {
run: bool,
code: u32,
i1: [u32; 4],
- p: [f32; 16],
+ p: [f32; 24],
/// Unsharp combine: reads the blurred image and the original.
combine: bool,
/// A blur pass of Unsharp Mask (the original stays untouched until the combine).
@@ -95,7 +96,7 @@ struct Step {
}
fn step(code: u32, i1: [u32; 4], p: &[f32]) -> Step {
- let mut q = [0.0; 16];
+ let mut q = [0.0; 24];
for (d, s) in q.iter_mut().zip(p) {
*d = *s;
}
@@ -185,6 +186,65 @@ fn steps(op: &FxOp) -> Vec<Step> {
FxOp::VideoLimiter { max, comp, axis, warn, warning_color } => {
out.push(step(OP_VIDEO_LIMITER, [*axis, *warn as u32, 0, 0], &[*max, *comp, warning_color[0], warning_color[1], warning_color[2]]));
}
+ FxOp::Lumetri {
+ gains,
+ exposure,
+ contrast,
+ hl,
+ sh,
+ wh,
+ bl,
+ sat,
+ creative_on,
+ faded,
+ vib,
+ st,
+ ht,
+ vignette_on,
+ va,
+ vmid,
+ vround,
+ vfeather,
+ aspect,
+ ..
+ } => {
+ let ge = [gains[0] * exposure, gains[1] * exposure, gains[2] * exposure];
+ let b0 = -bl * 0.15;
+ let w0 = 1.0 - wh * 0.15;
+ let sh_k = sh * 0.35;
+ let hl_k = hl * 0.35;
+ let vround_aspect = if *vround < 0.0 { aspect.powf(-vround) } else { 1.0 };
+ let st_k = [(st[0] - 0.5) * 0.3, (st[1] - 0.5) * 0.3, (st[2] - 0.5) * 0.3];
+ let ht_k = [(ht[0] - 0.5) * 0.3, (ht[1] - 0.5) * 0.3, (ht[2] - 0.5) * 0.3];
+
+ let p = [
+ ge[0],
+ ge[1],
+ ge[2],
+ b0,
+ w0,
+ sh_k,
+ hl_k,
+ *contrast,
+ *faded,
+ *sat,
+ *vib,
+ *va,
+ *vmid,
+ *vfeather,
+ vround_aspect,
+ 0.0,
+ st_k[0],
+ st_k[1],
+ st_k[2],
+ 0.0,
+ ht_k[0],
+ ht_k[1],
+ ht_k[2],
+ 0.0,
+ ];
+ out.push(step(OP_LUMETRI, [*creative_on as u32, *vignette_on as u32, 0, 0], &p));
+ }
}
out
}
@@ -350,7 +410,7 @@ impl FxStage {
(true, Some(o)) => views.get(o)?,
_ => dummy,
};
- let mut bytes = Vec::with_capacity(96);
+ let mut bytes = Vec::with_capacity(128);
for v in [s.code, w, h, 0].iter().chain(&s.i1) {
bytes.extend_from_slice(&v.to_le_bytes());
}
diff --git a/crates/gpu/src/fx.wgsl b/crates/gpu/src/fx.wgsl
index 6e617b7c3..1fe152f83 100644
--- a/crates/gpu/src/fx.wgsl
+++ b/crates/gpu/src/fx.wgsl
@@ -10,6 +10,8 @@ struct U {
p1: vec4<f32>,
p2: vec4<f32>,
p3: vec4<f32>,
+ p4: vec4<f32>,
+ p5: vec4<f32>,
};
@group(0) @binding(0) var<uniform> u: U;
@@ -47,6 +49,7 @@ const OP_VFLIP: u32 = 27u;
const OP_MIRROR: u32 = 28u;
const OP_OFFSET: u32 = 29u;
const OP_VIDEO_LIMITER: u32 = 30u;
+const OP_LUMETRI: u32 = 31u;
fn size() -> vec2<i32> {
return vec2<i32>(i32(u.i0.y), i32(u.i0.z));
@@ -433,6 +436,95 @@ fn color_op(op: u32, c: vec3<f32>) -> vec3<f32> {
}
}
+fn lumetri_op(c: vec3<f32>, p: vec2<i32>) -> vec3<f32> {
+ let s = size();
+ let creative_on = u.i1.x != 0u;
+ let vignette_on = u.i1.y != 0u;
+
+ // white balance + exposure in linear light
+ let ge = u.p0.xyz;
+ let lin = c * ge;
+ var v = enc(lin);
+
+ // whites / blacks: endpoints
+ let b0 = u.p0.w;
+ let w0 = u.p1.x;
+ let denom = max(w0 - b0, 1e-3);
+ v = (v - vec3(b0)) / denom;
+
+ // highlights / shadows: luma-weighted lift/compress, hue preserving
+ let sh_k = u.p1.y;
+ let hl_k = u.p1.z;
+ let l = luma709(v);
+ let ws_val = clamp(1.0 - l, 0.0, 1.0);
+ let ws = ws_val * ws_val * ws_val;
+ let whl_val = clamp(l, 0.0, 1.0);
+ let whl = whl_val * whl_val * whl_val;
+ let nl = max(l + sh_k * ws + hl_k * whl, 0.0);
+ if l > 1e-5 {
+ let k = nl / l;
+ v = v * k;
+ }
+
+ // contrast: smooth S-curve around mid grey
+ let contrast = u.p1.w;
+ if abs(contrast) > 1e-4 {
+ let k = 1.0 + contrast;
+ let q = clamp(v, vec3(0.0), vec3(1.0));
+ let sc = q * q * (vec3(3.0) - 2.0 * q);
+ if k >= 1.0 {
+ v = q + (sc - q) * (k - 1.0);
+ } else {
+ v = vec3(0.5) + (q - vec3(0.5)) * k;
+ }
+ }
+
+ // faded film: lift blacks and compress
+ let faded = u.p2.x;
+ if faded > 0.0 {
+ v = v * (1.0 - 0.25 * faded) + vec3(0.12 * faded);
+ }
+
+ // split tone
+ if creative_on {
+ let st_k = u.p4.xyz;
+ let ht_k = u.p5.xyz;
+ let l2 = clamp(luma709(v), 0.0, 1.0);
+ v = v + st_k * (1.0 - l2) + ht_k * l2;
+ }
+
+ // saturation & vibrance
+ let sat = u.p2.y;
+ let vib = u.p2.z;
+ let l3 = luma709(v);
+ let cur_sat = max(max(v.x, v.y), v.z) - min(min(v.x, v.y), v.z);
+ let sat_mult = sat * (1.0 + vib * (1.0 - clamp(cur_sat, 0.0, 1.0)));
+ v = vec3(l3) + (v - vec3(l3)) * sat_mult;
+
+ // vignette
+ let va = u.p2.w;
+ if vignette_on && abs(va) > 1e-4 {
+ let vmid = u.p3.x;
+ let vfeather = u.p3.y;
+ let vround_aspect = u.p3.z;
+ let w = f32(s.x);
+ let h = f32(s.y);
+ let nx = (f32(p.x) / w - 0.5) * 2.0 * vround_aspect;
+ let ny = (f32(p.y) / h - 0.5) * 2.0;
+ let d = sqrt(nx * nx + ny * ny) / 1.4142135623730951;
+ let edge = clamp((d - vmid * 0.9) / (max(vfeather, 0.01) * 0.9), 0.0, 1.0);
+ let e2 = edge * edge * (3.0 - 2.0 * edge);
+ let k = 1.0 + va * 0.2 * e2;
+ if va < 0.0 {
+ v = v * max(k, 0.0);
+ } else {
+ v = v + (vec3(1.0) - v) * (k - 1.0);
+ }
+ }
+
+ return dec(v);
+}
+
// One output pixel of every op but the running-sum box blur.
fn pixel(op: u32, p: vec2<i32>) -> vec4<f32> {
let s = size();
@@ -608,6 +700,16 @@ fn pixel(op: u32, p: vec2<i32>) -> vec4<f32> {
let res = vec3(enc_to_linear_limiter(out_enc.x), enc_to_linear_limiter(out_enc.y), enc_to_linear_limiter(out_enc.z));
return vec4(res * a, a);
}
+ case OP_LUMETRI: {
+ let o = ld(p);
+ let a = o.a;
+ if a <= 1e-6 {
+ return o;
+ }
+ let c = o.rgb / a;
+ let res = lumetri_op(c, p);
+ return vec4(res * a, a);
+ }
default: {
let o = ld(p);
let a = o.a;
diff --git a/crates/gpu/src/fx_tests.rs b/crates/gpu/src/fx_tests.rs
index 02d8e7114..2654556e2 100644
--- a/crates/gpu/src/fx_tests.rs
+++ b/crates/gpu/src/fx_tests.rs
@@ -214,6 +214,89 @@ fn cases() -> Vec<(&'static str, Vec<(&'static str, ParamValue)>, bool)> {
],
false,
),
+ (
+ "lumetri",
+ vec![
+ ("temperature", fl(20.0)),
+ ("tint", fl(-10.0)),
+ ("exposure", fl(0.5)),
+ ("contrast", fl(15.0)),
+ ("highlights", fl(-20.0)),
+ ("shadows", fl(25.0)),
+ ("whites", fl(10.0)),
+ ("blacks", fl(-15.0)),
+ ("saturation", fl(110.0)),
+ ("creative_on", ParamValue::Bool(false)),
+ ("vignette_on", ParamValue::Bool(false)),
+ ],
+ false,
+ ),
+ (
+ "lumetri",
+ vec![
+ ("basic_on", ParamValue::Bool(false)),
+ ("creative_sat", fl(120.0)),
+ ("vibrance", fl(30.0)),
+ ("faded_film", fl(25.0)),
+ ("shadow_tint", col(0.4, 0.45, 0.6)),
+ ("highlight_tint", col(0.6, 0.55, 0.4)),
+ ("vignette_on", ParamValue::Bool(false)),
+ ],
+ false,
+ ),
+ (
+ "lumetri",
+ vec![
+ ("basic_on", ParamValue::Bool(false)),
+ ("creative_on", ParamValue::Bool(false)),
+ ("vignette_amount", fl(-3.0)),
+ ("vignette_midpoint", fl(45.0)),
+ ("vignette_roundness", fl(-30.0)),
+ ("vignette_feather", fl(60.0)),
+ ],
+ false,
+ ),
+ (
+ "lumetri",
+ vec![
+ ("temperature", fl(-15.0)),
+ ("tint", fl(10.0)),
+ ("exposure", fl(0.3)),
+ ("contrast", fl(20.0)),
+ ("highlights", fl(-10.0)),
+ ("shadows", fl(15.0)),
+ ("whites", fl(-5.0)),
+ ("blacks", fl(5.0)),
+ ("saturation", fl(105.0)),
+ ("creative_sat", fl(110.0)),
+ ("vibrance", fl(20.0)),
+ ("faded_film", fl(15.0)),
+ ("shadow_tint", col(0.48, 0.5, 0.55)),
+ ("highlight_tint", col(0.52, 0.5, 0.45)),
+ ("vignette_amount", fl(2.0)),
+ ("vignette_midpoint", fl(50.0)),
+ ("vignette_roundness", fl(20.0)),
+ ("vignette_feather", fl(50.0)),
+ ],
+ false,
+ ),
+ (
+ "lumetri",
+ vec![("exposure", fl(4.0)), ("contrast", fl(100.0)), ("temperature", fl(100.0)), ("tint", fl(100.0)), ("whites", fl(100.0)), ("blacks", fl(100.0))],
+ false,
+ ),
+ (
+ "lumetri",
+ vec![
+ ("exposure", fl(-4.0)),
+ ("contrast", fl(-100.0)),
+ ("temperature", fl(-100.0)),
+ ("tint", fl(-100.0)),
+ ("whites", fl(-100.0)),
+ ("blacks", fl(-100.0)),
+ ],
+ false,
+ ),
]
}
@@ -354,6 +437,10 @@ fn hostile_parameters_are_bounded() {
effect("video_limiter", &[("compression", fl(f64::INFINITY))]),
effect("video_limiter", &[("axis", fl(f64::NAN))]),
effect("video_limiter", &[("warning_color", ParamValue::Color([f32::NAN, 0.0, 0.0, 1.0]))]),
+ effect("lumetri", &[("exposure", fl(f64::NAN))]),
+ effect("lumetri", &[("temperature", fl(f64::INFINITY))]),
+ effect("lumetri", &[("contrast", fl(f64::NAN))]),
+ effect("lumetri", &[("vignette_amount", fl(f64::NAN))]),
];
for e in &nan {
let op = FxOp::eval(e, &cx(Tick::ZERO, 1.0), w, h);
@@ -392,6 +479,67 @@ fn hostile_parameters_are_bounded() {
assert!(c.effect_image(&frame, &LayerFx { size: (1 << 20, 4), decimation: 1, ops: vec![FxOp::BlackWhite] }).is_none());
}
+#[test]
+fn lumetri_unsupported_sections_fall_back() {
+ let (w, h) = (40usize, 24usize);
+ // sharpen > 0 and input LUT (explicitly required by brief)
+ let e = effect("lumetri", &[("sharpen", fl(10.0)), ("input_lut", ParamValue::Text("custom.cube".into()))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()), "sharpen > 0 and input_lut must fall back to CPU");
+
+ // sharpen alone
+ let e = effect("lumetri", &[("sharpen", fl(20.0))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // input_lut alone
+ let e = effect("lumetri", &[("input_lut", ParamValue::Text("test.cube".into()))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // look_lut
+ let e = effect("lumetri", &[("look_lut", ParamValue::Text("look.cube".into()))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // look > 0
+ let e = effect("lumetri", &[("look", ch(1))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // curve_luma modified
+ let e = effect("lumetri", &[("curve_luma", ParamValue::Curve(vec![[0.0, 0.1], [1.0, 0.9]]))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // hue_vs_sat non-empty
+ let e = effect("lumetri", &[("hue_vs_sat", ParamValue::Curve(vec![[0.5, 0.5]]))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // wheel offset
+ let e = effect("lumetri", &[("wheel_shadows", pt(0.5, 0.0))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // wheel lightness
+ let e = effect("lumetri", &[("wheel_midtones_l", fl(20.0))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // HSL secondary on
+ let e = effect("lumetri", &[("hsl_on", ParamValue::Bool(true))]);
+ let op = FxOp::eval(&e, &cx(Tick::ZERO, 1.0), w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+
+ // HDR working space
+ let e = effect("lumetri", &[]);
+ let mut cx_hdr = cx(Tick::ZERO, 1.0);
+ cx_hdr.working = filmcraft_color::WorkingSpace::Rec2100Pq;
+ let op = FxOp::eval(&e, &cx_hdr, w, h);
+ assert!(op.as_ref().is_some_and(|o| !o.gpu_ok()));
+}
+
/// Layers with effect chains in a composited plan — YUV and RGBA sources, a decimated working
/// image, rotated / scaled placement, partial opacity and alpha, blend modes — match the CPU plan
/// executor.
diff --git a/crates/render/src/effects.rs b/crates/render/src/effects.rs
index 15ee24da2..07eb528af 100644
--- a/crates/render/src/effects.rs
+++ b/crates/render/src/effects.rs
@@ -40,11 +40,10 @@ pub(crate) fn f(e: &EffectInstance, id: &str, cx: &FxCtx) -> f32 {
pub(crate) fn b(e: &EffectInstance, id: &str) -> bool {
e.param(id).and_then(|p| p.value.as_bool()).unwrap_or(false)
}
-/// A section switch (missing in older projects = on).
-fn on(e: &EffectInstance, id: &str) -> bool {
+pub(crate) fn on(e: &EffectInstance, id: &str) -> bool {
e.param(id).and_then(|p| p.value.as_bool()).unwrap_or(true)
}
-fn text<'e>(e: &'e EffectInstance, id: &str) -> &'e str {
+pub(crate) fn text<'e>(e: &'e EffectInstance, id: &str) -> &'e str {
match e.param(id).map(|p| &p.value) {
Some(ParamValue::Text(s)) => s,
_ => "",
@@ -94,7 +93,7 @@ pub fn apply(img: &mut Image, e: &EffectInstance, cx: &FxCtx) {
return;
}
// effects with a GPU implementation: evaluated parameters + CPU reference (`gpufx`)
- if let Some(op) = crate::gpufx::FxOp::eval(e, cx, img.w, img.h) {
+ if let Some(op) = crate::gpufx::FxOp::eval(e, cx, img.w, img.h).filter(|op| e.effect != "lumetri" || op.gpu_ok()) {
op.apply(img);
return;
}
@@ -575,9 +574,7 @@ pub(crate) fn key(img: &mut Image, e: &EffectInstance, cx: &FxCtx) {
});
}
-/// The grading signal of the effect's section: HDR White (`white_id`) applies in HDR working
-/// spaces only.
-fn grade_space(e: &EffectInstance, cx: &FxCtx, white_id: &str) -> GradeSpace {
+pub(crate) fn grade_space(e: &EffectInstance, cx: &FxCtx, white_id: &str) -> GradeSpace {
GradeSpace::new(cx.working, f(e, white_id, cx))
}
@@ -1026,16 +1023,16 @@ fn hue_lut(points: &[[f32; 2]], n: usize) -> Option<Vec<f32>> {
Some(lut[n..2 * n].to_vec())
}
-fn curve_param(e: &EffectInstance, id: &str) -> Option<Vec<[f32; 2]>> {
+pub(crate) fn curve_param(e: &EffectInstance, id: &str) -> Option<Vec<[f32; 2]>> {
e.param(id).and_then(|p| p.value.as_curve().map(|c| c.to_vec()))
}
-fn is_identity_curve(c: &[[f32; 2]]) -> bool {
+pub(crate) fn is_identity_curve(c: &[[f32; 2]]) -> bool {
c.iter().all(|p| (p[0] - p[1]).abs() < 1e-4)
}
/// Wheel offset (zero-mean RGB direction for a wheel position).
-fn wheel_rgb(v: Vec2) -> [f32; 3] {
+pub(crate) fn wheel_rgb(v: Vec2) -> [f32; 3] {
let len = (v.x * v.x + v.y * v.y).sqrt().min(1.0) as f32;
if len < 1e-5 {
return [0.0; 3];
diff --git a/crates/render/src/gpufx.rs b/crates/render/src/gpufx.rs
index a718438a8..0b432ea0c 100644
--- a/crates/render/src/gpufx.rs
+++ b/crates/render/src/gpufx.rs
@@ -14,7 +14,7 @@ use filmcraft_geom::Affine;
use filmcraft_project::EffectInstance;
use rayon::prelude::*;
-use crate::effects::{FxCtx, b, choice, color, dec, enc, f, gaussian_boxes, point};
+use crate::effects::{FxCtx, b, choice, color, dec, enc, f, gaussian_boxes, grade_space, on, point, text};
use crate::image::Image;
/// A bilinear resample of the working image through an affine map (`Image::transformed` at a
@@ -175,6 +175,28 @@ pub enum FxOp {
warn: bool,
warning_color: [f32; 3],
},
+ Lumetri {
+ gains: [f32; 3],
+ exposure: f32,
+ contrast: f32,
+ hl: f32,
+ sh: f32,
+ wh: f32,
+ bl: f32,
+ sat: f32,
+ creative_on: bool,
+ faded: f32,
+ vib: f32,
+ st: [f32; 3],
+ ht: [f32; 3],
+ vignette_on: bool,
+ va: f32,
+ vmid: f32,
+ vround: f32,
+ vfeather: f32,
+ aspect: f32,
+ gpu_capable: bool,
+ },
}
/// Effect ids [`FxOp::eval`] understands (the GPU-capable standard effects).
@@ -212,6 +234,7 @@ pub const GPU_EFFECTS: &[&str] = &[
"alpha_adjust",
"vignette",
"video_limiter",
+ "lumetri",
];
/// `Image::transformed` without the mip path: the destination rectangle it writes and the inverse.
@@ -471,6 +494,80 @@ impl FxOp {
let wc = dec([c[0], c[1], c[2]]);
FxOp::VideoLimiter { max: 1.0 + clip_level / 100.0, comp: comp_val, axis, warn, warning_color: wc }
}
+ "lumetri" => {
+ use crate::effects::{curve_param, is_identity_curve, wheel_rgb};
+ let (basic_on, creative_on, vignette_on) = (on(e, "basic_on"), on(e, "creative_on"), on(e, "vignette_on"));
+ let gs = grade_space(e, cx, "hdr_white");
+ let is_hdr = gs.is_hdr();
+ let has_input_lut = basic_on && !text(e, "input_lut").is_empty();
+ let has_look_lut = creative_on && !text(e, "look_lut").is_empty();
+ let look = if creative_on && !has_look_lut { choice(e, "look") } else { 0 };
+ let sharpen = if creative_on { f(e, "sharpen", cx) / 100.0 } else { 0.0 };
+
+ let curves_on = on(e, "curves_on");
+ let has_curves = curves_on && {
+ ["curve_luma", "curve_red", "curve_green", "curve_blue"].iter().any(|id| curve_param(e, id).is_some_and(|c| !is_identity_curve(&c)))
+ || ["hue_vs_sat", "hue_vs_hue", "hue_vs_luma", "luma_vs_sat", "sat_vs_sat"]
+ .iter()
+ .any(|id| curve_param(e, id).is_some_and(|c| !c.is_empty()))
+ };
+
+ let wheels_on = on(e, "wheels_on");
+ let has_wheels = wheels_on && {
+ let v2 = |id: &str| e.param(id).map(|p| p.vec2_at(cx.t)).unwrap_or_default();
+ let (ws, wm, wh) = (wheel_rgb(v2("wheel_shadows")), wheel_rgb(v2("wheel_midtones")), wheel_rgb(v2("wheel_highlights")));
+ let (ls, lm, lh) = (f(e, "wheel_shadows_l", cx) / 100.0, f(e, "wheel_midtones_l", cx) / 100.0, f(e, "wheel_highlights_l", cx) / 100.0);
+ ws.iter().chain(&wm).chain(&wh).any(|v| v.abs() > 1e-5) || (ls.abs() + lm.abs() + lh.abs() > 1e-5)
+ };
+
+ let hsl_on = b(e, "hsl_on");
+
+ let gpu_capable = !is_hdr && !has_input_lut && !has_look_lut && look == 0 && sharpen.abs() <= 1e-3 && !has_curves && !has_wheels && !hsl_on;
+
+ let bf = |id: &str| if basic_on { f(e, id, cx) } else { 0.0 };
+ let temp = bf("temperature") / 100.0;
+ let tint = bf("tint") / 100.0;
+ let exposure = 2f32.powf(bf("exposure"));
+ let contrast = bf("contrast") / 100.0;
+ let hl = bf("highlights") / 100.0;
+ let sh = bf("shadows") / 100.0;
+ let wh = bf("whites") / 100.0;
+ let bl = bf("blacks") / 100.0;
+ let sat = if basic_on { f(e, "saturation", cx) / 100.0 } else { 1.0 } * if creative_on { f(e, "creative_sat", cx) / 100.0 } else { 1.0 };
+ let vib = if creative_on { f(e, "vibrance", cx) / 100.0 } else { 0.0 };
+ let faded = if creative_on { f(e, "faded_film", cx) / 100.0 } else { 0.0 };
+ let st = color(e, "shadow_tint", cx);
+ let ht = color(e, "highlight_tint", cx);
+ let va = if vignette_on { f(e, "vignette_amount", cx) } else { 0.0 };
+ let vmid = f(e, "vignette_midpoint", cx) / 100.0;
+ let vround = f(e, "vignette_roundness", cx) / 100.0;
+ let vfeather = f(e, "vignette_feather", cx) / 100.0;
+ let gains = [1.0 + 0.35 * temp, 1.0 - 0.3 * tint, 1.0 - 0.35 * temp];
+ let aspect = if h > 0 { (w as f32 / h as f32).max(1e-4) } else { 1.0 };
+
+ FxOp::Lumetri {
+ gains,
+ exposure,
+ contrast,
+ hl,
+ sh,
+ wh,
+ bl,
+ sat,
+ creative_on,
+ faded,
+ vib,
+ st: [st[0], st[1], st[2]],
+ ht: [ht[0], ht[1], ht[2]],
+ vignette_on,
+ va,
+ vmid,
+ vround,
+ vfeather,
+ aspect,
+ gpu_capable,
+ }
+ }
_ => return None,
})
}
@@ -516,6 +613,14 @@ impl FxOp {
FxOp::AlphaAdjust { opacity, .. } => opacity.is_finite(),
FxOp::Vignette { amount, midpoint, roundness, feather, target } => fin(&[*amount, *midpoint, *roundness, *feather]) && fin(target),
FxOp::VideoLimiter { max, comp, axis, warning_color, .. } => *axis <= 3 && max.is_finite() && comp.is_finite() && fin(warning_color),
+ FxOp::Lumetri { gains, exposure, contrast, hl, sh, wh, bl, sat, faded, vib, st, ht, va, vmid, vround, vfeather, aspect, gpu_capable, .. } => {
+ *gpu_capable
+ && fin(gains)
+ && fin(&[*exposure, *contrast, *hl, *sh, *wh, *bl, *sat, *faded, *vib])
+ && fin(st)
+ && fin(ht)
+ && fin(&[*va, *vmid, *vround, *vfeather, *aspect])
+ }
}
}
@@ -805,6 +910,88 @@ impl FxOp {
out.map(enc_to_linear)
});
}
+ FxOp::Lumetri {
+ gains,
+ exposure,
+ contrast,
+ hl,
+ sh,
+ wh,
+ bl,
+ sat,
+ creative_on,
+ faded,
+ vib,
+ st,
+ ht,
+ vignette_on,
+ va,
+ vmid,
+ vround,
+ vfeather,
+ aspect,
+ ..
+ } => {
+ let gains = *gains;
+ let (exposure, contrast, hl, sh, wh, bl, sat) = (*exposure, *contrast, *hl, *sh, *wh, *bl, *sat);
+ let (creative_on, faded, vib, st, ht) = (*creative_on, *faded, *vib, *st, *ht);
+ let (vignette_on, va, vmid, vround, vfeather, aspect) = (*vignette_on, *va, *vmid, *vround, *vfeather, *aspect);
+ let (w, h) = (img.w as f32, img.h as f32);
+ img.map_rgb(|c, x, y| {
+ // white balance + exposure in linear light
+ let lin = [c[0] * gains[0] * exposure, c[1] * gains[1] * exposure, c[2] * gains[2] * exposure];
+ let mut v = enc(lin);
+ // whites / blacks: endpoints
+ let b0 = -bl * 0.15;
+ let w0 = 1.0 - wh * 0.15;
+ v = v.map(|q| (q - b0) / (w0 - b0).max(1e-3));
+ // highlights / shadows: luma-weighted lift/compress, hue preserving
+ let l = 0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2];
+ let ws = (1.0 - l).clamp(0.0, 1.0).powi(3);
+ let whl = l.clamp(0.0, 1.0).powi(3);
+ let nl = (l + sh * 0.35 * ws + hl * 0.35 * whl).max(0.0);
+ if l > 1e-5 {
+ let k = nl / l;
+ v = v.map(|q| q * k);
+ }
+ // contrast: smooth S-curve around mid grey
+ if contrast.abs() > 1e-4 {
+ let k = 1.0 + contrast;
+ v = v.map(|q| {
+ let q = q.clamp(0.0, 1.0);
+ let s = q * q * (3.0 - 2.0 * q);
+ if k >= 1.0 { q + (s - q) * (k - 1.0) } else { 0.5 + (q - 0.5) * k }
+ });
+ }
+ // faded film: lift blacks and compress
+ if faded > 0.0 {
+ v = v.map(|q| q * (1.0 - 0.25 * faded) + 0.12 * faded);
+ }
+ // split tone
+ if creative_on {
+ let l2 = (0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2]).clamp(0.0, 1.0);
+ for k in 0..3 {
+ v[k] += (st[k] - 0.5) * 0.3 * (1.0 - l2) + (ht[k] - 0.5) * 0.3 * l2;
+ }
+ }
+ // saturation & vibrance
+ let l3 = 0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2];
+ let cur_sat = v[0].max(v[1]).max(v[2]) - v[0].min(v[1]).min(v[2]);
+ let s = sat * (1.0 + vib * (1.0 - cur_sat.clamp(0.0, 1.0)));
+ v = v.map(|q| l3 + (q - l3) * s);
+ // vignette
+ if vignette_on && va.abs() > 1e-4 {
+ let nx = (x as f32 / w - 0.5) * 2.0 * (1.0 + vround * 0.0) * if vround < 0.0 { aspect.powf(-vround) } else { 1.0 };
+ let ny = (y as f32 / h - 0.5) * 2.0;
+ let d = (nx * nx + ny * ny).sqrt() / std::f32::consts::SQRT_2;
+ let edge = ((d - vmid * 0.9) / (vfeather.max(0.01) * 0.9)).clamp(0.0, 1.0);
+ let e2 = edge * edge * (3.0 - 2.0 * edge);
+ let k = 1.0 + va * 0.2 * e2;
+ v = v.map(|q| if va < 0.0 { q * k.max(0.0) } else { q + (1.0 - q) * (k - 1.0) });
+ }
+ dec(v)
+ });
+ }
}
}
}