Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

problem loading a bitmap

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can some1 explain why i'm getting these result when loading a bitmap file header.
#include <iostream>
#include <cstdio>

struct BMPFILEHEADER {
  unsigned short bfType;
  unsigned int   bfSize;
  unsigned short bfReserved1;
  unsigned short bfReserved2;
  unsigned int   bfOffBits;
};


int main() {

   FILE * file = fopen("C:\\1.bmp", "rb");

   BMPFILEHEADER b;

   fread(&b, sizeof(BMPFILEHEADER), 1, file);

   std::cout << b.bfType << "\n";
   std::cout << b.bfSize << "\n";
   std::cout << b.bfReserved1 << "\n";
   std::cout << b.bfReserved2 << "\n";
   std::cout << b.bfOffBits << "\n";

}

output:


19778
17
0
54
2621440



I've read a few articles and this is the way that they all load the headers but these result are incorrect - the image size is actually 1125794. I get the correct results if I use:

fread(&b.bfType, 2, 1, file);
fread(&b.bfSize, 4, 1, file);
etc...


can any1 explain why this is?

Share this post


Link to post
Share on other sites
Advertisement
The most likely cause is that your definition of the bitmap header contains padding that doesn't exist in the file. Your compiler may contain methods to eliminate the padding. Ex: on MSVC look at #pragma pack.

Share this post


Link to post
Share on other sites
thank, there is another problem now... the bitmap data is only showing in grayscale when i use directdraw, is the image data arranged as: "BGRBGR...", blue green red or another way when using 24 bit color?
I use this code to draw the bitmap:


void DrawBMP(BMP * b, unsigned int * video_buffer, int mem_pitch) {
int c = 0;
for(int y = b->infoHeader.biHeight-1; y > 0; y--) {
for(int x = 0; x < b->infoHeader.biWidth; x++) {
video_buffer[y*mem_pitch + x] = b->bitData[c++] | // blue
b->bitData[c++] << 8 | // green
b->bitData[c++] << 16; // red
}
}
}





and here's the code I use to read the bitmap data:


// read all header information

for(int a = 0; a < b->infoHeader.biSizeImage; a++) {
b->bitData[a] = fgetc(file);
}

// fread(&b->bitData, 1, b->infoHeader.biSizeImage, file); // doesn't work for some reason







I don't take the padded bytes into account just yet, but im working with a 256x256 .bmp so it doesn't really matter just now. any ideas why its showing in grayscale? I've also used window RGB macro but it gives the same result.

Share this post


Link to post
Share on other sites
This looks suspect to me:

video_buffer[y*mem_pitch + x] = b->bitData[c++] | // blue
b->bitData[c++] << 8 | // green
b->bitData[c++] << 16; // red

What type is "bitData", and video_buffer? If it's an unsigned char, you might have an overflow there (<<8 on a 8-bit type will be 0).

See if this works:

DWORD r = ((DWORD)b->bitData[c+2]) << 16;
DWORD g = ((DWORD)b->bitData[c+1]) << 8;
DWORD b = ((DWORD)b->bitData[c+0]) << 0;
video_buffer[y*mem_pitch + x] = (r|g|b);



Also, off topic, BMP files have their image data stored vertically flipped (I.e. the first row of data in the BMP is the last row of the image)

Share this post


Link to post
Share on other sites
thanx a lot, it works now. bitData is unsigned char and video_buffer is unsigned int. I thought this was the problem before and tried->

video_buffer[y*mem_pitch + x] = (int)b->bitData[c++] << 8 | ......

but i see what i done wrong. thanx.

Edit:

the reason
fread(&b->bitData, 1, b->infoHeader.biSizeImage, file);
did not work was because I was passing a pointer to a pointer instead of:
fread(b->bitData, 1, b->infoHeader.biSizeImage, file);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!