I'm trying to save a screen region from opengl into a bitmap. I've tried using FreeImage, and SDL_Image, and they both require me to swap red and blue channels. Of course, that brings me to suspect that glReadPixels is the problem here... I have this sample code:
bool CaptureScreenRegionToFile ( uint32_t In_X, uint32_t In_Y, uint32_t In_Width, uint32_t In_Height, std::string In_Filename )
{
GLubyte* ImageData = ( GLubyte* ) malloc ( In_Width * In_Height * 3 );
glPixelStorei ( GL_PACK_ALIGNMENT, 1 );
glReadPixels ( In_X, In_Y, In_Width, In_Height, GL_RGB, GL_UNSIGNED_BYTE, ImageData );
if ( CheckError() == false )
{
free ( ImageData );
return false;
}
SDL_Surface *Surface;
// JTP TODO Known bug here. Red and blue are swapped, for some reason...
Surface = SDL_CreateRGBSurfaceFrom ( ImageData, In_Width, In_Height, 3 * 8, In_Width * 3, 0x00FF0000, 0x0000FF00, 0x000000FF, 0 );
SDL_SaveBMP ( Surface, In_Filename.c_str() );
SDL_FreeSurface ( Surface );
free ( ImageData );
return true;
}
Unless I swap the red and blue channels manually after the call to CreateRGBSurfaceFrom, its colors are going to be swapped on the BMP. Is glReadPixels supposed to do this? Am I calling it correctly? What's wrong here?