| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use ec_core::color::Matrix; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | pub fn remap(from: Matrix, to: Matrix, y: &mut [u8], u: &mut [u8], v: &mut [u8], width: usize) { |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | let coeffs = match (from, to) { |
| 29 | (Matrix::Bt601, Matrix::Bt709) => [-30, -53, 261, 29, 19, 262], |
| 30 | |
| 31 | (Matrix::Bt709, Matrix::Bt601) => [25, 49, 253, -28, -19, 252], |
| 32 | _ => return, |
| 33 | }; |
| 34 | let [yb, yr, ub, ur, vb, vr] = coeffs; |
| 35 | let cw = width.div_ceil(2); |
| 36 | for (i, y) in y.iter_mut().enumerate() { |
| 37 | let (row, col) = (i / width, i % width); |
| 38 | let ci = (row / 2) * cw + col / 2; |
| 39 | let (d, e) = (u[ci] as i32 - 128, v[ci] as i32 - 128); |
| 40 | *y = (*y as i32 + ((yb * d + yr * e + 128) >> 8)).clamp(0, 255) as u8; |
| 41 | } |
| 42 | for (u, v) in u.iter_mut().zip(v) { |
| 43 | let (d, e) = (*u as i32 - 128, *v as i32 - 128); |
| 44 | *u = (128 + ((ub * d + ur * e + 128) >> 8)).clamp(0, 255) as u8; |
| 45 | *v = (128 + ((vb * d + vr * e + 128) >> 8)).clamp(0, 255) as u8; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[cfg(test)] |
| 50 | mod tests { |
| 51 | use super::*; |
| 52 | |
| 53 | |
| 54 | |
| 55 | fn weights(matrix: Matrix) -> (f64, f64) { |
| 56 | match matrix { |
| 57 | Matrix::Bt601 => (0.299, 0.114), |
| 58 | Matrix::Bt709 => (0.2126, 0.0722), |
| 59 | Matrix::Bt2020Ncl => (0.2627, 0.0593), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | fn encode(matrix: Matrix, rgb: [f64; 3]) -> (u8, u8, u8) { |
| 66 | let (kr, kb) = weights(matrix); |
| 67 | let [r, g, b] = rgb.map(|c| c / 255.0); |
| 68 | let luma = kr * r + (1.0 - kr - kb) * g + kb * b; |
| 69 | let round = |x: f64| x.round().clamp(0.0, 255.0) as u8; |
| 70 | ( |
| 71 | round(16.0 + 219.0 * luma), |
| 72 | round(128.0 + 224.0 * 0.5 * (b - luma) / (1.0 - kb)), |
| 73 | round(128.0 + 224.0 * 0.5 * (r - luma) / (1.0 - kr)), |
| 74 | ) |
| 75 | } |
| 76 | |
| 77 | |
| 78 | fn decode(matrix: Matrix, (y, u, v): (u8, u8, u8)) -> [f64; 3] { |
| 79 | let (kr, kb) = weights(matrix); |
| 80 | let luma = (f64::from(y) - 16.0) / 219.0; |
| 81 | let cb = (f64::from(u) - 128.0) / 224.0; |
| 82 | let cr = (f64::from(v) - 128.0) / 224.0; |
| 83 | let r = luma + 2.0 * (1.0 - kr) * cr; |
| 84 | let b = luma + 2.0 * (1.0 - kb) * cb; |
| 85 | let g = (luma - kr * r - kb * b) / (1.0 - kr - kb); |
| 86 | [r, g, b].map(|c| c * 255.0) |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | fn tile((y, u, v): (u8, u8, u8)) -> (Vec<u8>, Vec<u8>, Vec<u8>) { |
| 92 | (vec![y; 4], vec![u], vec![v]) |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | #[test] |
| 100 | fn a_remap_is_the_same_colour_read_by_the_other_matrix() { |
| 101 | let colours = [ |
| 102 | [255.0, 0.0, 0.0], |
| 103 | [0.0, 255.0, 0.0], |
| 104 | [0.0, 0.0, 255.0], |
| 105 | [255.0, 255.0, 0.0], |
| 106 | [18.0, 200.0, 140.0], |
| 107 | [128.0, 128.0, 128.0], |
| 108 | [235.0, 210.0, 30.0], |
| 109 | ]; |
| 110 | for (from, to) in [ |
| 111 | (Matrix::Bt601, Matrix::Bt709), |
| 112 | (Matrix::Bt709, Matrix::Bt601), |
| 113 | ] { |
| 114 | for rgb in colours { |
| 115 | let (mut y, mut u, mut v) = tile(encode(from, rgb)); |
| 116 | remap(from, to, &mut y, &mut u, &mut v, 2); |
| 117 | let out = decode(to, (y[0], u[0], v[0])); |
| 118 | for (want, got) in rgb.iter().zip(&out) { |
| 119 | assert!( |
| 120 | (want - got).abs() <= 2.0, |
| 121 | "{from:?}->{to:?} {rgb:?} came out {out:?}" |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | #[test] |
| 133 | fn a_remap_leaves_the_greys_where_they_are() { |
| 134 | for (from, to) in [ |
| 135 | (Matrix::Bt601, Matrix::Bt709), |
| 136 | (Matrix::Bt709, Matrix::Bt601), |
| 137 | ] { |
| 138 | for luma in [16u8, 128, 235] { |
| 139 | let (mut y, mut u, mut v) = tile((luma, 128, 128)); |
| 140 | remap(from, to, &mut y, &mut u, &mut v, 2); |
| 141 | assert_eq!((y[0], u[0], v[0]), (luma, 128, 128), "{from:?}->{to:?}"); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | #[test] |
| 150 | fn a_same_space_remap_touches_nothing() { |
| 151 | let mut y: Vec<u8> = (0..64 * 48).map(|i| (i % 256) as u8).collect(); |
| 152 | let mut u: Vec<u8> = (0..32 * 24).map(|i| (i % 256) as u8).collect(); |
| 153 | let mut v: Vec<u8> = (0..32 * 24).map(|i| (255 - i % 256) as u8).collect(); |
| 154 | let (before_y, before_u, before_v) = (y.clone(), u.clone(), v.clone()); |
| 155 | for matrix in [Matrix::Bt601, Matrix::Bt709, Matrix::Bt2020Ncl] { |
| 156 | remap(matrix, matrix, &mut y, &mut u, &mut v, 64); |
| 157 | } |
| 158 | assert_eq!((y, u, v), (before_y, before_u, before_v)); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[test] |
| 164 | fn a_remap_round_trips_within_a_code() { |
| 165 | let y: Vec<u8> = (0..64 * 48).map(|i| (16 + i % 220) as u8).collect(); |
| 166 | let u: Vec<u8> = (0..32 * 24).map(|i| (16 + i % 225) as u8).collect(); |
| 167 | let v: Vec<u8> = (0..32 * 24).map(|i| (240 - i % 225) as u8).collect(); |
| 168 | let (mut ry, mut ru, mut rv) = (y.clone(), u.clone(), v.clone()); |
| 169 | remap(Matrix::Bt601, Matrix::Bt709, &mut ry, &mut ru, &mut rv, 64); |
| 170 | remap(Matrix::Bt709, Matrix::Bt601, &mut ry, &mut ru, &mut rv, 64); |
| 171 | let worst = |
| 172 | |a: &[u8], b: &[u8]| a.iter().zip(b).map(|(x, y)| x.abs_diff(*y)).max().unwrap(); |
| 173 | assert!(worst(&y, &ry) <= 2, "luma drifted by {}", worst(&y, &ry)); |
| 174 | assert!(worst(&u, &ru) <= 2, "cb drifted by {}", worst(&u, &ru)); |
| 175 | assert!(worst(&v, &rv) <= 2, "cr drifted by {}", worst(&v, &rv)); |
| 176 | } |
| 177 | } |