summaryrefslogtreecommitdiff
path: root/tools/gbagfx/main.c
diff options
context:
space:
mode:
authorshinny <shinny456@users.noreply.github.com>2020-06-02 14:51:57 -0400
committershinny <shinny456@users.noreply.github.com>2020-06-02 14:51:57 -0400
commit4f88655a79bb2d8f10bdce2841f427c0e27040bd (patch)
treeb750e64b62cec61ca3a43da6aa7d4e3b37e64c9e /tools/gbagfx/main.c
parentbc504264f1e54b3c1e482710c592e5549828bfe1 (diff)
update tools
Diffstat (limited to 'tools/gbagfx/main.c')
-rw-r--r--tools/gbagfx/main.c110
1 files changed, 99 insertions, 11 deletions
diff --git a/tools/gbagfx/main.c b/tools/gbagfx/main.c
index aa0681f..61e93ea 100644
--- a/tools/gbagfx/main.c
+++ b/tools/gbagfx/main.c
@@ -27,7 +27,17 @@ void ConvertGbaToPng(char *inputPath, char *outputPath, struct GbaToPngOptions *
if (options->paletteFilePath != NULL)
{
- ReadGbaPalette(options->paletteFilePath, &image.palette);
+ char *paletteFileExtension = GetFileExtensionAfterDot(options->paletteFilePath);
+
+ if (strcmp(paletteFileExtension, "gbapal") == 0)
+ {
+ ReadGbaPalette(options->paletteFilePath, &image.palette);
+ }
+ else
+ {
+ ReadJascPalette(options->paletteFilePath, &image.palette);
+ }
+
image.hasPalette = true;
}
else
@@ -35,6 +45,20 @@ void ConvertGbaToPng(char *inputPath, char *outputPath, struct GbaToPngOptions *
image.hasPalette = false;
}
+ if (options->tilemapFilePath != NULL)
+ {
+ int fileSize;
+ image.tilemap.data.affine = ReadWholeFile(options->tilemapFilePath, &fileSize);
+ if (options->isAffineMap && options->bitDepth != 8)
+ FATAL_ERROR("affine maps are necessarily 8bpp\n");
+ image.isAffine = options->isAffineMap;
+ image.tilemap.size = fileSize;
+ }
+ else
+ {
+ image.tilemap.data.affine = NULL;
+ }
+
ReadImage(inputPath, options->width, options->bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette);
image.hasTransparency = options->hasTransparency;
@@ -49,6 +73,7 @@ void ConvertPngToGba(char *inputPath, char *outputPath, struct PngToGbaOptions *
struct Image image;
image.bitDepth = options->bitDepth;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
ReadPng(inputPath, &image);
@@ -59,7 +84,7 @@ void ConvertPngToGba(char *inputPath, char *outputPath, struct PngToGbaOptions *
void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
- char *inputFileExtension = GetFileExtension(inputPath);
+ char *inputFileExtension = GetFileExtensionAfterDot(inputPath);
struct GbaToPngOptions options;
options.paletteFilePath = NULL;
options.bitDepth = inputFileExtension[0] - '0';
@@ -67,6 +92,7 @@ void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **a
options.width = 1;
options.metatileWidth = 1;
options.metatileHeight = 1;
+ options.isAffineMap = false;
for (int i = 3; i < argc; i++)
{
@@ -124,6 +150,17 @@ void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **a
if (options.metatileHeight < 1)
FATAL_ERROR("metatile height must be positive.\n");
}
+ else if (strcmp(option, "-tilemap") == 0)
+ {
+ if (i + 1 >= argc)
+ FATAL_ERROR("No tilemap value following \"-tilemap\".\n");
+ i++;
+ options.tilemapFilePath = argv[i];
+ }
+ else if (strcmp(option, "-affine") == 0)
+ {
+ options.isAffineMap = true;
+ }
else
{
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
@@ -138,13 +175,15 @@ void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **a
void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
- char *outputFileExtension = GetFileExtension(outputPath);
+ char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
int bitDepth = outputFileExtension[0] - '0';
struct PngToGbaOptions options;
options.numTiles = 0;
options.bitDepth = bitDepth;
options.metatileWidth = 1;
options.metatileHeight = 1;
+ options.tilemapFilePath = NULL;
+ options.isAffineMap = false;
for (int i = 3; i < argc; i++)
{
@@ -198,9 +237,17 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a
ConvertPngToGba(inputPath, outputPath, &options);
}
+void HandlePngToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
+{
+ struct Palette palette = {};
+
+ ReadPngPalette(inputPath, &palette);
+ WriteJascPalette(outputPath, &palette);
+}
+
void HandlePngToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
- struct Palette palette;
+ struct Palette palette = {};
ReadPngPalette(inputPath, &palette);
WriteGbaPalette(outputPath, &palette);
@@ -208,7 +255,7 @@ void HandlePngToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UN
void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
- struct Palette palette;
+ struct Palette palette = {};
ReadGbaPalette(inputPath, &palette);
WriteJascPalette(outputPath, &palette);
@@ -241,7 +288,7 @@ void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc,
}
}
- struct Palette palette;
+ struct Palette palette = {};
ReadJascPalette(inputPath, &palette);
@@ -254,6 +301,7 @@ void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc,
void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
ReadLatinFont(inputPath, &image);
WritePng(outputPath, &image);
@@ -264,6 +312,7 @@ void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNU
void HandlePngToLatinFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
image.bitDepth = 2;
@@ -276,6 +325,7 @@ void HandlePngToLatinFontCommand(char *inputPath, char *outputPath, int argc UNU
void HandleHalfwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
ReadHalfwidthJapaneseFont(inputPath, &image);
WritePng(outputPath, &image);
@@ -286,6 +336,7 @@ void HandleHalfwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath,
void HandlePngToHalfwidthJapaneseFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
image.bitDepth = 2;
@@ -298,6 +349,7 @@ void HandlePngToHalfwidthJapaneseFontCommand(char *inputPath, char *outputPath,
void HandleFullwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
ReadFullwidthJapaneseFont(inputPath, &image);
WritePng(outputPath, &image);
@@ -308,6 +360,7 @@ void HandleFullwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath,
void HandlePngToFullwidthJapaneseFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
{
struct Image image;
+ image.tilemap.data.affine = NULL; // initialize to NULL to avoid issues in FreeImage
image.bitDepth = 2;
@@ -483,6 +536,8 @@ void HandleHuffDecompressCommand(char *inputPath, char *outputPath, int argc UNU
int main(int argc, char **argv)
{
+ char converted = 0;
+
if (argc < 3)
FATAL_ERROR("Usage: gbagfx INPUT_PATH OUTPUT_PATH [options...]\n");
@@ -495,6 +550,7 @@ int main(int argc, char **argv)
{ "png", "4bpp", HandlePngToGbaCommand },
{ "png", "8bpp", HandlePngToGbaCommand },
{ "png", "gbapal", HandlePngToGbaPaletteCommand },
+ { "png", "pal", HandlePngToJascPaletteCommand },
{ "gbapal", "pal", HandleGbaToJascPaletteCommand },
{ "pal", "gbapal", HandleJascToGbaPaletteCommand },
{ "latfont", "png", HandleLatinFontToPngCommand },
@@ -514,14 +570,39 @@ int main(int argc, char **argv)
char *inputPath = argv[1];
char *outputPath = argv[2];
- char *inputFileExtension = GetFileExtension(inputPath);
- char *outputFileExtension = GetFileExtension(outputPath);
+ char *inputFileExtension = GetFileExtensionAfterDot(inputPath);
+ char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
if (inputFileExtension == NULL)
FATAL_ERROR("Input file \"%s\" has no extension.\n", inputPath);
if (outputFileExtension == NULL)
- FATAL_ERROR("Output file \"%s\" has no extension.\n", outputPath);
+ {
+ outputFileExtension = GetFileExtension(outputPath);
+
+ if (*outputFileExtension == '.')
+ outputFileExtension++;
+
+ if (*outputFileExtension == 0)
+ FATAL_ERROR("Output file \"%s\" has no extension.\n", outputPath);
+
+ size_t newOutputPathSize = strlen(inputPath) - strlen(inputFileExtension) + strlen(outputFileExtension);
+ outputPath = malloc(newOutputPathSize);
+
+ if (outputPath == NULL)
+ FATAL_ERROR("Failed to allocate memory for new output path.\n");
+
+ for (int i = 0; i < newOutputPathSize; i++)
+ {
+ outputPath[i] = inputPath[i];
+
+ if (outputPath[i] == '.')
+ {
+ strcpy(&outputPath[i + 1], outputFileExtension);
+ break;
+ }
+ }
+ }
for (int i = 0; handlers[i].function != NULL; i++)
{
@@ -529,9 +610,16 @@ int main(int argc, char **argv)
&& (handlers[i].outputFileExtension == NULL || strcmp(handlers[i].outputFileExtension, outputFileExtension) == 0))
{
handlers[i].function(inputPath, outputPath, argc, argv);
- return 0;
+ converted = 1;
+ break;
}
}
- FATAL_ERROR("Don't know how to convert \"%s\" to \"%s\".\n", inputPath, outputPath);
+ if (outputPath != argv[2])
+ free(outputPath);
+
+ if (!converted)
+ FATAL_ERROR("Don't know how to convert \"%s\" to \"%s\".\n", argv[1], argv[2]);
+
+ return 0;
}