How to use pre-multiplied during image convolution to solve alpha bleed problem?

后端 未结 3 722
南方客
南方客 2021-02-02 17:40

i\'m trying to apply a box blur to an transparent image, and i\'m getting a \"dark halo\" around the edges.

Jerry Huxtable has a short mention of the problem, and a very

3条回答
  •  时光取名叫无心
    2021-02-02 18:20

    tkerwin has already provided the correct answer, but it seems to need further explanation.

    The math you've shown in your question is absolutely correct, right up until the end. It is there that you're missing a step - the results are still in a pre-multiplied alpha mode, and must be "unmultiplied" back to the PixelFormat32bppARGB format. The opposite of a multiply is a divide, thus:

    finalRed = finalRed * 255 / finalAlpha;
    finalGreen = finalGreen * 255 / finalAlpha;
    finalBlue = finalBlue * 255 / finalAlpha;
    

    You've expressed a concern that the divide might create a result that is wildly out of range, but that won't happen. If you trace the math, you'll notice that the red, green, and blue values can't be greater than the alpha value, because of the pre-multiplication step. If you were using a more complicated filter than a simple box blur it might be a possibility, but that would be the case even if you weren't using alpha! The correct response is to clamp the result, turning negative numbers into 0 and anything greater than 255 into 255.

提交回复
热议问题