some encoding tweaks

main
cosmonaut 2021-11-30 19:35:00 -08:00
parent 3a5705eb17
commit b65ea2ca66
2 changed files with 95 additions and 78 deletions

View File

@ -11,7 +11,7 @@ namespace Palettizer
public List<byte[]> AlternateColorRows { get; }
public int Width { get => 256; }
public int Height { get => AlternateColorRows.Count + 2; }
public int Height { get => AlternateColorRows.Count + 1; }
public Palette()
{
@ -36,7 +36,7 @@ namespace Palettizer
break;
}
increment = ((increment > 0) ? (increment + 1) : (increment - 1)) * -1;
increment = ((increment > 0) ? (increment + 1) : (increment - 1)) * -1; // up by 1, down by 2, up by 3, etc
}
}
@ -61,39 +61,51 @@ namespace Palettizer
AlternateColorRows.Add(byteArray);
}
public void ClearAlternateColorRows()
{
AlternateColorRows.Clear();
}
public byte[] CreateIndexedPaletteBitmap()
{
var paletteBitmap = new byte[256 * (AlternateColorRows.Count + 2) * 4];
for (var i = 0; i < 256; i += 1)
{
paletteBitmap[i * 4] = (byte)i;
paletteBitmap[i * 4 + 1] = (byte)i;
paletteBitmap[i * 4 + 2] = (byte)i;
paletteBitmap[i * 4 + 3] = 255;
}
var paletteBitmap = new byte[256 * (AlternateColorRows.Count + 1) * 4];
for (var i = 0; i < 256; i += 1)
{
var color = new Color();
color.A = 255;
if (GrayscaleToColorMap.ContainsKey((byte)i))
{
color = GrayscaleToColorMap[(byte)i];
}
paletteBitmap[(256 + i) * 4] = color.R;
paletteBitmap[(256 + i) * 4 + 1] = color.G;
paletteBitmap[(256 + i) * 4 + 2] = color.B;
paletteBitmap[(256 + i) * 4 + 3] = color.A;
paletteBitmap[i * 4] = color.R;
paletteBitmap[i * 4 + 1] = color.G;
paletteBitmap[i * 4 + 2] = color.B;
paletteBitmap[i * 4 + 3] = color.A;
}
for (var i = 0; i < 256; i += 1)
{
for (var j = 0; j < AlternateColorRows.Count; j += 1)
{
paletteBitmap[(i + (256 * (j + 2))) * 4] = AlternateColorRows[j][i * 4];
paletteBitmap[(i + (256 * (j + 2))) * 4 + 1] = AlternateColorRows[j][i * 4 + 1];
paletteBitmap[(i + (256 * (j + 2))) * 4 + 2] = AlternateColorRows[j][i * 4 + 2];
paletteBitmap[(i + (256 * (j + 2))) * 4 + 3] = AlternateColorRows[j][i * 4 + 3];
var alpha = AlternateColorRows[j][i * 4 + 3];
if (alpha == 0)
{
// grab from top row
paletteBitmap[(i + (256 * (j + 1))) * 4] = paletteBitmap[i * 4];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 1] = paletteBitmap[i * 4 + 1];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 2] = paletteBitmap[i * 4 + 2];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 3] = paletteBitmap[i * 4 + 3];
}
else
{
paletteBitmap[(i + (256 * (j + 1))) * 4] = AlternateColorRows[j][i * 4];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 1] = AlternateColorRows[j][i * 4 + 1];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 2] = AlternateColorRows[j][i * 4 + 2];
paletteBitmap[(i + (256 * (j + 1))) * 4 + 3] = alpha;
}
}
}

View File

@ -22,9 +22,9 @@ namespace Palettizer
"textureGroup",
"The name of the texture group to palettize."
),
new Argument<string>(
"paletteSpriteName",
"The palette sprite to use in indexing."
new Argument<string[]>(
"paletteSpriteNames",
"A series of palette sprites to use in indexing."
)
};
@ -32,11 +32,11 @@ namespace Palettizer
project
};
project.Handler = CommandHandler.Create<FileInfo, string, string, IConsole>(HandleProject);
project.Handler = CommandHandler.Create<FileInfo, string, string[], IConsole>(HandleProject);
return root.Invoke(args);
}
static void HandleProject(FileInfo project, string textureGroup, string paletteSpriteName, IConsole console)
static void HandleProject(FileInfo project, string textureGroup, string[] paletteSpriteNames, IConsole console)
{
var jsonReaderOptions = new JsonDocumentOptions();
jsonReaderOptions.AllowTrailingCommas = true;
@ -103,6 +103,8 @@ namespace Palettizer
}
}
foreach (var paletteSpriteName in paletteSpriteNames)
{
var paletteSpriteJSONDir = Path.Combine(projectDir.FullName, "sprites", paletteSpriteName);
var paletteSpriteJSONPath = Path.Combine(paletteSpriteJSONDir, paletteSpriteName + ".yy");
@ -157,11 +159,14 @@ namespace Palettizer
}
// Write final palette sprite to PNG
var paletteOutputPath = Path.Combine(outputDir.FullName, textureGroup + "_Palette.png");
var paletteOutputPath = Path.Combine(outputDir.FullName, textureGroup + "_" + paletteSpriteName + "_Palette.png");
using (var stream = File.OpenWrite(paletteOutputPath))
{
writer.WritePng(palette.CreateIndexedPaletteBitmap(), palette.Width, palette.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
}
palette.ClearAlternateColorRows();
}
}
static byte[] AddSpriteToPalette(Palette palette, ImageResult image, IConsole console)