[cairo] How to flatten several data surfaces with transparency

Bill Spitzak spitzak at gmail.com
Mon Nov 5 12:10:53 PST 2012


Cairo wants pre-multiplied data for the OVER composite. This likely 
means that the green value should depend on y just like the alpha value 
does. Ie set the green to (y < 50 ? 0 : 50 * (y - 50)/height)

Also I recommend filling in the buffer as 32-bit words, rather than 
bytes. This is faster and will make the it work on a big-endian machine.

unsigned char* buf = (unsigned char*)malloc(4*width*height);
   cairo_surface_t* dataSurface = cairo_image_surface_create_for_data 
(buf, CAIRO_FORMAT_ARGB32, width, height, rowLen);
   for (y=0; y<height; y++)
   {
     for (x=0; x<width; x++)
     {
       //setColor(&buf[y*rowLen+x*4], 0x00FF00, y*255.0/3/height);
       buf[y*rowLen+x*4+0] = 0;
       buf[y*rowLen+x*4+1] = 50;
       buf[y*rowLen+x*4+2] = 0;
       buf[y*rowLen+x*4+3] = (y < 50 ? 0 : (y-50)*255.0/height);
     }
   }


More information about the cairo mailing list