Jump to content
  • Advertisement
Sign in to follow this  
jsloan

Quick question

This topic is 4263 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

Hello, Im working on my own bitmap loader, and ive run into a small stumbling block when trying to acquire a few values...
unsigned char test[55];

bmp = fopen(argv[1], "r");
//read in first 54 bytes of the bitmap (header)
fread(&test,sizeof(char), 54, bmp);

printf("first 2 bytes: %c %c\n", test[0],test[1]);
printf("bytes 18-22(width): %i %i %i %i\n",test[18],test[19],test[20],test[21]);
printf("bytes 22-25(height): %i %i %i %i\n",test[22],test[23],test[24],test[25]);
printf("size: %i %i %i %i\n",test[2],test[3],test[4],test[5]);
printf("offset: %i\n",test[10]);



outputs first 2 bytes: B M bytes 18-22(width): 64 0 0 0 bytes 22-25(height): 64 0 0 0 size: 56 48 0 0 offset: 54 first 2 bytes, offset and width and height numbers are correct, but the size is not...how do i turn that into a workable value?

Share this post


Link to post
Share on other sites
Advertisement
The size is > 256, which takes more than one byte. Note that two bytes are printed out. Print an int instead and you shall see the correct value.

Share this post


Link to post
Share on other sites
HI, and thanks for the reply.

They are printed as integers, if you look at the printf() statment im using %i, the problem is getting those bytes together to make a workable number?

Share this post


Link to post
Share on other sites
Quote:
Original post by jsloan
HI, and thanks for the reply.

They are printed as integers, if you look at the printf() statment im using %i, the problem is getting those bytes together to make a workable number?


You can make a structure like this


struct BitmapFileHeader
{
char bfType[2];
long bfSize;
short bfRes1;
short bfRes2;
long bfOffBits;
};


and do the same with the info header and you'll be right.

You can display the size without the worry of converting

Alternatively, I think you can do

int size=(int*)&test[2];

Share this post


Link to post
Share on other sites
Hello again,

Iv'e tried your suggestion regarding the struct and for some reason it yields me zero values and some rather unexpected ones...



struct theheader
{
char bfType[2];
long bfSize;
short bfRes1;
short bfRes2;
long bfOffBits;
};

theheader HDR;

//snip snip //

bmp = fopen(argv[1], "rb");
fread(&HDR, 1, sizeof(theheader), bmp)

printf("first 2 bytes: %c %c\n",HDR.bfType[0],HDR.bfType[1]);
printf("size: %i\n",HDR.bfSize);
printf("offset: %i\n", HDR.bfOffBits);




outputs

first 2 bytes: B M
size: 0 < should be 12344 ?
offset: 2621440 <- should be 54 ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
You can make a structure like this


struct BitmapFileHeader
{
char bfType[2];
long bfSize;
short bfRes1;
short bfRes2;
long bfOffBits;
};

...


And you have to disable padding!

Otherwise the offsets of the data members in the structure do not match those from the file. For instance with padding enabled (which should be the default), the compiler will silently insert two bytes between bfType and bfSize.


Share this post


Link to post
Share on other sites
Quote:
Original post by nmi
Quote:
Original post by Adam Hamilton
You can make a structure like this


struct BitmapFileHeader
{
char bfType[2];
long bfSize;
short bfRes1;
short bfRes2;
long bfOffBits;
};

...


And you have to disable padding!

Otherwise the offsets of the data members in the structure do not match those from the file. For instance with padding enabled (which should be the default), the compiler will silently insert two bytes between bfType and bfSize.


Well that explains it!

Works now!

P.S to disable padding on VS .NET

Project property > C/C++ > Code Generaton > struct member aligment (should be 1 byte).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!