diff options
author | YamaArashi <shadow962@live.com> | 2015-11-23 17:44:06 -0800 |
---|---|---|
committer | YamaArashi <shadow962@live.com> | 2015-11-23 17:44:06 -0800 |
commit | b951fb1113b9574002f5405ebcb4750c077746ff (patch) | |
tree | 5cfc5d5b9dec5525880ef1c9aebe083020c47937 /tools/gbagfx/main.c | |
parent | 22d87f81d7097f9d2eb79bd0c784f9c74d44a79c (diff) |
dumped fonts
Diffstat (limited to 'tools/gbagfx/main.c')
-rw-r--r-- | tools/gbagfx/main.c | 363 |
1 files changed, 178 insertions, 185 deletions
diff --git a/tools/gbagfx/main.c b/tools/gbagfx/main.c index 472a87f79..56a1c2c7b 100644 --- a/tools/gbagfx/main.c +++ b/tools/gbagfx/main.c @@ -9,25 +9,18 @@ #include "convert_png.h" #include "jasc_pal.h" #include "lz.h" +#include "font.h" -void ConvertToPng(char *imageFilePath, char *outputFilePath, char *paletteFilePath, bool isObject, int width) +struct CommandHandler { - struct Image image; - int bitDepth = 0; - - char *extension = GetFileExtension(imageFilePath); - - if (extension == NULL) - FATAL_ERROR("\"%s\" has no file extension.\n", imageFilePath); + const char *inputFileExtension; + const char *outputFileExtension; + void(*function)(char *inputPath, char *outputPath, int argc, char **argv); +}; - if (strcmp(extension, "1bpp") == 0) - bitDepth = 1; - else if (strcmp(extension, "4bpp") == 0) - bitDepth = 4; - else if (strcmp(extension, "8bpp") == 0) - bitDepth = 8; - else - FATAL_ERROR("Unexpected file extension \"%s\". Expected \"1bpp\", \"4bpp\", or \"8bpp\".\n", extension); +void ConvertGbaToPng(char *inputPath, char *outputPath, int width, int bitDepth, char *paletteFilePath, bool hasTransparency) +{ + struct Image image; if (paletteFilePath != NULL) { ReadGbaPalette(paletteFilePath, &image.palette); @@ -36,249 +29,249 @@ void ConvertToPng(char *imageFilePath, char *outputFilePath, char *paletteFilePa image.hasPalette = false; } - ReadImage(imageFilePath, width, bitDepth, &image, !image.hasPalette); - - image.isObject = isObject; + ReadImage(inputPath, width, bitDepth, &image, !image.hasPalette); - if (outputFilePath == NULL) { - ChangeFileExtension(imageFilePath, "png"); - outputFilePath = imageFilePath; - } + image.hasTransparency = hasTransparency; - WritePng(outputFilePath, &image); + WritePng(outputPath, &image); FreeImage(&image); } -void ConvertFromPng(char *imageFilePath, char *outputFilePath, int numTiles, int bitDepth) +void ConvertPngToGba(char *inputPath, char *outputPath, int numTiles, int bitDepth) { struct Image image; image.bitDepth = bitDepth; - ExpectFileExtension(imageFilePath, "png"); - - ReadPng(imageFilePath, &image); - - if (outputFilePath == NULL) { - char newExtension[5]; - snprintf(newExtension, 5, "%dbpp", bitDepth); - ChangeFileExtension(imageFilePath, newExtension); - outputFilePath = imageFilePath; - } + ReadPng(inputPath, &image); - WriteImage(outputFilePath, numTiles, bitDepth, &image, !image.hasPalette); + WriteImage(outputPath, numTiles, bitDepth, &image, !image.hasPalette); FreeImage(&image); } -void ConvertToJascPalette(char *paletteFilePath) +void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **argv) { - struct Palette palette; + char *inputFileExtension = GetFileExtension(inputPath); + int bitDepth = inputFileExtension[0] - '0'; + char *paletteFilePath; + bool hasPalette = false; + bool hasTransparency = false; + int width = 1; - ExpectFileExtension(paletteFilePath, "gbapal"); + for (int i = 3; i < argc; i++) { + char *option = argv[i]; - ReadGbaPalette(paletteFilePath, &palette); + if (strcmp(option, "-palette") == 0) { + if (i + 1 >= argc) + FATAL_ERROR("No palette file path following \"-palette\".\n"); - ChangeFileExtension(paletteFilePath, "pal"); + i++; - WriteJascPalette(paletteFilePath, &palette); -} + paletteFilePath = argv[i]; -void ConvertFromJascPalette(char *paletteFilePath) -{ - struct Palette palette; + hasPalette = true; + } else if (strcmp(option, "-object") == 0) { + hasTransparency = true; + } else if (strcmp(option, "-width") == 0) { + if (i + 1 >= argc) + FATAL_ERROR("No width following \"-width\".\n"); - ExpectFileExtension(paletteFilePath, "pal"); + i++; - ReadJascPalette(paletteFilePath, &palette); + if (!ParseNumber(argv[i], NULL, 10, &width)) + FATAL_ERROR("Failed to parse width.\n"); - ChangeFileExtension(paletteFilePath, "gbapal"); + if (width < 1) + FATAL_ERROR("Width must be positive.\n"); + } else { + FATAL_ERROR("Unrecognized option \"%s\".\n", option); + } + } - WriteGbaPalette(paletteFilePath, &palette); + ConvertGbaToPng(inputPath, outputPath, width, bitDepth, hasPalette ? paletteFilePath : NULL, hasTransparency); } -void LZCompressFile(char *path) +void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **argv) { - int fileSize; - unsigned char *buffer = ReadWholeFile(path, &fileSize); + char *outputFileExtension = GetFileExtension(outputPath); + int bitDepth = outputFileExtension[0] - '0'; + int numTiles = 0; - int compressedSize; - unsigned char *compressedData = LZCompress(buffer, fileSize, &compressedSize); + for (int i = 3; i < argc; i++) { + char *option = argv[i]; - free(buffer); + if (strcmp(option, "-num_tiles") == 0) { + if (i + 1 >= argc) + FATAL_ERROR("No number of tiles following \"-num_tiles\".\n"); - AddFileExtension(path, "lz"); + i++; - WriteWholeFile(path, compressedData, compressedSize); + if (!ParseNumber(argv[i], NULL, 10, &numTiles)) + FATAL_ERROR("Failed to parse number of tiles.\n"); - free(compressedData); + if (numTiles < 1) + FATAL_ERROR("Number of tiles must be positive.\n"); + } else { + FATAL_ERROR("Unrecognized option \"%s\".\n", option); + } + } + + ConvertPngToGba(inputPath, outputPath, numTiles, bitDepth); } -void LZDecompressFile(char *path) +void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) { - ExpectFileExtension(path, "lz"); - - int fileSize; - unsigned char *buffer = ReadWholeFile(path, &fileSize); - - int uncompressedSize; - unsigned char *uncompressedData = LZDecompress(buffer, fileSize, &uncompressedSize); - - free(buffer); - - RemoveFileExtension(path); - - WriteWholeFile(path, uncompressedData, uncompressedSize); + struct Palette palette; - free(uncompressedData); + ReadGbaPalette(inputPath, &palette); + WriteJascPalette(outputPath, &palette); } -int main(int argc, char **argv) +void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) { - if (argc < 2) - FATAL_ERROR("No args.\n"); - - char *command = argv[1]; - - if (strcmp(command, "png") == 0) { - if (argc < 3) - FATAL_ERROR("No image file path arg.\n"); - - CHECK_PATH_LENGTH(argv[2]); - - char imageFilePath[GBAGFX_MAX_PATH + 1]; - strcpy(imageFilePath, argv[2]); - - char *outputFilePath = NULL; - char paletteFilePath[GBAGFX_MAX_PATH + 1]; - bool hasPalette = false; - bool isObject = false; - int width = 1; - - for (int i = 3; i < argc; i++) { - char *option = argv[i]; - - if (strcmp(option, "-output") == 0) { - if (i + 1 >= argc) - FATAL_ERROR("No output file path following \"-output\".\n"); + struct Palette palette; - i++; + ReadJascPalette(inputPath, &palette); + WriteGbaPalette(outputPath, &palette); +} - outputFilePath = argv[i]; - } else if (strcmp(option, "-palette") == 0) { - if (i + 1 >= argc) - FATAL_ERROR("No palette file path following \"-palette\".\n"); +void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - i++; + ReadLatinFont(inputPath, &image); + WritePng(outputPath, &image); - CHECK_PATH_LENGTH(argv[i]); + FreeImage(&image); +} - strcpy(paletteFilePath, argv[i]); +void HandlePngToLatinFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - hasPalette = true; - } else if (strcmp(option, "-object") == 0) { - isObject = true; - } else if (strcmp(option, "-width") == 0) { - if (i + 1 >= argc) - FATAL_ERROR("No width following \"-width\".\n"); + image.bitDepth = 2; - i++; + ReadPng(inputPath, &image); + WriteLatinFont(outputPath, &image); - if (!ParseNumber(argv[i], NULL, 10, &width)) - FATAL_ERROR("Failed to parse width.\n"); + FreeImage(&image); +} - if (width < 1) - FATAL_ERROR("Width must be positive.\n"); - } else { - FATAL_ERROR("Unrecognized option \"%s\".\n", option); - } - } +void HandleHalfwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - ConvertToPng(imageFilePath, outputFilePath, hasPalette ? paletteFilePath : NULL, isObject, width); - } else if (strcmp(command, "1bpp") == 0 || strcmp(command, "4bpp") == 0 || strcmp(command, "8bpp") == 0) { - if (argc < 3) - FATAL_ERROR("No image file path arg.\n"); + ReadHalfwidthJapaneseFont(inputPath, &image); + WritePng(outputPath, &image); - CHECK_PATH_LENGTH(argv[2]); + FreeImage(&image); +} - char imageFilePath[GBAGFX_MAX_PATH + 1]; - strcpy(imageFilePath, argv[2]); +void HandlePngToHalfwidthJapaneseFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - char *outputFilePath = NULL; - int numTiles = 0; - int bitDepth = command[0] - '0'; + image.bitDepth = 2; - for (int i = 3; i < argc; i++) { - char *option = argv[i]; + ReadPng(inputPath, &image); + WriteHalfwidthJapaneseFont(outputPath, &image); - if (strcmp(option, "-output") == 0) { - if (i + 1 >= argc) - FATAL_ERROR("No output file path following \"-output\".\n"); + FreeImage(&image); +} - i++; +void HandleFullwidthJapaneseFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - outputFilePath = argv[i]; - } else if (strcmp(option, "-num_tiles") == 0) { - if (i + 1 >= argc) - FATAL_ERROR("No number of tiles following \"-num_tiles\".\n"); + ReadFullwidthJapaneseFont(inputPath, &image); + WritePng(outputPath, &image); - i++; + FreeImage(&image); +} - if (!ParseNumber(argv[i], NULL, 10, &numTiles)) - FATAL_ERROR("Failed to parse number of tiles.\n"); +void HandlePngToFullwidthJapaneseFontCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Image image; - if (numTiles < 1) - FATAL_ERROR("Number of tiles must be positive.\n"); - } else { - FATAL_ERROR("Unrecognized option \"%s\".\n", option); - } - } + image.bitDepth = 2; - ConvertFromPng(imageFilePath, outputFilePath, numTiles, bitDepth); - } else if (strcmp(command, "pal") == 0) { - if (argc < 3) - FATAL_ERROR("No palette file path arg.\n"); + ReadPng(inputPath, &image); + WriteFullwidthJapaneseFont(outputPath, &image); - CHECK_PATH_LENGTH(argv[2]); + FreeImage(&image); +} - char paletteFilePath[GBAGFX_MAX_PATH + 1]; - strcpy(paletteFilePath, argv[2]); +void HandleLZCompressCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + int fileSize; + unsigned char *buffer = ReadWholeFile(inputPath, &fileSize); - ConvertToJascPalette(paletteFilePath); - } else if (strcmp(command, "gbapal") == 0) { - if (argc < 3) - FATAL_ERROR("No palette file path arg.\n"); + int compressedSize; + unsigned char *compressedData = LZCompress(buffer, fileSize, &compressedSize); - CHECK_PATH_LENGTH(argv[2]); + free(buffer); - char paletteFilePath[GBAGFX_MAX_PATH + 1]; - strcpy(paletteFilePath, argv[2]); + WriteWholeFile(outputPath, compressedData, compressedSize); - ConvertFromJascPalette(paletteFilePath); - } else if (strcmp(command, "lz") == 0) { - if (argc < 3) - FATAL_ERROR("No file path arg.\n"); + free(compressedData); +} - CHECK_PATH_LENGTH(argv[2]); +void HandleLZDecompressCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + int fileSize; + unsigned char *buffer = ReadWholeFile(inputPath, &fileSize); - char path[GBAGFX_MAX_PATH + 1]; - strcpy(path, argv[2]); + int uncompressedSize; + unsigned char *uncompressedData = LZDecompress(buffer, fileSize, &uncompressedSize); - LZCompressFile(path); - } else if (strcmp(command, "unlz") == 0) { - if (argc < 3) - FATAL_ERROR("No file path arg.\n"); + free(buffer); - CHECK_PATH_LENGTH(argv[2]); + WriteWholeFile(outputPath, uncompressedData, uncompressedSize); - char path[GBAGFX_MAX_PATH + 1]; - strcpy(path, argv[2]); + free(uncompressedData); +} - LZDecompressFile(path); - } else { - FATAL_ERROR("Unrecognized command \"%s\".\n", command); +int main(int argc, char **argv) +{ + if (argc < 3) + FATAL_ERROR("Usage: gbagfx INPUT_PATH OUTPUT_PATH [options...]\n"); + + struct CommandHandler handlers[] = + { + { "1bpp", "png", HandleGbaToPngCommand }, + { "4bpp", "png", HandleGbaToPngCommand }, + { "8bpp", "png", HandleGbaToPngCommand }, + { "png", "1bpp", HandlePngToGbaCommand }, + { "png", "4bpp", HandlePngToGbaCommand }, + { "png", "8bpp", HandlePngToGbaCommand }, + { "gbapal", "pal", HandleGbaToJascPaletteCommand }, + { "pal", "gbapal", HandleJascToGbaPaletteCommand }, + { "latfont", "png", HandleLatinFontToPngCommand }, + { "png", "latfont", HandlePngToLatinFontCommand }, + { "hwjpnfont", "png", HandleHalfwidthJapaneseFontToPngCommand }, + { "png", "hwjpnfont", HandlePngToHalfwidthJapaneseFontCommand }, + { "fwjpnfont", "png", HandleFullwidthJapaneseFontToPngCommand }, + { "png", "fwjpnfont", HandlePngToFullwidthJapaneseFontCommand }, + { NULL, "lz", HandleLZCompressCommand }, + { "lz", NULL, HandleLZDecompressCommand }, + { NULL, NULL, NULL } + }; + + char *inputPath = argv[1]; + char *outputPath = argv[2]; + + for (int i = 0; handlers[i].function != NULL; i++) { + char *inputFileExtension = GetFileExtension(inputPath); + char *outputFileExtension = GetFileExtension(outputPath); + + if ((handlers[i].inputFileExtension == NULL || strcmp(handlers[i].inputFileExtension, inputFileExtension) == 0) + && (handlers[i].outputFileExtension == NULL || strcmp(handlers[i].outputFileExtension, outputFileExtension) == 0)) { + handlers[i].function(inputPath, outputPath, argc, argv); + return 0; + } } - return 0; + FATAL_ERROR("Don't know how to convert \"%s\" to \"%s\".\n", inputPath, outputPath); } |