I am currently trying to create a .BMP file. The BMP header, by my definition, is 54 bytes long. The code compiles and that’s it, but when I try to open the file, I get an “incorrect header format” error.
If I do sizeof (structtype), I get 56 bytes instead of the given 54 - and if I initialize the structure with values, then do sizeof (newStruct), I will get 8 bytes. Since I need the exact 54 bytes to write to the file, this is terrible.
Is there a way to keep GCC from resizing the structure this way?
Here's the definition of structures:
typedef struct
{
uint16_t typeSignature;
uint32_t filesize;
uint32_t reserved;
uint32_t headerOffset;
} BmpFileHeader;
typedef struct
{
uint32_t infoHeaderSize;
uint32_t Width;
uint32_t Height;
uint16_t Colors;
uint16_t bitsPerPixel;
uint32_t Compression;
uint32_t SizeImg;
uint32_t xPelsPerMeter;
uint32_t yPelsPerMeter;
uint32_t ColorsUsed;
uint32_t ColorsImportant;
} BmpInfoHeader;
typedef struct
{
BmpFileHeader fileheader;
BmpInfoHeader infoheader;
} bitmapHead;
and here is the function that initializes the new header:
bitmapHead* assembleHeader(int compCount)
{
bitmapHead* newHeader = (bitmapHead*) calloc(1, 54);
newHeader->fileheader.typeSignature = 0x4D42;
newHeader->fileheader.filesize = (compCount*100*51*3 + 54);
newHeader->fileheader.reserved = 0;
newHeader->fileheader.headerOffset = 54;
newHeader->infoheader.infoHeaderSize = 40;
newHeader->infoheader.Width = 100*compCount;
newHeader->infoheader.Height = 51;
newHeader->infoheader.Colors = 1;
newHeader->infoheader.bitsPerPixel = 21;
newHeader->infoheader.Compression = 0;
newHeader->infoheader.SizeImg = compCount*100*51*3;
newHeader->infoheader.xPelsPerMeter = 0;
newHeader->infoheader.yPelsPerMeter = 0;
newHeader->infoheader.ColorsUsed = 0;
newHeader->infoheader.ColorsImportant = 0;
printf("%lu \n", sizeof(newHeader));
return newHeader;
}