-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flood fill algorithm and regression test
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use image::{GenericImage, 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) | ||
where | ||
I: GenericImage, | ||
{ | ||
let target = image.get_pixel(x, y); | ||
|
||
let mut stack = Vec::new(); | ||
|
||
stack.push((x as i32, x as i32, y as i32, 1 as i32)); | ||
stack.push((x as i32, x as i32, y as i32 - 1, -1 as i32)); | ||
|
||
while !stack.is_empty() { | ||
let (x1, x2, y, dy) = stack.pop().unwrap(); | ||
let mut x1 = x1; | ||
let mut x = x1; | ||
if inside(image, x, y, target) { | ||
while inside(image, x - 1, y, target) { | ||
image.put_pixel(x as u32 - 1, y as u32, fill_with); | ||
x = x - 1; | ||
} | ||
if x < x1 { | ||
stack.push((x, x1 - 1, y - dy, -dy)) | ||
} | ||
} | ||
while x1 <= x2 { | ||
while inside(image, x1, y, target) { | ||
image.put_pixel(x1 as u32, y as u32, fill_with); | ||
x1 = x1 + 1; | ||
} | ||
if x1 > x { | ||
stack.push((x, x1 - 1, y + dy, dy)) | ||
} | ||
if x1 - 1 > x2 { | ||
stack.push((x2 + 1, x1 - 1, y - dy, -dy)) | ||
} | ||
x1 = x1 + 1; | ||
while x1 < x2 && !inside(image, x1, y, target) { | ||
x1 = x1 + 1 | ||
} | ||
x = x1 | ||
} | ||
} | ||
} | ||
|
||
/// 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 | ||
where | ||
I: GenericImage, | ||
{ | ||
if x < 0 || y < 0 { | ||
return false; | ||
} | ||
let x = x as u32; | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters