Please Help, LockedPixels driving me mad!

BlitzPlus Forums/BlitzPlus Programming/Please Help, LockedPixels driving me mad!

CopperCircle(Posted 2007) [#1]
Hi, I am trying to write a fast edge detection function using LockedPixels. It is all working fine except certain colours are being filled in! It seems that when some colours are Poked it changes surrounding colours to yellow, making the code think there is an edge.

Here is the code and the test image, any help would be great.

http://www.avds12.dsl.pipex.com/Test.bmp

Graphics 800,600,32,2

image=LoadImage("Test.bmp")

Width=ImageWidth(image)
Height=ImageHeight(image)

LockBuffer(ImageBuffer(image))
LBank=LockedPixels(ImageBuffer(image))
LPitch=LockedPitch(ImageBuffer(image))
LFormat=LockedFormat(ImageBuffer(image))

Select LFormat
		
	Case 1
		
	Case 2
	
	Case 3,4
		For y=0 To Height-1
			YPos=y*LPitch
			For x=0 To Width-1
				RGB=PeekInt(LBank,YPos+(x*LFormat))
				SrcR=(RGB And $FF0000) Shr 16
				SrcG=(RGB And $FF00) Shr 8
				SrcB=RGB And $FF
				
				If x<(Width-1)
					RGB=PeekInt(LBank,YPos+((x+1)*LFormat))
					x1R=(RGB And $FF0000) Shr 16
					x1G=(RGB And $FF00) Shr 8
					x1B=RGB And $FF
				EndIf
				
				If x>0
					RGB=PeekInt(LBank,YPos+((x-1)*LFormat))
					x2R=(RGB And $FF0000) Shr 16
					x2G=(RGB And $FF00) Shr 8
					x2B=RGB And $FF
				EndIf
				
				If y<(Height-1)
					RGB=PeekInt(LBank,(y+1)*LPitch+(x*LFormat))
					y1R=(RGB And $FF0000) Shr 16
					y1G=(RGB And $FF00) Shr 8
					y1B=RGB And $FF
				EndIf
				
				If y>0
					RGB=PeekInt(LBank,(y-1)*LPitch+(x*LFormat))
					y2R=(RGB And $FF0000) Shr 16
					y2G=(RGB And $FF00) Shr 8
					y2B=RGB And $FF
				EndIf				
				
				SrcBright=SrcR+SrcG+SrcB
				SrcBright=SrcBright/3
				
				x1Bright=x1R+x1G+x1B
				x1Bright=x1Bright/3
				
				x2Bright=x2R+x2G+x2B
				x2Bright=x2Bright/3
				
				y1Bright=y1R+y1G+y1B
				y1Bright=y1Bright/3
				
				y2Bright=y2R+y2G+y2B
				y2Bright=y2Bright/3
				
				If SrcBright>x1Bright And x<(Width-1)
					PokeInt LBank,YPos+((x+1)*LFormat),ConvertRGB(0,0,0,LFormat)
				EndIf
				If SrcBright>x2Bright And x>0
					PokeInt LBank,YPos+((x-1)*LFormat),ConvertRGB(0,0,0,LFormat)
				EndIf
				If SrcBright>y1Bright And y<(Height-1)
					PokeInt LBank,((y+1)*LPitch)+(x*LFormat),ConvertRGB(0,0,0,LFormat)
				EndIf
				If SrcBright>y2Bright And y>0
					PokeInt LBank,((y-1)*LPitch)+(x*LFormat),ConvertRGB(0,0,0,LFormat)
				EndIf
			Next
		Next
		
End Select
UnlockBuffer(ImageBuffer(image))
DrawImage image,0,0
Flip
WaitKey
End

Function ConvertRGB(r,g,b,Mode=0)
	
	Select Mode
			
		Case 1
			Col=((r/8) Shl 11) Or ((g/4) Shl 5) Or (b/8) 
			Return Col
			
		Case 2
			Col=((r/8) Shl 10) Or ((g/8) Shl 5) Or (b/8) 
			Return Col
			
		Default
			Col=(r Shl 16) Or (g Shl 8) Or b
			Return Col
			
	End Select
	
End Function



skidracer(Posted 2007) [#2]
You may want to rethink your algorithm. When you are peeking the pixels to the left or above they have already been overwritten by your function so their original values have been lost.


CopperCircle(Posted 2007) [#3]
Thanks, sorted now, silly mistake.