diff options
author | PikalaxALT <pikalaxalt@gmail.com> | 2021-08-21 18:42:30 -0400 |
---|---|---|
committer | PikalaxALT <pikalaxalt@gmail.com> | 2021-08-21 18:42:30 -0400 |
commit | 6a0b937064907c1790447f7ddd0c60e66e49cd70 (patch) | |
tree | 09cfd65ecb1b3f9816620f5b0091d5f512fa4239 | |
parent | 676ebab3d6ad726b3e154c994c4fcfdbb186ed22 (diff) |
nitrogfx compatibility with MSVCC
-rw-r--r-- | tools/nitrogfx/Makefile | 2 | ||||
-rw-r--r-- | tools/nitrogfx/gfx.c | 15 | ||||
-rw-r--r-- | tools/nitrogfx/global.h | 6 | ||||
-rw-r--r-- | tools/nitrogfx/huff.c | 28 | ||||
-rw-r--r-- | tools/nitrogfx/main.c | 6 |
5 files changed, 33 insertions, 24 deletions
diff --git a/tools/nitrogfx/Makefile b/tools/nitrogfx/Makefile index eadf2be5..18ecdbba 100644 --- a/tools/nitrogfx/Makefile +++ b/tools/nitrogfx/Makefile @@ -1,6 +1,6 @@ CC = gcc -CFLAGS = -Wall -Wextra -Werror -Wno-sign-compare -std=c11 -O2 -DPNG_SKIP_SETJMP_CHECK +CFLAGS = -Wall -Wextra -Werror -Wno-sign-compare -std=c11 -O2 -DPNG_SKIP_SETJMP_CHECK -D_CRT_SECURE_NO_WARNINGS LIBS = -lpng -lz diff --git a/tools/nitrogfx/gfx.c b/tools/nitrogfx/gfx.c index f5ff30e7..ee7e0d6f 100644 --- a/tools/nitrogfx/gfx.c +++ b/tools/nitrogfx/gfx.c @@ -101,7 +101,7 @@ static uint32_t ConvertFromScanned4Bpp(unsigned char *src, unsigned char *dest, uint16_t val = (src[i - 1] << 8) | src[i - 2]; val ^= (encValue & 0xFFFF); src[i - 1] = (val >> 8); - src[i - 2] = val; + src[i - 2] = (unsigned char)val; encValue = encValue * 1103515245; encValue = encValue + 24691; } @@ -225,7 +225,7 @@ static void ConvertToScanned4Bpp(unsigned char *src, unsigned char *dest, int fi encValue = (encValue - 24691) * 4005161829; val ^= (encValue & 0xFFFF); dest[i] = (val >> 8); - dest[i - 1] = val; + dest[i - 1] = (unsigned char)val; } } @@ -704,7 +704,7 @@ void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr, bool ir, in fwrite(palHeader, 1, 0x18, fp); - unsigned char colours[colourNum * 2]; + unsigned char * colours = malloc(colourNum * 2); //palette data for (int i = 0; i < colourNum; i++) { @@ -733,6 +733,7 @@ void WriteNtrPalette(char *path, struct Palette *palette, bool ncpr, bool ir, in } fwrite(colours, 1, colourNum * 2, fp); + free(colours); fclose(fp); } @@ -750,7 +751,7 @@ void WriteNtrCell(char *path, struct JsonToCellOptions *options) { for (int j = 0; j < options->labelCount; j++) { - totalSize += strlen(options->labels[j]) + 5; //strlen + terminator + pointer + totalSize += (unsigned)strlen(options->labels[j]) + 5; //strlen + terminator + pointer } } @@ -865,7 +866,7 @@ void WriteNtrCell(char *path, struct JsonToCellOptions *options) unsigned int lablSize = 8; for (int j = 0; j < options->labelCount; j++) { - lablSize += strlen(options->labels[j]) + 5; + lablSize += (unsigned)strlen(options->labels[j]) + 5; } unsigned char *labl = malloc(lablSize); @@ -884,14 +885,14 @@ void WriteNtrCell(char *path, struct JsonToCellOptions *options) labl[i + 8] = position & 0xff; labl[i + 9] = position >> 8; - position += strlen(options->labels[j]) + 1; + position += (unsigned)strlen(options->labels[j]) + 1; i += 4; } for (int j = 0; j < options->labelCount; j++) { strcpy((char *) (labl + (i + 8)), options->labels[j]); - i += strlen(options->labels[j]) + 1; + i += (int)strlen(options->labels[j]) + 1; } fwrite(labl, 1, lablSize, fp); diff --git a/tools/nitrogfx/global.h b/tools/nitrogfx/global.h index 65dd351d..32378a9f 100644 --- a/tools/nitrogfx/global.h +++ b/tools/nitrogfx/global.h @@ -28,4 +28,10 @@ do { \ #endif // _MSC_VER +#define PTR_ADD(ptr, value) ((void*)((uintptr_t)(ptr) + (value))) +#define PTR_SUB(ptr, value) ((void*)((uintptr_t)(ptr) - (value))) +#define PTR_IADD(ptr, value) do { (ptr) = PTR_ADD(ptr, value); } while (0) +#define PTR_ISUB(ptr, value) do { (ptr) = PTR_SUB(ptr, value); } while (0) +#define PTR_DIFF(right, left) ((uintptr_t)(right) - (uintptr_t)(left)) + #endif // GLOBAL_H diff --git a/tools/nitrogfx/huff.c b/tools/nitrogfx/huff.c index 143ed79b..d718fb71 100644 --- a/tools/nitrogfx/huff.c +++ b/tools/nitrogfx/huff.c @@ -34,17 +34,17 @@ int msort_r(void * data, size_t count, size_t size, cmpfun cmp, void * buffer) { case 2: // Swap the two entries if the right one compares higher. - if (cmp(data, data + size) > 0) { + if (cmp(data, PTR_ADD(data, size)) > 0) { memcpy(buffer, data, size); - memcpy(data, data + size, size); - memcpy(data + size, buffer, size); + memcpy(data, PTR_ADD(data, size), size); + memcpy(PTR_ADD(data, size), buffer, size); } break; default: // Merge sort out-of-place. leftPtr = data; - leftEnd = rightPtr = data + count / 2 * size; - rightEnd = data + count * size; + leftEnd = rightPtr = PTR_ADD(data, count / 2 * size); + rightEnd = PTR_ADD(data, count * size); // Sort the left half if (!msort_r(leftPtr, count / 2, size, cmp, buffer)) @@ -58,11 +58,11 @@ int msort_r(void * data, size_t count, size_t size, cmpfun cmp, void * buffer) { i = 0; do { if (cmp(leftPtr, rightPtr) <= 0) { - memcpy(buffer + i * size, leftPtr, size); - leftPtr += size; + memcpy(PTR_ADD(buffer, i * size), leftPtr, size); + PTR_IADD(leftPtr, size); } else { - memcpy(buffer + i * size, rightPtr, size); - rightPtr += size; + memcpy(PTR_ADD(buffer, i * size), rightPtr, size); + PTR_IADD(rightPtr, size); } } while (++i < count && leftPtr < leftEnd && rightPtr < rightEnd); @@ -70,10 +70,10 @@ int msort_r(void * data, size_t count, size_t size, cmpfun cmp, void * buffer) { // Copy the remainder if (i < count) { if (leftPtr < leftEnd) { - memcpy(buffer + i * size, leftPtr, leftEnd - leftPtr); + memcpy(PTR_ADD(buffer, i * size), leftPtr, PTR_DIFF(leftEnd, leftPtr)); } else { - memcpy(buffer + i * size, rightPtr, rightEnd - rightPtr); + memcpy(PTR_ADD(buffer, i * size), rightPtr, PTR_DIFF(rightEnd, rightPtr)); } } @@ -163,7 +163,7 @@ static void write_tree(unsigned char * dest, HuffNode_t * tree, int nitems, stru if (currNode->header.isLeaf) { dest[5 + i] = traversal[i].leaf.key; } else { - dest[5 + i] = (((currNode->branch.right - traversal - i) / 2) - 1); + dest[5 + i] = (unsigned char)(((currNode->branch.right - traversal - i) / 2) - 1); if (currNode->branch.left->header.isLeaf) dest[5 + i] |= 0x80; if (currNode->branch.right->header.isLeaf) @@ -194,8 +194,8 @@ static inline void read_32_le(unsigned char * src, int * srcPos, uint32_t * buff } static void write_bits(unsigned char * dest, int * destPos, struct BitEncoding * encoding, int value, uint32_t * buff, int * buffBits) { - int nbits = encoding[value].nbits; - uint32_t bitstring = encoding[value].bitstring; + int nbits = (int)encoding[value].nbits; + uint32_t bitstring = (uint32_t)encoding[value].bitstring; if (*buffBits + nbits >= 32) { int diff = *buffBits + nbits - 32; diff --git a/tools/nitrogfx/main.c b/tools/nitrogfx/main.c index 171cb5f3..b2d3352a 100644 --- a/tools/nitrogfx/main.c +++ b/tools/nitrogfx/main.c @@ -63,13 +63,14 @@ void ConvertNtrToPng(char *inputPath, char *outputPath, struct GbaToPngOptions * if (key) { - char string[strlen(outputPath) + 5]; + char* string = malloc(strlen(outputPath) + 5); sprintf(string, "%s.key", outputPath); FILE *fp = fopen(string, "wb"); if (fp == NULL) FATAL_ERROR("Failed to open key file for writing.\n"); fwrite(&key, 4, 1, fp); fclose(fp); + free(string); } image.hasTransparency = options->hasTransparency; @@ -103,7 +104,7 @@ void ConvertPngToNtr(char *inputPath, char *outputPath, struct PngToNtrOptions * uint32_t key = 0; if (options->scanned) { - char string[strlen(inputPath) + 5]; + char* string = malloc(strlen(inputPath) + 5); sprintf(string, "%s.key", inputPath); FILE *fp2 = fopen(string, "rb"); if (fp2 == NULL) @@ -112,6 +113,7 @@ void ConvertPngToNtr(char *inputPath, char *outputPath, struct PngToNtrOptions * if (count != 1) FATAL_ERROR("Not a valid key file.\n"); fclose(fp2); + free(string); } WriteNtrImage(outputPath, options->numTiles, image.bitDepth, options->metatileWidth, options->metatileHeight, &image, !image.hasPalette, options->clobberSize, options->byteOrder, options->version101, options->sopc, options->scanned, key); |