I am trying to write some code to draw the 2x2 bitmap on BMP file format - Wikipedia, the free encyclopedia.
I don't know how to fill in the array of colors .. so far as my thinking is going I think I need a structure with red green and brown elements and an array to hold copies of the structure.
how to access this or write out the results is still outside my skills .. I have been looking for a while on this issue .. so any help again very much appreciated ..
heres my code so far .. its a work in progress but you can see what I have done so far .. thanks 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>
#include<errno.h>
struct bmpfile{
unsigned char magic[2];
};
struct fileheader{
uint32_t filesize;
uint16_t creator1;
uint16_t creator2;
uint32_t bmpoffset;
};
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;
};
struct rgb{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;
};
void create( struct bmpfile* bfp, struct fileheader* fhp,
struct bmpinfoheader* bhp );
void *write( struct bmpfile* bfp, struct fileheader* fhp,
struct bmpinfoheader* bhp, FILE *fp, struct rgb* p );
FILE *open( FILE *fp );
int close( FILE *fp );
void create( struct bmpfile* bfp, struct fileheader* fhp,
struct bmpinfoheader* bhp ){
int bpl;
bfp->magic[0] = 'B';
bfp->magic[1] = 'M';
fhp->filesize = 70;
fhp->bmpoffset = 54;
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;
}
/*
the total number of bytes necessary to store one row of pixels can be calculated
as (( width * bitsperpixel ) / 32 ) * 4;
in this case it is (( 2 * 24 ) / 8 ) = 6 rounded up to 8
bpl = p->width * 3;
if( bpl & 0x0003 ){
bpl != 0x0003;
++bpl;
the total number of bytes necessary to store an array of pixels can be
calculated by accounting for the effect of rounding up the size of each row to
a multiple of 4 bytes ..
pixelarraysize = rowsize * | height |
8 * 2 = 16
the total bitmap image filesize can be approximated as filesize = 54 +
pixelarraysize = 70
offset is always 54
fhp->filesize = fhp->bmpoffset + bmp->hieght * bpl
fhp->bmpoffset = 54
headersize is for bytes after bm ie the 24 + 16 = 40
*/
void *write( struct bmpfile* bfp, struct fileheader* fhp,
struct bmpinfoheader* bhp, FILE *fp, struct rgb* p ){
fwrite(&bfp,sizeof(fp),1,fp);
fwrite(&fhp,sizeof(fp),1,fp);
fwrite(&bhp,sizeof(fp),1,fp);
/*
write out color array here
*/
}
struct rgb *colors( struct rgb* p ){
int c;
for( c = 0; c < 4; c++ ){
p->
}
// 2nd trouble spot
return( p );
}
FILE *open( FILE *fp ){
errno = 0;
//function may choke on null filename ..
fp = fopen("foo.bmp","wb");
if( fp == NULL ){
fprintf( stderr,"open fail %s\n",strerror(errno));
}
return( fp );
}
int close( FILE *fp ){
int s;
if( fp == NULL ){
return 0;
}
errno = 0;
fclose( fp );
if( s == EOF ){
perror("close fail");
}
return s;
}
int main(){
struct bmpfile bf;
struct bmpfile *bfp;
bfp = & bf;
struct fileheader fh;
struct fileheader *fhp;
fhp = &fh;
struct bmpinfoheader bh;
struct bmpinfoheader *bhp;
bhp = &bh;
struct rgb color[ 4 ];
struct rgb *p;
p = &color;
FILE *fp;
colors( p );
create( bfp,fhp,bhp );//bitmap
open( fp );
write( bfp, fhp, bhp, fp, p );
close( fp );
return( 0 );
}