v4l/format/
mod.rs

1use bitflags::bitflags;
2use std::{convert::TryFrom, fmt, mem};
3
4use crate::v4l_sys::*;
5
6pub mod colorspace;
7pub use colorspace::Colorspace;
8
9pub mod description;
10pub use description::Description;
11
12pub mod field;
13pub use field::FieldOrder;
14
15pub mod fourcc;
16pub use fourcc::FourCC;
17
18pub mod quantization;
19pub use quantization::Quantization;
20
21pub mod transfer;
22pub use transfer::TransferFunction;
23
24bitflags! {
25    #[allow(clippy::unreadable_literal)]
26    pub struct Flags : u32 {
27        const PREMUL_ALPHA  = 0x00000001;
28    }
29}
30
31impl From<u32> for Flags {
32    fn from(flags: u32) -> Self {
33        Self::from_bits_truncate(flags)
34    }
35}
36
37impl From<Flags> for u32 {
38    fn from(flags: Flags) -> Self {
39        flags.bits()
40    }
41}
42
43impl fmt::Display for Flags {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        fmt::Debug::fmt(self, f)
46    }
47}
48
49#[derive(Debug, Copy, Clone)]
50/// Streaming format (single-planar)
51pub struct Format {
52    /// width in pixels
53    pub width: u32,
54    /// height in pixels
55    pub height: u32,
56    /// pixelformat code
57    pub fourcc: FourCC,
58    /// field order for interlacing
59    pub field_order: FieldOrder,
60
61    /// bytes per line
62    pub stride: u32,
63    /// maximum number of bytes required to store an image
64    pub size: u32,
65
66    /// flags set by the application or driver
67    pub flags: Flags,
68
69    /// supplements the pixelformat (fourcc) information
70    pub colorspace: Colorspace,
71    /// the way colors are mapped
72    pub quantization: Quantization,
73    /// the transfer function for the colorspace
74    pub transfer: TransferFunction,
75}
76
77impl Format {
78    /// Returns a capture format
79    ///
80    /// # Arguments
81    ///
82    /// * `width` - Width in pixels
83    /// * `height` - Height in pixels
84    /// * `fourcc` - Four character code (pixelformat)
85    ///
86    /// # Example
87    ///
88    /// ```
89    /// use v4l::{Format, FourCC};
90    /// let fmt = Format::new(640, 480, FourCC::new(b"YUYV"));
91    /// ```
92    pub const fn new(width: u32, height: u32, fourcc: FourCC) -> Self {
93        Format {
94            width,
95            height,
96            fourcc,
97            field_order: FieldOrder::Any,
98            stride: 0,
99            size: 0,
100            flags: Flags::empty(),
101            colorspace: Colorspace::Default,
102            quantization: Quantization::Default,
103            transfer: TransferFunction::Default,
104        }
105    }
106}
107
108impl fmt::Display for Format {
109    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110        writeln!(f, "width          : {}", self.width)?;
111        writeln!(f, "height         : {}", self.height)?;
112        writeln!(f, "fourcc         : {}", self.fourcc)?;
113        writeln!(f, "field          : {}", self.field_order)?;
114        writeln!(f, "stride         : {}", self.stride)?;
115        writeln!(f, "size           : {}", self.size)?;
116        writeln!(f, "colorspace     : {}", self.colorspace)?;
117        writeln!(f, "quantization   : {}", self.quantization)?;
118        writeln!(f, "transfer       : {}", self.transfer)?;
119        Ok(())
120    }
121}
122
123impl From<v4l2_pix_format> for Format {
124    fn from(fmt: v4l2_pix_format) -> Self {
125        Self {
126            width: fmt.width,
127            height: fmt.height,
128            fourcc: FourCC::from(fmt.pixelformat),
129            field_order: FieldOrder::try_from(fmt.field).expect("Invalid field order"),
130            stride: fmt.bytesperline,
131            size: fmt.sizeimage,
132            flags: Flags::from(fmt.flags),
133            colorspace: Colorspace::try_from(fmt.colorspace).expect("Invalid colorspace"),
134            quantization: Quantization::try_from(fmt.quantization).expect("Invalid quantization"),
135            transfer: TransferFunction::try_from(fmt.xfer_func).expect("Invalid transfer function"),
136        }
137    }
138}
139
140impl From<Format> for v4l2_pix_format {
141    fn from(format: Format) -> Self {
142        Self {
143            width: format.width,
144            height: format.height,
145            pixelformat: format.fourcc.into(),
146            field: format.field_order as u32,
147            bytesperline: format.stride,
148            sizeimage: format.size,
149            colorspace: format.colorspace as u32,
150            flags: format.flags.into(),
151            quantization: format.quantization as u32,
152            xfer_func: format.transfer as u32,
153            ..unsafe { mem::zeroed() }
154        }
155    }
156}