Browse Source

various fixes

main
cosmonaut 6 months ago
parent
commit
1713c86afe
  1. 1
      .gitignore
  2. 5
      src/Color.cs
  3. 5
      src/Palette.cs
  4. 29
      src/Program.cs

1
.gitignore vendored

@ -1,2 +1,3 @@ @@ -1,2 +1,3 @@
bin/
obj/
output/

5
src/Color.cs

@ -6,5 +6,10 @@ namespace Palettizer @@ -6,5 +6,10 @@ namespace Palettizer
public byte G { get; set; }
public byte B { get; set; }
public byte A { get; set; }
public override string ToString()
{
return $"{{R: {R.ToString()}, G: {G.ToString()}, B: {B.ToString()}, A: {A.ToString()}}}";
}
}
}

5
src/Palette.cs

@ -40,8 +40,9 @@ namespace Palettizer @@ -40,8 +40,9 @@ namespace Palettizer
}
else
{
System.Console.WriteLine("That color doesn't exist in the grayscale palette!! Bailing!!!");
}
System.Console.WriteLine($"Color {color.ToString()} doesn't exist in the grayscale palette!! Bailing!!!");
return;
}
}
AlternateColorRows.Add(byteArray);

29
src/Program.cs

@ -43,7 +43,7 @@ namespace Palettizer @@ -43,7 +43,7 @@ namespace Palettizer
var gmProject = Importer.ImportProject(project);
var projectDir = project.Directory;
console.Out.Write("Parsing: " + gmProject.Name);
console.Out.Write("Parsing: " + gmProject.Name + "\n");
if (Directory.Exists("output"))
{
@ -54,10 +54,11 @@ namespace Palettizer @@ -54,10 +54,11 @@ namespace Palettizer
var palette = new Palette();
var writer = new StbImageWriteSharp.ImageWriter();
// Add all sprite colors to palette
// Add all sprite colors to palette
foreach (var resource in gmProject.Resources)
{
var path = Path.Combine(projectDir.FullName, resource.ID.Path);
var spriteDir = Path.GetDirectoryName(path);
var file = new FileInfo(path);
if (file.Name.StartsWith("pal_")) { continue; }
@ -71,12 +72,14 @@ namespace Palettizer @@ -71,12 +72,14 @@ namespace Palettizer
if (sprite.TextureGroupID.Name == textureGroup)
{
console.Out.Write(sprite.Name + "\n");
foreach (var spriteFrame in sprite.Frames)
{
ImageResult image;
byte[] grayscaleImage;
var spriteImageFilePath = Path.Combine(path, spriteFrame.CompositeImage.FrameID.Name + ".png");
var spriteImageFilePath = Path.Combine(spriteDir, spriteFrame.CompositeImage.FrameID.Name + ".png");
// Add sprite to palette
using (var stream = File.OpenRead(spriteImageFilePath))
@ -85,14 +88,18 @@ namespace Palettizer @@ -85,14 +88,18 @@ namespace Palettizer
grayscaleImage = AddSpriteToPalette(palette, image, console);
}
// Now write the image to output directory as grayscale
using (var stream = File.OpenWrite(Path.Combine(outputDir.FullName, resource.ID.Path)))
var spriteWriteDir = Path.Combine(outputDir.FullName, Path.GetRelativePath(projectDir.FullName, spriteDir));
Directory.CreateDirectory(spriteWriteDir);
var spriteWritePath = Path.Combine(spriteWriteDir, spriteFrame.CompositeImage.FrameID.Name + ".png");
// Now write the image to output directory as grayscale
using (var stream = File.OpenWrite(spriteWritePath))
{
writer.WritePng(grayscaleImage, image.Width, image.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
}
}
}
}
}
}
var paletteSpriteJSONDir = Path.Combine(projectDir.FullName, "sprites", paletteSpriteName);
@ -115,10 +122,10 @@ namespace Palettizer @@ -115,10 +122,10 @@ namespace Palettizer
{
colorPalette[i] = new Color
{
R = paletteImage.Data[j + i * paletteImage.Width],
G = paletteImage.Data[j + i * paletteImage.Width + 1],
B = paletteImage.Data[j + i * paletteImage.Width + 2],
A = paletteImage.Data[j + i * paletteImage.Width + 3]
R = paletteImage.Data[j * 4 + i * paletteImage.Width * 4],
G = paletteImage.Data[(j * 4 + i * paletteImage.Width * 4) + 1],
B = paletteImage.Data[(j * 4 + i * paletteImage.Width * 4) + 2],
A = paletteImage.Data[(j * 4 + i * paletteImage.Width * 4) + 3]
};
}
@ -127,7 +134,7 @@ namespace Palettizer @@ -127,7 +134,7 @@ namespace Palettizer
}
// Write final palette sprite to PNG
var paletteOutputPath = Path.Combine(outputDir.FullName, textureGroup, "Palette.png");
var paletteOutputPath = Path.Combine(outputDir.FullName, textureGroup + "_Palette.png");
using (var stream = File.OpenWrite(paletteOutputPath))
{
writer.WritePng(palette.CreateIndexedPaletteBitmap(), palette.Width, palette.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);

Loading…
Cancel
Save