Browse Source

Fix crash when loading a corrupted image

Equbuxu 1 year ago
parent
commit
89a3b2a6eb

+ 2 - 0
src/ChunkyImageLib/Surface.cs

@@ -64,6 +64,8 @@ public class Surface : IDisposable
     public static Surface Load(byte[] encoded)
     {
         using var image = Image.FromEncodedData(encoded);
+        if (image is null)
+            throw new ArgumentException($"The passed byte array does not contain a valid image");
 
         var surface = new Surface(new VecI(image.Width, image.Height));
         surface.DrawingSurface.Canvas.DrawImage(image, 0, 0);

+ 2 - 2
src/PixiEditor.DrawingApi.Core/Bridge/Operations/IImageImplementation.cs

@@ -8,8 +8,8 @@ namespace PixiEditor.DrawingApi.Core.Bridge.Operations
     {
         public Image Snapshot(DrawingSurface drawingSurface);
         public void DisposeImage(Image image);
-        public Image FromEncodedData(string path);
-        public Image FromEncodedData(byte[] dataBytes);
+        public Image? FromEncodedData(string path);
+        public Image? FromEncodedData(byte[] dataBytes);
         public void GetColorShifts(ref int platformColorAlphaShift, ref int platformColorRedShift, ref int platformColorGreenShift, ref int platformColorBlueShift);
         public ImgData Encode(Image image);
         public int GetWidth(IntPtr objectPointer);

+ 2 - 2
src/PixiEditor.DrawingApi.Core/Surface/ImageData/Image.cs

@@ -26,12 +26,12 @@ namespace PixiEditor.DrawingApi.Core.Surface.ImageData
             DrawingBackendApi.Current.ImageImplementation.DisposeImage(this);
         }
 
-        public static Image FromEncodedData(string path)
+        public static Image? FromEncodedData(string path)
         {
             return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(path);
         }
         
-        public static Image FromEncodedData(byte[] dataBytes)
+        public static Image? FromEncodedData(byte[] dataBytes)
         {
             return DrawingBackendApi.Current.ImageImplementation.FromEncodedData(dataBytes);
         }

+ 6 - 2
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaImageImplementation.cs

@@ -31,9 +31,11 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             return new Image(snapshot.Handle);
         }
         
-        public Image FromEncodedData(byte[] dataBytes)
+        public Image? FromEncodedData(byte[] dataBytes)
         {
             SKImage img = SKImage.FromEncodedData(dataBytes);
+            if (img is null)
+                return null;
             ManagedInstances[img.Handle] = img;
             
             return new Image(img.Handle);
@@ -45,9 +47,11 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
             ManagedInstances.TryRemove(image.ObjectPointer, out _);
         }
 
-        public Image FromEncodedData(string path)
+        public Image? FromEncodedData(string path)
         {
             var nativeImg = SKImage.FromEncodedData(path);
+            if (nativeImg is null)
+                return null;
             ManagedInstances[nativeImg.Handle] = nativeImg;
             return new Image(nativeImg.Handle);
         }

+ 13 - 2
src/PixiEditor/Models/IO/Importer.cs

@@ -30,8 +30,19 @@ internal class Importer : NotifyableObject
     /// <returns>WriteableBitmap of imported image.</returns>
     public static Surface? ImportImage(string path, VecI size)
     {
-        if (!Path.Exists(path)) return null;
-        Surface original = Surface.Load(path);
+        if (!Path.Exists(path))
+            throw new MissingFileException();
+            
+        Surface original;
+        try
+        {
+            original = Surface.Load(path);
+        }
+        catch (Exception e) when (e is ArgumentException or FileNotFoundException)
+        {
+            throw new CorruptedFileException(e);
+        }
+            
         if (original.Size == size || size == VecI.NegativeOne)
         {
             return original;