All-Integral Alpha Compositing Using Bitwise Operators - Also New to JGO!

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

Nice! This old thread might interest you too - http://www.java-gaming.org/topics/some-new-blendmodes-add-multiply-overlay-etc/24529/view.html - mostly bitwise operation and multiplication.

Ooooooooooooooooooooooooooh! It most certainly DOES! Thank you!!! You don’t know how much I love finding neat things you can do with bitwise operation! It’s certainly a unique class of challenges, that’s for sure! And graphics fx to boot, well this is right up my alley!

I almost did a double take when I saw BlueJay…

Haven’t seen that software in a long time!

Not a surprise xD! Truth is I love Blue J and I never grew out of it. I tend to prefer something more simplistic when programming anyway; something that doesn’t have a lot of clutter to bog down my concentration. I also just love it because it just gets things right that I wish other IDEs would also, like how it organizes open classes and packages into windows on the task bar rather than in tabs on the UI (which I personally hate) and also you’ll never find an IDE that gets code highlighting right the way Blue J does. Colored keywords and changeable background colors are ok but being able to easily visually distinguish entire code blocks just by their color is even better I feel. It helps me visually step through my program flow much more easily. So anywho, yeah that’s why I still use Blue J lol!