oxedyne/fe2o3/fe2o3_graphics/src/icc/grid.rs
4.6 KiB, 1 run
created by r1870400018:68823, 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 image grid: a transform from RGB sampled on a 33-step lattice and interpolated |
| 2 | //! tetrahedrally, so that a bitmap costs one interpolation for each pixel instead of the pipeline. |
| 3 | //! |
| 4 | //! The nodes are the transform's exact result at 33 levels on each axis, red slowest, spaced evenly |
| 5 | //! in the rooted Lab companding of the channel's intensity. Vector colours take the exact path, |
| 6 | //! [`Transform::eval`]; only an image is converted here. |
| 7 | //! |
| 8 | //! Where the destination is CMYK the nodes hold the colour as Lab, in the encoding of the profile's |
| 9 | //! output table, and the table is read at the interpolated Lab. The table is a lattice of its own, |
| 10 | //! with a crease at every node where the inks turn, so a grid of the finished inks misses by up to |
| 11 | //! 15 levels in the darks; a grid of Lab is smooth and misses by a fraction of a level. LittleCMS, |
| 12 | //! which grids the finished inks, misses its own exact path by up to 14 levels. |
| 13 | //! |
| 14 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 15 | //! Anthropic Claude |
| 16 | |
| 17 | use crate::icc::{ |
| 18 | lut::{ |
| 19 | locate, |
| 20 | tetra, |
| 21 | }, |
| 22 | read::{ |
| 23 | Curve, |
| 24 | Lut, |
| 25 | }, |
| 26 | transform::{ |
| 27 | Kind, |
| 28 | Transform, |
| 29 | }, |
| 30 | }; |
| 31 | |
| 32 | use oxedyne_fe2o3_core::prelude::*; |
| 33 | |
| 34 | pub const STEPS: usize = 33; |
| 35 | |
| 36 | const F0: f64 = 16.0 / 116.0; // the Lab companding function at zero |
| 37 | |
| 38 | // The Lab companding function of a linear intensity, scaled to run from 0 to 1 and then rooted, so |
| 39 | // that a lattice even in it crowds the darks more than L* does. The darks are where the pipeline bends |
| 40 | // fastest, and the root brought the worst error over 20000 colours from 3.4 levels to 0.8. |
| 41 | fn comp(t: f64) -> f64 { |
| 42 | let f = if t > 216.0 / 24389.0 { t.cbrt() } else { (24389.0 / 27.0 * t + 16.0) / 116.0 }; |
| 43 | ((f - F0) / (1.0 - F0)).sqrt() |
| 44 | } |
| 45 | |
| 46 | // The linear intensity whose companded value is `w`. |
| 47 | fn uncomp(w: f64) -> f64 { |
| 48 | let f = F0 + w * w * (1.0 - F0); |
| 49 | if f > 6.0 / 29.0 { f * f * f } else { (116.0 * f - 16.0) * 27.0 / 24389.0 } |
| 50 | } |
| 51 | |
| 52 | // Where an input level lies on the lattice axis: the lattice is even in the companded intensity. |
| 53 | fn place(trc: &Curve, x: f64) -> (usize, f64) { |
| 54 | locate(comp(trc.eval(x)), STEPS) |
| 55 | } |
| 56 | |
| 57 | #[derive(Debug)] |
| 58 | pub struct Grid { |
| 59 | nodes: Vec<[f32; 4]>, // STEPS^3 nodes, red slowest |
| 60 | tail: Option<Lut>, // the output table that reads the nodes, where the destination has one |
| 61 | trc: [Curve; 3], // the source's curves, which place a level on each axis |
| 62 | at8: Vec<(usize, f64)>, // the placing of each 8-bit level on each axis |
| 63 | } |
| 64 | |
| 65 | impl Grid { |
| 66 | /// Samples the transform at every node. |
| 67 | /// |
| 68 | /// The nodes are even in the rooted Lab companding of each channel's intensity, so that they |
| 69 | /// lie closest where a colour changes fastest, in the darks. |
| 70 | pub fn build(t: &Transform) -> Self { |
| 71 | let trc = match &t.src().kind { |
| 72 | Kind::Rgb(m) => m.trc.clone(), |
| 73 | _ => [Curve::Identity, Curve::Identity, Curve::Identity], |
| 74 | }; |
| 75 | let k = (STEPS - 1) as f64; |
| 76 | let at: Vec<Vec<f64>> = trc.iter().map(|c| { |
| 77 | (0..STEPS).map(|i| c.inverse(uncomp(i as f64 / k))).collect() |
| 78 | }).collect(); |
| 79 | let mut nodes = Vec::with_capacity(STEPS * STEPS * STEPS); |
| 80 | for r in 0..STEPS { |
| 81 | for g in 0..STEPS { |
| 82 | for b in 0..STEPS { |
| 83 | let v = t.node([at[0][r], at[1][g], at[2][b], 0.0]); |
| 84 | nodes.push([v[0] as f32, v[1] as f32, v[2] as f32, v[3] as f32]); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | let at8 = (0..3).flat_map(|c| { |
| 89 | let trc = &trc; |
| 90 | (0..256).map(move |i| place(&trc[c], i as f64 / 255.0)) |
| 91 | }).collect(); |
| 92 | Self { nodes, tail: t.tail().cloned(), trc, at8 } |
| 93 | } |
| 94 | |
| 95 | /// The interpolated result for an RGB colour of fractions of full scale. |
| 96 | pub fn eval(&self, rgb: [f64; 3]) -> [f32; 4] { |
| 97 | self.read([place(&self.trc[0], rgb[0]), place(&self.trc[1], rgb[1]), place(&self.trc[2], rgb[2])]) |
| 98 | } |
| 99 | |
| 100 | // The result at a placed colour: the lattice cell and the fractions past it on each axis. |
| 101 | fn read(&self, p: [(usize, f64); 3]) -> [f32; 4] { |
| 102 | let (r, g, b) = (p[0], p[1], p[2]); |
| 103 | let base = (r.0 * STEPS + g.0) * STEPS + b.0; |
| 104 | let n = |i: usize| { |
| 105 | let v = self.nodes[i]; |
| 106 | [v[0] as f64, v[1] as f64, v[2] as f64, v[3] as f64] |
| 107 | }; |
| 108 | let v = tetra(n, base, [STEPS * STEPS, STEPS, 1], [r.1, g.1, b.1]); |
| 109 | let v = match &self.tail { |
| 110 | Some(t) => t.eval(v), |
| 111 | None => v, |
| 112 | }; |
| 113 | [v[0] as f32, v[1] as f32, v[2] as f32, v[3] as f32] |
| 114 | } |
| 115 | |
| 116 | /// Converts 8-bit RGB triples; the result holds one colour for each triple. |
| 117 | pub fn convert8(&self, rgb: &[u8]) -> Outcome<Vec<[f32; 4]>> { |
| 118 | if rgb.len() % 3 != 0 { |
| 119 | return Err(err!( |
| 120 | "{} bytes of 8-bit RGB are not a whole number of pixels.", rgb.len(); |
| 121 | Invalid, Input, Size)); |
| 122 | } |
| 123 | Ok(rgb.chunks_exact(3).map(|p| { |
| 124 | self.read([self.at8[p[0] as usize], self.at8[256 + p[1] as usize], self.at8[512 + p[2] as usize]]) |
| 125 | }).collect()) |
| 126 | } |
| 127 | } |