Jump to content
  • Advertisement

Archived

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

10011010

bitmap headers not reading correctly

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

ok guys in this function im trying to load a 24 bitmap. well its not working, i added some printf''s to tell me the values of the BMP file headers, and they come out wrong, for instance fh. // their values after reading from file bmih->biSize 39321600 bmih->biWidth 37552128 bmih->biHeight 65536 bmih->biPlanes 24 bmih->biBitCount 0 bmih->biCompression -1125646336 bmih->biSizeImage 185729039 you see? the values seem to be shitfed off one field, SHOULD be: bitcount = 24 compression = 0 size should be 40, etc its KINDA right, but definately NOT. plz plz help me.... below is my code to load the bitmap: **************************************************************** unsigned char* LoadBitmap(char* filename, BITMAPINFOHEADER *bmih) { FILE *fileptr; BITMAPFILEHEADER bmfh; unsigned char* bitmapImage; int idx = 0; unsigned char tempRGB; fileptr = fopen(filename,"rb"); if(!fileptr) return NULL; fread(&bmfh,sizeof(BITMAPFILEHEADER),1,fileptr); if(bmfh.bfType != 19778) { printf("image is not a bitmap\n"); fclose(fileptr); return NULL; } fread(bmih,sizeof(BITMAPINFOHEADER),1,fileptr); // begin to test and see if we have read into the structs correctly printf("%d\n", sizeof(BITMAPFILEHEADER)); // 16 = correct printf("%d\n", sizeof(BITMAPINFOHEADER)); // 40 = correct printf("%d\n", bmih->biSize); // bad stuff ->->-> printf("%d\n", bmih->biWidth); printf("%d\n", bmih->biHeight); printf("%d\n", bmih->biPlanes); printf("%d\n", bmih->biBitCount); printf("%d\n", bmih->biCompression); printf("%d\n", bmih->biSizeImage); if(bmih->biBitCount != 24) { printf("%d bit bitmap, can only load 24 for now", bmih->biBitCount); fclose(fileptr); return NULL; } fseek(fileptr,bmfh.bfOffBits,SEEK_SET); bitmapImage = (unsigned char*)malloc(bmih->biSizeImage); if(!bitmapImage){ fclose(fileptr); return NULL; } fread(bitmapImage,1,bmih->biSizeImage,fileptr); if(bitmapImage == NULL){ fclose(fileptr); return NULL; } for(idx=0;idx < bmih->biSizeImage;idx+=3); { tempRGB = bitmapImage[idx]; bitmapImage[idx] = bitmapImage[idx +=2]; bitmapImage[idx + 2] = tempRGB; } fclose(fileptr); return bitmapImage; } ****************************************************************

Share this post


Link to post
Share on other sites
Advertisement
Looks like your bmih isn''t allocated properly when you pass it into LoadBitmap(). Always make sure you test for null pointers if you''re passing in a pointer as argument.

Share this post


Link to post
Share on other sites
His code is not segfaulting there, so that can''t be the problem... data is being read in ok.

Have you considered endianness? (A problem if you''re running the code on a non-Intel architecture, since that''s what BMP was designed for)

Have you considered alignment? The BITMAPINFOHEADER struct might not be packed tightly on your system, or its fields might not be in the same order as the BMP data for some reason. Though I would expect this to work if you''re using M$ stuff... anyway, try reading in each field individually and see if that solves the problem...

Share this post


Link to post
Share on other sites
well im writing this code under GNU/linux with ext3 filesystem, could that affect the structure of the file? if so, should i use targa's or something else instead?

[edited by - 10011010 on March 5, 2004 7:00:18 PM]

Share this post


Link to post
Share on other sites
You say 16 bytes for your BITMAPFILEHEADER?

Every spec I have ever seen says 14 bytes.

I tested your code by just copy-pasting, and it worked correctly without changing anything (in MSVC++ .NET) on a Win2k machine.
Here is my output:

14 <--- 14!!!
40
40
128
128
1
24
0
49152

"That''s a very nice hat."
-Korben Dallas

Share this post


Link to post
Share on other sites
quote:
Original post by 10011010
ok guys in this function im trying to load a 24 bitmap. well its not working, i added some printf's to tell me the values of the BMP file headers, and they come out wrong, for instance
fh.
// their values after reading from file
bmih->biSize 39321600
bmih->biWidth 37552128
bmih->biHeight 65536
bmih->biPlanes 24
bmih->biBitCount 0
bmih->biCompression -1125646336
bmih->biSizeImage 185729039

you see? the values seem to be shitfed off one field,
SHOULD be: bitcount = 24 compression = 0 size should be 40, etc
its KINDA right, but definately NOT. plz plz help me....

below is my code to load the bitmap:
****************************************************************

unsigned char* LoadBitmap(char* filename, BITMAPINFOHEADER *bmih)
{
FILE *fileptr;
BITMAPFILEHEADER bmfh;
unsigned char* bitmapImage;
int idx = 0;
unsigned char tempRGB;

fileptr = fopen(filename,"rb");
if(!fileptr)
return NULL;

fread(&bmfh,sizeof(BITMAPFILEHEADER),1,fileptr);
if(bmfh.bfType != 19778)
{
printf("image is not a bitmap\n");
fclose(fileptr);
return NULL;
}

fread(bmih,sizeof(BITMAPINFOHEADER),1,fileptr);

// begin to test and see if we have read into the structs correctly
printf("%d\n", sizeof(BITMAPFILEHEADER)); // 16 = correct
printf("%d\n", sizeof(BITMAPINFOHEADER)); // 40 = correct
printf("%d\n", bmih->biSize); // bad stuff ->->->
printf("%d\n", bmih->biWidth);
printf("%d\n", bmih->biHeight);
printf("%d\n", bmih->biPlanes);
printf("%d\n", bmih->biBitCount);
printf("%d\n", bmih->biCompression);
printf("%d\n", bmih->biSizeImage);


if(bmih->biBitCount != 24)
{
printf("%d bit bitmap, can only load 24 for now", bmih->biBitCount);
fclose(fileptr);
return NULL;
}

fseek(fileptr,bmfh.bfOffBits,SEEK_SET);

bitmapImage = (unsigned char*)malloc(bmih->biSizeImage);
if(!bitmapImage){
fclose(fileptr);
return NULL;
}

fread(bitmapImage,1,bmih->biSizeImage,fileptr);
if(bitmapImage == NULL){
fclose(fileptr);
return NULL;
}

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

fclose(fileptr);
return bitmapImage;
}
****************************************************************


I had the exact same problem, if you created the structures your self you need to tell the compiler to pack/align the data members in memory, for VS.NET put

#pragma pack(1)
struct ....
#pragma pack()

between the struct/class for GCC compilers just add:

__attribute__((packed))

to the end of the class/struct declaration like:

struct BITMAPFILEHEADER {
....
}__attribute__((packed));

and it should work like a charm


[edited by - snk_kid on March 5, 2004 7:46:19 PM]

Share this post


Link to post
Share on other sites

  • 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!