www.pudn.com > MapCut.rar > MapCutView.cpp


// MapCutView.cpp : implementation of the CMapCutView class 
// 
 
#include "stdafx.h" 
#include "MapCut.h" 
 
#include "MapCutDoc.h" 
#include "MapCutView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView 
 
IMPLEMENT_DYNCREATE(CMapCutView, CView) 
 
BEGIN_MESSAGE_MAP(CMapCutView, CView) 
	//{{AFX_MSG_MAP(CMapCutView) 
	ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771) 
	ON_COMMAND(ID_MENUITEM32772, OnMenuitem32772) 
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen) 
	ON_COMMAND(ID_MENUITEM32776, OnMenuitem32776) 
	ON_COMMAND(ID_MENUITEM32777, OnMenuitem32777) 
	ON_COMMAND(ID_MENUITEM32775, OnMenuitem32775) 
	ON_COMMAND(ID_MENUITEM32778, OnMenuitem32778) 
	ON_COMMAND(ID_MENUITEM32781, OnMenuitem32781) 
	ON_COMMAND(ID_MENUITEM32782, OnMenuitem32782) 
	ON_COMMAND(ID_MENUITEM32783, OnMenuitem32783) 
	ON_COMMAND(ID_MENUITEM32784, OnMenuitem32784) 
	ON_COMMAND(ID_MENUITEM32785, OnMenuitem32785) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView construction/destruction 
 
CMapCutView::CMapCutView() 
{ 
	// TODO: add construction code here 
	flag=0; 
	m_pBmpFileHeader=NULL; 
	m_pBmpInfo=NULL; 
	m_pBmpInfoHeader=NULL; 
	pdib=NULL; 
 
} 
 
CMapCutView::~CMapCutView() 
{ 
	if(m_pBmpFileHeader!=NULL) 
	    delete m_pBmpFileHeader; 
	if(m_pBmpInfo!=NULL)  
		delete m_pBmpInfo; 
	if(m_pBmpInfoHeader!=NULL) 
		delete m_pBmpInfoHeader; 
	if(pdib!=NULL) 
		delete pdib; 
} 
 
BOOL CMapCutView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView drawing 
 
void CMapCutView::OnDraw(CDC* pDC) 
{ 
	CMapCutDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	//给出图像的位置,显示图像	 
	 if(flag==1) 
	{ 
		int width=m_pBmpInfoHeader->biWidth; 
		int height=m_pBmpInfoHeader->biHeight; 
		ShowPhoto(50,10,width,height,pDC); 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView printing 
 
BOOL CMapCutView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMapCutView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMapCutView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView diagnostics 
 
#ifdef _DEBUG 
void CMapCutView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMapCutView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMapCutDoc* CMapCutView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapCutDoc))); 
	return (CMapCutDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapCutView message handlers 
 
void CMapCutView::OnMenuitem32771()  
{ 
	// TODO: Add your command handler code here 
	//图像预处理—灰度图 
 
	COLORREF c; 
	int width=m_pBmpInfoHeader->biWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	 
	int gray,x,y; 
	float s; 
	BeginWaitCursor(); 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0)  
		BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp1=new BYTE[lenth]; 
	} 
	for(y=0;yGetSafeHdc(),x,y,cx,cy, 
				0,0,m_pBmpInfoHeader->biWidth,m_pBmpInfoHeader->biHeight, 
				pdib,m_pBmpInfo,DIB_RGB_COLORS,SRCCOPY); 
} 
void CMapCutView::SetPixel(int x,int y,COLORREF color,BYTE *pd) 
{ 
	//在pd指向的空间的(x,y)点添入颜色color 
 
	int width=m_pBmpInfoHeader->biWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	int r,g,b; 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0) 
		BytesPerLine=(BytesPerLine/4+1)*4; 
	if(x<0||x>=width||y<0||y>=height) 
	{ 
		AfxMessageBox("超出图像范围"); 
		return; 
	} 
	y=height-1-y; 
	unsigned long temp=y*BytesPerLine+x*3; 
 
	r=GetRValue(color); 
	g=GetGValue(color); 
	b=GetBValue(color); 
	*(pd+temp)=b; 
	*(pd+temp+1)=g; 
	*(pd+temp+2)=r; 
} 
 
