A C++ to show bit slicing


#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:\\bf.bmp”;
char outfile[]=”d:\\bf-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);
do
{
cout<<“Which bit image you want to extract 0=LSB 7=MSB : “;
cin>>bi;
}
while(bi<0||bi>7);
while(!in.eof())
{
in.read((char*) (&pixel),sizeof(pixel));
cout<<“\n ORIGINAL “<<(int)pixel.r<<” “<<(int)pixel.g<<” “<<(int)pixel.b;
out1<<“ORIGINAL : “<<(int)pixel.r<<” , “<<(int)pixel.g<<” , “<<(int)pixel.b<<endl;
tmp=(pixel.b+pixel.g+pixel.r)/3;
pixel.b=pixel.g=pixel.r=tmp&(int)pow(2.0,bi);
if(pixel.b!=0)
pixel.b=pixel.g=pixel.r=255;
else
pixel.b=pixel.g=pixel.r=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();
}