mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 16:17:55 +00:00
craft-develop, craft-color-transform: deduplicate spline, add 1024-entry compose_len, spaces, and warm-green WB test (#2464)
* craft-develop, craft-color-transform: deduplicate spline, add 1024-entry Lut1::compose_len, spaces, and warm-green WB test * cct: check bradford identity and matrix tolerance in warm-green WB test * style(color-transform): fix clippy needless_range_loop in cct test
This commit is contained in:
Generated
+1
@@ -1794,6 +1794,7 @@ dependencies = [
|
||||
name = "craft-develop"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-color-transform",
|
||||
"craft-develop-model",
|
||||
"craft-render-parity",
|
||||
"proptest",
|
||||
|
||||
@@ -240,4 +240,24 @@ mod tests {
|
||||
assert_eq!(xy_to_temp_tint(p), (6500.0, 0.0), "{p:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Pins the current behavior for warm, strongly green illuminants (e.g. 2000–3500 K with high
|
||||
/// positive tint) where the source white's Bradford blue cone response is `<= 0.0`. The
|
||||
/// checked Bradford path currently returns `Mat3::IDENTITY` there (so `wb_matrix` is
|
||||
/// `REC2020.from_xyz() * REC2020.to_xyz()`, i.e. identity within `f64` rounding) rather than
|
||||
/// inverting blue; final `#587` process-version policy for this region remains open.
|
||||
#[test]
|
||||
fn warm_green_wb_with_non_positive_cone_response_returns_identity() {
|
||||
for (t, tint) in [(2000.0, 30.0), (2500.0, 60.0), (3000.0, 100.0), (3500.0, 125.0)] {
|
||||
let src = temp_tint_to_xy(t, tint);
|
||||
assert_eq!(crate::bradford(src, REC2020.white), Mat3::IDENTITY, "{t} K tint {tint}");
|
||||
let m = wb_matrix(&REC2020, src);
|
||||
for (i, row) in m.0.iter().enumerate() {
|
||||
for (j, &val) in row.iter().enumerate() {
|
||||
let want = if i == j { 1.0 } else { 0.0 };
|
||||
assert!((val - want).abs() < 1e-12, "{t} K tint {tint} [{i}][{j}] = {val}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,6 +225,32 @@ impl Lut1 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resample this table to `n` evenly spaced entries over `0..1` (2 to [`MAX_LUT`]).
|
||||
pub fn resample(&self, n: usize) -> Lut1 {
|
||||
let n = n.clamp(2, MAX_LUT);
|
||||
if self.v.len() == n {
|
||||
return self.clone();
|
||||
}
|
||||
Lut1::from_fn(n, |x| self.eval(x))
|
||||
}
|
||||
|
||||
/// `self ∘ inner` sampled at `n` evenly spaced entries over `0..1` (2 to [`MAX_LUT`]).
|
||||
///
|
||||
/// Preserves fixed-size GPU LUT contracts (such as 1,024-entry shader tables) where
|
||||
/// [`Lut1::compose`] would expand to the common refinement grid. When `inner` is empty it
|
||||
/// resamples `self` to `n` entries; when `inner` already has `n` entries it evaluates `self`
|
||||
/// directly at each of `inner`'s samples.
|
||||
pub fn compose_len(&self, inner: &Lut1, n: usize) -> Lut1 {
|
||||
let n = n.clamp(2, MAX_LUT);
|
||||
if inner.v.is_empty() {
|
||||
return self.resample(n);
|
||||
}
|
||||
if inner.v.len() == n {
|
||||
return Lut1 { v: inner.v.iter().map(|&x| self.eval(x)).collect() };
|
||||
}
|
||||
Lut1::from_fn(n, |x| self.eval(inner.eval(x)))
|
||||
}
|
||||
|
||||
pub fn is_identity(&self) -> bool {
|
||||
let n = self.v.len();
|
||||
n >= 2 && self.v.iter().enumerate().all(|(i, &y)| (y - i as f32 / (n - 1) as f32).abs() < 1e-6)
|
||||
@@ -319,6 +345,21 @@ mod tests {
|
||||
assert_eq!(a.compose(&Lut1 { v: vec![] }), a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_len_preserves_fixed_gpu_lut_size() {
|
||||
const N: usize = 1024;
|
||||
let a = Lut1::from_fn(N, |x| x * x);
|
||||
let b = Lut1::from_fn(N, f32::sqrt);
|
||||
let c = a.compose_len(&b, N);
|
||||
assert_eq!(c.v.len(), N);
|
||||
assert!((c.eval(0.3) - 0.3).abs() < 2e-3);
|
||||
assert_eq!(a.compose_len(&Lut1 { v: vec![] }, N), a);
|
||||
let small = Lut1::from_fn(17, f32::sqrt);
|
||||
let d = a.compose_len(&small, N);
|
||||
assert_eq!(d.v.len(), N);
|
||||
assert!((d.eval(0.3) - a.eval(small.eval(0.3))).abs() < 2e-3);
|
||||
}
|
||||
|
||||
/// A short `inner` does not drop the outer table's points between its own.
|
||||
#[test]
|
||||
fn composing_with_a_short_table_keeps_the_outer_curve() {
|
||||
|
||||
@@ -18,4 +18,6 @@ pub mod oklab;
|
||||
mod space;
|
||||
pub mod tonemap;
|
||||
|
||||
pub use space::{BRADFORD, D50, D65, DISPLAY_P3, Mat3, REC601_525, REC601_625, REC2020, RgbSpace, SRGB, Xy, bradford, bradford_xyz};
|
||||
pub use space::{
|
||||
ADOBE_RGB, BRADFORD, D50, D65, DISPLAY_P3, Mat3, PROPHOTO, REC601_525, REC601_625, REC2020, RgbSpace, SRGB, Xy, bradford, bradford_xyz,
|
||||
};
|
||||
|
||||
@@ -118,6 +118,11 @@ pub const REC601_525: RgbSpace = RgbSpace { r: Xy::new(0.630, 0.340), g: Xy::new
|
||||
pub const REC2020: RgbSpace = RgbSpace { r: Xy::new(0.708, 0.292), g: Xy::new(0.170, 0.797), b: Xy::new(0.131, 0.046), white: D65 };
|
||||
/// The DCI-P3 primaries with a D65 white (SMPTE EG 432-1).
|
||||
pub const DISPLAY_P3: RgbSpace = RgbSpace { r: Xy::new(0.680, 0.320), g: Xy::new(0.265, 0.690), b: Xy::new(0.150, 0.060), white: D65 };
|
||||
/// ProPhoto RGB (ROMM RGB, ISO 22028-2) primaries with a D50 white.
|
||||
pub const PROPHOTO: RgbSpace =
|
||||
RgbSpace { r: Xy::new(0.734699, 0.265301), g: Xy::new(0.159597, 0.840403), b: Xy::new(0.036598, 0.000105), white: D50 };
|
||||
/// The 1998 Adobe RGB primaries with a D65 white.
|
||||
pub const ADOBE_RGB: RgbSpace = RgbSpace { r: Xy::new(0.64, 0.33), g: Xy::new(0.21, 0.71), b: Xy::new(0.15, 0.06), white: D65 };
|
||||
|
||||
impl From<Primaries> for RgbSpace {
|
||||
fn from(p: Primaries) -> RgbSpace {
|
||||
@@ -292,4 +297,16 @@ mod tests {
|
||||
let m: [[f64; 3]; 3] = read.to_xyz().into();
|
||||
assert_eq!(Mat3::from(m), read.to_xyz());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prophoto_and_adobe_rgb_map_white_and_roundtrip() {
|
||||
for sp in [PROPHOTO, ADOBE_RGB] {
|
||||
let w = sp.to_space(&REC2020).apply([1.0, 1.0, 1.0]);
|
||||
assert!(w.iter().all(|c| (c - 1.0).abs() < 1e-3), "{sp:?} -> {w:?}");
|
||||
}
|
||||
let a = SRGB.to_space(&PROPHOTO);
|
||||
let b = PROPHOTO.to_space(&SRGB);
|
||||
let v = b.apply(a.apply([0.2, 0.5, 0.9]));
|
||||
assert!((v[0] - 0.2).abs() < 1e-9 && (v[1] - 0.5).abs() < 1e-9 && (v[2] - 0.9).abs() < 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ repository.workspace = true
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-color-transform = { workspace = true }
|
||||
craft-develop-model = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ craft-develop = { git = "https://github.com/storytold/craft-libs", rev = "<commi
|
||||
|
||||
| Module | From LightCraft (`a75497d6`) | Contents |
|
||||
|---|---|---|
|
||||
| `color::spline` | `crates/color/src/spline.rs` | `MonotoneCurve` (a tone curve through points that does not overshoot) and `Lut1`. |
|
||||
| `color::spline` | `crates/color/src/spline.rs` | Re-exports `craft_color_transform::curve` (`MonotoneCurve`, `Lut1`). |
|
||||
| `curve` | `crates/pipeline` | The parametric tone curve and its regions. |
|
||||
| `finish` | `crates/pipeline` | Pure-Rust finishing helpers: sharpening masks/limits, vignette parameters, value grain, gamut mapping, and curve saturation refinement. |
|
||||
| `lut` | `crates/pipeline` | 3D lookup tables and the `.cube` reader. |
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
//! Colour helpers for the develop pipeline.
|
||||
|
||||
pub mod spline;
|
||||
pub use craft_color_transform::curve as spline;
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
//! Curves for tone mapping: monotone cubic Hermite splines (Fritsch–Carlson) and lookup tables.
|
||||
//!
|
||||
//! Monotone interpolation never overshoots between control points, so a point curve can't invert
|
||||
//! tones by accident (a common complaint with natural cubic splines).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A curve through control points in `0..1 × 0..1` (x strictly increasing after normalization).
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct MonotoneCurve {
|
||||
xs: Vec<f64>,
|
||||
ys: Vec<f64>,
|
||||
ms: Vec<f64>,
|
||||
}
|
||||
|
||||
impl MonotoneCurve {
|
||||
/// Build from points; sorts by x, merges duplicates. Fewer than two points → identity.
|
||||
pub fn new(points: &[(f64, f64)]) -> MonotoneCurve {
|
||||
let mut p: Vec<(f64, f64)> = points.iter().copied().filter(|(x, y)| x.is_finite() && y.is_finite()).collect();
|
||||
p.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
p.dedup_by(|a, b| (a.0 - b.0).abs() < 1e-9);
|
||||
if p.len() < 2 {
|
||||
p = vec![(0.0, 0.0), (1.0, 1.0)];
|
||||
}
|
||||
let n = p.len();
|
||||
let xs: Vec<f64> = p.iter().map(|q| q.0).collect();
|
||||
let ys: Vec<f64> = p.iter().map(|q| q.1).collect();
|
||||
let d: Vec<f64> = (0..n - 1).map(|i| (ys[i + 1] - ys[i]) / (xs[i + 1] - xs[i])).collect();
|
||||
let mut ms = vec![0.0; n];
|
||||
ms[0] = d[0];
|
||||
ms[n - 1] = d[n - 2];
|
||||
for i in 1..n - 1 {
|
||||
ms[i] = if d[i - 1] * d[i] <= 0.0 { 0.0 } else { (d[i - 1] + d[i]) / 2.0 };
|
||||
}
|
||||
// Fritsch–Carlson limiter where the data is monotone within a segment.
|
||||
for i in 0..n - 1 {
|
||||
if d[i] == 0.0 {
|
||||
ms[i] = 0.0;
|
||||
ms[i + 1] = 0.0;
|
||||
continue;
|
||||
}
|
||||
let a = ms[i] / d[i];
|
||||
let b = ms[i + 1] / d[i];
|
||||
let s = a * a + b * b;
|
||||
if s > 9.0 {
|
||||
let t = 3.0 / s.sqrt();
|
||||
ms[i] = t * a * d[i];
|
||||
ms[i + 1] = t * b * d[i];
|
||||
}
|
||||
}
|
||||
MonotoneCurve { xs, ys, ms }
|
||||
}
|
||||
|
||||
pub fn identity() -> MonotoneCurve {
|
||||
MonotoneCurve::new(&[(0.0, 0.0), (1.0, 1.0)])
|
||||
}
|
||||
|
||||
pub fn eval(&self, x: f64) -> f64 {
|
||||
// NaN passes both bound checks below and would send the search past either end
|
||||
if x.is_nan() {
|
||||
return x;
|
||||
}
|
||||
let n = self.xs.len();
|
||||
if x <= self.xs[0] {
|
||||
return self.ys[0] + self.ms[0] * (x - self.xs[0]).min(0.0) * 0.0;
|
||||
}
|
||||
if x >= self.xs[n - 1] {
|
||||
return self.ys[n - 1];
|
||||
}
|
||||
let i = match self.xs.binary_search_by(|v| v.total_cmp(&x)) {
|
||||
Ok(i) => return self.ys[i],
|
||||
Err(i) => i - 1,
|
||||
};
|
||||
let h = self.xs[i + 1] - self.xs[i];
|
||||
let t = (x - self.xs[i]) / h;
|
||||
let (t2, t3) = (t * t, t * t * t);
|
||||
(2.0 * t3 - 3.0 * t2 + 1.0) * self.ys[i]
|
||||
+ (t3 - 2.0 * t2 + t) * h * self.ms[i]
|
||||
+ (-2.0 * t3 + 3.0 * t2) * self.ys[i + 1]
|
||||
+ (t3 - t2) * h * self.ms[i + 1]
|
||||
}
|
||||
|
||||
/// Sample into a LUT of `n` entries (at least 2) over `0..1`.
|
||||
pub fn to_lut(&self, n: usize) -> Lut1 {
|
||||
let n = n.max(2);
|
||||
Lut1 { v: (0..n).map(|i| self.eval(i as f64 / (n - 1) as f64) as f32).collect() }
|
||||
}
|
||||
|
||||
pub fn points(&self) -> Vec<(f64, f64)> {
|
||||
self.xs.iter().copied().zip(self.ys.iter().copied()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A 1-D lookup table over `0..1` with linear interpolation; inputs outside clamp.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Lut1 {
|
||||
pub v: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Lut1 {
|
||||
/// At least 2 entries, like [`Lut1::from_fn`].
|
||||
pub fn identity(n: usize) -> Lut1 {
|
||||
Lut1::from_fn(n, |x| x)
|
||||
}
|
||||
/// `f` sampled at `n` evenly spaced points over `0..1` (at least 2).
|
||||
pub fn from_fn(n: usize, f: impl Fn(f32) -> f32) -> Lut1 {
|
||||
let n = n.max(2);
|
||||
Lut1 { v: (0..n).map(|i| f(i as f32 / (n - 1) as f32)).collect() }
|
||||
}
|
||||
/// `v` is public, so a table shorter than 2 entries is read as constant (1) or the identity (0).
|
||||
#[inline]
|
||||
pub fn eval(&self, x: f32) -> f32 {
|
||||
let x = x.clamp(0.0, 1.0);
|
||||
let n = self.v.len();
|
||||
if n < 2 {
|
||||
return self.v.first().copied().unwrap_or(x);
|
||||
}
|
||||
let f = x * (n - 1) as f32;
|
||||
let i = (f as usize).min(n - 2);
|
||||
let t = f - i as f32;
|
||||
self.v[i] + (self.v[i + 1] - self.v[i]) * t
|
||||
}
|
||||
/// `self ∘ inner` (apply `inner` first).
|
||||
pub fn compose(&self, inner: &Lut1) -> Lut1 {
|
||||
Lut1 { v: inner.v.iter().map(|&x| self.eval(x)).collect() }
|
||||
}
|
||||
pub fn is_identity(&self) -> bool {
|
||||
let n = self.v.len();
|
||||
self.v.iter().enumerate().all(|(i, &y)| (y - i as f32 / (n - 1) as f32).abs() < 1e-6)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn identity_curve() {
|
||||
let c = MonotoneCurve::identity();
|
||||
for i in 0..=10 {
|
||||
let x = i as f64 / 10.0;
|
||||
assert!((c.eval(x) - x).abs() < 1e-12);
|
||||
}
|
||||
assert!(c.to_lut(256).is_identity());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_through_points_and_is_monotone() {
|
||||
let pts = [(0.0, 0.0), (0.25, 0.15), (0.5, 0.5), (0.75, 0.88), (1.0, 1.0)];
|
||||
let c = MonotoneCurve::new(&pts);
|
||||
for (x, y) in pts {
|
||||
assert!((c.eval(x) - y).abs() < 1e-12);
|
||||
}
|
||||
let mut prev = -1.0;
|
||||
for i in 0..=1000 {
|
||||
let y = c.eval(i as f64 / 1000.0);
|
||||
assert!(y >= prev - 1e-12);
|
||||
prev = y;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_overshoot_on_flat_segment() {
|
||||
let c = MonotoneCurve::new(&[(0.0, 0.0), (0.4, 0.5), (0.6, 0.5), (1.0, 1.0)]);
|
||||
for i in 400..=600 {
|
||||
let y = c.eval(i as f64 / 1000.0);
|
||||
assert!((y - 0.5).abs() < 1e-9, "{y}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lut_compose() {
|
||||
let a = Lut1::from_fn(1024, |x| x * x);
|
||||
let b = Lut1::from_fn(1024, |x| x.sqrt());
|
||||
let c = a.compose(&b);
|
||||
assert!((c.eval(0.3) - 0.3).abs() < 2e-3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nan_input_and_short_luts_do_not_panic() {
|
||||
let c = MonotoneCurve::new(&[(0.0, 0.0), (0.5, 0.6), (1.0, 1.0)]);
|
||||
assert!(c.eval(f64::NAN).is_nan());
|
||||
assert!(c.eval(-f64::NAN).is_nan());
|
||||
for n in [0, 1] {
|
||||
assert_eq!(c.to_lut(n).v.len(), 2, "{n}");
|
||||
assert!(Lut1::identity(n).is_identity(), "{n}");
|
||||
assert_eq!(Lut1::from_fn(n, |x| x).v.len(), 2, "{n}");
|
||||
}
|
||||
assert_eq!(Lut1 { v: vec![] }.eval(0.3), 0.3);
|
||||
assert_eq!(Lut1 { v: vec![] }.eval(-1.0), 0.0, "inputs outside 0..1 clamp, as for any table");
|
||||
assert_eq!(Lut1 { v: vec![] }.eval(2.0), 1.0);
|
||||
assert_eq!(Lut1 { v: vec![0.7] }.eval(0.3), 0.7);
|
||||
}
|
||||
|
||||
proptest::proptest! {
|
||||
#[test]
|
||||
fn monotone_for_increasing_data(ys in proptest::collection::vec(0.0f64..1.0, 3..8)) {
|
||||
let mut ys = ys; ys.sort_by(|a, b| a.total_cmp(b));
|
||||
let n = ys.len();
|
||||
let pts: Vec<(f64, f64)> = ys.iter().enumerate().map(|(i, y)| (i as f64 / (n - 1) as f64, *y)).collect();
|
||||
let c = MonotoneCurve::new(&pts);
|
||||
let mut prev = f64::MIN;
|
||||
for i in 0..=200 { let y = c.eval(i as f64 / 200.0); proptest::prop_assert!(y >= prev - 1e-9); prev = y; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user