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-24 17:52:20 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-10-24 17:52:20 +0300
commitee8f7a26df666f4e7d0103dd2ff7c62633273ba4 (patch)
treed62b1a413b0a9fa9cae534267a39152df49546fb
parent0ddbe9e7ed74ad7c9481467678eae4bf00aaba9d (diff)
Binary .pal files support.
-rw-r--r--NesTiler/Program.cs42
1 files changed, 35 insertions, 7 deletions
diff --git a/NesTiler/Program.cs b/NesTiler/Program.cs
index d9eb2a4..f738951 100644
--- a/NesTiler/Program.cs
+++ b/NesTiler/Program.cs
@@ -43,7 +43,7 @@ namespace com.clusterrr.Famicom.NesTiler
Console.WriteLine();
Console.WriteLine("Available options:");
Console.WriteLine(" {0,-40}{1}", "--in-<#> <file>[:offset[:height]]", "input file number #, optionally cropped vertically");
- Console.WriteLine(" {0,-40}{1}", "--colors <file>", $"JSON file with list of available colors (default - {DEFAULT_COLORS_FILE})");
+ Console.WriteLine(" {0,-40}{1}", "--colors <file>", $"JSON or PAL file with the list of available colors (default - {DEFAULT_COLORS_FILE})");
Console.WriteLine(" {0,-40}{1}", "--mode bg|sprite8x8|sprite8x16", "mode: backgrounds, 8x8 sprites or 8x16 sprites (default - bg)");
Console.WriteLine(" {0,-40}{1}", "--bg-color <color>", "background color in HTML color format (default - autodetected)");
Console.WriteLine(" {0,-40}{1}", "--enable-palettes <palettes>", "zero-based comma separated list of palette numbers to use (default - 0,1,2,3)");
@@ -272,20 +272,19 @@ namespace com.clusterrr.Famicom.NesTiler
if (!quiet) PrintAppInfo();
// Loading and parsing palette JSON
- var paletteJson = File.ReadAllText(colorsFile);
- var nesColorsStr = JsonSerializer.Deserialize<Dictionary<string, string>>(paletteJson);
- var nesColors = nesColorsStr.Select(kv => new KeyValuePair<byte, Color>(
- kv.Key.ToLower().StartsWith("0x") ? (byte)Convert.ToInt32(kv.Key.Substring(2), 16) : byte.Parse(kv.Key),
- ColorTranslator.FromHtml(kv.Value)
- )).Where(kv => !FORBIDDEN_COLORS.Contains(kv.Key)).ToDictionary(kv => kv.Key, kv => kv.Value);
+ var nesColors = LoadColors(colorsFile);
var outTilesCsvLines = !string.IsNullOrEmpty(outTilesCsv) ? new List<string>() : null;
var outPalettesCsvLines = !string.IsNullOrEmpty(outPalettesCsv) ? new List<string>() : null;
if (outColorsTable != null)
{
+ console($"Writing color tables to {outColorsTable}...");
WriteColorsTable(nesColors, outColorsTable);
}
+ // Stop if there are no images
+ if (!imageFiles.Any()) return 0;
+
// Change the fixed palettes to colors from the NES palette
for (int i = 0; i < fixedPalettes.Length; i++)
{
@@ -697,6 +696,35 @@ namespace com.clusterrr.Famicom.NesTiler
}
}
+ private static Dictionary<byte, Color> LoadColors(string filename)
+ {
+ var data = File.ReadAllBytes(filename);
+ Dictionary<byte, Color> nesColors;
+ // Detect file type
+ if ((Path.GetExtension(filename) == ".pal") || ((data.Length == 192 || data.Length == 1536) && data.Where(b => b >= 128).Any()))
+ {
+ // Binary file
+ nesColors = new Dictionary<byte, Color>();
+ for (byte c = 0; c < 64; c++)
+ {
+ var color = Color.FromArgb(data[c * 3], data[c * 3 + 1], data[c * 3 + 2]);
+ nesColors[c] = color;
+ }
+ }
+ else
+ {
+ var paletteJson = File.ReadAllText(filename);
+ var nesColorsStr = JsonSerializer.Deserialize<Dictionary<string, string>>(paletteJson);
+ nesColors = nesColorsStr.ToDictionary(
+ kv => kv.Key.ToLower().StartsWith("0x") ? (byte)Convert.ToInt32(kv.Key.Substring(2), 16) : byte.Parse(kv.Key),
+ kv => ColorTranslator.FromHtml(kv.Value)
+ );
+ }
+ // filter out invalid colors;
+ nesColors = nesColors.Where(kv => !FORBIDDEN_COLORS.Contains(kv.Key)).ToDictionary(kv => kv.Key, kv => kv.Value);
+ return nesColors;
+ }
+
static void WriteColorsTable(Dictionary<byte, Color> nesColors, string filename)
{
const int colorSize = 64;