diff --git a/gfx.c b/gfx.c index 797ccad..3ab8158 100644 --- a/gfx.c +++ b/gfx.c @@ -1,5 +1,6 @@ // Copyright (c) 2015 YamaArashi, 2021-2025 red031000 +#include #include #include #include @@ -1015,7 +1016,36 @@ void ApplyCellsToImage(char *cellFilePath, struct Image *image, bool toPNG, bool FreeNCERCell(options); } -void WriteImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int rowsPerChunk, struct Image *image, bool invertColors) +void WriteEmbeddableHeader(char *path, void *buffer, int bufferSize, const char *embedName) +{ + unsigned char *buf = buffer; + + char *headerPath; + asprintf(&headerPath, "%s.h", path); + if (headerPath == NULL) + FATAL_ERROR("Failed to allocate embeddable header filepath.\n"); + + FILE *header = fopen(headerPath, "wb"); + if (header == NULL) + FATAL_ERROR("Failed to open output file %s\n", headerPath); + + fprintf(header, "#ifndef GUARD_EMBEDDABLE_%s_H\n", embedName); + fprintf(header, "#define GUARD_EMBEDDABLE_%s_H\n", embedName); + fprintf(header, "\n"); + fprintf(header, "__attribute__((aligned(4))) const u8 %s[] = {\n", embedName); + + for (int i = 0; i < bufferSize; i++) + fprintf(header, " 0x%02X,\n", buf[i]); + + fprintf(header, "};\n"); + fprintf(header, "\n"); + fprintf(header, "#endif // GUARD_EMBEDDABLE_%s_H\n", embedName); + + fclose(header); + free(headerPath); +} + +void WriteImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int rowsPerChunk, const char *embedName, struct Image *image, bool invertColors) { int tileSize = bitDepth * 8; // number of bytes per tile @@ -1062,6 +1092,7 @@ void WriteImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int ro } WriteWholeFile(path, buffer, bufferSize); + if (embedName != NULL) WriteEmbeddableHeader(path, buffer, bufferSize, embedName); free(buffer); } diff --git a/gfx.h b/gfx.h index fcbfcac..159a79a 100644 --- a/gfx.h +++ b/gfx.h @@ -53,7 +53,7 @@ struct Image { void ReadImage(char *path, int tilesWide, int bitDepth, int colsPerChunk, int rowsPerChunk, struct Image *image, bool invertColors); uint32_t ReadNtrImage(char *path, int tilesWide, int bitDepth, int colsPerChunk, int rowsPerChunk, struct Image *image, bool invertColors, uint32_t encodeMode, bool convertTo8Bpp, int palIndex, bool verbose); void ApplyCellsToImage(char *cellFilePath, struct Image *image, bool toPNG, bool snap, bool noSkip); -void WriteImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int rowsPerChunk, struct Image *image, bool invertColors); +void WriteImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int rowsPerChunk, const char *embedName, struct Image *image, bool invertColors); void WriteNtrImage(char *path, int numTiles, int bitDepth, int colsPerChunk, int rowsPerChunk, struct Image *image, bool invertColors, bool clobberSize, bool byteOrder, bool version101, bool sopc, bool vram, bool scan, uint32_t encodeMode, uint32_t mappingType, uint32_t key, bool wrongSize, bool convertTo4Bpp, int rotate); diff --git a/main.c b/main.c index 71957cb..179adcc 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include "global.h" @@ -121,7 +122,7 @@ void ConvertPngToGba(char *inputPath, char *outputPath, struct PngToGbaOptions * ReadPng(inputPath, &image); - WriteImage(outputPath, options->numTiles, options->bitDepth, options->colsPerChunk, options->rowsPerChunk, &image, !image.hasPalette); + WriteImage(outputPath, options->numTiles, options->bitDepth, options->colsPerChunk, options->rowsPerChunk, options->embedName, &image, !image.hasPalette); FreeImage(&image); } @@ -449,6 +450,7 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a options.bitDepth = bitDepth; options.colsPerChunk = 1; options.rowsPerChunk = 1; + options.embedName = NULL; for (int i = 3; i < argc; i++) { @@ -493,6 +495,14 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a if (options.rowsPerChunk < 1) FATAL_ERROR("rows per chunk must be positive.\n"); } + else if (strcmp(option, "-embed") == 0) + { + if (i + 1 >= argc) + FATAL_ERROR("No symbol name after following \"%s\".\n", option); + i++; + + options.embedName = argv[i]; + } else { FATAL_ERROR("Unrecognized option \"%s\".\n", option); diff --git a/options.h b/options.h index abe7a61..951bc23 100644 --- a/options.h +++ b/options.h @@ -21,6 +21,7 @@ struct PngToGbaOptions { int bitDepth; int colsPerChunk; int rowsPerChunk; + char *embedName; }; struct PngToNtrOptions {