我正在尝试使用 pixman_image_fill_boxes() 执行 alpha 混合。
void blend1(void *di, int p, int w, int h, int r, int g, int b, int a)
{
pixman_color_t color = { .red = r, .green = g, .blue = b, .alpha = a };
pixman_image_t *dst = pixman_image_create_bits(PIXMAN_r8g8b8a8, w, h, di, p);
pixman_box32_t box = { .x1 = 0, .y1 = 0, .x2 = w, .y2 = h };
pixman_image_fill_boxes(PIXMAN_OP_OVER, dst, &color, 1, &box);
pixman_image_unref(dst);
}
但是
di
保持不变。 这个简单的操作我哪里出错了?
pixman_color_t
有 u16 通道,而不是通常的 u8。并且这些值必须在 0 到 0xffff 之间,而不是通常的 0 到 0xff。
错误
pixman_color_t color = { .red = r, .green = g, .blue = b, .alpha = a };
修复
pixman_color_t color;
color.red = (double)r*0xffff/0xff;
color.green = (double)g*0xffff/0xff;
color.blue = (double)b*0xffff/0xff;
color.alpha = (double)a*0xffff/0xff;
// premul
color.red = color.red*color.alpha/0xffff;
color.green = color.green*color.alpha/0xffff;
color.blue = color.blue*color.alpha/0xffff;