Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- surface greyscale_image(const surface &surf, bool optimize)
- {
- if(surf == NULL)
- return NULL;
- surface nsurf(make_neutral_surface(surf));
- if(nsurf == NULL) {
- std::cerr << "failed to make neutral surface\n";
- return NULL;
- }
- {
- surface_lock lock(nsurf);
- Uint32* beg = lock.pixels();
- Uint32* end = beg + nsurf->w*surf->h;
- while(beg != end) {
- Uint8 alpha = (*beg) >> 24;
- if(alpha) {
- Uint8 r, g, b;
- r = (*beg) >> 16;
- g = (*beg) >> 8;
- b = (*beg);
- //const Uint8 avg = (red+green+blue)/3;
- // Use the correct formula for RGB to grayscale conversion.
- // Ok, this is no big deal :)
- // The correct formula being:
- // gray=0.299red+0.587green+0.114blue
- const Uint8 avg = static_cast<Uint8>((
- 77 * static_cast<Uint16>(r) +
- 150 * static_cast<Uint16>(g) +
- 29 * static_cast<Uint16>(b) ) / 256);
- *beg = (alpha << 24) | (avg << 16) | (avg << 8) | avg;
- }
- ++beg;
- }
- }
- return optimize ? create_optimized_surface(nsurf) : nsurf;
- }
Advertisement
Add Comment
Please, Sign In to add comment