alpha blending with transparent background

823 views
Skip to first unread message

Marshall Greenblatt

unread,
May 24, 2012, 5:21:21 PM5/24/12
to skia-d...@googlegroups.com
Hi All,

I'm using skia to render a block with alpha transparency on top of a completely transparent background. I would like the result to keep the source RGB values and only change the alpha value. Instead, the whole ARGB value is blended. This appears to be done in the S32A_Blend_BlitRow32_SSE2 function:

alpha     = 0xbf
src_pixel = 0xffffffffffffffffffffffffffffffff
dst_pixel = 0000000000000000000000000000000000
result    = 0xbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf

Instead, I'm seeking the following result:

alpha     = 0xbf
src_pixel = 0xffffffffffffffffffffffffffffffff
dst_pixel = 0000000000000000000000000000000000
result    = 0xbfffffffbfffffffbfffffffbfffffff

Does skia support this type of blending in cases where the backround device is completely transparent? If not, would it be reasonable to add support for it?

Related issues:

http://code.google.com/p/skia/issues/detail?id=547
http://code.google.com/p/chromiumembedded/issues/detail?id=584

Thanks,
Marshall

liang jian

unread,
May 24, 2012, 8:51:56 PM5/24/12
to skia-d...@googlegroups.com
Hi, I am not an expert of skia, but I hope my anwser will help you.
Your expectation and the current result is actually the same
color. The difference is: the color result of skia is premultiplied
version of your expected color(which is not premultiplied). I think
skia will always store pixels in premultiplied manner for performance
reason. If you prefer unpremultiplied pixels you have to unpremultiply
the pixels generated by skia manually.
> --
> You received this message because you are subscribed to the Google Groups
> "skia-discuss" group.
> To post to this group, send email to skia-d...@googlegroups.com.
> To unsubscribe from this group, send email to
> skia-discuss...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/skia-discuss?hl=en.

Tom Hudson

unread,
May 25, 2012, 7:57:06 AM5/25/12
to skia-d...@googlegroups.com
On Fri, May 25, 2012 at 5:21 AM, Marshall Greenblatt
<magree...@gmail.com> wrote:
> Hi All,
>
> I'm using skia to render a block with alpha transparency on top of a
> completely transparent background. I would like the result to keep the
> source RGB values and only change the alpha value. Instead, the whole ARGB
> value is blended. This appears to be done in the S32A_Blend_BlitRow32_SSE2
> function:
>
> alpha     = 0xbf
> src_pixel = 0xffffffffffffffffffffffffffffffff
> dst_pixel = 0000000000000000000000000000000000
> result    = 0xbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf
>
> Instead, I'm seeking the following result:
>
> alpha     = 0xbf
> src_pixel = 0xffffffffffffffffffffffffffffffff
> dst_pixel = 0000000000000000000000000000000000
> result    = 0xbfffffffbfffffffbfffffffbfffffff
>
> Does skia support this type of blending in cases where the backround device
> is completely transparent? If not, would it be reasonable to add support for
> it?


As Jian Liang said, Skia colors are always stored premultiplied - RGB values are *already* multiplied by alpha. So your RGB components must be <= than A, and 0xbffffff would cause asserts throughout Skia to fail.

Tom

Marshall Greenblatt

unread,
May 25, 2012, 10:29:25 AM5/25/12
to skia-d...@googlegroups.com

Thanks, accounting for the premultiplied alpha fixed the problem.
 

Tom

itai.b...@overwolf.com

unread,
Mar 10, 2014, 4:27:57 AM3/10/14
to skia-d...@googlegroups.com
If I understand correctly, the suggestion is to re-multiply the RGB values by 1/alpha to gain the original RGB values.
Did I understand correctly? If I did then the problem with this solution is that it changes the RGB values from the original due to rounding. The problem exaggerates with very small alpha values.

If I did not, then is there another way to un-premultiply the values?
A flag would work best for me, but then - how do I set it from CEF?

Itai Bar-Haim,
Overwolf.com

Tom Hudson

unread,
Mar 10, 2014, 6:58:38 AM3/10/14
to skia-d...@googlegroups.com
On Mon, Mar 10, 2014 at 8:27 AM, <itai.b...@overwolf.com> wrote:
If I understand correctly, the suggestion is to re-multiply the RGB values by 1/alpha to gain the original RGB values.
Did I understand correctly? If I did then the problem with this solution is that it changes the RGB values from the original due to rounding. The problem exaggerates with very small alpha values.

If I did not, then is there another way to un-premultiply the values?
A flag would work best for me, but then - how do I set it from CEF?

Premultiplied alpha loses information. There's no way to exactly reconstruct the original values.

That said, if you look at the commit log for Skia, there's ongoing work to support unpremultiplied images in some circumstances. For example, https://code.google.com/p/chromium/codesearch#chromium/src/third_party/skia/include/core/SkImageInfo.h&q=SkAlphaType&sq=package:chromium&type=cs&l=20 shows there's support for image formats that are unpremultiplied. Last October it was extended into SkBitmap (https://codereview.chromium.org/25275004).

Tom

Reply all
Reply to author
Forward
0 new messages