A C++ to show gray level slicing without background


#include<iostream>
#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
using namespace std;
struct pix{
unsigned char b,g,r;}
pixel;
int L=255;
int bi;
unsigned char Header[54],tmp;
ifstream in;
ofstream out,out1;
int main() {
char infile[]=”d:\\lena.bmp”;
char outfile[]=”d:\\lena-mod.bmp”;
char imdata[]=”d:\\imdata.dat”;
in.open(infile,ios::in|ios::binary);
in.read((char*)(&Header),sizeof(Header));
out.open(outfile,ios::out|ios::binary);
out.write((char*)(&Header),sizeof(Header));
out1.open(imdata,ios::out);
while(!in.eof()) {
in.read((char*) (&pixel),sizeof(pixel));
out1<<“ORIGINAL : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” ,
“<<(int)pixel.b<<endl;
tmp=(pixel.b+pixel.g+pixel.r)/3;
if(tmp>50 && tmp<100)
pixel.r=pixel.g=pixel.b=255;
else
pixel.r=pixel.g=pixel.b=0;
cout<<“\n MODIFIED : “<<(int)pixel.r<<” “<<(int)pixel.g<<”
“<<(int)pixel.b<<endl;
out.write((char*) (&pixel),sizeof(pixel));
out1<<“MODIFIED : “<<(int)pixel.r<<” “<<(int)pixel.g<<”
“<<(int)pixel.b<<endl;
}
in.close();
out.close();
out1.close();
getch(); }