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-23 21:25:38 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-10-23 21:25:38 +0300
commit64e830ee0e3e42c6a5772b756e7f411fad4cf153 (patch)
tree86696400072c36fc9c060819049a4900fcb7fb16 /NesTiler
parent7dd48d7271047e2d18cc92d2fb0d3d4e9f62cb53 (diff)
Optimization.
Diffstat (limited to 'NesTiler')
-rw-r--r--NesTiler/Program.cs10
-rw-r--r--NesTiler/Tile.cs73
2 files changed, 38 insertions, 45 deletions
diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs
index c8b34c3..4254a8e 100644
--- a/NesTiler/Program.cs
+++ b/NesTiler/Program.cs
@@ -576,7 +576,7 @@ namespace com.clusterrr.Famicom.NesTiler
{
for (int tileX = 0; tileX < image.Width / tileWidth; tileX++)
{
- var tileData = new byte[tileWidth, tileHeight];
+ var tileData = new byte[tileWidth * tileHeight];
byte paletteIndex = 0;
for (int y = 0; y < tileHeight; y++)
for (int x = 0; x < tileWidth; x++)
@@ -589,9 +589,9 @@ namespace com.clusterrr.Famicom.NesTiler
paletteIndex = 1;
while (palette[paletteIndex] != color) paletteIndex++;
}
- tileData[x, y] = paletteIndex;
+ tileData[y * tileWidth + x] = paletteIndex;
}
- var tile = new Tile(tileData);
+ var tile = new Tile(tileData, tileWidth, tileHeight);
var existsTile = patternTable.Where(kv => kv.Value.Equals(tile));
int currentTileID;
if (existsTile.Any())
@@ -626,7 +626,7 @@ namespace com.clusterrr.Famicom.NesTiler
var patternTableRaw = new List<byte>();
for (int t = patternTableStartOffsets[imageNum]; t < tileID; t++)
{
- var raw = patternTable[t].GetRawData();
+ var raw = patternTable[t].GetAsTileData();
patternTableRaw.AddRange(raw);
}
File.WriteAllBytes(outPatternTable[imageNum], patternTableRaw.ToArray());
@@ -647,7 +647,7 @@ namespace com.clusterrr.Famicom.NesTiler
var patternTableRaw = new List<byte>();
for (int t = patternTableStartOffsetShared; t < tileID; t++)
{
- var raw = patternTables[0][t].GetRawData();
+ var raw = patternTables[0][t].GetAsTileData();
patternTableRaw.AddRange(raw);
}
File.WriteAllBytes(outPatternTableShared, patternTableRaw.ToArray());
diff --git a/NesTiler/Tile.cs b/NesTiler/Tile.cs
index faca8b9..c3db539 100644
--- a/NesTiler/Tile.cs
+++ b/NesTiler/Tile.cs
@@ -1,65 +1,58 @@
using System;
+using System.Linq;
+using System.Security.AccessControl;
namespace com.clusterrr.Famicom.NesTiler
{
- class Tile : IEquatable<Tile>
+ sealed record Tile : IEquatable<Tile>
{
- public readonly byte[,] pixels;
+ public readonly byte[] Pixels;
+ public readonly int Width;
+ public readonly int Height;
+ private int? hash;
+ private byte[] data = null;
- public Tile(byte[,] data)
+ public Tile(byte[] data, int width, int height)
{
- pixels = data;
+ (Pixels, Width, Height) = (data, width, height);
}
- public byte[] GetRawData()
+ public byte[] GetAsTileData()
{
- int width = pixels.GetLength(0);
- int height = pixels.GetLength(1);
- var raw = new byte[width * height / 8 * 2];
- int pixel = 0;
- byte bit = 7;
- for (int y = 0; y < height; y++)
+ if (data != null) return data;
+ data = new byte[Width * Height / 8 * 2];
+ lock (data)
{
- for (int x = 0; x < width; x++)
+ int pixel = 0;
+ byte bit = 7;
+ for (int y = 0; y < Height; y++)
{
- if ((pixels[x, y] & 1) != 0)
- raw[pixel / 64 * 2 + y] |= (byte)(1 << bit);
- if ((pixels[x, y] & 2) != 0)
- raw[pixel / 64 * 2 + y + 8] |= (byte)(1 << bit);
- pixel++;
- bit = (byte)((byte)(bit - 1) % 8);
+ for (int x = 0; x < Width; x++)
+ {
+ if ((Pixels[y * Width + x] & 1) != 0)
+ data[pixel / 64 * 2 + y] |= (byte)(1 << bit);
+ if ((Pixels[y * Width + x] & 2) != 0)
+ data[pixel / 64 * 2 + y + 8] |= (byte)(1 << bit);
+ pixel++;
+ bit = (byte)((byte)(bit - 1) % 8);
+ }
}
}
- return raw;
+ return data;
}
public bool Equals(Tile other)
{
- if ((pixels.GetLength(0) != other.pixels.GetLength(0))
- || (pixels.GetLength(1) != other.pixels.GetLength(1)))
- return false;
- int width = pixels.GetLength(0);
- int height = pixels.GetLength(1);
- for (int y = 0; y < height; y++)
- for (int x = 0; x < width; x++)
- if (pixels[x, y] != other.pixels[x, y])
- return false;
- return true;
+ var data1 = GetAsTileData();
+ var data2 = other.GetAsTileData();
+ return Enumerable.SequenceEqual(data1, data2);
}
public override int GetHashCode()
{
- int hash = 0;
- int width = pixels.GetLength(0);
- int height = pixels.GetLength(1);
- for (int y = 0; y < height; y++)
- for (int x = 0; x < width; x++)
- {
- hash ^= hash >> 28;
- hash <<= 4;
- hash ^= pixels[x, y];
- }
- return hash;
+ if (hash != null) return hash.Value;
+ hash = GetAsTileData().Sum(v => v);
+ return hash.Value;
}
}
}