Mister Zimbu
Member
I'm trying to write a bitmap file I generate for my raytracer. However, I'm having a bit of trouble writing out the headers to the file. The header contains 5 fields- the sizes being 2, 4, 2, 2, and 4 bytes respectively.
Right now I'm just taking a pointer to the beginning of the structure and writing out until I hit the end of the structure. However, in RAM, it's padding the first two-byte field so the 4-byte can start at a multiple of 4.
So the memory ends up looking like this:
01 01 00 00 02 02 02 02 03 03 04 04 (assuming each field has a value of the number of it)
Anyway, I'm being kind of long-winded here. The only way I can think of to resolve this is to write out each field in the structure individually, but that seems to make for rather messy code... I'm wondering if there's a more elegant solution to this.
Please help me, thanks .
Here's the relevant code in question:
Right now I'm just taking a pointer to the beginning of the structure and writing out until I hit the end of the structure. However, in RAM, it's padding the first two-byte field so the 4-byte can start at a multiple of 4.
So the memory ends up looking like this:
01 01 00 00 02 02 02 02 03 03 04 04 (assuming each field has a value of the number of it)
Anyway, I'm being kind of long-winded here. The only way I can think of to resolve this is to write out each field in the structure individually, but that seems to make for rather messy code... I'm wondering if there's a more elegant solution to this.
Please help me, thanks .
Here's the relevant code in question:
Code:
// I know these values are correct
typedef unsigned short u2;
typedef unsigned long u4;
// The header in question
typedef struct {
u2 bfType;
u4 bfSize;
u2 bfReserved1;
u2 bfReserved2;
u4 bfOffBits;
} bitmapFileHeader;
// Another, unmentioned header, that presumably suffers from the same problem as the first
typedef struct {
u4 biSize;
u4 biWidth;
u4 biHeight;
u2 biPlanes;
u2 biBitCount;
u4 biCompression;
u4 biSizeImage;
u4 biXPelsPerMeter;
u4 biYPelsPerMeter;
u4 biClrUsed;
u4 biClrImportant;
} bitmapInfoHeader;
...
...
...
bitmapFileHeader bff;
memset( &bff, 0, sizeof(bff) );
// These are the actual values I'm setting the field to. The file, when written, looks like:
// 22 11 00 00 66 55 44 33 88 77 AA 99 EE DD CC BB (those zeroes shouldnt be there, that's my problem)
bff.bfType = 0x1122;
bff.bfSize = 0x33445566;
bff.bfReserved1 = 0x7788;
bff.bfReserved2 = 0x99AA;
bff.bfOffBits = 0xBBCCDDEE;
FILE *f;
f = fopen( "out.bmp", "w" );
fwrite( &bff, 1, sizeof( bff ), f );