Sign in to follow this  
Azenris

BMP, writing to a struct

Recommended Posts

I ahve my struct
typedef struct _tBMP_HEADER
{
    unsigned short type;	// must be 19778
    unsigned long size;		// size of BMP file (in bytes)
    unsigned short reserved1;	// reserved, leave 0
    unsigned short reserved2;	// reserved, leave 0
    unsigned long imageOffset;	// offset to image data (in bytes)
} BMP_HEADER;




Within the loading function I have
FILE *pFile;   // file pointer
errno_t error; // used by fopen_s

error = fopen_s(&pFile, filename, "rb");         // open the file as read binary

if (pFile == NULL || error != 0)                 // check it opened ok
    return BMP_RETURN_FAIL_OPENFILE;             // just end now if it didnt

fread(&m_header, sizeof(BMP_HEADER), 1, pFile);  // read the header and store it




However when debugging and looking at m_header its shown as:
type =        19778
size =        0
reserved1 =   0
reserved2 =   1078
imageOffset = 2621440




Any help appreciated ty :) [Edited by - jpetrie on September 11, 2009 10:26:30 AM]

Share this post


Link to post
Share on other sites
The problem is almost certainly that your compiler tries to align the structure's size field on a 32-bit boundary, thus adding two bytes of padding after the type word.

There are ways of forcing many compilers to omit this padding but they're not exactly portable, so if you are targeting Windows then you're probably better off including <windows.h> and making use of BITMAPFILEHEADER instead. And if that's not feasible then I suggest extracting the fields manually, that is without a structure, and dealing with byte order issues while you're about it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this