Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Flood-fill #684

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/drawing/fill.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use image::{GenericImage, Pixel};
use crate::definitions::Image;
use image::Pixel;

/// Equivalent to bucket tool in MS-PAINT
/// Performs 4-way flood-fill based on this algorithm: https://en.wikipedia.org/wiki/Flood_fill#Span_filling
pub fn flood_fill<I>(image: &mut I, x: u32, y: u32, fill_with: I::Pixel)
pub fn flood_fill<P>(image: &Image<P>, x: u32, y: u32, fill_with: P) -> Image<P>
where
I: GenericImage,
P: Pixel + PartialEq,
{
let target = image.get_pixel(x, y);
let mut filled_image = image.clone();
flood_fill_mut(&mut filled_image, x, y, fill_with);
filled_image
}

#[doc=generate_mut_doc_comment!("draw_line_segment")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update this to the same name as the non-mut method

pub fn flood_fill_mut<P>(image: &mut Image<P>, x: u32, y: u32, fill_with: P)
where
P: Pixel + PartialEq,
{
let target = image.get_pixel(x, y).clone();

let mut stack = Vec::new();

Expand Down Expand Up @@ -47,9 +58,9 @@ where
}

/// Determines whether (x,y) is within the image bounds and if the pixel there is equal to target_color
fn inside<I>(image: &I, x: i32, y: i32, target_color: I::Pixel) -> bool
fn inside<P>(image: &Image<P>, x: i32, y: i32, target_pixel: P) -> bool
where
I: GenericImage,
P: Pixel + PartialEq,
{
if x < 0 || y < 0 {
return false;
Expand All @@ -58,5 +69,5 @@ where
let y = y as u32;
let (width, height) = image.dimensions();
//TODO: Compare pixel equality without conversion to rgba
x < width && y < height && image.get_pixel(x, y).to_rgba() == target_color.to_rgba()
x < width && y < height && *image.get_pixel(x, y) == target_pixel
}
2 changes: 1 addition & 1 deletion src/drawing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod text;
pub use self::text::{draw_text, draw_text_mut, text_size};

mod fill;
pub use self::fill::flood_fill;
pub use self::fill::{flood_fill, flood_fill_mut};

// Set pixel at (x, y) to color if this point lies within image bounds,
// otherwise do nothing.
Expand Down
7 changes: 4 additions & 3 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use image::{

use imageproc::contrast::ThresholdType;
use imageproc::definitions::Image;
use imageproc::drawing::flood_fill;
use imageproc::filter::bilateral::GaussianEuclideanColorDistance;
use imageproc::filter::bilateral_filter;
use imageproc::kernel::{self};
Expand Down Expand Up @@ -751,7 +750,7 @@ fn test_draw_filled_ellipse() {

#[test]
fn test_draw_flood_filled_shape() {
use imageproc::drawing::{draw_hollow_ellipse_mut, flood_fill};
use imageproc::drawing::{draw_hollow_ellipse_mut, flood_fill, flood_fill_mut};

let red = Rgb([255, 0, 0]);
let green = Rgb([0, 255, 0]);
Expand All @@ -763,8 +762,10 @@ fn test_draw_flood_filled_shape() {
draw_hollow_ellipse_mut(&mut image, (100, 150), 80, 30, green);
draw_hollow_ellipse_mut(&mut image, (150, 150), 100, 60, blue);

flood_fill(&mut image, 120, 120, red);
let filled_image = flood_fill(&image, 120, 120, red);
compare_to_truth_image(&filled_image, "flood_filled_shape.png");

flood_fill_mut(&mut image, 120, 120, red);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really any need to test both variants since they are the same method underneath.

compare_to_truth_image(&image, "flood_filled_shape.png");
}

Expand Down