Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion gfx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2015 YamaArashi, 2021-2025 red031000

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "global.h"
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct PngToGbaOptions {
int bitDepth;
int colsPerChunk;
int rowsPerChunk;
char *embedName;
};

struct PngToNtrOptions {
Expand Down