From dfd7e9a652514fe6abf5d84e2f1736036afdcc1c Mon Sep 17 00:00:00 2001 From: Alexey 'Cluster' Avdyukhin Date: Thu, 27 Oct 2022 17:59:30 +0400 Subject: Some fixes and optimization. --- NesTiler/FastBitmap.cs | 23 ++++++++++++++++------- NesTiler/Program.cs | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/NesTiler/FastBitmap.cs b/NesTiler/FastBitmap.cs index 5910f43..07b33e5 100644 --- a/NesTiler/FastBitmap.cs +++ b/NesTiler/FastBitmap.cs @@ -14,21 +14,30 @@ namespace com.clusterrr.Famicom.NesTiler private readonly Color[] colors; private static Dictionary imagesCache = new Dictionary(); - private FastBitmap(SKBitmap skBitmap, int verticalOffset = 0, int height = 0) + private FastBitmap(SKBitmap skBitmap, int verticalOffset = 0, int height = -1) { Width = skBitmap.Width; Height = height <= 0 ? skBitmap.Height - verticalOffset : height; - if (skBitmap.Height - verticalOffset - Height <= 0) throw new InvalidOperationException("Invalid image height."); + if (skBitmap.Height - verticalOffset - Height < 0 || Height <= 0) throw new InvalidOperationException("Invalid image height."); var pixels = skBitmap.Pixels; colors = skBitmap.Pixels.Skip(verticalOffset * Width).Take(Width * Height).Select(p => Color.FromArgb(p.Alpha, p.Red, p.Green, p.Blue)).ToArray(); } - public static FastBitmap? Decode(string filename, int verticalOffset = 0, int height = 0) + public static FastBitmap? Decode(string filename, int verticalOffset = 0, int height = -1) { - using var image = SKBitmap.Decode(filename); - if (image == null) return null; - imagesCache[filename] = image; - return new FastBitmap(image, verticalOffset, height); + try + { + using (var image = SKBitmap.Decode(filename)) + { + if (image == null) return null; + imagesCache[filename] = image; + return new FastBitmap(image, verticalOffset, height); + } + } + finally + { + GC.Collect(); + } } public Color GetPixelColor(int x, int y) diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs index 852cbc1..92c6d12 100644 --- a/NesTiler/Program.cs +++ b/NesTiler/Program.cs @@ -113,7 +113,7 @@ namespace com.clusterrr.Famicom.NesTiler var heightS = match.Groups["height"].Value; // Crop it if need int offset = 0; - int height = 0; + int height = -1; if (!string.IsNullOrEmpty(offsetS)) { offset = int.Parse(offsetS); -- cgit v1.2.3