Skip to content

Commit

Permalink
Horizontal chunks and filling
Browse files Browse the repository at this point in the history
  • Loading branch information
madsravn committed Jul 21, 2023
1 parent f4b5d02 commit 9f127c3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
11 changes: 11 additions & 0 deletions examples/filling.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use pixtra::canvas::Canvas;
use pixtra::pixels::Pixel;
use pixtra::utility;
use std::path::Path;

fn main() {
Expand All @@ -17,6 +18,16 @@ fn main() {
.draw_square(20, 20, 60, 60, &color)
.fill(1, 1, &Pixel::new(172, 172, 172, 255));
canvas.save(Path::new("testing.png")).unwrap();

let canvas = Canvas::load(Path::new("assets/20230709_111142.jpg")).unwrap().rotate90();
println!("Size of canvas: {} x {}", canvas.dimensions().width, canvas.dimensions().height);
let subcanvas = canvas.get_subimage(1600, 50, 1400, 400);
let (center, distance) = utility::find_center_and_size(&subcanvas);
println!("center and distance = {center} and {distance}");
let canvas = canvas.fill_by_color_and_distance(1600, 50, &Pixel::new(0,0,0,0), &center, 100.0);

println!("Size of canvas: {} x {}", canvas.dimensions().width, canvas.dimensions().height);
canvas.save(Path::new("testing-fill-by-center-and-distance.png")).unwrap();
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions notes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Make sure that the examples also runs on the CI. Not only their tests.
Move all tests to `tests/`


Something about adding, subtracting pixels should result in a pixelbuilder
Expand Down
27 changes: 25 additions & 2 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,28 @@ impl Canvas {
}

pub fn vertical_chunks(&self, size_of_chunk: u32) -> Vec<Canvas> {
let pixels: Vec<Canvas> = self
let chunks: Vec<Canvas> = self
.pixels
.chunks((size_of_chunk * self.width) as usize)
.map(|x| Canvas::new_with_data(self.width, size_of_chunk, x.to_vec()))
.collect();

pixels
chunks
}

//Ugly version
//Clone?
pub fn horizontal_chunks(&self, size_of_chunk: u32) -> Vec<Canvas> {
let chunks: Vec<Canvas> = self
.clone()
.rotate90()
.pixels
.chunks((size_of_chunk * self.width) as usize)
.map(|x| Canvas::new_with_data(self.width, size_of_chunk, x.to_vec()))
.map(|x| x.rotate270())
.collect();

chunks
}

fn index_to_coordinate(&self, index: u32) -> (u32, u32) {
Expand Down Expand Up @@ -635,6 +650,13 @@ impl Canvas {

pub fn fill_by_distance(mut self, x: u32, y: u32, fill_color: &Pixel, distance: f32) -> Canvas {
let find_color = self.get_pixel(x, y);
self = self.fill_by_color_and_distance(x, y, fill_color, &find_color, distance);
self
}

pub fn fill_by_color_and_distance(mut self, x: u32, y: u32, fill_color: &Pixel, color: &Pixel, distance: f32) -> Canvas {
let find_color = color.clone();
// TODO: This might not work if you hit this exact color by accident?
if fill_color == &find_color {
return self;
}
Expand All @@ -657,6 +679,7 @@ impl Canvas {
self
}


// By orlp
pub fn fill(mut self, x: u32, y: u32, fill_color: &Pixel) -> Canvas {
let find_color = self.get_pixel(x, y);
Expand Down
21 changes: 21 additions & 0 deletions src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ pub fn clamp<T: PartialOrd>(min: T, max: T, val: T) -> T {
val
}

// TODO: This is ugly. Can we make it prettier?
pub fn find_center_and_size(canvas: &Canvas) -> (Pixel, f32) {
let max_r = canvas.iter().map(|p| p.r).max().unwrap();
let max_g = canvas.iter().map(|p| p.g).max().unwrap();
let max_b = canvas.iter().map(|p| p.b).max().unwrap();
let max_a = canvas.iter().map(|p| p.a).max().unwrap();
let min_r = canvas.iter().map(|p| p.r).min().unwrap();
let min_g = canvas.iter().map(|p| p.g).min().unwrap();
let min_b = canvas.iter().map(|p| p.b).min().unwrap();
let min_a = canvas.iter().map(|p| p.a).min().unwrap();
let center_r = (max_r - min_r) / 2 + min_r;
let center_g = (max_g - min_g) / 2 + min_g;
let center_b = (max_b - min_b) / 2 + min_b;
let center_a = (max_a - min_a) / 2 + min_a;
let center = Pixel::new(center_r, center_g, center_b, center_a);
let corner = Pixel::new(max_r, max_g, max_b, max_a);
let distance = center.distance(&corner);

(center, distance)
}

pub fn diff_squared(p1: &Pixel, p2: &Pixel) -> (u32, u32, u32, u32) {
(
(p1.r - p2.r).pow(2).into(),
Expand Down

0 comments on commit 9f127c3

Please sign in to comment.