diff options
-rw-r--r-- | tools/nitrogfx/gfx.c | 91 | ||||
-rw-r--r-- | tools/nitrogfx/gfx.h | 3 | ||||
-rw-r--r-- | tools/nitrogfx/jasc_pal.c | 7 | ||||
-rw-r--r-- | tools/nitrogfx/main.c | 53 | ||||
-rw-r--r-- | tools/nitrogfx/util.c | 24 | ||||
-rw-r--r-- | tools/nitrogfx/util.h | 2 |
6 files changed, 180 insertions, 0 deletions
diff --git a/tools/nitrogfx/gfx.c b/tools/nitrogfx/gfx.c index f927deed..ac156575 100644 --- a/tools/nitrogfx/gfx.c +++ b/tools/nitrogfx/gfx.c @@ -1,5 +1,6 @@ // Copyright (c) 2015 YamaArashi +#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> @@ -322,6 +323,41 @@ void ReadGbaPalette(char *path, struct Palette *palette) free(data); } +void ReadNtrPalette(char *path, struct Palette *palette) +{ + int fileSize; + unsigned char *data = ReadWholeFile(path, &fileSize); + + if (memcmp(data, "RLCN", 4) != 0 && memcmp(data, "RPCN", 4) != 0) //NCLR / NCPR + { + FATAL_ERROR("Not a valid NCLR or NCPR palette file.\n"); + } + + unsigned char *paletteHeader = data + 0x10; + + if (memcmp(paletteHeader, "TTLP", 4) != 0) + { + FATAL_ERROR("No valid PLTT file after NCLR header.\n"); + } + + if ((fileSize - 0x28) % 2 != 0) + FATAL_ERROR("The file size (%d) is not a multiple of 2.\n", fileSize); + + palette->numColors = (fileSize - 0x28) / 2; //remove header and divide by 2 + + unsigned char *paletteData = paletteHeader + 0x18; + + for (int i = 0; i < palette->numColors; i++) + { + uint16_t paletteEntry = (paletteData[i * 2 + 1] << 8) | paletteData[i * 2]; + palette->colors[i].red = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_RED(paletteEntry)); + palette->colors[i].green = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_GREEN(paletteEntry)); + palette->colors[i].blue = UPCONVERT_BIT_DEPTH(GET_GBA_PAL_BLUE(paletteEntry)); + } + + free(data); +} + void WriteGbaPalette(char *path, struct Palette *palette) { FILE *fp = fopen(path, "wb"); @@ -342,3 +378,58 @@ void WriteGbaPalette(char *path, struct Palette *palette) fclose(fp); } + +void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr) +{ + FILE *fp = fopen(path, "wb"); + + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for writing.\n", path); + + uint32_t size = palette->numColors * 2; + uint32_t extSize = size + (ncpr ? 0x10 : 0x18); + + //NCLR header + WriteGenericNtrHeader(fp, (ncpr ? "RPCN" : "RLCN"), extSize, !ncpr); + + unsigned char palHeader[0x18] = + { + 0x54, 0x54, 0x4C, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00 + }; + + //section size + palHeader[4] = extSize & 0xFF; + palHeader[5] = (extSize >> 8) & 0xFF; + palHeader[6] = (extSize >> 16) & 0xFF; + palHeader[7] = (extSize >> 24) & 0xFF; + + //bit depth + palHeader[8] = palette->bitDepth == 4 ? 0x03: 0x04; + + //size + palHeader[16] = size & 0xFF; + palHeader[17] = (size >> 8) & 0xFF; + palHeader[18] = (size >> 16) & 0xFF; + palHeader[19] = (size >> 24) & 0xFF; + + fwrite(palHeader, 1, 0x18, fp); + + unsigned char colours[palette->numColors * 2]; + //palette data + for (int i = 0; i < palette->numColors; i++) + { + unsigned char red = DOWNCONVERT_BIT_DEPTH(palette->colors[i].red); + unsigned char green = DOWNCONVERT_BIT_DEPTH(palette->colors[i].green); + unsigned char blue = DOWNCONVERT_BIT_DEPTH(palette->colors[i].blue); + + uint16_t paletteEntry = SET_GBA_PAL(red, green, blue); + + colours[i * 2] = paletteEntry & 0xFF; + colours[i * 2 + 1] = paletteEntry >> 8; + } + + fwrite(colours, 1, palette->numColors * 2, fp); + + fclose(fp); +} diff --git a/tools/nitrogfx/gfx.h b/tools/nitrogfx/gfx.h index 5355ced8..fc57380e 100644 --- a/tools/nitrogfx/gfx.h +++ b/tools/nitrogfx/gfx.h @@ -15,6 +15,7 @@ struct Color { struct Palette { struct Color colors[256]; int numColors; + int bitDepth; }; struct Image { @@ -31,6 +32,8 @@ void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors); void FreeImage(struct Image *image); void ReadGbaPalette(char *path, struct Palette *palette); +void ReadNtrPalette(char *path, struct Palette *palette); void WriteGbaPalette(char *path, struct Palette *palette); +void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr); #endif // GFX_H diff --git a/tools/nitrogfx/jasc_pal.c b/tools/nitrogfx/jasc_pal.c index e5ba9c3c..4f80f5d9 100644 --- a/tools/nitrogfx/jasc_pal.c +++ b/tools/nitrogfx/jasc_pal.c @@ -91,6 +91,8 @@ void ReadJascPalette(char *path, struct Palette *palette) if (palette->numColors < 1 || palette->numColors > 256) FATAL_ERROR("%d is an invalid number of colors. The number of colors must be in the range [1, 256].\n", palette->numColors); + palette->bitDepth = 4; + for (int i = 0; i < palette->numColors; i++) { ReadJascPaletteLine(fp, line); @@ -146,6 +148,11 @@ void ReadJascPalette(char *path, struct Palette *palette) palette->colors[i].red = red; palette->colors[i].green = green; palette->colors[i].blue = blue; + if (i >= 16) + { + if (red || green || blue) + palette->bitDepth = 8; + } } if (fgetc(fp) != EOF) diff --git a/tools/nitrogfx/main.c b/tools/nitrogfx/main.c index b9f4272c..7365cf39 100644 --- a/tools/nitrogfx/main.c +++ b/tools/nitrogfx/main.c @@ -214,6 +214,14 @@ void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc U WriteJascPalette(outputPath, &palette); } +void HandleNtrToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Palette palette; + + ReadNtrPalette(inputPath, &palette); + WriteJascPalette(outputPath, &palette); +} + void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc, char **argv) { int numColors = 0; @@ -251,6 +259,48 @@ void HandleJascToGbaPaletteCommand(char *inputPath, char *outputPath, int argc, WriteGbaPalette(outputPath, &palette); } +void HandleJascToNtrPaletteCommand(char *inputPath, char *outputPath, int argc, char **argv) +{ + int numColors = 0; + bool ncpr = false; + + for (int i = 3; i < argc; i++) + { + char *option = argv[i]; + + if (strcmp(option, "-num_colors") == 0) + { + if (i + 1 >= argc) + FATAL_ERROR("No number of colors following \"-num_colors\".\n"); + + i++; + + if (!ParseNumber(argv[i], NULL, 10, &numColors)) + FATAL_ERROR("Failed to parse number of colors.\n"); + + if (numColors < 1) + FATAL_ERROR("Number of colors must be positive.\n"); + } + else if (strcmp(option, "-ncpr") == 0) + { + ncpr = true; + } + else + { + FATAL_ERROR("Unrecognized option \"%s\".\n", option); + } + } + + struct Palette palette; + + ReadJascPalette(inputPath, &palette); + + if (numColors != 0) + palette.numColors = numColors; + + WriteNtrPalette(outputPath, &palette, ncpr); +} + void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) { struct Image image; @@ -496,7 +546,10 @@ int main(int argc, char **argv) { "png", "8bpp", HandlePngToGbaCommand }, { "png", "gbapal", HandlePngToGbaPaletteCommand }, { "gbapal", "pal", HandleGbaToJascPaletteCommand }, + { "NCLR", "pal", HandleNtrToJascPaletteCommand }, + { "NCPR", "pal", HandleNtrToJascPaletteCommand }, { "pal", "gbapal", HandleJascToGbaPaletteCommand }, + { "pal", "NCLR", HandleJascToNtrPaletteCommand }, { "latfont", "png", HandleLatinFontToPngCommand }, { "png", "latfont", HandlePngToLatinFontCommand }, { "hwjpnfont", "png", HandleHalfwidthJapaneseFontToPngCommand }, diff --git a/tools/nitrogfx/util.c b/tools/nitrogfx/util.c index 87abeb31..73a128a1 100644 --- a/tools/nitrogfx/util.c +++ b/tools/nitrogfx/util.c @@ -4,6 +4,7 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <stdint.h> #include <errno.h> #include <limits.h> #include "global.h" @@ -122,3 +123,26 @@ void WriteWholeFile(char *path, void *buffer, int bufferSize) fclose(fp); } + +void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder) +{ + unsigned char header[0x10] = + { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00 }; + //magic number + memcpy(header, magicNumber, 4); + + //byte order + if (!byteorder) + { + memset(header + 4, 0, 2); + } + + //size + size += 0x10; //add header size + header[8] = size & 0xFF; + header[9] = (size >> 8) & 0xFF; + header[10] = (size >> 16) & 0xFF; + header[11] = (size >> 24) & 0xFF; + + fwrite(header, 1, 0x10, fp); +} diff --git a/tools/nitrogfx/util.h b/tools/nitrogfx/util.h index 6d7a9c21..f181b66e 100644 --- a/tools/nitrogfx/util.h +++ b/tools/nitrogfx/util.h @@ -4,11 +4,13 @@ #define UTIL_H #include <stdbool.h> +#include <stdint.h> bool ParseNumber(char *s, char **end, int radix, int *intValue); char *GetFileExtension(char *path); unsigned char *ReadWholeFile(char *path, int *size); unsigned char *ReadWholeFileZeroPadded(char *path, int *size, int padAmount); void WriteWholeFile(char *path, void *buffer, int bufferSize); +void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder); #endif // UTIL_H |