M13.9: gpu colour corrections (fx_tone)

Levels (Individual Controls), Gamma/Pedestal/Gain, Photo Filter, Change Color,
Change to Color, Leave Color, Broadcast Colors, Color Balance (HLS), Video
Limiter, PS Arbitrary Map, CC Toner, CC Color Offset, CC Kernel, Auto Levels /
Contrast / Color and Equalize (histograms measured on the CPU, applied on the
GPU) on the GPU; Hue/Saturation colour ranges and Levels' channel controls no
longer fall back to the CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 08:13:58 -04:00
co-authored by Claude Opus 5.5
parent 8ba936f87b
commit 7a96fab302
8 changed files with 1083 additions and 31 deletions
+46 -29
View File
@@ -330,10 +330,21 @@ fn stretch(v: f32, lo: f32, hi: f32) -> f32 {
/// 0 = Auto Levels (per channel), 1 = Auto Contrast (shared), 2 = Auto Color (per channel + neutral mids).
fn auto_correct(ctx: &EffectCtx, mut b: Buf, kind: u32) -> Buf {
let blend = (ctx.params.f("blend") / 100.0).clamp(0.0, 1.0) as f32;
let (ranges, gam) = auto_correct_settings(ctx, &b.img, kind);
map_ca(&mut b.img, |c, a| {
let o = [0, 1, 2].map(|k| stretch(c[k], ranges[k].0, ranges[k].1).powf(gam[k]));
(lerp3(o, c, blend), a)
});
b
}
/// What Auto Levels (`kind` 0), Auto Contrast (1) and Auto Color (2) measure on `img`: each
/// channel's input range (low, high) and gamma. (The GPU effects apply them.)
pub fn auto_correct_settings(ctx: &EffectCtx, img: &Image, kind: u32) -> ([(f32, f32); 3], [f32; 3]) {
let black = ctx.params.f("blackClip").clamp(0.0, 49.0) / 100.0;
let white = ctx.params.f("whiteClip").clamp(0.0, 49.0) / 100.0;
let blend = (ctx.params.f("blend") / 100.0).clamp(0.0, 1.0) as f32;
let h = smoothed_histograms(ctx, &b.img);
let h = smoothed_histograms(ctx, img);
let ranges: [(f32, f32); 3] = if kind == 1 {
let mut combined = vec![0.0; BINS];
for k in 0..3 {
@@ -350,7 +361,7 @@ fn auto_correct(ctx: &EffectCtx, mut b: Buf, kind: u32) -> Buf {
let mut gam = [1.0f32; 3];
if kind == 2 && ctx.params.b("snapNeutralMidtones") {
let mut sums = [0.0f64; 4];
for px in &b.img.data {
for px in &img.data {
let (c, a) = unpremul(*px);
if a > 1e-4 {
for k in 0..3 {
@@ -369,11 +380,7 @@ fn auto_correct(ctx: &EffectCtx, mut b: Buf, kind: u32) -> Buf {
}
}
}
map_ca(&mut b.img, |c, a| {
let o = [0, 1, 2].map(|k| stretch(c[k], ranges[k].0, ranges[k].1).powf(gam[k]));
(lerp3(o, c, blend), a)
});
b
(ranges, gam)
}
fn auto_levels(ctx: &EffectCtx, b: Buf) -> Buf {
@@ -397,24 +404,12 @@ fn cdf(h: &[f64]) -> Vec<f32> {
.collect()
}
fn equalize(ctx: &EffectCtx, mut b: Buf) -> Buf {
let amt = (ctx.params.f("amount") / 100.0).clamp(0.0, 1.0) as f32;
if amt <= 0.0 {
return b;
}
let style = ctx.params.e("style");
let h = histograms(&b.img);
let look = |c: &Vec<f32>, v: f32| c[((v.clamp(0.0, 1.0) * (BINS - 1) as f32).round() as usize).min(BINS - 1)];
/// Equalize's lookup tables (`BINS` entries each) for `img`: the R, G and B cumulative
/// histograms (Style RGB), the luminance one (Brightness) or the combined one (Photoshop Style).
pub fn equalize_tables(img: &Image, style: u32) -> Vec<Vec<f32>> {
let h = histograms(img);
match style {
1 => {
let c = cdf(&h[3]);
map_ca(&mut b.img, |col, a| {
let l = luminance(col[0], col[1], col[2]);
let nl = look(&c, l);
let o = if l > 1e-5 { col.map(|v| v * nl / l) } else { [nl; 3] };
(lerp3(col, o, amt), a)
});
}
1 => vec![cdf(&h[3])],
2 => {
let mut combined = vec![0.0; BINS];
for k in 0..3 {
@@ -422,12 +417,34 @@ fn equalize(ctx: &EffectCtx, mut b: Buf) -> Buf {
combined[i] += h[k][i];
}
}
let c = cdf(&combined);
map_ca(&mut b.img, |col, a| (lerp3(col, col.map(|v| look(&c, v)), amt), a));
vec![cdf(&combined)]
}
_ => vec![cdf(&h[0]), cdf(&h[1]), cdf(&h[2])],
}
}
fn equalize(ctx: &EffectCtx, mut b: Buf) -> Buf {
let amt = (ctx.params.f("amount") / 100.0).clamp(0.0, 1.0) as f32;
if amt <= 0.0 {
return b;
}
let style = ctx.params.e("style");
let t = equalize_tables(&b.img, style);
let look = |c: &Vec<f32>, v: f32| c[((v.clamp(0.0, 1.0) * (BINS - 1) as f32).round() as usize).min(BINS - 1)];
match style {
1 => {
map_ca(&mut b.img, |col, a| {
let l = luminance(col[0], col[1], col[2]);
let nl = look(&t[0], l);
let o = if l > 1e-5 { col.map(|v| v * nl / l) } else { [nl; 3] };
(lerp3(col, o, amt), a)
});
}
2 => {
map_ca(&mut b.img, |col, a| (lerp3(col, col.map(|v| look(&t[0], v)), amt), a));
}
_ => {
let cs = [cdf(&h[0]), cdf(&h[1]), cdf(&h[2])];
map_ca(&mut b.img, |col, a| (lerp3(col, [0, 1, 2].map(|k| look(&cs[k], col[k])), amt), a));
map_ca(&mut b.img, |col, a| (lerp3(col, [0, 1, 2].map(|k| look(&t[k], col[k])), amt), a));
}
}
b
+21 -2
View File
@@ -67,9 +67,10 @@ use std::sync::OnceLock;
pub use card3d::{CompLight, CompScene};
pub use color_fx::{
HUESAT_CHANNELS, LEVELS_CHANNELS, exposure_settings, fill_uses_masks, huesat_ranges_identity, levels_channel_ids, levels_channels_identity, levels_clip,
HUESAT_CHANNELS, HUESAT_RANGES, LEVELS_CHANNELS, PHOTO_FILTER_CUSTOM, PHOTO_FILTERS, exposure_settings, fill_uses_masks, huesat_ranges_identity,
levels_channel_ids, levels_channel_settings, levels_channels_identity, levels_clip,
};
pub use color2::Curve;
pub use color2::{Curve, auto_correct_settings, equalize_tables};
pub use distort::transform_shutter;
pub use distort2::parse_mesh;
use effectcraft_keyframe::Value;
@@ -831,6 +832,24 @@ pub const GPU_EFFECTS: &[&str] = &[
"ec.channel.calculations",
"ec.channel.compoundarithmetic",
//
// effectcraft-gpu fx_tone (colour correction)
"ec.color.levelsic",
"ec.color.gammapedestalgain",
"ec.color.photofilter",
"ec.color.changecolor",
"ec.color.changetocolor",
"ec.color.leavecolor",
"ec.color.broadcast",
"ec.color.colorbalancehls",
"ec.color.videolimiter",
"ec.color.psarbitrarymap",
"ec.color.cctoner",
"ec.color.cccoloroffset",
"ec.color.cckernel",
"ec.color.autolevels",
"ec.color.autocontrast",
"ec.color.autocolor",
"ec.color.equalize",
// effectcraft-gpu fx_stylize (stylize, distort)
//
// effectcraft-gpu fx_noise (noise, blur, time)