system for converting old palettes

main
cosmonaut 2023-05-18 11:43:00 -07:00
parent 6d0efda7e7
commit 1cf70e42df
2 changed files with 60 additions and 2 deletions

View File

@ -45,6 +45,50 @@ namespace Palettizer2
GrayscaleCount += 1;
}
public void AddAlternateRowsFromOldPalette(StbImageSharp.ImageResult paletteImage)
{
// map row indices to original colors
var indexToGrayscaleMap = new Dictionary<int, byte>();
for (var i = 0; i < paletteImage.Height; i += 1)
{
var sourceColor = new Color
{
R = paletteImage.Data[i * paletteImage.Width * 4],
G = paletteImage.Data[i * paletteImage.Width * 4 + 1],
B = paletteImage.Data[i * paletteImage.Width * 4 + 2],
A = paletteImage.Data[i * paletteImage.Width * 4 + 3]
};
if (!ColorToGrayscaleMap.ContainsKey(sourceColor))
{
continue;
//throw new System.Exception("Color not found! " + sourceColor);
}
indexToGrayscaleMap[i] = ColorToGrayscaleMap[sourceColor];
}
// build the rows
for (var j = 1; j < paletteImage.Width; j += 1)
{
var paletteRow = new Color[256];
for (var i = 0; i < paletteImage.Height; i += 1)
{
if (!indexToGrayscaleMap.ContainsKey(i)) { continue; }
var grayscale = indexToGrayscaleMap[i];
paletteRow[grayscale].R = paletteImage.Data[(j + i * paletteImage.Width) * 4];
paletteRow[grayscale].G = paletteImage.Data[(j + i * paletteImage.Width) * 4 + 1];
paletteRow[grayscale].B = paletteImage.Data[(j + i * paletteImage.Width) * 4 + 2];
paletteRow[grayscale].A = paletteImage.Data[(j + i * paletteImage.Width) * 4 + 3];
}
AddAlternateColorRow(paletteRow);
}
}
public void AddAlternateColorRow(Color[] colors)
{
var byteArray = new byte[256 * 4];

View File

@ -18,6 +18,11 @@ namespace Palettizer2
new Argument<string>(
"paletteName",
"The name of the palette sprite to output."
),
new Argument<FileInfo>(
"oldPalette",
() => null,
"For converting old-style palette to new"
)
};
@ -26,11 +31,11 @@ namespace Palettizer2
palettize
};
palettize.Handler = CommandHandler.Create<DirectoryInfo, string>(HandlePalettize);
palettize.Handler = CommandHandler.Create<DirectoryInfo, string, FileInfo>(HandlePalettize);
return root.Invoke(args);
}
static void HandlePalettize(DirectoryInfo directory, string paletteName)
static void HandlePalettize(DirectoryInfo directory, string paletteName, FileInfo oldPalette)
{
var outputDir = Directory.CreateDirectory("output");
var palette = new Palette();
@ -69,6 +74,15 @@ namespace Palettizer2
}
}
if (oldPalette != null)
{
using (var stream = File.OpenRead(oldPalette.FullName))
{
ImageResult paletteImage = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
palette.AddAlternateRowsFromOldPalette(paletteImage);
}
}
// write palette sprite to PNG
var paletteOutputPath = Path.Combine(outputDir.FullName, paletteName + ".png");
using (var stream = File.OpenWrite(paletteOutputPath))