Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with transparency #23

Open
jakearchibald opened this issue Mar 6, 2019 · 7 comments
Open

Issues with transparency #23

jakearchibald opened this issue Mar 6, 2019 · 7 comments

Comments

@jakearchibald
Copy link

Here's the gmail logo:

logo_gmail_lockup_default_2x

And here's the logo reduced a little using triangle:

logo_gmail_lockup_default_2x

Note the border around the mail icon. I'm guessing it's sampling colours in the transparent area of the image. This seems to happen with all the filters.

@Kagami
Copy link
Member

Kagami commented Mar 6, 2019

Thanks, I will look into it.

@kornelski
Copy link
Collaborator

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.

@jakearchibald
Copy link
Author

Do other resizing tools do this automatically? Do they use bilinear for the alpha only, or drop down to bilinear for all channels?

@jakearchibald
Copy link
Author

fwiw, ImageMagick seems to handle it automatically, but I haven't looked into what it does.

@kornelski
Copy link
Collaborator

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.

@Kagami
Copy link
Member

Kagami commented Mar 7, 2019

@kornelski should we provide method to do RGBA premultiplication in public library API?

@kornelski
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants