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:
Diffstat (limited to 'NesTiler/FastBitmap.cs')
-rw-r--r--NesTiler/FastBitmap.cs31
1 files changed, 18 insertions, 13 deletions
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);
}
}