* Add craft-color-transform: RGB spaces, white balance, OkLab, tone curves and PQ tone mapping craft-color-transform builds on craft-color. It holds RGB spaces from primaries and a white point with Bradford adaptation, white balance by correlated color temperature and tint, OkLab and OkLCh for any RGB space, monotone tone curves and 1D lookup tables, and the BT.2408-8 Annex 5 EETF with a maxRGB tone map. The per-pixel functions take f32 and do not allocate, and the crate builds for wasm32-unknown-unknown. Moved from StudioCraft's craft-color-transform (blamejs/StudioCraft crates/craft-color-transform). The README names the LightCraft and FilmCraft modules parts of it started from. * Color transform: compose, interpolate and invert at extreme values Lut1::compose samples the densest grid of at most MAX_LUT entries that holds both tables' points, so a short inner table, or one that crosses an outer peak between its own points, keeps the outer table's points to within one grid step. Lut1::eval weights the two entries in f64, so entries as far apart as -f32::MAX and f32::MAX no longer give NaN or infinity. MonotoneCurve::new compares the tangent pair's length with 3|d|, so a subnormal secant next to a normal one no longer gives a NaN tangent. Mat3::inverse divides the matrix by its largest entry before taking the determinant, so diag(1e110) and diag(1e-200) invert. OkLab::for_space falls back to XYZ_TO_LMS when a matrix has no inverse or an entry beyond f32. * Color transform: build the matrix arithmetic on craft_cms::math craft_cms::math gains the two hardened functions the color transforms need, and craft-color-transform calls them instead of carrying its own copies. - math::inverse divides the matrix by its largest entry before taking the determinant, so a matrix whose own determinant overflows or underflows still inverts, and it returns None for NaN, infinite or all-zero input and for an inverse beyond f64. math::invert keeps its absolute 1e-12 floor, which profile construction uses to validate ICC matrices. - math::bradford_checked returns None when a white's cone response is not finite or not above 0, or the ratios overflow. math::bradford still divides without the check. - math::BRADFORD is public. - Mat3::mul, apply, determinant and inverse, Xy::to_xyz, BRADFORD and bradford_xyz call craft_cms::math. - oklab::lms_matrices gives OkLab::for_space's matrices in f64. * Color transform: keep the EETF's black lift rising for a high target black The BT.2408 Annex 5 lift minLum * (1 - E2)^4 has a slope of 1 - 4 minLum at E2 = 0, so a target black above a quarter of the normalized range made the output fall near black, and it raised the peak well above the target peak. The lift is now minLum * (1 - E2 / r)^k with r = max(T, min(1, 2 - 4 minLum)) and k = min(4, r / minLum), where T is the top of the output (the smaller of maxLum and 1). It equals the Annex's lift up to minLum 0.25, keeps the output rising, changes continuously with the target black, and from minLum 0.5 reaches 0 at the top of the output. A target black above the source peak reads as the source peak. * Color transform: state the composition bound for the fallback grid
6.6 KiB
craft-color-transform
Color transforms for the Craft apps: RGB spaces and chromatic adaptation, white balance by
temperature and tint, OkLab and OkLCh, monotone tone curves and 1D lookup tables, and PQ tone
mapping. It builds on craft-color, which holds the frame color tags, the Y'CbCr
matrices and the sRGB, PQ and HLG transfer functions. It depends on craft-color, craft-cms
and serde, contains no unsafe code, and builds for wasm32-unknown-unknown. The per-pixel
functions take f32 and do not allocate.
The matrix arithmetic comes from craft_cms::math. Mat3 wraps its row-major [[f64; 3]; 3]
and converts to and from it with From. mul, apply and determinant call math::mul,
math::apply and math::det, Mat3::inverse calls math::inverse, Xy::to_xyz calls
math::xy_to_xyz after checking the chromaticity, BRADFORD is math::BRADFORD, and
bradford_xyz calls math::bradford_checked. math::inverse divides the matrix by its largest
entry before taking the determinant. math::invert, which craft-cms uses to validate ICC
matrices, keeps its absolute determinant floor of 1e-12. RgbSpace::to_xyz builds the
normalized primary matrix with math::inverse rather than with math::rgb_to_xyz_matrix, which
inverts with math::invert, and returns the identity when the normalized matrix has no inverse.
Moved from StudioCraft's craft-color-transform
(crates/craft-color-transform).
Parts of it started in other Craft apps:
cct,oklabandcurvecame from LightCraft'slightcraft-color(crates/color/src/cct.rs,perceptual.rsandspline.rs).MonotoneCurvenow loads a saved curve through its constructor, so a file cannot give it arrays of different lengths, and it bounds the points it keeps and the size of their coordinates.OkLab::for_spaceextends the BT.2020-only OkLab conversion to anyRgbSpace.tonemapcame from FilmCraft'sfilmcraft-color(crates/color/src/transform.rs,bt2390_eetfand its maxRGB operator). It now follows Report ITU-R BT.2408-8 Annex 5 with the black lift, passes values through when the target peak is at or above the source peak, and handles a target peak below a third of the source range (seeEetf).
License: MIT OR Apache-2.0.
Contents
| Item | What it does | Source of the formula |
|---|---|---|
Mat3, Xy, RgbSpace |
3×3 matrices, CIE 1931 chromaticities, RGB spaces from primaries and a white point. RgbSpace::to_xyz is the normalized primary matrix. RgbSpace::from(craft_color::Primaries) gives the space of a frame tag. |
SMPTE RP 177; primaries from ITU-R BT.709, BT.601, BT.2020 and SMPTE EG 432-1 |
bradford, bradford_xyz |
The Bradford chromatic adaptation matrix between two whites. RgbSpace::to_space converts between spaces with it. |
The Bradford cone response matrix |
cct::temp_tint_to_xy, cct::xy_to_temp_tint |
A white at a correlated color temperature and tint, and back. Tint is Duv × 3000 along the locus normal in CIE 1960 (u, v); positive is towards green. | Kim et al., "Design of Advanced Color Temperature Control System for HDTV Applications", Journal of the Korean Physical Society 41 (2002) |
cct::wb_matrix, cct::white_balance |
The RGB matrix that makes a scene lit by one white look lit by another. | Bradford adaptation |
oklab |
OkLab from linear sRGB with the published matrices, from XYZ, and for any RgbSpace (OkLab::for_space, with f32 matrices; oklab::lms_matrices gives the same matrices in f64); OkLCh. |
B. Ottosson, "A perceptual color space for image processing" (2020) |
curve::MonotoneCurve |
A tone curve through points that does not overshoot between them. It saves as its list of points. knots gives each point with its tangent for evaluation in a shader. |
Fritsch and Carlson, "Monotone Piecewise Cubic Interpolation", SIAM J. Numer. Anal. 17 (1980) |
curve::Lut1 |
A 1D lookup table over 0..1 with linear interpolation. | |
tonemap::Eetf |
The reference EETF from a mastering display to a target display, with the black lift, on PQ values or nits. When the target peak is below a third of the source range in PQ, where the Annex's knee would be negative, the knee is 0 and the spline starts with a slope of 3 × maxLum. When minLum is above 0.25, where the Annex's lift minLum × (1 − E2)⁴ would make the output fall near black and raise the peak above the target peak, the lift is minLum × (1 − E2 / r)ᵏ with r = max(T, min(1, 2 − 4 minLum)) and k = min(4, r / minLum), where T is the smaller of maxLum and 1. This equals the Annex's lift up to minLum 0.25, keeps the output rising, changes continuously with the target black, and from minLum 0.5 reaches 0 at the top of the output. A target black above the source peak reads as the source peak. Both rules are the crate's own. | Report ITU-R BT.2408-8, Annex 5 |
tonemap::ToneMap |
The EETF applied to linear RGB by max(R, G, B) as a ratio, with 1.0 at the 203-nit reference white. | Report ITU-R BT.2408-8, Annex 5 (the maxRGB option) |
Hostile input
- A chromaticity with
yat or below 0, or with a coordinate that is not finite, is rejected byXy::to_xyz.bradfordthen returns the identity, and so doesRgbSpace::to_xyzfor such a white, for collinear primaries and for a white that gives a primary no weight.Mat3::inverseandbradford_xyzreturnNoneor the identity instead of a matrix with entries beyondf64. - Temperatures are clamped to 1000 K to 100 000 K, and NaN reads as 6500 K. A tint that is not finite reads as 0.
MonotoneCurvekeeps at most 1024 points and drops points with a coordinate that is not finite or is beyond ±1e6. A saved curve is read back through the same constructor, so a file cannot give it mismatched arrays.Lut1::from_fnandMonotoneCurve::to_lutbuild at most 65 536 entries.- Luminances are clamped to 0..10 000 nits. A target peak that is NaN or not above 0 gives an
EETF and a
ToneMapthat pass values through.
Tests
cargo test -p craft-color-transform checks the code against published values: the sRGB matrix of
IEC 61966-2-1, the BT.709 and BT.2020 luminance weights, the BT.709 to BT.2020 matrix of
ITU-R BT.2087, the D65 to D50 Bradford matrix as Lindbloom's chromatic adaptation page prints it,
illuminant A and D65 from CIE 15:2004 (temperature, Duv, CIE 1960 coordinates), Ottosson's XYZ to
OkLab table, the OkLab value CSS Color Module Level 4 gives for #7654CD, the PQ levels of
BT.2408, and EETF values computed from the BT.2408 Annex 5 equations by a separate
implementation.