COLORREF CMapCutView::GetPixel(int x, int y) 
{ 
	//获得(x,y)点的象素值 
 
	int width=m_pBmpInfoHeader->biWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0) 
		BytesPerLine=(BytesPerLine/4+1)*4; 
	if(x<0||x>=width||y<0||y>=height) 
	{ 
		AfxMessageBox("超出图像范围"); 
	return RGB(0,0,0); 
	} 
	y=height-1-y; 
	unsigned long temp=y*BytesPerLine+x*3; 
	unsigned r,g,b; 
	b=*(pdib+temp); 
	g=*(pdib+temp+1); 
	r=*(pdib+temp+2); 
	return RGB(r,g,b);    
} 
 
 
float CMapCutView::get_s(int x,int y) 
{ 
	//计算(x,y)点的饱和度并返回 
 
    COLORREF c; 
	float r,g,b,m,s; 
	 
    c=GetPixel(x,y); 
	r=GetRValue(c); 
	g=GetGValue(c); 
	b=GetBValue(c); 
 
	if ((r+g+b)==0)     //r+g+b==0 
        s=0; 
	else {  
	      m=min(r,g); 
           if (b10) 
             s=1-3*m/(r+g+b);   //min(r,g,b)>10 
           else  
		   { m=min((255-r),(255-g)); 
                if ((255-b)biWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	 
	int grey,x,y,i,j; 
    COLORREF array[10]; 
	BeginWaitCursor();//?????????? 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0)  
		BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp2=new BYTE[lenth]; 
	} 
 
//3*3模板中值滤波 
 
	for(y=1;ybfType!=0x4d42) 
	{ 
		AfxMessageBox("This is not a valid bitmap file"); 
		Bitmapfile.Close(); 
		flag=0;return; 
	}//判断是否为bitmap文件 
	if(m_pBmpInfoHeader->biCompression!=0) 
	{ 
		AfxMessageBox("this system support un-imcompressed image"); 
		Bitmapfile.Close(); 
		return; 
	} 
	switch(m_pBmpInfoHeader->biBitCount) 
	{ 
	case 1: 
		numcolor=2; 
		break; 
	case 4: 
		numcolor=16; 
		break; 
	case 8: 
		numcolor=256; 
		break; 
	case 24: 
		numcolor=0; 
		break; 
	default: 
		AfxMessageBox("the number of colors is not valid"); 
		Bitmapfile.Close(); 
		return; 
	} 
 
	Bitmapfile.SeekToBegin(); 
 
	if(numcolor!=0) 
	{ 
		Bitmapfile.Read(m_pBmpFileHeader,sizeof(BITMAPFILEHEADER)); 
	    Bitmapfile.Read(m_pBmpInfoHeader,sizeof(BITMAPINFOHEADER)); 
	    m_pBmpInfo=(BITMAPINFO *)new char [sizeof(BITMAPINFOHEADER)+numcolor*sizeof(RGBQUAD)]; 
	    Bitmapfile.Read(&(m_pBmpInfo->bmiColors[0]),numcolor* sizeof(RGBQUAD)); 
	} 
	else 
	{ 
		Bitmapfile.Read(m_pBmpFileHeader,sizeof(BITMAPFILEHEADER)); 
	    Bitmapfile.Read(m_pBmpInfoHeader,sizeof(BITMAPINFOHEADER)); 
		m_pBmpInfo=(BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER)]; 
	} 
    m_pBmpInfo->bmiHeader=*m_pBmpInfoHeader;   
	pdib=(BYTE*)new char[m_pBmpInfo->bmiHeader.biSizeImage]; 
	Bitmapfile.Read(pdib,m_pBmpInfo->bmiHeader.biSizeImage); 
	Bitmapfile.Close(); 
	flag=1; 
	pdibtmp=pdib; 
	Invalidate(); 	 
	 
} 
 
void CMapCutView::OnMenuitem32776()  
{ 
	// TODO: Add your command handler code here 
	//水平差分图像 
	pdib=pdibtmp2; 
	int width=m_pBmpInfoHeader->biWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	 
	int grey,x,y; 
    COLORREF m,n; 
	BeginWaitCursor();//意思??????? 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0)  
		BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp3=new BYTE[lenth]; 
	} 
	for(x=0;xbiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
		COLORREF c; 
	BeginWaitCursor(); 
 
    for(y=0;ybiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
 
    init=hori[3]; 
	for(x=0;x<2;x++)          //利于公式进行二次平滑 
	{ 
	hori[2]=init; 
    for(y=3;ybiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
 
 
	COLORREF m; 
	int r,g,b; 
	BeginWaitCursor(); 
    max=hori[2]; 
	for(y=3;ymax) 
	{ max=hori[y];ym=y;} 
      
	y=ym;                   //找到靠近波峰的上下两个谷点 
	while(hori[y]>hori[y-1]) 
	y--; 
	min1=y; 
	y=ym; 
	while(hori[y]>hori[y+1]) 
		y++; 
    min2=y; 
 
	//将两个谷点间的图像显示出来 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0)  
		BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp7=new BYTE[lenth]; 
	} 
    hori1=min1+5;hori2=min2+5; 
    pdib=pdibtmp; 
	 
	for(y=0;ybiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	 
	int grey,x,y; 
    COLORREF m,n; 
	BeginWaitCursor(); 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0) BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp4=new BYTE[lenth]; 
	} 
	for(y=0;ybiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	COLORREF c; 
	BeginWaitCursor(); 
 
    for(x=0;xbiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
 
    init=vert[3]; 
	for(y=0;y<2;y++)  //利于公式二次平滑 
	{ 
	vert[2]=init; 
    for(x=3;xbiWidth; 
	int height=m_pBmpInfoHeader->biHeight; 
	COLORREF m; 
	int r,g,b; 
	BeginWaitCursor(); 
 
     //将平滑后的垂直投影直方图分成两部分,分别求波峰 
    max1=vert[2];               
	for(x=3;x<(width-2)/2;x++) 
    if (vert[x]>max1) 
	{ max1=vert[x];xm1=x;} 
    max2=vert[(width-2)/2]; 
	for(x=(width-2)/2+1;xmax2) 
	{ max2=vert[x];xm2=x;} 
	vert1=xm1-5;vert2=xm2-5; 
 
	unsigned long BytesPerLine=m_pBmpInfoHeader->biWidth*(m_pBmpInfoHeader->biBitCount/8); 
	if(BytesPerLine%4!=0) BytesPerLine=(BytesPerLine/4+1)*4; 
	long lenth=m_pBmpInfoHeader->biHeight*BytesPerLine; 
	if(pdib!=NULL) 
	{ 
		pdibtmp9=new BYTE[lenth]; 
	} 
	pdib=pdibtmp7;             
 
	for(y=0;y