Pre-multiplied alpha compositing

后端 未结 4 1281
野性不改
野性不改 2021-02-06 15:30

I am trying to implement pre-multiplied alpha blending. On this page : What Is Color Blending?, they do explain standard alpha blending but not for pre-multiplied values.

<
4条回答
  •  梦如初夏
    2021-02-06 16:02

    The problem you've got is depends on whether or not you pre-multiplied your source alpha value by itself as part of your pre-multiplication. If you did, then the srcA you're using in the target multiplications is the square of the real source Alpha, so you need to take the square root for that calculation:

    originalSrcA = Math.Sqrt(srcA);
    a = ((srcA)) + ((tgtA * (255 - originalSrcA)) >> 8);
    r = ((srcR)) + ((tgtR * (255 - originalSrcA)) >> 8);
    g = ((srcG)) + ((tgtG * (255 - originalSrcA)) >> 8);
    b = ((srcB)) + ((tgtB * (255 - originalSrcA)) >> 8);
    

    If you haven't pre-multiplied by itself (which I think is more likely), you will need to multiply by itself to get the same result as the working one:

    a = ((srcA * srcA) >> 8) + ((tgtA * (255 - srcA)) >> 8);
    r = ((srcR)) + ((tgtR * (255 - srcA)) >> 8);
    g = ((srcG)) + ((tgtG * (255 - srcA)) >> 8);
    b = ((srcB)) + ((tgtB * (255 - srcA)) >> 8);
    

提交回复
热议问题