john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

bmp raw

/* John Pfeiffer, 2010-07  write out raw bmp files
*/

#include <stdio.h>

typedef struct bmpFileHeaderT bmpFileHeader ;
typedef bmpFileHeader* bmpFileHeaderptr;

/* 14 bytes */
struct bmpFileHeaderT
{
    char bmIdentifier[2];
    char filesize[4];
    char reservedA[2];
    char reservedB[2];
    char dataAddress[4];
};


typedef struct bmpInfoHeaderT bmpInfoHeader ;
typedef bmpInfoHeader* bmpInfoHeaderptr;

/* size is 40 bytes for most compatible Win 3.0 version */
struct bmpInfoHeaderT
{
    char bmpInfoHeaderSize[4];
    char bmpWidth[4];
    char bmpHeight[4];
    char colorPlanes[2];
    char bitsPerPixel[2];
    char bmpCompression[4];
    char dataSize[4];  /* 4 bytes for every pixel if uncompressed */
    char horizontalResolution[4];
    char verticalResolution[4];
    char colorsInPalette[4];
    char importantColors[4];

};



void WritePixel( FILE** ofpbyref , char blue , char green , char red )
{
    fputc( blue , *ofpbyref );
    fputc( green , *ofpbyref );
    fputc( red , *ofpbyref );
} /* end WritePixel function */



/* #### MAIN  ########################## */

