⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imgprocview.cpp

📁 图像处理源代码VC
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// imgprocView.cpp : implementation of the CImgprocView class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "imgproc.h"

#include "imgprocDoc.h"
#include "imgprocView.h"

#include "dibapi.h"
#include "mainfrm.h"

#include "comapi.h"
#include "fig2.h"
#include "InDlg1.h"

#include "tuxiang.h"
#include "addfile.h"

//#include "add.h"
#include "AddDlg.h"
#include "DelDlg.h"
#include "DelCheck.h"
#include "In2Dlg.h"
//#include "afx.h"
/////////////////////////////////////////////////////////
//CString title;
/////////////////////////////////////////////////////////
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CImgprocView

IMPLEMENT_DYNCREATE(CImgprocView, CScrollView)

//#define WM_MYOWN     (WM_USER+10)

BEGIN_MESSAGE_MAP(CImgprocView, CScrollView)
	//{{AFX_MSG_MAP(CImgprocView)
	ON_COMMAND(ID_1_1, On1_1)
	ON_COMMAND(ID_3_1_5, On3_1_5)
	ON_COMMAND(ID_3_1_1, On3_1_1)
	ON_COMMAND(ID_3_1_2, On3_1_2)
	ON_COMMAND(ID_3_1_3, On3_1_3)
	ON_COMMAND(ID_3_1_4, On3_1_4)
	ON_COMMAND(ID_2_1, On2_1)
	ON_COMMAND(ID_2_2, On2_2)
	ON_COMMAND(ID_2_3, On2_3)
	ON_COMMAND(ID_2_4, On2_4)
	ON_COMMAND(ID_2_5, On2_5)
	ON_COMMAND(ID_3_2, On3_2)
	ON_COMMAND(ID_3_3, On3_3)
	ON_COMMAND(ID_3_4, On3_4)
	ON_COMMAND(ID_3_6, On3_6)
	ON_COMMAND(ID_1_2, On1_2)
	ON_COMMAND(ID_1_3, On1_3)
	ON_COMMAND(ID_1_4, On1_4)
	ON_COMMAND(ID_4_1_1, On4_1_1)
	ON_COMMAND(ID_4_1_2, On4_1_2)
	ON_COMMAND(ID_4_1_3, On4_1_3)
	ON_COMMAND(ID_4_1_4, On4_1_4)
	ON_COMMAND(ID_4_1_5, On4_1_5)
	ON_COMMAND(ID_4_1_6, On4_1_6)
	ON_COMMAND(ID_4_1_7, On4_1_7)
	ON_COMMAND(ID_TIANJIA, OnTianjia)
	ON_COMMAND(ID_ITEM0, OnItem0)
	ON_COMMAND(ID_ITEM1, OnItem1)
	ON_COMMAND(ID_ITEM2, OnItem2)
	ON_COMMAND(ID_ITEM3, OnItem3)
	ON_COMMAND(ID_ITEM4, OnItem4)
	ON_COMMAND(ID_ITEM5, OnItem5)
	ON_COMMAND(ID_ITEM6, OnItem6)
	ON_COMMAND(ID_ITEM7, OnItem7)
	ON_COMMAND(ID_ITEM8, OnItem8)
	ON_COMMAND(ID_ITEM9, OnItem9)
	ON_COMMAND(ID_ITEM10, OnItem10)
	ON_COMMAND(ID_ITEM11, OnItem11)
	ON_COMMAND(ID_ITEM12, OnItem12)
	ON_COMMAND(ID_ITEM13, OnItem13)
	ON_COMMAND(ID_ITEM14, OnItem14)
	ON_COMMAND(ID_ITEM15, OnItem15)
	ON_COMMAND(ID_ITEM16, OnItem16)
	ON_COMMAND(ID_ITEM17, OnItem17)
	ON_COMMAND(ID_ITEM18, OnItem18)
	ON_COMMAND(ID_ITEM19, OnItem19)
	ON_COMMAND(ID_RESET, OnReset)
	ON_COMMAND(ID_DEL, OnDel)
	ON_COMMAND(ID_2_6, On2_6)
	ON_COMMAND(ID_2_7, On2_7)
	ON_COMMAND(ID_2_8, On2_8)
	ON_COMMAND(ID_2_9, On2_9)
	ON_COMMAND(ID_2_10, On2_10)
	ON_COMMAND(ID_2_11, On2_11)
	ON_COMMAND(ID_3_5, On3_5)
	ON_COMMAND(ID_4_1_8, On4_1_8)
	ON_COMMAND(ID_4_2_1, On4_2_1)
	ON_COMMAND(ID_4_2_2, On4_2_2)
	ON_COMMAND(ID_5_1, On5_1)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
	ON_MESSAGE(WM_MYOWN,OnMyOwn)
	//}}AFX_MSG_MAP

	// Standard printing commands
