some cleanup

This commit is contained in:
red031000 2023-07-03 18:15:33 +01:00
parent a4e194fc9b
commit 8906c349fd
No known key found for this signature in database
GPG Key ID: D27E50C050AE0CE1
4 changed files with 347 additions and 290 deletions

14
gfx.c
View File

@ -354,10 +354,18 @@ uint32_t ReadNtrImage(char *path, int tilesWidth, int bitDepth, int metatileWidt
int tileSize = bitDepth * 8;
int numTiles = (charHeader[0x18] + (charHeader[0x19] << 8) + (charHeader[0x1A] << 16) + (charHeader[0x1B] << 24))
/ (64 / (8 / bitDepth));
if (tilesWidth == 0) {
tilesWidth = ReadS16(charHeader, 0xA);
if (tilesWidth < 0) {
tilesWidth = 1;
}
}
int tilesHeight = (numTiles + tilesWidth - 1) / tilesWidth;
int numTiles = ReadS32(charHeader, 0x18) / (64 / (8 / bitDepth));
int tilesHeight = ReadS16(charHeader, 0x8);
if (tilesHeight < 0)
tilesHeight = (numTiles + tilesWidth - 1) / tilesWidth;
if (tilesWidth % metatileWidth != 0)
FATAL_ERROR("The width in tiles (%d) isn't a multiple of the specified metatile width (%d)", tilesWidth, metatileWidth);

14
main.c
View File

@ -1,5 +1,6 @@
// Copyright (c) 2015 YamaArashi, 2021-2023 red031000
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
@ -171,7 +172,10 @@ void HandleGbaToPngCommand(char *inputPath, char *outputPath, int argc, char **a
char *inputFileExtension = GetFileExtension(inputPath);
struct GbaToPngOptions options;
options.paletteFilePath = NULL;
if (isdigit(inputFileExtension[0]))
options.bitDepth = inputFileExtension[0] - '0';
else
options.bitDepth = 4;
options.hasTransparency = false;
options.width = 1;
options.metatileWidth = 1;
@ -250,7 +254,7 @@ void HandleNtrToPngCommand(char *inputPath, char *outputPath, int argc, char **a
struct NtrToPngOptions options;
options.paletteFilePath = NULL;
options.hasTransparency = false;
options.width = 1;
options.width = 0;
options.metatileWidth = 1;
options.metatileHeight = 1;
options.palIndex = 1;
@ -340,7 +344,7 @@ void HandleNtrToPngCommand(char *inputPath, char *outputPath, int argc, char **a
}
}
if (options.metatileWidth > options.width)
if (options.width != 0 && options.metatileWidth > options.width)
options.width = options.metatileWidth;
ConvertNtrToPng(inputPath, outputPath, &options);
@ -349,7 +353,11 @@ void HandleNtrToPngCommand(char *inputPath, char *outputPath, int argc, char **a
void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **argv)
{
char *outputFileExtension = GetFileExtension(outputPath);
int bitDepth = outputFileExtension[0] - '0';
int bitDepth;
if (strcmp(outputFileExtension, "nbfc") == 0)
bitDepth = 4;
else
bitDepth = outputFileExtension[0] - '0';
struct PngToGbaOptions options;
options.numTiles = 0;
options.bitDepth = bitDepth;

41
util.h
View File

@ -13,4 +13,45 @@ 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, bool version101, uint16_t sectionCount);
// Unaligned IO
static inline uint8_t ReadU8(const unsigned char *ptr, const size_t offset) {
return ptr[offset];
}
static inline uint16_t ReadU16(const unsigned char *ptr, const size_t offset) {
return ptr[offset] | (ptr[offset + 1] << 8);
}
static inline uint32_t ReadU32(const unsigned char *ptr, const size_t offset) {
return ptr[offset] | (ptr[offset + 1] << 8) | (ptr[offset + 2] << 16) | (ptr[offset + 3] << 24);
}
static inline int8_t ReadS8(const unsigned char *ptr, const size_t offset) {
return ptr[offset];
}
static inline int16_t ReadS16(const unsigned char *ptr, const size_t offset) {
return ptr[offset] | (ptr[offset + 1] << 8);
}
static inline int32_t ReadS32(const unsigned char *ptr, const size_t offset) {
return ptr[offset] | (ptr[offset + 1] << 8) | (ptr[offset + 2] << 16) | (ptr[offset + 3] << 24);
}
static inline void WriteU8(unsigned char *ptr, const size_t offset, uint8_t value) {
ptr[offset] = value;
}
static inline void WriteU16(unsigned char *ptr, const size_t offset, uint16_t value) {
ptr[offset] = value;
ptr[offset + 1] = value >> 8;
}
static inline void WriteU32(unsigned char *ptr, const size_t offset, uint32_t value) {
ptr[offset] = value;
ptr[offset + 1] = value >> 8;
ptr[offset + 2] = value >> 16;
ptr[offset + 3] = value >> 24;
}
#endif // UTIL_H