Skip to main content
gamedev.net
🔒 Locked

Loading a bitmap?

Started by Malone1234 Jul 17, 2003 at 1:05 AM 3 replies 850+ views
Original Post
Malone1234
Malone1234
I'm trying to write a simple function that loads an uncompressed 24 bit bitmap from a file. I have the BMPFILEHEADER and BMPINOFHEADER structures all set up, but my question is how do I use ifstream exactly to pull this information out of the file and into my structure? for instance...
void loadBMP(const char * filename)
{
  struct BMPFILEHEADER fileHeader;
  std::ifstream fin(filename);

  if(!fin)
    return;

  // let's say i want to load the first field of the

  // BMPFILEHEADER structure.

  // bfType is an unsigned short

  fin >> fileHeader.bfType;

  // etc. etc. I would load all the rest of the fields

}
Is this how ifstream is used? I've searched the net for tutorials on file streams but every tutorial i've seen only loads character strings from files, never ints or floats or anything like that. [edited by - Malone1234 on July 17, 2003 2:06:14 AM]
JY
JY
Take a look at the LoadImage API. You can specify a filename and it will return you a handle to a bitmap.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Red Ghost
Red Ghost
Whoa, browser back. I copy here the content of the email response.

Anyways, here is some code example to load a bitmap.

void LoadBMP(char *filename)
{
BITMAPFILEHEADER BmpFH;
BITMAPINFOHEADER BmpIH;
unsigned char* image;
unsigned int imageSize;

// Reads the file and its header

FILE* fichier = fopen(filename,"rb"); //file is open in read only, binary mode

if (!fichier) return; //no file, bail out

if (fread(&BmpFH,1,14,fichier)!=14) return; //the file header is not correctly read

if (fread(&BmpIH,1,40,fichier)!=40) return; //the info header is not correctly read


if (BmpIH.biSizeImage == 0) //no size in the info header, then we calculate

{
//note that BmpIH.biBitCount holds 24 for a plain RGB bitmap.

imageSize = (unsigned int)(BmpIH.biWidth * BmpIH.biHeight * BmpIH.biBitCount / 8) ;
}
else
{
imageSize = BmpIH.biSizeImage;
}
image = new unsigned char[imageSize]; // this is the buffer that will hold image data


fread(image, 1, imageSize, fichier); //we read in the image data


fclose (fichier); //we close the file


return ;/**/
}

Now, to adapt it to the ifstream interface, you need to know precisely
the size of your header files to be able to correctly load in the bitmap
structures. Here is an example, you just need to test it:


void LoadBMP(char *filename)
{
BITMAPFILEHEADER BmpFH;
BITMAPINFOHEADER BmpIH;
unsigned char* image;
unsigned int imageSize;

std::ifstream fin(filename, ios::in, ios::binary); //we open in binary

if(!fin)
return;

// Reads the file and its header

fin.read((unsigned char*) &BmpFH, 14); //we read in the file header

if (fin.gcount() != 14) //the file header is not correctly read

return;

fin.read((unsigned char*) &BmpIH, 40); //we read in the info header

if (fin.gcount() != 40) //the info header is not correctly read

return;

if (BmpIH.biSizeImage == 0) //no size in the info header, then we calculate

{
imageSize = (unsigned int)(BmpIH.biWidth * BmpIH.biHeight * BmpIH.biBitCount / 8) ;
}
else
{
imageSize = BmpIH.biSizeImage;
}
image = new unsigned char[imageSize]; // this is the buffer that will hold image data


fin.read(image, imageSize);

return ;/**/
}


Hope that helps.
Ghostly yours,
Red.
Ghostly yours,Red.
Malone1234
Malone1234
Exactly what I was looking for, Red Ghost. Thanks a bunch!

It seems i need to cast &BmpFH to a (signed char*) in the fin.read() function to get it to compile. Maybe that's just VC++ though. Thanks again.

[edited by - Malone1234 on July 17, 2003 10:34:09 PM]
Red Ghost
Red Ghost
As a side note don''t forget to intervert red and blue values once you have loaded your bitmap. BMP files store pixel data as BGR instead of RGB.
Ghostly yours,
Red.
Ghostly yours,Red.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.