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
1033
1034
1035
//! Sinker impl for the Tier 9 packed-float-RGB **source** format
//! ([`Rgbf32`] — FFmpeg `AV_PIX_FMT_RGBF32`).
//!
//! Each pixel is `3 x f32` (linear `R, G, B`). Output paths:
//! - `with_rgb` — clamp `[0, 1]` x 255 → packed `R, G, B` u8
//!   (`rgbf32_to_rgb_row`).
//! - `with_rgba` — same conversion + constant `0xFF` alpha.
//! - `with_rgb_u16` — clamp `[0, 1]` x 65535 → packed `R, G, B` u16.
//! - `with_rgba_u16` — same + constant `0xFFFF` alpha.
//! - `with_rgb_f32` — **lossless** float pass-through (HDR values >
//!   1.0 and negatives are preserved).
//! - `with_luma` / `with_luma_u16` — staged through a u8 RGB scratch
//!   row (or the user's `with_rgb` buffer if attached) and the
//!   existing `rgb_to_luma_row` / `rgb_to_luma_u16_row` kernels —
//!   matches the design used by every other RGB-input sinker.
//! - `with_hsv` — same staging, then `rgb_to_hsv_row`.
//!
//! HDR values > 1.0 saturate to the integer output range; the float
//! output preserves them losslessly.

use super::{
  InsufficientBuffer, MixedSinker, MixedSinkerError, RowIndexOutOfRange, RowShapeMismatch,
  RowSlice, check_dimensions_match, rgb_row_buf_or_scratch, rgba_plane_row_slice,
  rgba_u16_plane_row_slice,
};
#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
use super::{
  packed_rgb_f32_resample, packed_rgb_f32_resample_emit, packed_rgb_f32_resample_filter,
};
use crate::{
  PixelSink,
  row::{
    rgb_to_hsv_row, rgb_to_luma_row, rgb_to_luma_u16_row, rgbf32_to_rgb_f32_row, rgbf32_to_rgb_row,
    rgbf32_to_rgb_u16_row, rgbf32_to_rgba_row, rgbf32_to_rgba_u16_row,
  },
  source::{Rgbf32, Rgbf32Row, Rgbf32Sink},
};

// ---- Rgbf32 impl -------------------------------------------------------

