Cannot change GCC to structure structure (.BMP file header)

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; // = "BM"
  uint32_t filesize;     //filesize in Bytes
  uint32_t reserved;     // = 0 for this program
  uint32_t headerOffset; // = 54
} BmpFileHeader;

typedef struct
{
  uint32_t infoHeaderSize;    //size of header in byte. ( = 40)
  uint32_t Width;             //width of file in pixels
  uint32_t Height;            // height of file in pixels

  uint16_t Colors;       //colorbits per pixel (24 for 3byte RGB)
  uint16_t bitsPerPixel;
  uint32_t Compression;       //compression mode; 0 for uncompressed.
  uint32_t SizeImg;         //if biCompress = 0, =0. Else: filesize.

  uint32_t xPelsPerMeter;     // for output device;
  uint32_t yPelsPerMeter;     // 0 for this program

  uint32_t ColorsUsed;      // Colours used; = 0 for all
  uint32_t ColorsImportant; // num. of used colours, 0 for all
} BmpInfoHeader;

typedef struct
{
  BmpFileHeader fileheader;
  BmpInfoHeader infoheader;
} bitmapHead; // sizeof = 56

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)); // This gives me 8. 
  return newHeader;
}
+4
4

" ". , C struct, , . C , , , , . ( Ada, , , .)

struct BMP , , / , uint8_t. Ya rly. . , , .

BmpFileHeader:

// Fixed fields not represented in in-memory header.
typedef struct
{
    uint32_t filesize;     // total size of file in bytes
    uint32_t headerOffset; // offset from beginning of file to end of headers
                           // (normally 54)
} BmpFileHeader;

#define BMP_FILE_HEADER_MAGIC_1  0
#define BMP_FILE_HEADER_MAGIC_2  1
#define BMP_FILE_HEADER_FILESIZE 2
#define BMP_FILE_HEADER_RESERVED 6
#define BMP_FILE_HEADER_HDROFF   10
#define BMP_FILE_HEADER_SIZE     14
typedef uint8_t BmpFileHeaderOnDisk[BMP_FILE_HEADER_SIZE];

uint32_t
le32_to_cpu(uint8_t *p)
{
    return ((((uint32_t)p[0]) <<  0) | 
            (((uint32_t)p[1]) <<  8) |
            (((uint32_t)p[2]) << 16) |
            (((uint32_t)p[3]) << 24));
}

// Returns true if header is successfully parsed.
bool
load_bmp_file_header(BmpFileHeaderOnDisk ondisk, BmpFileHeader *inmem)
{
    if (ondisk[BMP_FILE_HEADER_MAGIC_1] != 'B' ||
        ondisk[BMP_FILE_HEADER_MAGIC_2] != 'M' ||
        le32_to_cpu(ondisk + BMP_FILE_HEADER_RESERVED) != 0)
        return false; // not a BMP file

    inmem->filesize = le32_to_cpu(ondisk + BMP_FILE_HEADER_FILESIZE);
    inmem->headerOffset = le32_to_cpu(ondisk + BMP_FILE_HEADER_HDROFF);
    return true;
}

, "info" ​​ .

+6

- ( signed char unsigned char) . struct , ; , , , , .

- ; , , static inline , .

/* portably read a 32 bit unsigned int in little-endian representation */
static uint32_t
read_le32(const unsigned char buf[4])
{
    uint32_t result;

    result  = (buf[0] & 0xff) <<  0;
    result |= (buf[1] & 0xff) <<  8;
    result |= (buf[2] & 0xff) << 16;
    result |= (buf[3] & 0xff) << 24;

    return (result);
}

16- int:

static uint16_t
read_le16(const unsigned char buf[4])
{

    return (buf[0] & 0xff) | (buf[1] & 0xff) << 8;
}

:

static void
read_bitmap_header(bitmapHead *bfh, const unsigned char buf[56])
{

    bfh->fileheader.typeSignature =  read_le16(buf +  0);
    bfh->fileheader.filesize =       read_le32(buf +  2);
    bfh->fileheader.reserved =       read_le32(buf +  6);
    bfh->fileheader.headerOffset =   read_le32(buf + 10);
    bfh->infoheader.infoHeaderSize = read_le32(buf + 14);
    bfh->infoheader.Width =          read_le32(buf + 18);
    /* ... */
}

, .

+3

struct . . GCC attributes. , __packed__. __packed__ , struct. __packed__ BmpFileHeader ,

typedef struct
{
  uint16_t typeSignature; // = "BM"
  uint32_t filesize;     //filesize in Bytes
  uint32_t reserved;     // = 0 for this program
  uint32_t headerOffset; // = 54
} __attribute__((__packed__))
BmpFileHeader;

struct.

+1

pragma pack;

#pragma pack(push, 1)
// your typedef structs
#pragma pack(pop)

#pragma pack(push,1)
typedef struct
{
  uint16_t typeSignature; // = "BM"
  uint32_t filesize;     //filesize in Bytes
  uint32_t reserved;     // = 0 for this program
  uint32_t headerOffset; // = 54
} BmpFileHeader;

typedef struct
{
  uint32_t infoHeaderSize;    //size of header in byte. ( = 40)
  uint32_t Width;             //width of file in pixels
  uint32_t Height;            // height of file in pixels

  uint16_t Colors;       //colorbits per pixel (24 for 3byte RGB)
  uint16_t bitsPerPixel;
  uint32_t Compression;       //compression mode; 0 for uncompressed.
  uint32_t SizeImg;         //if biCompress = 0, =0. Else: filesize.

  uint32_t xPelsPerMeter;     // for output device;
  uint32_t yPelsPerMeter;     // 0 for this program

  uint32_t ColorsUsed;      // Colours used; = 0 for all
  uint32_t ColorsImportant; // num. of used colours, 0 for all
} BmpInfoHeader;

typedef struct
{
  BmpFileHeader fileheader;
  BmpInfoHeader infoheader;
} bitmapHead; // now you will get sizeof = 54
#pragma pack(pop)

,

-

#include <stdio.h>
#include <stdint.h>

#pragma pack(push,1)
typedef struct
{
  uint16_t typeSignature; // = "BM"
  uint32_t filesize;     //filesize in Bytes
  uint32_t reserved;     // = 0 for this program
  uint32_t headerOffset; // = 54
} BmpFileHeader;

typedef struct
{
  uint32_t infoHeaderSize;    //size of header in byte. ( = 40)
  uint32_t Width;             //width of file in pixels
  uint32_t Height;            // height of file in pixels

  uint16_t Colors;       //colorbits per pixel (24 for 3byte RGB)
  uint16_t bitsPerPixel;
  uint32_t Compression;       //compression mode; 0 for uncompressed.
  uint32_t SizeImg;         //if biCompress = 0, =0. Else: filesize.

  uint32_t xPelsPerMeter;     // for output device;
  uint32_t yPelsPerMeter;     // 0 for this program

  uint32_t ColorsUsed;      // Colours used; = 0 for all
  uint32_t ColorsImportant; // num. of used colours, 0 for all
} BmpInfoHeader;

typedef struct
{
  BmpFileHeader fileheader;
  BmpInfoHeader infoheader;
} bitmapHead; // now you will get sizeof = 54
#pragma pack(pop)

int main()
{
    printf("Size of bitmapHead : %d\n", sizeof(bitmapHead));
}

-

>pragmaExample
Size of bitmapHead : 54
0

All Articles