PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Modern Brightness/Contrast: real Photoshop curves (CPU + GPU)
Brandon Thomas4759d12b57f1
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rsindex b6ac92df5..69b963f8b 100644--- a/crates/compose/src/adjust.rs+++ b/crates/compose/src/adjust.rs@@ -73,10 +73,18 @@ pub fn apply_with(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer) {map_rgb(buf, |px| px.map(|v| ((v - 0.5) * k + 0.5 + b).clamp(0.0, 1.0)))}Adjustment::BrightnessContrast { brightness, contrast, .. } => {- // brightness in [-150,150], contrast in [-50,100] (Photoshop UI ranges)- let b = brightness / 255.0;- let k = if *contrast >= 0.0 { 1.0 + contrast / 50.0 } else { 1.0 + contrast / 100.0 };- map_rgb(buf, |c| c.map(|v| ((v + b - 0.5) * k + 0.5).clamp(0.0, 1.0)))+ // Modern (CS3+) Brightness/Contrast, reverse-engineered from Photoshop ground truth+ // (a 0..255 ramp pushed through the real app; see log/devlog.md). Unlike the legacy+ // linear scale, both are smooth curves that pin pure black and white:+ // • Brightness: a line of slope s = 1.375^(b/50) from the origin that rolls off to+ // (1,1) via a `v^P` white-anchor term (P grows as |b| grows).+ // • Contrast: a symmetric cubic-Hermite S-curve pivoting at 0.5, with endpoint slope+ // 1 - c/128 and centre slope 1 + c/128 (near-exact: ≤0.5/255 vs Photoshop).+ // Applied brightness-then-contrast. Exact at the sliders' zero and extremes still drift+ // on the brightness side (the real curve is a spline); contrast matches closely.+ let b = *brightness;+ let c = *contrast;+ map_rgb(buf, |px| px.map(|v| modern_contrast(modern_brightness(v, b), c).clamp(0.0, 1.0)))}Adjustment::Exposure { exposure, offset, gamma } => {// In linear light: (lin·2^exposure + offset)^(1/gamma), then back@@ -320,6 +328,39 @@ fn lut(table: &[f32], v: f32) -> f32 {/// Photoshop posterize: `n` equal input bins over 0..=255, output levels/// `floor(k * 255 / (n - 1))` (exact on the corpus for 3, 7, 13 and 21 levels).+/// Modern Brightness curve (one channel, `brightness` in [-150, 150]). A line of slope+/// `s = 1.375^(b/50)` from the origin, rolled off to (1, 1) by a `v^P` white-anchor term. Pins pure+/// black and white; `b = 0` is the identity. Fit to Photoshop ground truth (see modern B/C above).+pub fn modern_brightness(v: f32, brightness: f32) -> f32 {+ if brightness == 0.0 {+ return v;+ }+ let s = 1.375f32.powf(brightness / 50.0);+ // Exponent of the white-anchor term grows with |b|; shapes the roll-off toward (1,1).+ let p = if brightness >= 0.0 {+ (4.5 - 0.013 * brightness).max(2.0)+ } else {+ 5.0 - 0.072 * brightness+ };+ let v = v.clamp(0.0, 1.0);+ (s * v + (1.0 - s) * v.powf(p)).clamp(0.0, 1.0)+}++/// Modern Contrast curve (one channel, `contrast` in [-50, 100]). A symmetric cubic-Hermite S-curve+/// pivoting at 0.5: endpoint slope `1 - c/128`, centre slope `1 + c/128`. Near-exact vs Photoshop+/// (≤0.5/255). `c = 0` is the identity; pins 0, 0.5 and 1.+pub fn modern_contrast(v: f32, contrast: f32) -> f32 {+ if contrast == 0.0 {+ return v;+ }+ let k = contrast / 128.0;+ let (end, mid) = (1.0 - k, 1.0 + k);+ // Cubic Hermite on [0, 0.5]: pinned (0,0) slope `end`, (0.5,0.5) slope `mid`. `t` in [0,1].+ let half = |t: f32| end * 0.5 * (t * t * t - 2.0 * t * t + t) + (-2.0 * t * t * t + 3.0 * t * t) * 0.5 + mid * 0.5 * (t * t * t - t * t);+ let v = v.clamp(0.0, 1.0);+ if v <= 0.5 { half(v / 0.5) } else { 1.0 - half((1.0 - v) / 0.5) }+}+pub fn posterize(v: f32, levels: u32) -> f32 {let n = levels.clamp(2, 255) as f32;let x = (v.clamp(0.0, 1.0) * 255.0).round();diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex e1b535e79..f95cc6090 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -804,3 +804,47 @@ fn adjustment_results_are_rounded_to_the_document_depth() {}}}++// Ground truth captured from Adobe Photoshop 2026 (27.10.0): a 0..255 ramp pushed through modern+// (non-legacy) Brightness/Contrast via `executeAction("BrgC", ... useLegacy=false)`. These pin our+// reverse-engineered curves (see crates/compose/src/adjust.rs modern_* and log/devlog.md).+#[test]+fn modern_contrast_matches_photoshop() {+ // (contrast, [out at x = 0,16,32,64,96,128,160,192,224,255])+ let cases: [(f32, [u8; 10]); 4] = [+ (50.0, [0, 11, 23, 52, 87, 128, 169, 204, 233, 255]),+ (-50.0, [0, 21, 41, 76, 105, 128, 151, 180, 215, 255]),+ (100.0, [0, 5, 14, 40, 78, 128, 178, 216, 242, 255]),+ (-25.0, [0, 19, 37, 70, 101, 128, 155, 186, 220, 255]),+ ];+ let xs = [0usize, 16, 32, 64, 96, 128, 160, 192, 224, 255];+ for (c, out) in cases {+ for (i, &x) in xs.iter().enumerate() {+ let got = adjust::modern_contrast(x as f32 / 255.0, c) * 255.0;+ let err = (got - out[i] as f32).abs();+ assert!(err <= 2.0, "contrast {c} x={x}: got {got:.1} want {} (err {err:.1})", out[i]);+ }+ }+}++#[test]+fn modern_brightness_matches_photoshop() {+ let cases: [(f32, [u8; 10]); 2] = [+ (50.0, [0, 22, 44, 88, 132, 171, 203, 228, 246, 255]),+ (-50.0, [0, 12, 23, 47, 70, 93, 118, 148, 186, 255]),+ ];+ let xs = [0usize, 16, 32, 64, 96, 128, 160, 192, 224, 255];+ for (b, out) in cases {+ for (i, &x) in xs.iter().enumerate() {+ let got = adjust::modern_brightness(x as f32 / 255.0, b) * 255.0;+ let err = (got - out[i] as f32).abs();+ // The brightness roll-off is a spline; our fit is close but approximate at the top.+ assert!(err <= 6.0, "brightness {b} x={x}: got {got:.1} want {} (err {err:.1})", out[i]);+ }+ }+ // Endpoints and the zero slider are exact.+ assert_eq!(adjust::modern_brightness(0.0, 120.0), 0.0);+ assert_eq!(adjust::modern_brightness(1.0, -120.0), 1.0);+ assert_eq!(adjust::modern_brightness(0.37, 0.0), 0.37);+ assert_eq!(adjust::modern_contrast(0.37, 0.0), 0.37);+}diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgslindex d7691c435..92252fc48 100644--- a/crates/gpu/src/compose.wgsl+++ b/crates/gpu/src/compose.wgsl@@ -431,6 +431,31 @@ fn selective_color(c: vec3<f32>, relative: bool) -> vec3<f32> {return clamp(c - delta, vec3(0.0), vec3(1.0));}+// Modern Brightness curve: line of slope 1.375^(b/50) rolled off to (1,1) by a v^P white anchor.+// Mirrors compose::adjust::modern_brightness.+fn mbright(v: f32, b: f32) -> f32 {+ if (b == 0.0) { return v; }+ let s = pow(1.375, b / 50.0);+ var p: f32;+ if (b >= 0.0) { p = max(4.5 - 0.013 * b, 2.0); } else { p = 5.0 - 0.072 * b; }+ let vv = clamp(v, 0.0, 1.0);+ return clamp(s * vv + (1.0 - s) * pow(vv, p), 0.0, 1.0);+}++// Modern Contrast curve: symmetric cubic-Hermite S pivoting at 0.5.+// Mirrors compose::adjust::modern_contrast.+fn mcontrast(v: f32, ct: f32) -> f32 {+ if (ct == 0.0) { return v; }+ let k = ct / 128.0;+ let e = 1.0 - k; let m = 1.0 + k;+ let vv = clamp(v, 0.0, 1.0);+ var t: f32;+ if (vv <= 0.5) { t = vv / 0.5; } else { t = (1.0 - vv) / 0.5; }+ let h = e * 0.5 * (t * t * t - 2.0 * t * t + t) + (-2.0 * t * t * t + 3.0 * t * t) * 0.5 + m * 0.5 * (t * t * t - t * t);+ if (vv <= 0.5) { return h; }+ return 1.0 - h;+}+fn adjust(c: vec3<f32>) -> vec3<f32> {let p0 = op.p0;let p1 = op.p1;@@ -443,7 +468,10 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {}case 3: { return vec3(posterize(c.r, p0.x), posterize(c.g, p0.x), posterize(c.b, p0.x)); }case 4: { return clamp((c - 0.5) * p0.y + 0.5 + p0.x, vec3(0.0), vec3(1.0)); } // B/C legacy- case 5: { return clamp((c + p0.x - 0.5) * p0.y + 0.5, vec3(0.0), vec3(1.0)); } // B/C+ case 5: { // B/C modern+ let b = p0.x; let ct = p0.y;+ return vec3(mcontrast(mbright(c.r, b), ct), mcontrast(mbright(c.g, b), ct), mcontrast(mbright(c.b, b), ct));+ }case 6: { // Exposurevar o: vec3<f32>;for (var i = 0; i < 3; i++) {diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rsindex d5bae0746..4ba87d25a 100644--- a/crates/gpu/src/plan.rs+++ b/crates/gpu/src/plan.rs@@ -1152,8 +1152,9 @@ pub fn adjustment_program(adj: &Adjustment, transfer: Transfer) -> Program {(4, p, None)}Adjustment::BrightnessContrast { brightness, contrast, .. } => {- let k = if *contrast >= 0.0 { 1.0 + contrast / 50.0 } else { 1.0 + contrast / 100.0 };- p[0] = [brightness / 255.0, k, 0.0, 0.0];+ // Modern B/C: the shader rebuilds the curves from raw slider values (see compose.wgsl+ // `mbright`/`mcontrast`, mirroring compose::adjust::modern_brightness/modern_contrast).+ p[0] = [*brightness, *contrast, 0.0, 0.0];(5, p, None)}Adjustment::Exposure { exposure, offset, gamma } => {