15 void print_usage(const char *argv0);
17 int main(int argc, char **argv)
26 struct image img, tmpimg;
34 for(i=1; i<argc; i++) {
35 if(argv[i][0] == '-') {
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");
62 if(!argv[++i] || (shade_levels = atoi(argv[i])) == 0) {
63 fprintf(stderr, "-s must be followed by the number of shade levels\n");
78 fprintf(stderr, "%s must be followed by a filename\n", argv[i - 1]);
86 fprintf(stderr, "-S must be followed by a filename\n");
97 fprintf(stderr, "invalid option: %s\n", argv[i]);
102 fprintf(stderr, "invalid option: %s\n", argv[i]);
103 print_usage(argv[0]);
107 infiles[num_infiles++] = argv[i];
112 fprintf(stderr, "pass the filename of a PNG file\n");
115 if(load_image(&img, infiles[0]) == -1) {
116 fprintf(stderr, "failed to load PNG file: %s\n", infiles[0]);
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]);
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);
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);
136 overlay_key(&tmpimg, 0, &img);
139 /* generate shading LUT and quantize image as necessary */
142 fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
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));
150 if(!maxcol) maxcol = 256;
152 if(!(shade_lut = malloc(maxcol * shade_levels * sizeof *shade_lut))) {
153 fprintf(stderr, "failed to allocate shading look-up table\n");
157 gen_shades(&img, shade_levels, maxcol, shade_lut);
160 for(i=0; i<maxcol; i++) {
161 for(j=0; j<shade_levels; j++) {
163 fprintf(slut_out, "%d%c", *lutptr++, j < shade_levels - 1 ? ' ' : '\n');
165 fputc(*lutptr++, slut_out);
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);
177 quantize_image(&img, maxcol);
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);
189 if(!(out = fopen(outfname, "wb"))) {
190 fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
197 save_image_file(&img, out);
201 fwrite(img.pixels, 1, img.scansz * img.height, out);
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);
210 /*fwrite(img.cmap, sizeof img.cmap[0], img.cmap_ncolors, out);*/
211 fwrite(img.cmap, sizeof img.cmap[0], 1 << img.bpp, out);
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);
222 printf("color channels: %d\n", img.nchan);
231 void print_usage(const char *argv0)
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");