Alpha on alpha calculation

BlitzMax Forums/BlitzMax Programming/Alpha on alpha calculation

Taron(Posted 2008) [#1]
Ok, my brain's exploding.

Has anyone of you done the following before:

- 2 pixmaps with alphas to merge and paste on an image to display in alphablend over a background

I feel like a perfect idiot (The forum allows labeling myself that way, does it?). I'm writing this painter/2d animation tool right now and decided to add layers.

My current process has the painting happening on a pixmap (static), which then after finishing a stroke will be merged with the layer within a for-loop. That layer then gets locked into an image which then gets drawn over a background (or other layers).

The for-loop's pseudo code kinda looks like this:
for y...
for x...
source = brush_pixmap
destination = layer_pixmap
...the problem area of color blending...
destination_alpha:+source_alpha*(1.0-destination_alpha)
layer_pixmap = result
next
next

For example, if the source is white and the destination is black, fading the source color to the destination color together with the alpha of the source will create a darkening rim around the white stroke. That's very common and no wonder. That means I have to take in account the alpha of the destination for the blending. And here's where my noodle cracks. Where the dest_alpha is 0, the source color should be 1.0, regardless to the source_alpha. But where the dest_alpha has a value, the source color needs to fade to the dest_color, while still behaving properly on top of the new blended alphas.

HELP! <lol> ... <kinda lol>

But, honestly, help!

little addon: IF THERE ONLY WAS A pixmap.paste that automatically takes the alpha in account!!!


Taron(Posted 2008) [#2]
I THINK I GOT IT! (I'm sure by now nobody cares anymore...?!)
And it's in one formula that may still be optimized:

S = source
Sa = source alpha
D = destination
Da = destination alpha

((D+(S-D)*Sa)*Da+S*(Sa*(1.0-Da))/(Da+Sa*(1.0-Da))

There you go... just in case you've been through the same pains...

Let me know if you find this to be wrong


Banshee(Posted 2008) [#3]
I'm not sure I totally get the question because i've been deprived of sleep lately! (talking to countries with very little time overlap at each end of a normal day gah, for the love of my blankie let me sleep!)

However you are aware that the colour index is four bytes, with the fourth being alpha?

I have done non-alpha blends before and I tend to break the pixel down into individual colour components as I find this makes it easier.

blendLevel= value between 0 .. 1
red=( sourceRed*blendLevel ) + ( destinationRed* (1-blendLevel) )


Taron(Posted 2008) [#4]
Haha, yeah, thanks, though! I know how the voluntary sleep deprivation feels! ;)

I use PF_RGBA8888 as format for my pixmaps and I would proudly proclaim myself as pixmap black-belt by now, HAHAHAHA! I think it's mostly because I'm working with statics now, which feels very fluent to me.

I have to still look into reducing my formula up there, but it clearly is a tricky one, even when or if it could be simplified.

Imagine the evil nature of blending into an alpha blended image continuely, over and over. Even now it doesn't sound simple to me anymore.

Thanks anyway! AND GET SOME SLEEP... then tell me how you did THAT! 8)))