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

📄 contrastview.cpp

📁 visual c++数字图像与图形处理中的光盘内容
💻 CPP
字号:
// ContrastView.cpp : implementation of the CContrastView class
//

#include "stdafx.h"
#include "Contrast.h"

#include "ContrastDoc.h"
#include "ContrastView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "ContrastProcess.h"

/////////////////////////////////////////////////////////////////////////////
// CContrastView

IMPLEMENT_DYNCREATE(CContrastView,  CView)

BEGIN_MESSAGE_MAP(CContrastView,  CView)
	//{{AFX_MSG_MAP(CContrastView)
	ON_COMMAND(IDM_INCREASE_CONTRAST,  OnIncreaseContrast)
	ON_UPDATE_COMMAND_UI(IDM_INCREASE_CONTRAST,  OnUpdateIncreaseContrast)
	ON_COMMAND(IDM_DECREASE_CONTRAST,  OnDecreaseContrast)
	ON_UPDATE_COMMAND_UI(IDM_DECREASE_CONTRAST,  OnUpdateDecreaseContrast)
	ON_COMMAND(IDM_CONTRAST_RESTORE,  OnContrastRestore)
	ON_UPDATE_COMMAND_UI(IDM_CONTRAST_RESTORE,  OnUpdateContrastRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContrastView construction/destruction

CContrastView::CContrastView()
{
	// TODO: add construction code here
	m_nWidth = 800;
	m_nHeight = 600;
	m_nOperation = Restore;
	m_nIncrement = 0;

}

CContrastView::~CContrastView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CContrastView drawing

void CContrastView::OnDraw(CDC* pDC)
{
	CContrastDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CDib* pDib = pDoc->m_pDib;
	if(pDib)
	{
		BeginWaitCursor();
		m_nWidth = (int)pDib->GetWidth();
		m_nHeight = (int)pDib->GetHeight(); 

		CDC memDC;
		memDC.CreateCompatibleDC(pDC);
		CBitmap ddb;
		ddb.CreateCompatibleBitmap(pDC, m_nWidth, m_nHeight);
		CBitmap* pOldBitmap = memDC.SelectObject(&ddb);

		pDib->Draw(memDC.m_hDC, 0, 0, m_nWidth, m_nHeight, 
					0, 0, m_nWidth, m_nHeight, DIB_RGB_COLORS, SRCCOPY);
		
		if(m_nOperation == Restore)
			pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &memDC, 0, 0, SRCCOPY);
		else if((m_nOperation == Increase)|| (m_nOperation == Decrease))
			AdjustContrast(pDC, pDib, 0, 0, m_nWidth, m_nHeight);

		memDC.SelectObject(pOldBitmap);
		ddb.DeleteObject();
		EndWaitCursor();
	}

}

/////////////////////////////////////////////////////////////////////////////
// CContrastView diagnostics

#ifdef _DEBUG
void CContrastView::AssertValid() const
{
	CView::AssertValid();
}

void CContrastView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CContrastView message handlers

void CContrastView::AdjustContrast(CDC *pDC, CDib* pDib,  int x,  int y,  int nWidth,  int nHeight)
{
	ASSERT(pDib);
		//限制处理的区域----防止任意填写数据而使用访问无效
	if((x > (m_nWidth - 1)) || (y > (m_nHeight - 1))) return ;
	//实际处理的宽度和高度
	int w = min(nWidth, m_nWidth - x);
	int h = min(nHeight, m_nHeight - y);
	
	// 32位颜色数据
	DWORD dwSize = m_nWidth * m_nHeight * 4;

	//分配全局内存 32位源数据
	BYTE* pbyBits32 =  new BYTE[dwSize];
	if(pbyBits32 == NULL) return;
	memset(pbyBits32, 0, dwSize);

	//CDib类只提供了一个全部数据的函数
	pDib->GetDdbData32(pbyBits32);


	//对比度处理
	CContrastProcess* cp = new CContrastProcess();
	cp->SetContrastIncrement(m_nIncrement);

	//获取数据后, 其基本参考点变为(0, 0).
	cp->ChangeContrast(pbyBits32, x, y, w, h, m_nWidth, m_nHeight);

	CClientDC dc(this);
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	HBITMAP hBitmap = cp->CreateDdb(pDC->m_hDC, m_nWidth, m_nHeight, pbyBits32);
	HBITMAP hOldBitmap = (HBITMAP)memDC.SelectObject(hBitmap);
	pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &memDC, 0, 0, SRCCOPY);

	::DeleteObject(hBitmap);
	memDC.SelectObject(hOldBitmap);

	delete cp;								//No 21
	delete[] pbyBits32;
	ReleaseDC(&dc);
}

void CContrastView::OnIncreaseContrast() 
{
	// TODO: Add your command handler code here
	m_nOperation = Increase;
	m_nIncrement += 30;
	Invalidate();
}

void CContrastView::OnUpdateIncreaseContrast(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_nOperation == Increase)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CContrastView::OnDecreaseContrast() 
{
	// TODO: Add your command handler code here
	m_nOperation = Decrease;
	m_nIncrement -= 30;
	Invalidate();
	
}

void CContrastView::OnUpdateDecreaseContrast(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_nOperation == Decrease)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CContrastView::OnContrastRestore() 
{
	// TODO: Add your command handler code here
	m_nOperation = Restore;
	m_nIncrement = 0;
	Invalidate();
}

void CContrastView::OnUpdateContrastRestore(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_nOperation == Restore)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}


⌨️ 快捷键说明

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