I have a problem with displaying alpha transparency using GTK and Cairo. I try to display this image 1
If I do the alpha blending my self, everything works.
If I pass the alpha values directly to Cairo, the shadow seems to render fine, but the glow effect is corrupted.
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 push the 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 operator to CAIRO_OPERATOR_OVER does not help, the result is still the same.
auto
don't say much. How did you come up with your formula for the blending (cairographics.org/operators uses another formula for OVER). Do you know what pre-multiplied alpha means? Which of the following values describes "fully red with 50% transparency"? (1) (0.5,0,0,0.5) or (2) (1,0,0,0.5) (Hint: (1) is correct) – Vercelli