SavePixMapPNG Alpha

BlitzMax Forums/BlitzMax Beginners Area/SavePixMapPNG Alpha

BLaBZ(Posted 2010) [#1]
Hi there!

How would you save a .png and include it's alpha values?

I've tried
Local TheImage:TPixmap = LoadPixmap("file.png")
	
DrawPixmap(TheImage,0, 0)
			
'DrawPixmap(aPixmap, 0, 0)
aPixmap = GrabPixmap(0, 0, PixmapWidth(aPixmap), PixmapHeight(aPixmap))
'save the pixmap
SavePixmapPNG(aPixmap, SavePath, 9)




plash(Posted 2010) [#2]
GrabPixmap cannot get alpha values. It makes perfect sense if you stop and think about it.


BLaBZ(Posted 2010) [#3]
Yah, I can make sense out of it, I just can't make sense out of how you would save a png with its alpha values


plash(Posted 2010) [#4]
By not using GrabPixmap.


Jesse(Posted 2010) [#5]
if the png has alpha it will save it with alpha.
when you grab an image from the screen the alpha value will be 255.


Raph(Posted 2010) [#6]
I don't remember where in here this came from, but you can draw the image once on white and grab it, then again on black and grab it, then use this function:

' given two identical images, one drawn on white and one drawn on black,
' return a third image that is the true color plus the alpha mask.

Function makeAlpha:TImage(img1:TImage, img2:TImage) 
	i1:TPixmap = LockImage(img1) 
	i2:TPixmap = LockImage(img2) 
	For x = 0 To ImageWidth(img1) - 1
		For y = 0 To ImageHeight(img1) - 1
			rgba = ReadPixel(i1, x, y) 
			rgba2 = ReadPixel(i2, x, y) 
			If rgba <> rgba2
				GetRGB(rgba, a, r, g, b) 
				GetRGB(rgba2, a2, r2, g2, b2) 
				newa = 255 - Abs(r - r2) 
				newrgba = GenRgb(r2, g2, b2, newa) 
				WritePixel(i1, x, y, newrgba) 
			EndIf
		Next
	Next
	Return LoadImage(i1) 
End Function



ImaginaryHuman(Posted 2010) [#7]
Grabpixmap can potentially grab alpha values IF the backbuffer of the screen has an alpha channel. When you opent he screen using Graphics() or whatever, or in the default SetGraphicsDriver() flags, you must include a request for ALPHA_BUFFER. *IF* you get one, then it is technically possible to download RGBA data instead of just RGB ... but I don't know if grabpixmap has been designed to transfer the alpha channel from the graphics memory or just the RGB colors. ... using glReadPixels() at least, you can do it via opengl.

However, in order to get something INTO the alpha channel of the backbuffer you must draw using a blend mode that supports drawing to the alpha channel. If you use typical AlphaBlend, it uses the source alpha to scale source and destination colors but i'm doubtful that it writes to the destination alpha channel. SolidBlend might. You'd have to test it. Not sure if drawpixmap does either.


John G(Posted 2010) [#8]
Hi BLaBZ,

I've just been experimenting with something similar. I'm trying to simulate the beam from a headlight which can illuminate the background but the beam strength deteriorates with distance.

[code]

' ========================= HEADLIGHT ======================= 2010-08-18

Strict

Local I:Int, GW:Int, GH:Int, GD:Int, GWH:Int
Local HeadLightPIX:TPixmap = CreatePixmap ( 256, 256, PF_A8 ) ' PF_A8 = Grey Scale Option
Local HeadLightIMG:TImage = CreateImage ( 256, 256, MASKEDIMAGE | FILTEREDIMAGE | DYNAMICIMAGE )
SetImageHandle( HeadLightIMG, 128, 0 )

GW = DesktopWidth()
GH = DesktopHeight()
GD = DesktopDepth()
If GD=0 Then GD=32
GWH = GW/2

Graphics GW, GH, GD

SetOrigin GWH, 0
SetBlend ALPHABLEND
SetColor 255, 255, 255
SetClsColor 0, 0, 0

For I = 0 Until 256
SetAlpha 1.0 - I/256.0
DrawLine -0.375*(I+8), I, +0.375*(I+8), I
Next

GrabImage HeadLightIMG, GWH-128, 0

Flip
Delay 2000
Cls

SetAlpha 0.875
DrawImage HeadLightIMG, 0, 0

HeadLightPIX = GrabPixmap ( GWH-128, 0, 256, 256 )
SavePixmapPNG HeadLightPIX, "HeadLight.png"

Flip
Delay 4000
End

[/Code]

Once the PNG image is reloaded, it can be rotated, scaled, colored, or new alpha applied. Use LightBlend mode to illuminate the background. To improve edge smoothness, draw the original larger -- say 1024x1024. Hope this helps.