NO

Author Topic: Flood Fill  (Read 3934 times)

rob

  • Guest
Flood Fill
« on: June 18, 2007, 03:34:57 AM »
I'm trying to create a program that preforms the flood fill operation but I'm having troubles.  When I run the program it goes into an infinite loop.  I desperately need some help.  Any help is greatly appreciated.  Thanks in advance.  Here's my code:

Code: [Select]
int popx()
{
   if(stackxPointer>0)
   {
        stackxPointer--;
        return stackx[stackxPointer+1];
   }
   else
        return -1;   
}

int popy()
{
   if(stackyPointer>0)
   {
        stackyPointer--;
        return stacky[stackyPointer+1];
   }
   else
        return -1;
}

boolean pushx(int x)
{
   if (stackxPointer<stackSize-1)
   {
        stackxPointer++;
        stackx[stackxPointer]=x;
        return 1;
   }
   else
        return 0;
}

boolean pushy(int y)
{
   if (stackyPointer<stackSize-1)
   {
        stackyPointer++;
        stacky[stackyPointer]=y;
        return 1;
   }
   else
        return 0;
}

void emptyxStack()
{
    while(popx());
}

void emptyyStack()
{
    while(popy());
}


void Surrounding(int x, int y, int r, int g, int b, int r2, int g2, int b2)
{
   int new = RGB(r2,g2,b2);
   int old = RGB(r,g,b);
   if (new==old)
        return; 
   emptyxStack();
   emptyyStack();
   if ((!pushx(x)) || (!pushy(y)))
        return;
   int newx,newy;
   newx=popx();
   newy=popy();
   while (newx!=-1)
   {
        SetPixel(hDC,newx,newy, new);
        if ((newx+1<nWidth)&&(GetPixel(hDC,newx+1,newy)==old))
        {         
                if ((!pushx(newx+1))||(pushy(newy)))
                        return;           
        }   
        if ((newx-1<nWidth)&&(GetPixel(hDC,newx-1,newy)==old))
        {         
                if ((!pushx(newx-1))||(pushy(newy)))
                        return;         
        }   
        if ((newy+1<nHeight)&&(GetPixel(hDC,newx,newy+1)==old))
        {         
                if ((!pushx(newx))||(pushy(newy+1)))
                        return;             
        }     
        if ((newy-1<nHeight)&&(GetPixel(hDC,newx,newy-1)==old))
        {         
                if ((!pushx(newx))||(pushy(newy-1)))
                        return;           
        } 
        newx=popx();
        newy=popy();
   }     
}

and this is the code that calls the function:
Code: [Select]
color = GetPixel(hDC,xPos,yPos);
r = GetRValue(color);
g = GetGValue(color);
b = GetBValue(color);
Surrounding(xPos,yPos,r,g,b,r+40,g+80,b+20);

JohnF

  • Guest
Re: Flood Fill
« Reply #1 on: June 18, 2007, 07:25:47 AM »
That code will not compile - you should post working code.

John

rob

  • Guest
Re: Flood Fill
« Reply #2 on: June 19, 2007, 02:46:21 AM »
That code does compile.  Here it is in complete code:

Code: [Select]
#include <windows.h>

LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
int nWidth=850, nHeight=700,zoom=1,oldzoom=1,xPos,yPos,down=0,color;
HINSTANCE hInst;
HDC hDC;

#define stackSize 16777216
int stackx[stackSize];
int stacky[stackSize];
int stackxPointer=-1;
int stackyPointer=-1;

int popx()
{
if(stackxPointer>0)
{
stackxPointer--;
return stackx[stackxPointer+1];
}
else
return -1;   
}

int popy()
{
if(stackyPointer>0)
{
stackyPointer--;
return stacky[stackyPointer+1];
}
else
return -1;
}

boolean pushx(int x)
{
if (stackxPointer<stackSize-1)
{
stackxPointer++;
stackx[stackxPointer]=x;
return 1;
}
else
return 0;
}

boolean pushy(int y)
{
if (stackyPointer<stackSize-1)
{
stackyPointer++;
stacky[stackyPointer]=y;
return 1;
}
else
return 0;
}

void emptyxStack()
{
    while(popx());
}

void emptyyStack()
{
    while(popy());
}


