pixon 0.8.0

SIMD-dispatched color conversion for the FFmpeg AVPixelFormat space: one-call decode of validated source frames to RGB / RGBA / Luma / HSV, fused resampling while converting, colorimetry-honest matrices and chroma siting, no_std-friendly.
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
use super::*;

// ---- Bayer + Bayer16 MixedSinker integration tests ----------------------

/// Build a solid-channel RGGB Bayer plane (8-bit) so every R site
/// holds `r`, every B site holds `b`, and both G sites hold `g`.
fn solid_rggb8(width: u32, height: u32, r: u8, g: u8, b: u8) -> std::vec::Vec<u8> {
  let w = width as usize;
  let h = height as usize;
  let mut data = std::vec![0u8; w * h];
  for y in 0..h {
    for x in 0..w {
      data[y * w + x] = match (y & 1, x & 1) {
        (0, 0) => r,
        (0, 1) => g,
        (1, 0) => g,
        (1, 1) => b,
        _ => unreachable!(),
      };
    }
  }
  data
}

/// Build a 12-bit low-packed RGGB Bayer plane.
fn solid_rggb12(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
}

#[test]
fn bayer_mixed_sinker_with_rgb_red_interior() {
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb8(w, h, 255, 0, 0);
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_rgb(&mut rgb)
    .unwrap();
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  // Interior should be exactly red.
  let wu = w as usize;
  for y in 0..(h as usize) {
    for x in 0..wu {
      let i = (y * wu + x) * 3;
      assert_eq!(rgb[i], 255, "px ({x},{y}) R");
      assert_eq!(rgb[i + 1], 0, "px ({x},{y}) G");
      assert_eq!(rgb[i + 2], 0, "px ({x},{y}) B");
    }
  }
}

#[test]
fn bayer_mixed_sinker_with_luma_uniform_byte() {
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  // Uniform byte → uniform RGB → uniform luma at the same value.
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![200u8; (w * h) as usize];
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut luma = std::vec![0u8; (w * h) as usize];
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_luma(&mut luma)
    .unwrap();
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  // BT.709 luma of (200, 200, 200) = 200 (within 1 LSB rounding).
  for &y in &luma {
    assert!((y as i32 - 200).abs() <= 1, "luma got {y}");
  }
}

#[test]
fn bayer_mixed_sinker_with_hsv_solid_red_interior() {
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb8(w, h, 255, 0, 0);
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut hh = std::vec![0u8; (w * h) as usize];
  let mut ss = std::vec![0u8; (w * h) as usize];
  let mut vv = std::vec![0u8; (w * h) as usize];
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_hsv(&mut hh, &mut ss, &mut vv)
    .unwrap();
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  // Pure red at interior → H = 0 (red), S = 255 (max), V = 255.
  let wu = w as usize;
  for y in 0..(h as usize) {
    for x in 0..wu {
      let i = y * wu + x;
      assert_eq!(hh[i], 0, "px ({x},{y}) H");
      assert_eq!(ss[i], 255, "px ({x},{y}) S");
      assert_eq!(vv[i], 255, "px ({x},{y}) V");
    }
  }
}

