Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi every one this code is to write image of 8bpp heigt and width equal to 20*20
actually i want to make this header structure for 24bpp and height =480 and width=752 can any one help me to do this .

#include
unsigned char bitmap[1300];


void BMPmake()
{
int i,noColor=256,end_color=54+4*noColor;
static unsigned char temp=0;
// -- FILE HEADER -- //

// bitmap signature
bitmap[0] = 'B';
bitmap[1] = 'M';

// file size
bitmap[2] = 0xc6; // 40 + 14 + 256*4+400
bitmap[3] = 0x05;
bitmap[4] = 0;
bitmap[5] = 0;

// reserved field (in hex. 00 00 00 00)
for( i = 6; i 400 pixels ,,,, 20x20x1
bitmap[35] = 0x01;//0x190
bitmap[36] = 0;
bitmap[37] = 0;

// horizontal resolution of the image - pixels per meter (2835)
bitmap[38] = 0;
bitmap[39] = 0;
bitmap[40] = 0;
bitmap[41] = 0;

// vertical resolution of the image - pixels per meter (2835)
bitmap[42] = 0;
bitmap[43] = 0;
bitmap[44] = 0;
bitmap[45] = 0;

// color palette information here 256
bitmap[46]=0;
bitmap[47]=1;
for( i = 48; i < 50; i++) bitmap[i] = 0;

// number of important colors
// if 0 then all colors are important
for( i = 50; i < 54; i++) bitmap[i] = 0;

//Color Palette
//for less then or equal to 8 bit BMP Image we have to create a 4*noofcolor size color palette which is nothing but
//[BLUE][GREEN][RED][ZERO] values
//for 8 bit we have the following code
for (i=54;i<end_color;i+=4)
{
bitmap[i]=temp;
bitmap[i+1]=temp;
bitmap[i+2]=temp;
bitmap[i+3]=0;
temp++;
}

// -- PIXEL DATA -- //
for( i = end_color; i < end_color+400; i++) bitmap[i] = 0xff;
}

void BMPwrite()
{
FILE *file;
int i;

//use wb+ when writing to binary file .i.e. in binary form whereas w+ for txt file.
file = fopen("b.bmp", "wb+");
for( i = 0; i < 1478; i++)
{
fputc(bitmap[i], file);
}
fclose(file);
}
void main()
{

BMPmake();
BMPwrite();
printf("Done!!");
}

What I have tried:

i have tried as below code
 #include
   unsigned char bitmap[1300];


void BMPmake()
{
    int i,noColor=256,end_color=54+4*noColor;
    static unsigned char temp=0;
    // -- FILE HEADER -- //

    // bitmap signature
    bitmap[0] = 'B';
    bitmap[1] = 'M';

    // file size
    bitmap[2] = 0xc6; // 40 + 14 + 256*4+400
    bitmap[3] = 0x05;
    bitmap[4] = 0;
    bitmap[5] = 0;

    // reserved field (in hex. 00 00 00 00)
    for( i = 6; i  400 pixels ,,,, 20x20x1
    bitmap[35] = 0x01;//0x190
    bitmap[36] = 0;
    bitmap[37] = 0;

    // horizontal resolution of the image - pixels per meter (2835)
    bitmap[38] = 0;
    bitmap[39] = 0;
    bitmap[40] = 0;
    bitmap[41] = 0;

    // vertical resolution of the image - pixels per meter (2835)
    bitmap[42] = 0;
    bitmap[43] = 0;
    bitmap[44] = 0;
    bitmap[45] = 0;

    // color palette information here 256
    bitmap[46]=0;
    bitmap[47]=1;
    for( i = 48; i < 50; i++) bitmap[i] = 0;

    // number of important colors
    // if 0 then all colors are important
    for( i = 50; i < 54; i++) bitmap[i] = 0;

    //Color Palette
    //for less then or equal to 8 bit BMP Image we have to create a 4*noofcolor size color palette which is nothing but
    //[BLUE][GREEN][RED][ZERO] values
    //for 8 bit we have the following code
    for (i=54;i<end_color;i+=4)
    {
        bitmap[i]=temp;
        bitmap[i+1]=temp;
        bitmap[i+2]=temp;
        bitmap[i+3]=0;
        temp++;
    }

    // -- PIXEL DATA -- //
    for( i = end_color; i < end_color+400; i++) bitmap[i] = 0xff;
}

void BMPwrite()
{
    FILE *file;
    int i;

    //use wb+ when writing to binary file .i.e. in binary form whereas w+ for txt file.
    file = fopen("b.bmp", "wb+");
    for( i = 0; i < 1478; i++)
    {
        fputc(bitmap[i], file);
    }
    fclose(file);
}
void main()
{

    BMPmake();
    BMPwrite();
    printf("Done!!");
}
Posted
Updated 9-Feb-18 22:59pm

Use structures instead of a byte array:
BITMAPFILEHEADER structure (Windows)[^]
BITMAPINFO structure (Windows)[^]
BITMAPINFOHEADER structure (Windows)[^]

Define variables for these structures, initialise them, and write them to the file followed by the bitmap pixel data. Note that there is no colour table with 24 BPP (biClrUsed is zero).
C++
BITMAPFILEHEADER fh;
BITMAPINFOHEADER fi;

unsigned width = 752;
unsigned height = 480;
// 32-bit aligned
unsigned widthBytes = ((width * 24 + 31) & ~31) / 8;
unsigned bppSize = widthBytes * height;

fh.bfType = 0x4D42; // "BM"
fh.bfSize = sizeof(fh) + sizeof(fi) + bppSize;
fh.bfReserved1 = fh.bfReserved2 = 0;
fh.bfOffBits = sizeof(fh) + sizeof(fi);

fi.biSize = sizeof(fi);
fi.biWidth = width;
fi.biHeight = height;
fi.biPlanes = 1;
fi.biBitCount = 24;
fi.biCompression = BI_RGB; // BI_RGB is 0
fi.biSizeImage = bppSize;
fi.biXPelsPerMeter = fi.biYPelsPerMeter = 0;
fi.biClrUsed = 0;
fi.biClrImportant = 0;

fwrite(&fh, sizeof(fh), 1, file);
fwrite(&fi, sizeof(fi), 1, file);
// write bitmap data here
 
Share this answer
 
v2
Comments
Balaraj Nayak 10-Feb-18 4:43am    
Ok i got you point but can you tell me in the above code changes because actually i am writing a board side code above one is building seccessfull but only width and height is not coming please check that code once if any modification is there help me it will be helpful to me
Jochen Arndt 10-Feb-18 5:22am    
My code has width and height reversed. I will update it.
Balaraj Nayak 10-Feb-18 5:51am    
thank u.please try to modify my code or give me ur full source which can write raw to bmp or rgb to bmp in c
Balaraj Nayak 10-Feb-18 6:03am    
can u now update your code or u r changing my code??
Balaraj Nayak 10-Feb-18 5:50am    
please try to modify my code or give me ur full source which can write raw to bmp or rgb to bmp in c
 
Share this answer
 
Comments
Balaraj Nayak 10-Feb-18 4:25am    
i am seeing that but i am not getting where exactly i have to check and change the values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900