impl<'a, R, const BE: bool> MixedSinker<'a, Rgbf32<BE>, R> {
  /// Attaches a packed **8-bit** RGBA output buffer. Source values are
  /// clamped to `[0, 1]` and scaled by 255; alpha is forced to `0xFF`
  /// (the float source has no alpha channel).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
    self.set_rgba(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgba`](Self::with_rgba).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(4)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbaBuffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgba = Some(buf);
    Ok(self)
  }

  /// Attaches a `u16` RGB output buffer (`width x height x 3`
  /// elements). Each `f32` channel is clamped to `[0, 1]` and **scaled
  /// to the full u16 range** (x65535).
  ///
  /// # Naming consistency note
  ///
  /// Other source families' `with_rgb_u16` accessor preserves the
  /// source's *native integer precision* in a u16 carrier (e.g.
  /// 10-bit YUV stays in `[0, 1023]`). The `Rgbf32` variant has no
  /// native integer range to preserve, so it instead applies full-
  /// range scaling — a deliberate divergence to give callers a useful
  /// u16 output rather than refusing the operation.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_rgb_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgb_u16`](Self::with_rgb_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(3)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgb_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a `u16` RGBA output buffer. Same `[0, 1]` x 65535
  /// **full-range scaling** as
  /// [`with_rgb_u16`](Self::with_rgb_u16); alpha is forced to `0xFFFF`
  /// (the float source has no alpha channel). See
  /// [`with_rgb_u16`](Self::with_rgb_u16) for the divergence note vs
  /// integer-source families.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_rgba_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgba_u16`](Self::with_rgba_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(4)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgba_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a **`u16`** luma output buffer. Y' is computed at u8
  /// precision (matching `with_luma`'s output) and zero-extended to
  /// `u16` — same convention as the packed-YUV `with_luma_u16` family.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_luma_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_luma_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_luma_u16`](Self::with_luma_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_luma_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_pixels()?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientLumaU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.luma_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a packed f32 RGB output buffer. Lossless pass-through —
  /// HDR values > 1.0 and negative values are preserved bit-exact.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgb_f32(mut self, buf: &'a mut [f32]) -> Result<Self, MixedSinkerError> {
    self.set_rgb_f32(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgb_f32`](Self::with_rgb_f32).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgb_f32(&mut self, buf: &'a mut [f32]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(3)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbF32Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgb_f32 = Some(buf);
    Ok(self)
  }

  /// Runs the identity (no-resample) output derivation for one source
  /// row over every attached output buffer.
  ///
  /// This is the parity oracle: the byte-exact output every other path
  /// (including the area-resample tail) must reproduce for an identity
  /// plan. It recomputes `w`/`h`/`idx` from `self` + `row` and assumes
  /// the row shape and index are already validated by the caller.
  fn rgbf32_process_direct(
    &mut self,
    row: Rgbf32Row<'_>,
    use_simd: bool,
  ) -> Result<(), MixedSinkerError> {
    let w = self.width;
    let h = self.height;
    let idx = row.row();

    let Self {
      rgb,
      rgb_u16,
      rgb_f32,
      rgba,
      rgba_u16,
      luma,
      luma_u16,
      hsv,
      rgb_scratch,
      ..
    } = self;
    let one_plane_start = idx * w;
    let one_plane_end = one_plane_start + w;
    let rgb_in = row.rgb();

    // Lossless f32 pass-through is independent of all other paths —
    // emit it first so the SIMD memcpy doesn't share scratch usage
    // with downstream conversions.
    if let Some(buf) = rgb_f32.as_deref_mut() {
      let f32_start = one_plane_start * 3;
      let f32_end = one_plane_end * 3;
      rgbf32_to_rgb_f32_row::<BE>(rgb_in, &mut buf[f32_start..f32_end], w, use_simd);
    }

    // u16 RGB output — direct float→u16 conversion (no staging).
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let u16_start = one_plane_start * 3;
      let u16_end = one_plane_end * 3;
      rgbf32_to_rgb_u16_row::<BE>(rgb_in, &mut buf[u16_start..u16_end], w, use_simd);
    }

    // u16 RGBA output — direct float→u16 conversion (no staging).
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let rgba_row = rgba_u16_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
      rgbf32_to_rgba_u16_row::<BE>(rgb_in, rgba_row, w, use_simd);
    }

    // u8 RGBA standalone fast path — direct float→u8 conversion when
    // no RGB / luma / HSV consumer needs the staged u8 RGB row.
    let want_rgba_u8 = rgba.is_some();
    let want_rgb_u8 = rgb.is_some();
    let want_luma_u8 = luma.is_some();
    let want_luma_u16 = luma_u16.is_some();
    let want_hsv = hsv.is_some();
    let need_u8_rgb = want_rgb_u8 || want_luma_u8 || want_luma_u16 || want_hsv;

    if want_rgba_u8 && !need_u8_rgb {
      let rgba_buf = rgba.as_deref_mut().unwrap();
      let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
      rgbf32_to_rgba_row::<BE>(rgb_in, rgba_row, w, use_simd);
      return Ok(());
    }

    if !need_u8_rgb && !want_rgba_u8 {
      return Ok(());
    }

    // Stage the u8 RGB scratch row once. This is the same
    // rgb_scratch-sharing pattern the Bgr24 / Rgba / etc. sinkers use:
    // when the user requested an RGB output buffer it doubles as the
    // shared u8 RGB row; otherwise we use the lazily-grown scratch.
    let rgb_row = rgb_row_buf_or_scratch(
      rgb.as_deref_mut(),
      rgb_scratch,
      one_plane_start,
      one_plane_end,
      w,
      h,
    )?;
    rgbf32_to_rgb_row::<BE>(rgb_in, rgb_row, w, use_simd);

    if let Some(luma) = luma.as_deref_mut() {
      rgb_to_luma_row(
        rgb_row,
        &mut luma[one_plane_start..one_plane_end],
        w,
        row.matrix(),
        row.full_range(),
        use_simd,
      );
    }

    if let Some(luma_buf) = luma_u16.as_deref_mut() {
      rgb_to_luma_u16_row(
        rgb_row,
        &mut luma_buf[one_plane_start..one_plane_end],
        w,
        row.matrix(),
        row.full_range(),
        use_simd,
      );
    }

    if let Some(hsv) = hsv.as_mut() {
      let (h, s, v) = hsv.hsv();
      rgb_to_hsv_row(
        rgb_row,
        &mut h[one_plane_start..one_plane_end],
        &mut s[one_plane_start..one_plane_end],
        &mut v[one_plane_start..one_plane_end],
        w,
        use_simd,
      );
    }

    // u8 RGBA output (combined with RGB/luma/HSV path) — direct from
    // float source to keep alpha-fill cheap; the alternative would be
    // expanding from `rgb_row` via `expand_rgb_to_rgba_row`, which is
    // the same cost minus a pass over the float input. Direct is one
    // less memory pass for combined `with_rgb + with_rgba` callers.
    if let Some(buf) = rgba.as_deref_mut() {
      let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
      rgbf32_to_rgba_row::<BE>(rgb_in, rgba_row, w, use_simd);
    }

    Ok(())
  }
}

// The float area stream + its staging fields exist only when the engine
// is compiled in (`rgb-float` alone does not pull in the `AreaStream`
// machinery). When the engine is present the sink accepts any resampler
// `R` and routes a non-identity plan through the float area tail. When
// it is absent the sink is pinned to the identity-only `NoopResampler`
// (the default `R`): there is no `PixelSink` for any other `R`, so a
// `MixedSinker<Rgbf32, AreaResampler>` cannot be fed rows at all — the
// type-level fence that keeps output-sized buffers from being indexed
// with source offsets on a downscale.

#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
impl<R, const BE: bool> Rgbf32Sink<BE> for MixedSinker<'_, Rgbf32<BE>, R> {}

#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Rgbf32<BE>, R> {
  type Input<'r> = Rgbf32Row<'r>;
  type Error = MixedSinkerError;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn kernel_matrix(&self) -> crate::KernelMatrix {
    self.kernel_matrix
  }
  fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
    check_dimensions_match(self.width, self.height, width, height)?;
    if let Some(stream) = self.rgb_stream_f32.as_mut() {
      stream.reset();
    }
    if let Some(stream) = self.rgb_filter_stream_f32.as_mut() {
      stream.reset();
    }
    self.resample_outputs = None;
    Ok(())
  }

  fn process(&mut self, row: Rgbf32Row<'_>) -> Result<(), Self::Error> {
    let w = self.width;
    let idx = row.row();
    let use_simd = self.simd;

    if row.rgb().len() != w * 3 {
      return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
        RowSlice::RgbF32Packed,
        idx,
        w * 3,
        row.rgb().len(),
      )));
    }
    if idx >= self.height {
      return Err(MixedSinkerError::RowIndexOutOfRange(
        RowIndexOutOfRange::new(idx, self.height),
      ));
    }

    let Self {
      rgb,
      rgb_u16,
      rgb_f32,
      rgba,
      rgba_u16,
      luma,
      luma_u16,
      hsv,
      rgb_scratch,
      rgb_scratch_f32,
      rgb_stream_f32,
      rgb_filter_stream_f32,
      resample_outputs,
      plan,
      ..
    } = self;

    // Non-identity plan: convert the wire row to source-width host f32
    // RGB (lossless), bin it in float, then derive every attached
    // output from each finalized output row — `rgb_f32` copies the
    // binned row, every integer output mirrors the direct path's
    // clamp+scale kernels run over it. The span kind picks the engine —
    // float area or signed-coefficient filter (both bin the same staged
    // host-native f32 row and feed the same emit). f32 is full-range
    // float, so there is no native-depth clamp on the filter output.
    if let Some(plan) = plan.as_ref() {
      // Transactional resample (RFC #238 tail-collapse): the span kind picks the
      // shared 3-channel float driver (float area / signed-coefficient filter, both
      // binning the staged host-native f32 RGB row); each stages the wire row via
      // `convert` and derives every output via the `Rgbf32` `emit` (narrowed
      // outputs only — no `rgba_f32` / `rgb_f16` / `rgba_f16`).
      let matrix = row.matrix();
      let full_range = row.full_range();
      return match plan.kind() {
        crate::resample::SpanKind::Area => packed_rgb_f32_resample(
          rgb_stream_f32,
          resample_outputs,
          rgb,
          rgba,
          luma,
          rgb_u16,
          rgba_u16,
          luma_u16,
          rgb_f32,
          &mut None,
          &mut None,
          &mut None,
          hsv,
          rgb_scratch_f32,
          w,
          plan,
          idx,
          |src_f32| crate::row::rgbf32_to_rgb_f32_row::<BE>(row.rgb(), src_f32, w, use_simd),
          |stream, src, rgb, rgba, luma, rgb_u16, rgba_u16, luma_u16, rgb_f32, _, _, _, hsv| {
            packed_rgb_f32_resample_emit(
              stream,
              plan,
              rgb,
              rgba,
              luma,
              rgb_u16,
              rgba_u16,
              luma_u16,
              rgb_f32,
              hsv,
              src,
              rgb_scratch,
              matrix,
              full_range,
              idx,
              use_simd,
            )
          },
        ),
        crate::resample::SpanKind::Filter => packed_rgb_f32_resample_filter(
          rgb_filter_stream_f32,
          resample_outputs,
          rgb,
          rgba,
          luma,
          rgb_u16,
          rgba_u16,
          luma_u16,
          rgb_f32,
          &mut None,
          &mut None,
          &mut None,
          hsv,
          rgb_scratch_f32,
          w,
          plan,
          idx,
          |src_f32| crate::row::rgbf32_to_rgb_f32_row::<BE>(row.rgb(), src_f32, w, use_simd),
          |stream, src, rgb, rgba, luma, rgb_u16, rgba_u16, luma_u16, rgb_f32, _, _, _, hsv| {
            packed_rgb_f32_resample_emit(
              stream,
              plan,
              rgb,
              rgba,
              luma,
              rgb_u16,
              rgba_u16,
              luma_u16,
              rgb_f32,
              hsv,
              src,
              rgb_scratch,
              matrix,
              full_range,
              idx,
              use_simd,
            )
          },
        ),
      };
    }

    self.rgbf32_process_direct(row, use_simd)
  }
}

/// Identity-only fence for engine-absent builds.
///
/// Without the resample engine (`yuv-planar`/`rgb`) the float area
/// machinery is not compiled, so this sink is pinned to the default
/// [`NoopResampler`](crate::resample::NoopResampler): only the
/// identity output derivation exists, and no `PixelSink` is provided
/// for any non-identity resampler. That keeps a downscaling
/// `MixedSinker<Rgbf32, _>` from existing at the type level.
#[cfg(not(any(feature = "yuv-planar", feature = "rgb")))]
impl<const BE: bool> Rgbf32Sink<BE> for MixedSinker<'_, Rgbf32<BE>> {}

/// See the [`Rgbf32Sink`] fence note above: identity-only sink for
/// engine-absent builds, pinned to `NoopResampler`.
#[cfg(not(any(feature = "yuv-planar", feature = "rgb")))]
impl<const BE: bool> PixelSink for MixedSinker<'_, Rgbf32<BE>> {
  type Input<'r> = Rgbf32Row<'r>;
  type Error = MixedSinkerError;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn kernel_matrix(&self) -> crate::KernelMatrix {
    self.kernel_matrix
  }
  fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
    check_dimensions_match(self.width, self.height, width, height)
  }

  fn process(&mut self, row: Rgbf32Row<'_>) -> Result<(), Self::Error> {
    let w = self.width;
    let idx = row.row();
    let use_simd = self.simd;

    if row.rgb().len() != w * 3 {
      return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
        RowSlice::RgbF32Packed,
        idx,
        w * 3,
        row.rgb().len(),
      )));
    }
    if idx >= self.height {
      return Err(MixedSinkerError::RowIndexOutOfRange(
        RowIndexOutOfRange::new(idx, self.height),
      ));
    }

    self.rgbf32_process_direct(row, use_simd)
  }
}

// ---- Rgbaf32 impl (packed float RGBA — real source alpha) --------------
//
// The alpha-bearing twin of the [`Rgbf32`] sinker. Each pixel is
// `4 x f32` (linear `R, G, B, A`). Output paths mirror `Rgbf32` exactly,
// extended with the real source alpha:
// - `with_rgb` — clamp `[0, 1]` x 255 → packed `R, G, B` u8 (alpha dropped).
// - `with_rgba` — same RGB conversion + the source alpha (clamp x 255).
// - `with_rgb_u16` / `with_rgba_u16` — clamp x 65535 (full-range scaling;
//   alpha real for the RGBA form).
// - `with_rgb_f32` — lossless RGB pass-through (alpha dropped).
// - `with_rgba_f32` — lossless 4-channel pass-through (alpha preserved).
// - `with_luma` / `with_luma_u16` / `with_hsv` — staged through a u8 RGB
//   scratch row (alpha ignored), matching `Rgbf32`.

#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
use super::{
  packed_rgba_f32_packed_filter, packed_rgba_f32_packed_resample, packed_rgba_f32_resample_emit,
};
use crate::{
  row::{
    rgbaf32_to_rgb_f32_row, rgbaf32_to_rgb_row, rgbaf32_to_rgb_u16_row, rgbaf32_to_rgba_f32_row,
    rgbaf32_to_rgba_row, rgbaf32_to_rgba_u16_row,
  },
  source::{Rgbaf32, Rgbaf32Row, Rgbaf32Sink},
};

impl<'a, R, const BE: bool> MixedSinker<'a, Rgbaf32<BE>, R> {
  /// Attaches a packed **8-bit** RGBA output buffer. Colour channels are
  /// clamped to `[0, 1]` and scaled by 255; alpha is the **source** alpha,
  /// also clamped to `[0, 1]` and scaled by 255 (real per-pixel alpha).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
    self.set_rgba(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgba`](Self::with_rgba).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(4)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbaBuffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgba = Some(buf);
    Ok(self)
  }

