Archived

This topic is now archived and is closed to further replies.

sheker

Loading Bitmap

Recommended Posts

hi guys. i got this code from opengl game programming to load a bitmap but for some reason its not working im having problems when i tried to read the body of the file (the main data) the header file is read in fine i did all the checking, can you tell me whats wrong?
  unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
	FILE *filePtr;							// the file pointer

	BITMAPFILEHEADER	bitmapFileHeader;		// bitmap file header

	unsigned char		*bitmapImage;			// bitmap image data

	int					imageIdx = 0;		// image index counter

	unsigned char		tempRGB;				// swap variable


	// open filename in "read binary" mode

	filePtr = fopen(filename, "rb");
	if (filePtr == NULL)
		return NULL;
	
	// read the bitmap file header

	fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
	
	// verify that this is a bitmap by checking for the universal bitmap id

	if (bitmapFileHeader.bfType != BITMAP_ID)
	{
		fclose(filePtr);
		return NULL;
	}

	// read the bitmap information header

	fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
	
	// move file pointer to beginning of bitmap data

	fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
	
	// allocate enough memory for the bitmap image data

	bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

	
	// verify memory allocation

	if (!bitmapImage)
	{
		free(bitmapImage);
		fclose(filePtr);
		return NULL;
	}
	
	// read in the bitmap image data

	fread(bitmapImage, sizeof(unsigned char), bitmapInfoHeader->biSizeImage, filePtr);

	output<<bitmapImage;
	output<<strlen((char *)bitmapImage);
	
	// make sure bitmap image data was read

	if (bitmapImage == NULL)
	{
		fclose(filePtr);
		return NULL;
	}

	
	// swap the R and B values to get RGB since the bitmap color format is in BGR

	for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
	{
		tempRGB = bitmapImage[imageIdx];
		bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
		bitmapImage[imageIdx + 2] = tempRGB;
	}

	// close the file and return the bitmap image data

	fclose(filePtr);
	return bitmapImage;
} 
[edited by - sheker on April 5, 2004 10:00:13 PM]

Share this post


Link to post
Share on other sites
what do you mean "not working". does it not compile? does it not link? do you get errors when you run it? if any of the previous, what is the EXACT error (cut and paste here).

to post lots of code put it in [*source]code here[/source] tags, not code tags. it''s nicer for us.

-me

Share this post


Link to post
Share on other sites