/*	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)*/
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgprocView construction/destruction

CImgprocView::CImgprocView()
{
	// TODO: add construction code here

}

CImgprocView::~CImgprocView()
{
}

/*BOOL CImgprocView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}
*/
/////////////////////////////////////////////////////////////////////////////
// CImgprocView drawing

void CImgprocView::OnDraw(CDC* pDC)
{
	CImgprocDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	HDIB hDIB = pDoc->GetHDIB();
	if (hDIB != NULL)
	{
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		int cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
		int cyDIB = (int) ::DIBHeight(lpDIB);        // Size of DIB - y
		::GlobalUnlock((HGLOBAL) hDIB);
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		CRect rcDest;
		if (pDC->IsPrinting())   // printer DC
		{
			// get size of printer page (in pixels)
			int cxPage = pDC->GetDeviceCaps(HORZRES);
			int cyPage = pDC->GetDeviceCaps(VERTRES);
			// get printer pixels per inch
			int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
			int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);

			//
			// Best Fit case -- create a rectangle which preserves
			// the DIB's aspect ratio, and fills the page horizontally.
			//
			// The formula in the "->bottom" field below calculates the Y
			// position of the printed bitmap, based on the size of the
			// bitmap, the width of the page, and the relative size of
			// a printed pixel (cyInch / cxInch).
			//
			rcDest.top = rcDest.left = 0;
			rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
					/ ((double)cxDIB * cxInch));
			rcDest.right = cxPage;
		}
		else   // not printer DC
		{
			rcDest = rcDIB;
		}
		::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
			&rcDIB, pDoc->GetDocPalette());
	}
}

void CImgprocView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	ASSERT(GetDocument() != NULL);

	SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}

/////////////////////////////////////////////////////////////////////////////
// CImgprocView printing

BOOL CImgprocView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}


/////////////////////////////////////////////////////////////////////////////
LRESULT CImgprocView::OnDoRealize(WPARAM wParam, LPARAM)
{
	ASSERT(wParam != NULL);
	CImgprocDoc* pDoc = GetDocument();
	if (pDoc->GetHDIB() == NULL)
		return 0L;  // must be a new document

	CPalette* pPal = pDoc->GetDocPalette();
	if (pPal != NULL)
	{
		CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
		ASSERT_KINDOF(CMainFrame, pAppFrame);

		CClientDC appDC(pAppFrame);
		// All views but one should be a background palette.
		// wParam contains a handle to the active view, so the SelectPalette
		// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
		CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);

		if (oldPalette != NULL)
		{
			UINT nColorsChanged = appDC.RealizePalette();
			if (nColorsChanged > 0)
				pDoc->UpdateAllViews(NULL);
			appDC.SelectPalette(oldPalette, TRUE);
		}
		else
		{
			TRACE0("\tSelectPalette failed in CDibView::OnPaletteChanged\n");
		}
	}

	return 0L;
}
// CImgprocView diagnostics
void CImgprocView::OnActivateView(BOOL bActivate, CView* pActivateView,
					CView* pDeactiveView)
{
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);

	if (bActivate)
	{
		ASSERT(pActivateView == this);
		OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE);
	}
}

void CImgprocView::OnEditCopy()
{
	CImgprocDoc* pDoc = GetDocument();
	// Clean clipboard of contents, and copy the DIB.

	if (OpenClipboard())
	{
		BeginWaitCursor();
		EmptyClipboard();
		SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
		CloseClipboard();
		EndWaitCursor();
	}
}



void CImgprocView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}