  /// Attaches a `u16` RGB output buffer (`width x height x 3` elements).
  /// Each colour `f32` is clamped to `[0, 1]` and **scaled to the full u16
  /// range** (x65535); alpha dropped. See the [`Rgbf32`] `with_rgb_u16`
  /// divergence note vs integer-source families.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_rgb_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgb_u16`](Self::with_rgb_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(3)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgb_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a `u16` RGBA output buffer. Same `[0, 1]` x 65535 full-range
  /// scaling as [`with_rgb_u16`](Self::with_rgb_u16); alpha is the real
  /// source alpha (clamp x 65535).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_rgba_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgba_u16`](Self::with_rgba_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(4)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgba_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a **`u16`** luma output buffer. Y' is computed at u8
  /// precision and zero-extended to `u16` (alpha ignored).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_luma_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
    self.set_luma_u16(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_luma_u16`](Self::with_luma_u16).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_luma_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_pixels()?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientLumaU16Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.luma_u16 = Some(buf);
    Ok(self)
  }

  /// Attaches a packed f32 RGB output buffer. Lossless pass-through of the
  /// `R, G, B` channels (alpha dropped) — HDR values > 1.0 and negatives
  /// preserved bit-exact.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgb_f32(mut self, buf: &'a mut [f32]) -> Result<Self, MixedSinkerError> {
    self.set_rgb_f32(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgb_f32`](Self::with_rgb_f32).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgb_f32(&mut self, buf: &'a mut [f32]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(3)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbF32Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgb_f32 = Some(buf);
    Ok(self)
  }

