diff --git a/rlottie/src/lib.rs b/rlottie/src/lib.rs index dc543e04..4fff8b5e 100644 --- a/rlottie/src/lib.rs +++ b/rlottie/src/lib.rs @@ -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 = BGRA; @@ -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 { + 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::() + ) + } + } + /// Returns an iterator over the pixels of the surface. pub fn pixels(&self) -> impl Iterator + '_ { let width = self.width(); @@ -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);