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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Tier 10 sinker integration tests — planar GBR (`Gbrp` / `Gbrap`).

use super::*;
use crate::sinker::MixedSinker;

// ---- shared helpers ----------------------------------------------------

/// Build three planar G/B/R planes from a packed-RGB seed buffer (the
/// inverse of `gbr_to_rgb_row`). Returns `(g, b, r)` plane buffers, all
/// of length `width * height`.
fn planes_from_packed_rgb(rgb: &[u8], width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
  let n = width * height;
  let mut g = std::vec![0u8; n];
  let mut b = std::vec![0u8; n];
  let mut r = std::vec![0u8; n];
  for i in 0..n {
    r[i] = rgb[i * 3];
    g[i] = rgb[i * 3 + 1];
    b[i] = rgb[i * 3 + 2];
  }
  (g, b, r)
}

/// Random packed RGB seed (3 bytes per pixel).
fn random_rgb(width: usize, height: usize, seed: u32) -> Vec<u8> {
  let mut buf = std::vec![0u8; width * height * 3];
  pseudo_random_u8(&mut buf, seed);
  buf
}

/// Random alpha plane.
fn random_alpha(width: usize, height: usize, seed: u32) -> Vec<u8> {
  let mut buf = std::vec![0u8; width * height];
  pseudo_random_u8(&mut buf, seed);
  buf
}

