oxedyne/fe2o3/fe2o3_graphics/src/icc/lab.rs
7.7 KiB, 1 run
created by r1870400018:68825, 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 connection-space arithmetic of a transform: XYZ and Lab, the encodings a lookup table holds |
| 2 | //! Lab in, and the black points a transform compensates between. |
| 3 | //! |
| 4 | //! The conversions use the D50 white `(0.9642, 1.0, 0.8249)` that LittleCMS takes for the |
| 5 | //! connection space, not the profile header's s15Fixed16 illuminant, since the black points and |
| 6 | //! the swatches that pin them are LittleCMS's. A black point is an XYZ colour; the one for a |
| 7 | //! lookup-table CMYK profile is found by the methods Adobe published and LittleCMS follows: the |
| 8 | //! darker colorant of the table, the round trip of Lab black through the output table, or, for a |
| 9 | //! destination, a quadratic fitted to the shadow end of the round trip of a ramp of lightness. |
| 10 | //! |
| 11 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 12 | //! Anthropic Claude |
| 13 | |
| 14 | use crate::icc::{ |
| 15 | matrix::{ |
| 16 | inv, |
| 17 | mul_v, |
| 18 | }, |
| 19 | read::{ |
| 20 | Class, |
| 21 | Lut, |
| 22 | Profile, |
| 23 | Space, |
| 24 | }, |
| 25 | transform::Intent, |
| 26 | }; |
| 27 | |
| 28 | pub const D50: [f64; 3] = [0.9642, 1.0, 0.8249]; |
| 29 | |
| 30 | // The black point a version 4 profile has for the perceptual and saturation intents. |
| 31 | const V4_BLACK: [f64; 3] = [0.00336, 0.0034731, 0.00287]; |
| 32 | |
| 33 | const EDGE: f64 = 24.0 / 116.0; |
| 34 | |
| 35 | fn f(t: f64) -> f64 { |
| 36 | if t <= EDGE * EDGE * EDGE { 841.0 / 108.0 * t + 16.0 / 116.0 } else { t.cbrt() } |
| 37 | } |
| 38 | |
| 39 | fn f_inv(t: f64) -> f64 { |
| 40 | if t <= EDGE { 108.0 / 841.0 * (t - 16.0 / 116.0) } else { t * t * t } |
| 41 | } |
| 42 | |
| 43 | /// The Lab of an XYZ colour, both against the D50 white. |
| 44 | pub fn xyz_to_lab(xyz: [f64; 3]) -> [f64; 3] { |
| 45 | let (x, y, z) = (f(xyz[0] / D50[0]), f(xyz[1] / D50[1]), f(xyz[2] / D50[2])); |
| 46 | [116.0 * y - 16.0, 500.0 * (x - y), 200.0 * (y - z)] |
| 47 | } |
| 48 | |
| 49 | /// The XYZ of a Lab colour, both against the D50 white. |
| 50 | pub fn lab_to_xyz(lab: [f64; 3]) -> [f64; 3] { |
| 51 | let y = (lab[0] + 16.0) / 116.0; |
| 52 | let (x, z) = (y + lab[1] / 500.0, y - lab[2] / 200.0); |
| 53 | [f_inv(x) * D50[0], f_inv(y) * D50[1], f_inv(z) * D50[2]] |
| 54 | } |
| 55 | |
| 56 | /// A Lab colour as the fractions of full scale a table of `bits` bits takes it in. |
| 57 | /// |
| 58 | /// A lut16 holds the legacy encoding, where L* of 100 is 0xFF00 and a* or b* of zero is 0x8000. |
| 59 | /// A lut8 holds L* of 100 as 255 and a* or b* of zero as 128. |
| 60 | pub fn lab_enc(bits: u8, lab: [f64; 3]) -> [f64; 3] { |
| 61 | let k = if bits == 8 { 1.0 } else { 65280.0 / 65535.0 }; |
| 62 | [lab[0] / 100.0 * k, (lab[1] + 128.0) / 255.0 * k, (lab[2] + 128.0) / 255.0 * k] |
| 63 | } |
| 64 | |
| 65 | /// The Lab colour a table of `bits` bits holds as the fractions `v`; the inverse of [`lab_enc`]. |
| 66 | pub fn lab_dec(bits: u8, v: [f64; 3]) -> [f64; 3] { |
| 67 | let k = if bits == 8 { 1.0 } else { 65535.0 / 65280.0 }; |
| 68 | [v[0] * 100.0 * k, v[1] * 255.0 * k - 128.0, v[2] * 255.0 * k - 128.0] |
| 69 | } |
| 70 | |
| 71 | /// The table for an intent, or the first when the profile holds none for it. |
| 72 | pub fn pick(t: &[Option<Lut>; 3], i: usize) -> Option<&Lut> { |
| 73 | t[i].as_ref().or(t[0].as_ref()) |
| 74 | } |
| 75 | |
| 76 | // Lab to the CMYK of the output table `tag` and back through the relative colorimetric input table. |
| 77 | fn round(p: &Profile, tag: usize, lab: [f64; 3]) -> Option<[f64; 3]> { |
| 78 | let b = p.b2a[tag].as_ref()?; |
| 79 | let a = pick(&p.a2b, 1)?; |
| 80 | let e = lab_enc(b.bits, lab); |
| 81 | let cmyk = b.eval([e[0], e[1], e[2], 0.0]); |
| 82 | let g = a.eval(cmyk); |
| 83 | Some(lab_dec(a.bits, [g[0], g[1], g[2]])) |
| 84 | } |
| 85 | |
| 86 | // A neutral of the lightness of `lab`, no lighter than L* 50, as XYZ. |
| 87 | fn neutral(lab: [f64; 3]) -> [f64; 3] { |
| 88 | lab_to_xyz([lab[0].min(50.0), 0.0, 0.0]) |
| 89 | } |
| 90 | |
| 91 | /// The XYZ of a device black that is the darkest colorant of a matrix or grey profile. |
| 92 | pub fn dark_colorant(xyz: [f64; 3]) -> [f64; 3] { |
| 93 | neutral(xyz_to_lab(xyz)) |
| 94 | } |
| 95 | |
| 96 | // The darkest colorant of the input table for `tag`: all four inks at full strength. |
| 97 | fn darker(p: &Profile, tag: usize) -> [f64; 3] { |
| 98 | match p.a2b[tag].as_ref() { |
| 99 | Some(a) => { |
| 100 | let g = a.eval([1.0; 4]); |
| 101 | dark_colorant(lab_to_xyz(lab_dec(a.bits, [g[0], g[1], g[2]]))) |
| 102 | }, |
| 103 | None => [0.0; 3], |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Lab black through the perceptual output table and back: the black the ink limits allow. |
| 108 | fn perceptual_black(p: &Profile) -> [f64; 3] { |
| 109 | if p.a2b[0].is_none() { |
| 110 | return [0.0; 3]; |
| 111 | } |
| 112 | match round(p, 0, [0.0; 3]) { |
| 113 | Some(l) => neutral(l), |
| 114 | None => [0.0; 3], |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn linked(p: &Profile) -> bool { |
| 119 | matches!(p.head.class, Class::Link | Class::Abstract | Class::Named) |
| 120 | } |
| 121 | |
| 122 | /// The black point of a CMYK lookup-table profile as a source. |
| 123 | pub fn cmyk_black_src(p: &Profile, intent: Intent) -> [f64; 3] { |
| 124 | if linked(p) || intent == Intent::Absolute { |
| 125 | return [0.0; 3]; |
| 126 | } |
| 127 | if p.head.major >= 4 && matches!(intent, Intent::Perceptual | Intent::Saturation) { |
| 128 | return V4_BLACK; |
| 129 | } |
| 130 | if intent == Intent::Relative && p.head.class == Class::Output && p.head.space == Space::Cmyk { |
| 131 | return perceptual_black(p); |
| 132 | } |
| 133 | darker(p, intent.tag()) |
| 134 | } |
| 135 | |
| 136 | // The least squares quadratic through (x, y), and the root of it that the black point is. |
| 137 | fn quad_root(x: &[f64], y: &[f64]) -> f64 { |
| 138 | let n = x.len(); |
| 139 | if n < 4 { |
| 140 | return 0.0; |
| 141 | } |
| 142 | let (mut s1, mut s2, mut s3, mut s4) = (0.0, 0.0, 0.0, 0.0); |
| 143 | let (mut t0, mut t1, mut t2) = (0.0, 0.0, 0.0); |
| 144 | for i in 0..n { |
| 145 | let (a, b) = (x[i], y[i]); |
| 146 | s1 += a; |
| 147 | s2 += a * a; |
| 148 | s3 += a * a * a; |
| 149 | s4 += a * a * a * a; |
| 150 | t0 += b; |
| 151 | t1 += b * a; |
| 152 | t2 += b * a * a; |
| 153 | } |
| 154 | let m = [[s4, s3, s2], [s3, s2, s1], [s2, s1, n as f64]]; |
| 155 | let r = match inv(&m) { |
| 156 | Ok(i) => mul_v(&i, [t2, t1, t0]), |
| 157 | Err(_) => return 0.0, |
| 158 | }; |
| 159 | let (a, b, c) = (r[0], r[1], r[2]); |
| 160 | if a.abs() < 1e-10 { |
| 161 | return 0.0f64.min(50.0f64.max(-c / b)); |
| 162 | } |
| 163 | let d = b * b - 4.0 * a * c; |
| 164 | if d <= 0.0 { |
| 165 | return 0.0; |
| 166 | } |
| 167 | ((-b + d.sqrt()) / (2.0 * a)).min(50.0).max(0.0) |
| 168 | } |
| 169 | |
| 170 | /// The black point of a CMYK lookup-table profile as a destination. |
| 171 | pub fn cmyk_black_dst(p: &Profile, intent: Intent) -> [f64; 3] { |
| 172 | if linked(p) || intent == Intent::Absolute { |
| 173 | return [0.0; 3]; |
| 174 | } |
| 175 | if p.head.major >= 4 && matches!(intent, Intent::Perceptual | Intent::Saturation) { |
| 176 | return V4_BLACK; |
| 177 | } |
| 178 | let tag = intent.tag(); |
| 179 | if p.b2a[tag].is_none() { |
| 180 | return cmyk_black_src(p, intent); |
| 181 | } |
| 182 | let rel = intent == Intent::Relative; |
| 183 | let ini = if rel { xyz_to_lab(cmyk_black_src(p, intent)) } else { [0.0; 3] }; |
| 184 | let (mut ins, mut outs) = ([0.0f64; 256], [0.0f64; 256]); |
| 185 | for l in 0..256 { |
| 186 | let lab = [l as f64 * 100.0 / 255.0, ini[1].clamp(-50.0, 50.0), ini[2].clamp(-50.0, 50.0)]; |
| 187 | ins[l] = lab[0]; |
| 188 | outs[l] = match round(p, tag, lab) { |
| 189 | Some(o) => o[0], |
| 190 | None => return [0.0; 3], |
| 191 | }; |
| 192 | } |
| 193 | for l in (1..255).rev() { |
| 194 | outs[l] = outs[l].min(outs[l + 1]); |
| 195 | } |
| 196 | if !(outs[0] < outs[255]) { |
| 197 | return [0.0; 3]; |
| 198 | } |
| 199 | let (lo, hi) = (outs[0], outs[255]); |
| 200 | if rel && (0..256).all(|l| ins[l] <= lo + 0.2 * (hi - lo) || (ins[l] - outs[l]).abs() < 4.0) { |
| 201 | return lab_to_xyz(ini); |
| 202 | } |
| 203 | let (from, to) = if rel { (0.1, 0.5) } else { (0.03, 0.25) }; |
| 204 | let (mut px, mut py) = (Vec::new(), Vec::new()); |
| 205 | for l in 0..256 { |
| 206 | let y = (outs[l] - lo) / (hi - lo); |
| 207 | if y >= from && y < to { |
| 208 | px.push(ins[l]); |
| 209 | py.push(y); |
| 210 | } |
| 211 | } |
| 212 | if px.len() < 3 { |
| 213 | return [0.0; 3]; |
| 214 | } |
| 215 | let l = quad_root(&px, &py).max(0.0); |
| 216 | lab_to_xyz([l, ini[1], ini[2]]) |
| 217 | } |
| 218 | |
| 219 | #[cfg(test)] |
| 220 | mod tests { |
| 221 | use super::*; |
| 222 | |
| 223 | #[test] |
| 224 | fn lab_and_xyz_are_inverses_and_d50_is_white() { |
| 225 | let w = xyz_to_lab(D50); |
| 226 | assert!((w[0] - 100.0).abs() < 1e-9 && w[1].abs() < 1e-9 && w[2].abs() < 1e-9); |
| 227 | for xyz in [[0.2, 0.3, 0.1], [0.001, 0.0005, 0.002], [0.9, 0.95, 0.7]] { |
| 228 | let b = lab_to_xyz(xyz_to_lab(xyz)); |
| 229 | for i in 0..3 { |
| 230 | assert!((b[i] - xyz[i]).abs() < 1e-12, "{:?} came back as {:?}", xyz, b); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn the_encodings_round_trip_and_place_white_and_neutral() { |
| 237 | let w16 = lab_enc(16, [100.0, 0.0, 0.0]); |
| 238 | assert!((w16[0] * 65535.0 - 65280.0).abs() < 1e-9 && (w16[1] * 65535.0 - 32768.0).abs() < 1e-9); |
| 239 | let w8 = lab_enc(8, [100.0, 0.0, 0.0]); |
| 240 | assert!((w8[0] - 1.0).abs() < 1e-12 && (w8[1] * 255.0 - 128.0).abs() < 1e-9); |
| 241 | for bits in [8u8, 16] { |
| 242 | let l = [47.0, -33.0, 21.5]; |
| 243 | let b = lab_dec(bits, lab_enc(bits, l)); |
| 244 | for i in 0..3 { |
| 245 | assert!((b[i] - l[i]).abs() < 1e-9); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |