- fixed spritesheet conversion and setup
[gba_blender] / tools / pngdump / main.c
index 908818d..a12fb3d 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <assert.h>
 #include "image.h"
 
 void print_usage(const char *argv0);
@@ -10,6 +11,7 @@ int main(int argc, char **argv)
 {
        int i, mode = 0;
        int text = 0;
+       int renibble = 0;
        char *fname = 0, *outfname = 0;
        struct image img;
        FILE *out = stdout;
@@ -34,6 +36,10 @@ int main(int argc, char **argv)
                                        text = 1;
                                        break;
 
+                               case 'n':
+                                       renibble = 1;
+                                       break;
+
                                case 'o':
                                        if(!argv[++i]) {
                                                fprintf(stderr, "%s must be followed by a filename\n", argv[i - 1]);
@@ -75,6 +81,14 @@ int main(int argc, char **argv)
                return 1;
        }
 
+       if(img.bpp == 4 && renibble) {
+               unsigned char *ptr = img.pixels;
+               for(i=0; i<img.width * img.height; i++) {
+                       unsigned char p = *ptr;
+                       *ptr++ = (p << 4) | (p >> 4);
+               }
+       }
+
        if(outfname) {
                if(!(out = fopen(outfname, "wb"))) {
                        fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
@@ -84,17 +98,7 @@ int main(int argc, char **argv)
 
        switch(mode) {
        case 0:
-               if(img.bpp > 8 || img.cmap_ncolors == 0 || img.cmap_ncolors > 16) {
-                       fwrite(img.pixels, 1, img.scansz * img.height, out);
-               } else {
-                       /* pack into nibbles */
-                       unsigned char *ptr = img.pixels;
-                       for(i=0; i<img.width * img.height / 2; i++) {
-                               unsigned char pair = (ptr[0] << 4) | ptr[1];
-                               fputc(pair, out);
-                               ptr += 2;
-                       }
-               }
+               fwrite(img.pixels, 1, img.scansz * img.height, out);
                break;
 
        case 1:
@@ -127,9 +131,11 @@ void print_usage(const char *argv0)
 {
        printf("Usage: %s [options] <input file>\n", argv0);
        printf("Options:\n");
+       printf(" -o <output file>: specify output file (default: stdout)\n");
        printf(" -p: dump pixels (default)\n");
        printf(" -c: dump colormap (palette) entries\n");
        printf(" -i: print image information\n");
        printf(" -t: dump as text\n");
+       printf(" -n: swap the order of nibbles (for 4bpp)\n");
        printf(" -h: print usage and exit\n");
 }