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. :)
|