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-09-28 16:48:54 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-09-28 16:48:54 +0300
commit6d1ecd9887ec1d9ae6675c652455a233de2a2e6f (patch)
tree17a398225d5c59769f556446455e2ca437a9328e /NesTiler
parent0d50f7f46719ade1d1e06c0ab4f63ecf10fab224 (diff)
Optimizations
Diffstat (limited to 'NesTiler')
-rw-r--r--NesTiler/ColorExtensions.cs19
-rw-r--r--NesTiler/FastBitmap.cs2
-rw-r--r--NesTiler/Palette.cs18
3 files changed, 32 insertions, 7 deletions
diff --git a/NesTiler/ColorExtensions.cs b/NesTiler/ColorExtensions.cs
index ad6d682..2ddb434 100644
--- a/NesTiler/ColorExtensions.cs
+++ b/NesTiler/ColorExtensions.cs
@@ -2,6 +2,7 @@
using ColorMine.ColorSpaces.Comparisons;
using System;
using System.Collections.Generic;
+using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
@@ -9,13 +10,31 @@ using System.Threading.Tasks;
namespace com.clusterrr.Famicom.NesTiler
{
+ public record ColorPair
+ {
+ public Color Color1;
+ public Color Color2;
+ }
+
static class ColorExtensions
{
+ static Dictionary<ColorPair, double> cache = new();
+
+ public static void ClearCache() => cache.Clear();
+
public static double GetDelta(this Color color1, Color color2)
{
+ var pair = new ColorPair()
+ {
+ Color1 = color1,
+ Color2 = color2
+ };
+ if (cache.ContainsKey(pair))
+ return cache[pair];
var a = new Rgb { R = color1.R, G = color1.G, B = color1.B };
var b = new Rgb { R = color2.R, G = color2.G, B = color2.B };
var delta = a.Compare(b, new CieDe2000Comparison());
+ cache[pair] = delta;
return delta;
}
}
diff --git a/NesTiler/FastBitmap.cs b/NesTiler/FastBitmap.cs
index 962470d..1d7aac1 100644
--- a/NesTiler/FastBitmap.cs
+++ b/NesTiler/FastBitmap.cs
@@ -19,8 +19,6 @@ namespace com.clusterrr.Famicom.NesTiler
public FastBitmap(Bitmap bitmap)
{
- //if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
- // throw new FormatException($"Invalid pixel format, only {PixelFormat.Format32bppArgb} is supported");
this.bitmap = bitmap;
Lock();
}
diff --git a/NesTiler/Palette.cs b/NesTiler/Palette.cs
index 335eac5..6b29f02 100644
--- a/NesTiler/Palette.cs
+++ b/NesTiler/Palette.cs
@@ -9,6 +9,7 @@ namespace com.clusterrr.Famicom.NesTiler
class Palette : IEquatable<Palette>, IEnumerable<Color>
{
private Color?[] colors = new Color?[3];
+ private Dictionary<ColorPair, (Color color, double delta)> deltaCache = new();
public Color? this[int i]
{
@@ -53,10 +54,9 @@ namespace com.clusterrr.Famicom.NesTiler
public void Add(Color color)
{
- if (Count < 3)
- this[Count + 1] = color;
- else
- throw new IndexOutOfRangeException();
+ if (Count >= 3) throw new IndexOutOfRangeException();
+ this[Count + 1] = color;
+ deltaCache.Clear();
}
public Palette(IEnumerable<Color> colors)
@@ -82,9 +82,17 @@ namespace com.clusterrr.Famicom.NesTiler
private (Color color, double delta) GetMinDelta(Color color, Color bgColor)
{
+ var pair = new ColorPair()
+ {
+ Color1 = color,
+ Color2 = bgColor
+ };
+ if (deltaCache.ContainsKey(pair)) return deltaCache[pair];
var ac = Enumerable.Concat(colors.Where(c => c.HasValue).Select(c => c.Value), new Color[] { bgColor });
var result = ac.OrderBy(c => c.GetDelta(color)).First();
- return (result, result.GetDelta(color));
+ var r = (result, result.GetDelta(color));
+ deltaCache[pair] = r;
+ return r;
}
public bool Equals(Palette other)