alphablendingblendingpixman

How to modify alpha premultiplied image opacity?


I have an alpha premultiplied image that requires changing the opacity of it. The opacity change applied to the image can be from 0.0 to 1.0.

This process is easy to achieve using the following formula:

Cn = C*alpha;
An = A*alpha;

However, I'm trying to avoid writing this code my self and allow the highly optimized library pixman do the job. They provide various blending options but I'm not too sure how to use mask for or which blend option to utilize. I have tried using MULTIPLY but that just made the image darker as expected I guess...

Any ideas which potential blend option I should use?

{ "CLEAR",      PIXMAN_OP_CLEAR },
    { "SRC",        PIXMAN_OP_SRC },
    { "DST",        PIXMAN_OP_DST },
    { "OVER",       PIXMAN_OP_OVER },
    { "OVER_REVERSE",   PIXMAN_OP_OVER_REVERSE },
    { "IN",     PIXMAN_OP_IN },
    { "IN_REVERSE", PIXMAN_OP_IN_REVERSE },
    { "OUT",        PIXMAN_OP_OUT },
    { "OUT_REVERSE",    PIXMAN_OP_OUT_REVERSE },
    { "ATOP",       PIXMAN_OP_ATOP },
    { "ATOP_REVERSE",   PIXMAN_OP_ATOP_REVERSE },
    { "XOR",        PIXMAN_OP_XOR },
    { "ADD",        PIXMAN_OP_ADD },
    { "SATURATE",   PIXMAN_OP_SATURATE },

    { "MULTIPLY",   PIXMAN_OP_MULTIPLY },
    { "SCREEN",     PIXMAN_OP_SCREEN },
    { "OVERLAY",    PIXMAN_OP_OVERLAY },
    { "DARKEN",     PIXMAN_OP_DARKEN },
    { "LIGHTEN",    PIXMAN_OP_LIGHTEN },
    { "COLOR_DODGE",    PIXMAN_OP_COLOR_DODGE },
    { "COLOR_BURN", PIXMAN_OP_COLOR_BURN },
    { "HARD_LIGHT", PIXMAN_OP_HARD_LIGHT },
    { "SOFT_LIGHT", PIXMAN_OP_SOFT_LIGHT },
    { "DIFFERENCE", PIXMAN_OP_DIFFERENCE },
    { "EXCLUSION",  PIXMAN_OP_EXCLUSION },
    { "HSL_HUE",    PIXMAN_OP_HSL_HUE },
    { "HSL_SATURATION", PIXMAN_OP_HSL_SATURATION },
    { "HSL_COLOR",  PIXMAN_OP_HSL_COLOR },
    { "HSL_LUMINOSITY", PIXMAN_OP_HSL_LUMINOSITY },

This is the actual composite function:

void pixman_image_composite(pixman_op_t        op,
                           pixman_image_t    *src,
                           pixman_image_t    *mask,
                           pixman_image_t    *dest,
                           int16_t            src_x,
                           int16_t            src_y,
                           int16_t            mask_x,
                           int16_t            mask_y,
                           int16_t            dest_x,
                           int16_t            dest_y,
                           uint16_t           width,
                           uint16_t           height);

Solution

  • The solution was actually fairly easy. At the beginning I didn't think too much of the mask. Then I realized ah a mask of course just like photoshop uses.

    So applying a mask with the correct alpha value will cause the image to change it's opacity.

    So changing the image opacity to 50% we would use a mask with white color and alpha value at 50%.