+ /* generate shading LUT and quantize image as necessary */
+ if(slut_fname) {
+ if(img.bpp > 8) {
+ fprintf(stderr, "shading LUT generation is only supported for indexed color images\n");
+ return 1;
+ }
+ if(!(slut_out = fopen(slut_fname, "wb"))) {
+ fprintf(stderr, "failed to open shading LUT output file: %s: %s\n", slut_fname, strerror(errno));
+ return 1;
+ }
+
+ if(!maxcol) maxcol = 256;
+
+ if(!(shade_lut = malloc(maxcol * shade_levels * sizeof *shade_lut))) {
+ fprintf(stderr, "failed to allocate shading look-up table\n");
+ return 1;
+ }
+
+ gen_shades(&img, shade_levels, maxcol, shade_lut);
+
+ lutptr = shade_lut;
+ for(i=0; i<maxcol; i++) {
+ for(j=0; j<shade_levels; j++) {
+ if(text) {
+ fprintf(slut_out, "%d%c", *lutptr++, j < shade_levels - 1 ? ' ' : '\n');
+ } else {
+ fputc(*lutptr++, slut_out);
+ }
+ }
+ }
+ fclose(slut_out);
+
+ } else if(maxcol) {
+ /* perform any color reductions if requested */
+ if(img.bpp <= 8 && img.cmap_ncolors <= maxcol) {
+ fprintf(stderr, "requested reduction to %d colors, but image has %d colors\n", maxcol, img.cmap_ncolors);
+ return 1;
+ }
+ quantize_image(&img, maxcol);
+ }
+