use std::{fmt, mem};
use crate::fourcc::FourCC;
use crate::v4l_sys::*;
#[derive(Debug, Copy, Clone)]
pub struct Format {
pub width: u32,
pub height: u32,
pub fourcc: FourCC,
pub stride: u32,
pub size: u32,
}
impl Format {
pub fn new(width: u32, height: u32, fourcc: FourCC) -> Self {
Format {
width,
height,
fourcc,
stride: 0,
size: 0,
}
}
}
impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "width : {}", self.width)?;
writeln!(f, "height : {}", self.height)?;
writeln!(f, "fourcc : {}", self.fourcc)?;
writeln!(f, "stride : {}", self.stride)?;
writeln!(f, "size : {}", self.size)?;
Ok(())
}
}
impl From<v4l2_pix_format> for Format {
fn from(fmt: v4l2_pix_format) -> Self {
Format {
width: fmt.width,
height: fmt.height,
fourcc: FourCC::from(fmt.pixelformat),
stride: fmt.bytesperline,
size: fmt.sizeimage,
}
}
}
impl Into<v4l2_pix_format> for Format {
fn into(self: Format) -> v4l2_pix_format {
let mut fmt: v4l2_pix_format;
unsafe {
fmt = mem::zeroed();
}
fmt.width = self.width;
fmt.height = self.height;
fmt.pixelformat = self.fourcc.into();
fmt.bytesperline = self.stride;
fmt.sizeimage = self.size;
fmt
}
}