Overlaying one alpha'd pixmap on another

BlitzMax Forums/BlitzMax Programming/Overlaying one alpha'd pixmap on another

taxlerendiosk(Posted 2004) [#1]
I've not got much experience in dealing with alpha - does this seem right? I'm not that interested in speed improvements (this is only for preprocessing, not in realtime) just that it combines two alpha pixels correctly.

Edit: This is for alpha blendmode, so "correctly" means as though the image of "OldPixels" was drawn in alpha and then the image of "NewPixels" was drawn in alpha over it, but from drawing a single pixmap.

Function WritePixelAlpha(PM:TPixMap, X,Y, NewPixel)
	OldPixel = ReadPixel(PM,X,Y)
	
	NewA:Float =  (NewPixel Shr 24) / 255.0
	NewR = (NewPixel Shr 16) & $FF
	NewG = (NewPixel Shr  8) & $FF
	NewB =  NewPixel         & $FF

	OldA:Float =  (OldPixel Shr 24) / 255.0
	OldR = (OldPixel Shr 16) & $FF
	OldG = (OldPixel Shr  8) & $FF
	OldB =  OldPixel         & $FF

	CombA = 255.0 * (NewA + ((1.0 - NewA) * OldA))
	CombR = (NewR * NewA) + (OldR * (1.0 - NewA))
	CombG = (NewG * NewA) + (OldG * (1.0 - NewA))
	CombB = (NewB * NewA) + (OldB * (1.0 - NewA))

	CombPixel = (CombA Shl 24) | (CombR Shl 16) | (CombG Shl 8) | CombB
	
	WritePixel PM, X,Y, CombPixel
End Function


(Alternatively there could be a much easier way of doing this already and I'm just stupid. In which case I'd like to be told about that. :)


Dreamora(Posted 2004) [#2]
There is no "combining right", it fully depends on the choosen blendmode ...


taxlerendiosk(Posted 2004) [#3]
Alpha, I'll edit the original post...


ImaginaryHuman(Posted 2004) [#4]
Well it looks okay, but you might be better off converting the two pixmaps into images, draw one to the backbuffer, then draw the other one on top of it with alphablending and then grab it back into a pixmap... probably overall a lot faster than individual pixel reads and writes and no need for you to figure out how to blend everything yourself.


taxlerendiosk(Posted 2004) [#5]
That's the first thing I thought of, but would the alpha channel be retained? The final pixmap should have a proper alpha channel too, not just be a solid image. Also as I said, speed's not an issue particularly.


ImaginaryHuman(Posted 2004) [#6]
You can switch blending on to merge the alpha channels I think