pngdump fix: previous commit inadvertently made PNG output the default
[gbajam21] / tools / pngdump / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include "image.h"
7
8 enum {
9         MODE_PIXELS,
10         MODE_CMAP,
11         MODE_PNG,
12         MODE_INFO
13 };
14
15 void print_usage(const char *argv0);
16
17 int main(int argc, char **argv)
18 {
19         int i, j, mode = 0;
20         int text = 0;
21         int renibble = 0;
22         char *outfname = 0;
23         char *slut_fname = 0;
24         char *infiles[256];
25         int num_infiles = 0;
26         struct image img, tmpimg;
27         FILE *out = stdout;
28         FILE *slut_out = 0;
29         int *shade_lut = 0;
30         int *lutptr;
31         int shade_levels = 8;
32         int maxcol = 0;
33
34         for(i=1; i<argc; i++) {
35                 if(argv[i][0] == '-') {
36                         if(argv[i][2] == 0) {
37                                 switch(argv[i][1]) {
38                                 case 'P':
39                                         mode = MODE_PNG;
40                                         break;
41
42                                 case 'p':
43                                         mode = MODE_PIXELS;
44                                         break;
45
46                                 case 'c':
47                                         mode = MODE_CMAP;
48                                         break;
49
50                                 case 'i':
51                                         mode = MODE_INFO;
52                                         break;
53
54                                 case 'C':
55                                         if(!argv[++i] || (maxcol = atoi(argv[i])) < 2 || maxcol > 256) {
56                                                 fprintf(stderr, "-C must be followed by the number of colors to reduce down to\n");
57                                                 return 1;
58                                         }
59                                         break;
60
61                                 case 's':
62                                         if(!argv[++i] || (shade_levels = atoi(argv[i])) == 0) {
63                                                 fprintf(stderr, "-s must be followed by the number of shade levels\n");
64                                                 return 1;
65                                         }
66                                         break;
67
68                                 case 't':
69                                         text = 1;
70                                         break;
71
72                                 case 'n':
73                                         renibble = 1;
74                                         break;
75
76                                 case 'o':
77                                         if(!argv[++i]) {
78                                                 fprintf(stderr, "%s must be followed by a filename\n", argv[i - 1]);
79                                                 return 1;
80                                         }
81                                         outfname = argv[i];
82                                         break;
83
84                                 case 'S':
85                                         if(!argv[++i]) {
86                                                 fprintf(stderr, "-S must be followed by a filename\n");
87                                                 return 1;
88                                         }
89                                         slut_fname = argv[i];
90                                         break;
91
92                                 case 'h':
93                                         print_usage(argv[0]);
94                                         return 0;
95
96                                 default:
97                                         fprintf(stderr, "invalid option: %s\n", argv[i]);
98                                         print_usage(argv[0]);
99                                         return 1;
100                                 }
101                         } else {
102                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
103                                 print_usage(argv[0]);
104                                 return 1;
105                         }
106                 } else {
107                         infiles[num_infiles++] = argv[i];
108                 }
109         }
110
111         if(!num_infiles) {
112                 fprintf(stderr, "pass the filename of a PNG file\n");
113                 return 1;
114         }
115         if(load_image(&img, infiles[0]) == -1) {
116                 fprintf(stderr, "failed to load PNG file: %s\n", infiles[0]);
117                 return 1;
118         }
119
120         for(i=1; i<num_infiles; i++) {
121                 if(load_image(&tmpimg, infiles[i]) == -1) {
122                         fprintf(stderr, "failed to load PNG file: %s\n", infiles[i]);
123                         return 1;
124                 }
125                 if(tmpimg.width != img.width || tmpimg.height != img.height) {
126                         fprintf(stderr, "size mismatch: first image (%s) is %dx%d, %s is %dx%d\n",
127                                         infiles[0], img.width, img.height, infiles[i], tmpimg.width, tmpimg.height);
128                         return 1;
129                 }
130                 if(tmpimg.bpp != img.bpp) {
131                         fprintf(stderr, "bpp mismatch: first image (%s) is %d bpp, %s is %d bpp\n",
132                                         infiles[0], img.bpp, infiles[i], img.bpp);
133                         return 1;
134                 }
135
136                 overlay_key(&tmpimg, 0, &img);
137         }
138
139         /* generate shading LUT and quantize image as necessary */
140         if(slut_fname) {
141                 if(img.bpp > 8) {
142                         fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
143                         return 1;
144                 }
145                 if(!(slut_out = fopen(slut_fname, "wb"))) {
146                         fprintf(stderr, "failed to open shading LUT output file: %s: %s\n", slut_fname, strerror(errno));
147                         return 1;
148                 }
149
150                 if(!maxcol) maxcol = 256;
151
152                 if(!(shade_lut = malloc(maxcol * shade_levels * sizeof *shade_lut))) {
153                         fprintf(stderr, "failed to allocate shading look-up table\n");
154                         return 1;
155                 }
156
157                 gen_shades(&img, shade_levels, maxcol, shade_lut);
158
159                 lutptr = shade_lut;
160                 for(i=0; i<maxcol; i++) {
161                         for(j=0; j<shade_levels; j++) {
162                                 if(text) {
163                                         fprintf(slut_out, "%d%c", *lutptr++, j < shade_levels - 1 ? ' ' : '\n');
164                                 } else {
165                                         fputc(*lutptr++, slut_out);
166                                 }
167                         }
168                 }
169                 fclose(slut_out);
170
171         } else if(maxcol) {
172                 /* perform any color reductions if requested */
173                 if(img.bpp <= 8 && img.cmap_ncolors <= maxcol) {
174                         fprintf(stderr, "requested reduction to %d colors, but image has %d colors\n", maxcol, img.cmap_ncolors);
175                         return 1;
176                 }
177                 quantize_image(&img, maxcol);
178         }
179
180         if(img.bpp == 4 && renibble) {
181                 unsigned char *ptr = img.pixels;
182                 for(i=0; i<img.width * img.height; i++) {
183                         unsigned char p = *ptr;
184                         *ptr++ = (p << 4) | (p >> 4);
185                 }
186         }
187
188         if(outfname) {
189                 if(!(out = fopen(outfname, "wb"))) {
190                         fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
191                         return 1;
192                 }
193         }
194
195         switch(mode) {
196         case MODE_PNG:
197                 save_image_file(&img, out);
198                 break;
199
200         case MODE_PIXELS:
201                 fwrite(img.pixels, 1, img.scansz * img.height, out);
202                 break;
203
204         case MODE_CMAP:
205                 if(text) {
206                         for(i=0; i<img.cmap_ncolors; i++) {
207                                 printf("%d %d %d\n", img.cmap[i].r, img.cmap[i].g, img.cmap[i].b);
208                         }
209                 } else {
210                         /*fwrite(img.cmap, sizeof img.cmap[0], img.cmap_ncolors, out);*/
211                         fwrite(img.cmap, sizeof img.cmap[0], 1 << img.bpp, out);
212                 }
213                 break;
214
215         case MODE_INFO:
216                 printf("size: %dx%d\n", img.width, img.height);
217                 printf("bit depth: %d\n", img.bpp);
218                 printf("scanline size: %d bytes\n", img.scansz);
219                 if(img.cmap_ncolors > 0) {
220                         printf("colormap entries: %d\n", img.cmap_ncolors);
221                 } else {
222                         printf("color channels: %d\n", img.nchan);
223                 }
224                 break;
225         }
226
227         fclose(out);
228         return 0;
229 }
230
231 void print_usage(const char *argv0)
232 {
233         printf("Usage: %s [options] <input file>\n", argv0);
234         printf("Options:\n");
235         printf(" -o <output file>: specify output file (default: stdout)\n");
236         printf(" -p: dump pixels (default)\n");
237         printf(" -P: output in PNG format\n");
238         printf(" -c: dump colormap (palette) entries\n");
239         printf(" -C <colors>: reduce image down to specified number of colors\n");
240         printf(" -S <lut file>: generate and output shading LUT\n");
241         printf(" -s <shade levels>: used in conjunction with -C or -S (default: 8)\n");
242         printf(" -i: print image information\n");
243         printf(" -t: output as text when possible\n");
244         printf(" -n: swap the order of nibbles (for 4bpp)\n");
245         printf(" -h: print usage and exit\n");
246 }