fast image operations
box drawing
bendn 2023-09-11
parent 1dab1af · commit 5c00d74
-rw-r--r--Cargo.toml2
-rw-r--r--src/drawing/box.rs65
-rw-r--r--src/drawing/mod.rs3
3 files changed, 68 insertions, 2 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e568401..fc94d39 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "fimg"
-version = "0.3.4"
+version = "0.4.0"
authors = ["bend-n <[email protected]>"]
license = "MIT"
edition = "2021"
diff --git a/src/drawing/box.rs b/src/drawing/box.rs
new file mode 100644
index 0000000..294569a
--- /dev/null
+++ b/src/drawing/box.rs
@@ -0,0 +1,65 @@
+//! Box<cat>
+use crate::Image;
+
+impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
+ /// Draw a bordered box
+ ///
+ /// # Safety
+ ///
+ /// UB if the box is out of bounds
+ /// ```
+ /// # use fimg::Image;
+ /// let mut b = Image::alloc(10, 9);
+ /// unsafe { b.as_mut().r#box((1, 1), 7, 6, [255]) };
+ /// # assert_eq!(b.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\x00\x00\x00\x00\x00\x00\xff\x00\x00\xff\x00\x00\x00\x00\x00\x00\xff\x00\x00\xff\x00\x00\x00\x00\x00\x00\xff\x00\x00\xff\x00\x00\x00\x00\x00\x00\xff\x00\x00\xff\x00\x00\x00\x00\x00\x00\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
+ /// ```
+ pub unsafe fn r#box(
+ &mut self,
+ (x1, y1): (u32, u32),
+ width: u32,
+ height: u32,
+ c: [u8; CHANNELS],
+ ) {
+ // skip sides, leave that to second loop
+ for x in x1 + 1..width + x1 {
+ // top line
+ // SAFETY: responsibility is on caller
+ unsafe { self.set_pixel(x, x1, c) };
+ // bottom line
+ // SAFETY: shift responsibility
+ unsafe { self.set_pixel(x, x1 + height, c) };
+ }
+ for y in y1..=height + y1 {
+ // SAFETY: >> responsibility
+ unsafe { self.set_pixel(y1, y, c) };
+ // SAFETY: << responsibility
+ unsafe { self.set_pixel(y1 + width, y, c) };
+ }
+ }
+
+ /// Draw a *filled* box.
+ ///
+ /// # Safety
+ ///
+ /// UB if box is out of bounds
+ /// ```
+ /// # use fimg::Image;
+ /// let mut b = Image::alloc(10, 9);
+ /// unsafe { b.as_mut().filled_box((1, 1), 7, 6, [255]) };
+ /// # assert_eq!(b.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
+ /// ```
+ pub unsafe fn filled_box(
+ &mut self,
+ (x1, y1): (u32, u32),
+ width: u32,
+ height: u32,
+ c: [u8; CHANNELS],
+ ) {
+ for x in x1..=width + x1 {
+ for y in y1..=height + y1 {
+ // SAFETY: fill it
+ unsafe { self.set_pixel(x, y, c) };
+ }
+ }
+ }
+}
diff --git a/src/drawing/mod.rs b/src/drawing/mod.rs
index 8437083..6ffdf23 100644
--- a/src/drawing/mod.rs
+++ b/src/drawing/mod.rs
@@ -1,3 +1,4 @@
-//! contains drawing operations, like line drawing and triangle drawing
+//! contains drawing operations, like {line, box, triangle} drawing
+mod r#box;
mod line;
mod tri;