Re: AlphaBlending Question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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/


.



Relevant Pages

  • Re: AlphaBlending Question
    ... In order to save some calculations over a number of transparent png ... So my thinking is, if the alpha is 0, what does it matter what the R,G,B ... This is because with pre-multiplication, ... combined with the inverse-alpha multiplication of the current pixel. ...
    (microsoft.public.vb.winapi.graphics)
  • Re: Transparency of BMP image pixels
    ... which has widespread alpha channel support. ... the transparency depth of any texture's pixel from full ... transparent from the right to the left of the texture: ... I suppose I should draw far transparent objects first, ...
    (comp.graphics.api.opengl)
  • Re: Drawing images over tree icons
    ... pixel is semiopaque if alpha value is 127, ... pixel is transparent if alpha value is 0. ...
    (comp.lang.java.gui)
  • Re: Texture is transparent, but primitive is opaque?
    ... I have a texture with transparency information, ... colors of pixels in the texture all I want, if that pixel has an alpha ... But the primitive I use to show the texture is not transparent. ...
    (comp.graphics.api.opengl)
  • Texture is transparent, but primitive is opaque?
    ... I have a texture with transparency information, ... colors of pixels in the texture all I want, if that pixel has an alpha ... But the primitive I use to show the texture is not transparent. ...
    (comp.graphics.api.opengl)