Flood Fill Help

BlitzMax Forums/BlitzMax Programming/Flood Fill Help

Could someone please help me? I am playing about with a routine to flood fill areas of the screen and have got it almost working - but not quite. I am using a black and white line drawing and the idea is that you click on the image and it fills that section with a random colour. But what actually happens is it fills most of the area but not right up to the lines. At first I thought it was because the picture was greyscale rather than just black and white, but I changed it to be only 2 colours and the problem still occurs.

Below is my code - I am sure it is just my logic on how to do this is a little screwed, but can't see for looking.

I got the image from here (sorry I don't have anywhere to host it at the moment), and just cropped to 1024*768 and reduced to just 2 colours.

http://www.bbc.co.uk/cbeebies/printables/printcolour/storymakers/

Any help would be really appreciated.


Strict

Global intSelectedPixel:Int
Global intSelectedRed:Int
Global intSelectedGreen:Int
Global intSelectedBlue:Int
Local intMouseXPos:Int
Local intMouseYPos:Int
Local pxmPixel:TPixmap


'temp as will use argb in later version
Global intFillRed:Int
Global intFillGreen:Int
Global intFillBlue:Int

Global imgPrintable:TImage

Global intScreenWidth:Int = 1024 'temp
Global intScreenheight:Int = 768 'temp

Graphics 1024,768
imgPrintable = LoadImage("Images\Printable1.jpg")
DrawImage(imgPrintable,0,0)

While not KeyHit(KEY_ESCAPE)

	If MouseHit(1) = 1
	
		'get a random flood fill colour
		intFillRed = Rnd(1.0,255.0)	
		intFillGreen = Rnd(1.0,255.0)
		intFillBlue = Rnd(1.0,255.0)
	
		'Grab the pixel that the user clicks on and find out the colour of it
		intMouseXPos = MouseX()
		intMouseYPos = MouseY()
		pxmPixel = GrabPixmap(intMouseXPos,intMouseYPos,1,1)
		intSelectedPixel = ReadPixel(pxmPixel,0,0)
 	'	intAlpha = (intPixel & $ff000000)
  		intSelectedRed = (intSelectedPixel & $ff0000 ) Shr 16
  		intSelectedGreen = (intSelectedPixel & $ff00 ) Shr 8
  		intSelectedBlue = (intSelectedPixel & $ff )

		'if the selected pixel is a different colour from the flood colour then flood fill the area		
		If (intSelectedRed <> intFillRed or intSelectedBlue <> intFillBlue or intSelectedBlue <> intFillBlue)' and..
		   '(intSelectedRed <> 0 and intSelectedGreen <> 0 and intSelectedBlue <> 0)
  		
		   
	  		FloodFill(intMouseXPos,intMouseYPos)
	  		
	  	EndIf
		
	EndIf
	Flip

Wend

Function FloodFill(fnintXPos:Int,fnintYPos:Int)

   SetColor(intFillRed,intFillGreen,intFillBlue)
   Plot(fnintXPos,fnintYPos)
   
   FillRight(fnintXPos + 1, fnintYPos)
   FillDown(fnintXPos, fnintYPos + 1)
   FillLeft(fnintXPos - 1, fnintYPos)
   FillUp(fnintXPos, fnintYPos - 1)

EndFunction

Function FillRight(fnintXPos:Int, fnintYPos:Int)

	Local pxmNextPixel:TPixmap
	Local intNextPixel:Int
	Local intNextRed:Int
	Local intNextGreen:Int
	Local intNextBlue:Int
	
	If fnintXPos <= intScreenWidth

		pxmNextPixel = GrabPixmap(fnintXPos,fnintYPos,1,1)
		intNextPixel = ReadPixel(pxmNextPixel,0,0)
 	'	intAlpha = (intPixel & $ff000000)
  		intNextRed = (intNextPixel & $ff0000 ) Shr 16
  		intNextGreen = (intNextPixel & $ff00 ) Shr 8
  		intNextBlue = (intNextPixel & $ff )
  		
  		If intNextRed = intSelectedRed and intNextGreen = intSelectedGreen and intNextBlue = intSelectedBlue
  		
  			Plot(fnintXPos,fnintYPos)
  			
			FillRight(fnintXPos + 1, fnintYPos)
			FillDown(fnintXPos, fnintYPos + 1)
			FillUp(fnintXPos, fnintYPos - 1)			
  			
  		EndIf
  		
  	EndIf
  		
  EndFunction

Function FillLeft(fnintXPos:Int, fnintYPos:Int)

	Local pxmNextPixel:TPixmap
	Local intNextPixel:Int
	Local intNextRed:Int
	Local intNextGreen:Int
	Local intNextBlue:Int
	
	If fnintXPos > 0

		pxmNextPixel = GrabPixmap(fnintXPos,fnintYPos,1,1)
		intNextPixel = ReadPixel(pxmNextPixel,0,0)
 	'	intAlpha = (intPixel & $ff000000)
  		intNextRed = (intNextPixel & $ff0000 ) Shr 16
  		intNextGreen = (intNextPixel & $ff00 ) Shr 8
  		intNextBlue = (intNextPixel & $ff )
  		
  		If intNextRed = intSelectedRed and intNextGreen = intSelectedGreen and intNextBlue = intSelectedBlue
  		
			Plot(fnintXPos,fnintYPos)
			  			
			FillLeft(fnintXPos - 1, fnintYPos)
			FillDown(fnintXPos, fnintYPos + 1)
			FillUp(fnintXPos, fnintYPos - 1)			
			  			
  		EndIf
  		
  	EndIf
  		
  EndFunction
  
  Function FillDown(fnintXPos:Int, fnintYPos:Int)

	Local pxmNextPixel:TPixmap
	Local intNextPixel:Int
	Local intNextRed:Int
	Local intNextGreen:Int
	Local intNextBlue:Int
	
	If fnintYPos <= intScreenheight

		pxmNextPixel = GrabPixmap(fnintXPos,fnintYPos,1,1)
		intNextPixel = ReadPixel(pxmNextPixel,0,0)
 	'	intAlpha = (intPixel & $ff000000)
  		intNextRed = (intNextPixel & $ff0000 ) Shr 16
  		intNextGreen = (intNextPixel & $ff00 ) Shr 8
  		intNextBlue = (intNextPixel & $ff )
  		
  		If intNextRed = intSelectedRed and intNextGreen = intSelectedGreen and intNextBlue = intSelectedBlue
  		
  			Plot(fnintXPos,fnintYPos)
			  			
			FillRight(fnintXPos + 1, fnintYPos)
			FillLeft(fnintXPos - 1, fnintYPos)
			FillDown(fnintXPos, fnintYPos + 1)			
  			
  		EndIf
  		
  	EndIf
  		
  EndFunction
  
  Function FillUp(fnintXPos:Int, fnintYPos:Int)

	Local pxmNextPixel:TPixmap
	Local intNextPixel:Int
	Local intNextRed:Int
	Local intNextGreen:Int
	Local intNextBlue:Int
	
	If fnintYPos > 0

		pxmNextPixel = GrabPixmap(fnintXPos,fnintYPos,1,1)
		intNextPixel = ReadPixel(pxmNextPixel,0,0)
 	'	intAlpha = (intPixel & $ff000000)
  		intNextRed = (intNextPixel & $ff0000 ) Shr 16
  		intNextGreen = (intNextPixel & $ff00 ) Shr 8
  		intNextBlue = (intNextPixel & $ff )
  		
  		If intNextRed = intSelectedRed and intNextGreen = intSelectedGreen and intNextBlue = intSelectedBlue
  		
  			Plot(fnintXPos,fnintYPos)
  			
			FillRight(fnintXPos + 1, fnintYPos)
			FillLeft(fnintXPos - 1, fnintYPos)
			FillUp(fnintXPos, fnintYPos - 1)		
  			
  		EndIf
  		
  	EndIf
  		
  EndFunction