summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2018-09-13 15:32:54 +0000
committerIan C <ianc@noddybox.co.uk>2018-09-13 15:32:54 +0000
commit285950af18a703eaf91a3fdcc74e8497d9c0efae (patch)
treec6f5f8a6e6425dce1a987b266631c666b8f49eca
parentb9481b72bb8288909165a4452ac2a9478c5437af (diff)
Fixed mirror on 8x8 generate. Started on 16x16 generate.
-rw-r--r--Makefile2
-rw-r--r--README7
-rw-r--r--snesgfx.c115
3 files changed, 81 insertions, 43 deletions
diff --git a/Makefile b/Makefile
index 04bc3b3..2fb1ff5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
snesgfx: snesgfx.c
- c99 -I/usr/local/include -L/usr/local/lib -g -o snesgfx snesgfx.c -lpng
+ $(CC) -I/usr/local/include -L/usr/local/lib -g -o snesgfx snesgfx.c -lpng
clean:
rm -f snesgfx core
diff --git a/README b/README
index b387ae3..2992bf1 100644
--- a/README
+++ b/README
@@ -1,11 +1,12 @@
Simple tool to convert PNG files with a palette to 4 or 16 colour SNES
-tiles. It currently only supports 8x8 tiles.
+tiles.
Usage: snesgfx [-p palette_file] [-s] [-c colours] source.png dest.data
-p palette_file Write a SNES palette based on the one in the image to the
passed file.
--s Indicates 16x16 tiles. Currently unsupported.
+-s size Indicates either 8 (for 8x8 the default) or 16.
-c colours Colours -- either 4 or 16. Defaults to 4.
-source.png The source PNG file.
+source.png The source PNG file. This is read left to right then top to
+ bottom.
dest.data Where to write the SNES tile data.
diff --git a/snesgfx.c b/snesgfx.c
index d19e5bb..d066c04 100644
--- a/snesgfx.c
+++ b/snesgfx.c
@@ -31,7 +31,7 @@
static void Usage(void)
{
- fprintf(stderr, "usage: snesgfx [-p palette_file] [-s] [-c colours] "
+ fprintf(stderr, "usage: snesgfx [-p palette_file] [-s size] [-c colours] "
"source.png dest.data\n");
exit(EXIT_FAILURE);
}
@@ -48,25 +48,24 @@ static int GetPixel(int x, int y, png_bytep *data, int width, int height)
}
}
-static void Convert4Colour(png_bytep *data,
- int width,
- int height,
- int size,
- FILE *output)
+static void Convert4Colour8x8(png_bytep *data,
+ int width,
+ int height,
+ FILE *output)
{
int x,y;
int dx,dy;
- for(y = 0; y < height; y += size)
+ for(y = 0; y < height; y += 8)
{
- for(x = 0; x < width; x+= size)
+ for(x = 0; x < width; x+= 8)
{
- for(dy = 0; dy < size; dy++)
+ for(dy = 0; dy < 8; dy++)
{
int plane[2] = {0};
int f;
- for(dx = 0; dx < size; dx++)
+ for(dx = 0; dx < 8; dx++)
{
int pix = GetPixel(x + dx, y + dy, data, width, height);
@@ -74,7 +73,7 @@ static void Convert4Colour(png_bytep *data,
{
if ((pix & (1 << f)) == (1 << f))
{
- plane[f] = plane[f] | (1 << dx);
+ plane[f] = plane[f] | (1 << (7 - dx));
}
}
}
@@ -88,33 +87,56 @@ static void Convert4Colour(png_bytep *data,
}
}
-static void Convert16Colour(png_bytep *data,
- int width,
- int height,
- int size,
- FILE *output)
+static void Convert4Colour16x16(png_bytep *data,
+ int width,
+ int height,
+ FILE *output)
{
int x,y;
int dx,dy;
+ int tile = 0;
+ unsigned char *mem;
+ size_t size;
- for(y = 0; y < height; y += 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);
+ }
+
+ // fwrite(mem, 1, size, output);
+
+ free(mem);
+}
+
+static void Convert16Colour8x8(png_bytep *data,
+ int width,
+ int height,
+ FILE *output)
+{
+ int x,y;
+ int dx,dy;
+
+ for(y = 0; y < height; y += 8)
{
- for(x = 0; x < width; x+= size)
+ for(x = 0; x < width; x+= 8)
{
- int plane[size][4];
+ int plane[8][4] = {0};
int f;
- for(dy = 0; dy < size; dy++)
- {
- for(f = 0; f < 4; f++)
- {
- plane[dy][f] = 0;
- }
- }
-
- for(dy = 0; dy < size; dy++)
+ for(dy = 0; dy < 8; dy++)
{
- for(dx = 0; dx < size; dx++)
+ for(dx = 0; dx < 8; dx++)
{
int pix = GetPixel(x + dx, y + dy, data, width, height);
@@ -122,13 +144,13 @@ static void Convert16Colour(png_bytep *data,
{
if ((pix & (1 << f)) == (1 << f))
{
- plane[dy][f] = plane[dy][f] | (1 << dx);
+ plane[dy][f] = plane[dy][f] | (1 << (7 - dx));
}
}
}
}
- for(dy = 0; dy < size; dy++)
+ for(dy = 0; dy < 8; dy++)
{
for(f = 0; f < 2; f++)
{
@@ -136,7 +158,7 @@ static void Convert16Colour(png_bytep *data,
}
}
- for(dy = 0; dy < size; dy++)
+ for(dy = 0; dy < 8; dy++)
{
for(f = 2; f < 4; f++)
{
@@ -147,6 +169,13 @@ static void Convert16Colour(png_bytep *data,
}
}
+static void Convert16Colour16x16(png_bytep *data,
+ int width,
+ int height,
+ FILE *output)
+{
+}
+
static void SavePalette(const char *palette,
int colours,
png_color *pal,
@@ -215,7 +244,7 @@ int main(int argc, char *argv[])
palette = argv[++f];
break;
case 's':
- size = 16;
+ size = atoi(argv[++f]);
break;
case 'c':
colours = atoi(argv[++f]);
@@ -243,9 +272,9 @@ int main(int argc, char *argv[])
Usage();
}
- if (size == 16)
+ if (size != 8 && size != 16)
{
- fprintf(stderr, "16x16 mode not yet done\n");
+ fprintf(stderr, "Size must be 8 or 16\n");
return EXIT_FAILURE;
}
@@ -313,13 +342,21 @@ int main(int argc, char *argv[])
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
- if (colours == 4)
+ if (colours == 4 && size == 8)
{
- Convert4Colour(rows, width, height, size, output);
+ Convert4Colour8x8(rows, width, height, output);
}
- else
+ else if (colours == 16 && size == 8)
+ {
+ Convert16Colour8x8(rows, width, height, output);
+ }
+ else if (colours == 4 && size == 16)
+ {
+ Convert4Colour16x16(rows, width, height, output);
+ }
+ else if (colours == 16 && size == 16)
{
- Convert16Colour(rows, width, height, size, output);
+ Convert16Colour16x16(rows, width, height, output);
}
fclose(input);