Masking a currently loaded image

BlitzMax Forums/BlitzMax Beginners Area/Masking a currently loaded image

_Skully(Posted 2009) [#1]
So I have this image open, and I want to set the mask color for it...

SetMaskColor is used to set the mask color prior to loading right? OK, So I set it and then load the image from the original images pixelmap and tada! nothin!

What am I doing wrong?


_Skully(Posted 2009) [#2]
Got it...

http://www.blitzmax.com/codearcs/codearcs.php?code=1250

Updated so it works without exception
Function SetImageMask(p_image:TImage, p_red:Int,p_green:Int,p_blue:Int)
	Local l_maskrgb:Int
	Local l_maskargb:Int
	Local l_pixelrgb:Int
	Local l_pixelraw:Int
	Local l_x:Int,l_y:Int
	Local l_pix1:Tpixmap
	
	l_maskrgb = p_red Shl 16 + p_green Shl 8 + p_blue
	l_pix1 = LockImage(p_image)
	For l_x = 0 To ImageWidth(p_image)-1
		For l_y = 0 To ImageHeight(p_image)-1
			l_pixelraw = ReadPixel(l_pix1,l_x,l_y)
			l_pixelrgb = l_pixelraw & 16777215
			If l_pixelrgb = l_maskrgb Then
				WritePixel(l_pix1,l_x,l_y,l_maskrgb)
			End if
		Next 
	next
	UnlockImage(p_image)
End function



_Skully(Posted 2009) [#3]
Grrrr.... thats destructive :(

Works for now I suppose


_Skully(Posted 2009) [#4]
I don't like using destructive code as the above function is... but...

I'm thinking I should be able to adapt the code above to take the pixels with the mask color and make them fully transparent. I don't have time to code right now but does that sound right?

To save me a moment of headache... 0 is transparent on the alpha channel?


_Skully(Posted 2009) [#5]
Here we go... it was just applying alpha each time the setimagemask was called... so I've made it reverse the previous alpha settings..

Function SetImageMask(p_image:TImage, p_red:Int,p_green:Int,p_blue:Int)
	Local l_maskrgb:Int
	Local l_maskargb:Int
	Local l_pixelrgb:Int
	Local l_pixelraw:Int
	Local l_x:Int,l_y:Int
	Local l_pix1:Tpixmap
	
	l_maskrgb = p_red Shl 16 + p_green Shl 8 + p_blue
	l_pix1 = LockImage(p_image)
	For l_x = 0 To ImageWidth(p_image)-1
		For l_y = 0 To ImageHeight(p_image)-1
			l_pixelraw = ReadPixel(l_pix1,l_x,l_y)
			l_pixelrgb = l_pixelraw & 16777215
			If l_pixelrgb = l_maskrgb Then
				' make transparent
				WritePixel(l_pix1,l_x,l_y,l_maskrgb)
			Else
				' remove transparency
				Local l_PixelNoAlpha:Int=l_pixelrgb | (255 Shl 24)
				WritePixel(l_pix1,l_x,l_y,l_PixelNoAlpha)
			End if
		Next 
	next
	UnlockImage(p_image)
End function