Skip to content

Commit

Permalink
Merge pull request #202 from red031000/master
Browse files Browse the repository at this point in the history
conversion from 8bpp scanning mode, vram transfer, and mapping types
  • Loading branch information
adrienntindall authored Jul 27, 2023
2 parents 7b0be66 + 0b20e08 commit 8a33bc2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
82 changes: 77 additions & 5 deletions tools/nitrogfx/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,45 @@ static void ConvertFromTiles8Bpp(unsigned char *src, unsigned char *dest, int nu
}
}

static uint32_t ConvertFromScanned8Bpp(unsigned char *src, unsigned char *dest, int fileSize, bool invertColours, bool scanFrontToBack)
{
uint32_t encValue = 0;
if (scanFrontToBack) {
encValue = (src[1] << 8) | src[0];
for (int i = 0; i < fileSize; i += 2)
{
uint16_t val = src[i] | (src[i + 1] << 8);
val ^= (encValue & 0xFFFF);
src[i] = val;
src[i + 1] = val >> 8;
encValue = encValue * 1103515245;
encValue = encValue + 24691;
}
} else {
encValue = (src[fileSize - 1] << 8) | src[fileSize - 2];
for (int i = fileSize; i > 0; i -= 2)
{
uint16_t val = (src[i - 1] << 8) | src[i - 2];
val ^= (encValue & 0xFFFF);
src[i - 1] = (val >> 8);
src[i - 2] = val;
encValue = encValue * 1103515245;
encValue = encValue + 24691;
}
}
for (int i = 0; i < fileSize; i++)
{
unsigned char srcPixel = src[i];

if (invertColours) {
srcPixel = 255 - srcPixel;
}

dest[i] = srcPixel;
}
return encValue;
}

static void ConvertToTiles1Bpp(unsigned char *src, unsigned char *dest, int numTiles, int metatilesWide, int metatileWidth, int metatileHeight, bool invertColors)
{
int subTileX = 0;
Expand Down Expand Up @@ -393,7 +432,7 @@ uint32_t ReadNtrImage(char *path, int tilesWidth, int bitDepth, int metatileWidt
key = ConvertFromScanned4Bpp(imageData, image->pixels, fileSize - 0x30, invertColors, scanFrontToBack);
break;
case 8:
FATAL_ERROR("8bpp is not implemented yet\n");
key = ConvertFromScanned8Bpp(imageData, image->pixels, fileSize - 0x30, invertColors, scanFrontToBack);
break;
}
}
Expand Down Expand Up @@ -468,8 +507,8 @@ void WriteImage(char *path, int numTiles, int bitDepth, int metatileWidth, int m
}

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, uint32_t scanMode,
uint32_t key, bool wrongSize)
bool invertColors, bool clobberSize, bool byteOrder, bool version101, bool sopc, bool vram, uint32_t scanMode,
uint32_t mappingType, uint32_t key, bool wrongSize)
{
FILE *fp = fopen(path, "wb");

Expand Down Expand Up @@ -560,14 +599,47 @@ void WriteNtrImage(char *path, int numTiles, int bitDepth, int metatileWidth, in
charHeader[10] = 0xFF;
charHeader[11] = 0xFF;

charHeader[16] = 0x10; //seems to be set when size is clobbered
charHeader[16] = 0x10; //size clobbering implies mapping type is some variant of 1d - *should* have a mapping type that's not 0

if (mappingType == 0)
{
mappingType = 32; // if not specified assume that it is 32k
}
}

charHeader[12] = bitDepth == 4 ? 3 : 4;

if (mappingType != 0) {
uint32_t val = 0;
switch (mappingType) {
case 32:
val = 0;
break;
case 64:
val = 0x10;
break;
case 128:
val = 0x20;
break;
case 256:
val = 0x30;
break;
default:
FATAL_ERROR("Invalid mapping type %d\n", mappingType);
break;
}

charHeader[18] = val;
}

if (scanMode)
{
charHeader[20] = 1;
charHeader[20] = 1; //implies BMP
}

if (vram)
{
charHeader[21] = 1; //implies VRAM transfer
}

charHeader[24] = bufferSize & 0xFF;
Expand Down
4 changes: 2 additions & 2 deletions tools/nitrogfx/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void ReadImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int
uint32_t ReadNtrImage(char *path, int tilesWidth, int bitDepth, int metatileWidth, int metatileHeight, struct Image *image, bool invertColors, bool scanFrontToBack);
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, bool version101, bool sopc, uint32_t scanMode,
uint32_t key, bool wrongSize);
bool invertColors, bool clobberSize, bool byteOrder, bool version101, bool sopc, bool vram, uint32_t scanMode,
uint32_t mappingType, uint32_t key, bool wrongSize);
void FreeImage(struct Image *image);
void ReadGbaPalette(char *path, struct Palette *palette);
void ReadNtrPalette(char *path, struct Palette *palette, int bitdepth, int palIndex);
Expand Down
20 changes: 19 additions & 1 deletion tools/nitrogfx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void ConvertPngToNtr(char *inputPath, char *outputPath, struct PngToNtrOptions *

WriteNtrImage(outputPath, options->numTiles, image.bitDepth, options->metatileWidth, options->metatileHeight,
&image, !image.hasPalette, options->clobberSize, options->byteOrder, options->version101,
options->sopc, options->scanMode, key, options->wrongSize);
options->sopc, options->vramTransfer, options->scanMode, options->mappingType, key, options->wrongSize);

FreeImage(&image);
}
Expand Down Expand Up @@ -430,6 +430,8 @@ void HandlePngToNtrCommand(char *inputPath, char *outputPath, int argc, char **a
options.sopc = false;
options.scanMode = 0;
options.handleEmpty = false;
options.vramTransfer = false;
options.mappingType = 0;

for (int i = 3; i < argc; i++)
{
Expand Down Expand Up @@ -522,6 +524,22 @@ void HandlePngToNtrCommand(char *inputPath, char *outputPath, int argc, char **a
{
options.handleEmpty = true;
}
else if (strcmp(option, "-vram") == 0)
{
options.vramTransfer = true;
}
else if (strcmp(option, "-mappingtype") == 0) {
if (i + 1 >= argc)
FATAL_ERROR("No mapping type value following \"-mappingtype\".\n");

i++;

if (!ParseNumber(argv[i], NULL, 10, &options.mappingType))
FATAL_ERROR("Failed to parse mapping type.\n");

if (options.mappingType != 0 && options.mappingType != 32 && options.mappingType != 64 && options.mappingType != 128 && options.mappingType != 256)
FATAL_ERROR("bitdepth must be one of the following: 0, 32, 64, 128, or 256\n");
}
else
{
FATAL_ERROR("Unrecognized option \"%s\".\n", option);
Expand Down
2 changes: 2 additions & 0 deletions tools/nitrogfx/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ struct PngToNtrOptions {
uint32_t scanMode;
bool wrongSize;
bool handleEmpty;
bool vramTransfer;
int mappingType;
};

struct NtrToPngOptions {
Expand Down

0 comments on commit 8a33bc2

Please sign in to comment.