PDA

View Full Version : Bitmap Image Loading fails



charisma
03-06-2011, 05:00 PM
I have the following code:



BOOL LoadBitmap(LPTSTR szFileName, GLuint &texture)
{

HANDLE fileHandle;

//open the bitmap file
fileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if( fileHandle == INVALID_HANDLE_VALUE )
{
MessageBox(hWnd,"Cannot open file","ERROR",MB_OKCANCEL | MB_ICONASTERISK);
exit(0);
return FALSE;
}



BITMAPFILEHEADER bmpHeader;
BYTE *pBitmapData;
BITMAPINFO *pBitmapInfo;
DWORD dwBytesToRead;

//read in the bitmap header information
ReadFile(fileHandle,&bmpHeader,sizeof(BITMAPFILEHE ADER),&dwBytesToRead,NULL);
int texWidth, texHeight;

try{
if( dwBytesToRead != sizeof(BITMAPFILEHEADER) )
{
return FALSE;
}

//check format of bitmap file
if ( bmpHeader.bfType != 'MB')
{
return FALSE;
}


/*read in bitmap information structure*/
unsigned long lInfoSize;
unsigned long lBitSize;

lInfoSize = bmpHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
pBitmapInfo = (BITMAPINFO *) malloc(sizeof(BYTE)*lInfoSize);

ReadFile(fileHandle,pBitmapInfo,lInfoSize,&dwBytes ToRead,NULL);

if(dwBytesToRead != lInfoSize){
free(pBitmapInfo);
CloseHandle(fileHandle);
return FALSE;
}

texWidth = pBitmapInfo->bmiHeader.biWidth;
texHeight = pBitmapInfo->bmiHeader.biHeight;
lBitSize = pBitmapInfo->bmiHeader.biSizeImage;

if( lBitSize == 0)
{
lBitSize = (texWidth * pBitmapInfo->bmiHeader.biBitCount + 7 ) / 8 * abs(texHeight);
}
/*allocate space for teh actual bitmap*/
pBitmapData = (BYTE *) malloc (sizeof(BYTE) * lBitSize);

/*read in the bitmap bits*/

ReadFile(fileHandle,pBitmapData,lBitSize,&dwBytesT oRead,NULL);

if( lBitSize != dwBytesToRead)
{
if(pBitmapData)
free(pBitmapData);
pBitmapData = NULL;
return FALSE;
}

}
catch(exception &ex)
{
MessageBox(hWnd,ex.what(),"Error",MB_OKCANCEL);
exit(0);
}

CloseHandle(fileHandle);
if( pBitmapInfo != NULL)
free(pBitmapInfo);

glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
glTexImage2D(GL_TEXTURE_2D,0,3,texWidth,texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,pBitmapData);

if(pBitmapData)
free(pBitmapData);
return TRUE;
}


But I always end up fails at opening the file.

I use the visual studio 2008.

Thank you if you could help me abit.

mobeen
03-06-2011, 08:33 PM
The code looks ok maybe the image is not in the folder u r referring in the code. Double check the folder/ image filename

charisma
03-06-2011, 09:55 PM
I have put the image files in any kind of possible places. But it still can't work. This is so sad as I can't progress my learning before completing this texture stuff.