📄 dlgintensity.cpp
字号:
// DlgIntensity.cpp : implementation file
//
#include "stdafx.h"
#include "hwpre.h"
#include "DlgIntensity.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgIntensity dialog
CDlgIntensity::CDlgIntensity(CWnd* pParent /*=NULL*/)
: CDialog(CDlgIntensity::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgIntensity)
m_intThre = 128;
//}}AFX_DATA_INIT
m_pParent = pParent;
m_nID = CDlgIntensity::IDD;
m_grayHigh=255;
m_grayLow=0;
}
void CDlgIntensity::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgIntensity)
DDX_Text(pDX, IDC_EDIT_THRE, m_intThre);
DDV_MinMaxUInt(pDX, m_intThre, 0, 255);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgIntensity, CDialog)
//{{AFX_MSG_MAP(CDlgIntensity)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_EN_KILLFOCUS(IDC_EDIT_THRE, OnKillfocusEditThre)
ON_WM_PAINT()
ON_EN_CHANGE(IDC_EDIT_THRE, OnChangeEditThre)
ON_BN_CLICKED(IDC_BTN_EQUA, OnBtnEqua)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgIntensity message handlers
BOOL CDlgIntensity::Create()
{
return CDialog::Create(m_nID, m_pParent);
}
BOOL CDlgIntensity::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_intThre = 128;
UpdateData(FALSE);
CSpinButtonCtrl * spin1 = (CSpinButtonCtrl*) GetDlgItem(IDC_SPIN_THRE);
spin1->SetRange(0,255);
CWnd * pWnd = GetDlgItem(IDC_STATIC_COORD);
pWnd->GetClientRect(m_MouseRect);
pWnd->ClientToScreen(&m_MouseRect);
CRect rect;
GetClientRect(rect);
ClientToScreen(&rect);
m_MouseRect.top -= rect.top;
m_MouseRect.left -= rect.left;
m_MouseRect.top += 25;
m_MouseRect.left += 10;
m_MouseRect.bottom = m_MouseRect.top + 255;
m_MouseRect.right = m_MouseRect.left + 256;
m_iIsDraging = 0;
//计算原图灰度直方图
BeginWaitCursor();
int i,j;
LPSTR lpdib,lpBits;
LPBYTE lpSrc;
lpdib = (LPSTR) ::GlobalLock((HGLOBAL)hDibSource);
lpBits = ::FindDIBBits(lpdib);
m_lWidth = ::DIBWidth(lpdib);
m_lHeight =::DIBHeight(lpdib);
m_lLineBytes = WIDTHBYTES( m_lWidth*8 );
for (i=0;i<256;i++) {
m_lCount[i]=0;
}
for (i=0;i<m_lHeight;i++) {
for (j=0;j<m_lWidth;j++) {
lpSrc =(LPBYTE) lpBits + i * m_lLineBytes +j;
m_lCount[*lpSrc]++;
}
}
::GlobalUnlock((HGLOBAL)hDibSource);
EndWaitCursor();
OnOK();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgIntensity::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_MouseRect.PtInRect(point)) {
if (0!=m_iIsDraging) {
switch(m_iIsDraging) {
case 1: //拖动阈值线
m_intThre = (BYTE) (point.x - m_MouseRect.left);
UpdateData(FALSE);
InvalidateRect(m_MouseRect,TRUE);
OnOK();
break;
case 2: //拖动下线
// 判断是否下限<上限
if (point.x - m_MouseRect.left < m_grayHigh)
{
// 更改下限
m_grayLow = point.x - m_MouseRect.left;
}
else
{
// 下限拖过上限,设置为上限-1
m_grayLow = m_grayHigh - 1;
// 重设鼠标位置
point.x = m_MouseRect.left + m_grayHigh - 1;
}
break;
case 3: //拖动上线
// 判断是否上限>下限
if (point.x - m_MouseRect.left > m_grayLow)
{
// 更改下限
m_grayHigh = point.x - m_MouseRect.left;
}
else
{
// 下限拖过上限,设置为下限+1
m_grayHigh = m_grayLow + 1;
// 重设鼠标位置
point.x = m_MouseRect.left + m_grayLow + 1;
}
break;
default:
;
}
::SetCursor(::LoadCursor(NULL,IDC_SIZEWE));
UpdateData(FALSE);
InvalidateRect(m_MouseRect,TRUE);
}else if (((m_MouseRect.left+m_intThre)==point.x) ||
(point.x == (m_MouseRect.left + m_grayLow)) ||
(point.x == (m_MouseRect.left + m_grayHigh))) {
::SetCursor(::LoadCursor(NULL,IDC_SIZEWE));
}
}
CDialog::OnMouseMove(nFlags, point);
}
void CDlgIntensity::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_MouseRect.PtInRect(point))
{
if (point.x == (m_MouseRect.left + m_intThre))
{
// 设置拖动状态
m_iIsDraging = 1;
// 更改光标
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}else if ((point.x == (m_MouseRect.left + m_grayLow)) ) {
m_iIsDraging = 2;
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}else if ((point.x == (m_MouseRect.left + m_grayHigh))) {
m_iIsDraging = 3;
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
} CDialog::OnLButtonDown(nFlags, point);
}
void CDlgIntensity::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (0 != m_iIsDraging)
{
// 重置拖动状态
m_iIsDraging = 0;
}
CDialog::OnLButtonUp(nFlags, point);
}
void CDlgIntensity::OnKillfocusEditThre()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
OnOK();
}
void CDlgIntensity::OnOK()
{
// TODO: Add extra validation here
BeginWaitCursor();
UpdateData(TRUE);
int i,j;
LPSTR lpdibS,lpdibD,lpBitsS,lpBitsD;
LPBYTE lpSrcS,lpSrcD;
lpdibS = (LPSTR) ::GlobalLock((HGLOBAL)hDibSource);
lpdibD = (LPSTR) ::GlobalLock((HGLOBAL)hDibDest);
lpBitsS = ::FindDIBBits(lpdibS);
lpBitsD = ::FindDIBBits(lpdibD);
m_lWidth = ::DIBWidth(lpdibS);
m_lHeight =::DIBHeight(lpdibS);
m_lLineBytes = WIDTHBYTES( m_lWidth*8 );
for (i=0;i<m_lHeight;i++) {
for (j=0;j<m_lWidth;j++) {
lpSrcS =(LPBYTE) lpBitsS + i * m_lLineBytes +j;
lpSrcD =(LPBYTE) lpBitsD + i * m_lLineBytes +j;
if ((*lpSrcS)<m_intThre) {
*lpSrcD = 0;
}else{
*lpSrcD = 255;
}
}
}
::GlobalUnlock((HGLOBAL)hDibSource);
::GlobalUnlock((HGLOBAL)hDibDest);
EndWaitCursor();
((CHwpreView*)m_pParent )->GetDocument()->SetModifiedFlag(TRUE);
((CHwpreView*)m_pParent)->GetDocument()->UpdateAllViews(NULL);
//UpdateAllViews::OnOK();
}
void CDlgIntensity::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
UpdateData();
CWnd * pWnd = GetDlgItem(IDC_STATIC_COORD);
CDC * pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,330,300);
CPen * pPenRed = new CPen;
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
CPen* pPenBlue = new CPen;
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255));
CPen* pPenGreen = new CPen;
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));
CGdiObject* pOldPen = pDC->SelectObject(pPenRed);
// 绘制坐标轴
pDC->MoveTo(10,10);
pDC->LineTo(10,280);
pDC->LineTo(320,280);
CString str;
LONG i, lMaxCount=0;
// 写X轴刻度值
str.Format("0");
pDC->TextOut(10, 283, str);
str.Format("50");
pDC->TextOut(60, 283, str);
str.Format("100");
pDC->TextOut(110, 283, str);
str.Format("150");
pDC->TextOut(160, 283, str);
str.Format("200");
pDC->TextOut(210, 283, str);
str.Format("255");
pDC->TextOut(265, 283, str);
// 绘制X轴刻度
for (i = 0; i < 256; i += 5)
{
if ((i & 1) == 0)
{
// 10的倍数
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 284);
}
else
{
// 10的倍数
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 282);
}
}
// 绘制X轴箭头
pDC->MoveTo(315,275);
pDC->LineTo(320,280);
pDC->LineTo(315,285);
// 绘制X轴箭头
pDC->MoveTo(10,10);
pDC->LineTo(5,15);
pDC->MoveTo(10,10);
pDC->LineTo(15,15);
// 计算最大计数值
for (i = m_grayLow; i <= m_grayHigh; i ++)
{
// 判断是否大于当前最大值
if (m_lCount[i] > lMaxCount)
{
// 更新最大值
lMaxCount = m_lCount[i];
}
}
// 输出最大计数值
pDC->MoveTo(10, 25);
pDC->LineTo(14, 25);
str.Format("%d", lMaxCount);
pDC->TextOut(11, 26, str);
// 更改成绿色画笔
pDC->SelectObject(pPenGreen);
// 绘制窗口上下限
pDC->MoveTo(m_grayLow + 10, 25);
pDC->LineTo(m_grayLow + 10, 280);
pDC->MoveTo(m_grayHigh + 10, 25);
pDC->LineTo(m_grayHigh + 10, 280); // 更改成蓝色画笔
pDC->SelectObject(pPenBlue);
// 判断是否有计数
if (lMaxCount > 0)
{
// 绘制直方图
for (i = m_grayLow; i <= m_grayHigh; i ++)
{
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 281 - (int) (m_lCount[i] * 256 / lMaxCount));
}
}
//绘制阈值线
pDC->SelectObject(pPenGreen);
pDC->MoveTo(10+ m_intThre,25);
pDC->LineTo(10+ m_intThre,285);
// 恢复以前的画笔
pDC->SelectObject(pOldPen);
// 删除新的画笔
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
}
void CDlgIntensity::PostNcDestroy()
{
// TODO: Add your specialized code here and/or call the base class
delete this;
//CDialog::PostNcDestroy();
}
void CDlgIntensity::OnCancel()
{
// TODO: Add extra cleanup here
//((CMainDlg*)m_pParent)->BoxDone();
((CHwpreView*)m_pParent)->m_pdlgParaIntensity= NULL;
GlobalFree((HGLOBAL) hDibSource);
DestroyWindow();
// CDialog::OnCancel();
}
void CDlgIntensity::OnChangeEditThre()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(m_MouseRect,TRUE);
OnOK();
}
void CDlgIntensity::OnBtnEqua()
{
// TODO: Add your control notification handler code here
BeginWaitCursor();
UpdateData(TRUE);
int i,j;
LPSTR lpdibS,lpdibD,lpBitsS,lpBitsD;
LPBYTE lpSrcS,lpSrcD;
lpdibS = (LPSTR) ::GlobalLock((HGLOBAL)hDibSource);
lpdibD = (LPSTR) ::GlobalLock((HGLOBAL)hDibDest);
lpBitsS = ::FindDIBBits(lpdibS);
lpBitsD = ::FindDIBBits(lpdibD);
m_lWidth = ::DIBWidth(lpdibS);
m_lHeight =::DIBHeight(lpdibS);
m_lLineBytes = WIDTHBYTES( m_lWidth*8 );
// 灰度映射表
BYTE bMap[256];
// 临时变量
LONG lTemp;
for (i=0 ;i<256;i++) {
lTemp = 0;
for (j=0; j<=i;j++) {
lTemp += m_lCount[j];
}
bMap[i]=(BYTE) ((double)lTemp * 255/m_lWidth/m_lHeight);
(BYTE) (lTemp * 255/m_lWidth/m_lHeight);
}
for (i=0;i<m_lHeight;i++) {
for (j=0;j<m_lWidth;j++) {
lpSrcS =(LPBYTE) lpBitsS + i * m_lLineBytes +j;
lpSrcD =(LPBYTE) lpBitsD + i * m_lLineBytes +j;
*lpSrcD = bMap[*lpSrcS];
}
}
::GlobalUnlock((HGLOBAL)hDibSource);
::GlobalUnlock((HGLOBAL)hDibDest);
EndWaitCursor();
((CHwpreView*)m_pParent)->GetDocument()->SetModifiedFlag(TRUE);
((CHwpreView*)m_pParent)->GetDocument()->UpdateAllViews(NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -