summaryrefslogtreecommitdiff
path: root/snesgfx.c
diff options
context:
space:
mode:
Diffstat (limited to 'snesgfx.c')
-rw-r--r--snesgfx.c115
1 files changed, 76 insertions, 39 deletions
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);