initial commit

main
cosmonaut 2021-11-29 18:04:25 -08:00
commit c96be2b26b
12 changed files with 423 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/
obj/

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "lib/StbImageSharp"]
path = lib/StbImageSharp
url = https://github.com/StbSharp/StbImageSharp.git
[submodule "lib/StbImageWriteSharp"]
path = lib/StbImageWriteSharp
url = https://github.com/StbSharp/StbImageWriteSharp.git

27
Palettizer.csproj Normal file
View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);lib\**\*</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include=".\lib\StbImageSharp\src\StbImageSharp.csproj" />
<ProjectReference Include=".\lib\StbImageWriteSharp\src\StbImageWriteSharp.csproj" />
</ItemGroup>
</Project>

22
Palettizer.sln Normal file
View File

@ -0,0 +1,22 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Palettizer", "Palettizer.csproj", "{CC59DC79-B418-411E-BA42-017422F30821}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CC59DC79-B418-411E-BA42-017422F30821}.Debug|x64.ActiveCfg = Debug|x64
{CC59DC79-B418-411E-BA42-017422F30821}.Debug|x64.Build.0 = Debug|x64`
{CC59DC79-B418-411E-BA42-017422F30821}.Release|x64.ActiveCfg = Release|x86
{CC59DC79-B418-411E-BA42-017422F30821}.Release|x64.Build.0 = Release|x86
EndGlobalSection
EndGlobal

1
lib/StbImageSharp Submodule

@ -0,0 +1 @@
Subproject commit fd127d69f232207b03a673d2cf9b51ce82197f35

@ -0,0 +1 @@
Subproject commit c14f96af618175c73c1cbedbf613c6a0d3c996ee

10
src/Color.cs Normal file
View File

@ -0,0 +1,10 @@
namespace Palettizer
{
public struct Color
{
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public byte A { get; set; }
}
}

20
src/GMProject.cs Normal file
View File

@ -0,0 +1,20 @@
namespace Palettizer
{
public struct GMProjectResourceValue
{
public string Name { get; set; }
public string Path { get; set; }
}
public struct GMProjectResource
{
public GMProjectResourceValue ID { get; set; }
public uint Order { get; set; }
}
public struct GMProject
{
public GMProjectResource[] Resources { get; set; }
public string Name { get; set; }
}
}

31
src/GMSprite.cs Normal file
View File

@ -0,0 +1,31 @@
namespace Palettizer
{
public struct GMTextureGroupID
{
public string Name { get; set; }
public string Path { get; set; } // this doesnt seem to be used lol
}
public struct GMSpriteFrameID
{
public string Name { get; set; } // this is actually a GUID lol
}
public struct GMSpriteCompositeImage
{
public GMSpriteFrameID FrameID { get; set; }
}
public struct GMSpriteFrame
{
public GMSpriteCompositeImage CompositeImage { get; set; }
}
public struct GMSprite
{
public GMTextureGroupID TextureGroupID { get; set; }
public GMSpriteFrame[] Frames { get; set; }
public string ResourceType { get; set; }
public string Name { get; set; }
}
}

35
src/Importer.cs Normal file
View File

@ -0,0 +1,35 @@
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Palettizer
{
public static class Importer
{
static JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true
};
static Importer()
{
options.Converters.Add(new JsonStringEnumConverter());
}
public static GMProject ImportProject(FileInfo projectFile)
{
return ImportResource<GMProject>(projectFile);
}
public static GMSprite ImportSprite(FileInfo soundFile)
{
return ImportResource<GMSprite>(soundFile);
}
private static T ImportResource<T>(FileInfo resourceFile)
{
return JsonSerializer.Deserialize<T>(File.ReadAllText(resourceFile.FullName), options);
}
}
}

85
src/Palette.cs Normal file
View File

@ -0,0 +1,85 @@
using System.Collections.Generic;
namespace Palettizer
{
public class Palette
{
public Dictionary<Color, byte> ColorToGrayscaleMap { get; }
public byte GrayscaleIndex { get; set; }
public List<byte[]> AlternateColorRows { get; }
public int Width { get => ColorToGrayscaleMap.Count; }
public int Height { get => AlternateColorRows.Count + 1; }
public Palette()
{
ColorToGrayscaleMap = new Dictionary<Color, byte>();
GrayscaleIndex = 0;
AlternateColorRows = new List<byte[]>();
}
public void AddColor(Color color, byte grayscaleIndex)
{
ColorToGrayscaleMap.Add(color, grayscaleIndex);
}
public void AddAlternateColorRow(Color[] colors)
{
var byteArray = new byte[ColorToGrayscaleMap.Count * 4];
foreach (var color in colors)
{
if (ColorToGrayscaleMap.ContainsKey(color))
{
var grayscaleIndex = ColorToGrayscaleMap[color];
byteArray[grayscaleIndex] = color.R;
byteArray[grayscaleIndex + 1] = color.G;
byteArray[grayscaleIndex + 2] = color.B;
byteArray[grayscaleIndex + 3] = color.A;
}
else
{
System.Console.WriteLine("That color doesn't exist in the grayscale palette!! Bailing!!!");
}
}
AlternateColorRows.Add(byteArray);
}
public byte[] CreateIndexedPaletteBitmap()
{
var paletteBitmap = new byte[ColorToGrayscaleMap.Count * 4 * (AlternateColorRows.Count + 1)];
var index = 0;
foreach (KeyValuePair<Color, byte> kv in ColorToGrayscaleMap)
{
var color = kv.Key;
var grayscale = kv.Value;
paletteBitmap[index] = grayscale;
paletteBitmap[index + 1] = grayscale;
paletteBitmap[index + 2] = grayscale;
paletteBitmap[index + 3] = 255;
var alternateIndex = 1;
foreach (var alternateColorRow in AlternateColorRows)
{
for (var j = 0; j < ColorToGrayscaleMap.Count * 4; j += 4)
{
paletteBitmap[index + (ColorToGrayscaleMap.Count * alternateIndex)] = alternateColorRow[j];
paletteBitmap[index + 1 + (ColorToGrayscaleMap.Count * alternateIndex)] = alternateColorRow[j + 1];
paletteBitmap[index + 2 + (ColorToGrayscaleMap.Count * alternateIndex)] = alternateColorRow[j + 2];
paletteBitmap[index + 3 + (ColorToGrayscaleMap.Count * alternateIndex)] = alternateColorRow[j + 3];
}
alternateIndex += 1;
}
index += 4;
}
return paletteBitmap;
}
}
}

183
src/Program.cs Normal file
View File

@ -0,0 +1,183 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Text;
using System.Text.Json;
using StbImageSharp;
namespace Palettizer
{
class Program
{
static int Main(string[] args)
{
var project = new Command("project")
{
new Argument<FileInfo>(
"project",
"Path to a GMS2.3 yyp file."
),
new Argument<string>(
"textureGroup",
"The name of the texture group to palettize."
),
new Argument<string>(
"paletteSpriteName",
"The palette sprite to use in indexing."
)
};
var root = new RootCommand{
project
};
project.Handler = CommandHandler.Create<FileInfo, string, string, IConsole>(HandleProject);
return root.Invoke(args);
}
static void HandleProject(FileInfo project, string textureGroup, string paletteSpriteName, IConsole console)
{
var jsonReaderOptions = new JsonDocumentOptions();
jsonReaderOptions.AllowTrailingCommas = true;
var gmProject = Importer.ImportProject(project);
var projectDir = project.Directory;
console.Out.Write("Parsing: " + gmProject.Name);
if (Directory.Exists("output"))
{
Directory.Delete("output", true);
}
var outputDir = Directory.CreateDirectory("output");
var palette = new Palette();
var writer = new StbImageWriteSharp.ImageWriter();
// Add all sprite colors to palette
foreach (var resource in gmProject.Resources)
{
var path = Path.Combine(projectDir.FullName, resource.ID.Path);
var file = new FileInfo(path);
if (file.Name.StartsWith("pal_")) { continue; }
var resourceJsonString = File.ReadAllText(path);
var document = JsonDocument.Parse(resourceJsonString, jsonReaderOptions);
if (document.RootElement.GetProperty("resourceType").GetString() == "GMSprite")
{
var sprite = Importer.ImportSprite(file);
if (sprite.TextureGroupID.Name == textureGroup)
{
foreach (var spriteFrame in sprite.Frames)
{
ImageResult image;
byte[] grayscaleImage;
var spriteImageFilePath = Path.Combine(path, spriteFrame.CompositeImage.FrameID.Name + ".png");
// Add sprite to palette
using (var stream = File.OpenRead(spriteImageFilePath))
{
image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
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)))
{
writer.WritePng(grayscaleImage, image.Width, image.Height, StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
}
}
}
}
}
var paletteSpriteJSONDir = Path.Combine(projectDir.FullName, "sprites", paletteSpriteName);
var paletteSpriteJSONPath = Path.Combine(paletteSpriteJSONDir, paletteSpriteName + ".yy");
var paletteSpriteData = Importer.ImportSprite(new FileInfo(paletteSpriteJSONPath));
var paletteSpriteImagePath = Path.Combine(paletteSpriteJSONDir, paletteSpriteData.Frames[0].CompositeImage.FrameID.Name + ".png");
// Add alternate color palettes to palette
// NOTE: our original palettes were column-indexed, we want to convert to row-index
using (var stream = File.OpenRead(paletteSpriteImagePath))
{
ImageResult paletteImage = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
for (var j = 0; j < paletteImage.Width; j += 1)
{
var colorPalette = new Color[paletteImage.Height];
for (var i = 0; i < paletteImage.Height; i += 1)
{
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]
};
}
palette.AddAlternateColorRow(colorPalette);
}
}
// Write final palette sprite to 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);
}
}
static byte[] AddSpriteToPalette(Palette palette, ImageResult image, IConsole console)
{
var grayscaleImage = new byte[image.Width * image.Height * 4];
for (var i = 0; i < image.Width * image.Height * 4; i += 4)
{
var color = new Color
{
R = image.Data[i],
G = image.Data[i + 1],
B = image.Data[i + 2],
A = image.Data[i + 3]
};
if (color.A == 0)
{
continue;
}
if (palette.ColorToGrayscaleMap.ContainsKey(color))
{
var grayscaleColor = palette.ColorToGrayscaleMap[color];
grayscaleImage[i] = grayscaleColor;
grayscaleImage[i + 1] = grayscaleColor;
grayscaleImage[i + 2] = grayscaleColor;
grayscaleImage[i + 3] = color.A;
}
else
{
if (palette.GrayscaleIndex == 255)
{
console.Out.Write("Too many colors! Bailing!\n");
return grayscaleImage;
}
else
{
palette.ColorToGrayscaleMap.Add(color, palette.GrayscaleIndex);
palette.GrayscaleIndex += 1;
}
}
}
return grayscaleImage;
}
}
}