15 void dump_colormap(struct image *img, int text, FILE *fp);
16 void print_usage(const char *argv0);
18 int main(int argc, char **argv)
24 char *slut_fname = 0, *cmap_fname = 0;
27 struct image img, tmpimg;
36 for(i=1; i<argc; i++) {
37 if(argv[i][0] == '-') {
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");
64 if(!argv[++i] || (shade_levels = atoi(argv[i])) == 0) {
65 fprintf(stderr, "-s must be followed by the number of shade levels\n");
80 fprintf(stderr, "%s must be followed by a filename\n", argv[i - 1]);
91 fprintf(stderr, "invalid option: %s\n", argv[i]);
96 if(strcmp(argv[i], "-oc") == 0) {
98 fprintf(stderr, "-oc must be followed by a filename\n");
101 cmap_fname = argv[i];
103 } else if(strcmp(argv[i], "-os") == 0) {
105 fprintf(stderr, "-os must be followed by a filename\n");
108 slut_fname = argv[i];
111 fprintf(stderr, "invalid option: %s\n", argv[i]);
112 print_usage(argv[0]);
117 infiles[num_infiles++] = argv[i];
122 fprintf(stderr, "pass the filename of a PNG file\n");
125 if(load_image(&img, infiles[0]) == -1) {
126 fprintf(stderr, "failed to load PNG file: %s\n", infiles[0]);
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]);
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);
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);
146 overlay_key(&tmpimg, 0, &img);
149 /* generate shading LUT and quantize image as necessary */
152 fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
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));
160 if(!maxcol) maxcol = 256;
162 if(!(shade_lut = malloc(maxcol * shade_levels * sizeof *shade_lut))) {
163 fprintf(stderr, "failed to allocate shading look-up table\n");
167 gen_shades(&img, shade_levels, maxcol, shade_lut);
170 for(i=0; i<maxcol; i++) {
171 for(j=0; j<shade_levels; j++) {
172 lvl = lutptr[shade_levels - j - 1];
174 fprintf(aux_out, "%d%c", lvl, j < shade_levels - 1 ? ' ' : '\n');
179 lutptr += shade_levels;
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);
189 quantize_image(&img, maxcol);
194 fprintf(stderr, "colormap output works only for indexed color images\n");
197 if(!(aux_out = fopen(cmap_fname, "wb"))) {
198 fprintf(stderr, "failed to open colormap output file: %s: %s\n", cmap_fname, strerror(errno));
201 dump_colormap(&img, text, aux_out);
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);
214 if(!(out = fopen(outfname, "wb"))) {
215 fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
222 save_image_file(&img, out);
226 fwrite(img.pixels, 1, img.scansz * img.height, out);
230 dump_colormap(&img, text, out);
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);
240 printf("color channels: %d\n", img.nchan);
249 void dump_colormap(struct image *img, int text, FILE *fp)
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);
258 fwrite(img->cmap, sizeof img->cmap[0], 1 << img->bpp, fp);
262 void print_usage(const char *argv0)
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");