use std::fmt::Debug;
#[cfg(feature = "image")]
use image::ImageBuffer;
#[allow(unused_imports)]
use crate::{ColorMode, Decoder, Error};
pub struct Frame {
timestamp: i32,
frame_data: Vec<u8>,
#[allow(dead_code)]
color_mode: ColorMode,
dimensions: (u32, u32),
}
impl Frame {
pub(crate) fn new_from_decoder(
timestamp: i32,
color_mode: ColorMode,
frame_data: Vec<u8>,
dimensions: (u32, u32),
) -> Self {
Self {
timestamp,
color_mode,
frame_data,
dimensions,
}
}
pub fn dimensions(&self) -> (u32, u32) {
self.dimensions
}
pub fn color_mode(&self) -> ColorMode {
self.color_mode
}
pub fn timestamp(&self) -> i32 {
self.timestamp
}
pub fn data(&self) -> &[u8] {
&self.frame_data
}
#[cfg(feature = "image")]
pub fn into_image(self) -> Result<ImageBuffer<image::Rgba<u8>, Vec<u8>>, Error> {
self.into_rgba_image()
}
#[cfg(feature = "image")]
pub fn into_rgba_image(self) -> Result<ImageBuffer<image::Rgba<u8>, Vec<u8>>, Error> {
if self.color_mode != ColorMode::Rgba {
return Err(Error::WrongColorMode(self.color_mode, ColorMode::Rgba));
}
Ok(ImageBuffer::from_vec(self.dimensions.0, self.dimensions.1, self.frame_data).unwrap())
}
}
impl Debug for Frame {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Frame {{ timestamp: {}, frame_data: {}b }}",
self.timestamp,
self.frame_data.len()
)
}
}