Issues with transparency
jakearchibald opened this issue · 7 comments
Thanks, I will look into it.
This is the expected behaviour in RGBA with uncorrelated alpha channel.
The usual way to avoid it is to convert the image to premultiplied RGBA color space, i.e.
r = r * a / 255
Then resize, and to get the usual uncorrelated RGBA back, divide by alpha (with care about division by 0)
You will also have to use bilinear filter, because other filters sharpen the channels, and "sharpening" of alpha makes no sense.
Do other resizing tools do this automatically? Do they use bilinear for the alpha only, or drop down to bilinear for all channels?
fwiw, ImageMagick seems to handle it automatically, but I haven't looked into what it does.
In my tools I do it automatically :)
macOS goes as far as only supporting premultiplied RGBA color space.
You have to use bilinear for all channels. If you use other method, RGB and A will "go out of sync" at the edges and expose nonsense pixels.
@kornelski should we provide method to do RGBA premultiplication in public library API?
Conversion back and forth is expensive and lossy, so apps may want to structure it in a way that minimizes conversions throughout the entire app.
I think we should only document this pitfall, but leave conversion to other crates.