colconv 0.1.0

SIMD-dispatched color-conversion kernels covering the FFmpeg AVPixelFormat space, with a Sink-based API so consumers pick which derived outputs (RGB / Luma / HSV / custom) they want without paying for the ones they don't.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use mediaframe::{
  PixelSink,
  frame::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance},
};

use super::*;
use crate::{
  frame::Bayer12Frame,
  row::{bayer16_to_rgb_row, bayer16_to_rgb_u16_row},
};
use core::convert::Infallible;

/// Sink that walks each `BayerRow16<BITS>` through the public
/// u8-output dispatcher with SIMD off.
struct CaptureRgbU8<'a, const BITS: u32> {
  out: &'a mut [u8],
  width: u32,
}

impl<const BITS: u32> PixelSink for CaptureRgbU8<'_, BITS> {
  type Input<'b> = BayerRow16<'b, BITS>;
  type Error = Infallible;

  fn begin_frame(&mut self, width: u32, _height: u32) -> Result<(), Self::Error> {
    self.width = width;
    Ok(())
  }

  fn process(&mut self, row: BayerRow16<'_, BITS>) -> Result<(), Self::Error> {
    let r = row.row();
    let w = self.width as usize;
    let off = r * w * 3;
    let dst = &mut self.out[off..off + 3 * w];
    bayer16_to_rgb_row::<BITS>(
      row.above(),
      row.mid(),
      row.below(),
      row.row_parity(),
      row.pattern(),
      row.demosaic(),
      row.m(),
      dst,
      false,
    );
    Ok(())
  }
}

impl<const BITS: u32> BayerSink16<BITS> for CaptureRgbU8<'_, BITS> {}

/// Sink that walks each `BayerRow16<BITS>` through the public
/// u16-output dispatcher with SIMD off.
struct CaptureRgbU16<'a, const BITS: u32> {
  out: &'a mut [u16],
  width: u32,
}

impl<const BITS: u32> PixelSink for CaptureRgbU16<'_, BITS> {
  type Input<'b> = BayerRow16<'b, BITS>;
  type Error = Infallible;

  fn begin_frame(&mut self, width: u32, _height: u32) -> Result<(), Self::Error> {
    self.width = width;
    Ok(())
  }

  fn process(&mut self, row: BayerRow16<'_, BITS>) -> Result<(), Self::Error> {
    let r = row.row();
    let w = self.width as usize;
    let off = r * w * 3;
    let dst = &mut self.out[off..off + 3 * w];
    bayer16_to_rgb_u16_row::<BITS>(
      row.above(),
      row.mid(),
      row.below(),
      row.row_parity(),
      row.pattern(),
      row.demosaic(),
      row.m(),
      dst,
      false,
    );
    Ok(())
  }
}

impl<const BITS: u32> BayerSink16<BITS> for CaptureRgbU16<'_, BITS> {}

/// Build a 12-bit low-packed RGGB Bayer plane from per-channel
/// nominal values (each 0..=4095). Bayer16 is low-packed: samples
/// occupy the low 12 bits of each `u16`, no shift required.
fn solid_rggb_12bit(width: u32, height: u32, r: u16, g: u16, b: u16) -> std::vec::Vec<u16> {
  let w = width as usize;
  let h = height as usize;
  let mut data = std::vec![0u16; w * h];
  for y in 0..h {
    for x in 0..w {
      let v = match (y & 1, x & 1) {
        (0, 0) => r,
        (0, 1) => g,
        (1, 0) => g,
        (1, 1) => b,
        _ => unreachable!(),
      };
      data[y * w + x] = v;
    }
  }
  data
}

fn assert_full_frame_u8(rgb: &[u8], w: u32, h: u32, expect: (u8, u8, u8)) {
  let w = w as usize;
  let h = h as usize;
  for y in 0..h {
    for x in 0..w {
      let i = (y * w + x) * 3;
      assert_eq!(rgb[i], expect.0, "u8 px ({x},{y}) R");
      assert_eq!(rgb[i + 1], expect.1, "u8 px ({x},{y}) G");
      assert_eq!(rgb[i + 2], expect.2, "u8 px ({x},{y}) B");
    }
  }
}

