oximedia-core 0.1.3

Core types and traits for OxiMedia
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
//! Pixel format conversion functions.
//!
//! This module provides functions to convert between different pixel formats,
//! including YUV and RGB color spaces with proper color matrix support.
#![allow(
    clippy::similar_names,
    clippy::many_single_char_names,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::trivially_copy_pass_by_ref
)]

/// Color matrix standard for YUV<->RGB conversion.
///
/// Different standards define different coefficients for converting between
/// YUV and RGB color spaces.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColorMatrix {
    /// ITU-R BT.601 standard (Standard Definition TV).
    ///
    /// Used for SD content (480p, 576p).
    Bt601,

    /// ITU-R BT.709 standard (High Definition TV).
    ///
    /// Used for HD content (720p, 1080p).
    Bt709,
}

impl ColorMatrix {
    /// Returns the Y coefficient for RGB to YUV conversion.
    const fn kr(&self) -> f32 {
        match self {
            Self::Bt601 => 0.299,
            Self::Bt709 => 0.2126,
        }
    }

    /// Returns the Y coefficient for RGB to YUV conversion.
    const fn kb(&self) -> f32 {
        match self {
            Self::Bt601 => 0.114,
            Self::Bt709 => 0.0722,
        }
    }

    /// Returns the derived green coefficient.
    fn kg(&self) -> f32 {
        1.0 - self.kr() - self.kb()
    }
}

/// Pixel format converter with pre-computed lookup tables.
///
/// This struct provides efficient conversion using pre-computed tables
/// to avoid repeated floating-point calculations.
#[derive(Clone, Debug)]
pub struct PixelConverter {
    /// Lookup table for Y to RGB conversion.
    y_table: [i32; 256],
    /// Lookup table for U to R conversion.
    u_r_table: [i32; 256],
    /// Lookup table for U to G conversion.
    u_g_table: [i32; 256],
    /// Lookup table for V to G conversion.
    v_g_table: [i32; 256],
    /// Lookup table for V to B conversion.
    v_b_table: [i32; 256],
    /// RGB to Y coefficients (scaled).
    r_y: i32,
    /// RGB to Y coefficients (scaled).
    g_y: i32,
    /// RGB to Y coefficients (scaled).
    b_y: i32,
    /// RGB to U coefficients (scaled).
    r_u: i32,
    /// RGB to U coefficients (scaled).
    g_u: i32,
    /// RGB to U coefficients (scaled).
    b_u: i32,
    /// RGB to V coefficients (scaled).
    r_v: i32,
    /// RGB to V coefficients (scaled).
    g_v: i32,
    /// RGB to V coefficients (scaled).
    b_v: i32,
}

impl PixelConverter {
    /// Creates a new pixel converter for the specified color matrix.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_core::convert::pixel::{PixelConverter, ColorMatrix};
    ///
    /// let converter = PixelConverter::new(ColorMatrix::Bt709);
    /// ```
    #[must_use]
    #[allow(clippy::similar_names)]
    pub fn new(matrix: ColorMatrix) -> Self {
        let kr = matrix.kr();
        let kb = matrix.kb();
        let kg = matrix.kg();

        // Pre-compute YUV to RGB tables
        let mut y_table = [0i32; 256];
        let mut u_r_table = [0i32; 256];
        let mut u_g_table = [0i32; 256];
        let mut v_g_table = [0i32; 256];
        let mut v_b_table = [0i32; 256];

        for i in 0..256 {
            let y = i as f32 - 16.0;
            let u = i as f32 - 128.0;
            let v = i as f32 - 128.0;

            y_table[i] = (y * 1.164).round() as i32;
            u_r_table[i] = (v * 2.0 * (1.0 - kr)).round() as i32;
            v_b_table[i] = (u * 2.0 * (1.0 - kb)).round() as i32;
            u_g_table[i] = (u * 2.0 * (1.0 - kb) * kb / kg).round() as i32;
            v_g_table[i] = (v * 2.0 * (1.0 - kr) * kr / kg).round() as i32;
        }

        // Pre-compute RGB to YUV coefficients (scaled by 65536 for fixed-point)
        let scale = 65536.0;
        let r_y = (kr * scale).round() as i32;
        let g_y = (kg * scale).round() as i32;
        let b_y = (kb * scale).round() as i32;

        let r_u = ((-0.5 * kr / (1.0 - kb)) * scale).round() as i32;
        let g_u = ((-0.5 * kg / (1.0 - kb)) * scale).round() as i32;
        let b_u = ((0.5) * scale).round() as i32;

        let r_v = ((0.5) * scale).round() as i32;
        let g_v = ((-0.5 * kg / (1.0 - kr)) * scale).round() as i32;
        let b_v = ((-0.5 * kb / (1.0 - kr)) * scale).round() as i32;

        Self {
            y_table,
            u_r_table,
            u_g_table,
            v_g_table,
            v_b_table,
            r_y,
            g_y,
            b_y,
            r_u,
            g_u,
            b_u,
            r_v,
            g_v,
            b_v,
        }
    }

