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

Strange white overlay, when resizing some images with alpha channel #2422

Open
qarmin opened this issue Feb 17, 2025 · 4 comments
Open

Strange white overlay, when resizing some images with alpha channel #2422

qarmin opened this issue Feb 17, 2025 · 4 comments

Comments

@qarmin
Copy link

qarmin commented Feb 17, 2025

When testing differences of resizing in image-rs and fast_image_resize, I found that with some images wtih alpha channel, image-rs output is looking strange with additional whitish border

Left - original
Middle - fast_image_resize (5.1.2)
Right - image-rs (0.25.5)

Image
Image

Files - F.zip

I used this test project to test differences

https://github.com/qarmin/img_hash/blob/master/tools/test_resize_diff/src/main.rs

@awxkee
Copy link
Contributor

awxkee commented Feb 17, 2025

You have to associate (premultiply) alpha before resampling and unassociate alpha after the operation if needed, this is required for correct blending.
Alpha association/unassociation currently is not supported in resizing.
To avoid artifacts in high-frequency regions of the image do not associate alpha if this is not necessary, or fill background with constant color ( and this one have some drawbacks ).

@awxkee
Copy link
Contributor

awxkee commented Feb 17, 2025

Also, I'd like to note that because hashing is involved, this implementation is not deterministic, just like in fast-image-resize.
Even fast-image-resize uses the fixed point only for convolution of common u8, u16 images, it does use non deterministic methods to compute filter weights.
The results are implementation-defined, so you can't reliably use them for hashing.
To get deterministic results across all platforms, you need to use deterministic methods only. For example, using only power-of-2 scaling allows deterministic halving or multiplying by a power of 2.
Neither fast-image-resize nor the image crate (or others) do so.

@kornelski
Copy link
Contributor

At very least the resize docs should warn about scaling alpha without premultiplication.

It's unfortunate that Pixel/GenericImage can't define whether the alpha is already premultiplied or not. It would be nice if resize could handle this properly by default.

@awxkee
Copy link
Contributor

awxkee commented Feb 17, 2025

We implemented with @Shnatsel more or less reasonable solution for alpha premultiplication in #2223.

However, I don't think there is any agreement on its integration into the existing pipelines.

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