| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | pub const MAX_SAMPLES: usize = 16_384; |
| 12 | |
| 13 | pub const LEVELS: usize = 64; |
| 14 | |
| 15 | pub const VECTOR: usize = 64; |
| 16 | |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq)] |
| 19 | pub struct Scopes { |
| 20 | |
| 21 | pub cols: usize, |
| 22 | |
| 23 | pub wave: Vec<u32>, |
| 24 | |
| 25 | pub parade: [Vec<u32>; 3], |
| 26 | |
| 27 | |
| 28 | pub vector: Vec<u32>, |
| 29 | |
| 30 | hist: [[u32; 256]; 4], |
| 31 | |
| 32 | chroma_sum: [f64; 3], |
| 33 | |
| 34 | pub samples: u32, |
| 35 | } |
| 36 | |
| 37 | |
| 38 | fn luma(r: f32, g: f32, b: f32) -> f32 { |
| 39 | 0.2126 * r + 0.7152 * g + 0.0722 * b |
| 40 | } |
| 41 | |
| 42 | |
| 43 | pub fn compute(bgra: &[u8], width: usize, height: usize, cols: usize) -> Scopes { |
| 44 | let cols = cols.max(1); |
| 45 | let mut s = Scopes { |
| 46 | cols, |
| 47 | wave: vec![0; cols * LEVELS], |
| 48 | parade: [vec![0; cols * LEVELS], vec![0; cols * LEVELS], vec![0; cols * LEVELS]], |
| 49 | vector: vec![0; VECTOR * VECTOR], |
| 50 | hist: [[0; 256]; 4], |
| 51 | chroma_sum: [0.0; 3], |
| 52 | samples: 0, |
| 53 | }; |
| 54 | if width == 0 || height == 0 || bgra.len() < width * height * 4 { |
| 55 | return s; |
| 56 | } |
| 57 | |
| 58 | let step = ((width * height) as f64 / MAX_SAMPLES as f64).sqrt().ceil().max(1.0) as usize; |
| 59 | let level = |v: f32| ((v / 255.0 * LEVELS as f32) as usize).min(LEVELS - 1); |
| 60 | for y in (0..height).step_by(step) { |
| 61 | for x in (0..width).step_by(step) { |
| 62 | let p = &bgra[(y * width + x) * 4..]; |
| 63 | let (b, g, r) = (f32::from(p[0]), f32::from(p[1]), f32::from(p[2])); |
| 64 | let l = luma(r, g, b); |
| 65 | let col = x * cols / width; |
| 66 | s.wave[level(l) * cols + col] += 1; |
| 67 | for (c, v) in [r, g, b].into_iter().enumerate() { |
| 68 | s.parade[c][level(v) * cols + col] += 1; |
| 69 | s.hist[c + 1][v as usize] += 1; |
| 70 | } |
| 71 | s.hist[0][(l.round() as usize).min(255)] += 1; |
| 72 | let cb = (b - l) / 1.8556 / 255.0; |
| 73 | let cr = (r - l) / 1.5748 / 255.0; |
| 74 | let cell = |v: f32| (((v + 0.5) * VECTOR as f32) as usize).min(VECTOR - 1); |
| 75 | s.vector[(VECTOR - 1 - cell(cr)) * VECTOR + cell(cb)] += 1; |
| 76 | s.chroma_sum[0] += f64::from(cb); |
| 77 | s.chroma_sum[1] += f64::from(cr); |
| 78 | s.chroma_sum[2] += f64::from((cb * cb + cr * cr).sqrt()); |
| 79 | s.samples += 1; |
| 80 | } |
| 81 | } |
| 82 | s |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 87 | pub struct Spread { |
| 88 | pub min: f32, |
| 89 | pub p5: f32, |
| 90 | pub median: f32, |
| 91 | pub p95: f32, |
| 92 | pub max: f32, |
| 93 | pub mean: f32, |
| 94 | } |
| 95 | |
| 96 | |
| 97 | #[derive(Debug, Clone, PartialEq)] |
| 98 | pub struct Summary { |
| 99 | |
| 100 | pub spread: [Spread; 4], |
| 101 | |
| 102 | pub crushed: f32, |
| 103 | pub clipped: f32, |
| 104 | |
| 105 | pub bands: Vec<[f32; 3]>, |
| 106 | |
| 107 | pub saturation: f32, |
| 108 | |
| 109 | |
| 110 | pub cast_angle: f32, |
| 111 | pub cast: f32, |
| 112 | } |
| 113 | |
| 114 | fn pct(i: usize) -> f32 { |
| 115 | i as f32 / 255.0 * 100.0 |
| 116 | } |
| 117 | |
| 118 | fn spread(h: &[u32; 256], n: u32) -> Spread { |
| 119 | let at = |q: f64| { |
| 120 | let want = (q * f64::from(n)).ceil().max(1.0) as u64; |
| 121 | let mut acc = 0u64; |
| 122 | for (i, &c) in h.iter().enumerate() { |
| 123 | acc += u64::from(c); |
| 124 | if acc >= want { |
| 125 | return pct(i); |
| 126 | } |
| 127 | } |
| 128 | 100.0 |
| 129 | }; |
| 130 | let mean = h.iter().enumerate().map(|(i, &c)| i as f64 * f64::from(c)).sum::<f64>() / f64::from(n.max(1)); |
| 131 | Spread { |
| 132 | min: h.iter().position(|&c| c > 0).map_or(0.0, pct), |
| 133 | p5: at(0.05), |
| 134 | median: at(0.5), |
| 135 | p95: at(0.95), |
| 136 | max: h.iter().rposition(|&c| c > 0).map_or(0.0, pct), |
| 137 | mean: (mean / 255.0 * 100.0) as f32, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl Scopes { |
| 142 | |
| 143 | pub fn summary(&self, bands: usize) -> Summary { |
| 144 | let n = self.samples.max(1); |
| 145 | let spread = [0, 1, 2, 3].map(|c| spread(&self.hist[c], self.samples)); |
| 146 | let share = |r: std::ops::RangeInclusive<usize>| { |
| 147 | self.hist[0][r].iter().map(|&c| f64::from(c)).sum::<f64>() as f32 / n as f32 * 100.0 |
| 148 | }; |
| 149 | let bands = bands.clamp(1, self.cols); |
| 150 | let band_rows: Vec<[f32; 3]> = (0..bands) |
| 151 | .map(|b| { |
| 152 | let (c0, c1) = (b * self.cols / bands, (b + 1) * self.cols / bands); |
| 153 | let mut h = vec![0u32; LEVELS]; |
| 154 | for (lvl, slot) in h.iter_mut().enumerate() { |
| 155 | *slot = (c0..c1).map(|c| self.wave[lvl * self.cols + c]).sum(); |
| 156 | } |
| 157 | let total: u32 = h.iter().sum(); |
| 158 | let at = |q: f32| { |
| 159 | let want = (q * total as f32).ceil().max(1.0) as u32; |
| 160 | let mut acc = 0; |
| 161 | for (lvl, &c) in h.iter().enumerate() { |
| 162 | acc += c; |
| 163 | if acc >= want { |
| 164 | return (lvl as f32 + 0.5) / LEVELS as f32 * 100.0; |
| 165 | } |
| 166 | } |
| 167 | 100.0 |
| 168 | }; |
| 169 | [at(0.05), at(0.5), at(0.95)] |
| 170 | }) |
| 171 | .collect(); |
| 172 | let (cb, cr) = (self.chroma_sum[0] / f64::from(n), self.chroma_sum[1] / f64::from(n)); |
| 173 | Summary { |
| 174 | spread, |
| 175 | crushed: share(0..=5), |
| 176 | clipped: share(250..=255), |
| 177 | bands: band_rows, |
| 178 | saturation: (self.chroma_sum[2] / f64::from(n) * 200.0) as f32, |
| 179 | cast_angle: (cr.atan2(cb).to_degrees() as f32).rem_euclid(360.0), |
| 180 | cast: ((cb * cb + cr * cr).sqrt() * 200.0) as f32, |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | pub fn hue_word(angle: f32) -> &'static str { |
| 187 | |
| 188 | const HUES: [(f32, &str); 6] = [ |
| 189 | (103.0, "red"), |
| 190 | (175.0, "yellow"), |
| 191 | (230.0, "green"), |
| 192 | (283.0, "cyan"), |
| 193 | (355.0, "blue"), |
| 194 | (50.0, "magenta"), |
| 195 | ]; |
| 196 | let dist = |a: f32| { |
| 197 | let d = (a - angle).rem_euclid(360.0); |
| 198 | d.min(360.0 - d) |
| 199 | }; |
| 200 | HUES.iter().min_by(|a, b| dist(a.0).total_cmp(&dist(b.0))).map_or("grey", |h| h.1) |
| 201 | } |
| 202 | |
| 203 | #[cfg(test)] |
| 204 | mod tests { |
| 205 | use super::*; |
| 206 | |
| 207 | fn solid(w: usize, h: usize, bgr: [u8; 3]) -> Vec<u8> { |
| 208 | (0..w * h).flat_map(|_| [bgr[0], bgr[1], bgr[2], 255]).collect() |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn grey_frame_sits_on_one_level_with_no_chroma() { |
| 213 | let s = compute(&solid(320, 180, [128, 128, 128]), 320, 180, 32); |
| 214 | assert!(s.samples > 1000 && s.samples as usize <= MAX_SAMPLES); |
| 215 | let sum = s.summary(4); |
| 216 | for sp in sum.spread { |
| 217 | assert!((sp.median - 50.2).abs() < 0.5 && sp.min == sp.max, "{sp:?}"); |
| 218 | } |
| 219 | assert!(sum.saturation < 0.01 && sum.cast < 0.01); |
| 220 | assert_eq!(s.vector.iter().filter(|&&c| c > 0).count(), 1, "grey is one dot at the centre"); |
| 221 | assert_eq!(sum.bands.len(), 4); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn a_warm_frame_reads_as_a_red_ish_cast_and_red_above_blue() { |
| 226 | let s = compute(&solid(64, 64, [60, 120, 200]), 64, 64, 16); |
| 227 | let sum = s.summary(2); |
| 228 | assert!(sum.spread[1].mean > sum.spread[3].mean + 30.0); |
| 229 | assert!(sum.cast > 10.0); |
| 230 | let w = hue_word(sum.cast_angle); |
| 231 | assert!(w == "red" || w == "yellow", "{w} at {}", sum.cast_angle); |
| 232 | } |
| 233 | |
| 234 | #[test] |
| 235 | fn black_and_white_halves_report_crush_and_clip() { |
| 236 | let (w, h) = (100, 10); |
| 237 | let bgra: Vec<u8> = (0..w * h).flat_map(|i| if i % w < 50 { [0, 0, 0, 255] } else { [255; 4] }).collect(); |
| 238 | let sum = compute(&bgra, w, h, 10).summary(2); |
| 239 | assert!((sum.crushed - 50.0).abs() < 2.0 && (sum.clipped - 50.0).abs() < 2.0, "{sum:?}"); |
| 240 | assert!(sum.bands[0][2] < 5.0 && sum.bands[1][0] > 95.0, "left band dark, right band bright"); |
| 241 | assert!(compute(&[], 0, 0, 8).summary(3).bands.len() == 3); |
| 242 | } |
| 243 | } |