C++ Builder BMP Loading Problem!!!!!!!
I''m having huge problems trying to load a simple texture map into C++ Builder. Here''s my code for loading the BMP file.
BITMAPINFOHEADER biHeader;
BITMAPFILEHEADER header;
unsigned char *data;
unsigned char color;
FILE *pfile;
pfile = fopen(filename, "rb");
if(pfile == NULL) return 0;
fread(&header, sizeof(BITMAPFILEHEADER), 1, pfile);
if(header.bfType != BITMAP_ID)
{
fclose(pfile);
return 0;
}
fread(&biHeader, sizeof(BITMAPINFOHEADER), 1, pfile);
fseek(pfile, header.bfOffBits, SEEK_SET);
data = new unsigned char[biHeader.biSizeImage];
fread(data, 1, biHeader.biSizeImage, pfile);
int index;
for(index=0; index < (int)biHeader.biSizeImage; index+=3)
{
color = data[index];
data[index] = data[index+2];
data[index+2] = color;
}
fclose(pfile);
This works perfectly fine in Visual C++, but in Borland C++ Builder if I check the values of biHeader.biWidth, biHeader.biHeight, and/or biHeader.biSizeImage they all have either 0 or really "high" values, like 88083838 or something along those lines. I have the images in the current directory of the project so that''s not the issue, and I check to make sure it loads, but for some reason this code doesn''t grab the information correctly. I even tried outputing the contents of data to a file and it turned up blank!
I know texture mapping is working because I constructed my own texture map manually and loaded it and it displayed fine, but with this code all I get is a white triangle. Any ideas?
fread''ing structures isn''t a good idea, as different platforms or different compilers (even different compiler options) can screw it up. (because of any internal padding)
the SizeImage param is usually 0 ;( so u''ll have to recalculate it ( = with * height * numBytesPerPixel).
watch for the padding if u dont use 2^n sized textures.
watch for the padding if u dont use 2^n sized textures.
As was previousley suggested, the problem may be with fread''ing the whole struct.
Try fread''ing each member of the struct seperatley.
Also, if I remember correctly BMP''s have scan-line padding, if necessary... You should read it a line at a time and skip the padding...
I have found that a hex editor/dumper is amazingly handy when trying to figure out why image loading code isn''t working.
Tony
Try fread''ing each member of the struct seperatley.
Also, if I remember correctly BMP''s have scan-line padding, if necessary... You should read it a line at a time and skip the padding...
I have found that a hex editor/dumper is amazingly handy when trying to figure out why image loading code isn''t working.
Tony
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement