Skip to content

Use BitmapCreateOptions.IgnoreColorProfile to workaround WPF issue when using DecodePixelWidth#3677

Merged
dominoFire merged 2 commits intoNuGet:devfrom
campersau:IgnoreColorProfile
Oct 1, 2020
Merged

Use BitmapCreateOptions.IgnoreColorProfile to workaround WPF issue when using DecodePixelWidth#3677
dominoFire merged 2 commits intoNuGet:devfrom
campersau:IgnoreColorProfile

Conversation

@campersau
Copy link
Contributor

@campersau campersau commented Sep 19, 2020

Bug

Fixes: NuGet/Home#10037
Regression: No

  • Last working version: Don't know
  • How are we preventing it in future: Tests

Fix

Details can be found here dotnet/wpf#3503 as well as the workaround which is implemented by this PR.

See also NuGetPackageExplorer/NuGetPackageExplorer#1105 for more analysis.

Testing/Validation

Tests Added: Yes
Reason for not adding tests:
Validation:

image

@campersau campersau requested a review from a team as a code owner September 19, 2020 10:46
@nkolev92 nkolev92 added the Community PRs created by someone not in the NuGet team label Sep 29, 2020
@nkolev92
Copy link
Member

cc @zivkan

@zivkan
Copy link
Member

zivkan commented Sep 29, 2020

@campersau where was the image sourced from? I'm guessing it's not an image you created yourself just to test this fix. I want to make sure it has a license that's compatible.

Copy link
Member

@nkolev92 nkolev92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

Copy link
Member

@zivkan zivkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the image is 166 KB. I'm still curious about the license to make sure there's no issue us using it, even if it's just for a test. However, I also want to be more mindful about not bloating the repo by having needlessly large binary files. a 10x10 pixel image with whatever colour palette should be able to reproduce the issue, and be probably less than 1KB.

@campersau
Copy link
Contributor Author

@zivkan The image is from here https://cdn.statically.io/gh/Starz0r/ChocolateyPackagingScripts/2055976c/assets/legendary.png
which is the URL from the chocolatly nuget feed. The project is on github https://github.com/derrod/legendary

I have changed the original image by resizing it and setting all pixels to black. It is now 9KB mostly because of the color profiles (8KB) which are required for this test.

Is this okay?

Copy link
Contributor

@donnie-msft donnie-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not thrilled with the idea of having to potentially distort colors to avoid this issue, but reading the linked content, it seems the only way to work-around the problem.

I may revisit this when I work on Default Package Icons NuGet/Home#5937

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting. If the image has a bad color profile or any other bad image data, then the catch at line 170 should have caught the exception. Instead, the whole devenv.exe process crashes.

Copy link
Contributor Author

@campersau campersau Sep 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is because the image needs to be downloaded from the URI so the BitmapImage creates a BitmapDecoder which uses a LateBoundBitmapDecoder which uses the BitmapDownload helper class which uses a separate thread for when the download finishes. And that's where the exception is thrown and so it crashes the application.

Edit: The test works because it is using a file path and so throws with a different stack trace:

  Message: 
    System.IO.FileFormatException : Der Bitmap-Farbkontext ist ungültig.
    ---- System.Runtime.InteropServices.COMException : Das angegebene Farbprofil ist ungültig. (Ausnahme von HRESULT: 0x800707DB)
  Stack Trace: 
    ColorConvertedBitmap.FinalizeCreation()
    ColorConvertedBitmap.ctor(BitmapSource source, ColorContext sourceColorContext, ColorContext destinationColorContext, PixelFormat format)
    BitmapImage.FinalizeCreation()
    BitmapSource.CompleteDelayedCreation()
    BitmapSource.get_PixelWidth()

otherwise we would have to wait until the image is fully downloaded.

Copy link
Contributor

@dominoFire dominoFire Sep 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, IconUrlToImageCacheConverter accepts either embedded icon URIs (an absolute URI to a package filepath with a fragment part, pointing to the relative icon path inside the package) or absolute URIs. Since we are just testing that image is read, it's fine to leave the relative URI in tests.

@rrelyea

to workaround WPF issue when using DecodePixelWidth
Copy link
Contributor

@dominoFire dominoFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution, @campersau !

I verified the fix works as expected

Also, I verified that both images (test and legendary.png) has some unknown ICC metadata that tests this fix. Validation is done via wicexplorer. The 'Unknown reader' elements in the tree view represent the unknown metadata contained in the images.

image

Copy link
Member

@zivkan zivkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dominoFire dominoFire merged commit 769c078 into NuGet:dev Oct 1, 2020
@campersau campersau deleted the IgnoreColorProfile branch October 1, 2020 18:46
zivkan pushed a commit that referenced this pull request Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Icons with certain color pallets causes PM UI to crash VS

5 participants