I am trying to write a bitmap file taking BMP file format - Wikipedia, the free encyclopedia as my reference .. I am trying to write the 2x2 bitmap.
the code compiles and runs ok but when I try and open the file I get an error "cannot be displayed, because it contains errors"
any help appreciated thanks in advance al.
Code:
//objective write bimap from
// http://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header
#include<stdio.h>
#include<stdlib.h>
#include<inttypes.h>
struct bmpfile{
unsigned char magic[2];
}bf,*p;
struct fileheader{
uint32_t filesize;
uint16_t creator1;
uint16_t creator2;
uint32_t bmpoffset;
}fh,*fhp;
struct bmpinfoheader{
uint32_t headersize;
int32_t width;
int32_t height;
uint16_t nplanes;
uint16_t bitsperpixel;
uint32_t compressiontype;
uint32_t bitmapbytesize;
int32_t hres;
int32_t vres;
uint32_t ncolors;
uint32_t nimpcolors;
}bh,*bhp;
int main(){
printf("main\n");
int c;
FILE *fp;
unsigned char array[16],*pp;
pp = array;
p = &bf;
fhp = &fh;
bhp = &bh;
printf("set array to zero\n");
for( c = 0; c <= 16; c++ ){
pp[c] = 0;
}
printf("colors\n");
pp[0] = 255;
pp[3] = 255;
pp[4] = 255;
pp[5] = 255;
pp[8] = 255;
pp[12] = 255;
p->magic[0] = 'B';
p->magic[1] = 'M';
printf("set bm\n");
fhp->filesize = 70;
fhp->bmpoffset = 54;
fhp->creator1 = 0;
fhp->creator2 = 0;
printf("set filesize\n");
bhp->width = 2;
bhp->height = 2;
bhp->nplanes = 1;
bhp->bitsperpixel = 24;
bhp->compressiontype = 0;
bhp->bitmapbytesize = 16;
bhp->hres = 2835;
bhp->vres = 2835;
bhp->ncolors = 0;
bhp->nimpcolors = 0;
bhp->headersize = bhp->bitsperpixel + bhp->bitmapbytesize;
printf("bitmapinfoheader\n");
fp = fopen("foo.bmp","ab");
printf("fopen\n");
fwrite(p,sizeof(bf),1,fp);
printf("bfp\n");
fwrite(fhp,sizeof(fh),1,fp);
printf("fhp\n");
fwrite(bhp,sizeof(bh),1,fp);
printf("bhp\n");
fwrite(array,sizeof(array),1,fp );
// fwrite( p,sizeof(array[ 0 ]),16,fp );
printf("array\n");
fclose( fp );
printf("close\n");
return( 0 );
}