static void putPcxColorInHash(colorhash_table const cht, pixel const newPcxColor, unsigned int const newColorIndex, pixval const maxval) { pixel ppmColor; /* Same color as 'newPcxColor', but at the PPM image's color resolution: 'maxval' */ int rc; PPM_DEPTH(ppmColor, newPcxColor, PCX_MAXVAL, maxval); rc = ppm_lookupcolor(cht, &ppmColor); if (rc == -1) /* This color is not in the hash yet, so we just add it */ ppm_addtocolorhash(cht, &ppmColor, newColorIndex); else { /* This color is already in the hash. That's because the subject image has less color resolution than PCX (i.e. 'maxval' is less than PCX_MAXVAL), and two distinct colors in the standard palette are indistinguishable at subject image color resolution. So we have to figure out wether color 'newPcxColor' or 'existingPcxColor' is a better match for 'ppmColor'. */ unsigned int const existingColorIndex = rc; pixel idealPcxColor; pixel existingPcxColor; PPM_DEPTH(idealPcxColor, ppmColor, maxval, PCX_MAXVAL); PPM_ASSIGN(existingPcxColor, stdPalette[existingColorIndex].r, stdPalette[existingColorIndex].g, stdPalette[existingColorIndex].b); if (PPM_DISTANCE(newPcxColor, idealPcxColor) < PPM_DISTANCE(existingPcxColor, idealPcxColor)) { /* The new PCX color is a better match. Make it the new translation of image color 'ppmColor'. */ ppm_delfromcolorhash(cht, &ppmColor); ppm_addtocolorhash(cht, &ppmColor, newColorIndex); } } }
static void processColorfileEntry(struct colorfile_entry const ce, colorhash_table const cht, const char ** const colornames, pixel * const colors, unsigned int * const colornameIndexP) { if (*colornameIndexP >= MAXCOLORNAMES) pm_error("Too many colors in colorname dictionary. " "Max allowed is %u", MAXCOLORNAMES); else { pixel color; PPM_ASSIGN(color, ce.r, ce.g, ce.b); if (ppm_lookupcolor(cht, &color) >= 0) { /* The color is already in the hash, which means we saw it earlier in the file. We prefer the first name that the file gives for each color, so we just ignore the current entry. */ } else { ppm_addtocolorhash(cht, &color, *colornameIndexP); colornames[*colornameIndexP] = strdup(ce.colorname); colors[*colornameIndexP] = color; if (colornames[*colornameIndexP] == NULL) pm_error("Unable to allocate space for color name"); ++(*colornameIndexP); } } }
static void computecolorhash(pixel ** const pixels, gray ** const alpha, int const cols, int const rows, gray const alphaMaxval, colorhash_table * const chtP, unsigned int * const ncolorsP, bool * const transparentSomewhereP) { /*---------------------------------------------------------------------------- Compute a colorhash_table with one entry for each color in 'pixels' that is not mostly transparent according to alpha mask 'alpha' (which has maxval 'alphaMaxval'). alpha == NULL means all pixels are opaque. The value associated with the color in the hash we build is meaningless. Return the colorhash_table as *chtP, and the number of colors in it as *ncolorsP. Return *transparentSomewhereP == TRUE iff the image has at least one pixel that is mostly transparent. -----------------------------------------------------------------------------*/ colorhash_table cht; int row; cht = ppm_alloccolorhash( ); *ncolorsP = 0; /* initial value */ *transparentSomewhereP = FALSE; /* initial assumption */ /* Go through the entire image, building a hash table of colors. */ for (row = 0; row < rows; ++row) { int col; for (col = 0; col < cols; ++col) { if (!alpha || alpha[row][col] > alphaMaxval/2) { /* It's mostly opaque, so add this color to the hash if it's not already there. */ pixel const color = pixels[row][col]; int const lookupRc = ppm_lookupcolor(cht, &color); if (lookupRc < 0) { /* It's not in the hash yet, so add it */ ppm_addtocolorhash(cht, &color, 0); ++(*ncolorsP); } } else *transparentSomewhereP = TRUE; } } *chtP = cht; }