tunnel shading
[gbajam21] / tools / pngdump / main.c
index d7aba3e..71bcd7c 100644 (file)
@@ -5,33 +5,66 @@
 #include <assert.h>
 #include "image.h"
 
+enum {
+       MODE_PIXELS,
+       MODE_CMAP,
+       MODE_PNG,
+       MODE_INFO
+};
+
+void dump_colormap(struct image *img, int text, FILE *fp);
 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, *cmap_fname = 0;
        char *infiles[256];
        int num_infiles = 0;
        struct image img, tmpimg;
        FILE *out = stdout;
+       FILE *aux_out;
+       int *shade_lut = 0;
+       int *lutptr;
+       int shade_levels = 8;
+       int maxcol = 0;
+       int lvl;
 
        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 = 0;
+                                       mode = MODE_PIXELS;
                                        break;
 
                                case 'c':
-                                       mode = 1;
+                                       mode = MODE_CMAP;
                                        break;
 
                                case 'i':
-                                       mode = 2;
+                                       mode = MODE_INFO;
+                                       break;
+
+                               case 'C':
+                                       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':
+                                       if(!argv[++i] || (shade_levels = atoi(argv[i])) == 0) {
+                                               fprintf(stderr, "-s must be followed by the number of shade levels\n");
+                                               return 1;
+                                       }
                                        break;
 
                                case 't':
@@ -60,9 +93,25 @@ int main(int argc, char **argv)
                                        return 1;
                                }
                        } else {
-                               fprintf(stderr, "invalid option: %s\n", argv[i]);
-                               print_usage(argv[0]);
-                               return 1;
+                               if(strcmp(argv[i], "-oc") == 0) {
+                                       if(!argv[++i]) {
+                                               fprintf(stderr, "-oc must be followed by a filename\n");
+                                               return 1;
+                                       }
+                                       cmap_fname = argv[i];
+
+                               } else if(strcmp(argv[i], "-os") == 0) {
+                                       if(!argv[++i]) {
+                                               fprintf(stderr, "-os must be followed by a filename\n");
+                                               return 1;
+                                       }
+                                       slut_fname = argv[i];
+
+                               } else {
+                                       fprintf(stderr, "invalid option: %s\n", argv[i]);
+                                       print_usage(argv[0]);
+                                       return 1;
+                               }
                        }
                } else {
                        infiles[num_infiles++] = argv[i];
@@ -97,6 +146,62 @@ 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(!(aux_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++) {
+                               lvl = lutptr[shade_levels - j - 1];
+                               if(text) {
+                                       fprintf(aux_out, "%d%c", lvl, j < shade_levels - 1 ? ' ' : '\n');
+                               } else {
+                                       fputc(lvl, aux_out);
+                               }
+                       }
+                       lutptr += shade_levels;
+               }
+               fclose(aux_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(cmap_fname) {
+               if(img.bpp > 8) {
+                       fprintf(stderr, "colormap output works only for indexed color images\n");
+                       return 1;
+               }
+               if(!(aux_out = fopen(cmap_fname, "wb"))) {
+                       fprintf(stderr, "failed to open colormap output file: %s: %s\n", cmap_fname, strerror(errno));
+                       return 1;
+               }
+               dump_colormap(&img, text, aux_out);
+               fclose(aux_out);
+       }
+
        if(img.bpp == 4 && renibble) {
                unsigned char *ptr = img.pixels;
                for(i=0; i<img.width * img.height; i++) {
@@ -113,22 +218,19 @@ int main(int argc, char **argv)
        }
 
        switch(mode) {
-       case 0:
+       case MODE_PNG:
+               save_image_file(&img, out);
+               break;
+
+       case MODE_PIXELS:
                fwrite(img.pixels, 1, img.scansz * img.height, out);
                break;
 
-       case 1:
-               if(text) {
-                       for(i=0; i<img.cmap_ncolors; i++) {
-                               printf("%d %d %d\n", img.cmap[i].r, img.cmap[i].g, img.cmap[i].b);
-                       }
-               } else {
-                       /*fwrite(img.cmap, sizeof img.cmap[0], img.cmap_ncolors, out);*/
-                       fwrite(img.cmap, sizeof img.cmap[0], 1 << img.bpp, out);
-               }
+       case MODE_CMAP:
+               dump_colormap(&img, text, out);
                break;
 
-       case 2:
+       case MODE_INFO:
                printf("size: %dx%d\n", img.width, img.height);
                printf("bit depth: %d\n", img.bpp);
                printf("scanline size: %d bytes\n", img.scansz);
@@ -144,15 +246,33 @@ int main(int argc, char **argv)
        return 0;
 }
 
+void dump_colormap(struct image *img, int text, FILE *fp)
+{
+       int i;
+
+       if(text) {
+               for(i=0; i<img->cmap_ncolors; i++) {
+                       fprintf(fp, "%d %d %d\n", img->cmap[i].r, img->cmap[i].g, img->cmap[i].b);
+               }
+       } else {
+               fwrite(img->cmap, sizeof img->cmap[0], 1 << img->bpp, fp);
+       }
+}
+
 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(" -oc <cmap file>: output colormap to separate file\n");
+       printf(" -os <lut file>: generate and output shading LUT\n");
        printf(" -p: dump pixels (default)\n");
+       printf(" -P: output in PNG format\n");
        printf(" -c: dump colormap (palette) entries\n");
+       printf(" -C <colors>: reduce image down to specified number of colors\n");
+       printf(" -s <shade levels>: used in conjunction with -os (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");
 }