Creating bitmap using C -


this follow question here, link

i read , copy pasted code link code::blocks little change,

now want create 20x20 black image

so edited code unable open image on win7, says 'windows photo viewer can't open image'.

can please advice me wrong code ??

my code:-

#include<stdio.h>  unsigned char bitmap[1000];   void bmpmake() {     int i;     // -- file header -- //      // bitmap signature     bitmap[0] = 'b';     bitmap[1] = 'm';      // file size     bitmap[2] = 0xc6; // 40 + 14 + 400     bitmap[3] = 0x01;     bitmap[4] = 0;     bitmap[5] = 0;      // reserved field (in hex. 00 00 00 00)     for( = 6; < 10; i++) bitmap[i] = 0;      // offset of pixel data inside image     //thats 54 or d8 difference between starting , position data starts.     bitmap[10]=0xd8;     for( = 11; < 14; i++) bitmap[i] = 0;       // -- bitmap header -- //      // header size     bitmap[14] = 40;     for( = 15; < 18; i++) bitmap[i] = 0;      // width of image     bitmap[18] = 20;     for( = 19; < 22; i++) bitmap[i] = 0;      // height of image     bitmap[22] = 20;     for( = 23; < 26; i++) bitmap[i] = 0;      // no of color planes, must 1     bitmap[26] = 1;     bitmap[27] = 0;      // number of bits per pixel     bitmap[28] = 8; // 1 byte     bitmap[29] = 0;      // compression method (no compression here)     for( = 30; < 34; i++) bitmap[i] = 0;      // size of pixel data     bitmap[34] = 0x90; // 400 bytes => 400 pixels ,,,, 20x20x1     bitmap[35] = 0x01;     bitmap[36] = 0;     bitmap[37] = 0;      // horizontal resolution of image - pixels per meter (2835)     bitmap[38] = 0;     bitmap[39] = 0;     bitmap[40] = 0;     bitmap[41] = 0;      // vertical resolution of image - pixels per meter (2835)     bitmap[42] = 0;     bitmap[43] = 0;     bitmap[44] = 0;     bitmap[45] = 0;      // color palette information here 256     bitmap[46]=0xff;     bitmap[47]=1;     for( = 48; < 50; i++) bitmap[i] = 0;      // number of important colors     for( = 50; < 54; i++) bitmap[i] = 0;      // -- pixel data -- //     for( = 54; < 454; i++) bitmap[i] = 0xff; }  void bmpwrite() {     file *file;     int i;     file = fopen("b.bmp", "wb+");     for( = 0; < 454; i++)     {         fputc(bitmap[i], file);     }     fclose(file); } void main() {      bmpmake();     bmpwrite();     printf("done!!"); } 

with weather vane, wildplasser , user35443 able solve problem.

the main problem didn't implement color table necessary in bitmap when no of bits/pixel<=8, comparing original bitmap mine able find format of 8bit greyscale color table, generated using following code:

unsigned char temp=0; int end_color=54+4*nocolor; //where nocolor 256 2^8 here 8 no of bits/pixel. (i=54;i<end_color;i+=4)     {         bitmap[i]=temp;         bitmap[i+1]=temp;         bitmap[i+2]=temp;         bitmap[i+3]=0;         temp++;     } 

overall code:-

#include<stdio.h>    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( = 6; < 10; i++) bitmap[i] = 0;      // offset of pixel data inside image     //the offset, i.e. starting address, of byte bitmap image data (pixel array) can found.     //here 1078     bitmap[10]=0x36;     bitmap[11]=0x04;     for( = 12; < 14; i++) bitmap[i] = 0;       // -- bitmap header -- //      // header size     bitmap[14] = 40;     for( = 15; < 18; i++) bitmap[i] = 0;      // width of image     bitmap[18] = 20;     for( = 19; < 22; i++) bitmap[i] = 0;      // height of image     bitmap[22] = 20;     for( = 23; < 26; i++) bitmap[i] = 0;      // no of color planes, must 1     bitmap[26] = 1;     bitmap[27] = 0;      // number of bits per pixel     bitmap[28] = 8; // 1 byte     bitmap[29] = 0;      // compression method (no compression here)     for( = 30; < 34; i++) bitmap[i] = 0;      // size of pixel data     bitmap[34] = 0x90; // 400 bytes => 400 pixels ,,,, 20x20x1     bitmap[35] = 0x01;//0x190     bitmap[36] = 0;     bitmap[37] = 0;      // horizontal resolution of image - pixels per meter (2835)     bitmap[38] = 0;     bitmap[39] = 0;     bitmap[40] = 0;     bitmap[41] = 0;      // vertical resolution of 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( = 48; < 50; i++) bitmap[i] = 0;      // number of important colors     // if 0 colors important     for( = 50; < 54; i++) bitmap[i] = 0;      //color palette     //for less or equal 8 bit bmp image have create 4*noofcolor size color palette nothing     //[blue][green][red][zero] values     //for 8 bit have following code     (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( = end_color; < end_color+400; i++) bitmap[i] = 0xff; }  void bmpwrite() {     file *file;     int i;      //use wb+ when writing binary file .i.e. in binary form whereas w+ txt file.     file = fopen("b.bmp", "wb+");     for( = 0; < 1478; i++)     {         fputc(bitmap[i], file);     }     fclose(file); } void main() {      bmpmake();     bmpwrite();     printf("done!!"); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -