So I’m new to the site and I figured what better way to introduce myself than to post some cool code I wrote recently? Just as the name suggests I’ve written some code that allows me to blend partially transparent images using only bitwise operations. Not to be confused with transparency masking (although my engine supports that too), arbitrarily translucent colors can be blended to within a modest granularity without relying on floating point or even fixed point arithmetic, but by direct manipulation of the bits comprising the composite images. It’s a surprisingly accurate solution despite its rather quantized domain…! Anywho, for those of you who might find it useful or are curious I’ve provided a link to a demonstration video so you can see it in action! Feel free to ask if you have any questions!
Anywho, glad to be here; thanks!
int alpha = ...;//can be retrieved or set depending on needs, therefor per pixel alpha is possible
alpha = (alpha >>> 8) | (alpha >>> 16) | (alpha >>> 24);//copy the alpha to rgb components in this variable
int spritePixel = spritePixels[i];
int lsb = 0x000000ff; //least significant byte; sign correction
int flag = ((spritePixel >> 16) & (spritePixel >> 8) & spritePixel & lsb) + 1;//it gets complicated here; basically it ensures pixel values
int mask = (flag << 23) >> 31; //of 0xffffff (white) generate a mask of 0xffffff and anything
//else generates a mask of 0 (this is the automatic bit mask)
if(lsb - (alpha & lsb) <= 0)//completely opaque?
{
this.pixels[i] &= mask;//transparency mask "AND" phase (black silhouette over background)
this.pixels[i] |= ~mask & spritePixel;//transparency mask "OR" phase (full color sprite over background)
}
else//arbitrarily translucent
this.pixels[i] &= spritePixel | alpha;//BLACK MAGIC, I SWEAR
tvbmPSVQoOc