SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.49k stars 850 forks source link

SaveAsWebpAsync throws IndexOutOfRangeException in UWP published app #2567

Closed brunomuratore closed 4 months ago

brunomuratore commented 1 year ago

Prerequisites

ImageSharp version

2.1.6

Other ImageSharp packages and versions

none

Environment (Operating system, version and so on)

Win 11 and Win 10 on any* version

.NET Framework version

UWP x64 app (Target: win11 22H2, Min: win10 1903), c# 9.0

Description

IndexOutOfRangeException is thrown when running SaveAsWebpAsync on a published UWP app, independently of which image is loaded into the stream, any image will fail, any format will fail. The same code runs fine when running the UWP app directly from Visual Studio, either Debug or Release.

 IndexOutOfRangeException: ImageCreation/Create > Failure while creating image
  -- IndexOutOfRangeException, "Index was outside the bounds of the array."
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException() + 0x25
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoding.ITransformOne(Span`1, Span`1, Span`1, Span`1) + 0x422
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoding.ITransformTwo(Span`1, Span`1, Span`1, Span`1) + 0x93
   at SixLabors.ImageSharp.Formats.Webp.Lossy.QuantEnc.ReconstructIntra16(Vp8EncIterator, Vp8SegmentInfo, Vp8ModeScore, Span`1, Int32) + 0x865
   at SixLabors.ImageSharp.Formats.Webp.Lossy.QuantEnc.PickBestIntra16(Vp8EncIterator, Vp8ModeScore&, Vp8SegmentInfo[], Vp8EncProba) + 0x828
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.Decimate(Vp8EncIterator, Vp8ModeScore&, Vp8RdLevel) + 0x65
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.OneStatPass(Int32, Int32, Int32, Int32, Vp8RdLevel, Int32, PassStats) + 0x560
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.StatLoop(Int32, Int32, Int32, Int32) + 0x145
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.Encode[TPixel](Image`1, Stream) + 0xa33
   at SixLabors.ImageSharp.Formats.Webp.WebpEncoderCore.Encode[TPixel](Image`1, Stream, CancellationToken) + 0x1b9
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.<EncodeAsync>g__DoEncodeAsync|0_0[TPixel](Stream, ImageEncoderUtilities.<>c__DisplayClass0_0`1&) + 0x67
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x21
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x7e
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x53
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.<EncodeAsync>d__0`1.MoveNext() + 0x37e
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x21
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x7e
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x53
   at App.Images.ImageCreation.<Create>d__1.MoveNext() + 0x185

Steps to Reproduce

var outputStream = new MemoryStream();
using (var image = await Image.LoadAsync(stream))
{
     image.Mutate(img => img.Resize(Size, Size));
     await image.SaveAsWebpAsync(outputStream); // Exception here
}

The code work fine when running the app from Visual Studio, either from Debug or Release modes.

But it will fail only if:

If I try to create a sample app to try demostrate the issue, it works, although the code is being tested is virtually the same.

Images

Any image, any format.

JimBobSquarePants commented 1 year ago

Hi @brunomuratore

Normally we wouldn't support issues raised against previous major versions (we're on v3 now) but I'm not sure what target frameworks UWP support.

The issue itself is not an ImageSharp one but rather appears to me to be a bug in the compiler.

@Sergio0694 Apologies for the tag but you know a lot more about UWP than me. What are your thoughts?

Sergio0694 commented 1 year ago

"I'm not sure what target frameworks UWP support"

UWP is picking up the .NET Standard 2.0 target (it uses the uap TFM, which is a weird ~.NET Core 2.1 TFM, but with all Span<T> APIs missing from its ref assembly... It's a whole thing 😅), long story short it only accepts either code targeting uap specifically, or otherwise any .NET Standard <= 2.0 TFM.

"The issue itself is not an ImageSharp one but rather appears to me to be a bug in the compiler."

Yeah this is a weird one, especially because it also repros with sideloaded Release packages (meaning ones you compiled locally), yet it doesn't with a normal Release build from VS. Impossible to say exactly what's causing that without just debugging the app. I do remember using ImageSharp on UWP in the past, and it worked (my app Legere is using 1.0.0-beta0010).

...Honestly I think your best bet for UWP right now is try downgrading the ImageSharp package step by step until you find a previous release that happens to work 🙃

JimBobSquarePants commented 1 year ago

Thanks @Sergio0694 appreciate it!!

@brunomuratore when you say the following

Any image, any format.

I'm assuming you mean input images. Does saving as other image formats (jpg, png, etc) work?

JimBobSquarePants commented 1 year ago

Shouldn't UWP users be migrating to WinUI 3?