#[test]
fn bayer16_mixed_sinker_with_rgb_red_interior() {
  use crate::{
    frame::Bayer12Frame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb12(w, h, 4095, 0, 0);
  let raw = raw
    .iter()
    .map(|&v| le_wire_u16(v))
    .collect::<std::vec::Vec<u16>>();
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb(&mut rgb)
    .unwrap();
  bayer16_to::<12, _>(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  let wu = w as usize;
  for y in 0..(h as usize) {
    for x in 0..wu {
      let i = (y * wu + x) * 3;
      assert_eq!(rgb[i], 255, "px ({x},{y}) R");
      assert_eq!(rgb[i + 1], 0, "px ({x},{y}) G");
      assert_eq!(rgb[i + 2], 0, "px ({x},{y}) B");
    }
  }
}

#[test]
fn bayer16_mixed_sinker_with_rgb_u16_red_interior() {
  use crate::{
    frame::Bayer12Frame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb12(w, h, 4095, 0, 0);
  let raw = raw
    .iter()
    .map(|&v| le_wire_u16(v))
    .collect::<std::vec::Vec<u16>>();
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb_u16(&mut rgb)
    .unwrap();
  bayer16_to::<12, _>(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  // Low-packed 12-bit white = 4095 at interior.
  let wu = w as usize;
  for y in 0..(h as usize) {
    for x in 0..wu {
      let i = (y * wu + x) * 3;
      assert_eq!(rgb[i], 4095, "px ({x},{y}) R");
      assert_eq!(rgb[i + 1], 0, "px ({x},{y}) G");
      assert_eq!(rgb[i + 2], 0, "px ({x},{y}) B");
    }
  }
}

#[test]
fn bayer16_mixed_sinker_dual_rgb_and_rgb_u16() {
  use crate::{
    frame::Bayer12Frame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to},
  };
  // Both u8 RGB and u16 RGB attached — both kernels run.
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb12(w, h, 4095, 0, 0);
  let raw = raw
    .iter()
    .map(|&v| le_wire_u16(v))
    .collect::<std::vec::Vec<u16>>();
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();
  let mut rgb_u8 = std::vec![0u8; (w * h * 3) as usize];
  let mut rgb_u16 = std::vec![0u16; (w * h * 3) as usize];
  let mut sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb(&mut rgb_u8)
    .unwrap()
    .with_rgb_u16(&mut rgb_u16)
    .unwrap();
  bayer16_to::<12, _>(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  let wu = w as usize;
  for y in 0..(h as usize) {
    for x in 0..wu {
      let i = (y * wu + x) * 3;
      assert_eq!(rgb_u8[i], 255);
      assert_eq!(rgb_u16[i], 4095);
    }
  }
}

#[test]
fn bayer_mixed_sinker_returns_row_shape_mismatch_on_bad_above() {
  use crate::raw::{BayerDemosaic, BayerPattern, BayerRow};
  let mut rgb = std::vec![0u8; 8 * 6 * 3];
  let mut sinker = MixedSinker::<Bayer>::new(8, 6).with_rgb(&mut rgb).unwrap();
  sinker.begin_frame(8, 6).unwrap();
  let mid = std::vec![0u8; 8];
  let below = std::vec![0u8; 8];
  let bad_above = std::vec![0u8; 7]; // wrong length
  let m = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
  let row = BayerRow::for_tests(
    &bad_above,
    &mid,
    &below,
    0,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    m,
  );
  let err = sinker.process(row).unwrap_err();
  assert!(matches!(
    &err,
    MixedSinkerError::RowShapeMismatch(e)
      if matches!(e.which(), RowSlice::BayerAbove) && e.expected() == 8 && e.actual() == 7
  ));
}

#[test]
fn bayer16_mixed_sinker_returns_row_shape_mismatch_on_bad_mid() {
  use crate::raw::{BayerDemosaic, BayerPattern, BayerRow16};
  let mut rgb = std::vec![0u8; 8 * 6 * 3];
  let mut sinker = MixedSinker::<Bayer16<12>>::new(8, 6)
    .with_rgb(&mut rgb)
    .unwrap();
  sinker.begin_frame(8, 6).unwrap();
  let above = std::vec![0u16; 8];
  let bad_mid = std::vec![0u16; 7]; // wrong length
  let below = std::vec![0u16; 8];
  let m = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
  let row = BayerRow16::<12>::for_tests(
    &above,
    &bad_mid,
    &below,
    0,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    m,
  );
  let err = sinker.process(row).unwrap_err();
  assert!(matches!(
    &err,
    MixedSinkerError::RowShapeMismatch(e)
      if matches!(e.which(), RowSlice::Bayer16Mid) && e.expected() == 8 && e.actual() == 7
  ));
}

// ---- Bayer luma-coefficients tests --------------------------------------
//
// Cover the gap that earlier `bayer_mixed_sinker_with_luma_uniform_byte`
// missed: every coefficient set agrees on gray, so a hard-coded BT.709
// path could go undetected. The non-gray cases below force the rows
// apart — solid red goes through `cr` only, so each variant produces a
// distinct luma value.

/// Resolve a [`LumaCoefficients`] preset and run a solid-red 8-bit
/// Bayer frame through it; return the `cr` actually applied.
fn bayer8_solid_red_luma(coeffs: LumaCoefficients) -> u8 {
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb8(w, h, 255, 0, 0);
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut luma = std::vec![0u8; (w * h) as usize];
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_luma_coefficients(coeffs);
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  let center = luma[(h as usize / 2) * (w as usize) + (w as usize / 2)];
  for (i, &y) in luma.iter().enumerate() {
    assert_eq!(
      y, center,
      "luma not uniform at idx {i}: {y} vs center {center}"
    );
  }
  center
}

#[test]
fn bayer_with_luma_coefficients_solid_red_differs_by_preset() {
  // Solid red after demosaic is `(255, 0, 0)` everywhere
  // (`bayer_mixed_sinker_with_rgb_red_interior` proves this).
  // Luma reduces to `(cr * 255 + 128) >> 8` for each preset, so
  // each coefficient set must produce a different value. A
  // hard-coded BT.709 implementation would make these all 54.
  let bt709 = bayer8_solid_red_luma(LumaCoefficients::Bt709);
  let bt2020 = bayer8_solid_red_luma(LumaCoefficients::Bt2020);
  let bt601 = bayer8_solid_red_luma(LumaCoefficients::Bt601);
  let dcip3 = bayer8_solid_red_luma(LumaCoefficients::DciP3);
  let aces = bayer8_solid_red_luma(LumaCoefficients::AcesAp1);

  assert_eq!(bt709, 54, "BT.709 red luma");
  assert_eq!(bt2020, 67, "BT.2020 red luma");
  assert_eq!(bt601, 77, "BT.601 red luma");
  assert_eq!(dcip3, 59, "DCI-P3 red luma");
  assert_eq!(aces, 70, "ACES AP1 red luma");

  // Distinct values guard against silent collapse to the default.
  let mut all = std::vec![bt709, bt2020, bt601, dcip3, aces];
  all.sort_unstable();
  all.dedup();
  assert_eq!(all.len(), 5, "presets collapsed to fewer values: {all:?}");
}

#[test]
fn bayer_with_luma_coefficients_custom_round_trips_to_q8() {
  // Custom weights `(1.0, 0.0, 0.0)` → Q8 `(256, 0, 0)`. Solid red
  // 255 then reduces to `(256 * 255 + 128) >> 8 = 255` (clamped).
  let custom = LumaCoefficients::try_custom(1.0, 0.0, 0.0).unwrap();
  let red = bayer8_solid_red_luma(custom);
  assert_eq!(red, 255, "Custom (1.0, 0.0, 0.0) on red 255 → 255");
}

#[test]
fn bayer_with_luma_coefficients_default_is_bt709() {
  // No `with_luma_coefficients` call → default (BT.709). Same red
  // input must produce the BT.709 value (54). This pins the
  // public default so a future refactor can't silently change it.
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb8(w, h, 255, 0, 0);
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut luma = std::vec![0u8; (w * h) as usize];
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_luma(&mut luma)
    .unwrap();
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  for (i, &y) in luma.iter().enumerate() {
    assert_eq!(y, 54, "default red luma at idx {i}");
  }
  assert_eq!(LumaCoefficients::default(), LumaCoefficients::Bt709);
}

#[test]
fn bayer_with_luma_coefficients_uniform_gray_invariant() {
  // The reverse of the above: gray content *must* be invariant
  // under any preset (this is the property the original
  // `*_with_luma_uniform_byte` test relied on, and the reason
  // the hard-coded BT.709 bug was invisible there).
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![200u8; (w * h) as usize];
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let presets = [
    LumaCoefficients::Bt709,
    LumaCoefficients::Bt2020,
    LumaCoefficients::Bt601,
    LumaCoefficients::DciP3,
    LumaCoefficients::AcesAp1,
  ];
  for preset in presets {
    let mut luma = std::vec![0u8; (w * h) as usize];
    let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
      .with_luma(&mut luma)
      .unwrap()
      .with_luma_coefficients(preset);
    bayer_to(
      &frame,
      BayerPattern::Rggb,
      BayerDemosaic::Bilinear,
      WhiteBalance::neutral(),
      ColorCorrectionMatrix::identity(),
      &mut sinker,
    )
    .unwrap();
    for &y in &luma {
      assert!(
        (y as i32 - 200).abs() <= 1,
        "{preset:?} on gray 200 → {y} (expected ~200)"
      );
    }
  }
}

#[test]
fn bayer16_with_luma_coefficients_solid_red_differs_by_preset() {
  // Mirror of the 8-bit test for the high-bit-depth path
  // (`MixedSinker<Bayer16<BITS>>`). 12-bit white = 4095 →
  // demosaic produces `(255, 0, 0)` u8 RGB after CCM identity
  // and right-shift to u8 (the bayer16→u8 path reduces samples
  // before the luma kernel).
  use crate::{
    frame::Bayer12Frame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = solid_rggb12(w, h, 4095, 0, 0);
  let raw = raw
    .iter()
    .map(|&v| le_wire_u16(v))
    .collect::<std::vec::Vec<u16>>();
  let frame = Bayer12Frame::try_new(&raw, w, h, w).unwrap();

  let run = |coeffs: LumaCoefficients| -> u8 {
    let mut luma = std::vec![0u8; (w * h) as usize];
    let mut sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
      .with_luma(&mut luma)
      .unwrap()
      .with_luma_coefficients(coeffs);
    bayer16_to(
      &frame,
      BayerPattern::Rggb,
      BayerDemosaic::Bilinear,
      WhiteBalance::neutral(),
      ColorCorrectionMatrix::identity(),
      &mut sinker,
    )
    .unwrap();
    let center = luma[(h as usize / 2) * (w as usize) + (w as usize / 2)];
    for (i, &y) in luma.iter().enumerate() {
      assert_eq!(y, center, "luma not uniform at idx {i}");
    }
    center
  };

  let bt709 = run(LumaCoefficients::Bt709);
  let bt2020 = run(LumaCoefficients::Bt2020);
  let bt601 = run(LumaCoefficients::Bt601);
  let dcip3 = run(LumaCoefficients::DciP3);
  let aces = run(LumaCoefficients::AcesAp1);

  assert_eq!(bt709, 54, "BT.709 red luma (Bayer16<12>)");
  assert_eq!(bt2020, 67, "BT.2020 red luma (Bayer16<12>)");
  assert_eq!(bt601, 77, "BT.601 red luma (Bayer16<12>)");
  assert_eq!(dcip3, 59, "DCI-P3 red luma (Bayer16<12>)");
  assert_eq!(aces, 70, "ACES AP1 red luma (Bayer16<12>)");

  let mut all = std::vec![bt709, bt2020, bt601, dcip3, aces];
  all.sort_unstable();
  all.dedup();
  assert_eq!(all.len(), 5, "Bayer16 presets collapsed: {all:?}");
}

#[test]
fn luma_coefficients_to_q8_presets_sum_to_256() {
  // Round-to-nearest of the published weights for each preset
  // must still sum to exactly 256 — the rgb_row_to_luma_row
  // kernel divides by 256 implicitly via `>> 8`, so any preset
  // that drifts from 256 produces a brightness-scaled luma plane.
  for preset in [
    LumaCoefficients::Bt709,
    LumaCoefficients::Bt2020,
    LumaCoefficients::Bt601,
    LumaCoefficients::DciP3,
    LumaCoefficients::AcesAp1,
  ] {
    let (cr, cg, cb) = preset.to_q8();
    assert_eq!(cr + cg + cb, 256, "{preset:?} Q8 weights don't sum to 256");
  }
}

// ---- CustomLumaCoefficients validation tests ----------------------------
//
// The kernel multiplies these weights into a `u32` accumulator
// after a saturating `f32 → u32` cast. Without validation, NaN
// / negative / ±∞ / very-large finite weights would silently
// corrupt every Bayer luma plane (NaN → 0, +∞ → u32::MAX,
// negative → 0, large finite → debug-panic on multiply or
// wrapping in release). `try_new` rejects all four classes
// upfront so the kernel can stay branchless.

#[test]
fn custom_luma_coefficients_accepts_valid_weights() {
  // Standard BT.709 weights pass through cleanly.
  let c = CustomLumaCoefficients::try_new(0.2126, 0.7152, 0.0722).unwrap();
  assert_eq!(c.r(), 0.2126);
  assert_eq!(c.g(), 0.7152);
  assert_eq!(c.b(), 0.0722);

  // Zeroes are allowed (zero a channel out — degenerate but valid).
  let z = CustomLumaCoefficients::try_new(0.0, 1.0, 0.0).unwrap();
  assert_eq!(z.r(), 0.0);

  // Boundary: exactly `MAX_COEFFICIENT` is allowed (`<=`, not `<`).
  let edge =
    CustomLumaCoefficients::try_new(CustomLumaCoefficients::MAX_COEFFICIENT, 0.0, 0.0).unwrap();
  assert_eq!(edge.r(), CustomLumaCoefficients::MAX_COEFFICIENT);
}

#[test]
fn custom_luma_coefficients_rejects_nan() {
  for (channel, r, g, b) in [
    (LumaChannel::R, f32::NAN, 1.0, 0.0),
    (LumaChannel::G, 0.0, f32::NAN, 0.0),
    (LumaChannel::B, 0.5, 0.5, f32::NAN),
  ] {
    let err = CustomLumaCoefficients::try_new(r, g, b).unwrap_err();
    assert!(
      matches!(err, LumaCoefficientsError::NonFinite { channel: ch, .. } if ch == channel),
      "expected NonFinite for {channel:?}, got {err:?}"
    );
  }
}

#[test]
fn custom_luma_coefficients_rejects_infinity() {
  // Both +∞ and -∞ caught by `is_finite`. The earlier
  // `as u32` saturating cast would turn +∞ into `u32::MAX`,
  // overflowing `cr * 255` in debug builds.
  for inf in [f32::INFINITY, f32::NEG_INFINITY] {
    let err_r = CustomLumaCoefficients::try_new(inf, 0.0, 0.0).unwrap_err();
    let err_g = CustomLumaCoefficients::try_new(0.0, inf, 0.0).unwrap_err();
    let err_b = CustomLumaCoefficients::try_new(0.0, 0.0, inf).unwrap_err();
    for (err, channel) in [
      (err_r, LumaChannel::R),
      (err_g, LumaChannel::G),
      (err_b, LumaChannel::B),
    ] {
      assert!(
        matches!(err, LumaCoefficientsError::NonFinite { channel: ch, .. } if ch == channel),
        "expected NonFinite for {channel:?} with inf={inf}, got {err:?}"
      );
    }
  }
}

#[test]
fn custom_luma_coefficients_rejects_negative() {
  for (channel, r, g, b) in [
    (LumaChannel::R, -0.001, 1.0, 0.0),
    (LumaChannel::G, 0.0, -1.0, 0.0),
    (LumaChannel::B, 0.5, 0.5, -42.0),
  ] {
    let err = CustomLumaCoefficients::try_new(r, g, b).unwrap_err();
    assert!(
      matches!(err, LumaCoefficientsError::Negative { channel: ch, .. } if ch == channel),
      "expected Negative for {channel:?}, got {err:?}"
    );
  }
}

#[test]
fn custom_luma_coefficients_rejects_oversized() {
  let over = CustomLumaCoefficients::MAX_COEFFICIENT + 1.0;
  for (channel, r, g, b) in [
    (LumaChannel::R, over, 0.0, 0.0),
    (LumaChannel::G, 0.0, over, 0.0),
    (LumaChannel::B, 0.0, 0.0, over),
  ] {
    let err = CustomLumaCoefficients::try_new(r, g, b).unwrap_err();
    assert!(
      matches!(
        err,
        LumaCoefficientsError::OutOfBounds { channel: ch, .. } if ch == channel
      ),
      "expected OutOfBounds for {channel:?}, got {err:?}"
    );
  }

  // Pathological value that previously caused saturation:
  // `1e9_f32 * 256.0 ≈ 2.56e11` saturates `as u32` to
  // `u32::MAX`, then `cr * 255` overflows.
  let err = CustomLumaCoefficients::try_new(1.0e9, 0.0, 0.0).unwrap_err();
  assert!(matches!(err, LumaCoefficientsError::OutOfBounds { .. }));
}

#[test]
fn luma_coefficients_try_custom_routes_through_validation() {
  // Convenience constructor surfaces the same errors as
  // `CustomLumaCoefficients::try_new` and yields the wrapped
  // variant on success.
  let ok = LumaCoefficients::try_custom(0.5, 0.4, 0.1).unwrap();
  assert!(ok.is_custom());

  let err = LumaCoefficients::try_custom(f32::NAN, 0.0, 0.0).unwrap_err();
  assert!(matches!(err, LumaCoefficientsError::NonFinite { .. }));
}

#[test]
#[should_panic(expected = "invalid CustomLumaCoefficients")]
fn custom_luma_coefficients_new_panics_on_invalid() {
  // The `::new` and `LumaCoefficients::custom` panicking
  // constructors are intended for compile-time-known weights;
  // hostile input must blow up loudly, not silently corrupt
  // downstream luma.
  let _ = CustomLumaCoefficients::new(f32::NAN, 0.0, 0.0);
}

#[test]
fn custom_luma_coefficients_at_max_does_not_overflow_kernel() {
  // End-to-end proof that `MAX_COEFFICIENT` is conservative:
  // even worst-case (all three channels at max, all pixels at
  // 255) the per-row accumulator stays well under `u32::MAX`,
  // and the final `>> 8 / .min(255)` clamps cleanly to 255.
  use crate::{
    frame::BayerFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer_to},
  };
  let (w, h) = (8u32, 6u32);
  let raw = std::vec![255u8; (w * h) as usize];
  let frame = BayerFrame::try_new(&raw, w, h, w).unwrap();
  let mut luma = std::vec![0u8; (w * h) as usize];
  let max = CustomLumaCoefficients::MAX_COEFFICIENT;
  let mut sinker = MixedSinker::<Bayer>::new(w as usize, h as usize)
    .with_luma(&mut luma)
    .unwrap()
    .with_luma_coefficients(LumaCoefficients::try_custom(max, max, max).unwrap());
  bayer_to(
    &frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  for &y in &luma {
    assert_eq!(
      y, 255,
      "max-weight saturated luma should clamp to 255, got {y}"
    );
  }
}

// ---- Bayer16 big-endian (BE) tests --------------------------------------
//
// `Bayer16<BITS, true>` (the `Bayer{10,12,14}Be` / `Bayer16BitBe`
// aliases) reads BE-encoded `u16` plane samples; the kernel byte-swaps
// each sample before the demosaic, so a BE plane carrying the *same
// logical samples* as an LE plane must produce *identical* RGB. These
// tests build the BE plane host-independently (`to_be_bytes` →
// `from_ne_bytes`) so they assert real byte-order handling on both
// little- and big-endian hosts, and pin that the LE path is unchanged.

/// Reinterpret a logical low-packed `u16` as the `u16` element a BE
/// wire plane would store: serialize big-endian, then read back in the
/// host's native order. On an LE host this yields the byte-swapped
/// value; on a BE host it is the identity. Host-independent by
/// construction — mirrors the y216 / p2xx BE test convention.
fn be_wire_u16(logical: u16) -> u16 {
  u16::from_ne_bytes(logical.to_be_bytes())
}

/// LE-wire twin of [`be_wire_u16`]: serialize little-endian, then read
/// back in the host's native order. On an LE host it is the identity;
/// on a BE host it byte-swaps. Encoding the LE-oracle plane through
/// this makes the oracle host-independent — the `from_le` wire decode
/// inside the LE frame recovers the original logical samples on every
/// host, instead of mis-reading a host-native plane as LE.
fn le_wire_u16(logical: u16) -> u16 {
  u16::from_ne_bytes(logical.to_le_bytes())
}

/// Build a 12-bit low-packed Bayer plane for an arbitrary `pattern`,
/// placing `r` at R sites, `b` at B sites, and `g` at both G sites.
fn solid_pattern12(
  pattern: crate::raw::BayerPattern,
  width: u32,
  height: u32,
  r: u16,
  g: u16,
  b: u16,
) -> std::vec::Vec<u16> {
  use crate::raw::BayerPattern::*;
  // (R-site, B-site) parities — must match the kernel's `pattern_phases`.
  let (r_par, b_par) = match pattern {
    Rggb => ((0usize, 0usize), (1usize, 1usize)),
    Bggr => ((1, 1), (0, 0)),
    Grbg => ((0, 1), (1, 0)),
    Gbrg => ((1, 0), (0, 1)),
  };
  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 par = (y & 1, x & 1);
      data[y * w + x] = if par == r_par {
        r
      } else if par == b_par {
        b
      } else {
        g
      };
    }
  }
  data
}

/// LE 12-bit RGB (u8 out) for the given logical plane, via the
/// `Bayer16<12, false>` path. The oracle the BE output must match.
fn le12_rgb_u8(
  pattern: crate::raw::BayerPattern,
  w: u32,
  h: u32,
  plane: &[u16],
) -> std::vec::Vec<u8> {
  use crate::{
    frame::Bayer12Frame,
    raw::{BayerDemosaic, ColorCorrectionMatrix, WhiteBalance, bayer16_to},
  };
  let frame = Bayer12Frame::try_new(plane, w, h, w).unwrap();
  let mut rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb(&mut rgb)
    .unwrap();
  bayer16_to::<12, _>(
    &frame,
    pattern,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut sinker,
  )
  .unwrap();
  rgb
}

/// A BE 12-bit plane carrying the same logical samples as its LE twin
/// produces byte-identical RGB (u8 out), for every Bayer pattern. The
/// byte-swap is load-bearing: an asymmetric `g` (`0x0ABC`, whose bytes
/// differ) would demosaic to a wildly different value if the kernel
/// skipped the swap.
#[test]
fn bayer16_be_matches_le_u8_all_patterns() {
  use crate::{
    frame::Bayer12BeFrame,
    raw::{BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to_endian},
  };
  let (w, h) = (8u32, 6u32);
  // Asymmetric logical samples so the byte order is observable.
  let (r, g, b) = (0x0FFFu16, 0x0ABCu16, 0x0123u16);
  for pattern in [
    BayerPattern::Rggb,
    BayerPattern::Bggr,
    BayerPattern::Grbg,
    BayerPattern::Gbrg,
  ] {
    // Build the logical samples once, then derive both wire planes from
    // them: the LE oracle via `le_wire_u16`, the BE input via
    // `be_wire_u16`. Both decode back to `logical` on any host.
    let logical = solid_pattern12(pattern, w, h, r, g, b);
    let le_plane: std::vec::Vec<u16> = logical.iter().map(|&v| le_wire_u16(v)).collect();
    let expected = le12_rgb_u8(pattern, w, h, &le_plane);

    let be_plane: std::vec::Vec<u16> = logical.iter().map(|&v| be_wire_u16(v)).collect();
    let frame = Bayer12BeFrame::try_new(&be_plane, w, h, w).unwrap();
    let mut rgb = std::vec![0u8; (w * h * 3) as usize];
    let mut sinker = MixedSinker::<Bayer16<12, true>>::new(w as usize, h as usize)
      .with_rgb(&mut rgb)
      .unwrap();
    bayer16_to_endian::<12, true, _>(
      &frame,
      pattern,
      BayerDemosaic::Bilinear,
      WhiteBalance::neutral(),
      ColorCorrectionMatrix::identity(),
      &mut sinker,
    )
    .unwrap();
    assert_eq!(rgb, expected, "BE/LE RGB mismatch for pattern {pattern:?}");
  }
}

/// Same parity check for the native-depth `u16` output path.
#[test]
fn bayer16_be_matches_le_u16_output() {
  use crate::{
    frame::{Bayer12BeFrame, Bayer12Frame},
    raw::{
      BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to,
      bayer16_to_endian,
    },
  };
  let (w, h) = (8u32, 6u32);
  let (r, g, b) = (0x0FFFu16, 0x0ABCu16, 0x0123u16);
  // Shared logical samples; both wire planes derive from them.
  let logical = solid_pattern12(BayerPattern::Rggb, w, h, r, g, b);
  let le_plane: std::vec::Vec<u16> = logical.iter().map(|&v| le_wire_u16(v)).collect();

  // LE oracle (u16 out).
  let le_frame = Bayer12Frame::try_new(&le_plane, w, h, w).unwrap();
  let mut le_rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut le_sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb_u16(&mut le_rgb)
    .unwrap();
  bayer16_to::<12, _>(
    &le_frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut le_sinker,
  )
  .unwrap();

  // BE under test (u16 out) — output is host-native, only the input
  // wire order differs.
  let be_plane: std::vec::Vec<u16> = logical.iter().map(|&v| be_wire_u16(v)).collect();
  let be_frame = Bayer12BeFrame::try_new(&be_plane, w, h, w).unwrap();
  let mut be_rgb = std::vec![0u16; (w * h * 3) as usize];
  let mut be_sinker = MixedSinker::<Bayer16<12, true>>::new(w as usize, h as usize)
    .with_rgb_u16(&mut be_rgb)
    .unwrap();
  bayer16_to_endian::<12, true, _>(
    &be_frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut be_sinker,
  )
  .unwrap();

  assert_eq!(be_rgb, le_rgb, "BE/LE u16 RGB mismatch");
}

/// Proves the byte-swap actually fires: feeding the *BE-encoded* bytes
/// through the LE marker (the wrong byte order) yields different output
/// than the correct BE path, given asymmetric samples. If the BE path
/// silently read host-native (no swap), the two would coincide.
#[test]
fn bayer16_be_byte_swap_is_load_bearing() {
  use crate::{
    frame::{Bayer12BeFrame, Bayer12Frame},
    raw::{
      BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to,
      bayer16_to_endian,
    },
  };
  let (w, h) = (8u32, 6u32);
  // Logical samples bounded to `< 0x10` so that the *same raw BE bytes*
  // read back through the LE marker (i.e. `logical << 8`) also stay
  // inside the 12-bit low-packed range — both frame constructors must
  // accept the plane for the comparison to be meaningful.
  let (r, g, b) = (0x00Fu16, 0x00Au16, 0x003u16);

  // Shared logical samples; the BE plane derives from them host-
  // independently. The "wrong" reading below reinterprets the *same*
  // BE bytes through the LE marker — so it must stay the be_plane, not
  // an le_wire-encoded oracle, for the divergence to be meaningful.
  let logical = solid_pattern12(BayerPattern::Rggb, w, h, r, g, b);

  // Correct BE path.
  let be_plane: std::vec::Vec<u16> = logical.iter().map(|&v| be_wire_u16(v)).collect();
  let be_frame = Bayer12BeFrame::try_new(&be_plane, w, h, w).unwrap();
  let mut be_rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut be_sinker = MixedSinker::<Bayer16<12, true>>::new(w as usize, h as usize)
    .with_rgb(&mut be_rgb)
    .unwrap();
  bayer16_to_endian::<12, true, _>(
    &be_frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut be_sinker,
  )
  .unwrap();

  // Same raw bytes, but interpreted by the LE marker (wrong order).
  // Each sample reads as `v << 8` (bounded < 4096 by `v < 0x10`), so
  // the plane is still low-packed-valid but numerically different.
  let wrong_frame = Bayer12Frame::try_new(&be_plane, w, h, w).unwrap();
  let mut wrong_rgb = std::vec![0u8; (w * h * 3) as usize];
  let mut wrong_sinker = MixedSinker::<Bayer16<12>>::new(w as usize, h as usize)
    .with_rgb(&mut wrong_rgb)
    .unwrap();
  bayer16_to::<12, _>(
    &wrong_frame,
    BayerPattern::Rggb,
    BayerDemosaic::Bilinear,
    WhiteBalance::neutral(),
    ColorCorrectionMatrix::identity(),
    &mut wrong_sinker,
  )
  .unwrap();

  assert_ne!(
    be_rgb, wrong_rgb,
    "BE byte-swap not applied: BE path coincides with the wrong-endian LE reading"
  );
}

/// Per-depth BE round-trip: 10 / 14 / 16-bit BE planes each match their
/// LE twin (u8 out). Exercises the `BITS`-generic monomorphization of
/// the BE kernel across every depth, not just 12-bit.
#[test]
fn bayer16_be_matches_le_per_depth() {
  use crate::{
    frame::{
      Bayer10BeFrame, Bayer10Frame, Bayer14BeFrame, Bayer14Frame, Bayer16BeFrame, Bayer16Frame,
    },
    raw::{
      BayerDemosaic, BayerPattern, ColorCorrectionMatrix, WhiteBalance, bayer16_to,
      bayer16_to_endian,
    },
  };
  let (w, h) = (8u32, 6u32);

  macro_rules! depth_case {
    ($bits:literal, $le_frame:ident, $be_frame:ident, $r:expr, $g:expr, $b:expr) => {{
      // Build the logical samples once (reuse the 12-bit pattern helper
      // shape; the values are within range for this depth), then derive
      // both wire planes from them so the comparison holds on any host.
      let logical = solid_pattern12(BayerPattern::Rggb, w, h, $r, $g, $b);
      let le_plane: std::vec::Vec<u16> = logical.iter().map(|&v| le_wire_u16(v)).collect();

      let le_frame = $le_frame::try_new(&le_plane, w, h, w).unwrap();
      let mut le_rgb = std::vec![0u8; (w * h * 3) as usize];
      let mut le_sinker =
        MixedSinker::<Bayer16<$bits, false>>::new(w as usize, h as usize)
          .with_rgb(&mut le_rgb)
          .unwrap();
      bayer16_to::<$bits, _>(
        &le_frame,
        BayerPattern::Rggb,
        BayerDemosaic::Bilinear,
        WhiteBalance::neutral(),
        ColorCorrectionMatrix::identity(),
        &mut le_sinker,
      )
      .unwrap();

      let be_plane: std::vec::Vec<u16> = logical.iter().map(|&v| be_wire_u16(v)).collect();
      let be_frame = $be_frame::try_new(&be_plane, w, h, w).unwrap();
      let mut be_rgb = std::vec![0u8; (w * h * 3) as usize];
      let mut be_sinker =
        MixedSinker::<Bayer16<$bits, true>>::new(w as usize, h as usize)
          .with_rgb(&mut be_rgb)
          .unwrap();
      bayer16_to_endian::<$bits, true, _>(
        &be_frame,
        BayerPattern::Rggb,
        BayerDemosaic::Bilinear,
        WhiteBalance::neutral(),
        ColorCorrectionMatrix::identity(),
        &mut be_sinker,
      )
      .unwrap();

      assert_eq!(be_rgb, le_rgb, "BE/LE mismatch at {} bits", $bits);
    }};
  }

  // 10-bit: max 1023; 14-bit: max 16383; 16-bit: full u16.
  depth_case!(10, Bayer10Frame, Bayer10BeFrame, 0x03FF, 0x02AB, 0x0123);
  depth_case!(14, Bayer14Frame, Bayer14BeFrame, 0x3FFF, 0x2ABC, 0x1234);
  depth_case!(16, Bayer16Frame, Bayer16BeFrame, 0xFFFF, 0xABCD, 0x1234);
}