add Image::boxed

This commit is contained in:
bendn 2023-10-04 11:32:03 +07:00
parent f465e25077
commit a0f555439e
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
2 changed files with 19 additions and 3 deletions

View file

@ -1,7 +1,7 @@
use fimg::*;
fn tri() {
let mut i: Image<_, 4> = fimg::make!(4 channels 1000 x 1000);
i.as_mut()
.tri((0., 0.), (1000., 500.), (0., 999.), [255, 255, 255, 255]);
let mut i: Image<_, 4> = fimg::make!(4 channels 1000 x 1000).boxed();
i.tri((0., 0.), (1000., 500.), (0., 999.), [255, 255, 255, 255]);
std::hint::black_box(i);
}
iai::main!(tri);

View file

@ -243,6 +243,22 @@ impl<const CHANNELS: usize> Image<&[u8], CHANNELS> {
}
}
impl<const CHANNELS: usize, const N: usize> Image<[u8; N], CHANNELS> {
/// Box this array image.
pub fn boxed(self) -> Image<Box<[u8]>, CHANNELS> {
// SAFETY: ctor
unsafe { Image::new(self.width, self.height, Box::new(self.buffer)) }
}
}
impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS> {
/// Box this owned image.
pub fn boxed(self) -> Image<Box<[u8]>, CHANNELS> {
// SAFETY: ctor
unsafe { Image::new(self.width, self.height, self.buffer.into_boxed_slice()) }
}
}
#[macro_export]
/// Create a <code>[Image]<[[u8]; N], C></code> with ease. If your looking for a <code>[Image]<&'static [[u8]]></code>, try [`Image::make`].
///