add make()

This commit is contained in:
bendn 2023-09-06 18:36:44 +07:00
parent 58a769292b
commit 5139fa82f7
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
2 changed files with 27 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "fimg"
version = "0.3.0"
version = "0.3.1"
authors = ["bend-n <bend.n@outlook.com>"]
license = "MIT"
edition = "2021"

View file

@ -3,9 +3,11 @@
//! Provides fast image operations, such as rotation, flipping, and overlaying.
#![feature(
slice_swap_unchecked,
generic_const_exprs,
slice_as_chunks,
unchecked_math,
portable_simd,
const_option,
array_chunks,
test
)]
@ -19,7 +21,7 @@
clippy::dbg_macro,
missing_docs
)]
#![allow(clippy::zero_prefixed_literal)]
#![allow(clippy::zero_prefixed_literal, incomplete_features)]
use std::{num::NonZeroU32, slice::SliceIndex};
@ -140,6 +142,29 @@ impl<const CHANNELS: usize> Image<&[u8], CHANNELS> {
buffer: self.buffer,
}
}
/// Create a new immutable image of width x, y.
///
/// # Panics
///
/// if width || height == 0
///
/// ```
/// # use fimg::Image;
/// let img = Image::make::<5, 5>();
/// # let img: Image<_, 4> = img;
/// ```
pub const fn make<'a, const WIDTH: u32, const HEIGHT: u32>() -> Image<&'a [u8], CHANNELS>
where
[(); CHANNELS * WIDTH as usize * HEIGHT as usize]: Sized,
{
Image {
width: NonZeroU32::new(WIDTH).expect("passed zero width to builder"),
height: NonZeroU32::new(HEIGHT).expect("passed zero height to builder"),
buffer: &[0; CHANNELS * WIDTH as usize * HEIGHT as usize],
}
}
}
impl<T: std::ops::Deref<Target = [u8]>, const CHANNELS: usize> Image<T, CHANNELS> {