Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-10-27 19:46:03 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-10-27 19:47:30 +0300
commitcf82096887d8fc5ed9a1efd85d8ca1522857f5ff (patch)
treeff2dd6abd591d5533eaf2af4e1c75a2d05c4faf3
parent7eef9314dd24ba73bb40e058b50d4f4e7bce1035 (diff)
Minor optimization.
-rw-r--r--NesTiler/ColorExtensions.cs5
-rw-r--r--NesTiler/FastBitmap.cs31
2 files changed, 21 insertions, 15 deletions
diff --git a/NesTiler/ColorExtensions.cs b/NesTiler/ColorExtensions.cs
index 70f8bc0..239497d 100644
--- a/NesTiler/ColorExtensions.cs
+++ b/NesTiler/ColorExtensions.cs
@@ -14,7 +14,8 @@ namespace com.clusterrr.Famicom.NesTiler
static class ColorExtensions
{
- static Dictionary<ColorPair, double> cache = new();
+ private static Dictionary<ColorPair, double> cache = new();
+ private static CieDe2000Comparison comparer = new();
public static void ClearCache() => cache.Clear();
@@ -38,7 +39,7 @@ namespace com.clusterrr.Famicom.NesTiler
return cache[pair];
var a = new Rgb { R = color1.Red, G = color1.Green, B = color1.Blue };
var b = new Rgb { R = color2.Red, G = color2.Green, B = color2.Blue };
- var delta = a.Compare(b, new CieDe2000Comparison());
+ var delta = a.Compare(b, comparer);
cache[pair] = delta;
return delta;
}
diff --git a/NesTiler/FastBitmap.cs b/NesTiler/FastBitmap.cs
index 1015354..5831170 100644
--- a/NesTiler/FastBitmap.cs
+++ b/NesTiler/FastBitmap.cs
@@ -11,27 +11,32 @@ namespace com.clusterrr.Famicom.NesTiler
public int Width { get; }
public int Height { get; }
- private readonly SKColor[] colors;
- private static Dictionary<string, SKBitmap> imagesCache = new Dictionary<string, SKBitmap>();
+ private readonly int verticalOffset;
+ private readonly SKColor[] pixels;
+ private static Dictionary<string, (SKColor[] pixels, int w, int h)> imagesCache = new();
- private FastBitmap(SKBitmap skBitmap, int verticalOffset = 0, int height = -1)
+ private FastBitmap(SKColor[] pixels, int originalWidth, int originalHeight, int verticalOffset = 0, int height = -1)
{
- Width = skBitmap.Width;
- Height = height <= 0 ? skBitmap.Height - verticalOffset : 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).ToArray();
+ Width = originalWidth;
+ Height = height <= 0 ? originalHeight - verticalOffset : height;
+ this.verticalOffset = verticalOffset;
+ this.pixels = pixels;
}
public static FastBitmap? Decode(string filename, int verticalOffset = 0, int height = -1)
{
+ if (imagesCache.TryGetValue(filename, out (SKColor[] pixels, int w, int h) cachedImage))
+ {
+ return new FastBitmap(cachedImage.pixels, cachedImage.w, cachedImage.h, verticalOffset, height);
+ }
try
{
using (var image = SKBitmap.Decode(filename))
{
if (image == null) return null;
- imagesCache[filename] = image;
- return new FastBitmap(image, verticalOffset, height);
+ var pixels = image.Pixels;
+ imagesCache[filename] = (pixels, image.Width, image.Height);
+ return new FastBitmap(pixels, image.Width, image.Height, verticalOffset, height);
}
}
finally
@@ -42,12 +47,12 @@ namespace com.clusterrr.Famicom.NesTiler
public SKColor GetPixelColor(int x, int y)
{
- return colors[(y * Width) + x];
+ return pixels[((y + verticalOffset) * Width) + x];
}
public void SetPixelColor(int x, int y, SKColor color)
{
- colors[(y * Width) + x] = color;
+ pixels[((y + verticalOffset) * Width) + x] = color;
}
public byte[] Encode(SKEncodedImageFormat format, int v)
@@ -57,7 +62,7 @@ namespace com.clusterrr.Famicom.NesTiler
{
for (int x = 0; x < Width; x++)
{
- var color = colors[(y * Width) + x];
+ var color = GetPixelColor(x, y);
skImage.SetPixel(x, y, color);
}
}