  /// Attaches a packed f32 RGBA output buffer. **Lossless** 4-channel
  /// pass-through including the source alpha — HDR values > 1.0, negatives,
  /// NaN, and Inf are preserved bit-exact.
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn with_rgba_f32(mut self, buf: &'a mut [f32]) -> Result<Self, MixedSinkerError> {
    self.set_rgba_f32(buf)?;
    Ok(self)
  }
  /// In-place variant of [`with_rgba_f32`](Self::with_rgba_f32).
  #[cfg_attr(not(tarpaulin), inline(always))]
  pub fn set_rgba_f32(&mut self, buf: &'a mut [f32]) -> Result<&mut Self, MixedSinkerError> {
    let expected = self.frame_elems(4)?;
    if buf.len() < expected {
      return Err(MixedSinkerError::InsufficientRgbaF32Buffer(
        InsufficientBuffer::new(expected, buf.len()),
      ));
    }
    self.rgba_f32 = Some(buf);
    Ok(self)
  }

  /// Runs the identity (no-resample) output derivation for one source row
  /// over every attached output buffer — the parity oracle the
  /// area/filter-resample tail must reproduce for an identity plan.
  fn rgbaf32_process_direct(
    &mut self,
    row: Rgbaf32Row<'_>,
    use_simd: bool,
  ) -> Result<(), MixedSinkerError> {
    let w = self.width;
    let h = self.height;
    let idx = row.row();

    let Self {
      rgb,
      rgb_u16,
      rgb_f32,
      rgba,
      rgba_u16,
      rgba_f32,
      luma,
      luma_u16,
      hsv,
      rgb_scratch,
      ..
    } = self;
    let one_plane_start = idx * w;
    let one_plane_end = one_plane_start + w;
    let rgba_in = row.rgba();

    // Lossless 4-channel f32 pass-through — emit first (independent).
    if let Some(buf) = rgba_f32.as_deref_mut() {
      let start = one_plane_start * 4;
      let end = one_plane_end * 4;
      rgbaf32_to_rgba_f32_row::<BE>(rgba_in, &mut buf[start..end], w, use_simd);
    }

    // Lossless RGB f32 pass-through (alpha dropped).
    if let Some(buf) = rgb_f32.as_deref_mut() {
      let start = one_plane_start * 3;
      let end = one_plane_end * 3;
      rgbaf32_to_rgb_f32_row::<BE>(rgba_in, &mut buf[start..end], w, use_simd);
    }

    // u16 RGB output (drop alpha) — direct float→u16 conversion.
    if let Some(buf) = rgb_u16.as_deref_mut() {
      let start = one_plane_start * 3;
      let end = one_plane_end * 3;
      rgbaf32_to_rgb_u16_row::<BE>(rgba_in, &mut buf[start..end], w, use_simd);
    }

    // u16 RGBA output (real alpha) — direct float→u16 conversion.
    if let Some(buf) = rgba_u16.as_deref_mut() {
      let rgba_row = rgba_u16_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
      rgbaf32_to_rgba_u16_row::<BE>(rgba_in, rgba_row, w, use_simd);
    }

    // u8 RGBA standalone fast path — direct float→u8 when no RGB / luma /
    // HSV consumer needs the staged u8 RGB row.
    let want_rgba_u8 = rgba.is_some();
    let want_rgb_u8 = rgb.is_some();
    let want_luma_u8 = luma.is_some();
    let want_luma_u16 = luma_u16.is_some();
    let want_hsv = hsv.is_some();
    let need_u8_rgb = want_rgb_u8 || want_luma_u8 || want_luma_u16 || want_hsv;

    if want_rgba_u8 && !need_u8_rgb {
      let rgba_buf = rgba.as_deref_mut().unwrap();
      let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
      rgbaf32_to_rgba_row::<BE>(rgba_in, rgba_row, w, use_simd);
      return Ok(());
    }

    if !need_u8_rgb && !want_rgba_u8 {
      return Ok(());
    }

    // Stage the u8 RGB scratch row once (alpha dropped). When the user
    // requested an RGB output buffer it doubles as the shared u8 RGB row.
    let rgb_row = rgb_row_buf_or_scratch(
      rgb.as_deref_mut(),
      rgb_scratch,
      one_plane_start,
      one_plane_end,
      w,
      h,
    )?;
    rgbaf32_to_rgb_row::<BE>(rgba_in, rgb_row, w, use_simd);

    if let Some(luma) = luma.as_deref_mut() {
      rgb_to_luma_row(
        rgb_row,
        &mut luma[one_plane_start..one_plane_end],
        w,
        row.matrix(),
        row.full_range(),
        use_simd,
      );
    }

    if let Some(luma_buf) = luma_u16.as_deref_mut() {
      rgb_to_luma_u16_row(
        rgb_row,
        &mut luma_buf[one_plane_start..one_plane_end],
        w,
        row.matrix(),
        row.full_range(),
        use_simd,
      );
    }

    if let Some(hsv) = hsv.as_mut() {
      let (h, s, v) = hsv.hsv();
      rgb_to_hsv_row(
        rgb_row,
        &mut h[one_plane_start..one_plane_end],
        &mut s[one_plane_start..one_plane_end],
        &mut v[one_plane_start..one_plane_end],
        w,
        use_simd,
      );
    }

    // u8 RGBA output (combined with RGB/luma/HSV path) — direct from the
    // float source so the real source alpha lands in slot 3.
    if let Some(buf) = rgba.as_deref_mut() {
      let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
      rgbaf32_to_rgba_row::<BE>(rgba_in, rgba_row, w, use_simd);
    }

    Ok(())
  }
}