    /// Converts a single YUV pixel to RGB.
    ///
    /// Returns (R, G, B) in range [0, 255].
    #[must_use]
    #[inline]
    #[allow(clippy::too_many_lines, clippy::similar_names)]
    pub fn yuv_to_rgb(&self, y: u8, u: u8, v: u8) -> (u8, u8, u8) {
        let y_val = self.y_table[y as usize];
        let u_r = self.u_r_table[v as usize];
        let u_g = self.u_g_table[u as usize];
        let v_g = self.v_g_table[v as usize];
        let v_b = self.v_b_table[u as usize];

        let r = (y_val + u_r).clamp(0, 255);
        let g = (y_val - u_g - v_g).clamp(0, 255);
        let b = (y_val + v_b).clamp(0, 255);

        (r as u8, g as u8, b as u8)
    }

    /// Converts a single RGB pixel to YUV.
    ///
    /// Returns (Y, U, V) in range [0, 255].
    #[must_use]
    #[inline]
    #[allow(clippy::similar_names)]
    pub fn rgb_to_yuv(&self, r: u8, g: u8, b: u8) -> (u8, u8, u8) {
        let r = i32::from(r);
        let g = i32::from(g);
        let b = i32::from(b);

        let y = ((r * self.r_y + g * self.g_y + b * self.b_y) >> 16) + 16;
        let u = ((r * self.r_u + g * self.g_u + b * self.b_u) >> 16) + 128;
        let v = ((r * self.r_v + g * self.g_v + b * self.b_v) >> 16) + 128;

        (
            y.clamp(0, 255) as u8,
            u.clamp(0, 255) as u8,
            v.clamp(0, 255) as u8,
        )
    }
}

impl Default for PixelConverter {
    fn default() -> Self {
        Self::new(ColorMatrix::Bt709)
    }
}

/// Converts `YUV420p` to RGB24.
///
/// `YUV420p` is a planar format with Y plane at full resolution and U/V planes
/// at half resolution in both dimensions.
///
/// # Arguments
///
/// * `y_plane` - Y (luma) plane data
/// * `u_plane` - U (chroma) plane data (width/2 * height/2)
/// * `v_plane` - V (chroma) plane data (width/2 * height/2)
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
/// * `matrix` - Color matrix to use for conversion
///
/// # Returns
///
/// RGB24 packed data (width * height * 3 bytes)
///
/// # Panics
///
/// Panics if input planes have incorrect sizes.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::{yuv420p_to_rgb24, ColorMatrix};
///
/// let width = 4;
/// let height = 4;
/// let y_plane = vec![128u8; width * height];
/// let u_plane = vec![128u8; (width / 2) * (height / 2)];
/// let v_plane = vec![128u8; (width / 2) * (height / 2)];
///
/// let rgb = yuv420p_to_rgb24(&y_plane, &u_plane, &v_plane, width, height, ColorMatrix::Bt709);
/// assert_eq!(rgb.len(), width * height * 3);
/// ```
#[must_use]
pub fn yuv420p_to_rgb24(
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    width: usize,
    height: usize,
    matrix: ColorMatrix,
) -> Vec<u8> {
    assert_eq!(y_plane.len(), width * height);
    assert_eq!(u_plane.len(), (width / 2) * (height / 2));
    assert_eq!(v_plane.len(), (width / 2) * (height / 2));

    let converter = PixelConverter::new(matrix);
    let mut rgb = vec![0u8; width * height * 3];

    for y in 0..height {
        for x in 0..width {
            let y_val = y_plane[y * width + x];
            let u_val = u_plane[(y / 2) * (width / 2) + (x / 2)];
            let v_val = v_plane[(y / 2) * (width / 2) + (x / 2)];

            let (r, g, b) = converter.yuv_to_rgb(y_val, u_val, v_val);

            let offset = (y * width + x) * 3;
            rgb[offset] = r;
            rgb[offset + 1] = g;
            rgb[offset + 2] = b;
        }
    }

    rgb
}

