Re: AlphaBlending Question
- From: "Mike D Sutton" <EDais@xxxxxxxx>
- Date: Sat, 10 Feb 2007 14:03:45 -0000
I've been using Mike Sutton's ModifyDIBArrayHack routine to quickly pre-multiple the pixels of a transparent png
before running the AlphaBlend function into an existing DC, but there is something I'm not understanding.
In order to save some calculations over a number of transparent png files, I was attempting to ignore the
premultiplication if the alpha byte was already 0 (transparent)...since all that does is change the R,G,B bytes to 0
anyways.
So my thinking is, if the alpha is 0, what does it matter what the R,G,B values are???
Interestingly enough, when I set the RGBs of zero alpha pixels to 0, the AlphaBlend function works fine. However, if I
simply ignore the pre-multiplication if the alpha channel is 0, then I see the a white/grey color for those pixels.
Can someone explain this logic to me?
This is because with pre-multiplication, the data that's being drawn forgoes the multiplication against its alpha
component and is simply combined with the inverse-alpha multiplication of the current pixel. In this case it takes the
full colour of the current pixel and adds white to that which caps out at white; a neat trick for performing additive
blending but not the desired output.
Put another way, here's the normal calculation for alpha blending a pixel:
P = (D * (1 - A)) + (S * A)
Now compare this with the pre-multiplied version:
P = (D * (1 - A)) + S
Where S and D are the source and destination pixels respectively, A is the alpha component and P is the pixel value to
plot.
As you can see, a costly multiplication has been avoided since it assumes that the (S * A) operation has already taken
place - this is pre-multiplication.
Since any pixel with an alpha component of 0 will always pre-multiply to 0 in the other three channels, you don't need
to perform the multiplication when pre-multiplying the data if you're worried about performance, just set them to 0
which will still give you the optimisation you're looking for. This of course assumes that you're working with images
that have large completely transparent areas otherwise it will actually slow the routine down for images with few or no
completely transparent areas since you're now performing an additional three branches per pixel. Personally, I've never
found putting the extra check in to give much of a performance difference for the majority of images but you may want to
make it an option.
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/
.
- Follow-Ups:
- Re: AlphaBlending Question
- From: wxforecaster
- Re: AlphaBlending Question
- References:
- AlphaBlending Question
- From: wxforecaster
- AlphaBlending Question
- Prev by Date: AlphaBlending Question
- Next by Date: Re: AlphaBlending Question
- Previous by thread: AlphaBlending Question
- Next by thread: Re: AlphaBlending Question
- Index(es):
Relevant Pages
|