mirror of
https://github.com/bend-n/fimg.git
synced 2024-12-22 18:38:21 -06:00
make drawing functions safe
This commit is contained in:
parent
f74d3dc450
commit
1b5b06c36f
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "fimg"
|
name = "fimg"
|
||||||
version = "0.4.2"
|
version = "0.4.3"
|
||||||
authors = ["bend-n <bend.n@outlook.com>"]
|
authors = ["bend-n <bend.n@outlook.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
|
@ -1,65 +1,51 @@
|
||||||
//! `Box<cat>`
|
//! `Box<cat>`
|
||||||
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::Image;
|
use crate::Image;
|
||||||
|
|
||||||
impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
|
impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
|
||||||
/// Draw a bordered box
|
/// Draw a bordered box
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// UB if the box is out of bounds
|
|
||||||
/// ```
|
/// ```
|
||||||
/// # use fimg::Image;
|
/// # use fimg::Image;
|
||||||
/// let mut b = Image::alloc(10, 9);
|
/// let mut b = Image::alloc(10, 9);
|
||||||
/// unsafe { b.as_mut().r#box((1, 1), 7, 6, [255]) };
|
/// 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")
|
/// # 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(
|
pub fn r#box(&mut self, (x1, y1): (u32, u32), width: u32, height: u32, c: [u8; CHANNELS]) {
|
||||||
&mut self,
|
|
||||||
(x1, y1): (u32, u32),
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
c: [u8; CHANNELS],
|
|
||||||
) {
|
|
||||||
// skip sides, leave that to second loop
|
// skip sides, leave that to second loop
|
||||||
for x in x1 + 1..width + x1 {
|
for x in clamp(x1 + 1..width + x1, 0..self.width()) {
|
||||||
// top line
|
// top line
|
||||||
// SAFETY: responsibility is on caller
|
// SAFETY: clamped to bounds
|
||||||
unsafe { self.set_pixel(x, x1, c) };
|
unsafe { self.set_pixel(x, x1, c) };
|
||||||
// bottom line
|
// SAFETY: clamped to bounds
|
||||||
// SAFETY: shift responsibility
|
|
||||||
unsafe { self.set_pixel(x, x1 + height, c) };
|
unsafe { self.set_pixel(x, x1 + height, c) };
|
||||||
}
|
}
|
||||||
for y in y1..=height + y1 {
|
for y in clamp(y1..height + y1 + 1, 0..self.height()) {
|
||||||
// SAFETY: >> responsibility
|
// SAFETY: clamped to bounds
|
||||||
unsafe { self.set_pixel(y1, y, c) };
|
unsafe { self.set_pixel(y1, y, c) };
|
||||||
// SAFETY: << responsibility
|
// SAFETY: clamped to bounds
|
||||||
unsafe { self.set_pixel(y1 + width, y, c) };
|
unsafe { self.set_pixel(y1 + width, y, c) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draw a *filled* box.
|
/// Draw a *filled* box.
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// UB if box is out of bounds
|
|
||||||
/// ```
|
/// ```
|
||||||
/// # use fimg::Image;
|
/// # use fimg::Image;
|
||||||
/// let mut b = Image::alloc(10, 9);
|
/// let mut b = Image::alloc(10, 9);
|
||||||
/// unsafe { b.as_mut().filled_box((1, 1), 7, 6, [255]) };
|
/// 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")
|
/// # 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(
|
pub fn filled_box(&mut self, (x1, y1): (u32, u32), width: u32, height: u32, c: [u8; CHANNELS]) {
|
||||||
&mut self,
|
for x in clamp(x1..1 + width + x1, 0..self.width()) {
|
||||||
(x1, y1): (u32, u32),
|
for y in clamp(y1..1 + height + y1, 0..self.height()) {
|
||||||
width: u32,
|
// SAFETY: clamped to bounds
|
||||||
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) };
|
unsafe { self.set_pixel(x, y, c) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// clamp a range with another range
|
||||||
|
fn clamp(r: Range<u32>, within: Range<u32>) -> Range<u32> {
|
||||||
|
r.start.clamp(within.start, within.end)..r.end.clamp(within.start, within.end)
|
||||||
|
}
|
||||||
|
|
|
@ -3,19 +3,15 @@ use crate::Image;
|
||||||
|
|
||||||
impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
|
impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
|
||||||
/// Draw a (filled) triangle
|
/// Draw a (filled) triangle
|
||||||
///
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// UB if any point is out of bounds
|
|
||||||
/// ```
|
/// ```
|
||||||
/// # use fimg::*;
|
/// # use fimg::*;
|
||||||
/// let mut a = Image::alloc(10, 10);
|
/// let mut a = Image::alloc(10, 10);
|
||||||
/// // draw a triangle from point a v point b v point c v
|
/// // draw a triangle from point a v point b v point c v
|
||||||
/// // with color white
|
/// // with color white
|
||||||
/// unsafe { a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]) };
|
/// a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]);
|
||||||
/// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
|
/// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||||
/// ```
|
/// ```
|
||||||
pub unsafe fn tri(
|
pub fn tri(
|
||||||
&mut self,
|
&mut self,
|
||||||
(x1, y1): (f32, f32),
|
(x1, y1): (f32, f32),
|
||||||
(x2, y2): (f32, f32),
|
(x2, y2): (f32, f32),
|
||||||
|
@ -33,10 +29,11 @@ impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let d = (x3 - x2) * (y as f32 - y2) - (y3 - y2) * (x as f32 - x2);
|
let d = (x3 - x2) * (y as f32 - y2) - (y3 - y2) * (x as f32 - x2);
|
||||||
if d == 0.0 || (d < 0.0) == (s + t <= 0.0) {
|
if (d == 0.0 || (d < 0.0) == (s + t <= 0.0))
|
||||||
// SAFETY:
|
&& x < self.width()
|
||||||
// caller gurantees triangle is in bounds, this loops over the
|
&& y < self.height()
|
||||||
// bounding box of the triangle, therefore this is fine.
|
{
|
||||||
|
// SAFETY: we just checked the bounds
|
||||||
unsafe { self.set_pixel(x, y, c) };
|
unsafe { self.set_pixel(x, y, c) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue