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