Sync -num_tiles flags
This commit is contained in:
+26
-6
@@ -397,7 +397,7 @@ void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int
|
|||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors)
|
void WriteImage(char *path, enum NumTilesMode numTilesMode, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors)
|
||||||
{
|
{
|
||||||
int tileSize = bitDepth * 8;
|
int tileSize = bitDepth * 8;
|
||||||
|
|
||||||
@@ -424,7 +424,8 @@ void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int m
|
|||||||
FATAL_ERROR("The specified number of tiles (%d) is greater than the maximum possible value (%d).\n", numTiles, maxNumTiles);
|
FATAL_ERROR("The specified number of tiles (%d) is greater than the maximum possible value (%d).\n", numTiles, maxNumTiles);
|
||||||
|
|
||||||
int bufferSize = numTiles * tileSize;
|
int bufferSize = numTiles * tileSize;
|
||||||
unsigned char *buffer = malloc(bufferSize);
|
int maxBufferSize = maxNumTiles * tileSize;
|
||||||
|
unsigned char *buffer = malloc(maxBufferSize);
|
||||||
|
|
||||||
if (buffer == NULL)
|
if (buffer == NULL)
|
||||||
FATAL_ERROR("Failed to allocate memory for pixels.\n");
|
FATAL_ERROR("Failed to allocate memory for pixels.\n");
|
||||||
@@ -433,17 +434,36 @@ void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int m
|
|||||||
|
|
||||||
switch (bitDepth) {
|
switch (bitDepth) {
|
||||||
case 1:
|
case 1:
|
||||||
ConvertToTiles1Bpp(image->pixels, buffer, numTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
ConvertToTiles1Bpp(image->pixels, buffer, maxNumTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
ConvertToTiles4Bpp(image->pixels, buffer, numTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
ConvertToTiles4Bpp(image->pixels, buffer, maxNumTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
ConvertToTiles8Bpp(image->pixels, buffer, numTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
ConvertToTiles8Bpp(image->pixels, buffer, maxNumTiles, metatilesWide, metatileWidth, metatileHeight, invertColors);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteWholeFile(path, buffer, bufferSize);
|
bool zeroPadded = true;
|
||||||
|
for (int i = bufferSize; i < maxBufferSize && zeroPadded; i++) {
|
||||||
|
if (buffer[i] != 0)
|
||||||
|
{
|
||||||
|
switch (numTilesMode)
|
||||||
|
{
|
||||||
|
case NUM_TILES_IGNORE:
|
||||||
|
break;
|
||||||
|
case NUM_TILES_WARN:
|
||||||
|
fprintf(stderr, "Ignoring -num_tiles %d because tile %d contains non-transparent pixels.\n", numTiles, 1 + i / tileSize);
|
||||||
|
zeroPadded = false;
|
||||||
|
break;
|
||||||
|
case NUM_TILES_ERROR:
|
||||||
|
FATAL_ERROR("Tile %d contains non-transparent pixels.\n", 1 + i / tileSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteWholeFile(path, buffer, zeroPadded ? bufferSize : maxBufferSize);
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-1
@@ -44,8 +44,14 @@ struct Image {
|
|||||||
bool isAffine;
|
bool isAffine;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum NumTilesMode {
|
||||||
|
NUM_TILES_IGNORE,
|
||||||
|
NUM_TILES_WARN,
|
||||||
|
NUM_TILES_ERROR,
|
||||||
|
};
|
||||||
|
|
||||||
void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors);
|
void ReadImage(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 WriteImage(char *path, enum NumTilesMode numTilesMode, int numTiles, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors);
|
||||||
void FreeImage(struct Image *image);
|
void FreeImage(struct Image *image);
|
||||||
void ReadGbaPalette(char *path, struct Palette *palette);
|
void ReadGbaPalette(char *path, struct Palette *palette);
|
||||||
void WriteGbaPalette(char *path, struct Palette *palette);
|
void WriteGbaPalette(char *path, struct Palette *palette);
|
||||||
|
|||||||
+8
-1
@@ -77,7 +77,7 @@ void ConvertPngToGba(char *inputPath, char *outputPath, struct PngToGbaOptions *
|
|||||||
|
|
||||||
ReadPng(inputPath, &image);
|
ReadPng(inputPath, &image);
|
||||||
|
|
||||||
WriteImage(outputPath, options->numTiles, options->bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette);
|
WriteImage(outputPath, options->numTilesMode, options->numTiles, options->bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette);
|
||||||
|
|
||||||
FreeImage(&image);
|
FreeImage(&image);
|
||||||
}
|
}
|
||||||
@@ -179,6 +179,7 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a
|
|||||||
char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
|
char *outputFileExtension = GetFileExtensionAfterDot(outputPath);
|
||||||
int bitDepth = outputFileExtension[0] - '0';
|
int bitDepth = outputFileExtension[0] - '0';
|
||||||
struct PngToGbaOptions options;
|
struct PngToGbaOptions options;
|
||||||
|
options.numTilesMode = NUM_TILES_IGNORE;
|
||||||
options.numTiles = 0;
|
options.numTiles = 0;
|
||||||
options.bitDepth = bitDepth;
|
options.bitDepth = bitDepth;
|
||||||
options.metatileWidth = 1;
|
options.metatileWidth = 1;
|
||||||
@@ -203,6 +204,12 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a
|
|||||||
if (options.numTiles < 1)
|
if (options.numTiles < 1)
|
||||||
FATAL_ERROR("Number of tiles must be positive.\n");
|
FATAL_ERROR("Number of tiles must be positive.\n");
|
||||||
}
|
}
|
||||||
|
else if (strcmp(option, "-Wnum_tiles") == 0) {
|
||||||
|
options.numTilesMode = NUM_TILES_WARN;
|
||||||
|
}
|
||||||
|
else if (strcmp(option, "-Werror=num_tiles") == 0) {
|
||||||
|
options.numTilesMode = NUM_TILES_ERROR;
|
||||||
|
}
|
||||||
else if (strcmp(option, "-mwidth") == 0)
|
else if (strcmp(option, "-mwidth") == 0)
|
||||||
{
|
{
|
||||||
if (i + 1 >= argc)
|
if (i + 1 >= argc)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#define OPTIONS_H
|
#define OPTIONS_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include "gfx.h"
|
||||||
|
|
||||||
struct GbaToPngOptions {
|
struct GbaToPngOptions {
|
||||||
char *paletteFilePath;
|
char *paletteFilePath;
|
||||||
@@ -18,6 +19,7 @@ struct GbaToPngOptions {
|
|||||||
|
|
||||||
struct PngToGbaOptions {
|
struct PngToGbaOptions {
|
||||||
int numTiles;
|
int numTiles;
|
||||||
|
enum NumTilesMode numTilesMode;
|
||||||
int bitDepth;
|
int bitDepth;
|
||||||
int metatileWidth;
|
int metatileWidth;
|
||||||
int metatileHeight;
|
int metatileHeight;
|
||||||
|
|||||||
Reference in New Issue
Block a user