Alpha transparency in Cairo

I have a problem displaying alpha transparency using GTK and Cairo. I am trying to display this image 1

If I do alpha blending myself, everything works.

Manual alpha blending

If I pass the alpha values ​​directly to Cairo, the shadow seems beautiful, but the glow effect is damaged.

Cairo alpha rendering

Is this a bug in Cairo 1.14.2, or am I missing something?

//Need deprecated API to get background color
GdkColor color = gtk_widget_get_style(widget)->bg[GTK_STATE_NORMAL];
Pixel color_blend
    {
     uint8_t(255*color.red/65535.0f)
    ,uint8_t(255*color.green/65535.0f)
    ,uint8_t(255*color.blue/65535.0f)
    ,255
    };
while(ptr!=ptr_end)
    {
//  TODO: Interpolate
    auto row_src=size_t(row*factor);
    auto col_src=size_t(col*factor);

    auto alpha=ptr_src[row_src*width_in + col_src].v3/255.0f;
    *ptr=
        {
    //  Using manual alpha blend works  
        uint8_t(alpha*ptr_src[row_src*width_in + col_src].v2 + (1-alpha)*color_blend.v2)
        ,uint8_t(alpha*ptr_src[row_src*width_in + col_src].v1 + (1-alpha)*color_blend.v1)
        ,uint8_t(alpha*ptr_src[row_src*width_in + col_src].v0 + (1-alpha)*color_blend.v0)
        ,255
    /*  This appears to be broken 
        ptr_src[row_src*width_in + col_src].v2
        ,ptr_src[row_src*width_in + col_src].v1
        ,ptr_src[row_src*width_in + col_src].v0
        ,ptr_src[row_src*width_in + col_src].v3*/
        };

    ++col;
    if(col==width_out)
        {
        col=0;
        ++row;
        }
    ++ptr;
    }

I click pixels using

auto surface=cairo_image_surface_create_for_data((uint8_t*)pixels.begin(),CAIRO_FORMAT_ARGB32,width_out,height_out,width_out*sizeof(Pixel));

cairo_set_source_surface(cr, surface, 0.5*(width-width_out), 0.0);
cairo_paint(cr);

cairo_surface_destroy(surface);

Explicitly setting the statement to CAIRO_OPERATOR_OVER does not help, the result is the same.

+4
source share
1 answer

, . . ( ), 50% 0x7f << 24 | 0x7f . (- -) undefined, 0xff << 24 | 0x7f .

. http://www.cairographics.org/manual/cairo-Image-Surfaces.html#cairo-format-t:

. ( 50% 0x80800000, 0x80ff0000.)

P.S.: , - uint32_t , . uint32_t pixel = (r << 24) | (g << 16) | (b << 8) | a;. , .

P.P.S.: OVER Cairo source_color + target_color * (1 - source_alpha), source_color * source_alpha + target_color * (1 - source_alpha). . http://www.cairographics.org/operators/. .

: , , pre-multipie alpha. .

+3

Source: https://habr.com/ru/post/1629676/


All Articles