-
Notifications
You must be signed in to change notification settings - Fork 150
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2a187c
flood fill algorithm and regression test
tkallady 3785074
add _mut version, change from GenericImage to Image
tkallady 610e71e
Remove redundant test and fix doc comment
tkallady 279f2c1
Fix url in docs
tkallady b40f5b2
Merge branch 'master' into feature/flood-fill
tkallady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,73 @@ | ||
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<P>(image: &Image<P>, x: u32, y: u32, fill_with: P) -> Image<P> | ||
where | ||
P: Pixel + PartialEq, | ||
{ | ||
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")] | ||
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(); | ||
|
||
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 | ||
theotherphil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
/// Determines whether (x,y) is within the image bounds and if the pixel there is equal to target_color | ||
fn inside<P>(image: &Image<P>, x: i32, y: i32, target_pixel: P) -> bool | ||
where | ||
P: Pixel + PartialEq, | ||
{ | ||
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) == target_pixel | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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