[cairo] How to flatten several data surfaces with transparency

Steve Moreau moreau.steve at free.fr
Mon Nov 5 07:24:59 PST 2012

Hi all,

I was playing with cairo to flatten several layers with transparency.
Some of my surfaces are data surface filled by an external lib.
Pixels on these surfaces have distributed alpha values (not only fully
transparent or opaque).

When I dump any surface independently, the transparency seems to be fine.

But as soon as I want to compose them, the transparent values of the top
layer behaves oddly.
I did a lot of tests but the best I could do to explain is the dummy cpp
file attached.

Basically I draw a gradient manually (let's say it is what the external lib
do). So alpha layer is in [0,1]. I want a red square to be behind this
gradient. So I expect the result square not to change on parts where
transparency is near 0.

When line 52 : buf[y*rowLen+x*4+1] = 50;
I see the square but is already altered because it is not its actual color.

When line 52 : buf[y*rowLen+x*4+1] = 255;
I see everything in green and I just can't explain why for now.

I keep on reading the doc to get it, but if someone could explain what
happens and how I could merge/flatten/composite several layers, it would be
nice, because it is a little unclear to me for now.



A copy/paste way to compile and run the example :
$ g++ -g -I/usr/include/cairo -o test Dummy.cpp -lpng -lcairo && ./test

A way to see the result :
$ eog test.png
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20121105/7d7e1207/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Dummy.cpp
Type: text/x-c++src
Size: 5000 bytes
Desc: not available
URL: <http://lists.cairographics.org/archives/cairo/attachments/20121105/7d7e1207/attachment.cpp>

More information about the cairo mailing list