Android Question Canvas Flood Fill Help

mhartwig

Member
Licensed User
Longtime User
I have the following code, but it does not seem to be working? What is going wrong?

B4X:
Sub FloodFill(image As Bitmap, node As Point, targetColor As Int, replacementColor As Int)
    Dim width As Int = image.width
    Dim height As Int = image.height
    Dim target As Int = targetColor
    Dim replacement As Int = replacementColor
   
    jpg.BmpWidth = image.Width
    jpg.BmpHeight = image.Height
    jpg.SetPixelsFromBmp(image)
    jpg.PixelsABGRtoARGB
   
    If target <> replacement Then
        Dim queue As LinkedList
        queue.Initialize
       
        Do While queue.Size <> 0
            Dim x As Int = node.x
            Dim y As Int = node.y
           
            Do While x > 0 AND jpg.GetBmpPixel(x - 1, y) = target
                x = x - 1
            Loop
           
            Dim spanUp As Boolean
            Dim spanDown As Boolean
           
            Do While x < width AND jpg.GetBmpPixel(x, y) = target
                jpg.SetBmpPixel(x, y, replacement)
               
                If spanUp = False AND y > 0 AND jpg.GetBmpPixel(x, y - 1) = target Then
                    Dim p As Point
                    p.Initialize
                    p.x = x
                    p.y = y - 1
                    queue.Add(p)
                   
                    spanUp = True
                Else If spanUp = True AND y > 0 AND jpg.GetBmpPixel(x, y - 1) <> target Then
                    spanUp = False
                End If
               
                If spanDown = False AND y < height - 1 AND jpg.GetBmpPixel(x, y + 1) = target Then
                    Dim p As Point
                    p.Initialize
                    p.x = x
                    p.y = y + 1
                    queue.Add(p)
                   
                    spanDown = True
                Else If spanDown = True AND y < height - 1 AND jpg.GetBmpPixel(x, y + 1) <> target Then
                    spanDown = False
                End If
               
                x = x + 1
            Loop
        Loop
    End If

    Activity.Invalidate
End Sub

Is there a library that has flood fill built in?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…