document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. SDL_Surface* CSurface::RGB2GrayScale( SDL_Surface* Surf_Src){
  2.  
  3. if(Surf_Src == NULL) { //verify if source surface exists
  4.     return NULL;
  5. }
  6.  
  7. SDL_Surface* Surf_Dest = NULL;
  8.  
  9.  
  10. Surf_Dest = SDL_DisplayFormatAlpha(Surf_Src);
  11.  
  12. if(Surf_Dest == NULL) { //verify if temporary surface exists
  13.     return NULL;
  14. }
  15.  
  16. Uint8 R, G, B, A;
  17. Uint32* srcpixels = (Uint32 *)Surf_Src->pixels;;
  18. Uint32* dstpixels = (Uint32 *)Surf_Dest->pixels;
  19. Uint8 intensity;
  20.  
  21. for(uint y = 0; y != Surf_Src->h; y++){
  22.  
  23.     for(uint x = 0; x != Surf_Src->w ; x++){
  24.  
  25.         SDL_GetRGBA(srcpixels[ ( y * Surf_Src->w ) + x ],Surf_Src->format, &R, &G, &B, &A);
  26.  
  27.         intensity = 0.2989 * R + 0.5870 * G + 0.1140 * B;
  28.  
  29.         dstpixels[ ( y * Surf_Src->w ) + x ] = SDL_MapRGBA(Surf_Dest->format, intensity, intensity, intensity, A);
  30.     }
  31. }
  32.  
  33. return Surf_Dest;
  34. }
');