#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
impl<R, const BE: bool> Rgbaf32Sink<BE> for MixedSinker<'_, Rgbaf32<BE>, R> {}

#[cfg(any(feature = "yuv-planar", feature = "rgb"))]
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Rgbaf32<BE>, R> {
  type Input<'r> = Rgbaf32Row<'r>;
  type Error = MixedSinkerError;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn kernel_matrix(&self) -> crate::KernelMatrix {
    self.kernel_matrix
  }
  fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
    check_dimensions_match(self.width, self.height, width, height)?;
    if let Some(stream) = self.rgba_stream_f32.as_mut() {
      stream.reset();
    }
    if let Some(stream) = self.rgba_filter_stream_f32.as_mut() {
      stream.reset();
    }
    self.resample_outputs = None;
    Ok(())
  }

  fn process(&mut self, row: Rgbaf32Row<'_>) -> Result<(), Self::Error> {
    let w = self.width;
    let idx = row.row();
    let use_simd = self.simd;

    if row.rgba().len() != w * 4 {
      return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
        RowSlice::RgbaF32Packed,
        idx,
        w * 4,
        row.rgba().len(),
      )));
    }
    if idx >= self.height {
      return Err(MixedSinkerError::RowIndexOutOfRange(
        RowIndexOutOfRange::new(idx, self.height),
      ));
    }

    let Self {
      rgb,
      rgb_u16,
      rgb_f32,
      rgba,
      rgba_u16,
      rgba_f32,
      luma,
      luma_u16,
      hsv,
      rgb_scratch,
      rgba_scratch_f32,
      rgba_stream_f32,
      rgba_filter_stream_f32,
      resample_outputs,
      plan,
      ..
    } = self;

    // Non-identity plan: convert the wire row to source-width host-native
    // packed RGBA f32 (lossless), bin all four channels (straight alpha),
    // then derive every attached output from each finalized output row via
    // the exact direct `rgbaf32_*` kernels. The span kind picks the engine
    // — float area or signed-coefficient filter. f32 is full-range, so
    // there is no native-depth clamp on the filter output.
    if let Some(plan) = plan.as_ref() {
      // Transactional resample (RFC #238 tail-collapse): the span kind picks the
      // shared 4-channel packed-float driver (float area / signed-coefficient
      // filter, both binning the staged host-native f32 RGBA row, straight alpha);
      // each stages the wire row via `convert` and derives every output via the
      // `Rgbaf32` `emit` (`rgb_f32` / `rgba_f32` lossless, no `rgb_f16` /
      // `rgba_f16`).
      let matrix = row.matrix();
      let full_range = row.full_range();
      return match plan.kind() {
        crate::resample::SpanKind::Area => packed_rgba_f32_packed_resample(
          rgba_stream_f32,
          resample_outputs,
          rgb,
          rgba,
          luma,
          rgb_u16,
          rgba_u16,
          luma_u16,
          rgb_f32,
          rgba_f32,
          &mut None,
          &mut None,
          hsv,
          rgba_scratch_f32,
          w,
          plan,
          idx,
          |src_rgba| crate::row::rgbaf32_to_rgba_f32_row::<BE>(row.rgba(), src_rgba, w, use_simd),
          |stream,
           src,
           rgb,
           rgba,
           luma,
           rgb_u16,
           rgba_u16,
           luma_u16,
           rgb_f32,
           rgba_f32,
           _,
           _,
           hsv| {
            packed_rgba_f32_resample_emit(
              stream,
              plan,
              rgb,
              rgba,
              luma,
              rgb_u16,
              rgba_u16,
              luma_u16,
              rgb_f32,
              rgba_f32,
              hsv,
              src,
              rgb_scratch,
              matrix,
              full_range,
              idx,
              use_simd,
            )
          },
        ),
        crate::resample::SpanKind::Filter => packed_rgba_f32_packed_filter(
          rgba_filter_stream_f32,
          resample_outputs,
          rgb,
          rgba,
          luma,
          rgb_u16,
          rgba_u16,
          luma_u16,
          rgb_f32,
          rgba_f32,
          &mut None,
          &mut None,
          hsv,
          rgba_scratch_f32,
          w,
          plan,
          idx,
          |src_rgba| crate::row::rgbaf32_to_rgba_f32_row::<BE>(row.rgba(), src_rgba, w, use_simd),
          |stream,
           src,
           rgb,
           rgba,
           luma,
           rgb_u16,
           rgba_u16,
           luma_u16,
           rgb_f32,
           rgba_f32,
           _,
           _,
           hsv| {
            packed_rgba_f32_resample_emit(
              stream,
              plan,
              rgb,
              rgba,
              luma,
              rgb_u16,
              rgba_u16,
              luma_u16,
              rgb_f32,
              rgba_f32,
              hsv,
              src,
              rgb_scratch,
              matrix,
              full_range,
              idx,
              use_simd,
            )
          },
        ),
      };
    }

    self.rgbaf32_process_direct(row, use_simd)
  }
}

