Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   help needed for reading bitmap image with c (http://www.go4expert.com/forums/help-reading-bitmap-image-c-t18648/)

dssr 21Jul2009 19:38

help needed for reading bitmap image with c
 
hi i want to process a bitmap image with c, and store the pixel information in a file, row-column wise or in any other format but the padding should be omitted, and there should be another program which takes this pixel information file as an input file and produces the original bitmap file as an output, with my little knowledge i got very little success, i can read the header pretty successfully but i am having problem with the pixels & padding thing

here is my code


Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>


long extract(FILE *,long ,int );
void information(FILE *);


long extract(FILE *fp1,long offset,int size)
{
        unsigned char *ptr;
        unsigned char temp='0';
        long value=0L;
        int i;

        //to initialize the ptr
        ptr=&temp;

        //sets the file pointer at specific position i.e. after the offset
        fseek(fp1,offset,SEEK_SET);

        //now extracting (size) values starting from the offset
        for(i=1;i<=size;i++)
        {
                fread(ptr,sizeof(char),1,fp1);
                value=(long)(value+(*ptr)*(pow(256,(i-1))));  //combining the values one after another in a single variable
        }

        return value;
}



void information(FILE *fp1)
{
        FILE *fp4;
        if((fp4=fopen("info.txt","w"))==NULL)
        {
                printf("\n\aError while creating a file.\nSystem is exiting ..... ");
                exit(0);
        }

        fprintf(fp4,"Header Information");
        fprintf(fp4,"\n\nThe Magic Number or Signatur of the file : %c%c",(char)extract(fp1,0L,1),(char)extract(fp1,1L,1));
        fprintf(fp4,"\nThe size of the file : %ld bytes",extract(fp1,2L,4));
        fprintf(fp4,"\nUnused reserve data, application specific : ",extract(fp1,6L,2));
        fprintf(fp4,"\nUnused reserve data, application specific : ",extract(fp1,8L,2));
        fprintf(fp4,"\nThe offset where the bitmap data or pixel can be found : %d bytes",(int)extract(fp1,10L,4));
        fprintf(fp4,"\nThe number of bytes in the header (after this point) : %d bytes",(int)extract(fp1,14L,4));
        fprintf(fp4,"\nThe width of the bitmap in pixel : %d pixel",(int)extract(fp1,18L,4));
        fprintf(fp4,"\nThe height of the bitmap in pixel : %d pixel",(int)extract(fp1,22L,4));
        fprintf(fp4,"\nNumber of color planes being used : %d plane",(int)extract(fp1,26L,2));
        fprintf(fp4,"\nThe number of bits/pixel : %d bits",(int)extract(fp1,28L,2));
        fprintf(fp4,"\nCompression style : %d ",(int)extract(fp1,30L,4));
        fprintf(fp4,"\nThe size of the raw bmp data(after the header) : %d bytes",(int)extract(fp1,34L,4));
        fprintf(fp4,"\nThe horizontal resolution of the image : %d pixels/meter",(int)extract(fp1,38L,4));
        fprintf(fp4,"\nThe vertical resolution of the image  : %d pixels/meter",(int)extract(fp1,42L,4));
        fprintf(fp4,"\nNumber of colors in the palette : %d colors",(int)extract(fp1,46L,4));
        fprintf(fp4,"\nImportant colors : %d colors",(int)extract(fp1,50L,4));
}



int main()
{

        int row,col;
        int i,j,k;
        int dataoffset,offset;
        char magicnum[2];
        FILE *fp1,*fp2;


        if((fp1=fopen("alone.bmp","rb"))==NULL)
        {
                printf("\a\nCant open the image.\nSystem is exiting.");
                exit(0);
        }

        //setting the file pointer at the beginning
        //fseek(fp1,0L,SEEK_SET);
        rewind(fp1);

        //CHECKING WHETHER THE FILE IS IN BMP FORMAT OR NOT, WE CHECK THE MAGIC NUMBER OF THE FILE, MAGIC NUMBER'S OFFSET IS 0 i.e. IT'S STORED AT THE FRONT OF THE IMAGE, AND THE SIZE IS 2

        //at first extracting the magic number
        for(i=0;i<2;i++)
        {
                magicnum[i]=(char)extract(fp1,i,1);
        }

        //now checking
        if((magicnum[0]=='B') && (magicnum[1]=='M'))
                ;
        else
        {
                printf("\aThe image is not a bitmap image.\nSystem is exiting ...... ");
                exit(0);
        }


        //storing the header information
        information(fp1);

        //get the starting position or offset of the data(pixel)
        dataoffset=(int)extract(fp1,10,4);
                //printf("%d",dataoffset);

        //get the number of rows
        row=(int)extract(fp1,22,4);
                //printf("%d\n",row);

        //get the number of columns
        col=(int)extract(fp1,18,4);
                //printf("%d\n",col);


        //storing the data
        if((fp2=fopen("pixel.txt","wb"))==NULL)
        {
                printf("\a\nError while creating a file.\nSystem is exiting....");
                exit(0);
        }




        offset=dataoffset;
        for(j=0;j<col;j++)
        {
                for(k=0;k<row;k++)
                {
                        for(i=0;i<3;i++)
                        {
                                fprintf(fp2,"%d",(int)extract(fp1,offset++,1));
                                fprintf(fp2," ");

                        }
                        fprintf(fp2," ");
                }
                fprintf(fp2,"\n");
                offset+=2;
        }

        printf("\nFor image information see info.txt.\nFor pixels see pixel.txt.");
        fcloseall();
        return 0;
}

now plss help


All times are GMT +5.5. The time now is 04:45.