fn assert_full_frame_u16(rgb: &[u16], w: u32, h: u32, expect: (u16, u16, u16)) {
  let w = w as usize;
  let h = h as usize;
  for y in 0..h {
    for x in 0..w {
      let i = (y * w + x) * 3;
      assert_eq!(rgb[i], expect.0, "u16 px ({x},{y}) R");
      assert_eq!(rgb[i + 1], expect.1, "u16 px ({x},{y}) G");
      assert_eq!(rgb[i + 2], expect.2, "u16 px ({x},{y}) B");
    }
  }
}

#[test]
fn bayer12_solid_red_rggb_yields_u8_red_full_frame() {
  // 12-bit max = 4095. R = 4095 (white at this channel), G = B = 0.
  // Mirror-by-2 boundary handling means every output pixel
  // matches, including borders and corners.
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb_12bit(w, h, 4095, 0, 0);
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  assert_full_frame_u8(&rgb, w, h, (255, 0, 0));
}

#[test]
fn bayer12_solid_red_rggb_yields_u16_red_full_frame() {
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb_12bit(w, h, 4095, 0, 0);
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut sink = CaptureRgbU16::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  assert_full_frame_u16(&rgb, w, h, (4095, 0, 0));
}

#[test]
fn bayer12_uniform_value_yields_uniform_u8_output() {
  // Every sample = 2048 (low-packed 12-bit midgray). u8 output:
  // 2048 / 4095 * 255 ≈ 127.53 → 128 everywhere (uniform input
  // so edge clamping doesn't shift the value).
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![2048u16; (w * h) as usize];
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  for &c in &rgb {
    assert!((c as i32 - 128).abs() <= 1, "got {c}");
  }
}

#[test]
fn bayer12_uniform_value_yields_uniform_u16_output() {
  // Every sample = 4095 (max 12-bit low-packed). u16 output
  // should be 4095 (low-packed full white).
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![4095u16; (w * h) as usize];
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut sink = CaptureRgbU16::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  for &c in &rgb {
    assert_eq!(c, 4095);
  }
}

#[test]
fn bayer10_low_packed_white_yields_full_scale_u8() {
  // 10-bit low-packed white (1023). u8 output should be 255.
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![1023u16; (w * h) as usize];
  let frame = crate::frame::Bayer10Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<10> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  for &c in &rgb {
    assert_eq!(c, 255, "10-bit low-packed white must scale to u8 255");
  }
}

#[test]
fn bayer14_low_packed_white_yields_full_scale_u16() {
  // 14-bit low-packed white (16383). u16 output should be 16383.
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![16383u16; (w * h) as usize];
  let frame = crate::frame::Bayer14Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut sink = CaptureRgbU16::<14> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  for &c in &rgb {
    assert_eq!(c, 16383, "14-bit low-packed white must stay 16383");
  }
}

/// `Bayer12Frame::try_new` rejects out-of-range samples as
/// `BayerFrame16Error::SampleOutOfRange` — a recoverable
/// `Result::Err`, not a panic. Sample-range validation is now
/// part of standard frame construction so the walker is fully
/// fallible.
#[test]
fn bayer12_try_new_rejects_sample_above_max() {
  let (w, h) = (4u32, 2u32);
  let mut raw = std::vec![100u16; (w * h) as usize];
  raw[3] = 4096; // just above 12-bit max
  let e = Bayer12Frame::try_new(&raw, w, h, w).unwrap_err();
  let crate::frame::BayerFrame16Error::SampleOutOfRange(p) = e else {
    panic!("expected SampleOutOfRange, got {e:?}");
  };
  assert_eq!(p.index(), 3);
  assert_eq!(p.value(), 4096);
  assert_eq!(p.max_valid(), 4095);
}

/// MSB-aligned 12-bit midgray (e.g., `2048 << 4 = 0x8000`) is
/// exactly the common packing-mismatch bug, where a caller forgot
/// to right-shift before constructing the `Bayer12Frame`. Caught
/// at construction as `Result::Err` instead of a runtime panic.
#[test]
fn bayer12_try_new_rejects_msb_aligned_input() {
  let (w, h) = (4u32, 2u32);
  let raw = std::vec![0x8000u16; (w * h) as usize]; // MSB-aligned 12-bit midgray
  let e = Bayer12Frame::try_new(&raw, w, h, w).unwrap_err();
  let crate::frame::BayerFrame16Error::SampleOutOfRange(p) = e else {
    panic!("expected SampleOutOfRange, got {e:?}");
  };
  assert_eq!(p.value(), 0x8000);
  assert_eq!(p.max_valid(), 4095);
}

