Jump to content

FillScanline algorithm


Andreik
 Share

Recommended Posts

I tried to write this function in autoit but I`m a bit confused about emptyStack(),push() and pop() functions. I know that exists this function in array udf but here seems to be different. For example how can I write this in autoit:

if(!push(x, y)) return;

This is the full code:

void floodFillScanlineStack(int x, int y, int newColor, int oldColor)
{
    if(oldColor == newColor) return;
    emptyStack();
    
    int y1; 
    bool spanLeft, spanRight;
    
    if(!push(x, y)) return;
    
    while(pop(x, y))
    {    
        y1 = y;
        while(y1 >= 0 && screenBuffer[x][y1] == oldColor) y1--;
        y1++;
        spanLeft = spanRight = 0;
        while(y1 < h && screenBuffer[x][y1] == oldColor )
        {
            screenBuffer[x][y1] = newColor;
            if(!spanLeft && x > 0 && screenBuffer[x - 1][y1] == oldColor) 
            {
                if(!push(x - 1, y1)) return;
                spanLeft = 1;
            }
            else if(spanLeft && x > 0 && screenBuffer[x - 1][y1] != oldColor)
            {
                spanLeft = 0;
            }
            if(!spanRight && x < w - 1 && screenBuffer[x + 1][y1] == oldColor) 
            {
                if(!push(x + 1, y1)) return;
                spanRight = 1;
            }
            else if(spanRight && x < w - 1 && screenBuffer[x + 1][y1] != oldColor)
            {
                spanRight = 0;
            } 
            y1++;
        }
    }
}
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

If you can get the definition for 'screenBuffer' then it would make life a lot easier (and 'h' as well while you're there)

I think that push adds a new item to screenBuffer, with the given x and y... Not sure though, as then it wants it as a color. emptyStack just resets the screenBuffer I think.

This is what I get sans pop and push. I guessed that screenBuffer is not anything like what I have it here as... But ah well.

Global $screenBuffer[1][1]

Func floodFillScanlineStack($x, $y, $newColor, $oldColor)
    If $oldColor == $newColor Then Return
    emptyStack()

    Local $y1
    Local $spanLeft, $spanRight

    If Not push($x, $y) Then Return

    While pop($x, $y)
        $y1 = $y

        While ($y1 >= 0) And ($screenBuffer[$x][$y1] == $oldColor)
            $y1 -= 1
        WEnd
        $y1 += 1

        $spanLeft = 0
        $spanRight = 0

        While ($y1 < $h) And ($screenBuffer[$x][$y1] == $oldColor)
            $screenBuffer[$x][$y1] = $newColor;
            If (Not $spanLeft) And ($x > 0) And ($screenBuffer[$x - 1][$y1] == $oldColor) Then
                If Not push($x - 1, $y1) Then Return
                $spanLeft = 1
            ElseIf ($spanLeft) And ($x > 0) And ($screenBuffer[$x - 1][$y1] <> $oldColor) Then
                $spanLeft = 0
            EndIf

            If (Not $spanRight) And ($x < $w - 1) And ($screenBuffer[$x + 1][$y1] == $oldColor) Then
                If Not push($x + 1, $y1) Then Return
                $spanRight = 1
            ElseIf ($spanRight) And ($x < $w - 1) And ($screenBuffer[$x + 1][$y1] <> $oldColor) Then
                $spanRight = 0
            EndIf

            $y1 += 1
        WEnd
    WEnd
EndFunc   ;==>floodFillScanlineStack

; Reset the global array
Func emptyStack()
    ReDim $screenBuffer[1][1]
    $screenBuffer[0][0] = 0
EndFunc

Mat

Link to comment
Share on other sites

I forgot from where I get there code but after a quick search on google I got this:

http://www.student.kuleuven.be/~m0216922/CG/files/floodfill.cpp

So the missing part is this:

bool pop(int &x, int &y)
{
    if(stackPointer > 0)
    {
        int p = stack[stackPointer];
        x = p / h;
        y = p % h;
        stackPointer--;
        return 1;
    }    
    else
    {
        return 0;
    }   
}   
 
bool push(int x, int y)
{
    if(stackPointer < stackSize - 1)
    {
        stackPointer++;
        stack[stackPointer] = h * x + y;
        return 1;
    }    
    else
    {
        return 0;
    }   
}    

void emptyStack()
{
    int x, y;
    while(pop(x, y));
}

Now make more sense, I think I know how to write this is autoit.

When the words fail... music speaks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...