[cairo] Compositing strategies and pre-multiplied alpha

Behdad Esfahbod behdad at cs.toronto.edu
Wed Sep 21 14:24:51 PDT 2005


On Wed, 21 Sep 2005, Mauricio Piacentini wrote:

> I need some help understanding the rationale behind using pre-multiplied
> alpha in a graphics API, and I understand this is used in Cairo image
> surfaces, according to the docs. Please understand I am not questioning
> this decision, just trying to grasp the implications of this method as
> far as my particular needs are concerned.

Hi there,

Not that I really know about these things myself, but sharing my
understanding only.  IIRC pre-multiplied alpha makes most of
blending functions faster by not requiring a division, and
moreover, makes computation for the rgb channels and the alpha
channel symmetric, ie. the same equation.  Read the Porter-Duff
paper, it's all there.


> Let me explain this better: suppose I want to display two rotated
> bitmaps and two svg files in the same screen, but animated. A possible
> optimization would be to maybe render each element to a different
> surface, and then composite the resulting "sprites" in a final screen
> surface. This way I would not have to re-render the svg data for example
> or rotate the bitmap at every frame, unless I decide to rotate or scale
> one of the elements. As long as I am simply moving the sprites in x and
> y the caching of data as bitmaps would conceivably speed up things a
> lot, right?

Right.  And you can do that in cairo by rendering them into two
surfaces and then paste them using cairo_paint or
cairo_paint_with_alpha.


> The big question is: am I right to assume that doing the above (caching
> the data as bitmaps with pre-multiplied alpha) will automatically reduce
> the quality of antialiasing? The effect would probably not be noticeable
> on a black background, but if I composite one cached bitmap on top of
> another I will have a drop in antialiasing quality, wouldn't I? Or is
> this not important as long as I move my sprites in whole pixel
> increments (subpixel is obviously not possible in a scenario like this.)

No idea, but I think the pre-multiplied alpha calculations are
cleaner and more precise than nomal ones, so I wouldn't be
surprised if it was the other way around.  I may be completely
wrong.

> Regards,
> Mauricio

--behdad
http://behdad.org/


More information about the cairo mailing list