/// Converts RGB24 to `YUV420p`.
///
/// RGB24 is a packed format with R, G, B bytes interleaved.
/// `YUV420p` has separate planes with chroma subsampling.
///
/// # Arguments
///
/// * `rgb` - RGB24 packed data (width * height * 3 bytes)
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
/// * `matrix` - Color matrix to use for conversion
///
/// # Returns
///
/// Tuple of (Y plane, U plane, V plane)
///
/// # Panics
///
/// Panics if RGB data has incorrect size.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::{rgb24_to_yuv420p, ColorMatrix};
///
/// let width = 4;
/// let height = 4;
/// let rgb = vec![128u8; width * height * 3];
///
/// let (y_plane, u_plane, v_plane) = rgb24_to_yuv420p(&rgb, width, height, ColorMatrix::Bt709);
/// assert_eq!(y_plane.len(), width * height);
/// assert_eq!(u_plane.len(), (width / 2) * (height / 2));
/// assert_eq!(v_plane.len(), (width / 2) * (height / 2));
/// ```
#[must_use]
#[allow(clippy::similar_names)]
pub fn rgb24_to_yuv420p(
    rgb: &[u8],
    width: usize,
    height: usize,
    matrix: ColorMatrix,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
    assert_eq!(rgb.len(), width * height * 3);

    let converter = PixelConverter::new(matrix);
    let mut y_plane = vec![0u8; width * height];
    let mut u_plane = vec![0u8; (width / 2) * (height / 2)];
    let mut v_plane = vec![0u8; (width / 2) * (height / 2)];

    // Convert RGB to YUV and downsample chroma
    for y in 0..height {
        for x in 0..width {
            let offset = (y * width + x) * 3;
            let r = rgb[offset];
            let g = rgb[offset + 1];
            let b = rgb[offset + 2];

            let (y_val, u_val, v_val) = converter.rgb_to_yuv(r, g, b);
            y_plane[y * width + x] = y_val;

            // Subsample chroma (average 2x2 blocks)
            if y % 2 == 0 && x % 2 == 0 {
                let mut u_sum = u32::from(u_val);
                let mut v_sum = u32::from(v_val);
                let mut count = 1;

                // Average with neighboring pixels if they exist
                if x + 1 < width {
                    let offset = (y * width + x + 1) * 3;
                    let (_, u, v) =
                        converter.rgb_to_yuv(rgb[offset], rgb[offset + 1], rgb[offset + 2]);
                    u_sum += u32::from(u);
                    v_sum += u32::from(v);
                    count += 1;
                }
                if y + 1 < height {
                    let offset = ((y + 1) * width + x) * 3;
                    let (_, u, v) =
                        converter.rgb_to_yuv(rgb[offset], rgb[offset + 1], rgb[offset + 2]);
                    u_sum += u32::from(u);
                    v_sum += u32::from(v);
                    count += 1;
                }
                if x + 1 < width && y + 1 < height {
                    let offset = ((y + 1) * width + x + 1) * 3;
                    let (_, u, v) =
                        converter.rgb_to_yuv(rgb[offset], rgb[offset + 1], rgb[offset + 2]);
                    u_sum += u32::from(u);
                    v_sum += u32::from(v);
                    count += 1;
                }

                let u_idx = (y / 2) * (width / 2) + (x / 2);
                u_plane[u_idx] = (u_sum / count) as u8;
                v_plane[u_idx] = (v_sum / count) as u8;
            }
        }
    }

    (y_plane, u_plane, v_plane)
}

