SDL_Surface* CSurface::RGB2GrayScale( SDL_Surface* Surf_Src){
if(Surf_Src == NULL) { //verify if source surface exists
return NULL;
}
SDL_Surface* Surf_Dest = NULL;
Surf_Dest = SDL_DisplayFormatAlpha(Surf_Src);
if(Surf_Dest == NULL) { //verify if temporary surface exists
return NULL;
}
Uint8 R, G, B, A;
Uint32* srcpixels = (Uint32 *)Surf_Src->pixels;;
Uint32* dstpixels = (Uint32 *)Surf_Dest->pixels;
Uint8 intensity;
for(uint y = 0; y != Surf_Src->h; y++){
for(uint x = 0; x != Surf_Src->w ; x++){
SDL_GetRGBA(srcpixels[ ( y * Surf_Src->w ) + x ],Surf_Src->format, &R, &G, &B, &A);
intensity = 0.2989 * R + 0.5870 * G + 0.1140 * B;
dstpixels[ ( y * Surf_Src->w ) + x ] = SDL_MapRGBA(Surf_Dest->format, intensity, intensity, intensity, A);
}
}
return Surf_Dest;
}