16 void print_usage(const char *argv0);
18 int main(int argc, char **argv)
26 struct image img, tmpimg;
28 struct cmapent shade_cmap[256] = {0};
32 for(i=1; i<argc; i++) {
33 if(argv[i][0] == '-') {
49 mode = MODE_SHADE_CMAP;
53 mode = MODE_SHADE_LUT;
57 if(!argv[++i] || (shade_levels = atoi(argv[i])) == 0) {
58 fprintf(stderr, "-s must be followed by the number of shade levels\n");
73 fprintf(stderr, "%s must be followed by a filename\n", argv[i - 1]);
84 fprintf(stderr, "invalid option: %s\n", argv[i]);
89 fprintf(stderr, "invalid option: %s\n", argv[i]);
94 infiles[num_infiles++] = argv[i];
99 fprintf(stderr, "pass the filename of a PNG file\n");
102 if(load_image(&img, infiles[0]) == -1) {
103 fprintf(stderr, "failed to load PNG file: %s\n", infiles[0]);
107 for(i=1; i<num_infiles; i++) {
108 if(load_image(&tmpimg, infiles[i]) == -1) {
109 fprintf(stderr, "failed to load PNG file: %s\n", infiles[i]);
112 if(tmpimg.width != img.width || tmpimg.height != img.height) {
113 fprintf(stderr, "size mismatch: first image (%s) is %dx%d, %s is %dx%d\n",
114 infiles[0], img.width, img.height, infiles[i], tmpimg.width, tmpimg.height);
117 if(tmpimg.bpp != img.bpp) {
118 fprintf(stderr, "bpp mismatch: first image (%s) is %d bpp, %s is %d bpp\n",
119 infiles[0], img.bpp, infiles[i], img.bpp);
123 overlay_key(&tmpimg, 0, &img);
126 if(img.bpp == 4 && renibble) {
127 unsigned char *ptr = img.pixels;
128 for(i=0; i<img.width * img.height; i++) {
129 unsigned char p = *ptr;
130 *ptr++ = (p << 4) | (p >> 4);
135 if(!(out = fopen(outfname, "wb"))) {
136 fprintf(stderr, "failed to open output file: %s: %s\n", outfname, strerror(errno));
143 fwrite(img.pixels, 1, img.scansz * img.height, out);
148 for(i=0; i<img.cmap_ncolors; i++) {
149 printf("%d %d %d\n", img.cmap[i].r, img.cmap[i].g, img.cmap[i].b);
152 /*fwrite(img.cmap, sizeof img.cmap[0], img.cmap_ncolors, out);*/
153 fwrite(img.cmap, sizeof img.cmap[0], 1 << img.bpp, out);
158 printf("size: %dx%d\n", img.width, img.height);
159 printf("bit depth: %d\n", img.bpp);
160 printf("scanline size: %d bytes\n", img.scansz);
161 if(img.cmap_ncolors > 0) {
162 printf("colormap entries: %d\n", img.cmap_ncolors);
164 printf("color channels: %d\n", img.nchan);
169 if(!(shade_lut = malloc(256 * shade_levels * sizeof *shade_lut))) {
170 fprintf(stderr, "failed to allocate shading look-up table\n");
173 case MODE_SHADE_CMAP:
174 if(!img.cmap_ncolors) {
175 fprintf(stderr, "can't generate shade levels for non-indexed images\n");
178 if(gen_shade_lut(&img, shade_levels, 256, shade_cmap, shade_lut) == -1) {
181 if(mode == MODE_SHADE_CMAP) {
182 fwrite(shade_cmap, sizeof shade_cmap, 1, out);
184 for(i=0; i<img.cmap_ncolors * shade_levels; i++) {
185 fputc(shade_lut[i], out);
195 void print_usage(const char *argv0)
197 printf("Usage: %s [options] <input file>\n", argv0);
198 printf("Options:\n");
199 printf(" -o <output file>: specify output file (default: stdout)\n");
200 printf(" -p: dump pixels (default)\n");
201 printf(" -c: dump colormap (palette) entries\n");
202 printf(" -C: generate shading colormap\n");
203 printf(" -S: generate shading LUT\n");
204 printf(" -s <shade levels>: used in conjunction with -C or -S (default: 8)\n");
205 printf(" -i: print image information\n");
206 printf(" -t: dump as text\n");
207 printf(" -n: swap the order of nibbles (for 4bpp)\n");
208 printf(" -h: print usage and exit\n");