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

ResampleOp.resample() doesn't clamp premultiplied alphas #537

Open
sachindshinde opened this issue Apr 23, 2020 · 1 comment
Open

ResampleOp.resample() doesn't clamp premultiplied alphas #537

sachindshinde opened this issue Apr 23, 2020 · 1 comment

Comments

@sachindshinde
Copy link

The code for ResampleOp.resample(), when working with source images that have premultiplied alphas, doesn't ensure destination color samples are clamped to the alpha value. This can result in other ImageIO code somewhat mysteriously failing (in my case, I had ComponentColorModel.extractComponent() returning a value beyond its expected 8-bit range).

Regarding alpha handling, it seems like the ideal thing to do (based on this post in ImageMagick forums, and this part of ImageMagick's resize.c) would be something like:

  • If there's a source alpha channel and it's not premultiplied, when multiplying source sample values by the weight, also multiply by the alpha for the source sample and divide by the source alpha's channel max. We'd additionally need to keep track of the sum of (weight * alpha / max_alpha) for the destination sample, if the destination isn't premultiplied.
  • If the destination is premultiplied, then clamp the destination sample value between 0 and the min of the destination sample's channel max and the alpha for the destination sample.
  • If the destination isn't premultiplied, divide the destination sample value by the sum of (weight * alpha / alpha_max ) for the destination sample, and clamp as normal between 0 and the destination sample's channel max.
@haraldk
Copy link
Owner

haraldk commented Apr 24, 2020

Hi Sachin,

Thanks for looking into this!

If you are able to convert the above into code in a PR, I'd be happy to merge it! If not, I might still look into it some day. It's a bit down on my priority list right now though, as I hardly ever work with premultiplied images.

Best regards,

--
Harald K

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

No branches or pull requests

2 participants