I’m following this tutorial
http://www.gamedev.net/page/resources/_/technical/game-programming/how-to-load-a-bitmap-r1966 and I got the problem of wrong value for all the bitmap header/info that I load.
I’ve declared the structure to store the bitmap header, info
typedef struct BITMAPFILE_HEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILE_HEADER;
//Bitmap information header
//provides information specific to the image data
typedef struct BITMAPINFO_HEADER{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFO_HEADER;
//Colour palette
typedef struct RGB_QUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGB_QUAD;
After that I read the bitmap by using following codes:
FILE *in;
in = fopen("picture.bmp", "rb");
if (in == NULL)
{
printf("Error opening file\n");
}
else
{
BITMAPFILE_HEADER bmfh;
BITMAPINFO_HEADER bmih;
fread(&bmfh, sizeof(BITMAPFILE_HEADER), 1, in);
fread(&bmih, sizeof(BITMAPINFO_HEADER), 1, in);
if (bmih.biBitCount != 24)
printf("not 24");
}
My picture is 24-bit but when I run this program, it shows “not 24”. I try to debug it in Visual Studio then I saw the bmih.biBitCount is 0. In addition, the image width and height is wrong as well, the only correct data that I found in the header is the bmfh.bfType which is 19778.
Anyone know what’s wrong with my codes?
Again, I just want to read the bmp but not displaying it.
PS:
Originally the picture is in JPEG format. I converted it to BMP format by using MS PAINT and re-save it in BMP format. I wonder does it affect the values?
I would imagine that you ran into an alignment issue, because the compiler can choose to align data in a structure as he sees fit. You can try to pack your struct, but I would suggest to read out every field and fill it in separately, it is more work, but it is clearer and avoids alignment issues.