void CImgprocView::OnEditPaste()
{
	HDIB hNewDIB = NULL;

	if (OpenClipboard())
	{
		BeginWaitCursor();

		hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));

		CloseClipboard();

		if (hNewDIB != NULL)
		{
			CImgprocDoc* pDoc = GetDocument();
			pDoc->ReplaceHDIB(hNewDIB); // and free the old DIB
			pDoc->InitDIBData();    // set up new size & palette
			pDoc->SetModifiedFlag(TRUE);

			SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
			OnDoRealize((WPARAM)m_hWnd,0);  // realize the new palette
			pDoc->UpdateAllViews(NULL);
		}
		EndWaitCursor();
	}
}


void CImgprocView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}



#ifdef _DEBUG
void CImgprocView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CImgprocView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CImgprocDoc* CImgprocView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CImgprocDoc)));
	return (CImgprocDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImgprocView message handlers

void CImgprocView::On1_1() 
{

	::box1(this,"高斯噪声方差",addnoise);
	
//	title="高斯噪声";
   /* CInDlg1  dlg;
	
	dlg.m_value=0.5;
	if(dlg.DoModal()==IDOK)
	{
        unsigned short col,row;
    	unsigned char** uppimage;
	    unsigned char** outimage;

        CImgprocDoc* pDoc = GetDocument();
    	HDIB hDIB = pDoc->GetHDIB();

	    if (hDIB != NULL)
		{   
			HDIB back=(HDIB)CopyHandle((HANDLE)hDIB);

			uppimage=::DIBToArray(hDIB,col,row);

               	outimage=(unsigned char**)::fspace_2d(col,row,sizeof(unsigned char));

            //::addnoise(uppimage,outimage,row,col,dlg.m_value);
            ::addnoise(uppimage,outimage,col,row,(int)dlg.m_value);
		//		::Sobel(uppimage,outimage,row,col);
				
			::ArrayToDIB(col,row,outimage,hDIB);
			    
			    ::ffree_2d((void**)outimage,col);
			
			OnEditCopy();
            pDoc->ReplaceHDIB(back);
		    pDoc->SetModifiedFlag();

            SendMessage(WM_MYOWN);
		}
	}*/
//	box1(5,this,rotate);
}

void CImgprocView::OnMyOwn()
{
	((CImgprocApp*)AfxGetApp())->myFileNew();


     mfuc();


}

void CImgprocView::mfuc()
{
	CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

// Get the active MDI child window.
    CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

// or CMDIChildWnd *pChild = pFrame->MDIGetActive();
//	pChild->SetWindowText(title);
// Get the active view attached to the active MDI child
// window.
    CImgprocView *pView = (CImgprocView *) pChild->GetActiveView();
     
//  	CImgprocDoc* pDoc = pView->GetDocument();
   pView->OnEditPaste();

    Invalidate();  
}

void CImgprocView::On3_1_5() 
{
        
	unsigned short col,row;
    unsigned char** uppimage;
	unsigned char** outimage;

    CImgprocDoc* pDoc = GetDocument();
    HDIB hDIB = pDoc->GetHDIB();

	if (hDIB != NULL)
	{   
		BeginWaitCursor();

		HDIB back=(HDIB)CopyHandle((HANDLE)hDIB);

		uppimage=::DIBToArray(hDIB,col,row);

       	outimage=(unsigned char**)::fspace_2d(col,row,sizeof(unsigned char));

		::Sobel(uppimage,outimage,row,col);
				
		::ArrayToDIB(col,row,outimage,hDIB);
			    
		::ffree_2d((void**)outimage,col);
			
		OnEditCopy();
        pDoc->ReplaceHDIB(back);
//		pDoc->SetModifiedFlag();

        SendMessage(WM_MYOWN);

		EndWaitCursor();
	}
//	title="Sobel..无方向";	
}

void CImgprocView::On3_1_1() 
{
  	::box0(this,Sobel0);
//	title="Sobel..0";
}

void CImgprocView::myEditCopy()
{
       OnEditCopy();
}

void CImgprocView::On3_1_2() 
{
	::box0(this,Sobel45);
//	title="Sobel..45";
}

void CImgprocView::On3_1_3() 
{
	::box0(this,Sobel90);
//	title="Sobel..90";
}

void CImgprocView::On3_1_4() 
{
	::box0(this,Sobel135);
//	title="Sobel..135";	
}

void CImgprocView::On2_1() 
{
	// TODO: Add your command handler code here
	unsigned short col,row;
    unsigned char** uppimage;
	unsigned char** outimage;

    CImgprocDoc* pDoc = GetDocument();
    HDIB hDIB = pDoc->GetHDIB();

	if (hDIB != NULL)
	{   
		BeginWaitCursor();

		HDIB back=(HDIB)CopyHandle((HANDLE)hDIB);
		uppimage=::DIBToArray(hDIB,row,col);//row代表行数,col代表列数
       	outimage=(unsigned char**)::fspace_2d(row,col,sizeof(unsigned char));
		::FFT(uppimage,outimage,col,row,0);
		::ArrayToDIB(row,col,outimage,hDIB);
		::ffree_2d((void**)outimage,row);
		OnEditCopy();
        pDoc->ReplaceHDIB(back);
//		pDoc->SetModifiedFlag();
        SendMessage(WM_MYOWN);

		EndWaitCursor();
	}	
}

void CImgprocView::On2_2() 
{
	// TODO: Add your command handler code here
	unsigned short col,row;
    unsigned char** uppimage;
	unsigned char** outimage;

    CImgprocDoc* pDoc = GetDocument();
    HDIB hDIB = pDoc->GetHDIB();

	if (hDIB != NULL)
	{   
		BeginWaitCursor();

		HDIB back=(HDIB)CopyHandle((HANDLE)hDIB);
		uppimage=::DIBToArray(hDIB,row,col);//row代表行数,col代表列数
       	outimage=(unsigned char**)::fspace_2d(row,col,sizeof(unsigned char));
		::FFT(uppimage,outimage,col,row,1);
		::ArrayToDIB(row,col,outimage,hDIB);
		::ffree_2d((void**)outimage,row);
		OnEditCopy();
        pDoc->ReplaceHDIB(back);
//		pDoc->SetModifiedFlag();
        SendMessage(WM_MYOWN);

		EndWaitCursor();
	}		
}

void CImgprocView::On2_3() 
{
    box_0(this,KJFFT);	
}

void CImgprocView::On2_4() 
{
	::box_0(this,WFFT);	
//	title="微分";
	
}

void CImgprocView::On2_5() 
{
	::box_0(this,JFFT);
//	title="积分";
}

void CImgprocView::On3_2() 
{
	::box0(this,Roberts);
//	title="Roberts";
}

void CImgprocView::On3_3() 
{

	::box0(this,Kirsch);	
//	title="Kirsch";

}

void CImgprocView::On3_4() 
{
	::box0(this,Laplacian);	
//	title="Laplacian";
}

void CImgprocView::On3_6() 
{
	::box0(this,OtsuSegment);
//	title="Otsu区域";
}

void CImgprocView::On1_2() 
{
    ::box1(this,"旋转角度",rotate);	
//	title="旋转";
}

void CImgprocView::On1_3() 
{
    CInDlg1  dlg;
	
	dlg.m_value=1;
	dlg.m_text="降分辨率倍数";
	if(dlg.DoModal()==IDOK)
	{
        unsigned short col,row;
    	unsigned char** uppimage;
	    unsigned char** outimage;

        CImgprocDoc* pDoc = GetDocument();
    	HDIB hDIB = pDoc->GetHDIB();

	    if (hDIB != NULL)
		{   
		BeginWaitCursor();

			HDIB back=(HDIB)CopyHandle((HANDLE)hDIB);

			uppimage=::DIBToArray(hDIB,col,row);

               	outimage=(unsigned char**)::fspace_2d(col,row,sizeof(unsigned char));

            ::lower(uppimage,outimage,row,col,(short)dlg.m_value);
				
			::ArrayToDIB(col,row,outimage,hDIB);
			    
			    ::ffree_2d((void**)outimage,col);
			
			OnEditCopy();
            pDoc->ReplaceHDIB(back);
//		    pDoc->SetModifiedFlag();

            SendMessage(WM_MYOWN);

			EndWaitCursor();

		}
	}
//	title="降分辨率";
}

void CImgprocView::On1_4() 
{
    ::box1(this,"高斯噪声方差",smoothing);	
//	title="平滑";
}

void CImgprocView::On4_1_1() 
{
    ::box_1(this,Expand);	
//	title="Expand";
}

void CImgprocView::On4_1_2() 
{
	::box_1(this,Rot);	
//	title="Rot";
}

void CImgprocView::On4_1_3() 
{
    ::box_1(this,Open);		
//	title="Open";
}

void CImgprocView::On4_1_4() 
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -