Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a program to convert a bitmap file of an image into binary form using macros in C

What I have tried:

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h> 
#include<math.h>
#define RGB(r,g,b) (r|g<<8|b<<16)

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) 
{ 
	 printf("\nThe width of the bitmap in pixel : %d pixel\n",(int)extract(fp1,18L,4));     
	 printf("\nThe height of the bitmap in pixel : %d pixel\n",(int)extract(fp1,22L,4));     
}       

int main() 
{ 
      int row,col;     
	  int i,j,k;     
	  int dataoffset,offset;     
	  char num[2];     
	  int color;
	  FILE *fp1,*fp2;         
	  if((fp1=fopen("C:\\Users\\Raghava\\Desktop\\logo104.bmp","rb"))==NULL)     
	  {
	           printf("\a\nCant open the image.\nSystem is exiting.");         
			      
	  }       //setting the file pointer at the beginning 
     
	  rewind(fp1);       //CHECKING WHETHER THE FILE IS IN BMP FORMAT OR NOT, WE CHECK THE NUMBER OF THE FILE, 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 number     
	  for(i=0;i<2;i++)     
	  {  
	         num[i]=(char)extract(fp1,i,1); 
      }       //now checking     
	  if((num[0]=='B') && (num[1]=='M'));     
	  else    
	  {         
	  		printf("\a The image is not a bitmap image.\n System is exiting ...... ");             
	  }         //storing the header information 
	      
	  information(fp1);       //get the starting position or offset of the data(pixel)     
	  dataoffset=(int)extract(fp1,10,4);
	  
	  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("pixel2.txt","wb"))==NULL)     
	  { 
	          printf("\a\n Error while creating a file.\n System is exiting....");         
			  //exit(0);     
	  }        
	     
	  offset=dataoffset; 
	  for(k=0;k<row;k++)       
	  {
	  	for(j=0;j<col;j++) 
	  	{	       
	  		/*if(RGB(0,0,0))
	  		{
	  			fprintf(fp2,"1");
			}
			else
			{
				fprintf(fp2,"0");
			}*/
	    }
	    fprintf(fp2,"\r\n");
	  }

	  printf("\n For pixels see pixel2.txt.");          
	  return 0; 
}
Posted
Updated 26-Jun-18 23:52pm
v2
Comments
OriginalGriff 27-Jun-18 5:46am    
And? Apart from probably crashing because your pointers are ... um ... interesting ... what does it do that you didn't expect or not do that you did?
Are there any error messages? If so, what are they, and where?
What have you tried to fix this?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
KarstenK 27-Jun-18 8:10am    
Tip: each bitmap file starts with the file header. Read about the data format to understand that. The header informs you HOW to deal with the following bits. ;-)

1 solution

You'll need to read the number of bits per pixel:
bpp=(int)extract(fp1,24,2); 

How to read each pixel depends on the number of bits per pixel. For the most common case of 24 bpp, you can read 3 bytes which will be the blue, green and red values of your pixel.

Have a look here for a complete description: BMP file format - Wikipedia[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900