problem loading bmp files....

Started by
6 comments, last by logout 20 years, 3 months ago
I have a problem loading bmp files.. it just wont work ! !! ! ! The idea is that im going to load the bmp. It must be a 24bit bmp. And then im going to add a alpha channel and set it to 0 if the color equal the colorkey. The image now show a white space ... *my tga loading rutine is working but i want my bmp thing to work too... : the code displaying the image is ok* Here is the code i use for loading a bmp: *removed most of the error handling to make it easy to read* EDIT: plz exuse me for not fixing the tabs n stuff.. its not how the code look in my IDE EDIT2: dint know editing screwed the source tags... put some /////// into to seperate :D

uint CTexture::LoadBMP( string strFile )
{
	ifstream			fin;

	BITMAPFILEHEADER	bmp_header;
	BITMAPINFOHEADER	bmp_info;

	BYTE*				bImageData = NULL;
	WORD				wChannels = 0;
	DWORD				dwImageSize = 0;

	BYTE*				bFinalData = NULL;
	WORD				wFinalChannels = 0;
	DWORD				dwFinalSize = 0;

	uint				iID = 0; //return id


	fin.open( strFile.c_str(), ios::binary | ios::in );
	if( fin.fail() )
	{
	}



	fin.read( (char*)&bmp_header , sizeof(bmp_header) );
	if( !fin.good() ) // read error ?

	{
	}

	if( bmp_header.bfType != 0x4D42 ) // check that is a bmp

	{
	}

	fin.read( (char*)&bmp_info, sizeof(bmp_info) );
	if( !fin.good() ) // read error ?

	{
	}


	// move to the start of data

	fin.seekg( bmp_header.bfOffBits, ios::beg );

	// how many channels

	wChannels = bmp_info.biBitCount / 8;
	if( wChannels != 3 )
	{
	}

	wFinalChannels = 4;
	dwImageSize = abs(bmp_info.biWidth) * abs(bmp_info.biHeight) * wChannels;
	dwFinalSize = abs(bmp_info.biWidth) * abs(bmp_info.biHeight) * wFinalChannels;

	// allocate mem for the image data

	bImageData = new BYTE[dwImageSize];
	if( bImageData == NULL)
	{
	}

	bFinalData = new BYTE[dwFinalSize];
	if( bFinalData == NULL)
	{
	}

	// read the pixel data

	fin.read( (char*)bImageData, dwImageSize );
	if( !fin.good() ) // any unexpected errors(1) ?

	{
	}

	fin.close(); // close the file


	// convert & fake the image

	// convert BGR into RGB

	uint src;
	uint dst;
	for( src = 0, dst = 0;src < dwImageSize;src += wChannels, dst += wFinalChannels)
	{
		BYTE tmp			= bImageData[src+2];
		bImageData[src+2]	= bImageData[src];
		bImageData[src]	= tmp;

		// copy the reults over to the new header

		bFinalData[dst]		= bImageData[src];
		bFinalData[dst+1]	= bImageData[src+1];
		bFinalData[dst+2]	= bImageData[src+2];
		bFinalData[dst+3]	= 0xFF;
		
		// r g b a

		Colorkey( bFinalData[dst], bFinalData[dst+1], bFinalData[dst+2], bFinalData[dst+3] );
	}

	iID = CreateTexture( bImageData, strFile, bmp_info.biWidth, bmp_info.biHeight );
	
	delete[] bImageData;
	delete[] bFinalData;

	return( iID );
}

//////////////////////////////////////////////////////////////

//here is the colorkey function:

//////////////////////////////////////////////////////////////   

void Colorkey( BYTE& r, BYTE& g, BYTE& b, BYTE& a )
{
	// check if we should colorkey it

	if( (r == COLORKEY_R) && (g == COLORKEY_G) && (b == COLORKEY_B) )
		a = 0;
	else
		a = 0xFF;
}

//////////////////////////////////////////////////////////////

//here is the CreateTexture function:

//////////////////////////////////////////////////////////////

uint CreateTexture( BYTE* bImageData, const string& strFile, DWORD sizeX, DWORD sizeY )
{
	uint	ID;
	glGenTextures( 1, &ID );

	glBindTexture( GL_TEXTURE_2D, ID );

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, sizeX, sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, bImageData ); 

	return( ID );
}
[edited by - LogouT on January 23, 2004 5:56:08 PM]
Advertisement
You passed bImageData to CreateTexture(). Didn''t you mean to pass bFinalData?
oh.. jupp, thx

but its still not working... *all i get is a white rectangle*
Is your image size a power of 2 in both directions?
its 256x256
Okay after much test and trial, i have fixed the problem..

now the problem is that i can only load 24bits bmps saved in paint.

I can not load a 24bit bmp saved in Photoshop...

*fixed*:
i found out that photoshop does not set the right value in:
BITMAPINFOHEADER::biSizeImage

instead it set the value of 0

[edited by - LogouT on January 24, 2004 10:01:28 AM]
The docs state that biSizeImage can be set to 0. You shouldn''t rely on that value.
Usually when loading you don''t need that parameter anyway.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Right.

dwImageSize=54+(bmp_info.biWidth*bmp_info.biHeight*wChannels)+
((bmp_info.biWidth%4)*bmp_info.biHeight);

That''s how you would calculate the size of a 24-bit bitmap. The 54 represents the size of the header.

This topic is closed to new replies.

Advertisement