#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#define _WIDTH 555
#define _HEIGHT 479
#define _BYTE_PER_PIXEL 1
using namespace std;
class BMP
{
private:
typedef unsigned char var8;
typedef unsigned int uvar32;
typedef unsigned short int uvar16;
typedef struct { var8 Blue,Green,Red,Reserved;} Palette;
var8 imdata[_WIDTH][_HEIGHT];
var8 imdata2[_WIDTH*_HEIGHT*_BYTE_PER_PIXEL];
public:
int getArrayRGB(int m,int n);
int init(char* BMPPath);
int findBorder(int & leftborder,int & rightborder);
int maxK(int column);
int avergeMaxK();
};
int BMP::getArrayRGB(int m,int n)// the mth row and the nth column;m=0,1,2,3...;n=0,1,2,3,...
{
return imdata2[_BYTE_PER_PIXEL*n+m*_WIDTH];
}
int BMP::init(char* BMPPath)
{
uvar32 Size,Offset,Height,Width,Compress,Reserved,
InfoHeaderSize,Colors,ImportantColors,DataSize,HResolution,VResolution;
uvar16 Planes,Bit;
int i;
var8 type[2];
Palette Rgbquad;
ifstream fin;
ofstream fout;
ofstream fout1("out1.txt");
fin.open(BMPPath,ios::binary);
bool debug=0;
if(!fin)
{
fout1<<"No this file!\n";
return 1;
}
fin.read((char*)&type ,2);
fout1<<"file type: "<<type[0]<<type[1]<<endl;
if(debug)cout<<"file type: "<<type[0]<<type[1]<<endl;
fin.read((char*)&Size ,sizeof(uvar32));
fout1<<"file size: "<<Size<<endl;
if(debug)cout<<"file size: "<<Size<<endl;
fin.read((char*)&Reserved,sizeof(uvar32));
fout1<<"Reserved dword: "<<Reserved<<endl;
if(debug)cout<<"Reserved dword: "<<Reserved<<endl;
fin.read((char*)&Offset ,sizeof(uvar32));
fout1<<"Offset: "<<Offset<<endl;
if(debug)cout<<"Offset: "<<Offset<<endl;
fin.read((char*)&InfoHeaderSize,sizeof(uvar32));
fout1<<"Bitmap Info Header Size: "<<InfoHeaderSize<<endl;
if(debug)cout<<"Bitmap Info Header Size: "<<InfoHeaderSize<<endl;
fin.read((char*)&Width,sizeof(uvar32));
fout1<<"Bitmap Width: "<<Width<<endl;
if(debug)cout<<"Bitmap Width: "<<Width<<endl;
fin.read((char*)&Height,sizeof(uvar32));
fout1<<"Bitmap Height: "<<Height<<endl;
if(debug)cout<<"Bitmap Height: "<<Height<<endl;
fin.read((char*)&Planes,sizeof(uvar16));
fout1<<"Bitmap Planes: "<<Planes<<endl;
if(debug)cout<<"Bitmap Planes: "<<Planes<<endl;
fin.read((char*)&Bit,sizeof(uvar16));
fout1<<"Bits per Pixel: "<<Bit<<endl;
if(debug)cout<<"Bits per Pixel: "<<Bit<<endl;
fin.read((char*)&Compress,sizeof(uvar32));
fout1<<"Compresssion Y or N: "<<Compress<<endl;
if(debug)cout<<"Compresssion Y or N: "<<Compress<<endl;
if(Compress) { fin.close();return 2;}
fin.read((char*)&DataSize,sizeof(uvar32));
fout1<<"Data Size: "<<DataSize<<endl;
fout1<<"Data Size: "<<DataSize<<endl;
fin.read((char*)&HResolution,sizeof(uvar32));
fout1<<"HResolution: "<<HResolution<<endl;
if(debug)cout<<"HResolution: "<<HResolution<<endl;
fin.read((char*)&VResolution,sizeof(uvar32));
fout1<<"VResolution: "<<VResolution<<endl;
if(debug)cout<<"VResolution: "<<VResolution<<endl;
fin.read((char*)&Colors,sizeof(uvar32));
fout1<<"Colors: "<<Colors<<endl;
if(debug)cout<<"Colors: "<<Colors<<endl;
fin.read((char*)&ImportantColors,sizeof(uvar32));
fout1<<"ImportantColors: "<<ImportantColors<<endl;
if(debug)cout<<"ImportantColors: "<<ImportantColors<<endl;
for(i=0;i<2;i++)
{
fin.read((char*)&Rgbquad,sizeof(Palette));
fout1<<int(Rgbquad.Blue)<<" "<<int(Rgbquad.Green)<<" "<<int(Rgbquad.Red)<<" "<<int(Rgbquad.Reserved)<<endl;
if(debug)cout<<int(Rgbquad.Blue)<<" "<<int(Rgbquad.Green)<<" "<<int(Rgbquad.Red)<<" "<<int(Rgbquad.Reserved)<<endl;
}
fin.seekg(Offset,ios::beg);
for( i=0;i<_HEIGHT*_WIDTH*_BYTE_PER_PIXEL;i++)
{
if(i%(_BYTE_PER_PIXEL*_WIDTH)==0)
{
if(debug)cout<<"\n"<<i/(_BYTE_PER_PIXEL*_WIDTH)<<endl;
fout1<<"\n"<<i/(_BYTE_PER_PIXEL*_WIDTH)<<endl;
}
if(i%_BYTE_PER_PIXEL==0)
{
if(debug)cout<<";";
fout1<<";";
}
if(i%(_BYTE_PER_PIXEL*_WIDTH)==0 && i!=0)
{
fin.read((char*)&imdata2[i],sizeof(var8));
//fin.read((char*)&imdata2[i],sizeof(var8));
//fin.read((char*)&imdata2[i],sizeof(var8));
}
else
{
fin.read((char*)&imdata2[i],sizeof(var8));
}
fout<<imdata2[i];
if(debug)cout<<(int)imdata2[i]<<" ";
fout1<<(int)imdata2[i]<<" ";
}
if(debug)cout<<endl;
fout.close();
return 0;
}
int BMP::findBorder(int & leftborder,int & rightborder)
{
int i,j,flag=0;
for(j=0;j<_WIDTH;j++)
{
for(i=0;i<_HEIGHT;i++)
{
if(getArrayRGB(i,j)>250)
{
leftborder=j;
flag=1;
break;
}
}
if(flag) break;
}
flag=0;
for(j=_WIDTH-1;j>=0;j--)
{
for(i=0;i<_HEIGHT;i++)
{
if(getArrayRGB(i,j)>200)
{
rightborder=j;
flag=1;
break;
}
}
if(flag) break;
}
return 0;
}
int BMP::maxK(int column)
{
int h[_HEIGHT],k=0;
//
//cout<<a<<" "<<b;
for(int i=0;i<_HEIGHT;i++)
{
h[i]=getArrayRGB(i,column);
//cout<<h[i]<<" ";
}
for( i=0;i<_HEIGHT-1;i++)
{
int temp=h[i+1]-h[i];
temp=(temp>0)?temp:-temp;
if(temp>k)
{
k=temp;
}
}
return k;
}
int BMP::avergeMaxK()
{
int a,b,K=0;
findBorder(a,b);
for(int j=a;j<b;j++)
{
K+=maxK(j);
}
K=K/(b-a);
return K;
}
/*
int main()
{
BMP a;
a.init("test.bmp");
return 0;
}
*/