int main(int argc, char* argv[])
{
    /*
    char c;
    int buffer_counter=0;
    */
    bmpFileHeader outputDataFileHeader;
    bmpInfoHeader outputDataInfoHeader;

    FILE* ofp;

    ofp = fopen( "test.bmp" , "w" );

    if( (ofp == NULL) )
    {    printf( "Error opening file" );
    }

/* #### file header ################################ */    
    outputDataFileHeader.bmIdentifier[0] = 'B';
    outputDataFileHeader.bmIdentifier[1] = 'M';

    outputDataFileHeader.filesize[0] = 70;
    outputDataFileHeader.filesize[1] = 0;
    outputDataFileHeader.filesize[2] = 0;
    outputDataFileHeader.filesize[3] = 0;

    outputDataFileHeader.reservedA[0] = 0;
    outputDataFileHeader.reservedA[1] = 0;
    outputDataFileHeader.reservedB[0] = 0;
    outputDataFileHeader.reservedB[1] = 0;


    outputDataFileHeader.dataAddress[0] = 54;
    outputDataFileHeader.dataAddress[1] = 0;
    outputDataFileHeader.dataAddress[2] = 0;
    outputDataFileHeader.dataAddress[3] = 0;

    fwrite( outputDataFileHeader.bmIdentifier , 1 , sizeof( outputDataFileHeader.bmIdentifier ) , ofp );
    fwrite( outputDataFileHeader.filesize , 1 , sizeof( outputDataFileHeader.filesize ) , ofp );
    fwrite( outputDataFileHeader.reservedA , 1 , sizeof( outputDataFileHeader.reservedA ) , ofp );
    fwrite( outputDataFileHeader.reservedB , 1 , sizeof( outputDataFileHeader.reservedB ) , ofp );
    fwrite( outputDataFileHeader.dataAddress , 1 , sizeof( outputDataFileHeader.dataAddress ) , ofp );

/* #### info header ################################ */

    outputDataInfoHeader.bmpInfoHeaderSize[0] = 40;
    outputDataInfoHeader.bmpInfoHeaderSize[1] = 0;
    outputDataInfoHeader.bmpInfoHeaderSize[2] = 0;
    outputDataInfoHeader.bmpInfoHeaderSize[3] = 0;

    fwrite( outputDataInfoHeader.bmpInfoHeaderSize , 1 , sizeof( outputDataInfoHeader.bmpInfoHeaderSize ) , ofp );

    outputDataInfoHeader.bmpWidth[0] = 2;
    outputDataInfoHeader.bmpWidth[1] = 0;
    outputDataInfoHeader.bmpWidth[2] = 0;
    outputDataInfoHeader.bmpWidth[3] = 0;

    fwrite( outputDataInfoHeader.bmpWidth , 1 , sizeof( outputDataInfoHeader.bmpWidth ) , ofp );

    outputDataInfoHeader.bmpHeight[0] = 2;
    outputDataInfoHeader.bmpHeight[1] = 0;
    outputDataInfoHeader.bmpHeight[2] = 0;
    outputDataInfoHeader.bmpHeight[3] = 0;

    fwrite( outputDataInfoHeader.bmpHeight , 1 , sizeof( outputDataInfoHeader.bmpHeight ) , ofp );

    outputDataInfoHeader.colorPlanes[0] = 1;
    outputDataInfoHeader.colorPlanes[1] = 0;

    fwrite( outputDataInfoHeader.colorPlanes , 1 , sizeof( outputDataInfoHeader.colorPlanes ) , ofp );

    outputDataInfoHeader.bitsPerPixel[0] = 24;
    outputDataInfoHeader.bitsPerPixel[1] = 0;

    fwrite( outputDataInfoHeader.bitsPerPixel , 1 , sizeof( outputDataInfoHeader.bitsPerPixel ) , ofp );

    outputDataInfoHeader.bmpCompression[0] = 0;
    outputDataInfoHeader.bmpCompression[1] = 0;
    outputDataInfoHeader.bmpCompression[2] = 0;
    outputDataInfoHeader.bmpCompression[3] = 0;

    fwrite( outputDataInfoHeader.bmpCompression , 1 , sizeof( outputDataInfoHeader.bmpCompression ) , ofp );

    outputDataInfoHeader.dataSize[0] = 16;
    outputDataInfoHeader.dataSize[1] = 0;
    outputDataInfoHeader.dataSize[2] = 0;
    outputDataInfoHeader.dataSize[3] = 0;

    fwrite( outputDataInfoHeader.dataSize , 1 , sizeof( outputDataInfoHeader.dataSize ) , ofp );

    outputDataInfoHeader.horizontalResolution[0] = 0;
    outputDataInfoHeader.horizontalResolution[1] = 0;
    outputDataInfoHeader.horizontalResolution[2] = 0;
    outputDataInfoHeader.horizontalResolution[3] = 0;

    outputDataInfoHeader.verticalResolution[0] = 0;
    outputDataInfoHeader.verticalResolution[1] = 0;
    outputDataInfoHeader.verticalResolution[2] = 0;
    outputDataInfoHeader.verticalResolution[3] = 0;

    outputDataInfoHeader.colorsInPalette[0] = 0;
    outputDataInfoHeader.colorsInPalette[1] = 0;
    outputDataInfoHeader.colorsInPalette[2] = 0;
    outputDataInfoHeader.colorsInPalette[3] = 0;

    outputDataInfoHeader.importantColors[0] = 0;
    outputDataInfoHeader.importantColors[1] = 0;
    outputDataInfoHeader.importantColors[2] = 0;
    outputDataInfoHeader.importantColors[3] = 0;


    fwrite( outputDataInfoHeader.horizontalResolution , 1 , sizeof( outputDataInfoHeader.horizontalResolution ) , ofp );
    fwrite( outputDataInfoHeader.verticalResolution , 1 , sizeof( outputDataInfoHeader.verticalResolution ) , ofp );
    fwrite( outputDataInfoHeader.colorsInPalette , 1 , sizeof( outputDataInfoHeader.colorsInPalette ) , ofp );
    fwrite( outputDataInfoHeader.importantColors , 1 , sizeof( outputDataInfoHeader.importantColors ) , ofp );

/* #### begin bmp data ################################ */

/* bottom row left to right*/
    WritePixel( &ofp , 255 , 0 , 0 );
    WritePixel( &ofp , 0 , 255 , 0 );

/* alignment */
    fputc( 0 , ofp );
    fputc( 0 , ofp );

    WritePixel( &ofp , 255 , 255 , 255 );
    WritePixel( &ofp , 255 , 255 , 255 );

/* alignment */
    fputc( 0 , ofp );
    fputc( 0 , ofp );



    fclose(ofp);


    return 0;
}/* end main */

  • « bmp research
  • wss rss feeds using xml webpart »

Published

Jul 25, 2010

Category

c

~367 words

Tags

  • bmp 3
  • c 95
  • raw 3