PDA

View Full Version : FreeImage doesn't work



Limas
10-29-2010, 08: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)
{
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
//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
if(FreeImage_FIFSupportsReading(fif))
dib = FreeImage_Load(fif, filename);
//if the image failed to load, return failure
if(!dib)
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
FreeImage_Unload(dib);

//return success
return true;
}


When I try to render this Image Data with



glRasterPos2i(50,0);
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?

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

Limas
10-30-2010, 10: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.

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

mfort
10-30-2010, 10: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, ....