summaryrefslogtreecommitdiff
path: root/tools/nitrogfx
diff options
context:
space:
mode:
Diffstat (limited to 'tools/nitrogfx')
-rw-r--r--tools/nitrogfx/gfx.c46
-rw-r--r--tools/nitrogfx/gfx.h6
-rw-r--r--tools/nitrogfx/main.c70
-rw-r--r--tools/nitrogfx/options.h2
-rw-r--r--tools/nitrogfx/util.c11
-rw-r--r--tools/nitrogfx/util.h2
6 files changed, 115 insertions, 22 deletions
diff --git a/tools/nitrogfx/gfx.c b/tools/nitrogfx/gfx.c
index 3315e00e..8f943698 100644
--- a/tools/nitrogfx/gfx.c
+++ b/tools/nitrogfx/gfx.c
@@ -359,7 +359,7 @@ void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int m
free(buffer);
}
-void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors, bool clobberSize, bool byteOrder)
+void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors, bool clobberSize, bool byteOrder, bool version101, bool sopc)
{
FILE *fp = fopen(path, "wb");
@@ -407,7 +407,7 @@ void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, in
break;
}
- WriteGenericNtrHeader(fp, "RGCN", bufferSize + 0x20, byteOrder);
+ WriteGenericNtrHeader(fp, "RGCN", bufferSize + (sopc ? 0x30 : 0x20), byteOrder, version101, sopc ? 2 : 1);
unsigned char charHeader[0x20] = { 0x52, 0x41, 0x48, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00 };
@@ -419,11 +419,11 @@ void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, in
if (!clobberSize)
{
- charHeader[8] = numTiles & 0xFF;
- charHeader[9] = (numTiles >> 8) & 0xFF;
+ charHeader[8] = tilesHeight & 0xFF;
+ charHeader[9] = (tilesHeight >> 8) & 0xFF;
- charHeader[10] = tileSize & 0xFF;
- charHeader[11] = (tileSize >> 8) & 0xFF;
+ charHeader[10] = tilesWidth & 0xFF;
+ charHeader[11] = (tilesWidth >> 8) & 0xFF;
}
else
{
@@ -446,6 +446,19 @@ void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, in
fwrite(pixelBuffer, 1, bufferSize, fp);
+ if (sopc)
+ {
+ unsigned char sopcBuffer[0x10] = { 0x53, 0x4F, 0x50, 0x43, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+ sopcBuffer[12] = tilesWidth & 0xFF;
+ sopcBuffer[13] = (tilesWidth >> 8) & 0xFF;
+
+ sopcBuffer[14] = tilesHeight & 0xFF;
+ sopcBuffer[15] = (tilesHeight >> 8) & 0xFF;
+
+ fwrite(sopcBuffer, 1, 0x10, fp);
+ }
+
free(pixelBuffer);
fclose(fp);
}
@@ -476,7 +489,7 @@ void ReadGbaPalette(char *path, struct Palette *palette)
free(data);
}
-void ReadNtrPalette(char *path, struct Palette *palette)
+void ReadNtrPalette(char *path, struct Palette *palette, int bitdepth)
{
int fileSize;
unsigned char *data = ReadWholeFile(path, &fileSize);
@@ -498,7 +511,9 @@ void ReadNtrPalette(char *path, struct Palette *palette)
palette->bitDepth = paletteHeader[0x8] == 3 ? 4 : 8;
- palette->numColors = palette->bitDepth == 4 ? 16 : 256; //remove header and divide by 2
+ bitdepth = bitdepth ? bitdepth : palette->bitDepth;
+
+ palette->numColors = bitdepth == 4 ? 16 : 256; //remove header and divide by 2
unsigned char *paletteData = paletteHeader + 0x18;
@@ -534,7 +549,7 @@ void WriteGbaPalette(char *path, struct Palette *palette)
fclose(fp);
}
-void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr)
+void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr, bool ir, int bitdepth)
{
FILE *fp = fopen(path, "wb");
@@ -545,7 +560,7 @@ void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr)
uint32_t extSize = size + (ncpr ? 0x10 : 0x18);
//NCLR header
- WriteGenericNtrHeader(fp, (ncpr ? "RPCN" : "RLCN"), extSize, !ncpr);
+ WriteGenericNtrHeader(fp, (ncpr ? "RPCN" : "RLCN"), extSize, !ncpr, false, 1);
unsigned char palHeader[0x18] =
{
@@ -561,8 +576,11 @@ void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr)
if (!palette->bitDepth)
palette->bitDepth = 4;
+
+ bitdepth = bitdepth ? bitdepth : palette->bitDepth;
+
//bit depth
- palHeader[8] = palette->bitDepth == 4 ? 0x03: 0x04;
+ palHeader[8] = bitdepth == 4 ? 0x03: 0x04;
//size
palHeader[16] = size & 0xFF;
@@ -594,6 +612,12 @@ void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr)
}
}
+ if (ir)
+ {
+ colours[510] = 'I';
+ colours[511] = 'R';
+ }
+
fwrite(colours, 1, 256 * 2, fp);
fclose(fp);
diff --git a/tools/nitrogfx/gfx.h b/tools/nitrogfx/gfx.h
index da56c5c5..e5189100 100644
--- a/tools/nitrogfx/gfx.h
+++ b/tools/nitrogfx/gfx.h
@@ -31,11 +31,11 @@ struct Image {
void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors);
void ReadNtrImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors);
void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors);
-void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors, bool clobberSize, bool byteOrder);
+void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors, bool clobberSize, bool byteOrder, bool version101, bool sopc);
void FreeImage(struct Image *image);
void ReadGbaPalette(char *path, struct Palette *palette);
-void ReadNtrPalette(char *path, struct Palette *palette);
+void ReadNtrPalette(char *path, struct Palette *palette, int bitdepth);
void WriteGbaPalette(char *path, struct Palette *palette);
-void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr);
+void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr, bool ir, int bitdepth);
#endif // GFX_H
diff --git a/tools/nitrogfx/main.c b/tools/nitrogfx/main.c
index 2f5b2735..188bdf0a 100644
--- a/tools/nitrogfx/main.c
+++ b/tools/nitrogfx/main.c
@@ -50,7 +50,7 @@ void ConvertNtrToPng(char *inputPath, char *outputPath, struct GbaToPngOptions *
if (options->paletteFilePath != NULL)
{
- ReadNtrPalette(options->paletteFilePath, &image.palette);
+ ReadNtrPalette(options->paletteFilePath, &image.palette, options->bitDepth);
image.hasPalette = true;
}
else
@@ -88,7 +88,7 @@ void ConvertPngToNtr(char *inputPath, char *outputPath, struct PngToNtrOptions *
ReadPng(inputPath, &image);
- WriteNtrImage(outputPath, options->numTiles, options->bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette, options->clobberSize, options->byteOrder);
+ WriteNtrImage(outputPath, options->numTiles, image.bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette, options->clobberSize, options->byteOrder, options->version101, options->sopc);
FreeImage(&image);
}
@@ -320,6 +320,8 @@ void HandlePngToNtrCommand(char *inputPath, char *outputPath, int argc, char **a
options.metatileHeight = 1;
options.clobberSize = false;
options.byteOrder = true;
+ options.version101 = false;
+ options.sopc = false;
for (int i = 3; i < argc; i++)
{
@@ -385,6 +387,14 @@ void HandlePngToNtrCommand(char *inputPath, char *outputPath, int argc, char **a
{
options.byteOrder = false;
}
+ else if (strcmp(option, "-version101") == 0)
+ {
+ options.version101 = true;
+ }
+ else if (strcmp(option, "-sopc") == 0)
+ {
+ options.sopc = true;
+ }
else
{
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
@@ -406,6 +416,7 @@ void HandlePngToNtrPaletteCommand(char *inputPath, char *outputPath, int argc, c
{
struct Palette palette;
bool ncpr = false;
+ bool ir = false;
for (int i = 3; i < argc; i++)
{
@@ -415,6 +426,10 @@ void HandlePngToNtrPaletteCommand(char *inputPath, char *outputPath, int argc, c
{
ncpr = true;
}
+ else if (strcmp(option, "-ir") == 0)
+ {
+ ir = true;
+ }
else
{
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
@@ -422,7 +437,7 @@ void HandlePngToNtrPaletteCommand(char *inputPath, char *outputPath, int argc, c
}
ReadPngPalette(inputPath, &palette);
- WriteNtrPalette(outputPath, &palette, ncpr);
+ WriteNtrPalette(outputPath, &palette, ncpr, ir, palette.bitDepth);
}
void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
@@ -433,11 +448,35 @@ void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc U
WriteJascPalette(outputPath, &palette);
}
-void HandleNtrToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
+void HandleNtrToJascPaletteCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
struct Palette palette;
+ int bitdepth = 0;
+
+ for (int i = 3; i < argc; i++)
+ {
+ char *option = argv[i];
+
+ if (strcmp(option, "-bitdepth") == 0)
+ {
+ if (i + 1 >= argc)
+ FATAL_ERROR("No bitdepth following \"-bitdepth\".\n");
+
+ i++;
+
+ if (!ParseNumber(argv[i], NULL, 10, &bitdepth))
+ FATAL_ERROR("Failed to parse bitdepth.\n");
- ReadNtrPalette(inputPath, &palette);
+ if (bitdepth != 4 && bitdepth != 8)
+ FATAL_ERROR("Bitdepth must be 4 or 8.\n");
+ }
+ else
+ {
+ FATAL_ERROR("Unrecognized option \"%s\".\n", option);
+ }
+ }
+
+ ReadNtrPalette(inputPath, &palette, bitdepth);
WriteJascPalette(outputPath, &palette);
}
@@ -482,6 +521,8 @@ void HandleJascToNtrPaletteCommand(char *inputPath, char *outputPath, int argc,
{
int numColors = 0;
bool ncpr = false;
+ bool ir = false;
+ int bitdepth = 0;
for (int i = 3; i < argc; i++)
{
@@ -500,10 +541,27 @@ void HandleJascToNtrPaletteCommand(char *inputPath, char *outputPath, int argc,
if (numColors < 1)
FATAL_ERROR("Number of colors must be positive.\n");
}
+ if (strcmp(option, "-bitdepth") == 0)
+ {
+ if (i + 1 >= argc)
+ FATAL_ERROR("No bitdepth following \"-bitdepth\".\n");
+
+ i++;
+
+ if (!ParseNumber(argv[i], NULL, 10, &bitdepth))
+ FATAL_ERROR("Failed to parse bitdepth.\n");
+
+ if (bitdepth != 4 && bitdepth != 8)
+ FATAL_ERROR("Bitdepth must be 4 or 8.\n");
+ }
else if (strcmp(option, "-ncpr") == 0)
{
ncpr = true;
}
+ else if (strcmp(option, "-ir") == 0)
+ {
+ ir = true;
+ }
else
{
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
@@ -517,7 +575,7 @@ void HandleJascToNtrPaletteCommand(char *inputPath, char *outputPath, int argc,
if (numColors != 0)
palette.numColors = numColors;
- WriteNtrPalette(outputPath, &palette, ncpr);
+ WriteNtrPalette(outputPath, &palette, ncpr, ir, bitdepth);
}
void HandleLatinFontToPngCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED)
diff --git a/tools/nitrogfx/options.h b/tools/nitrogfx/options.h
index 8376348f..dc90a2a2 100644
--- a/tools/nitrogfx/options.h
+++ b/tools/nitrogfx/options.h
@@ -28,6 +28,8 @@ struct PngToNtrOptions {
int metatileHeight;
bool clobberSize;
bool byteOrder;
+ bool version101;
+ bool sopc;
};
diff --git a/tools/nitrogfx/util.c b/tools/nitrogfx/util.c
index 73a128a1..7dc4ca89 100644
--- a/tools/nitrogfx/util.c
+++ b/tools/nitrogfx/util.c
@@ -124,13 +124,18 @@ void WriteWholeFile(char *path, void *buffer, int bufferSize)
fclose(fp);
}
-void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder)
+void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder, bool version101, uint16_t sectionCount)
{
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);
+ if (version101)
+ {
+ header[6] = 0x01;
+ }
+
//byte order
if (!byteorder)
{
@@ -144,5 +149,9 @@ void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, boo
header[10] = (size >> 16) & 0xFF;
header[11] = (size >> 24) & 0xFF;
+ //section count
+ header[14] = sectionCount & 0xFF;
+ header[15] = (sectionCount >> 8) & 0xFF;
+
fwrite(header, 1, 0x10, fp);
}
diff --git a/tools/nitrogfx/util.h b/tools/nitrogfx/util.h
index f181b66e..b757aa4d 100644
--- a/tools/nitrogfx/util.h
+++ b/tools/nitrogfx/util.h
@@ -11,6 +11,6 @@ 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);
+void WriteGenericNtrHeader(FILE* fp, const char* magicNumber, uint32_t size, bool byteorder, bool version101, uint16_t sectionCount);
#endif // UTIL_H