GifByteType GifTranscoder::computeNewColorIndex(GifFileType* gifIn, int transparentColorIndex, ColorARGB* renderBuffer, int x, int y) { ColorMapObject* colorMap = getColorMap(gifIn); // Compute the average color of 4 adjacent pixels from the input image. ColorARGB c1 = *getPixel(renderBuffer, gifIn->SWidth, x * 2, y * 2); ColorARGB c2 = *getPixel(renderBuffer, gifIn->SWidth, x * 2 + 1, y * 2); ColorARGB c3 = *getPixel(renderBuffer, gifIn->SWidth, x * 2, y * 2 + 1); ColorARGB c4 = *getPixel(renderBuffer, gifIn->SWidth, x * 2 + 1, y * 2 + 1); ColorARGB avgColor = computeAverage(c1, c2, c3, c4); // Search the color map for the best match. return findBestColor(colorMap, transparentColorIndex, avgColor); }
unsigned long APalette::findDarkest() { return findBestColor(0x000000L); }
void APalette::remap(APalette *org) { if(readOnly) return; //if(error()) return; if(tooBig) return; if(!org) { #ifdef DEBUG_VERBOSE dBug<<"NULL palette in remap!\n"; #endif // DEBUG_VERBOSE return; } //if(org->error()) return; if(org->tooBig) return; unsigned int dSize=numColors,sSize=org->numColors; if(isHBrite()) dSize/=2; if(sSize<=dSize) { if(sSize!=dSize) { // Source is smaller than dest, just copy and leave extra colors alone #ifdef DEBUG_VERBOSE dBug<<"remap: src ("<<sSize<<") is smaller than dest ("<<dSize<<")...\n"; #endif // DEBUG_VERBOSE } unsigned int t; for(t=0;t<dSize;t++) used[t]=false; for(t=0;t<sSize;t++) { r[t]=org->r[t]; g[t]=org->g[t]; b[t]=org->b[t]; used[t]=true; if(score&&org->score) score[t]=org->score[t]; } } if(sSize>dSize) { if(sSize!=dSize) { // Source is larger than dest, we need to remap using histogram... #ifdef DEBUG_VERBOSE dBug<<"remap: dest ("<<dSize<<") is smaller than src ("<<sSize<<")...\n"; #endif // DEBUG_VERBOSE } if(!org->histogramComputed()) { #ifdef DEBUG_VERBOSE dBug<<"Source doesn't have histogram!\n"; #endif // DEBUG_VERBOSE return; } // We want the first dSize colors with the highest score values... // NOTE: This destroys org's histogram which is marked as "not done" unsigned int p; long best; for(unsigned int t=0;t<sSize;t++) org->map[t]=-1; #ifdef DEBUG_VERBOSE dBug<<"mapping first dSize popular colors...\n"; #endif // DEBUG_VERBOSE for(unsigned int d=0;d<dSize;d++) { best=0; p=0; for(unsigned int s=0;s<sSize;s++) { if(org->score[s]>best) { p=s; best=org->score[p]; } } if(score) score[d]=org->score[p]; org->score[p]=0; // Mark this color as "used" org->map[p]=d; r[d]=org->r[p]; g[d]=org->g[p]; b[d]=org->b[p]; used[d]=true; if(isHBrite()) { r[d+32]=org->r[p]/2; g[d+32]=org->g[p]/2; b[d+32]=org->b[p]/2; used[d+32]=true; } } #ifdef DEBUG_VERBOSE dBug<<"remapping unpopular colors...\n"; #endif // DEBUG_VERBOSE for(unsigned int o=0;o<sSize;o++) { if(org->map[o]== -1) org->map[o]=(int)findBestColor(org->r[o],org->g[o],org->b[o]); } org->histComputed=false; } org->printMap(); // Now, I think since we copy the scores from org that this is true... // ...but I'm not sure... touch(); histComputed=true; }
unsigned long APalette::findLightest() { return findBestColor(0xffffffL); }
unsigned long APalette::findBestColor(unsigned long triplet) { return findBestColor(getRGBRed24(triplet),getRGBGreen24(triplet),getRGBBlue24(triplet)); }