improved shade-lut generation
[gbajam21] / tools / pngdump / main.c
index 9f281cc..2d1922e 100644 (file)
@@ -6,33 +6,39 @@
 #include "image.h"
 
 enum {
+       MODE_PNG,
        MODE_PIXELS,
        MODE_CMAP,
-       MODE_INFO,
-       MODE_SHADE_CMAP,
-       MODE_SHADE_LUT
+       MODE_INFO
 };
 
 void print_usage(const char *argv0);
 
 int main(int argc, char **argv)
 {
-       int i, mode = 0;
+       int i, j, mode = 0;
        int text = 0;
        int renibble = 0;
        char *outfname = 0;
+       char *slut_fname = 0;
        char *infiles[256];
        int num_infiles = 0;
        struct image img, tmpimg;
        FILE *out = stdout;
-       struct cmapent shade_cmap[256] = {0};
+       FILE *slut_out = 0;
        int *shade_lut = 0;
+       int *lutptr;
        int shade_levels = 8;
+       int maxcol = 0;
 
        for(i=1; i<argc; i++) {
                if(argv[i][0] == '-') {
                        if(argv[i][2] == 0) {
                                switch(argv[i][1]) {
+                               case 'P':
+                                       mode = MODE_PNG;
+                                       break;
+
                                case 'p':
                                        mode = MODE_PIXELS;
                                        break;
@@ -46,11 +52,10 @@ int main(int argc, char **argv)
                                        break;
 
                                case 'C':
-                                       mode = MODE_SHADE_CMAP;
-                                       break;
-
-                               case 'S':
-                                       mode = MODE_SHADE_LUT;
+                                       if(!argv[++i] || (maxcol = atoi(argv[i])) < 2 || maxcol > 256) {
+                                               fprintf(stderr, "-C must be followed by the number of colors to reduce down to\n");
+                                               return 1;
+                                       }
                                        break;
 
                                case 's':
@@ -76,6 +81,14 @@ int main(int argc, char **argv)
                                        outfname = argv[i];
                                        break;
 
+                               case 'S':
+                                       if(!argv[++i]) {
+                                               fprintf(stderr, "-S must be followed by a filename\n");
+                                               return 1;
+                                       }
+                                       slut_fname = argv[i];
+                                       break;
+
                                case 'h':
                                        print_usage(argv[0]);
                                        return 0;
@@ -123,6 +136,47 @@ int main(int argc, char **argv)
                overlay_key(&tmpimg, 0, &img);
        }
 
+       /* generate shading LUT and quantize image as necessary */
+       if(slut_fname) {
+               if(img.bpp > 8) {
+                       fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
+                       return 1;
+               }
+               if(!(slut_out = fopen(slut_fname, "wb"))) {
+                       fprintf(stderr, "failed to open shading LUT output file: %s: %s\n", slut_fname, strerror(errno));
+                       return 1;
+               }
+
+               if(!maxcol) maxcol = 256;
+
+               if(!(shade_lut = malloc(maxcol * shade_levels * sizeof *shade_lut))) {
+                       fprintf(stderr, "failed to allocate shading look-up table\n");
+                       return 1;
+               }
+
+               gen_shades(&img, shade_levels, maxcol, shade_lut);
+
+               lutptr = shade_lut;
+               for(i=0; i<maxcol; i++) {
+                       for(j=0; j<shade_levels; j++) {
+                               if(text) {
+                                       fprintf(slut_out, "%d%c", *lutptr++, j < shade_levels - 1 ? ' ' : '\n');
+                               } else {
+                                       fputc(*lutptr++, slut_out);
+                               }
+                       }
+               }
+               fclose(slut_out);
+
+       } else if(maxcol) {
+               /* perform any color reductions if requested */
+               if(img.bpp <= 8 && img.cmap_ncolors <= maxcol) {
+                       fprintf(stderr, "requested reduction to %d colors, but image has %d colors\n", maxcol, img.cmap_ncolors);
+                       return 1;
+               }
+               quantize_image(&img, maxcol);
+       }
+
        if(img.bpp == 4 && renibble) {
                unsigned char *ptr = img.pixels;
                for(i=0; i<img.width * img.height; i++) {
@@ -139,6 +193,10 @@ int main(int argc, char **argv)
        }
 
        switch(mode) {
+       case MODE_PNG:
+               save_image_file(&img, out);
+               break;
+
        case MODE_PIXELS:
                fwrite(img.pixels, 1, img.scansz * img.height, out);
                break;
@@ -164,28 +222,6 @@ int main(int argc, char **argv)
                        printf("color channels: %d\n", img.nchan);
                }
                break;
-
-       case MODE_SHADE_LUT:
-               if(!(shade_lut = malloc(256 * shade_levels * sizeof *shade_lut))) {
-                       fprintf(stderr, "failed to allocate shading look-up table\n");
-                       return 1;
-               }
-       case MODE_SHADE_CMAP:
-               if(!img.cmap_ncolors) {
-                       fprintf(stderr, "can't generate shade levels for non-indexed images\n");
-                       return 1;
-               }
-               if(gen_shade_lut(&img, shade_levels, 256, shade_cmap, shade_lut) == -1) {
-                       return 1;
-               }
-               if(mode == MODE_SHADE_CMAP) {
-                       fwrite(shade_cmap, sizeof shade_cmap, 1, out);
-               } else {
-                       for(i=0; i<img.cmap_ncolors * shade_levels; i++) {
-                               fputc(shade_lut[i], out);
-                       }
-               }
-               break;
        }
 
        fclose(out);
@@ -198,12 +234,13 @@ void print_usage(const char *argv0)
        printf("Options:\n");
        printf(" -o <output file>: specify output file (default: stdout)\n");
        printf(" -p: dump pixels (default)\n");
+       printf(" -P: output in PNG format\n");
        printf(" -c: dump colormap (palette) entries\n");
-       printf(" -C: generate shading colormap\n");
-       printf(" -S: generate shading LUT\n");
+       printf(" -C <colors>: reduce image down to specified number of colors\n");
+       printf(" -S <lut file>: generate and output shading LUT\n");
        printf(" -s <shade levels>: used in conjunction with -C or -S (default: 8)\n");
        printf(" -i: print image information\n");
-       printf(" -t: dump as text\n");
+       printf(" -t: output as text when possible\n");
        printf(" -n: swap the order of nibbles (for 4bpp)\n");
        printf(" -h: print usage and exit\n");
 }