/// A Bayer12 frame with a bad sample in a *later* row would
/// otherwise trigger a runtime panic mid-walk; `try_new` catches
/// the bad sample upfront and returns `Err`, so the user's output
/// buffer is never touched. (The `bayer16_to` walker cannot be
/// reached with bad sample data because no `BayerFrame16<BITS>`
/// value can exist with out-of-range samples.)
#[test]
fn bayer12_try_new_rejects_bad_sample_in_later_row() {
  let (w, h) = (4u32, 8u32);
  let mut raw = std::vec![100u16; (w * h) as usize];
  let off = (6 * w) as usize + 2;
  raw[off] = 4096; // exceeds 12-bit max
  let e = Bayer12Frame::try_new(&raw, w, h, w).unwrap_err();
  let crate::frame::BayerFrame16Error::SampleOutOfRange(p) = e else {
    panic!("expected SampleOutOfRange, got {e:?}");
  };
  assert_eq!(p.value(), 4096);
  assert_eq!(p.max_valid(), 4095);
}

/// A valid padded RAW buffer (`stride > width`) with **stale high
/// bits in the row padding** must NOT trip the upfront pre-pass.
/// The walker only reads the active per-row region (`r * stride ..
/// r * stride + width`) so padding bytes are out of scope.
#[test]
fn bayer12_walker_accepts_padded_stride_with_dirty_padding() {
  let w: u32 = 4;
  let h: u32 = 4;
  let stride: u32 = 8; // padding = 4 samples per row
  let mut raw = std::vec![100u16; (stride * h) as usize];
  // Fill the padding with stale high bits (would trigger the
  // upfront panic if validated). Active region (cols 0..4) is
  // valid 12-bit data.
  for r in 0..(h as usize) {
    for c in 4..(stride as usize) {
      raw[r * stride as usize + c] = 0xFFFF;
    }
  }
  let frame = Bayer12Frame::try_new(&raw, w, h, stride).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  // Sanity: kernel ran without panicking. Output content is
  // not the focus; the test is the absence of panic.
}

/// Companion regression: trailing backing storage past
/// `(h - 1) * stride + width` with junk must NOT trip the
/// pre-pass either — the walker doesn't read past the last
/// active row.
#[test]
fn bayer12_walker_accepts_overlong_slice_with_trailing_junk() {
  let w: u32 = 4;
  let h: u32 = 2;
  let stride: u32 = 4;
  // Backing storage is twice the declared geometry; trailing
  // half is filled with values that would trip a wholesale
  // scan.
  let mut raw = std::vec![100u16; (stride * h * 2) as usize];
  for v in raw.iter_mut().skip((stride * h) as usize) {
    *v = 0xFFFF;
  }
  let frame = Bayer12Frame::try_new(&raw, w, h, stride).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<12> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
}

/// At BITS=16 every `u16` is valid; the dispatcher's bad-bit
/// mask is zero so the check is a no-op and 0xFFFF passes.
#[test]
fn bayer16bit_dispatcher_accepts_full_u16_range() {
  let (w, h) = (4u32, 2u32);
  let raw = std::vec![0xFFFFu16; (w * h) as usize];
  let frame = crate::frame::Bayer16Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sink = CaptureRgbU8::<16> {
    out: &mut rgb,
    width: 0,
  };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  // Solid 0xFFFF saturates to 255 on every channel.
  for &c in &rgb {
    assert_eq!(c, 255);
  }
}

#[test]
fn bayer12_walker_calls_sink_once_per_row() {
  struct CountSink<const BITS: u32> {
    rows: u32,
  }
  impl<const BITS: u32> PixelSink for CountSink<BITS> {
    type Input<'a> = BayerRow16<'a, BITS>;
    type Error = Infallible;
    fn process(&mut self, _row: BayerRow16<'_, BITS>) -> Result<(), Self::Error> {
      self.rows += 1;
      Ok(())
    }
  }
  impl<const BITS: u32> BayerSink16<BITS> for CountSink<BITS> {}

  let (w, h) = (8u32, 6u32);
  let raw = std::vec![0u16; (w * h) as usize];
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut sink = CountSink::<12> { rows: 0 };
  bayer16_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sink,
  )
  .unwrap();
  assert_eq!(sink.rows, h);
}