/// Converts `YUV420p` to `YUV444p`.
///
/// `YUV444p` has full chroma resolution (no subsampling).
/// This upsamples the chroma planes using bilinear interpolation.
///
/// # Arguments
///
/// * `y_plane` - Y (luma) plane data
/// * `u_plane` - U (chroma) plane data (width/2 * height/2)
/// * `v_plane` - V (chroma) plane data (width/2 * height/2)
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// Tuple of (Y plane, U plane, V plane) all at full resolution
///
/// # Panics
///
/// Panics if input planes have incorrect sizes.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::yuv420p_to_yuv444p;
///
/// let width = 4;
/// let height = 4;
/// let y_plane = vec![128u8; width * height];
/// let u_plane = vec![100u8; (width / 2) * (height / 2)];
/// let v_plane = vec![150u8; (width / 2) * (height / 2)];
///
/// let (y_out, u_out, v_out) = yuv420p_to_yuv444p(&y_plane, &u_plane, &v_plane, width, height);
/// assert_eq!(y_out.len(), width * height);
/// assert_eq!(u_out.len(), width * height);
/// assert_eq!(v_out.len(), width * height);
/// ```
#[must_use]
pub fn yuv420p_to_yuv444p(
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    width: usize,
    height: usize,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
    assert_eq!(y_plane.len(), width * height);
    assert_eq!(u_plane.len(), (width / 2) * (height / 2));
    assert_eq!(v_plane.len(), (width / 2) * (height / 2));

    let y_out = y_plane.to_vec();
    let mut u_out = vec![0u8; width * height];
    let mut v_out = vec![0u8; width * height];

    let chroma_width = width / 2;

    // Upsample chroma using bilinear interpolation
    for y in 0..height {
        for x in 0..width {
            let cx = x / 2;
            let cy = y / 2;
            #[allow(clippy::cast_precision_loss)]
            let fx = (x % 2) as f32 * 0.5;
            #[allow(clippy::cast_precision_loss)]
            let fy = (y % 2) as f32 * 0.5;

            let c00_idx = cy * chroma_width + cx;
            let c10_idx = if cx + 1 < chroma_width {
                c00_idx + 1
            } else {
                c00_idx
            };
            let c01_idx = if cy + 1 < height / 2 {
                (cy + 1) * chroma_width + cx
            } else {
                c00_idx
            };
            let c11_idx = if cx + 1 < chroma_width && cy + 1 < height / 2 {
                (cy + 1) * chroma_width + cx + 1
            } else {
                c00_idx
            };

            // Bilinear interpolation for U
            let u00 = f32::from(u_plane[c00_idx]);
            let u10 = f32::from(u_plane[c10_idx]);
            let u01 = f32::from(u_plane[c01_idx]);
            let u11 = f32::from(u_plane[c11_idx]);

            let u_top = u00 * (1.0 - fx) + u10 * fx;
            let u_bottom = u01 * (1.0 - fx) + u11 * fx;
            let u_val = u_top * (1.0 - fy) + u_bottom * fy;

            // Bilinear interpolation for V
            let v00 = f32::from(v_plane[c00_idx]);
            let v10 = f32::from(v_plane[c10_idx]);
            let v01 = f32::from(v_plane[c01_idx]);
            let v11 = f32::from(v_plane[c11_idx]);

            let v_top = v00 * (1.0 - fx) + v10 * fx;
            let v_bottom = v01 * (1.0 - fx) + v11 * fx;
            let v_val = v_top * (1.0 - fy) + v_bottom * fy;

            let out_idx = y * width + x;
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            {
                u_out[out_idx] = u_val.round().clamp(0.0, 255.0) as u8;
                v_out[out_idx] = v_val.round().clamp(0.0, 255.0) as u8;
            }
        }
    }

    (y_out, u_out, v_out)
}

