aboutsummaryrefslogtreecommitdiff
path: root/lib/algo/flood_fill.c
blob: e2ee870821ea6537753c97eaf5a4109e1a635010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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(ImageData_t *image, MaskData_t *mask, size_t width, size_t height,
             size_t channels, size_t x, size_t y, ImageData_t *fill_color,
             MaskData_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;
}