diff --git a/crates/color/src/blend.rs b/crates/color/src/blend.rs index 72e6d681db..8c6a6b48e1 100644 --- a/crates/color/src/blend.rs +++ b/crates/color/src/blend.rs @@ -288,7 +288,9 @@ fn clip_color(c: [f32; 3]) -> [f32; 3] { out } -fn set_lum(c: [f32; 3], l: f32) -> [f32; 3] { +/// `c` with its luminosity ([`lum`]) moved to `l`, clipped back into gamut towards grey (the +/// Luminosity blend mode's SetLum + ClipColor). +pub fn set_lum(c: [f32; 3], l: f32) -> [f32; 3] { let d = l - lum(c); clip_color([c[0] + d, c[1] + d, c[2] + d]) } diff --git a/crates/color/src/convert.rs b/crates/color/src/convert.rs index 77fb1a62a2..ed7cb60e8e 100644 --- a/crates/color/src/convert.rs +++ b/crates/color/src/convert.rs @@ -173,7 +173,20 @@ pub fn rgb_to_cmyk_naive(rgb: [f32; 3]) -> [f32; 4] { [(1.0 - rgb[0] - k) / d, (1.0 - rgb[1] - k) / d, (1.0 - rgb[2] - k) / d, k] } -const D50: [f32; 3] = [0.964_22, 1.0, 0.825_21]; +/// The D50 white point (ICC PCS). +pub const D50: [f32; 3] = [0.964_22, 1.0, 0.825_21]; + +/// Linear sRGB → D50 XYZ (Bradford-adapted, as in the ICC sRGB profile). Rows sum to [`D50`]. +pub const SRGB_TO_XYZ_D50: [[f32; 3]; 3] = [[0.436_074, 0.385_064, 0.143_080], [0.222_504, 0.716_878, 0.060_618], [0.013_932, 0.097_104, 0.714_173]]; + +/// D50 XYZ → linear sRGB (the inverse of [`SRGB_TO_XYZ_D50`]). +pub const XYZ_D50_TO_SRGB: [[f32; 3]; 3] = [[3.133_856, -1.616_867, -0.490_615], [-0.978_768, 1.916_142, 0.033_454], [0.071_945, -0.228_991, 1.405_243]]; + +/// `m · v` for a row-major 3 × 3 matrix. +#[inline] +pub fn mat3_mul(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] { + m.map(|r| r[0] * v[0] + r[1] * v[1] + r[2] * v[2]) +} /// CIE L*a*b* (D50) → encoded sRGB (Bradford-adapted to D65). pub fn lab_to_srgb(lab: [f32; 3]) -> [f32; 3] { @@ -183,26 +196,15 @@ pub fn lab_to_srgb(lab: [f32; 3]) -> [f32; 3] { let fz = fy - b / 200.0; let finv = |t: f32| if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0f32 / 29.0).powi(2) * (t - 4.0 / 29.0) }; let xyz = [finv(fx) * D50[0], finv(fy) * D50[1], finv(fz) * D50[2]]; - // D50 XYZ → linear sRGB (Bradford-adapted matrix) - let m = [[3.133_856, -1.616_867, -0.490_615], [-0.978_768, 1.916_142, 0.033_454], [0.071_945, -0.228_991, 1.405_243]]; - let mut out = [0.0f32; 3]; - for i in 0..3 { - let lin = m[i][0] * xyz[0] + m[i][1] * xyz[1] + m[i][2] * xyz[2]; - out[i] = linear_to_srgb(lin.clamp(0.0, 1.0)); - } - out + mat3_mul(&XYZ_D50_TO_SRGB, xyz).map(|lin| linear_to_srgb(lin.clamp(0.0, 1.0))) } /// Encoded sRGB → CIE L*a*b* (D50). pub fn srgb_to_lab(rgb: [f32; 3]) -> [f32; 3] { - let lin = [srgb_to_linear(rgb[0]), srgb_to_linear(rgb[1]), srgb_to_linear(rgb[2])]; - let m = [[0.436_074, 0.385_064, 0.143_080], [0.222_504, 0.716_878, 0.060_618], [0.013_932, 0.097_104, 0.714_173]]; - let mut xyz = [0.0f32; 3]; - for i in 0..3 { - xyz[i] = (m[i][0] * lin[0] + m[i][1] * lin[1] + m[i][2] * lin[2]) / D50[i]; - } + let lin = rgb.map(srgb_to_linear); + let xyz = mat3_mul(&SRGB_TO_XYZ_D50, lin); let f = |t: f32| if t > (6.0f32 / 29.0).powi(3) { t.cbrt() } else { t / (3.0 * (6.0f32 / 29.0).powi(2)) + 4.0 / 29.0 }; - let (fx, fy, fz) = (f(xyz[0]), f(xyz[1]), f(xyz[2])); + let (fx, fy, fz) = (f(xyz[0] / D50[0]), f(xyz[1] / D50[1]), f(xyz[2] / D50[2])); [116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)] } diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs index bacdb88321..a794445955 100644 --- a/crates/compose/src/adjust.rs +++ b/crates/compose/src/adjust.rs @@ -4,7 +4,7 @@ //! milestone M7 against Photoshop-rendered PSD composites (the oracle in `testkit`). use photocraft_color::SampleType; -use photocraft_color::convert::rgb_to_gray; +use photocraft_color::convert::{D50, SRGB_TO_XYZ_D50, XYZ_D50_TO_SRGB, mat3_mul, rgb_to_gray, srgb_to_linear}; use photocraft_doc::Adjustment; use photocraft_doc::adjust::{CurvePoint, HueRange, LevelsChannel, ToneSpace}; @@ -154,15 +154,16 @@ pub fn apply_depth(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, depth let mix = |row: &[f32; 4]| (row[0] * c[0] + row[1] * c[1] + row[2] * c[2] + row[3]).clamp(0.0, 1.0); if *monochrome { [mix(&matrix[0]); 3] } else { [mix(&matrix[0]), mix(&matrix[1]), mix(&matrix[2])] } }), - Adjustment::PhotoFilter { color, density, preserve_luminosity } => map_rgb(buf, |c| { - let filtered: [f32; 3] = std::array::from_fn(|i| c[i] * (1.0 - density) + c[i] * color[i] * density); - if *preserve_luminosity { - let (l0, l1) = (rgb_to_gray(c), rgb_to_gray(filtered).max(1e-6)); - filtered.map(|v| (v * l0 / l1).clamp(0.0, 1.0)) - } else { - filtered - } - }), + Adjustment::PhotoFilter { color, density, preserve_luminosity } => { + let linear_doc = depth == Some(SampleType::F32); + let m = photo_filter_matrix(*color, *density, *preserve_luminosity && linear_doc); + let set_lum = *preserve_luminosity && !linear_doc; + map_rgb(buf, |c| { + let f = mat3_mul(&m, c.map(|v| transfer.decode(v))).map(|v| transfer.encode(v)); + let f = if set_lum { photocraft_color::blend::set_lum(f, photocraft_color::blend::lum(c)) } else { f }; + f.map(|v| v.clamp(0.0, 1.0)) + }) + } Adjustment::BlackWhite { weights, tint } => map_rgb(buf, |c| { let g = black_white_gray(c, weights); match tint { @@ -461,6 +462,23 @@ fn lut(table: &[f32], v: f32) -> f32 { table[i] * (1.0 - f) + table[j] * f } +/// Photo Filter's linear-light RGB matrix. Photoshop multiplies the pixel's D50 XYZ, relative to +/// white, by the filter colour's (mixed in by `density`): a fixed linear map, so it is folded into +/// one matrix. With `normalize_y` the map keeps luminance Y (32-bit Preserve Luminosity; 8/16-bit +/// documents instead restore the encoded luminosity like the Luminosity blend mode). Fitted on the +/// photoshop corpus `photo-filter.psd`: rgb16 within 0.6/255 everywhere. +pub fn photo_filter_matrix(color: [f32; 3], density: f32, normalize_y: bool) -> [[f32; 3]; 3] { + let d = if density.is_finite() { density.clamp(0.0, 1.0) } else { 0.0 }; + let xyz = mat3_mul(&SRGB_TO_XYZ_D50, color.map(|v| srgb_to_linear(v.clamp(0.0, 1.0)))); + let mut s: [f32; 3] = std::array::from_fn(|i| 1.0 - d + d * xyz[i] / D50[i]); + if normalize_y && s[1] > 1e-6 { + let y = s[1]; + s = s.map(|v| v / y); + } + // XYZ_D50_TO_SRGB · diag(s) · SRGB_TO_XYZ_D50 + std::array::from_fn(|r| std::array::from_fn(|c| (0..3).map(|k| XYZ_D50_TO_SRGB[r][k] * s[k] * SRGB_TO_XYZ_D50[k][c]).sum())) +} + /// 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 diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs index 33e83bb520..2738518932 100644 --- a/crates/compose/src/effects.rs +++ b/crates/compose/src/effects.rs @@ -684,6 +684,28 @@ fn paint_fx( } } +/// The opacity gain of a gradient glow: it is opaque from strength `range²` on (see [`paint_glow`]). +pub fn glow_gradient_gain(range: f32) -> f32 { + let r = if range.is_finite() { range.clamp(0.01, 1.0) } else { 1.0 }; + 1.0 / (r * r) +} + +/// A glow's paint over its map `m`. A gradient runs along the glow rather than across the +/// canvas: Photoshop colours the glow where its (ranged, contoured) strength is `v` with the +/// gradient at `1 - v`, so the first stop hugs the edge, and the glow is opaque from `v = range²` +/// on (photoshop corpus outer-glow-gradient.psd: 1.4/255 mean, against bands across the canvas; +/// psd-tools layer_params.psd). Glows have no gradient angle, style or Reverse. +#[allow(clippy::too_many_arguments)] +fn paint_glow(dst: &mut Buffer, m: &Map, g: &Glow, shape_bounds: Rect, anchor: (f64, f64), big: Rect, patterns: &PreparedPatterns<'_>) { + let FxPaint::Gradient(gradient) = &g.paint else { + return paint_fx(dst, m, &g.paint, shape_bounds, anchor, big, g.common.blend, g.common.opacity, patterns); + }; + let prepared = PreparedGradient::new(gradient); + let gain = glow_gradient_gain(g.range); + let strength = Map { w: m.w, h: m.h, v: m.v.iter().map(|v| (v * gain).min(1.0)).collect() }; + paint(dst, &strength, |i| prepared.sample(1.0 - m.v.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0)), g.common.blend, g.common.opacity); +} + /// Normalised weights of a centred box of (fractional) width `w`: tap `i` gets the overlap of /// `[i - 0.5, i + 0.5]` with `[-w/2, w/2]`. fn box_weights(w: f32) -> Vec { @@ -1228,8 +1250,7 @@ pub(crate) fn composite_with_effects_prepared( } for (i, e) in rev() { if let Effect::OuterGlow(g) = e { - let m = fx(i, 0); - paint_fx(&mut work, &m, &g.paint, sb, anchor, big, g.common.blend, g.common.opacity, patterns); + paint_glow(&mut work, &fx(i, 0), g, sb, anchor, big, patterns); } } @@ -1279,8 +1300,7 @@ pub(crate) fn composite_with_effects_prepared( } for (i, e) in rev() { if let Effect::InnerGlow(g) = e { - let m = rel(fx(i, 0)); - paint_fx(&mut lay, &m, &g.paint, sb, anchor, big, g.common.blend, g.common.opacity, patterns); + paint_glow(&mut lay, &rel(fx(i, 0)), g, sb, anchor, big, patterns); } } for (i, e) in rev() { diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 132181f917..3e2bc1230b 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -547,6 +547,27 @@ fn outer_and_inner_glow_regions() { assert!(close4(px(&d, 5, 20), [1.0; 4])); } +// A glow gradient runs along the glow, not across the canvas: the first stop hugs the shape on +// every side and later stops lie further out, opaque until the glow fades (#443). +#[test] +fn glow_gradient_follows_the_shape() { + let red_to_blue = Gradient { stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], ..Gradient::default() }; + let g = Glow { paint: FxPaint::Gradient(red_to_blue), technique: GlowTechnique::Softer, size: 8.0, ..glow(GlowTechnique::Softer, GlowSource::Edge) }; + let d = fx_doc(vec![Effect::OuterGlow(g.clone())]); + let near = px(&d, 9, 20); + for p in [px(&d, 30, 20), px(&d, 20, 9), px(&d, 20, 30)] { + assert!((0..4).all(|c| (p[c] - near[c]).abs() < 1.0 / 255.0), "{p:?} vs {near:?}"); + } + assert!(near[0] > 0.8 && near[2] < 0.2 && near[1] < 0.1, "red at the edge: {near:?}"); + let out = px(&d, 5, 20); + assert!(out[2] > out[0] && out[1] < 0.9, "bluer and fading further out: {out:?}"); + // An inner glow's gradient also starts at the edge. + let d = fx_doc(vec![Effect::InnerGlow(g)]); + let (edge, top) = (px(&d, 10, 20), px(&d, 20, 10)); + assert!((0..4).all(|c| (edge[c] - top[c]).abs() < 1.0 / 255.0), "{edge:?} vs {top:?}"); + assert!(edge[0] > 0.8 && edge[2] < 0.2, "{edge:?}"); +} + #[test] fn gradient_overlay_follows_angle_and_reverse() { let g = Gradient { stops: vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], angle: 0.0, ..Gradient::default() }; @@ -1429,3 +1450,40 @@ fn effect_maps_with_nested_rayon_work_do_not_deadlock() { Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => panic!("render thread panicked"), } } + +// Photo Filter multiplies D50 XYZ (relative to white) by the filter colour's, then restores the +// encoded luminosity like the Luminosity blend mode; 32-bit documents keep luminance Y instead. +// Samples from the photoshop corpus photo-filter.psd (Warming Filter (85), density 40 %, Preserve +// Luminosity): pure blue gains red, and white stays white (#442). +#[test] +fn photo_filter_matches_photoshop() { + let filter = |color: [f32; 3], depth, v: [f32; 3]| { + let adj = Adjustment::PhotoFilter { color, density: 0.4, preserve_luminosity: true }; + let mut b = Buffer::filled(Rect::new(0, 0, 1, 1), [v[0], v[1], v[2], 1.0]); + adjust::apply_depth(&adj, &mut b, adjust::Transfer::for_document(ColorMode::Rgb, depth), Some(depth)); + [b.px[0][0], b.px[0][1], b.px[0][2]].map(|c| c * 255.0) + }; + let warming = [236.0 / 255.0, 138.0 / 255.0, 0.0]; + for depth in [SampleType::U8, SampleType::U16] { + for (v, ps) in [ + ([1.0, 0.0, 0.0], [239.0, 7.0, 7.0]), + ([0.0, 1.0, 0.0], [69.0, 220.0, 0.0]), + ([0.0, 0.0, 1.0], [46.0, 0.0, 130.0]), + ([1.0, 1.0, 0.0], [255.0, 240.0, 80.0]), + ([0.5019, 0.5019, 0.5019], [140.0, 124.0, 116.0]), + ([0.949, 0.949, 0.949], [255.0, 238.0, 229.0]), + ([1.0, 1.0, 1.0], [255.0, 255.0, 255.0]), + ([0.0, 0.0, 0.0], [0.0, 0.0, 0.0]), + ] { + let got = filter(warming, depth, v); + assert!(got.iter().zip(ps).all(|(g, p)| (g - p).abs() <= 1.0), "{depth:?} {v:?}: got {got:?} want {ps:?}"); + } + } + // The 32-bit file picks the same numbers as linear values (Lab 81.5, 9.41, 82.19). Its fit is + // looser (mean 0.9/255 over the file, 6.9/255 with a plain RGB multiply). + let warming32 = [0.966_445_6, 0.761_806_3, 0.0]; + for (v, ps) in [([0.2159, 0.2159, 0.2159], [66.0, 53.0, 40.0]), ([1.0, 1.0, 0.0], [255.0, 251.0, 0.0]), ([1.0, 0.0, 0.0], [255.0, 0.0, 0.0])] { + let got = filter(warming32, SampleType::F32, v); + assert!(got.iter().zip(ps).all(|(g, p)| (g - p).abs() <= 2.5), "F32 {v:?}: got {got:?} want {ps:?}"); + } +} diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 76ce8a4505..506f5f7256 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -533,13 +533,12 @@ fn adjust(c: vec3) -> vec3 { return vec3(r, clamp(dot(p1.xyz, c) + p1.w, 0.0, 1.0), clamp(dot(p2.xyz, c) + p2.w, 0.0, 1.0)); } case 11: { // Photo filter - let f = c * (1.0 - p0.w) + c * p0.rgb * p0.w; - if (p1.x > 0.5) { - let l0 = gray(c); - let l1 = max(gray(f), 1e-6); - return clamp(f * l0 / l1, vec3(0.0), vec3(1.0)); - } - return f; + // compose::adjust::photo_filter_matrix (rows p0..p2) in linear light, then SetLum. + let g = p3.y; + let lin = vec3(t_decode(c.r, g), t_decode(c.g, g), t_decode(c.b, g)); + var f = vec3(t_encode(dot(p0.xyz, lin), g), t_encode(dot(p1.xyz, lin), g), t_encode(dot(p2.xyz, lin), g)); + if (p3.x > 0.5) { f = set_lum(f, lum(c)); } + return clamp(f, vec3(0.0), vec3(1.0)); } case 12: { // Black & White // compose::adjust::black_white_gray: grey + secondary + primary parts, weighted. @@ -793,7 +792,7 @@ fn pattern_sample(d: vec2) -> vec4 { } // Effect paint colour at `d`: p2.z = 0 solid (`color`), 1 gradient (stops in `lut_tex`), -// 2 pattern. +// 2 pattern (4, a glow's gradient, is resolved in `fs_fxpaint`). fn fx_color(d: vec2) -> vec4 { let kind = i32(op.p2.z); if (kind == 1) { @@ -868,6 +867,13 @@ fn fs_fxpaint(in: VOut) -> @location(0) vec4 { if ((op.flags & F_STROKE_OUT) != 0u && inside_shape) { m = select(1.0, 0.0, (op.flags & F_VECTOR) != 0u); } + if (i32(op.p2.z) == 4) { + // effects::paint_glow: the gradient at 1 - strength, opaque from strength 1 / gain (p0.x). + let k = min(m * op.p0.x, 1.0) * op.opacity; + if (k <= 0.0) { return dst; } + let t = 1.0 - clamp(m, 0.0, 1.0); + return composite(op.mode, dst, vec4(lut(0, t), lut(1, t), lut(2, t), lut(3, t) * k), 1.0); + } let k = m * op.opacity; if (k <= 0.0) { return dst; } let c = fx_color(d); diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index b087f8c6a3..9ae880dc6f 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -16,7 +16,7 @@ use photocraft_color::BlendMode; use photocraft_compose::adjust::{self, Transfer}; use photocraft_compose::effects::has_effects; use photocraft_doc::adjust::ToneSpace; -use photocraft_doc::{Adjustment, Document, Effect, Fill, FxPaint, GlobalLight, Gradient, Layer, LayerContent, LayerId, Pattern, StrokePosition}; +use photocraft_doc::{Adjustment, Document, Effect, Fill, FxPaint, GlobalLight, Glow, Gradient, Layer, LayerContent, LayerId, Pattern, StrokePosition}; use photocraft_geom::Rect; use photocraft_raster::Surface; @@ -999,7 +999,7 @@ impl<'a> Planner<'a> { } for &(i, e) in &rev { if let Effect::OuterGlow(g) = e { - let paint = self.fx_paint(&g.paint, anchor); + let paint = self.glow_paint(g, anchor); w = self.paint(w, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, 0, clip, sb); } } @@ -1037,7 +1037,7 @@ impl<'a> Planner<'a> { } for &(i, e) in &rev { if let Effect::InnerGlow(g) = e { - let paint = self.fx_paint(&g.paint, anchor); + let paint = self.glow_paint(g, anchor); l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_REL, clip, sb); } } @@ -1235,6 +1235,13 @@ impl<'a> Planner<'a> { } } + fn glow_paint(&self, g: &'a Glow, anchor: (f64, f64)) -> Paint<'a> { + match &g.paint { + FxPaint::Gradient(gradient) => Paint::GlowGradient(gradient, photocraft_compose::effects::glow_gradient_gain(g.range)), + p => self.fx_paint(p, anchor), + } + } + /// An effect pass reading `dst` (A, retained) and the layer `content` (B, retained) with its /// paint set up. #[allow(clippy::too_many_arguments)] @@ -1273,14 +1280,13 @@ impl<'a> Planner<'a> { p.params[2][0] = offset.0; p.params[2][1] = offset.1; p.params[2][2] = 1.0; - let mut rows = vec![[0.0f32; 4096]; 4]; - for k in 0..4096 { - let v = photocraft_compose::effects::sample_gradient(g, k as f32 / 4095.0); - for (ch, row) in rows.iter_mut().enumerate() { - row[k] = v[ch]; - } - } - p.lut = Some(rows); + p.lut = Some(gradient_rows(g)); + } + Paint::GlowGradient(g, gain) => { + // effects::paint_glow: the gradient at 1 - strength, opaque from 1 / gain. + p.params[0][0] = *gain; + p.params[2][2] = 4.0; + p.lut = Some(gradient_rows(g)); } Paint::Pattern(pat, pl) => { p.params[2][2] = 2.0; @@ -1337,6 +1343,8 @@ enum Paint<'a> { None, Color([f32; 3]), Gradient(&'a Gradient), + /// A glow's gradient with its opacity gain (`effects::paint_glow`). + GlowGradient(&'a Gradient, f32), Pattern(&'a Pattern, Placement), } @@ -1426,6 +1434,14 @@ pub fn adjustment_on_gpu(adj: &Adjustment) -> bool { !matches!(adj, Adjustment::Levels { space: ToneSpace::Cmyk | ToneSpace::Lab, .. } | Adjustment::Curves { space: ToneSpace::Cmyk | ToneSpace::Lab, .. }) } +/// A tone transfer as the shader's `t_decode`/`t_encode` exponent (0: the sRGB curve). +fn transfer_exponent(t: Transfer) -> f32 { + match t { + Transfer::Srgb => 0.0, + Transfer::Gamma(g) => g, + } +} + /// Adjustment → (kernel kind, parameters, LUT rows). Kinds are the `switch` in `adjust()`. pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraft_color::SampleType) -> Program { let mut p = [[0.0f32; 4]; 4]; @@ -1452,11 +1468,7 @@ pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraf (5, p, None) } Adjustment::Exposure { exposure, offset, gamma } => { - let g = match transfer.for_exposure() { - Transfer::Srgb => 0.0, - Transfer::Gamma(g) => g, - }; - p[0] = [2f32.powf(*exposure), *offset, gamma.max(0.01), g]; + p[0] = [2f32.powf(*exposure), *offset, gamma.max(0.01), transfer_exponent(transfer.for_exposure())]; (6, p, None) } // RGB space only (see `adjustment_on_gpu`); the rows are the CPU's channel∘master LUTs. @@ -1485,8 +1497,13 @@ pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraf (10, p, None) } Adjustment::PhotoFilter { color, density, preserve_luminosity } => { - p[0] = [color[0], color[1], color[2], *density]; - p[1][0] = if *preserve_luminosity { 1.0 } else { 0.0 }; + // compose::adjust: the linear-light matrix, then SetLum on the encoded values (8/16-bit). + let linear_doc = depth == photocraft_color::SampleType::F32; + let m = adjust::photo_filter_matrix(*color, *density, *preserve_luminosity && linear_doc); + for (row, m) in p.iter_mut().zip(m) { + *row = [m[0], m[1], m[2], 0.0]; + } + p[3] = [if *preserve_luminosity && !linear_doc { 1.0 } else { 0.0 }, transfer_exponent(transfer), 0.0, 0.0]; (11, p, None) } Adjustment::BlackWhite { weights, tint } => { @@ -1540,6 +1557,18 @@ pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraf } } +/// A gradient's colour and alpha as four 4096-entry LUT rows (`lut_tex`). +fn gradient_rows(g: &Gradient) -> Vec<[f32; 4096]> { + let mut rows = vec![[0.0f32; 4096]; 4]; + for k in 0..4096 { + let v = photocraft_compose::effects::sample_gradient(g, k as f32 / 4095.0); + for (ch, row) in rows.iter_mut().enumerate() { + row[k] = v[ch]; + } + } + rows +} + /// Mode index used by the shader (declaration order of [`BlendMode`]). pub fn mode_index(m: BlendMode) -> i32 { match m { diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 050667d3cb..c496161e73 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -251,6 +251,10 @@ fn adjustment_layers() { let lv = Adjustment::Levels { master: lc, per_channel: Default::default(), space: Default::default(), black: LevelsChannel::default() }; d.layers.push(Layer::new("lv", LayerContent::Adjustment(lv))); check(&mut g, &d, &format!("levels {depth:?}")); + // Photo Filter: SetLum on the encoded values in 16-bit, luminance-normalised in 32-bit. + let pf = Adjustment::PhotoFilter { color: [0.93, 0.54, 0.0], density: 0.6, preserve_luminosity: true }; + d.layers.push(Layer::new("pf", LayerContent::Adjustment(pf))); + check(&mut g, &d, &format!("photo filter {depth:?}")); } for adj in adjustments() { for (mode, opacity, masked) in [(BlendMode::Normal, 1.0, false), (BlendMode::Multiply, 0.7, true)] { @@ -676,6 +680,11 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { ("outer glow softer", vec![Effect::OuterGlow(glow(FxPaint::Color(Color::rgb(1.0, 0.9, 0.2)), GlowTechnique::Softer, 10.0, 0.3, GlowSource::Edge))]), ("outer glow contour", vec![Effect::OuterGlow(og_contour)]), ("outer glow precise gradient", vec![Effect::OuterGlow(glow(FxPaint::Gradient(gradient()), GlowTechnique::Precise, 8.0, 0.25, GlowSource::Edge))]), + ( + "outer glow softer gradient", + vec![Effect::OuterGlow(Glow { range: 0.4, ..glow(FxPaint::Gradient(gradient()), GlowTechnique::Softer, 10.0, 0.0, GlowSource::Edge) })], + ), + ("inner glow softer gradient", vec![Effect::InnerGlow(glow(FxPaint::Gradient(gradient()), GlowTechnique::Softer, 7.0, 0.1, GlowSource::Edge))]), ("inner glow softer edge", vec![Effect::InnerGlow(glow(FxPaint::Color(Color::rgb(0.9, 1.0, 0.8)), GlowTechnique::Softer, 7.0, 0.2, GlowSource::Edge))]), ( "inner glow softer center", diff --git a/crates/io/tests/corpus.rs b/crates/io/tests/corpus.rs index 4d8c289946..c997fe9b0a 100644 --- a/crates/io/tests/corpus.rs +++ b/crates/io/tests/corpus.rs @@ -156,7 +156,7 @@ const PSD_TOOLS: Source = Source { label: "psd-tools corpus", dir: "corpus/psd-t /// Our Photoshop-authored oracles (256 files; https://github.com/storytold/photocraft-corpus), /// grouped by feature (`smart-filters`, `effects`, `text`, `adjustments/`). -const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 132, roundtrip_floor: 258 }; +const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 133, roundtrip_floor: 258 }; /// The corpus directory; a missing corpus fails the test. fn locate(src: &Source) -> PathBuf { diff --git a/docs/scorecard.md b/docs/scorecard.md index f41ca78277..f903a51080 100644 --- a/docs/scorecard.md +++ b/docs/scorecard.md @@ -7,11 +7,11 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js | Area | Done | Partial | Missing | Key numbers | |---|---:|---:|---:|---| | [Performance](#performance) | 3 | 22 | 13 | 47 scenarios: 3 within budget, 22 over budget or failing, 15 tracked only, 6 not run, 7 not measurable yet (baseline 2026-10-05) | -| [File compatibility](#file-compatibility) | 3 | 9 | 15 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 229/309, round trip ≥ 307/309; photoshop oracles: oracle ≥ 132/256, round trip ≥ 258/256 | +| [File compatibility](#file-compatibility) | 3 | 9 | 15 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 229/309, round trip ≥ 307/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 | | [Tools](#tools) | 6 | 15 | 9 | | | [Workspace and UI](#workspace-and-ui) | 0 | 5 | 18 | settings that do nothing: 57 of 135 (target 0) | | [Type](#type) | 0 | 3 | 6 | | -| [Automation](#automation) | 3 | 2 | 6 | 626 of 626 menu items live (100.0%) | +| [Automation](#automation) | 3 | 2 | 6 | 627 of 627 menu items live (100.0%) | | [Reliability](#reliability) | 4 | 1 | 2 | never-crash lints on 27 of 27 crates; panic_hunt present | | [Distribution](#distribution) | 4 | 4 | 3 | | | [Open bugs](#open-bugs) | 12 | 4 | 16 | | @@ -85,7 +85,7 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t |---|---|---:|---:|---:| | io corpus | `corpus/psd` | 170 | 146 | 169 | | psd-tools corpus | `corpus/psd-tools` | 309 | 229 | 307 | -| photoshop oracles | `corpus/photoshop` | 256 | 132 | 258 | +| photoshop oracles | `corpus/photoshop` | 256 | 133 | 258 | | Id | Target | Status | Issue | Note | |---|---|---|---|---| @@ -200,7 +200,7 @@ Unread: `general.color_picker`, `general.beep_when_done`, `general.export_clipbo ## Automation -Menu wiring (`docs/parity.md`): 626 of 626 menu items live (100.0%). This counts dispatch, not behaviour. +Menu wiring (`docs/parity.md`): 627 of 627 menu items live (100.0%). This counts dispatch, not behaviour. | Id | Target | Status | Issue | Note | |---|---|---|---|---|