diff options
author | Christian C <cc@localhost> | 2025-03-04 19:10:07 -0800 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-04 19:10:07 -0800 |
commit | c61bb864c026d26a393f2a09c82de9bbc5043c6f (patch) | |
tree | 9f589bd8c0fa0823fe04da0753004386f0968844 /src/lib/algo/flood_fill.c | |
parent | 49412195efd93573eb96ddbae6e41099964fdb65 (diff) |
Modularization
Diffstat (limited to 'src/lib/algo/flood_fill.c')
-rw-r--r-- | src/lib/algo/flood_fill.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/lib/algo/flood_fill.c b/src/lib/algo/flood_fill.c new file mode 100644 index 0000000..91f94ad --- /dev/null +++ b/src/lib/algo/flood_fill.c @@ -0,0 +1,34 @@ +#include <lib/algo/flood_fill.h> + +//----------------------------------------------- +// Flood +// Floods a mask from a given set of image to determine the contiguous regions +// 1. Check that the (x,y) is within the picture +// 2. Check if the (x,y) coordinate in the mask is unused +// 3. Check if the (x,y) coordinate in the image is non-background +// 4. Check if the (x,y) coordinate in the image is the same color as the fill color +// 5. If all hold, set the label for the pixel, and check each neighbor +// Otherwise, stop flooding +bool_t flood(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, uint8_t* fill_color, uint16_t label) +{ + if ((x >= width) | (y >= height)) { + return FALSE; + } + size_t coord = x + y*width; + if (mask[coord] != 0) { + return FALSE; + } + if (color_zero(&(image[coord*channels]), channels)) { + return FALSE; + } + if (color_equal(&(image[coord*channels]), fill_color, channels)) { + mask[coord] = label; + flood(image, mask, width, height, channels, x, y+1, fill_color, label); + flood(image, mask, width, height, channels, x, y-1, fill_color, label); + flood(image, mask, width, height, channels, x+1, y, fill_color, label); + flood(image, mask, width, height, channels, x-1, y, fill_color, label); + return TRUE; + } + return FALSE; +} + |