Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/18/10 06:07
Read: times


 
Msg Score: -1
 -2 Off-Topic
 +1 Good Question
#178067 - *.bmp file
hi
i am writing a c program for converting a *.bmp file to its equivalent hex file.i have written the following code
but i am not able to get the pixel information .i want information if file is monochrome that is 1bit/pixel format.

#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("naError while creating a file.nSystem is exiting ..... ");
exit(0);
}

fprintf(fp4,"Header Information");
fprintf(fp4,"nnThe 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));
}



void main(void)
{

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

clrscr();
if((fp1=fopen("abc.bmp","rb"))==NULL)
{
printf("anCant open the image.nSystem is exiting.");
goto exitcode;
}

//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 ...... ");
goto exitcode;
}


//storing the header information
information(fp1);

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

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

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


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




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.");
exitcode: fcloseall();
getch();

}
please help



List of 4 messages in thread
TopicAuthorDate
*.bmp file            01/01/70 00:00      
   Embedded decode of BMP to text file???            01/01/70 00:00      
      .bmp to .c            01/01/70 00:00      
         A hexdump of a file does not need to know file format            01/01/70 00:00      

Back to Subject List