From 6cd4196bfb302f54fa11591a4aa4658b9896fade Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 14 Sep 2018 10:11:46 +0000 Subject: Fixed 16x16 modes for 4 and 16 colours. --- snesgfx.c | 171 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 134 insertions(+), 37 deletions(-) diff --git a/snesgfx.c b/snesgfx.c index d066c04..8e0de8a 100644 --- a/snesgfx.c +++ b/snesgfx.c @@ -48,39 +48,51 @@ static int GetPixel(int x, int y, png_bytep *data, int width, int height) } } +static void GetPlanes(int x, int y, int plane[16][4], int depth, int size, + png_bytep *data, int width, int height) +{ + int dx,dy; + + for(dy = 0; dy < size; dy++) + { + for(dx = 0; dx < 8; dx++) + { + int pix = GetPixel(x + dx, y + dy, data, width, height); + int f; + + for(f = 0; f < depth; f++) + { + if ((pix & (1 << f)) == (1 << f)) + { + plane[dy][f] = plane[dy][f] | (1 << (7 - dx)); + } + } + } + } +} + static void Convert4Colour8x8(png_bytep *data, int width, int height, FILE *output) { int x,y; - int dx,dy; + int dy; for(y = 0; y < height; y += 8) { for(x = 0; x < width; x+= 8) { - for(dy = 0; dy < 8; dy++) - { - int plane[2] = {0}; - int f; + int plane[16][4] = {0}; + int f; - for(dx = 0; dx < 8; dx++) - { - int pix = GetPixel(x + dx, y + dy, data, width, height); - - for(f = 0; f < 2; f++) - { - if ((pix & (1 << f)) == (1 << f)) - { - plane[f] = plane[f] | (1 << (7 - dx)); - } - } - } + GetPlanes(x, y, plane, 2, 8, data, width, height); + for(dy = 0; dy < 8; dy++) + { for(f = 0; f < 2; f++) { - putc(plane[f], output); + putc(plane[dy][f], output); } } } @@ -98,7 +110,7 @@ static void Convert4Colour16x16(png_bytep *data, unsigned char *mem; size_t size; - size = width / 16 * height / 16 * 1024; + size = width / 16 * height / 16 * 512; if (!size) { @@ -114,7 +126,40 @@ static void Convert4Colour16x16(png_bytep *data, exit(EXIT_FAILURE); } - // fwrite(mem, 1, size, output); + memset(mem, 0, size); + + for(y = 0; y < height; y += 16) + { + for(x = 0; x < width; x+= 16) + { + int plane_left[16][4] = {0}; + int plane_right[16][4] = {0}; + int offset; + int f; + + offset = (tile / 16) * 512 + (tile % 16) * 32; + + GetPlanes(x, y, plane_left, 2, 16, data, width, height); + GetPlanes(x + 8, y, plane_right, 2, 16, data, width, height); + + for(dy = 0; dy < 8; dy++) + { + mem[offset + dy * 2] = plane_left[dy][0]; + mem[offset + dy * 2 + 1] = plane_left[dy][1]; + mem[offset + dy * 2 + 16] = plane_right[dy][0]; + mem[offset + dy * 2 + 17] = plane_right[dy][1]; + + mem[offset + dy * 2 + 256] = plane_left[8 + dy][0]; + mem[offset + dy * 2 + 256 + 1] = plane_left[8 + dy][1]; + mem[offset + dy * 2 + 256 + 16] = plane_right[8 + dy][0]; + mem[offset + dy * 2 + 256 + 17] = plane_right[8 + dy][1]; + } + + tile++; + } + } + + fwrite(mem, 1, size, output); free(mem); } @@ -125,30 +170,16 @@ static void Convert16Colour8x8(png_bytep *data, FILE *output) { int x,y; - int dx,dy; + int dy; for(y = 0; y < height; y += 8) { for(x = 0; x < width; x+= 8) { - int plane[8][4] = {0}; + int plane[16][4] = {0}; int f; - for(dy = 0; dy < 8; dy++) - { - for(dx = 0; dx < 8; dx++) - { - int pix = GetPixel(x + dx, y + dy, data, width, height); - - for(f = 0; f < 4; f++) - { - if ((pix & (1 << f)) == (1 << f)) - { - plane[dy][f] = plane[dy][f] | (1 << (7 - dx)); - } - } - } - } + GetPlanes(x, y, plane, 4, 8, data, width, height); for(dy = 0; dy < 8; dy++) { @@ -174,6 +205,72 @@ static void Convert16Colour16x16(png_bytep *data, int height, FILE *output) { + int x,y; + int dx,dy; + int tile = 0; + unsigned char *mem; + size_t size; + + size = width / 16 * height / 16 * 1024; + + if (!size) + { + fprintf(stderr, "No tiles to export\n"); + exit(EXIT_FAILURE); + } + + mem = malloc(size); + + if (!mem) + { + fprintf(stderr, "malloc() failed\n"); + exit(EXIT_FAILURE); + } + + memset(mem, 0, size); + + for(y = 0; y < height; y += 16) + { + for(x = 0; x < width; x+= 16) + { + int plane_left[16][4] = {0}; + int plane_right[16][4] = {0}; + int offset; + int f; + + offset = (tile / 16) * 1024 + (tile % 16) * 64; + + GetPlanes(x, y, plane_left, 4, 16, data, width, height); + GetPlanes(x + 8, y, plane_right, 4, 16, data, width, height); + + for(dy = 0; dy < 8; dy++) + { + mem[offset + dy * 2] = plane_left[dy][0]; + mem[offset + dy * 2 + 1] = plane_left[dy][1]; + mem[offset + dy * 2 + 16] = plane_left[dy][2]; + mem[offset + dy * 2 + 17] = plane_left[dy][3]; + mem[offset + dy * 2 + 32] = plane_right[dy][0]; + mem[offset + dy * 2 + 33] = plane_right[dy][1]; + mem[offset + dy * 2 + 48] = plane_right[dy][2]; + mem[offset + dy * 2 + 49] = plane_right[dy][3]; + + mem[offset + dy * 2 + 512] = plane_left[8+dy][0]; + mem[offset + dy * 2 + 512 + 1] = plane_left[8+dy][1]; + mem[offset + dy * 2 + 512 + 16] = plane_left[8+dy][2]; + mem[offset + dy * 2 + 512 + 17] = plane_left[8+dy][3]; + mem[offset + dy * 2 + 512 + 32] = plane_right[8+dy][0]; + mem[offset + dy * 2 + 512 + 33] = plane_right[8+dy][1]; + mem[offset + dy * 2 + 512 + 48] = plane_right[8+dy][2]; + mem[offset + dy * 2 + 512 + 49] = plane_right[8+dy][3]; + } + + tile++; + } + } + + fwrite(mem, 1, size, output); + + free(mem); } static void SavePalette(const char *palette, -- cgit v1.2.3