Live data from Hacker News

Beware of Transparent Pixels

adriancourreges.com

41–50 of 97 posts

Re: Beware of Transparent Pixels

#41

Using premultiplied alpha avoids this. Jim Blinn's books from the 90s give a very thoughtful treatment of the topic.

The Porter and Duff compositing paper is good, too. One clarification, though: With premultiplied colors, something like (1,1,1,0) is either illegal or a light source. It's not a valid normal color.

But then you can do additive blending without changing blend modes - or even some additive and some standard transparency modes in the same sprite!

Re: Beware of Transparent Pixels

#42
Really nice article! Succinctly demonstrates the problem with not using premultiplied alpha.

> As an Artist: Make it Bleed!

> If you’re in charge of producing the asset, be defensive and don’t trust the programmers or the engine down the line.

If you are an artist working with programmers that can fix the engine, your absolute first choice should be to ask them to fix the blending so they convert your non-premultiplied images into premultiplied images before rendering them!

Do not start bleeding your mattes manually if you have any say in the matter at all, that doesn't solve the whole problem, and it sets you up for future pain. The only right answer is for the programmers to use premultiplied images. What if someone decides to blur your bled transparent image? It will break. (And there are multiple valid reasons this might happen without your input.)

Even if you have no control over the engine, file a bug report. But in that case, go ahead and bleed your transparent images manually & do whatever you have to, to get your work done.

Eric Haines wrote a more technical piece on this problem that elaborates on the other issues besides halo-ing:

http://www.realtimerendering.com/blog/gpus-prefer-premultipl...

Re: Beware of Transparent Pixels

#43
post #8

Reminds me of "Is there a reason Hillary Clinton's logo has hidden notches?" https://graphicdesign.stackexchange.com/questions/73601/is-t...

... which reminds me of similar notches from typography at small sizes. Interesting how physical bleeding ink and rendering issues result in similar workarounds.

Re: Beware of Transparent Pixels

#44
post #40
post #31

Earlier quoted context omitted.

But it's specifically a problem with PNG because the spec explicitly allows encoders to substitute their own RGB values for any transparent pixel. Most other formats are expected to store the values you give them without modification.

No, the problem discussed in the article can occur with any image format (that supports alpha) and occurs later in the pipeline so it really doesn't matter how the texture was stored on disc. Or even whether it was stored - this could happen with procedurally generated textures.

The article discussed one possible workaround, which was to specify RGB values for the fully transparent pixels in the artwork. If the PNG tool you're using substitutes its own RGB values in those transparent pixels, as allowed by the spec, the workaround doesn't work. That's why PNG in particular is a problem.

If you follow the recommendation at the end to use premultiplied alpha for all computations, this becomes a moot point.

Re: Beware of Transparent Pixels

#45
post #8

Reminds me of "Is there a reason Hillary Clinton's logo has hidden notches?" https://graphicdesign.stackexchange.com/questions/73601/is-t...

... which reminds me of similar notches from typography at small sizes. Interesting how physical bleeding ink and rendering issues result in similar workarounds.

For the curious: https://en.wikipedia.org/wiki/Ink_trap

Re: Beware of Transparent Pixels

#46

Premultiplied alpha results in less color depth, though. If my alpha is 10%, then my possible RGB values become 0-25. Even if I multiply by 10, I still lose the maximum possible values 251-255, and only values 0, 10, 20, 30... 250, are possible. The correct solution is to pay close attention to all of the factors... and to be ESPECIALLY aware of pixel scaling. Provide your RGBA textures at the 1:1 pixel scale they wi…

If you were just doing image compositing would this be an issue? I can easily imagine how that would adversely effect a 3d game where the images are used as texture maps and the values of the diffuse texture map may change from other lighting contributions and shaders. It seems like in that case doing some kind of HDR/higher precision texturing would be good right? Disclaimer: not a graphics programmer just a hobbyis…

In practice it's the other way around. Pre-multiplying alpha adversely affects image editing more than 3d rendering.

For image editing, the problem is that you might send the same pixel through many different operations, and if you flatten something and further manipulate, then the rounding error will continue to accumulate. So losing granularity early in the process due to alpha can result in accumulated rounding error.

In 3d graphics, the texture typically goes through a very predictable and short number of transformations, and they will rarely need to "stretch" the color range of the transparent pixel. When you alpha blend with pre-multiplied alpha, you're literally just adding the whole color value. And then you're slapping a whole lot of other colors onto it in the lighting/shadow/other passes, so the subtle nuances of that mostly transparent window get lost in the bustle.

Re: Beware of Transparent Pixels

#47

Earlier quoted context omitted.

Surely the answer if you want this is to weight the final RGB by the transparency. E.g. the final red channel would be (R1xT1 + R2xT2)/(T1+T2)

You're mostly right. The industry-wide accepted answer is to multiply the opacity into the colors before interpolating, so the formula would be (R1xT1 + R2xT2)/2 for the average, and then to do later transparency blending as if the opacity term was already multiplied in.

Which means that your output pixel cant be both white and low transparency. I guess its a typical graphics 'close enough and better performance' outcome (where mine is marginally more difficult to calculate and needs some more logic to avoid divide by 0)

Re: Beware of Transparent Pixels

#48
post #22

This is extremely useful to take advantage of (that you can store RGB values in 0-alpha pixels). I've written some pretty simple but powerful shaders for a game I'm working on by utilizing transparent pixels' "extra storage" which allowed for either neat visuals or greatly reduced the number of images required to achieve a certain affect. For instance, I wrote a shader for a characters hair that had source images col…

I'm a bit confused about your explanation. Are you using the transparent pixels to store extra data, or are you using the alpha channel to store extra data? And if you're using the transparent pixels, then what data is it and how do your shaders know how to find the transparent pixels?

Re: Beware of Transparent Pixels

#49
While reading this article, it struck me that the amount of "useless" data increases as the alpha value approaches 0. For example: in a pixel with rgba values of (1.0, 0.4, 0.5, 0.0), the rgb values are redundant. Is there a color format that would prevent this redundancy? Perhaps by some clever equation that incorporates the alpha values into the rgb values? I don't think Premultiplied alpha would work, because you still need to store the alpha value for compositing later...

Re: Beware of Transparent Pixels

#50
post #42

Really nice article! Succinctly demonstrates the problem with not using premultiplied alpha. > As an Artist: Make it Bleed! > If you’re in charge of producing the asset, be defensive and don’t trust the programmers or the engine down the line. If you are an artist working with programmers that can fix the engine, your absolute first choice should be to ask them to fix the blending so they convert your non-premultipli…

I'm not sure I understand your concern. If the software converts all your assets into a premultiplied form, the bleeding you applied won't hurt anything even if it doesn't help. Yes, it's extra work that shouldn't be necessary - but we often find ourselves living in an imperfect world.

I completely agree that premultiplied alpha should be used everywhere. I'd even go a step farther and say that you should use high bit depth linear values too, but that's a topic for another day.

Post reply on HN