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-13 21:01:50 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-10-13 21:01:50 +0300
commit3cee364775cd740b9f31c0ff71e2969b1cdca3b0 (patch)
treefc2b9ef03f7a4dbb2547b8bb5f894dd7bf51f916
parent6f96036ef87bd24d54e713ae0700ceba5428b32f (diff)
Migrated to SkiaSharp
-rw-r--r--NesTiler/ColorExtensions.cs15
-rw-r--r--NesTiler/NesTiler.csproj5
-rw-r--r--NesTiler/Palette.cs12
-rw-r--r--NesTiler/Program.cs38
-rw-r--r--Tests/Program.cs2
5 files changed, 43 insertions, 29 deletions
diff --git a/NesTiler/ColorExtensions.cs b/NesTiler/ColorExtensions.cs
index 2ddb434..d3d70be 100644
--- a/NesTiler/ColorExtensions.cs
+++ b/NesTiler/ColorExtensions.cs
@@ -1,5 +1,6 @@
using ColorMine.ColorSpaces;
using ColorMine.ColorSpaces.Comparisons;
+using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Data.Common;
@@ -7,6 +8,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using static System.Net.Mime.MediaTypeNames;
namespace com.clusterrr.Famicom.NesTiler
{
@@ -37,5 +39,18 @@ namespace com.clusterrr.Famicom.NesTiler
cache[pair] = delta;
return delta;
}
+
+ public static Color GetPixelColor(this SKBitmap image, int x, int y)
+ {
+ var skColor = image.GetPixel(x, y);
+ var color = Color.FromArgb(skColor.Red, skColor.Green, skColor.Blue);
+ return color;
+ }
+
+ public static void SetPixelColor(this SKBitmap image, int x, int y, Color color)
+ {
+ var skColor = new SKColor(color.R, color.G, color.B);
+ image.SetPixel(x, y, skColor);
+ }
}
}
diff --git a/NesTiler/NesTiler.csproj b/NesTiler/NesTiler.csproj
index 83a849b..3c82ec5 100644
--- a/NesTiler/NesTiler.csproj
+++ b/NesTiler/NesTiler.csproj
@@ -18,10 +18,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ColorMinePortable" Version="2.0.1" />
- <PackageReference Include="System.Drawing.Common" Version="5.0.3" />
- <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.346202">
- <PrivateAssets>all</PrivateAssets>
- </PackageReference>
+ <PackageReference Include="SkiaSharp" Version="2.88.3" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
diff --git a/NesTiler/Palette.cs b/NesTiler/Palette.cs
index a448917..16da496 100644
--- a/NesTiler/Palette.cs
+++ b/NesTiler/Palette.cs
@@ -1,8 +1,10 @@
-using System;
+using SkiaSharp;
+using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
+using Color = System.Drawing.Color;
namespace com.clusterrr.Famicom.NesTiler
{
@@ -31,7 +33,7 @@ namespace com.clusterrr.Famicom.NesTiler
// Empty palette
}
- public Palette(Bitmap image, int leftX, int topY, int width, int height, Color bgColor)
+ public Palette(SKBitmap image, int leftX, int topY, int width, int height, Color bgColor)
{
Dictionary<Color, int> colorCounter = new Dictionary<Color, int>();
colorCounter[bgColor] = 0;
@@ -39,7 +41,7 @@ namespace com.clusterrr.Famicom.NesTiler
{
for (int x = leftX; x < leftX + width; x++)
{
- var color = image.GetPixel(x, y);
+ var color = image.GetPixelColor(x, y);
if (!colorCounter.ContainsKey(color)) colorCounter[color] = 0;
colorCounter[color]++;
}
@@ -66,14 +68,14 @@ namespace com.clusterrr.Famicom.NesTiler
if (colorsList.Count > i) this[i + 1] = colorsList[i];
}
- public double GetTileDelta(Bitmap image, int leftX, int topY, int width, int height, Color bgColor)
+ public double GetTileDelta(SKBitmap image, int leftX, int topY, int width, int height, Color bgColor)
{
double delta = 0;
for (int y = topY; y < topY + height; y++)
{
for (int x = leftX; x < leftX + width; x++)
{
- var color = image.GetPixel(x, y);
+ var color = image.GetPixelColor(x, y);
delta += GetMinDelta(color, bgColor).delta;
}
}
diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs
index f29a43c..86258cc 100644
--- a/NesTiler/Program.cs
+++ b/NesTiler/Program.cs
@@ -9,6 +9,7 @@ using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
+using SkiaSharp;
namespace com.clusterrr.Famicom.NesTiler
{
@@ -72,8 +73,8 @@ namespace com.clusterrr.Famicom.NesTiler
var mode = TilesMode.Backgrounds;
int tilePalWidth = 16;
int tilePalHeight = 16;
- var imagesOriginal = new Dictionary<int, Bitmap>();
- var imagesRecolored = new Dictionary<int, Bitmap>();
+ var imagesOriginal = new Dictionary<int, SKBitmap>();
+ var imagesRecolored = new Dictionary<int, SKBitmap>();
var palleteIndexes = new Dictionary<int, byte[,]>();
var patternTableStartOffsets = new Dictionary<int, int>();
var patternTables = new Dictionary<int, Dictionary<int, Tile>>();
@@ -230,7 +231,7 @@ namespace com.clusterrr.Famicom.NesTiler
var offsetRegex = new Regex(@"^(?<filename>.*?)(:(?<offset>[0-9]+)(:(?<height>[0-9]+))?)?$");
var match = offsetRegex.Match(image.Value);
var filename = match.Groups["filename"].Value;
- imagesOriginal[image.Key] = Image.FromFile(filename) as Bitmap;
+ imagesOriginal[image.Key] = SKBitmap.Decode(filename);
var offsetS = match.Groups["offset"].Value;
var heightS = match.Groups["height"].Value;
// Crop it if need
@@ -241,13 +242,13 @@ namespace com.clusterrr.Famicom.NesTiler
if (!string.IsNullOrEmpty(heightS))
height = int.Parse(heightS);
Console.WriteLine($"Cropping it to {offset}:{height}...");
- var cropped = new Bitmap(imagesOriginal[image.Key].Width, height);
- var gr = Graphics.FromImage(cropped);
- gr.DrawImageUnscaledAndClipped(imagesOriginal[image.Key],
- new Rectangle(0, -offset, imagesOriginal[image.Key].Width, imagesOriginal[image.Key].Height));
- gr.Flush();
+ var cropped = new SKBitmap(imagesOriginal[image.Key].Width, height);
+ using (SKCanvas bitmapCanvas = new SKCanvas(cropped))
+ {
+ bitmapCanvas.DrawBitmap(imagesOriginal[image.Key], 0, -offset);
+ }
imagesOriginal[image.Key].Dispose();
- imagesOriginal[image.Key] = new Bitmap(cropped);
+ imagesOriginal[image.Key] = cropped;
}
if ((imagesOriginal[image.Key].Width % tilePalWidth != 0) || (imagesOriginal[image.Key].Height % tilePalHeight != 0))
throw new InvalidDataException("Invalid image size");
@@ -258,15 +259,15 @@ namespace com.clusterrr.Famicom.NesTiler
foreach (var imageNum in imagesOriginal.Keys)
{
Console.WriteLine($"Adjusting colors for file #{imageNum} - {imageFiles[imageNum]}...");
- var image = new Bitmap(imagesOriginal[imageNum]);
+ var image = imagesOriginal[imageNum].Copy();
imagesRecolored[imageNum] = image;
for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
- var color = image.GetPixel(x, y);
+ var color = image.GetPixelColor(x, y);
var similarColor = nesColors[FindSimilarColor(nesColors, color, nesColorsCache)];
- image.SetPixel(x, y, similarColor);
+ image.SetPixelColor(x, y, similarColor);
}
}
}
@@ -295,7 +296,7 @@ namespace com.clusterrr.Famicom.NesTiler
{
for (int x = 0; x < tilePalWidth; x++)
{
- var color = image.GetPixel(tileX * tilePalWidth + x, tileY * tilePalHeight + y);
+ var color = image.GetPixelColor(tileX * tilePalWidth + x, tileY * tilePalHeight + y);
if (!colorsInTile.Contains(color))
colorsInTile.Add(color);
}
@@ -479,7 +480,7 @@ namespace com.clusterrr.Famicom.NesTiler
{
Console.WriteLine($"Mapping palettes for file #{imageNum} - {imageFiles[imageNum]}...");
var image = imagesOriginal[imageNum];
- var imageRecolored = new Bitmap(image);
+ var imageRecolored = image.Copy();
imagesRecolored[imageNum] = imageRecolored;
palleteIndexes[imageNum] = new byte[image.Width / tilePalWidth, image.Height / tilePalHeight];
// For each tile/sprite
@@ -512,12 +513,12 @@ namespace com.clusterrr.Famicom.NesTiler
{
for (int x = 0; x < tilePalWidth; x++)
{
- var color = image.GetPixel(tileX * tilePalWidth + x, tileY * tilePalHeight + y);
+ var color = image.GetPixelColor(tileX * tilePalWidth + x, tileY * tilePalHeight + y);
var similarColor = FindSimilarColor(Enumerable.Concat(
bestPalette,
new Color[] { bgColor.Value }
), color);
- imageRecolored.SetPixel(
+ imageRecolored.SetPixelColor(
tileX * tilePalWidth + x,
tileY * tilePalHeight + y,
similarColor);
@@ -529,12 +530,11 @@ namespace com.clusterrr.Famicom.NesTiler
// Save preview if required
if (outPreview.ContainsKey(imageNum))
{
- imageRecolored.Save(outPreview[imageNum], ImageFormat.Png);
+ File.WriteAllBytes(outPreview[imageNum], imageRecolored.Encode(SKEncodedImageFormat.Png, 0).ToArray());
Console.WriteLine($"Preview #{imageNum} saved to {outPreview[imageNum]}");
}
}
-
// Generate attribute tables
foreach (var imageNum in outAttributeTable.Keys)
{
@@ -614,7 +614,7 @@ namespace com.clusterrr.Famicom.NesTiler
for (int y = 0; y < tileHeight; y++)
for (int x = 0; x < tileWidth; x++)
{
- var color = image.GetPixel(tileX * tileWidth + x, tileY * tileHeight + y);
+ var color = image.GetPixelColor(tileX * tileWidth + x, tileY * tileHeight + y);
var palette = palettes[palleteIndexes[imageNum][tileX / (tilePalWidth / tileWidth), tileY / (tilePalHeight / tileHeight)]];
byte colorIndex = 0;
if (color != bgColor)
diff --git a/Tests/Program.cs b/Tests/Program.cs
index 135457d..49c9390 100644
--- a/Tests/Program.cs
+++ b/Tests/Program.cs
@@ -2,7 +2,7 @@
namespace com.clusterrr.Famicom.NesTiler.Benchmarks
{
- public class Benchmarks
+ public class Tests
{
const string ImagesPath = "Images";
const string ReferencesDir = "References";