feat: add basic imgutils support

This commit is contained in:
meh
2021-12-13 17:14:01 +01:00
parent 0a6a9cb151
commit f2e986a472
3 changed files with 43 additions and 4 deletions
-4
View File
@@ -318,10 +318,6 @@ impl Video {
)
}
}
pub fn as_image(&self) -> crate::util::Image {
crate::util::Image(self)
}
}
impl Deref for Video {
+42
View File
@@ -0,0 +1,42 @@
use crate::{format::Pixel, sys::*, util::frame, Error};
pub fn buffer_size(format: Pixel, width: u32, height: u32, align: u32) -> crate::Result<usize> {
unsafe {
match av_image_get_buffer_size(format.into(), width as i32, height as i32, align as i32) {
e if e < 0 => Err(Error::from(e)),
size => Ok(size as usize),
}
}
}
pub fn to_buffer(source: &frame::Video) -> crate::Result<Vec<u8>> {
let mut buffer = Vec::new();
into_buffer(source, &mut buffer)?;
Ok(buffer)
}
pub fn into_buffer(source: &frame::Video, buffer: &mut Vec<u8>) -> crate::Result<()> {
let size = buffer_size(source.format(), source.width(), source.height(), 1)?;
buffer.resize(size, 0);
let written = unsafe {
match av_image_copy_to_buffer(
buffer.as_mut_ptr(),
size as i32,
(*source.as_ptr()).data.as_ptr() as *mut _,
(*source.as_ptr()).linesize.as_ptr() as *mut _,
source.format().into(),
source.width() as i32,
source.height() as i32,
1,
) {
e if e < 0 => Err(Error::from(e)),
size => Ok(size),
}
}?;
buffer.shrink_to(written as usize);
Ok(())
}
+1
View File
@@ -14,6 +14,7 @@ pub mod picture;
pub mod range;
pub mod rational;
pub mod time;
pub mod image;
#[cfg(feature = "log")]
pub mod log;