// // file name : alloc_cl.C // This file contains only one function AllocColor(). // It is from XV source codes with minor changes. // AllocColor will allocate the specified colormap // from the colors arrays r[], g[], b[], and then // put the map from the color arrays index to // X pixel values in the array cols[]. // #include #include #include #define NOPIX 0xffffffff typedef unsigned char byte; extern byte r[256], g[256], b[256]; // colormap extern int nfcols; // # of colors to free extern int ncols; // max # of colors to alloc extern int numcols; // # of desired colors extern unsigned long freecols[256]; // list of pixel values to free extern unsigned long cols[256]; // maps pic pixels vals to X pixel vals extern int fc2pcol[256]; // maps freecols into pic pixel vals void AllocColor(Display *theDisp, Window mainW, Colormap theCmap) { int i, j, unique, p2alloc, p3alloc; Colormap cmap; XColor defs[256]; XColor ctab[256]; int dc; nfcols = unique = p2alloc = p3alloc = 0; if (ncols == 0) { fprintf(stderr,"no colors allocated. Using black & white.\n"); return; } /* FIRST PASS COLOR ALLOCATION: for each color in the 'desired colormap', try to get it via XAllocColor(). If for any reason it fails, mark that pixel for each color in the 'desired colormap', try to get it via XAllocColor(). If for any reason it fails, mark that pixel 'unallocated' and worry about it later. Repeat. */ /* attempt to allocate first ncols entries in colormap note: On displays with less than 8 bits per RGB gun, it's quite possible that different colors in the original picture will be mapped to the same color on the screen. X does this for you silently. However, this is not-desirable for this application, because when I say 'allocate me 32 colors' I want it to allocate because when I say 'allocate me 32 colors' I want it to allocate 32 different colors, not 32 instances of the same 4 shades... */ for (i=0; i>8)) + abs(gi - (ctab[j].green>>8)) + abs(bi - (ctab[j].blue>>8)); if (d>8)) + abs(gi - (ctab[j].green>>8)) + abs(bi - (ctab[j].blue>>8)); if (d1) ? "s" : "", p3alloc, (p3alloc>1) ? "s" : ""); else if (p2alloc && !p3alloc) fprintf(stderr,"Got %d 'close' color%s.", p2alloc, (p2alloc>1) ? "s" : ""); else if (!p2alloc && p3alloc) fprintf(stderr,"'Borrowed' %d color%s.", p3alloc, (p3alloc>1) ? "s" : ""); */ }