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 List<byte[]> AlternateColorRows { get; }
public int Width { get => 256; } public int Width { get => 256; }
public int Height { get => AlternateColorRows.Count + 2; } public int Height { get => AlternateColorRows.Count + 1; }
public Palette() public Palette()
{ {
@ -36,7 +36,7 @@ namespace Palettizer
break; 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); AlternateColorRows.Add(byteArray);
} }
public void ClearAlternateColorRows()
{
AlternateColorRows.Clear();
}
public byte[] CreateIndexedPaletteBitmap() public byte[] CreateIndexedPaletteBitmap()
{ {
var paletteBitmap = new byte[256 * (AlternateColorRows.Count + 2) * 4]; var paletteBitmap = new byte[256 * (AlternateColorRows.Count + 1) * 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;
}
for (var i = 0; i < 256; i += 1) for (var i = 0; i < 256; i += 1)
{ {
var color = new Color(); var color = new Color();
color.A = 255;
if (GrayscaleToColorMap.ContainsKey((byte)i)) if (GrayscaleToColorMap.ContainsKey((byte)i))
{ {
color = GrayscaleToColorMap[(byte)i]; color = GrayscaleToColorMap[(byte)i];
} }
paletteBitmap[(256 + i) * 4] = color.R;
paletteBitmap[(256 + i) * 4 + 1] = color.G; paletteBitmap[i * 4] = color.R;
paletteBitmap[(256 + i) * 4 + 2] = color.B; paletteBitmap[i * 4 + 1] = color.G;
paletteBitmap[(256 + i) * 4 + 3] = color.A; paletteBitmap[i * 4 + 2] = color.B;
paletteBitmap[i * 4 + 3] = color.A;
} }
for (var i = 0; i < 256; i += 1) for (var i = 0; i < 256; i += 1)
{ {
for (var j = 0; j < AlternateColorRows.Count; j += 1) for (var j = 0; j < AlternateColorRows.Count; j += 1)
{ {
paletteBitmap[(i + (256 * (j + 2))) * 4] = AlternateColorRows[j][i * 4]; var alpha = AlternateColorRows[j][i * 4 + 3];
paletteBitmap[(i + (256 * (j + 2))) * 4 + 1] = AlternateColorRows[j][i * 4 + 1]; if (alpha == 0)
paletteBitmap[(i + (256 * (j + 2))) * 4 + 2] = AlternateColorRows[j][i * 4 + 2]; {
paletteBitmap[(i + (256 * (j + 2))) * 4 + 3] = AlternateColorRows[j][i * 4 + 3]; // 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", "textureGroup",
"The name of the texture group to palettize." "The name of the texture group to palettize."
), ),
new Argument<string>( new Argument<string[]>(
"paletteSpriteName", "paletteSpriteNames",
"The palette sprite to use in indexing." "A series of palette sprites to use in indexing."
) )
}; };
@ -32,11 +32,11 @@ namespace Palettizer
project project
}; };
project.Handler = CommandHandler.Create<FileInfo, string, string, IConsole>(HandleProject); project.Handler = CommandHandler.Create<FileInfo, string, string[], IConsole>(HandleProject);
return root.Invoke(args); 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(); var jsonReaderOptions = new JsonDocumentOptions();
jsonReaderOptions.AllowTrailingCommas = true; jsonReaderOptions.AllowTrailingCommas = true;
@ -103,6 +103,8 @@ namespace Palettizer
} }
} }
foreach (var paletteSpriteName in paletteSpriteNames)
{
var paletteSpriteJSONDir = Path.Combine(projectDir.FullName, "sprites", paletteSpriteName); var paletteSpriteJSONDir = Path.Combine(projectDir.FullName, "sprites", paletteSpriteName);
var paletteSpriteJSONPath = Path.Combine(paletteSpriteJSONDir, paletteSpriteName + ".yy"); var paletteSpriteJSONPath = Path.Combine(paletteSpriteJSONDir, paletteSpriteName + ".yy");
@ -157,11 +159,14 @@ namespace Palettizer
} }
// Write final palette sprite to PNG // 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)) using (var stream = File.OpenWrite(paletteOutputPath))
{ {
writer.WritePng(palette.CreateIndexedPaletteBitmap(), palette.Width, palette.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream); writer.WritePng(palette.CreateIndexedPaletteBitmap(), palette.Width, palette.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
} }
palette.ClearAlternateColorRows();
}
} }
static byte[] AddSpriteToPalette(Palette palette, ImageResult image, IConsole console) static byte[] AddSpriteToPalette(Palette palette, ImageResult image, IConsole console)