oxedyne/fe2o3/fe2o3_graphics/src/icc/builtin.rs
3.4 KiB, 1 run
created by r1870400018:68821, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The two spaces built into the reader, computed from their definitions rather than held as |
| 2 | //! profile bytes: sRGB and sGray. |
| 3 | //! |
| 4 | //! sRGB is IEC 61966-2-1: the Rec. 709 primaries, the D65 white, and the piecewise curve with an |
| 5 | //! exponent of 2.4. The colorants are adapted from D65 to the D50 of the connection space by the |
| 6 | //! Bradford transform, as a profile of sRGB carries them. sGray is the sRGB curve on Y, with the D50 |
| 7 | //! white that is a grey profile's. |
| 8 | //! |
| 9 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 10 | //! Anthropic Claude |
| 11 | |
| 12 | use crate::icc::{ |
| 13 | matrix::{ |
| 14 | Grey, |
| 15 | M3, |
| 16 | RgbMatrix, |
| 17 | inv, |
| 18 | mul, |
| 19 | mul_v, |
| 20 | }, |
| 21 | read::Curve, |
| 22 | }; |
| 23 | |
| 24 | use oxedyne_fe2o3_core::prelude::*; |
| 25 | |
| 26 | // The connection space illuminant, as an s15Fixed16 profile holds it. |
| 27 | const D50: [f64; 3] = [0.964_202_880_859_375, 1.0, 0.824_905_395_507_812_5]; |
| 28 | const D65_XY: [f64; 2] = [0.3127, 0.3290]; |
| 29 | const PRIM_XY: [[f64; 2]; 3] = [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06]]; |
| 30 | |
| 31 | // The Bradford cone response matrix. |
| 32 | const BRADFORD: M3 = [ |
| 33 | [ 0.8951, 0.2664, -0.1614], |
| 34 | [-0.7502, 1.7135, 0.0367], |
| 35 | [ 0.0389, -0.0685, 1.0296], |
| 36 | ]; |
| 37 | |
| 38 | fn xyz_of(xy: [f64; 2]) -> [f64; 3] { |
| 39 | [xy[0] / xy[1], 1.0, (1.0 - xy[0] - xy[1]) / xy[1]] |
| 40 | } |
| 41 | |
| 42 | // The matrix that takes linear RGB of the given primaries and white to XYZ. |
| 43 | fn primaries(prim: [[f64; 2]; 3], white: [f64; 3]) -> Outcome<M3> { |
| 44 | let p: Vec<[f64; 3]> = prim.iter().map(|xy| xyz_of(*xy)).collect(); |
| 45 | let cols = [ |
| 46 | [p[0][0], p[1][0], p[2][0]], |
| 47 | [p[0][1], p[1][1], p[2][1]], |
| 48 | [p[0][2], p[1][2], p[2][2]], |
| 49 | ]; |
| 50 | let s = mul_v(&res!(inv(&cols)), white); |
| 51 | let mut m = cols; |
| 52 | for row in m.iter_mut() { |
| 53 | for (j, v) in row.iter_mut().enumerate() { |
| 54 | *v *= s[j]; |
| 55 | } |
| 56 | } |
| 57 | Ok(m) |
| 58 | } |
| 59 | |
| 60 | // The Bradford adaptation matrix from one white to another. |
| 61 | fn adapt(from: [f64; 3], to: [f64; 3]) -> Outcome<M3> { |
| 62 | let (a, b) = (mul_v(&BRADFORD, from), mul_v(&BRADFORD, to)); |
| 63 | let mut scale = [[0.0; 3]; 3]; |
| 64 | for i in 0..3 { |
| 65 | scale[i][i] = b[i] / a[i]; |
| 66 | } |
| 67 | Ok(mul(&res!(inv(&BRADFORD)), &mul(&scale, &BRADFORD))) |
| 68 | } |
| 69 | |
| 70 | // The sRGB transfer curve as a version 4 parametric curve of function type 3. |
| 71 | fn srgb_curve() -> Curve { |
| 72 | Curve::Para { |
| 73 | func: 3, |
| 74 | p: [2.4, 1.0 / 1.055, 0.055 / 1.055, 1.0 / 12.92, 0.04045, 0.0, 0.0], |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// The sRGB space, its colorants adapted to D50. |
| 79 | pub fn srgb() -> Outcome<RgbMatrix> { |
| 80 | let white = xyz_of(D65_XY); |
| 81 | let m = res!(primaries(PRIM_XY, white)); |
| 82 | let a = res!(adapt(white, D50)); |
| 83 | Ok(RgbMatrix { m: mul(&a, &m), trc: [srgb_curve(), srgb_curve(), srgb_curve()] }) |
| 84 | } |
| 85 | |
| 86 | /// The sGray space, the sRGB curve on Y. |
| 87 | pub fn sgray() -> Grey { |
| 88 | Grey { trc: srgb_curve() } |
| 89 | } |
| 90 | |
| 91 | #[cfg(test)] |
| 92 | mod tests { |
| 93 | use super::*; |
| 94 | |
| 95 | #[test] |
| 96 | fn srgb_colorants_are_the_d50_profile_values() -> Outcome<()> { |
| 97 | let s = res!(srgb()); |
| 98 | // The D50 sRGB colorants every v2 profile of sRGB carries, to four places. |
| 99 | let want = [[0.4360, 0.3851, 0.1431], [0.2225, 0.7169, 0.0606], [0.0139, 0.0971, 0.7141]]; |
| 100 | for i in 0..3 { |
| 101 | for j in 0..3 { |
| 102 | assert!((s.m[i][j] - want[i][j]).abs() < 6e-4, "m[{}][{}] = {}", i, j, s.m[i][j]); |
| 103 | } |
| 104 | } |
| 105 | // White maps to the D50 illuminant. |
| 106 | let w = s.xyz([1.0, 1.0, 1.0]); |
| 107 | for i in 0..3 { |
| 108 | assert!((w[i] - D50[i]).abs() < 1e-6, "white[{}] = {}", i, w[i]); |
| 109 | } |
| 110 | Ok(()) |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn srgb_curve_has_the_known_values() { |
| 115 | let c = srgb_curve(); |
| 116 | assert!((c.eval(0.5) - 0.214_041).abs() < 1e-5); |
| 117 | assert!((c.eval(0.04) - 0.04 / 12.92).abs() < 1e-12); |
| 118 | assert!((c.inverse(c.eval(0.73)) - 0.73).abs() < 1e-9); |
| 119 | } |
| 120 | } |