static void mkclrmap( /* make our color map */ int nc ) { register int i; if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1) goto memerr; for (i = 0; i < ymax; i++) { getrow(i); if (samplefac) neu_colrs(scanln, xmax); else cnt_colrs(scanln, xmax); } if (samplefac) neu_clrtab(nc); else new_clrtab(nc); for (i = 0; i < nc; i++) { rmap[i] = clrtab[i][RED]; gmap[i] = clrtab[i][GRN]; bmap[i] = clrtab[i][BLU]; } if (dither && (pixscan = (uby8 *)malloc(xmax)) == NULL) goto memerr; return; memerr: fprintf(stderr, "%s: out of memory\n", progname); exit(1); }
void remap_imagecolors(void) { int i; /* if monochrome, return */ if (tool_cells <= 2 || appres.monochrome) return; npixels = 0; /* first see if there are enough colorcells for all image colors */ usenet = False; /* see if the total number of colors will fit without using the neural net */ ncolors = count_colors(); if (ncolors == 0) return; put_msg(REMAP_MSG); set_temp_cursor(wait_cursor); app_flush(); if (ncolors > appres.max_image_colors) { if (appres.DEBUG) fprintf(stderr,"More colors (%d) than allowed (%d), using neural net\n", ncolors,appres.max_image_colors); ncolors = appres.max_image_colors; usenet = True; } /* if this is the first image, allocate the number of colorcells we need */ if (num_oldcolors != ncolors) { if (num_oldcolors != -1) { unsigned long pixels[MAX_USR_COLS]; for (i=0; i<num_oldcolors; i++) pixels[i] = image_cells[i].pixel; if (tool_vclass == PseudoColor) XFreeColors(tool_d, tool_cm, pixels, num_oldcolors, 0); } alloc_imagecolors(ncolors); /* hmm, we couldn't get that number of colors anyway; use the net, Luke */ if (ncolors > avail_image_cols) { usenet = True; if (appres.DEBUG) fprintf(stderr,"More colors (%d) than available (%d), using neural net\n", ncolors,avail_image_cols); } num_oldcolors = avail_image_cols; if (avail_image_cols < 2 && ncolors >= 2) { file_msg("Cannot allocate even 2 colors for pictures"); reset_cursor(); num_oldcolors = -1; reset_cursor(); put_msg(REMAP_MSG2); app_flush(); return; } } reset_cursor(); if (usenet) { int stat; int mult = 1; /* check if user pressed cancel button (in file preview) */ if (check_cancel()) return; /* count total number of pixels in all the pictures */ npixels = count_pixels(); /* check if user pressed cancel button */ if (check_cancel()) return; /* initialize the neural network */ /* -1 means can't alloc memory, -2 or more means must have that many times as many pixels */ set_temp_cursor(wait_cursor); if ((stat=neu_init(npixels)) <= -2) { mult = -stat; npixels *= mult; /* try again with more pixels */ stat = neu_init2(npixels); } if (stat == -1) { /* couldn't alloc memory for network */ fprintf(stderr,"Can't alloc memory for neural network\n"); reset_cursor(); put_msg(REMAP_MSG2); app_flush(); return; } /* now add all pixels to the samples */ for (i=0; i<mult; i++) add_all_pixels(); /* make a new colortable with the optimal colors */ avail_image_cols = neu_clrtab(avail_image_cols); /* now change the color cells with the new colors */ /* clrtab[][] is the colormap produced by neu_clrtab */ for (i=0; i<avail_image_cols; i++) { image_cells[i].red = (unsigned short) clrtab[i][N_RED] << 8; image_cells[i].green = (unsigned short) clrtab[i][N_GRN] << 8; image_cells[i].blue = (unsigned short) clrtab[i][N_BLU] << 8; } YStoreColors(tool_cm, image_cells, avail_image_cols); reset_cursor(); /* check if user pressed cancel button */ if (check_cancel()) return; /* get the new, mapped indices for the image colormap */ remap_image_colormap(); } else { /* * Extract the RGB values from the image's colormap and allocate * the appropriate X colormap entries. */ scol = 0; /* global color counter */ set_temp_cursor(wait_cursor); extract_cmap(); for (i=0; i<scol; i++) { image_cells[i].flags = DoRed|DoGreen|DoBlue; } YStoreColors(tool_cm, image_cells, scol); scol = 0; /* global color counter */ readjust_cmap(); if (appres.DEBUG) fprintf(stderr,"Able to use %d colors without neural net\n",scol); reset_cursor(); } put_msg(REMAP_MSG2); app_flush(); }