private void BlendPixel( int imageOffset, int tileOffset )
{
int sourceAlpha;
if ( ctp[tileOffset + 3] == 0 )
return;
byte tA = ctp[tileOffset + 3];
// Get final alpha channel.
int finalAlpha = tA + ( ( 255 - tA ) * image[imageOffset + 3] ) / 255;
// Get percentage (out of 256) of source alpha compared to final alpha
if ( finalAlpha == 0 ) {
sourceAlpha = 0;
} else {
sourceAlpha = tA * 255 / finalAlpha;
}
// Destination percentage is just the additive inverse.
int destAlpha = 255 - sourceAlpha;
if ( z < ( Map.Height >> 1 ) ) {
int shadow = ( z >> 1 ) + mh34;
image[imageOffset] = ( byte )( ( ctp[tileOffset] * sourceAlpha * shadow + image[imageOffset] * destAlpha * Map.Height ) / blendDivisor );
image[imageOffset + 1] = ( byte )( ( ctp[tileOffset + 1] * sourceAlpha * shadow + image[imageOffset + 1] * destAlpha * Map.Height ) / blendDivisor );
image[imageOffset + 2] = ( byte )( ( ctp[tileOffset + 2] * sourceAlpha * shadow + image[imageOffset + 2] * destAlpha * Map.Height ) / blendDivisor );
} else {
int shadow = ( z - ( Map.Height >> 1 ) ) * ShadingStrength;
image[imageOffset] = ( byte )Math.Min( 255, ( ctp[tileOffset] * sourceAlpha + shadow + image[imageOffset] * destAlpha ) / 255 );
image[imageOffset + 1] = ( byte )Math.Min( 255, ( ctp[tileOffset + 1] * sourceAlpha + shadow + image[imageOffset + 1] * destAlpha ) / 255 );
image[imageOffset + 2] = ( byte )Math.Min( 255, ( ctp[tileOffset + 2] * sourceAlpha + shadow + image[imageOffset + 2] * destAlpha ) / 255 );
}
image[imageOffset + 3] = ( byte )finalAlpha;
}