Strip alpha from pixels above 0

BlitzMax Forums/BlitzMax Beginners Area/Strip alpha from pixels above 0

ima747(Posted 2008) [#1]
I have a PNG loaded into a pixmap, I need to loop through it and change any pixels that are not totally transparent to being solid.

Everything I try seems to crash, I think somehow readpixel or writepixel is getting out of the application memory and windows is... less than happy about that...

Though it seems to work when I do it once, but then when I do it a second time to a different pixmap, or even the same one, it crashes...

Does anyone have a sample they can share?


Jesse(Posted 2008) [#2]
can you post some code?


grable(Posted 2008) [#3]
Your probably doing something like this?
For Local y:Int = 0 To pixmap.Height
  For Local x:Int = 0 To pixmap.Width
     ...

IE, using To instead of Until (or forgetting to -1)

Either way, using ReadPixel() and WritePixel() when your going to access the whole pixmap isnt realy necessary.

Try this instead:
Function StripPartialAlpha( px:TPixmap)
	If Not px Then Return
	Local p:Byte Ptr = px.Pixels
	Local stop:Byte Ptr = p + px.Capacity
	Select px.Format
		Case PF_RGBA8888
			While p < stop
	  			If p[3] > 0 Then p[3] = 255
				p :+ 4
			Wend
		Case PF_BGRA8888
			While p < stop
	  			If p[0] > 0 Then p[0] = 255
				p :+ 4
			Wend	
	EndSelect
EndFunction



ima747(Posted 2008) [#4]
I was using To, I still have no idea what I was doing wrong, but your function is way faster and best of all it works! thank you!


ImaginaryHuman(Posted 2008) [#5]
You could do it a lot faster than that but it looks like it would work. You shouldn't be looping TO pixmap.Height, you should be going Until pixmap.Height

Same for the width. And if you compile with debug on it should be telling you something?