screencapturekit/stream/configuration/
pixel_format.rs

1//! Pixel format enumeration
2//!
3//! Defines the available pixel formats for captured frames.
4
5use core::fmt;
6use std::fmt::{Display, Formatter};
7
8use crate::utils::four_char_code::FourCharCode;
9
10/// Pixel format for captured video frames
11///
12/// Specifies the layout and encoding of pixel data in captured frames.
13///
14/// # Examples
15///
16/// ```
17/// use screencapturekit::stream::configuration::PixelFormat;
18///
19/// let format = PixelFormat::BGRA;
20/// println!("Format: {}", format); // Prints "BGRA"
21/// ```
22#[allow(non_camel_case_types)]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
24pub enum PixelFormat {
25    /// Packed little endian ARGB8888 (most common)
26    #[default]
27    BGRA,
28    /// Packed little endian ARGB2101010 (10-bit color)
29    l10r,
30    /// Two-plane "video" range YCbCr 4:2:0
31    YCbCr_420v,
32    /// Two-plane "full" range YCbCr 4:2:0
33    YCbCr_420f,
34    /// Two-plane "full" range `YCbCr10` 4:4:4 (10-bit)
35    xf44,
36    /// 64-bit RGBA IEEE half-precision float, 16-bit little-endian (HDR)
37    RGhA,
38}
39impl Display for PixelFormat {
40    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41        let c: FourCharCode = (*self).into();
42        write!(f, "{}", c.display())
43    }
44}
45
46impl From<PixelFormat> for FourCharCode {
47    fn from(val: PixelFormat) -> Self {
48        // Use infallible byte array constructor for compile-time constants
49        match val {
50            PixelFormat::BGRA => Self::from_bytes(*b"BGRA"),
51            PixelFormat::l10r => Self::from_bytes(*b"l10r"),
52            PixelFormat::YCbCr_420v => Self::from_bytes(*b"420v"),
53            PixelFormat::YCbCr_420f => Self::from_bytes(*b"420f"),
54            PixelFormat::xf44 => Self::from_bytes(*b"xf44"),
55            PixelFormat::RGhA => Self::from_bytes(*b"RGhA"),
56        }
57    }
58}
59impl From<u32> for PixelFormat {
60    fn from(value: u32) -> Self {
61        // FourCharCode stores u32 directly, no byte conversion needed
62        let c = FourCharCode::from_u32(value);
63        c.into()
64    }
65}
66impl From<FourCharCode> for PixelFormat {
67    fn from(val: FourCharCode) -> Self {
68        match val.display().as_str() {
69            "l10r" => Self::l10r,
70            "420v" => Self::YCbCr_420v,
71            "420f" => Self::YCbCr_420f,
72            "xf44" => Self::xf44,
73            "RGhA" => Self::RGhA,
74            _ => Self::BGRA, // Default to BGRA for unknown formats (including "BGRA")
75        }
76    }
77}