// ---- Gbrp tests --------------------------------------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_with_rgb_reorders_planes_to_packed_rgb() {
  let w = 16usize;
  let h = 4usize;
  // Solid colour: R=200, G=100, B=50.
  let mut g = std::vec![100u8; w * h];
  let mut b = std::vec![50u8; w * h];
  let mut r = std::vec![200u8; w * h];
  // Touch one pixel to make sure we're not just memcpying a constant.
  g[7] = 33;
  b[7] = 44;
  r[7] = 55;

  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let mut rgb_out = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Gbrp>::new(w, h)
    .with_rgb(&mut rgb_out)
    .unwrap();
  gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  for i in 0..w * h {
    assert_eq!(rgb_out[i * 3], r[i], "R px {i}");
    assert_eq!(rgb_out[i * 3 + 1], g[i], "G px {i}");
    assert_eq!(rgb_out[i * 3 + 2], b[i], "B px {i}");
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_with_rgba_appends_opaque_alpha() {
  let w = 16usize;
  let h = 4usize;
  let g = std::vec![100u8; w * h];
  let b = std::vec![50u8; w * h];
  let r = std::vec![200u8; w * h];
  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let mut rgba = std::vec![0u8; w * h * 4];
  let mut sink = MixedSinker::<Gbrp>::new(w, h).with_rgba(&mut rgba).unwrap();
  gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  for px in rgba.chunks(4) {
    assert_eq!(px, [200, 100, 50, 0xFF]);
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_planar_parity_with_rgb24() {
  // Convert the same pixel data through both the Rgb24 and Gbrp source
  // paths; outputs (RGB, RGBA, luma, HSV) must be byte-identical.
  let w = 16usize;
  let h = 8usize;
  let rgb_seed = random_rgb(w, h, 0x1234_5678);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);

  // ---- Rgb24 reference outputs ----
  let rgb24 = Rgb24Frame::try_new(&rgb_seed, w as u32, h as u32, (w * 3) as u32).unwrap();
  let mut rgb_ref = std::vec![0u8; w * h * 3];
  let mut rgba_ref = std::vec![0u8; w * h * 4];
  let mut luma_ref = std::vec![0u8; w * h];
  let mut h_ref = std::vec![0u8; w * h];
  let mut s_ref = std::vec![0u8; w * h];
  let mut v_ref = std::vec![0u8; w * h];
  {
    let mut sink = MixedSinker::<Rgb24>::new(w, h)
      .with_rgb(&mut rgb_ref)
      .unwrap()
      .with_rgba(&mut rgba_ref)
      .unwrap()
      .with_luma(&mut luma_ref)
      .unwrap()
      .with_hsv(&mut h_ref, &mut s_ref, &mut v_ref)
      .unwrap();
    rgb24_to(&rgb24, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  // ---- Gbrp outputs from the same pixel data ----
  let gbrp =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();
  let mut rgb_g = std::vec![0u8; w * h * 3];
  let mut rgba_g = std::vec![0u8; w * h * 4];
  let mut luma_g = std::vec![0u8; w * h];
  let mut h_g = std::vec![0u8; w * h];
  let mut s_g = std::vec![0u8; w * h];
  let mut v_g = std::vec![0u8; w * h];
  {
    let mut sink = MixedSinker::<Gbrp>::new(w, h)
      .with_rgb(&mut rgb_g)
      .unwrap()
      .with_rgba(&mut rgba_g)
      .unwrap()
      .with_luma(&mut luma_g)
      .unwrap()
      .with_hsv(&mut h_g, &mut s_g, &mut v_g)
      .unwrap();
    gbrp_to(&gbrp, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  assert_eq!(rgb_g, rgb_ref, "RGB mismatch Gbrp vs Rgb24");
  assert_eq!(rgba_g, rgba_ref, "RGBA mismatch Gbrp vs Rgb24");
  assert_eq!(luma_g, luma_ref, "luma mismatch Gbrp vs Rgb24");
  assert_eq!(h_g, h_ref, "H mismatch Gbrp vs Rgb24");
  assert_eq!(s_g, s_ref, "S mismatch Gbrp vs Rgb24");
  assert_eq!(v_g, v_ref, "V mismatch Gbrp vs Rgb24");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_with_luma_u16_zero_extends_u8_luma() {
  let w = 16usize;
  let h = 4usize;
  let rgb_seed = random_rgb(w, h, 0xABCD_EF01);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let mut luma_u8 = std::vec![0u8; w * h];
  let mut luma_u16 = std::vec![0u16; w * h];
  let mut sink = MixedSinker::<Gbrp>::new(w, h)
    .with_luma(&mut luma_u8)
    .unwrap()
    .with_luma_u16(&mut luma_u16)
    .unwrap();
  gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  for i in 0..w * h {
    assert_eq!(luma_u16[i], luma_u8[i] as u16, "px {i}");
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_with_luma_u16_wide_row_no_alloc_regression() {
  // Regression for the prior `STACK_CAP = 8192` heap-fallback path:
  // verify a wider row produces the same byte-zero-extended luma as a
  // narrower row would, exercising the direct `rgb_to_luma_u16_row`
  // path that replaced the per-row `Vec<u8>` allocation.
  let w = 9000usize;
  let h = 1usize;
  let rgb_seed = random_rgb(w, h, 0x1234_5678);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let mut luma_u8 = std::vec![0u8; w * h];
  let mut luma_u16 = std::vec![0u16; w * h];
  let mut sink = MixedSinker::<Gbrp>::new(w, h)
    .with_luma(&mut luma_u8)
    .unwrap()
    .with_luma_u16(&mut luma_u16)
    .unwrap();
  gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  // u16 luma == u8 luma zero-extended (same byte values, native u16).
  for i in 0..w * h {
    assert_eq!(luma_u16[i], luma_u8[i] as u16, "px {i}");
  }
}

// ---- Gbrap tests -------------------------------------------------------

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrap_with_rgba_passes_source_alpha() {
  let w = 16usize;
  let h = 4usize;
  let g = std::vec![100u8; w * h];
  let b = std::vec![50u8; w * h];
  let r = std::vec![200u8; w * h];
  // Random alpha — make sure each pixel keeps its own α.
  let a = random_alpha(w, h, 0xDEAD_BEEF);
  let src = GbrapFrame::try_new(
    &g, &b, &r, &a, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();

  let mut rgba = std::vec![0u8; w * h * 4];
  let mut sink = MixedSinker::<Gbrap>::new(w, h)
    .with_rgba(&mut rgba)
    .unwrap();
  gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  for i in 0..w * h {
    assert_eq!(rgba[i * 4], r[i], "R px {i}");
    assert_eq!(rgba[i * 4 + 1], g[i], "G px {i}");
    assert_eq!(rgba[i * 4 + 2], b[i], "B px {i}");
    assert_eq!(rgba[i * 4 + 3], a[i], "A px {i}");
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrap_with_rgb_drops_alpha() {
  let w = 16usize;
  let h = 4usize;
  let rgb_seed = random_rgb(w, h, 0xBEEF_F00D);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let a = random_alpha(w, h, 0x1357_9BDF);
  let src = GbrapFrame::try_new(
    &g, &b, &r, &a, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();

  let mut rgb_out = std::vec![0u8; w * h * 3];
  let mut sink = MixedSinker::<Gbrap>::new(w, h)
    .with_rgb(&mut rgb_out)
    .unwrap();
  gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  // Output must equal the rgb_seed reconstructed from the planes.
  let (rg, rb, rr) = planes_from_packed_rgb(&rgb_out, w, h);
  assert_eq!(rg, g, "G");
  assert_eq!(rb, b, "B");
  assert_eq!(rr, r, "R");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrap_with_rgb_and_with_rgba_strategy_a_plus_matches_independent() {
  // Strategy A+ correctness: the combo path (with_rgb + with_rgba)
  // should produce byte-identical output to running each request
  // through its own dedicated sinker.
  let w = 32usize;
  let h = 8usize;
  let rgb_seed = random_rgb(w, h, 0xABBA_BABE);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let a = random_alpha(w, h, 0xCAFE_BABE);
  let src = GbrapFrame::try_new(
    &g, &b, &r, &a, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();

  // ---- Reference: two independent sinkers ----
  let mut rgb_ref = std::vec![0u8; w * h * 3];
  let mut rgba_ref = std::vec![0u8; w * h * 4];
  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgb(&mut rgb_ref)
      .unwrap();
    gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }
  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgba(&mut rgba_ref)
      .unwrap();
    gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  // ---- Strategy A+ combo: one sinker writes both ----
  let mut rgb_combo = std::vec![0u8; w * h * 3];
  let mut rgba_combo = std::vec![0u8; w * h * 4];
  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgb(&mut rgb_combo)
      .unwrap()
      .with_rgba(&mut rgba_combo)
      .unwrap();
    gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  assert_eq!(rgb_combo, rgb_ref, "RGB mismatch combo vs independent");
  assert_eq!(rgba_combo, rgba_ref, "RGBA mismatch combo vs independent");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrap_planar_parity_with_rgb24_when_alpha_drop() {
  // Same pixel data fed through Rgb24 and Gbrap (with α discarded
  // via with_rgb): outputs must be byte-identical.
  let w = 16usize;
  let h = 8usize;
  let rgb_seed = random_rgb(w, h, 0xC001_F00D);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let a = random_alpha(w, h, 0xDEAD_FA11);

  let rgb24 = Rgb24Frame::try_new(&rgb_seed, w as u32, h as u32, (w * 3) as u32).unwrap();
  let gbrap = GbrapFrame::try_new(
    &g, &b, &r, &a, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();

  // Rgb24 path.
  let mut rgb_ref = std::vec![0u8; w * h * 3];
  let mut luma_ref = std::vec![0u8; w * h];
  {
    let mut sink = MixedSinker::<Rgb24>::new(w, h)
      .with_rgb(&mut rgb_ref)
      .unwrap()
      .with_luma(&mut luma_ref)
      .unwrap();
    rgb24_to(&rgb24, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  // Gbrap with α-drop path.
  let mut rgb_g = std::vec![0u8; w * h * 3];
  let mut luma_g = std::vec![0u8; w * h];
  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgb(&mut rgb_g)
      .unwrap()
      .with_luma(&mut luma_g)
      .unwrap();
    gbrap_to(&gbrap, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  assert_eq!(rgb_g, rgb_ref, "RGB mismatch Gbrap vs Rgb24");
  assert_eq!(luma_g, luma_ref, "luma mismatch Gbrap vs Rgb24");
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_simd_matches_scalar() {
  // Differential test: SIMD vs scalar fan-out across {RGB, RGBA, luma,
  // luma_u16, HSV} on random GBR data.
  let w = 64usize;
  let h = 8usize;
  let rgb_seed = random_rgb(w, h, 0xFEED_FACE);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let make_buffers = || {
    (
      std::vec![0u8; w * h * 3], // rgb
      std::vec![0u8; w * h * 4], // rgba
      std::vec![0u8; w * h],     // luma
      std::vec![0u16; w * h],    // luma_u16
      std::vec![0u8; w * h],     // h
      std::vec![0u8; w * h],     // s
      std::vec![0u8; w * h],     // v
    )
  };

  let (mut r1, mut a1, mut l1, mut lu1, mut h1, mut s1, mut v1) = make_buffers();
  let (mut r2, mut a2, mut l2, mut lu2, mut h2, mut s2, mut v2) = make_buffers();

  {
    let mut sink = MixedSinker::<Gbrp>::new(w, h)
      .with_rgb(&mut r1)
      .unwrap()
      .with_rgba(&mut a1)
      .unwrap()
      .with_luma(&mut l1)
      .unwrap()
      .with_luma_u16(&mut lu1)
      .unwrap()
      .with_hsv(&mut h1, &mut s1, &mut v1)
      .unwrap()
      .with_simd(true);
    gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }
  {
    let mut sink = MixedSinker::<Gbrp>::new(w, h)
      .with_rgb(&mut r2)
      .unwrap()
      .with_rgba(&mut a2)
      .unwrap()
      .with_luma(&mut l2)
      .unwrap()
      .with_luma_u16(&mut lu2)
      .unwrap()
      .with_hsv(&mut h2, &mut s2, &mut v2)
      .unwrap()
      .with_simd(false);
    gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  assert_eq!(r1, r2, "rgb SIMD vs scalar");
  assert_eq!(a1, a2, "rgba SIMD vs scalar");
  assert_eq!(l1, l2, "luma SIMD vs scalar");
  assert_eq!(lu1, lu2, "luma_u16 SIMD vs scalar");
  // HSV SIMD vs scalar can drift by ±1 LSB (OpenCV-style; H is
  // circular). See the existing per-arch HSV tests for the tolerance
  // rationale.
  assert_hsv_within_one_lsb(&h1, &s1, &v1, &h2, &s2, &v2);
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrap_simd_matches_scalar() {
  let w = 64usize;
  let h = 8usize;
  let rgb_seed = random_rgb(w, h, 0xFACE_F00D);
  let (g, b, r) = planes_from_packed_rgb(&rgb_seed, w, h);
  let a = random_alpha(w, h, 0xC0DE_BABE);
  let src = GbrapFrame::try_new(
    &g, &b, &r, &a, w as u32, h as u32, w as u32, w as u32, w as u32, w as u32,
  )
  .unwrap();

  let make_buffers = || {
    (
      std::vec![0u8; w * h * 3],
      std::vec![0u8; w * h * 4],
      std::vec![0u8; w * h],
      std::vec![0u16; w * h],
      std::vec![0u8; w * h],
      std::vec![0u8; w * h],
      std::vec![0u8; w * h],
    )
  };

  let (mut r1, mut a1, mut l1, mut lu1, mut h1, mut s1, mut v1) = make_buffers();
  let (mut r2, mut a2, mut l2, mut lu2, mut h2, mut s2, mut v2) = make_buffers();

  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgb(&mut r1)
      .unwrap()
      .with_rgba(&mut a1)
      .unwrap()
      .with_luma(&mut l1)
      .unwrap()
      .with_luma_u16(&mut lu1)
      .unwrap()
      .with_hsv(&mut h1, &mut s1, &mut v1)
      .unwrap()
      .with_simd(true);
    gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }
  {
    let mut sink = MixedSinker::<Gbrap>::new(w, h)
      .with_rgb(&mut r2)
      .unwrap()
      .with_rgba(&mut a2)
      .unwrap()
      .with_luma(&mut l2)
      .unwrap()
      .with_luma_u16(&mut lu2)
      .unwrap()
      .with_hsv(&mut h2, &mut s2, &mut v2)
      .unwrap()
      .with_simd(false);
    gbrap_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();
  }

  assert_eq!(r1, r2, "rgb SIMD vs scalar");
  assert_eq!(a1, a2, "rgba SIMD vs scalar");
  assert_eq!(l1, l2, "luma SIMD vs scalar");
  assert_eq!(lu1, lu2, "luma_u16 SIMD vs scalar");
  assert_hsv_within_one_lsb(&h1, &s1, &v1, &h2, &s2, &v2);
}

/// HSV SIMD vs scalar can disagree by ±1 LSB at boundary pixels (the
/// scalar path uses an integer LUT, SIMD uses true f32 division). H is
/// circular: distance between 0 and 179 is 1, not 179.
fn assert_hsv_within_one_lsb(h1: &[u8], s1: &[u8], v1: &[u8], h2: &[u8], s2: &[u8], v2: &[u8]) {
  for (i, (&a, &b)) in h1.iter().zip(h2.iter()).enumerate() {
    let d = a.abs_diff(b);
    let circ = d.min(180 - d);
    assert!(circ <= 1, "H divergence at pixel {i}: simd={a} scalar={b}");
  }
  for (i, (&a, &b)) in s1.iter().zip(s2.iter()).enumerate() {
    assert!(
      a.abs_diff(b) <= 1,
      "S divergence at pixel {i}: simd={a} scalar={b}"
    );
  }
  for (i, (&a, &b)) in v1.iter().zip(v2.iter()).enumerate() {
    assert!(
      a.abs_diff(b) <= 1,
      "V divergence at pixel {i}: simd={a} scalar={b}"
    );
  }
}

#[test]
#[cfg_attr(
  miri,
  ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn gbrp_with_luma_bt709_pure_red() {
  // Pure red full-range BT.709: Y = 0.2126 * 255 ≈ 54.21 → 54.
  let w = 16usize;
  let h = 4usize;
  let g = std::vec![0u8; w * h];
  let b = std::vec![0u8; w * h];
  let r = std::vec![255u8; w * h];
  let src =
    GbrpFrame::try_new(&g, &b, &r, w as u32, h as u32, w as u32, w as u32, w as u32).unwrap();

  let mut luma = std::vec![0u8; w * h];
  let mut sink = MixedSinker::<Gbrp>::new(w, h).with_luma(&mut luma).unwrap();
  gbrp_to(&src, true, ColorMatrix::Bt709, &mut sink).unwrap();

  for &y in &luma {
    assert!(y.abs_diff(54) <= 1, "got Y={y}");
  }
}