resize icon indicating copy to clipboard operation
resize copied to clipboard

Issues with transparency

Open jakearchibald opened this issue 5 years ago • 7 comments

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.

jakearchibald avatar Mar 06 '19 18:03 jakearchibald

Thanks, I will look into it.

Kagami avatar Mar 06 '19 22:03 Kagami

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.

kornelski avatar Mar 06 '19 22:03 kornelski

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

jakearchibald avatar Mar 07 '19 06:03 jakearchibald

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

jakearchibald avatar Mar 07 '19 08:03 jakearchibald

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 avatar Mar 07 '19 11:03 kornelski

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

Kagami avatar Mar 07 '19 11:03 Kagami

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.

kornelski avatar Mar 07 '19 16:03 kornelski