fast image operations
Diffstat (limited to 'src/indexed/builder.rs')
-rw-r--r--src/indexed/builder.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/indexed/builder.rs b/src/indexed/builder.rs
new file mode 100644
index 0000000..90bf3c5
--- /dev/null
+++ b/src/indexed/builder.rs
@@ -0,0 +1,59 @@
+use super::{uint, IndexedImage as Image};
+use std::marker::PhantomData;
+impl<B, P> Image<B, P> {
+ /// creates a builder
+ pub const fn build<I, J>(w: u32, h: u32) -> Builder<B, P>
+ where
+ B: AsRef<[I]>,
+ P: AsRef<[J]>,
+ {
+ Builder::new(w, h)
+ }
+}
+
+/// Safe [`Image`] builder.
+#[must_use = "builder must be consumed"]
+pub struct Builder<B, P> {
+ /// the width in a zeroable type. zeroable so as to make the check in [`buf`] easier.
+ width: u32,
+ /// the height in a zeroable type.
+ height: u32,
+ palette: Option<P>,
+ #[allow(clippy::missing_docs_in_private_items)]
+ _buffer: PhantomData<B>,
+}
+impl<B, P> Builder<B, P> {
+ /// create new builder
+ pub const fn new(w: u32, h: u32) -> Self {
+ Self {
+ width: w,
+ height: h,
+ _buffer: PhantomData,
+ palette: None,
+ }
+ }
+
+ /// add a palette
+ pub fn pal(self, p: P) -> Self {
+ Self {
+ palette: Some(p),
+ ..self
+ }
+ }
+
+ /// apply a buffer, and build
+ #[track_caller]
+ #[must_use = "what is it going to do?"]
+ #[allow(private_bounds)]
+ pub fn buf<I: uint, J>(self, buffer: B) -> Image<B, P>
+ where
+ B: AsRef<[I]>,
+ P: AsRef<[J]>,
+ {
+ Image::from_raw_parts(
+ crate::Image::build(self.width, self.height).buf(buffer),
+ self.palette.expect("require palette"),
+ )
+ .unwrap()
+ }
+}