PaintLab / PixelFarm

From Vectors to (sub) Pixels, C# 2D Rendering Library
Other
156 stars 20 forks source link

[NOTES] Premultiplied alpha #12

Closed prepare closed 6 years ago

prepare commented 6 years ago

Premultiplied alpha

from from https://microsoft.github.io/Win2D/html/PremultipliedAlpha.htm

alpha1


...

Alpha in Win2D

Win2D uses straight alpha in its API surface, but premultiplied alpha for internal rendering operations.

Windows.UI.Color values use straight alpha. Whenever you pass a color to a Draw or Fill method, set the color of a brush, or clear to a color value, this color is specified using straight alpha.

The pixel values stored in a bitmap or rendertarget, and the drawing or blending operations that operate on these surfaces, use premultiplied alpha. When bitmaps are loaded from a file their contents are automatically converted into premultiplied format. When you call a Win2D drawing method, its color parameter is converted from straight to premultiplied before the actual drawing takes place. ....


alpha2


see more at
https://blogs.msdn.microsoft.com/shawnhar/2009/11/06/premultiplied-alpha/

https://blogs.msdn.microsoft.com/shawnhar/2009/11/07/premultiplied-alpha-and-image-composition/

http://ssp.impulsetrain.com/gamma-premult.html

prepare commented 6 years ago

from https://blogs.msdn.microsoft.com/shawnhar/2009/11/06/premultiplied-alpha/

In this world, RGB and alpha are linked. To make an object transparent you must reduce both its RGB (to contribute less color) and also its alpha (to obscure less of whatever is behind it). Fully transparent objects no longer have any RGB color, so there is only one value that represents 100% transparency (RGB and alpha all zero).

This is more like how light behaves in the real world. What is the RGB of my car windscreen? None: it is transparent, so has no color. How about my sunglasses? These have a fractional alpha value (letting some light some through, while blocking some) and also contribute some RGB for that nice rose-tinted glow.

To use premultiplied alpha, in addition to setting the appropriate renderstates, you must also convert your source graphics into premultiplied format. Drawing a non premultiplied color with premultiplied blending will not give sensible results!