1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <fstream>
#include <cstdlib>
#include "Bmp.h"
int main()
{
ifstream myBmpFile("lena512.bmp",ios::in|ios::binary);
if(!myBmpFile)
{
cerr<<"File Could not be Opened."<<endl;
exit(1);
}
BmpFileHeader myFH;
BmpInfoHeader myIH;
myBmpFile.read(reinterpret_cast<char*>(&myFH),sizeof(myFH));
myBmpFile.read(reinterpret_cast<char*>(&myIH),sizeof(myIH));
RGBQUAD* myPallete=new RGBQUAD[myIH.ncolors];
for(int i=0;i<myIH.ncolors;++i)
{
myBmpFile.read(reinterpret_cast<char*>(&myPallete[i]),sizeof(RGBQUAD));
}
cout<<"Size of File Header is : "<<sizeof(myFH)<<endl
<<"Size of Info Header is : "<<sizeof(myIH)<<endl;
long endOfHeaders=myBmpFile.tellg();
if(endOfHeaders!=1078)
{
cout<<"The file is corrupted"<<endl;
exit(1);
}
Bmp myBmp(myFH,myIH,myIH.width,myIH.height,myIH.bits,myIH.ncolors);//Constructing bmp object and initializing all private data members
cout<<endl<<myBmp<<endl;
myBmp.loadPallete(myPallete,myIH.ncolors);
long startLocation=myBmpFile.tellg();
cout<<"File pointer at the beginning of storing to the 2Darray :"<<myBmpFile.tellg()<<endl;
unsigned char*** myArray;
myArray=new unsigned char**[myBmp.getHeight()];
for(int i=0;i<myBmp.getHeight();++i)
{
myArray[i]=new unsigned char*[myBmp.getWidth()];
for(int j=0;j<myBmp.getWidth();++j)
{
myArray[i][j]=new unsigned char[(myBmp.getBpp()/8)];
for(int k=0;k<(myBmp.getBpp()/8);++k)
myArray[i][j][k]=0;
}
}
for(int i=0;i<myBmp.getHeight();++i)
{
for(int j=0;j<myBmp.getWidth();++j)
{
for(int k=0;k<myBmp.getBpp()/8;++k)
{
myBmpFile.read(reinterpret_cast<char*>(&myArray[i][j][k]),sizeof(unsigned char));
}
}
}
myBmp.loadArrayPtr(myArray,myBmp.getHeight(),myBmp.getWidth(),(myBmp.getBpp()/8));
long endLocation=myBmpFile.tellg();
cout<<"File pointer at the end of storing to the 2Darray :"<<myBmpFile.tellg()<<endl
<<"Total number of bytes read is : "<<endLocation-startLocation<<endl;
myBmpFile.close();
ofstream myCopiedBmp("copied.bmp",ios::out|ios::binary);
if(!myCopiedBmp)
{
cerr<<"File Could not be Opened."<<endl;
exit(1);
}
myCopiedBmp.write(reinterpret_cast<char*>(&myBmp.getBMPFH()),sizeof(myFH));
myCopiedBmp.write(reinterpret_cast<char*>(&myBmp.getBMPIH()),sizeof(myIH));
for(int i=0;i<myIH.ncolors;++i)
{
myCopiedBmp.write(reinterpret_cast<char*>(&myBmp.getPallete(i)),sizeof(RGBQUAD));
}
for(int i=0;i<myBmp.getHeight();++i)
{
for(int j=0;j<myBmp.getWidth();++j)
{
for(int k=0;k<myBmp.getBpp()/8;++k)
{
unsigned char data=myBmp.getImageArray(i,j,k);
myCopiedBmp.write(reinterpret_cast<char*>(&data),sizeof(unsigned char));
}
}
}
while(1);
return 0;
}
|