0

I have a problem with my flood fill function:

void floodfill(int x, int y,Pixel old, Pixel new){

Pixel current = getPixel(x,y);

if(current.r == old.r && current.g == old.g && current.b == old.b){

    setPixel(x,y,new);

    floodfill(x+1,y,old,new);
    floodfill(x-1,y,old,new);
    floodfill(x,y+1,old,new);
    floodfill(x,y-1,old,new);
    floodfill(x+1,y+1,old,new);
    floodfill(x-1,y-1,old,new);
    floodfill(x+1,y+1,old,new);
    floodfill(x-1,y+1,old,new);
}
}

In struct 'Pixel' I have rgb values of the pixel. I am trying to fill a square and when I come to the border of the square (color changes from white to black, the border is at point x=200) the function is not changing to other pixels but just endlessly changing the x value to 198 and 199 (nevermind the y value). Can someone tell me what I am doing wrong?

  • You're only filling if the current color is *equal to* the old color. Don't you want to fill only if the current color is *not equal to* the old color? P.S - `new` is a reserved keyword in C++ so you should not be trying to use it as a variable name. – aardvarkk Jun 19 '14 at 20:15
  • What happens if `new` is equal to `old`? – Daniel Jun 19 '14 at 20:19
  • 2
    ?? `new` is a keyword in C++; you can't use it for variable names. – Cornstalks Jun 19 '14 at 20:21
  • Aardvarkk, the square is white inside and has a black borders. I set the old color to white, I want to set pixel and start recursion when the current color is white. When I get to the border of the square current changes to black. – user3115426 Jun 19 '14 at 20:37

2 Answers2

0

One possible issue is that you are filling (x+1,y+1) twice and missing (x+1,y-1):

floodfill(x+1,y,old,new);
floodfill(x-1,y,old,new);
floodfill(x,y+1,old,new);
floodfill(x,y-1,old,new);
floodfill(x+1,y+1,old,new);
floodfill(x+1,y-1,old,new); //Missing this case
floodfill(x-1,y+1,old,new);
floodfill(x-1,y-1,old,new);
uesp
  • 6,194
  • 20
  • 15
  • Thank you for correcting my mistake but just after the first fill (x+1,y) my program loops. – user3115426 Jun 19 '14 at 21:31
  • Do you mean your program gets into an infinite loop or something else? I assume your square border has a complete solid outline including its corners filled with the old color? – uesp Jun 19 '14 at 21:40
  • Yes, infinite loop and the square border is solid black and corners are black too (square inside is white). – user3115426 Jun 19 '14 at 21:50
  • What sort of system are you on? Does `setPixel()` actually set the pixel's color or is there a buffering layer that may interfere with subsequent `getPixel()` calls? – uesp Jun 19 '14 at 22:31
  • I did a quick test and with the above fixes to the function and changing `new` to `newColor` it works fine (using a virtual pixel screen). This means the likely culprit is related to the `setPixel()/getPixel()` functions. – uesp Jun 19 '14 at 22:53
0

This sounds like setPixel isn't setting the colour of (x,y) to the "new". So, what's happening is the following sequence of recursive calls:

(199,y)->(200,y) [This stops because the border doesn't have the same colour as old] ->(198, y)->(199,y) [(199, y) is getting called because setPixel likely didn't change the colour of (199, y)]->...

Pradhan
  • 16,391
  • 3
  • 44
  • 59