_ALGORITHM ALLEY_ written by Dean Clark edited by Bruce Schneier Example 1 /**************************************************************************** ** AddColorToList ** Adds a color to the list of most popular colors in the image, if necessary *****************************************************************************/ void AddColorToList(COLOR_NODE **popcolors, int npal, int *currentcount, COLOR_NODE *color) { COLOR_NODE *temp; int i; /* If table is empty then insert this color */ if (*currentcount < 0) { *currentcount = 0; popcolors[*currentcount] = color; return; } /* Search the table for the same color */ for (i = 0; i <= *currentcount; i++) { if (popcolors[i] == color) break; } if (popcolors[i] == color) { /* Found it. Since the color is already in the table, adjust it ** to its proper position */ while (i && (popcolors[i-1]->count < popcolors[i]->count)) { temp = popcolors[i-1]; popcolors[i-1] = popcolors[i]; popcolors[i] = temp; i--; } return; } /* This color isn't in the list. See if it belongs there. First of all, ** if the list isn't full, this color must have a count of 1 and can be ** simply added to the end */ if (*currentcount < npal-1) { (*currentcount)++; popcolors[*currentcount] = color; } else { /* Otherwise the list is full and this color may belong in the list. ** Start at the low end (if the color had a high count it would ** already be in the list) */ if (color->count > popcolors[npal-1]->count) { i = npal - 1; popcolors[i] = color; i--; while ((i >= 0) && (popcolors[i]->count < popcolors[i+1]->count)) { temp = popcolors[i+1]; popcolors[i+1] = popcolors[i]; popcolors[i] = temp; i--; } } } }