void colorCount()
{
SDL_Surface* source = SDL_LoadBMP("a.bmp");
Slock(source); // lock surface
rgb col; // the current color we are working with
pair< map<int, rgb>::iterator, bool > pr;
map<int, rgb>::size_type size;
// COUNT THE COLORS ----------------------------------------------------------------------------
for(int y = 0; y < source->h; y++)
{
for(int x = 0; x < source->w; x++)
{
// get the color in Uint32
SDL_GetRGB(getPixel(source, x, y), source->format, &(col.r), &(col.g), &(col.b));
map<int, rgb>::iterator i;
for(i = colors.begin(); i != colors.end(); i++)
{
if(i->second == col)
{
i->second.count++; // we have seen it before, increase the count
break;
}
}
if(i == colors.end()) // we havent seen this color before, store it in the map
{
size = colors.size();
pr = colors.insert(col_pair(size, col));
}
}
cout << y << " lines of " << source->h << " done, found " << colors.size() << " unique colors so far." << endl;
}
cout << "Color counting DONE.\n----------------------------------------------------------------\n\n";
Sulock(source); // unlock surface
}
class rgb
{
public:
Uint8 r;
Uint8 g;
Uint8 b;
int count; // how many times have we seen this exact color?
rgb()
{
r = g = b = 0;
count = 1;
}
bool operator==(const rgb& a)
{
if(r == a.r && g == a.g && b == a.b)
return true;
else
return false;
}
};
map<int, rgb> colors;
typedef pair <int, rgb> col_pair;