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}
35impl Display for PixelFormat {
36    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
37        let c: FourCharCode = (*self).into();
38        write!(f, "{}", c.display())
39    }
40}
41
42impl From<PixelFormat> for FourCharCode {
43    fn from(val: PixelFormat) -> Self {
44        // Use infallible byte array constructor for compile-time constants
45        match val {
46            PixelFormat::BGRA => Self::from_bytes(*b"BGRA"),
47            PixelFormat::l10r => Self::from_bytes(*b"l10r"),
48            PixelFormat::YCbCr_420v => Self::from_bytes(*b"420v"),
49            PixelFormat::YCbCr_420f => Self::from_bytes(*b"420f"),
50        }
51    }
52}
53impl From<u32> for PixelFormat {
54    fn from(value: u32) -> Self {
55        // Use infallible byte array constructor
56        let c = FourCharCode::from_bytes(value.to_le_bytes());
57        c.into()
58    }
59}
60impl From<FourCharCode> for PixelFormat {
61    fn from(val: FourCharCode) -> Self {
62        match val.display().as_str() {
63            "BGRA" => Self::BGRA,
64            "l10r" => Self::l10r,
65            "420v" => Self::YCbCr_420v,
66            "420f" => Self::YCbCr_420f,
67            _ => unreachable!("Unknown pixel format"),
68        }
69    }
70}