View Full Version : FreeImage doesn't work

10-29-2010, 07:22 AM
Hey guys.
I have a big problem and I don't know how to solve this.
I am loading an image file with this function:

bool LoadTexture(const char* filename, GLenum imageFormat, GLint internalFormat, BYTE *imageData)
//pointer to the image, once loaded
FIBITMAP *dib(0);
//check the file signature and deduce its format
fif = FreeImage_GetFileType(filename, 0);
//if still unknown, try to guess the file format from the file extension
if(fif == FIF_UNKNOWN)
fif = FreeImage_GetFIFFromFilename(filename);
//if still unkown, return failure
if(fif == FIF_UNKNOWN)
return false;

//check that the plugin has reading capabilities and load the file
dib = FreeImage_Load(fif, filename);
//if the image failed to load, return failure
return false;

//retrieve the image data
imageData = FreeImage_GetBits(dib);
//if this somehow one of these failed (they shouldn't), return failure
if(imageData == 0)
return false;

//Free FreeImage's copy of the data

//return success
return true;

When I try to render this Image Data with

glDrawPixels(64, 64, GL_RGB, GL_BYTE, iData);

I only see a black rectangle on the screen and I don't know why. Please can anyone help?

10-29-2010, 09:03 AM
FreeImage_GetBits returns pointer that is valid until you call FreeImage_Unload. So you are calling glDrawPixels with point to unallocated memory.

10-30-2010, 09:09 AM
Ok thank you. But there is another problem. The picture should be red but it is still green. Why?
I tried to change GL_RGB to GL_BGR but now it is blue.

10-30-2010, 09:38 AM
I found out that I need to use GL_UNSIGNED_BYTE instead of GL_BYTE but please can anyone tell me why?

10-30-2010, 09:45 AM
GL_UNSIGNED_BYTE means that values are in range 0..255
GL_BYTE is signed number from -128 to 127.
Images are usually stored using unsigned numbers.

beware that FreeImage loads the image in pixel format defined by the image. It could be 8bit, 16bit, float, rgb, rgba, ....