Public Group

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

Recommended Posts

Hey, I've been trying to write function which loads bmp file to string. Starting code is following:
FILE *filePtr;
unsigned char  * bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;

filePtr=fopen("fire.bmp","rb");

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


and then when I call fseek(filePtr, bitmapFileHeader.bfOffBits,0); the position indicator is being mooved at 2621440 byte [dead] !! where of course is absolutely no data. I thought data is starting after two those headers and this is at 56 byte ( size of BITMAPFILEHEADER + BITMAPINFOHEADER) In addition all data read from headers is very strange for example in 24bit bitmap 128x128 pixels bitmapInfoHeader->biSizeImage equals 185794560 instead of 128*128*3 What is wrong?

Share on other sites
Are you using the structs from windows.h or your own creations?

If it's your own make sure you get the padding right (BITMAPFILEHEADER has its members packed to 16bit).

Also note that the biSizeImage is not necessarily set to a valid value, it may as well be 0.

In any case the values you show are off. Either the wrong padding has led to interpreting the wrong bytes as biSizeImage or the BMP is corrupt.

Share on other sites
I'm using structures from windows.h so sizes are ok: BITMAPFILEHEADER has 16 bytes
BITMAPINFOHEADER has 40 bytes so I suppose bitmapFileHeader.bfOffBits should have had 56 but as I mentioned have 2621440. I tried several bitmaps and on each one function loads big, strange values to headers.

Share on other sites
Ok, in that case is the code you showed the exact code you use?

Because there is no & at

I'd use a hex viewer and actually compare all members if they match the file content. This should let you find the actual problem pretty fast.

Share on other sites
So I fixed my function and now it is
but it doesn't change the situation.
I noticed that sum of fields of struct BITMAPFILEHEADER is 14 not 16
because there is: short + long + short + short + long (2+4+2+2+4=14)
In addition when I'm looking at size of bitmap file 128x128 in properties it is 49206
which would confirm my idea because 128*128*3 + 14 + 40=49206

maybe there is a problem in here and data read from headers is simply moved 2 bits to the left. But on the other hand it would mean that bitmap structures from windows.h are incorrect what is impossible.

Share on other sites
That's the problem right there. The structs padding seems to be set for 32bit.

The Windows headers define a pack pragma around the BITMAPFILEHEADER struct which ought to set the padding to 16bit.

What compiler are you using?

If yours doesn't work with that pragma you have to resort to read the struct members manually (member by member).

1. 1
2. 2
3. 3
Rutin
17
4. 4
5. 5

• 14
• 9
• 10
• 12
• 17
• Forum Statistics

• Total Topics
632905
• Total Posts
3009153
• Who's Online (See full list)

There are no registered users currently online

×