/// Identity-only fence for engine-absent builds (see the [`Rgbf32`] note).
#[cfg(not(any(feature = "yuv-planar", feature = "rgb")))]
impl<const BE: bool> Rgbaf32Sink<BE> for MixedSinker<'_, Rgbaf32<BE>> {}

/// See the [`Rgbaf32Sink`] fence note above: identity-only sink for
/// engine-absent builds, pinned to `NoopResampler`.
#[cfg(not(any(feature = "yuv-planar", feature = "rgb")))]
impl<const BE: bool> PixelSink for MixedSinker<'_, Rgbaf32<BE>> {
  type Input<'r> = Rgbaf32Row<'r>;
  type Error = MixedSinkerError;

  #[cfg_attr(not(tarpaulin), inline(always))]
  fn kernel_matrix(&self) -> crate::KernelMatrix {
    self.kernel_matrix
  }
  fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
    check_dimensions_match(self.width, self.height, width, height)
  }

  fn process(&mut self, row: Rgbaf32Row<'_>) -> Result<(), Self::Error> {
    let w = self.width;
    let idx = row.row();
    let use_simd = self.simd;

    if row.rgba().len() != w * 4 {
      return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
        RowSlice::RgbaF32Packed,
        idx,
        w * 4,
        row.rgba().len(),
      )));
    }
    if idx >= self.height {
      return Err(MixedSinkerError::RowIndexOutOfRange(
        RowIndexOutOfRange::new(idx, self.height),
      ));
    }

    self.rgbaf32_process_direct(row, use_simd)
  }
}