Browse Source

Refactoring PixiFilePreviewImage.cs

CPKreuz 1 year ago
parent
commit
d7e6a2829d
1 changed files with 66 additions and 44 deletions
  1. 66 44
      src/PixiEditor/Views/Visuals/PixiFilePreviewImage.cs

+ 66 - 44
src/PixiEditor/Views/Visuals/PixiFilePreviewImage.cs

@@ -29,7 +29,7 @@ internal class PixiFilePreviewImage : SurfaceControl
         get => GetValue(FilePathProperty);
         set => SetValue(FilePathProperty, value);
     }
-    
+
     public VecI ImageSize
     {
         get => GetValue(ImageSizeProperty);
@@ -57,7 +57,7 @@ internal class PixiFilePreviewImage : SurfaceControl
     private void LoadImage(string path)
     {
         var surface = LoadPreviewSurface(path);
-        
+
         Dispatcher.UIThread.Post(() => SetImage(surface));
     }
 
@@ -78,77 +78,99 @@ internal class PixiFilePreviewImage : SurfaceControl
             previewImage.Surface = null;
             return;
         }
-        
+
         previewImage.RunLoadImage();
     }
-    
+
     private Surface? LoadPreviewSurface(string filePath)
     {
         if (!File.Exists(filePath))
         {
             return null;
         }
-        
+
         var fileExtension = Path.GetExtension(filePath);
 
         if (fileExtension == ".pixi")
         {
-            try
-            {
-                var result = Importer.GetPreviewSurface(filePath);
-
-                if (result == null)
-                {
-                    SetCorrupt();
-                }
-            }
-            catch
-            {
-                SetCorrupt();
-                return null;
-            }
+            return LoadPixiPreview(filePath);
         }
 
         if (SupportedFilesHelper.IsExtensionSupported(fileExtension))
         {
-            Surface bitmap = null;
+            return LoadNonPixiPreview(filePath);
+        }
 
-            try
-            {
-                bitmap = Surface.Load(filePath);
-            }
-            catch (RecoverableException)
+        return null;
+
+    }
+
+    private Surface LoadPixiPreview(string filePath)
+    {
+        try
+        {
+            var loaded = Importer.GetPreviewSurface(filePath);
+
+            if (loaded.Size is { X: <= Constants.MaxPreviewWidth, Y: <= Constants.MaxPreviewHeight })
             {
-                SetCorrupt();
+                return loaded;
             }
 
-            if (bitmap == null) //prevent crash
-                return null;
+            var downscaled = DownscaleSurface(loaded);
+            loaded.Dispose();
+            return downscaled;
+        }
+        catch
+        {
+            SetCorrupt();
+            return null;
+        }
+    }
+
+    private Surface LoadNonPixiPreview(string filePath)
+    {
+        Surface loaded = null;
 
-            return DownscaleToMaxSize(bitmap);
+        try
+        {
+            loaded = Surface.Load(filePath);
+        }
+        catch (RecoverableException)
+        {
+            SetCorrupt();
         }
 
-        return null;
+        if (loaded == null) //prevent crash
+            return null;
 
-        // TODO: This does not actually set the dot to gray
-        void SetCorrupt()
+        if (loaded.Size is { X: <= Constants.MaxPreviewWidth, Y: <= Constants.MaxPreviewHeight })
         {
-            Dispatcher.UIThread.Post(() => Corrupt = true);
+            return loaded;
         }
+
+        var downscaled = DownscaleSurface(loaded);
+        loaded.Dispose();
+        return downscaled;
+
     }
 
-    private static Surface DownscaleToMaxSize(Surface bitmap)
+    private static Surface DownscaleSurface(Surface surface)
     {
-        if (bitmap.Size.X > Constants.MaxPreviewWidth || bitmap.Size.Y > Constants.MaxPreviewHeight)
-        {
-            double factor = Math.Min(Constants.MaxPreviewWidth / (double)bitmap.Size.X, Constants.MaxPreviewHeight / (double)bitmap.Size.Y);
-            var scaledBitmap = bitmap.Resize(new VecI((int)(bitmap.Size.X * factor), (int)(bitmap.Size.Y * factor)), ResizeMethod.HighQuality);
-            
-            bitmap.Dispose();
-            return scaledBitmap;
-        }
-    
-        return bitmap;
+        double factor = Math.Min(
+            Constants.MaxPreviewWidth / (double)surface.Size.X,
+            Constants.MaxPreviewHeight / (double)surface.Size.Y);
+
+        var newSize = new VecI((int)(surface.Size.X * factor), (int)(surface.Size.Y * factor));
+        
+        var scaledBitmap = surface.Resize(newSize, ResizeMethod.HighQuality);
+
+        surface.Dispose();
+        return scaledBitmap;
     }
     
+    // TODO: This does not actually set the dot to gray
+    void SetCorrupt()
+    {
+        Dispatcher.UIThread.Post(() => Corrupt = true);
+    }
 }