#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
#include<math.h>
#include<fstream.h>
#define MAXX 300
#define MAXY 300
#define biwidth 256
#define biheight 256
#define off_bits 1078
struct bithead {
char bm[2];
long int size;
int no1;
int no2;
long int offset;
};
struct info_header {
long int size1;
long int width;
long int height;
int plane;
int bit_count;
long int comp;
long int simage;
long int xppm;
long int yppm;
long int clrused;
long int clrimp;
};
void write_to_file(FILE *);
void read_image_into_array(FILE *);
void make_image_data_binary();
int huge image[MAXX][MAXY];
char a[1078];
void main()
{
clrscr();
FILE *fp,*op;
struct bithead bmp; struct info_header inf;
fp=fopen("bird.bmp","rb");
if(fp==NULL) {
cout<<"Cannot open the file";
getch();
exit(0);
}
fread(&bmp,sizeof(bmp),1,fp); fread(&inf,sizeof(inf),1,fp);
cout<<"\n********Header information*********\n\n";
cout<<"\nBMP FILE HEADER="<<bmp.bm;
cout<<"\n\nSize of file="<<bmp.size;
cout<<"\nRESERVED WORD1="<<bmp.no1;
cout<<"\nRESERVED WORD2="<<bmp.no2;
cout<<"\nOffset of Data="<<bmp.offset;
cout<<"\n\nBITMAP INFO HEADER";
cout<<"\n\nSize of Bitmap Info header="<<inf.size1;
cout<<"\nWidth of Bitmap="<<inf.width;
cout<<"\nHeight of Bitmap="<<inf.height;
cout<<"\nNumber of planes="<<inf.plane;
cout<<"\nNumber of bits per pixel="<<inf.bit_count;
cout<<"\nType of Compression="<<inf.comp;
cout<<"\nSize of image="<<inf.simage;
cout<<"\nHorizontal Resolution="<<inf.xppm;
cout<<"\nVertical Resolution="<<inf.yppm;
cout<<"\nNo. of Colors used="<<inf.clrused;
cout<<"\nNo. of Important Colors ="<<inf.clrimp;
fclose(fp);
fp=fopen("bird.bmp","rb");
op=fopen("c:\\tc\\results\\copy.bmp","wb");
printf("\n********************************** \n");
printf("\nImage Copied at following location: \n");
printf("\nc:\\tc\\results \n");
if(fp==NULL||op==NULL)
{
exit(0);
}
fread(&a,sizeof(a),1,fp); fwrite(&a,sizeof(a),1,op);
read_image_into_array(fp);
fclose(fp);
write_to_file(op);
fclose(op);
fp=fopen("bird.bmp","rb");
if(fp==NULL)
{
exit(0);
}
fread(&a,sizeof(a),1,fp);
read_image_into_array(fp);
fclose(fp);
op=fopen("c:\\tc\\results\\binary.bmp","wb"); fwrite(&a,sizeof(a),1,op);
printf("\n********************************** \n");
printf("\nImage Binarization successfully performed \n");
printf("\Binary Image stored at following location: \n");
printf("\nc:\\tc\\results \n");
make_image_data_binary();
write_to_file(op);
fclose(op);
printf("\n********************************** \n");
getch();
exit(0);
}
void write_to_file(FILE *file)
{
int x=0,y=0;
fseek(file,off_bits,0);
for(x=0;x<biheight;x++)>
for(y=0;y<biwidth;y++)>
fputc(int(image[x][y]),file);
}
void read_image_into_array(FILE *file)
{
int x=0,y=0;
fseek(file,off_bits,0);
for(x=0;x<biheight;x++)>
for(y=0;y<biwidth;y++)>
image[x][y]=fgetc(file);
}
void make_image_data_binary()
{
int x=0,y=0;
for(x=0;x<biheight;x++)>
for(y=0;y<biwidth;y++)>
{
if(image[x][y]>128)
image[x][y]=255;
else if(image[x][y]<=128)
image[x][y]=0;
}
}
in this code iv converted color image to binary .but now i want to convert color image to grascale image.and i dont know how to go about doing it.plz help