/// Converts `YUV444p` to `YUV420p`.
///
/// This downsamples the chroma planes by averaging 2x2 blocks.
///
/// # Arguments
///
/// * `y_plane` - Y (luma) plane data
/// * `u_plane` - U (chroma) plane data at full resolution
/// * `v_plane` - V (chroma) plane data at full resolution
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// Tuple of (Y plane, U plane, V plane) with subsampled chroma
///
/// # Panics
///
/// Panics if input planes have incorrect sizes.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::yuv444p_to_yuv420p;
///
/// let width = 4;
/// let height = 4;
/// let y_plane = vec![128u8; width * height];
/// let u_plane = vec![100u8; width * height];
/// let v_plane = vec![150u8; width * height];
///
/// let (y_out, u_out, v_out) = yuv444p_to_yuv420p(&y_plane, &u_plane, &v_plane, width, height);
/// assert_eq!(y_out.len(), width * height);
/// assert_eq!(u_out.len(), (width / 2) * (height / 2));
/// assert_eq!(v_out.len(), (width / 2) * (height / 2));
/// ```
#[must_use]
pub fn yuv444p_to_yuv420p(
    y_plane: &[u8],
    u_plane: &[u8],
    v_plane: &[u8],
    width: usize,
    height: usize,
) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
    assert_eq!(y_plane.len(), width * height);
    assert_eq!(u_plane.len(), width * height);
    assert_eq!(v_plane.len(), width * height);

    let y_out = y_plane.to_vec();
    let mut u_out = vec![0u8; (width / 2) * (height / 2)];
    let mut v_out = vec![0u8; (width / 2) * (height / 2)];

    // Downsample chroma by averaging 2x2 blocks
    for y in (0..height).step_by(2) {
        for x in (0..width).step_by(2) {
            let mut u_sum = 0u32;
            let mut v_sum = 0u32;

            for dy in 0..2 {
                for dx in 0..2 {
                    let idx = (y + dy) * width + (x + dx);
                    u_sum += u32::from(u_plane[idx]);
                    v_sum += u32::from(v_plane[idx]);
                }
            }

            let out_idx = (y / 2) * (width / 2) + (x / 2);
            #[allow(clippy::cast_possible_truncation)]
            {
                u_out[out_idx] = (u_sum / 4) as u8;
                v_out[out_idx] = (v_sum / 4) as u8;
            }
        }
    }

    (y_out, u_out, v_out)
}

/// Converts `YUV420p` to grayscale (8-bit).
///
/// Simply extracts the Y (luma) plane.
///
/// # Arguments
///
/// * `y_plane` - Y (luma) plane data
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// Grayscale image data
///
/// # Panics
///
/// Panics if Y plane has incorrect size.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::yuv420p_to_gray8;
///
/// let width = 4;
/// let height = 4;
/// let y_plane = vec![128u8; width * height];
///
/// let gray = yuv420p_to_gray8(&y_plane, width, height);
/// assert_eq!(gray.len(), width * height);
/// ```
#[must_use]
pub fn yuv420p_to_gray8(y_plane: &[u8], width: usize, height: usize) -> Vec<u8> {
    assert_eq!(y_plane.len(), width * height);
    y_plane.to_vec()
}

/// Converts RGB24 to grayscale (8-bit).
///
/// Uses standard luminance formula: Y = 0.299*R + 0.587*G + 0.114*B
///
/// # Arguments
///
/// * `rgb` - RGB24 packed data
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// Grayscale image data
///
/// # Panics
///
/// Panics if RGB data has incorrect size.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::rgb24_to_gray8;
///
/// let width = 4;
/// let height = 4;
/// let rgb = vec![128u8; width * height * 3];
///
/// let gray = rgb24_to_gray8(&rgb, width, height);
/// assert_eq!(gray.len(), width * height);
/// ```
#[must_use]
pub fn rgb24_to_gray8(rgb: &[u8], width: usize, height: usize) -> Vec<u8> {
    assert_eq!(rgb.len(), width * height * 3);

    let mut gray = vec![0u8; width * height];

    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    for (i, gray_val) in gray.iter_mut().enumerate().take(width * height) {
        let offset = i * 3;
        let r = f32::from(rgb[offset]);
        let g = f32::from(rgb[offset + 1]);
        let b = f32::from(rgb[offset + 2]);

        let y = (0.299 * r + 0.587 * g + 0.114 * b)
            .round()
            .clamp(0.0, 255.0);
        *gray_val = y as u8;
    }

    gray
}

