fast image operations
-rw-r--r--src/indexed.rs1
-rw-r--r--src/indexed/builder.rs20
2 files changed, 21 insertions, 0 deletions
diff --git a/src/indexed.rs b/src/indexed.rs
index 426565d..e0f4412 100644
--- a/src/indexed.rs
+++ b/src/indexed.rs
@@ -107,6 +107,7 @@ impl<I, P> IndexedImage<I, P> {
.ok_or("not all indexes are in palette")
}
}
+
impl<P, I: uint> IndexedImage<Box<[MaybeUninit<I>]>, P> {
/// Assumes this MU image is, in fact, initialized. You must be very sure that it is. Also, none of the pixels can be out of bounds.
pub unsafe fn assume_init(self) -> IndexedImage<Box<[I]>, P> {
diff --git a/src/indexed/builder.rs b/src/indexed/builder.rs
index c774712..b00c12f 100644
--- a/src/indexed/builder.rs
+++ b/src/indexed/builder.rs
@@ -85,4 +85,24 @@ impl<B, P> Builder<B, P> {
palette: self.palette.expect("require palette"),
}
}
+
+ /// Safety
+ ///
+ /// read implementation
+ pub unsafe fn from_iter<I: uint, J>(
+ self,
+ iter: impl Iterator<Item = ((u32, u32), I)> + ExactSizeIterator,
+ ) -> Image<Box<[I]>, P>
+ where
+ P: AsRef<[J]>,
+ {
+ debug_assert!(iter.len() >= self.width as usize * self.height as usize);
+ let mut me = self.uninit();
+ // SAFETY: the pixel must be in bounds
+ iter.for_each(|((x, y), item)| unsafe {
+ me.pixel_mut(x, y).write(item);
+ });
+ // SAFETY: all pixels must have been visited at least once
+ unsafe { me.assume_init() }
+ }
}