Skip to content

Commit

Permalink
Add data_as_bytes and into_data functions and AsRef impl for `S…
Browse files Browse the repository at this point in the history
…urface` (#26)
  • Loading branch information
msrd0 authored Oct 7, 2022
1 parent e3a9ea7 commit e588028
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion rlottie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ use rlottie_sys::*;
use std::{
ffi::CString,
fmt::{self, Debug},
mem,
os::unix::ffi::OsStrExt,
path::Path,
ptr::NonNull
ptr::NonNull,
slice
};

pub type Bgra<T = u8> = BGRA<T>;
Expand Down Expand Up @@ -127,6 +129,24 @@ impl Surface {
&self.data
}

/// Return the pixel data of the surface. You should prefer [`data()`] unless you
/// absolutely need owned access to the data.
pub fn into_data(self) -> Vec<Bgra> {
self.data
}

/// Return the raw pixel data of the surface.
pub fn data_as_bytes(&self) -> &[u8] {
// Safety: We are not mutating the surface data for the lifetime of the returned
// slice, and the memory was properly allocated by Vec, so this is fine.
unsafe {
slice::from_raw_parts(
self.data.as_ptr() as *const u8,
self.data.len() * mem::size_of::<Bgra>()
)
}
}

/// Returns an iterator over the pixels of the surface.
pub fn pixels(&self) -> impl Iterator<Item = (usize, usize, Bgra)> + '_ {
let width = self.width();
Expand All @@ -148,6 +168,12 @@ impl Surface {
}
}

impl AsRef<[u8]> for Surface {
fn as_ref(&self) -> &[u8] {
self.data_as_bytes()
}
}

/// A lottie animation.
pub struct Animation(NonNull<Lottie_Animation_S>);

Expand Down

0 comments on commit e588028

Please sign in to comment.