void Surrounding(int x, int y, int r, int g, int b, int r2, int g2, int b2)
{
int new = RGB(r2,g2,b2);
int old = RGB(r,g,b);
if (new==old)
return; 
emptyxStack();
emptyyStack();
    if ((!pushx(x)) || (!pushy(y)))
return;
int newx,newy;
newx=popx();
newy=popy();
     while (newx!=-1)
{
SetPixel(hDC,newx,newy, new);
        if ((newx+1<nWidth)&&(GetPixel(hDC,newx+1,newy)==old))
        {         
          if ((!pushx(newx+1))||(pushy(newy)))
return;           
        }   
if ((newx-1<nWidth)&&(GetPixel(hDC,newx-1,newy)==old))
        {         
          if ((!pushx(newx-1))||(pushy(newy)))
return;         
        }   
if ((newy+1<nHeight)&&(GetPixel(hDC,newx,newy+1)==old))
        {         
          if ((!pushx(newx))||(pushy(newy+1)))
return;             
        }     
if ((newy-1<nHeight)&&(GetPixel(hDC,newx,newy-1)==old))
        {         
          if ((!pushx(newx))||(pushy(newy-1)))
return;           
       
newx=popx();
newy=popy();
    }     
}

INT PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{

    WNDCLASSEX  WndCls;
HWND hWnd;
    static char szAppName[] = "BitmapIntro";
    MSG         Msg;

    WndCls.cbSize        = sizeof(WndCls);
    WndCls.style = 0;
    WndCls.lpfnWndProc   = WindProcedure;
    WndCls.cbClsExtra    = 0;
    WndCls.cbWndExtra    = 0;
    WndCls.hInstance     = hInstance; 
    WndCls.hIcon  = NULL;
    WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndCls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    WndCls.lpszMenuName  = NULL;
    WndCls.lpszClassName = szAppName;
    WndCls.hIconSm       = NULL;
    RegisterClassEx(&WndCls);
    hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                          szAppName,
                          "Rooms",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          nWidth,
                          nHeight,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);
hInst = hInstance;
int r, g, b;

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
hDC = GetDC(hWnd);
SelectObject(hDC, GetStockObject(GRAY_BRUSH));
Rectangle(hDC, 100, 100, 200, 200);
if (down){
color = GetPixel(hDC,xPos,yPos);
r = GetRValue(color);
g = GetGValue(color);
b = GetBValue(color);
Surrounding(xPos,yPos,r,g,b,r+40,g+80,b+20);
down=0;
}
    ReleaseDC(hWnd, hDC);
    }
    return Msg.wParam;
}

LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg,
   WPARAM wParam, LPARAM lParam)
{
    switch(Msg)
    {
case WM_DESTROY:
    PostQuitMessage(WM_QUIT);
    break;
case WM_SIZE:
            nWidth = LOWORD(lParam);
            nHeight = HIWORD(lParam);
            break;
case WM_LBUTTONDOWN:
xPos = (short)LOWORD(lParam);
  yPos = (short)HIWORD(lParam);
down=1;
break;
default:
    return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
 

rob

  • Guest
Re: Flood Fill
« Reply #3 on: June 19, 2007, 04:28:30 AM »
I managed to get it working.  The problem was with the stack so I removed the functions and added it to the surrounding function.

JohnF

  • Guest
Re: Flood Fill
« Reply #4 on: June 19, 2007, 07:33:17 AM »
That code does compile.  Here it is in complete code:

Well, below is the result of pasting the original code into an existing project.

When asking for help it is not so good if the helper has to mess about filling in missing parts to get it to compile.

-----------------------------------------------
Building main.obj.
D:\PellesC\Projects\win1\main.c(34): warning #2027: Missing prototype for 'popx'.
D:\PellesC\Projects\win1\main.c(35): error #2048: Undeclared identifier 'stackxPointer'.
D:\PellesC\Projects\win1\main.c(38): error #2048: Undeclared identifier 'stackx'.
D:\PellesC\Projects\win1\main.c(38): error #2144: Type error: pointer expected.
D:\PellesC\Projects\win1\main.c(45): warning #2027: Missing prototype for 'popy'.
D:\PellesC\Projects\win1\main.c(46): error #2048: Undeclared identifier 'stackyPointer'.
D:\PellesC\Projects\win1\main.c(49): error #2048: Undeclared identifier 'stacky'.
D:\PellesC\Projects\win1\main.c(49): error #2144: Type error: pointer expected.
D:\PellesC\Projects\win1\main.c(57): error #2048: Undeclared identifier 'stackxPointer'.
D:\PellesC\Projects\win1\main.c(57): error #2048: Undeclared identifier 'stackSize'.
D:\PellesC\Projects\win1\main.c(60): error #2048: Undeclared identifier 'stackx'.
D:\PellesC\Projects\win1\main.c(60): error #2144: Type error: pointer expected.
D:\PellesC\Projects\win1\main.c(69): error #2048: Undeclared identifier 'stackyPointer'.
D:\PellesC\Projects\win1\main.c(69): error #2048: Undeclared identifier 'stackSize'.
D:\PellesC\Projects\win1\main.c(72): error #2048: Undeclared identifier 'stacky'.
D:\PellesC\Projects\win1\main.c(72): error #2144: Type error: pointer expected.
D:\PellesC\Projects\win1\main.c(80): warning #2027: Missing prototype for 'emptyxStack'.
D:\PellesC\Projects\win1\main.c(85): warning #2027: Missing prototype for 'emptyyStack'.
D:\PellesC\Projects\win1\main.c(107): error #2048: Undeclared identifier 'nWidth'.
D:\PellesC\Projects\win1\main.c(117): error #2048: Undeclared identifier 'nHeight'.
D:\PellesC\Projects\win1\main.c(266): error #2048: Undeclared identifier 'color'.
D:\PellesC\Projects\win1\main.c(266): error #2048: Undeclared identifier 'xPos'.
D:\PellesC\Projects\win1\main.c(266): error #2048: Undeclared identifier 'yPos'.
D:\PellesC\Projects\win1\main.c(267): error #2048: Undeclared identifier 'r'.
D:\PellesC\Projects\win1\main.c(268): error #2048: Undeclared identifier 'g'.
D:\PellesC\Projects\win1\main.c(269): error #2048: Undeclared identifier 'b'.
*** Error code: 1 ***
Done.
-----------------------------------------------

Anyway, glad you have fixed it.

John