I'm trying to save the output of my rendering to file. I'm already using FreeImage as a dependency so I'd like to continue using it. I'm using glReadPixels
to read the RGB values from the buffer. These values were entered into the buffer as GLubytes, so I figure I need to read them as such. However, when I run the following code snippet, the FreeImage_ConvertFromRawBits
call seg faults and I don't understand why. I've consulted the docs, and it seems that I'm doing everything correctly. Has anyone else run into this issue?
GLubyte pixels[3*_winWidth*_winHeight];
glReadPixels(0, 0, _winWidth, _winHeight, GL_RGB, GL_UNSIGNED_BYTE, pixels);
FIBITMAP *bitmap = FreeImage_ConvertFromRawBits(pixels, _winWidth,
_winHeight, 3 *_winWidth, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK, false);
I saw this question a bit after you posted it. If I recall correctly, I think you said your snippet was simplified in contrast to what you're actually using.
Thus the best guess then is that you aren't allocating enough memory for pixels
.
Here is a more complete example for taking a screenshot and saving it with FreeImage.
BYTE *pixels = (BYTE*)malloc(width * height * 3);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pixels);
FIBITMAP *image = FreeImage_ConvertFromRawBits(pixels, width, height, 3 * width, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
if (FreeImage_Save(FIF_BMP, image, "screenshot.bmp", 0))
printf("Successfully saved!\n");
else
printf("Failed saving!\n");
FreeImage_Unload(image);
free(pixels);
Here you can see that OpenGL as well as the photo viewer are showing the same.
Lastly I remembered something about the color mask being redundant. I looked a bit around and found this thread from 2005.
At the moment, color mask are only used for 16-bit RGB images, that's why your code produces two identical images.
It's of course an old thread, so take it with a grain of salt. I wasn't however able to find any newer mentions of it. Changing the masks for FreeImage_ConvertFromRawBits()
had no effect when I tested it.