[cairo] Missing transparency in ImageSurface

Kees Kling ckling at upcmail.nl
Mon Nov 26 06:51:29 PST 2012


Hi,

I have an in memory dataset which I want to overlay over a background

Here is the part I'm doing the overlay

Cairo::RefPtr<Cairo::Surface> radarModule::composeProduct() {
     Cairo::RefPtr<Cairo::ImageSurface> sfc = 
Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 800, 800);
     Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(sfc);
     cr->set_source(background->getLayers(),0,0);
     cr->paint();
     if (dataset != NULL) {
         cr->save();
         cr->set_operator(Cairo::OPERATOR_ATOP);
         cr->set_source(radarSfc,0,0);
         cr->paint();
         cr->restore();
     }
     return sfc;
}

And here is the part where I create a surface from a unsigned char buffer
void radarModule::drawImage() {
     // storing GdalDataset in an buffer;
     // this will be a one byte per pixel data
     int bufferSize =  dataset->GetRasterXSize() * 
dataset->GetRasterYSize();
     unsigned char* buffer = (unsigned char*) CPLMalloc(sizeof(char) * 
bufferSize);
     // convert this data with colortable to ARGB 4 bytes per pixel data
     unsigned char* colorbuffer = (unsigned char*) 
CPLMalloc(sizeof(char) * bufferSize * 4 );

     try {
         dataset->getPixelData(buffer);
     } catch ( string &e) {
         ctrl->getLogger()->errorStream()<<e;
     }
     // convert this buffer to the colorbuffer
     for ( int i = 0; i< bufferSize; i++) {
         // lookup color
         unsigned char a, r, g, b;
         ctable->getColor(buffer[i]).getColor(r,g,b,a);
         colorbuffer[ i * 4 ] = b;//b
         colorbuffer[ (i * 4) + 1 ]  = g;//g
         colorbuffer[ (i * 4) + 2 ]  = r;//r
         colorbuffer[ (i * 4) + 3 ]  = a;//a
     }
     int stride = 
Cairo::ImageSurface::format_stride_for_width(Cairo::FORMAT_ARGB32,dataset->GetRasterXSize());
     // Here creating the image surface and draw the pixels on
     radarSfc = 
Cairo::ImageSurface::create(colorbuffer,Cairo::FORMAT_ARGB32,
             dataset->GetRasterXSize(), dataset->GetRasterYSize(), stride);

     CPLFree(buffer);
     //CPLFree(colorbuffer);

     //radarSfc = Cairo::ImageSurface::create_from_png("r.png");
     radarSfc->write_to_png("r.png");
}

before I construct the image I'm adding a colortable, bcause the dataset 
is an index array
If I run this code al parts which are fully transparent turns out white, 
but in the l;ast line I save the surface as
png.file and that image is correct according to gimp.

I also tried to create the surface of the png file from disk and then 
the alpha blending is ok.

What am I doing wrong??

regards
Kees Kling


More information about the cairo mailing list