More or less like that:
void fill (x, y, col) {
setPixel (x, y, col)
if (getPixel (x + 1, y) != col then fill (x + 1, y, col);
if (getPixel (x, y + 1) != col then fill (x, y + 1, col);
if (getPixel (x - 1, y) != col then fill (x - 1, y, col);
if (getPixel (x, y - 1) != col then fill (x, y - 1, col);
}
Flood fill is an inherently recursive activity. (Too) simple implementations can easily run into a stack overflow (the real thing, not the web site ;)) More sophisticated implementations wouldn't use the CPU stack, but a separately allocated one to be able to observe memory usage and avoid that. With some more code invested, you can improve the algorithm to make sure every pixel is only visited once.
Note the getPixel()
and putPixel()
complexity stands and falls with the hardware implementation of the frame buffer. Implementations that use separate bit planes/color can make the code pretty expensive, while hardware implementations that use one byte (or one word) per pixel make for a much faster implementation.
pcmpgtb
orpcmpeqb
andpmovmskb eax, xmm0
/test eax,eax
/jnz any_matches
, and maybe bit-scan the integer bitmap of element matches. Horizontally within a line, you want to search for a start point (probably starting near the start column of the previous line, so maybe only a couple vectors). And find an end point, either as you go reading + filling, or find then start+end with scalar then basically SIMD memset vs.rep stosd
.