Why is my cairo_surface_t drawing semi-transparent?

I am trying to draw a png image the contents of which I have in memory in ARGB32 format using C ++ Cairo and Gtk on Ubuntu.

First, I create a GtkDrawingArea, and then on my exponential event, I draw a solid blue background with a red line from top to bottom to the right, then I create a surface and try to draw it over the drawing area. Here's my event callback call:

unsigned char *pCairoBuf = ...

....

gboolean OnDrawingAreaExposeEvent(GtkWidget *pWidget, GdkEventExpose *pEvent, gpointer data)
{
    cairo_t *cr = gdk_cairo_create(pWidget->window);

    cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
    cairo_rectangle(cr, 0.0, 0.0, pEvent->area.width, pEvent->area.height);
    cairo_fill(cr);

    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
    cairo_set_line_width(cr, 10.0);
    cairo_move_to(cr, 0.0, 0.0);
    cairo_line_to(cr, pEvent->area.width, pEvent->area.height);
    cairo_stroke(cr);

    // Use the existing buffer pCairoBuf that has (iRenderWidth * iRenderHeight * 4) bytes, 4 bytes per pixel for ARGB32 and zero row padding   
    cairo_surface_t *pSurface = cairo_image_surface_create_for_data(pCairoBuf, CAIRO_FORMAT_ARGB32, iRenderWidth, iRenderHeight, (iRenderWidth * 4));

    cairo_set_source_surface(cr, pSurface, 0.0, 0.0);
    cairo_paint(cr);

    cairo_destroy(cr);

    return TRUE;
}

      

The drawing area and image are 244x278 pixels. Image - image of Smokey Bear's head and transparently around its head:

enter image description here

And I expect the final output to look like this:

enter image description here

But it looks like this:

enter image description here

I haven't added any code that shows how I got the pCairoBuf data buffer, because I realized that this would only cloud the problem, but perhaps I am wrong? I thought there was something else I was doing wrong with Cairo surfaces etc. that would explain the difference between what I expect and what I get.

Thanks in advance for your help!

+3


source to share


2 answers


I understood that. When I was filling the ARGB32 data in the buffer I was transitioning to cairo_image_surface_create_for_data()

, I was filling in bytes in this order: A, R, G, B. As an experiment, I reversed the order and filled in the B, G, R, A bytes and it worked fine.



0


source


As expected, without using any of these libraries, I would say that your alpha channel is uniform across your image and rendering, applying this also to the parts of the image you want opaque. Try to apply the alpha channel only to the pixels that you would like to be transparent.



+1


source







All Articles