Optimize recoloring function:

Strangely it seems that someone didn't used the find function of
STL-map and instead checked the whole map until finding a match (but
why using a map then?)  Obviously it was very slow (done for each
pixel, costed ~75% of the total time)
This commit is contained in:
Ali El Gariani 2008-04-19 17:02:11 +00:00
parent 122765501d
commit 6a5cd8a80a

View File

@ -648,21 +648,21 @@ surface recolor_image(surface surf, const std::map<Uint32, Uint32>& map_rgb){
Uint32* beg = lock.pixels();
Uint32* end = beg + nsurf->w*surf->h;
std::map<Uint32, Uint32>::const_iterator map_rgb_end = map_rgb.end();
while(beg != end) {
Uint8 red, green, blue, alpha;
SDL_GetRGBA(*beg,nsurf->format,&red,&green,&blue,&alpha);
if(alpha){ // don't recolor invisible pixels.
Uint32 oldrgb = (red<<16) + (green<<8) + (blue);
for(std::map<Uint32, Uint32>::const_iterator i=map_rgb.begin(); i!= map_rgb.end(); i++){
if(oldrgb==i->first){
Uint32 new_rgb = i->second;
Uint8 new_r = (new_rgb & 0x00FF0000)>>16;
Uint8 new_g = (new_rgb & 0x0000FF00)>>8;
Uint8 new_b = (new_rgb & 0x000000FF);
std::map<Uint32, Uint32>::const_iterator i = map_rgb.find(oldrgb);
if(i != map_rgb_end){
Uint32 new_rgb = i->second;
Uint8 new_r = (new_rgb & 0x00FF0000)>>16;
Uint8 new_g = (new_rgb & 0x0000FF00)>>8;
Uint8 new_b = (new_rgb & 0x000000FF);
*beg = SDL_MapRGBA(nsurf->format,new_r,new_g,new_b,alpha);
break; // no double replacements.
}
}
}
++beg;