1

This is my second month since I've started C/C++ programming. I want to create a random BMP image. I did some research about the way of creating such a file, and I came up with this code:

#pragma pack(push,1)
struct PixelBMP
{
   uint8_t higher:4;
   uint8_t lower:4;
}pxl;
#pragma pack(pop)

#pragma pack(push,1)
struct BMPHEADER
{
    char BM[2]={'B','M'};
    uint32_t Size = 54+360000;
    uint16_t Reserved1 = 0;
    uint16_t reserved2 = 0;
    uint32_t offBits = 54;

}BMPHEADERFILE;
#pragma pack(pop)

#pragma pack(push,1)
struct BMPINFOHEADER
{
    uint32_t infoHsize = 40;
    uint32_t width = 600;
    uint32_t height = 600;
    uint16_t planes = 1;
    uint16_t bitCount = 4;
    uint32_t compression = 0;
    uint32_t biSizeImage = 0;
    uint32_t meter = 3780;
    uint32_t vertical = 3780;
    uint32_t crlUsed =0;
    uint32_t crlImp = 0;
}BMPINFOHEADERFILE;
#pragma pack(pop)

void create_bmp()
{
    FILE *output_file;
    output_file = fopen("generated_img.bmp", "wb");
    if(output_file==NULL)
    {
        perror("Error while opening the file ");
        exit(1);
    }
    fwrite(&BMPHEADERFILE, sizeof(BMPHEADERFILE), 1, output_file);
    fwrite(&BMPINFOHEADERFILE, sizeof(BMPINFOHEADERFILE), 1,output_file);
    size_t bmp_size = BMPINFOHEADERFILE.width*BMPINFOHEADERFILE.height;
    for(int i=0;i<bmp_size;i++)
    {
        pxl.lower = rand()%17;
        pxl.higher = rand()%17;

        fwrite(&pxl, sizeof(pxl),1,output_file);
    }
    fclose(output_file);
}

int main()
{
create_bmp();
return 0;
}

For some reason, the bmp file generated, cannot be open. I get some error from Windows. I have windows 10. PS: I also ran the code from this source, and it gave me the same error when I tried to open it.

Can anybody help me with a hint? Thank you very much! I much appreciate your time!

UPDATE: I have done one first edit, and it seems to work, my bmp image is generated. It has weird colours, but I think it works. In regard with bits per pixel, I need to use 16 colours palete, using 4 bit mode.

geekt
  • 1,979
  • 3
  • 10
  • 20

1 Answers1

1
  • If you want to create a bitmap image of 8bit per color (24bit per pixel), assign BMPINFOHEADERFILE.bitCount to 24, not 4.
  • Your struct BMPHEADER may be padded and word-aligned after char BM[2], which causes misalignment of the bitmap file format. To avoid this, say struct __attribute__ ((__packed__)) BMPHEADER {..}.
  • The value of BMPHEADERFILE.Size should be 54+270000, not 52+270000.
  • As for the random pixel values, rand() % 17 generates the pixel values between 0 and 17, which will be too dark to see. rand() % 256 might be better in terms of visibility.
tshiono
  • 21,248
  • 2
  • 14
  • 22
  • Thanks for your advises! The only thing is that I want 16 bit per pixel. For this matter, I changed the the definition of struct. I will do an update to the code to have a look at it. Thanks! – geekt Nov 24 '21 at 07:15
  • The 16bit-per-pixel bmp format should be grayscale image or color-pallette indexed image, NOT with immediate RGB values. Besides they are very uncommon. Are you sure about that? – tshiono Nov 24 '21 at 07:26
  • well, this is the task I was assigned with. To use 4 bits color mode which contains 16 colors :-?. and I used this [link](https://web.archive.org/web/20080912171714/http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html) as documentation. – geekt Nov 24 '21 at 07:57
  • Understood. I have no idea why your teacher instructs you to such an unusual format but good luck anyway ;) – tshiono Nov 24 '21 at 08:02
  • 1
    Thank you! I succesfully created ! All the best! – geekt Nov 25 '21 at 07:28
  • Great to hear that. Congrats! – tshiono Nov 25 '21 at 07:31