/// Converts grayscale (8-bit) to RGB24.
///
/// Replicates the grayscale value across all three channels.
///
/// # Arguments
///
/// * `gray` - Grayscale image data
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// RGB24 packed data
///
/// # Panics
///
/// Panics if grayscale data has incorrect size.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::gray8_to_rgb24;
///
/// let width = 4;
/// let height = 4;
/// let gray = vec![128u8; width * height];
///
/// let rgb = gray8_to_rgb24(&gray, width, height);
/// assert_eq!(rgb.len(), width * height * 3);
/// ```
#[must_use]
pub fn gray8_to_rgb24(gray: &[u8], width: usize, height: usize) -> Vec<u8> {
    assert_eq!(gray.len(), width * height);

    let mut rgb = vec![0u8; width * height * 3];

    for (i, &val) in gray.iter().enumerate().take(width * height) {
        let offset = i * 3;
        rgb[offset] = val;
        rgb[offset + 1] = val;
        rgb[offset + 2] = val;
    }

    rgb
}

/// Converts grayscale (8-bit) to `YUV420p`.
///
/// Sets Y plane to grayscale values and U/V planes to neutral (128).
///
/// # Arguments
///
/// * `gray` - Grayscale image data
/// * `width` - Frame width in pixels
/// * `height` - Frame height in pixels
///
/// # Returns
///
/// Tuple of (Y plane, U plane, V plane)
///
/// # Panics
///
/// Panics if grayscale data has incorrect size.
///
/// # Examples
///
/// ```
/// use oximedia_core::convert::pixel::gray8_to_yuv420p;
///
/// let width = 4;
/// let height = 4;
/// let gray = vec![128u8; width * height];
///
/// let (y_plane, u_plane, v_plane) = gray8_to_yuv420p(&gray, width, height);
/// assert_eq!(y_plane.len(), width * height);
/// assert_eq!(u_plane.len(), (width / 2) * (height / 2));
/// assert_eq!(v_plane.len(), (width / 2) * (height / 2));
/// ```
#[must_use]
pub fn gray8_to_yuv420p(gray: &[u8], width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
    assert_eq!(gray.len(), width * height);

    let y_plane = gray.to_vec();
    let u_plane = vec![128u8; (width / 2) * (height / 2)];
    let v_plane = vec![128u8; (width / 2) * (height / 2)];

    (y_plane, u_plane, v_plane)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_color_matrix_coefficients() {
        let bt601 = ColorMatrix::Bt601;
        let bt709 = ColorMatrix::Bt709;

        // BT.601 coefficients
        assert!((bt601.kr() - 0.299).abs() < f32::EPSILON);
        assert!((bt601.kb() - 0.114).abs() < f32::EPSILON);

        // BT.709 coefficients
        assert!((bt709.kr() - 0.2126).abs() < f32::EPSILON);
        assert!((bt709.kb() - 0.0722).abs() < f32::EPSILON);
    }

    #[test]
    fn test_pixel_converter_yuv_to_rgb() {
        let converter = PixelConverter::new(ColorMatrix::Bt709);

        // Test neutral gray (Y=128, U=128, V=128)
        let (r, g, b) = converter.yuv_to_rgb(128, 128, 128);
        // Should be approximately gray
        assert!((i16::from(r) - i16::from(g)).abs() < 20);
        assert!((i16::from(g) - i16::from(b)).abs() < 20);
    }

    #[test]
    fn test_yuv420p_to_rgb24() {
        let width = 4;
        let height = 4;
        let y_plane = vec![128u8; width * height];
        let u_plane = vec![128u8; (width / 2) * (height / 2)];
        let v_plane = vec![128u8; (width / 2) * (height / 2)];

        let rgb = yuv420p_to_rgb24(
            &y_plane,
            &u_plane,
            &v_plane,
            width,
            height,
            ColorMatrix::Bt709,
        );
        assert_eq!(rgb.len(), width * height * 3);
    }

    #[test]
    fn test_rgb24_to_yuv420p() {
        let width = 4;
        let height = 4;
        let rgb = vec![128u8; width * height * 3];

        let (y_plane, u_plane, v_plane) = rgb24_to_yuv420p(&rgb, width, height, ColorMatrix::Bt709);
        assert_eq!(y_plane.len(), width * height);
        assert_eq!(u_plane.len(), (width / 2) * (height / 2));
        assert_eq!(v_plane.len(), (width / 2) * (height / 2));
    }

    #[test]
    fn test_yuv420p_to_yuv444p() {
        let width = 4;
        let height = 4;
        let y_plane = vec![128u8; width * height];
        let u_plane = vec![100u8; (width / 2) * (height / 2)];
        let v_plane = vec![150u8; (width / 2) * (height / 2)];

        let (y_out, u_out, v_out) = yuv420p_to_yuv444p(&y_plane, &u_plane, &v_plane, width, height);
        assert_eq!(y_out.len(), width * height);
        assert_eq!(u_out.len(), width * height);
        assert_eq!(v_out.len(), width * height);
    }

    #[test]
    fn test_yuv444p_to_yuv420p() {
        let width = 4;
        let height = 4;
        let y_plane = vec![128u8; width * height];
        let u_plane = vec![100u8; width * height];
        let v_plane = vec![150u8; width * height];

        let (y_out, u_out, v_out) = yuv444p_to_yuv420p(&y_plane, &u_plane, &v_plane, width, height);
        assert_eq!(y_out.len(), width * height);
        assert_eq!(u_out.len(), (width / 2) * (height / 2));
        assert_eq!(v_out.len(), (width / 2) * (height / 2));
    }

    #[test]
    fn test_grayscale_conversions() {
        let width = 4;
        let height = 4;

        // Test YUV420p to gray
        let y_plane = vec![128u8; width * height];
        let gray = yuv420p_to_gray8(&y_plane, width, height);
        assert_eq!(gray.len(), width * height);
        assert_eq!(gray[0], 128);

        // Test RGB to gray
        let rgb = vec![128u8; width * height * 3];
        let gray = rgb24_to_gray8(&rgb, width, height);
        assert_eq!(gray.len(), width * height);

        // Test gray to RGB
        let rgb = gray8_to_rgb24(&gray, width, height);
        assert_eq!(rgb.len(), width * height * 3);

        // Test gray to YUV420p
        let (y, u, v) = gray8_to_yuv420p(&gray, width, height);
        assert_eq!(y.len(), width * height);
        assert_eq!(u.len(), (width / 2) * (height / 2));
        assert_eq!(v.len(), (width / 2) * (height / 2));
        assert_eq!(u[0], 128); // Neutral chroma
        assert_eq!(v[0], 128); // Neutral chroma
    }

    #[test]
    fn test_roundtrip_rgb_yuv() {
        let width = 4;
        let height = 4;
        let mut rgb = vec![0u8; width * height * 3];

        // Create a simple pattern
        for i in 0..rgb.len() {
            rgb[i] = ((i * 50) % 256) as u8;
        }

        // Convert RGB -> YUV -> RGB
        let (y, u, v) = rgb24_to_yuv420p(&rgb, width, height, ColorMatrix::Bt709);
        let rgb2 = yuv420p_to_rgb24(&y, &u, &v, width, height, ColorMatrix::Bt709);

        assert_eq!(rgb.len(), rgb2.len());
        // Due to chroma subsampling, we expect some loss.
        // Verify rgb2 is non-empty (all u8 values are inherently <= 255).
        assert!(!rgb2.is_empty());
    }
}