Thread: bitmap file ..

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    bitmap file ..

    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 );
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read this: Data structure alignment - Wikipedia, the free encyclopedia.

    I don't know what compiler you're using and whether #pragma pack will work for you (you want single-byte alignment). You can always write out the struct components one-by-one to be safe.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Your fopen is appending to the file.
    If you have a bad file you're just adding data to it.

    Dylan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming with the bitmap file format
    By redneon in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2007, 06:12 PM
  2. reading bitmap file
    By R.Stiltskin in forum C++ Programming
    Replies: 7
    Last Post: 01-03-2007, 01:41 PM
  3. Writing Raw Bitmap File
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 06-05-2005, 12:06 AM
  4. Hello all (re:bitmap image file)
    By Plastyksouljah in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2003, 10:07 PM
  5. large bitmap file
    By sunis in forum Windows Programming
    Replies: 3
    Last Post: 01-16-2003, 04:06 AM