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

[ImageResizer] Fix blurry images in some cases #32797

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kissaki
Copy link
Contributor

@Kissaki Kissaki commented May 9, 2024

Summary of the Pull Request

In some cases a resized image becomes very blurry.

Loading the input image file into cache in BitmapDecoder resolves the blurriness.

PR Checklist

Detailed Description of the Pull Request / Additional comments

I tried to find the technical reason why caching resolves it, or why it is blurry in the first place, but was unable to locate it.

The issue seems to occur only on specific images. I was not able to identify a strong parameter that would cause it.

Nevertheless, with the images I will attach to the PR, I hope to make an evident case for the factual resolution of the issue. And maybe or hopefully someone with more knowledge about the respective used APIs will be able to provide the factual, technical reason for it.


ResizeOperation uses BitmapDecoder to load the image file. In main the decoder is instantiated with BitmapCacheOption.None. Changing it to BitmapCacheOption.OnLoad resolves the issue.

The pass-along of image data is as follows:

  1. create decoder
  2. decoder -> image file metadata
  3. decoder -> BitmapFrame -> TransformedBitmap -> BitmapFrame -> encoder (Frames.Add()) -> encoder.Save()

transformdoc mentions the following about loaded cached image data potentially mismatching target pixel size, which made me wonder whether the no-cache delayed reading in combination with the transformation leads to reduced-size-reading. I was not able to confirm it. Given that most images I tested did not have a problem, I don't think it's a plausible cause.

If you don't do this, the application will cache the image as though it were rendered as its normal size rather than just the size that is displayed.


For multiple test images, any differences were hard to spot, they did not result in blurriness. The image I had issues with in the past being very colorful made me wonder whether amount of color information could make the difference. But with the technical context, that would only make sense if the encoder caching somehow handled color information differently, which seems unlikely too.

Validation Steps Performed

Manual tests with multiple image files with variance. I will attach test cases in the following comment.

Fixes microsoft#10675

In some cases a resized image becomes very blurry.

Loading the input image file into cache in BitmapDecoder resolves the blurriness.

---

I tried to find the technical reason why caching resolves it, or why it is blurry in the first place, but was unable to locate it.

The issue seems to occur only on specific images. I was not able to identify a strong parameter that would cause it.

Nevertheless, with the images I will attach to the PR, I hope to make an evident case for the factual resolution of the issue. And maybe or hopefully someone with more knowledge about the respective used APIs will be able to provide the factual, technical reason for it.

---

`ResizeOperation` uses `BitmapDecoder` to load the image file. In main the decoder is instantiated with `BitmapCacheOption.None`. Changing it to `BitmapCacheOption.OnLoad` resolves the issue.

The pass-along of image data is as follows:

1. create decoder
2. decoder -> image file metadata
3. decoder -> `BitmapFrame` -> `TransformedBitmap` -> `BitmapFrame` -> encoder (`Frames.Add()`) -> `encoder.Save()`

---

[transformdoc] mentions the following about loaded cached image data potentially mismatching target pixel size, which made me wonder whether the no-cache delayed reading in combination with the transformation leads to reduced-size-reading. I was not able to confirm it. Given that most images I tested did not have a problem, I don't think it's a plausible cause.

> If you don't do this, the application will cache the image as though it were rendered as its normal size rather than just the size that is displayed.

[transformdoc]: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-apply-a-transform-to-a-bitmapimage

---

For multiple test images, any differences were hard to spot, they did not result in blurriness. The image I had issues with in the past being very colorful made me wonder whether amount of color information could make the difference. But with the technical context, that would only make sense if the encoder caching somehow handled color information differently, which seems unlikely too.
@Kissaki
Copy link
Contributor Author

Kissaki commented May 9, 2024

Given that I don't have a full technical explanation why this works, I'm creating this PR as a draft. I'm looking for feedback whether this would be fine, or for help in explaining why it resolves it.

@Kissaki
Copy link
Contributor Author

Kissaki commented May 9, 2024

test case 1: blurry result original:

3_orig

scaled to 480p, and face 2x zoomed to more obviously show the difference:

main cached
3_orig_480p-main 3_orig_480p-cached
3_orig_480p-main-face2x 3_orig_480p-cached-face2x
test case 2: subtitles with text online slightly different original:

2_orig

main cached
2_orig_480p-main 2_orig_480p-cached
no obvious/barely visual differences

1_orig

main cached
1_orig_480p-main 1_orig_480p-cached

4_ref

main cached
4_ref_480p-main 4_ref_480p-cached

slight differences in:

5_ref

main cached
5_ref_480p-main 5_ref_480p-cached

@jaimecbernardo
Copy link
Collaborator

Hi @Kissaki ,
Thanks for opening this PR!
Can you please provide a sample image that's affected by this change? Inside a zip file might be better, since GitHub might manipulate the image to embed in the comment.
Thanks, in advance.

@Kissaki
Copy link
Contributor Author

Kissaki commented May 29, 2024

@jaimecbernardo
Copy link
Collaborator

@Kissaki
Thank you for opening this PR!
I had the chance to give it a try. On images with text I found it that it looked like it made the text harder to read, so this might be something not everyone would want.
I think this might be a good Setting with a Toggle to have on the Settings page of Image Resizer, under encoding. Something like "Use bitmap cache when decoding the original image", with the description text that it might help avoid blurriness in some edge cases.
What do you think?

@Kissaki
Copy link
Contributor Author

Kissaki commented Jun 4, 2024

That seems like a reasonable workaround/implementation.

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

Successfully merging this pull request may close these issues.

2 participants