ImageSharp icon indicating copy to clipboard operation
ImageSharp copied to clipboard

Image scaling and conversion is slow in iOS

Open JPfahl opened this issue 2 years ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have verified that I am running the latest version of ImageSharp
  • [X] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [X] I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

3.0.1

Other ImageSharp packages and versions

None

Environment (Operating system, version and so on)

iOS 16.5.1

.NET Framework version

7

Description

I am using the Maui MediaPicker to take pictures on an iPhone. I am trying to use ImageSharp to fix the problems with MediaPicker, specifically that it returns the image as a png and does not honor phone orientation. I receive an ~18mb png from the media picker. Sizing it to a width of 1920 and saving as a jpg ~350kb with the right orientation takes ~17 seconds. Is there anyway to improve the speed. You product does exactly what I need but the performance makes it unusable in this case.

Steps to Reproduce

        Stream stream = await fileResult.OpenReadAsync();

        if (EnableImageSharp)
        {
            if (fileResult.ContentType == "png")
            {
                DateTime start = DateTime.Now;
                bool isPortrait = await MainThread.InvokeOnMainThreadAsync(() => 
                    DeviceDisplay.Current.MainDisplayInfo.Rotation ==
                    DisplayRotation.Rotation0 ||
                    DeviceDisplay.Current.MainDisplayInfo.Rotation ==
                    DisplayRotation.Rotation180);
                Sys.Log(isPortrait);
                SixLabors.ImageSharp.Image image =
                    await SixLabors.ImageSharp.Image.LoadAsync(new DecoderOptions()
                        {
                            TargetSize = new SixLabors.ImageSharp.Size(1920, 0)
                        },
                        stream);

                Sys.Log(stream.Length);
                stream.Dispose();
                Sys.Log(DateTime.Now - start);
                Sys.Log(image.Width);
                Sys.Log(image.Height);

                if (isPortrait)
                {
                    ushort portrait = 6;

                    image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, portrait);
                    Sys.Log("Portrait");
                }

                stream = new MemoryStream();

                await image.SaveAsJpegAsync(stream);

                Sys.Log(DateTime.Now - start);
                Sys.Log(stream.Length);

                fileResult.FileName =
                    Path.ChangeExtension(fileResult.FileName, "jpeg");
            }
        }

Images

I can't provide an image because you don't accept anything over 10mb.

JPfahl avatar Jun 30 '23 16:06 JPfahl

Does anyone know what the intrinsics story is for Vector4 in iOS for .NET 7/8 + ?

We rely heavily on that type during resize.

JimBobSquarePants avatar Jul 05 '23 09:07 JimBobSquarePants

I think 17 seconds is way too much for this to be about Vector4 or intrinsics. Mono AOT is essentially broken for open generics code we use heavily, falling back to very slow interpreted (?) execution, see https://github.com/dotnet/runtime/issues/71210#issuecomment-1193075312.

@JPfahl a comment/upvote on that issue may help to push them in order to have this fixed eventually.

antonfirsov avatar Jul 05 '23 13:07 antonfirsov

Closing this. According to the performance testing in #2762 performance in iOS is now very good.

JimBobSquarePants avatar Jul 31 '24 13:07 JimBobSquarePants