📄 twothreshold.cpp
字号:
// TwoThreshold.cpp : implementation file
//
#include "stdafx.h"
#include "linjunjuan.h"
#include "TwoThreshold.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// TwoThreshold dialog
TwoThreshold::TwoThreshold(CWnd* pParent /*=NULL*/)
: CDialog(TwoThreshold::IDD, pParent)
{
gray=NULL;
flagpic=FALSE;
flagHis=NULL;
m_hDIB = NULL;
m_palDIB = NULL;
input.p=NULL;
output.p=NULL;
//{{AFX_DATA_INIT(TwoThreshold)
m_rightnum = 255;
m_leftnum = 0;
//}}AFX_DATA_INIT
}
void TwoThreshold::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(TwoThreshold)
DDX_Control(pDX, IDC_RIGHT, m_right);
DDX_Control(pDX, IDC_SLIDER2, m_left);
DDX_Text(pDX, IDC_EDIT2, m_rightnum);
DDX_Text(pDX, IDC_EDIT1, m_leftnum);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(TwoThreshold, CDialog)
//{{AFX_MSG_MAP(TwoThreshold)
ON_WM_PAINT()
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_RIGHT, OnCustomdrawRight)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// TwoThreshold message handlers
void TwoThreshold::OnPaint()
{
CPaintDC dc(this); // device context for painting
int max=0;
dc.MoveTo(45,174);
dc.LineTo(301,174);
dc.MoveTo(45,174);
dc.LineTo(45,24);
if (flagHis)
{
//=========归一化便于显示==============
for (int k=0;k<256;k++)
{
if (gray[k]>max)
max=gray[k];
}
for (k=0;k<256;k++)
gray[k]=gray[k]*150/max;
for (int i=0;i<256;i++)
{
dc.MoveTo(i+46,174);
dc.LineTo(i+46,174-gray[i]);
}
}
CWnd* pwnd =GetDlgItem(IDC_PRESEA);
CDC *pControlDC =pwnd->GetDC();
pwnd->GetClientRect(&rect);
point.x=(rect.right-rect.left)/2;
point.y=(rect.bottom-rect.top)/2;
pwnd->Invalidate();
pwnd->UpdateWindow();
if (flagpic!=FALSE)
{
if (left==0&&right==255)
{
if ((m_hDIB=ChangeToHDIB(input))==NULL)
{
AfxMessageBox("不能显示!");
return ;
}
}
else
{
if ((m_hDIB=ChangeToHDIB(output))==NULL)
{
AfxMessageBox("不能显示!");
return ;
}
}
InitDIBData();
rectdest.left=rectdib.left=rect.left;
rectdest.top=rectdib.top=rect.top;
rectdib.right=output.col;
rectdib.bottom=output.row;
rectdest.bottom=rect.bottom;
rectdest.right=rect.right;
PaintDIB(pControlDC->m_hDC,&rectdest,m_hDIB,&rectdib,m_palDIB);
}
pwnd->ReleaseDC(pControlDC);
// Do not call CDialog::OnPaint() for painting messages
}
BOOL TwoThreshold::OnInitDialog()
{
CDialog::OnInitDialog();
m_left.SetRange(0,255);
m_left.SetPos(0);
m_right.SetRange(0,255);
m_right.SetPos(255);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void TwoThreshold::OnCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
left=m_left.GetPos();
m_leftnum=left;
m_rightnum=right;
if (left>=right)
m_right.SetPos(left);
if (right!=255||left!=0)
{
for (int i=0;i<input.row;i++)
for (int j=0;j<input.col;j++)
{
if (input.p[i][j]>=left&&input.p[i][j]<=right)
output.p[i][j]=0;
else output.p[i][j]=255;
}
}
UpdateData(FALSE);
OnPaint();
}
void TwoThreshold::InitDIBData()
{
if (m_palDIB != NULL)
{
delete m_palDIB;
m_palDIB = NULL;
}
if (m_hDIB == NULL)
{
return;
}
// Set up document size
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
{
::GlobalUnlock((HGLOBAL) m_hDIB);
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
CString strMsg;
strMsg = "IDS_DIB_TOO_BIG";
// MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
return;
}
// m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL) m_hDIB);
// Create copy of palette
m_palDIB = new CPalette;
if (m_palDIB == NULL)
{
// we must be really low on memory
::GlobalFree((HGLOBAL) m_hDIB);
m_hDIB = NULL;
return;
}
if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
{
// DIB may not have a palette
delete m_palDIB;
m_palDIB = NULL;
return;
}
}
void TwoThreshold::OnCustomdrawRight(NMHDR* pNMHDR, LRESULT* pResult)
{
right=m_right.GetPos();
m_rightnum=right;
m_leftnum=left;
if(right<=left)
m_left.SetPos(right);
if (right!=255||left!=0)
{
for (int i=0;i<input.row;i++)
for (int j=0;j<input.col;j++)
{
if (input.p[i][j]>=left&&input.p[i][j]<=right)
output.p[i][j]=0;
else output.p[i][j]=255;
}
}
UpdateData(FALSE);
OnPaint();
*pResult = 0;
}
void TwoThreshold::OnCancel()
{
if (output.p!=NULL)
dspace_2d(output.p,output.row,output.col);
delete []gray;
if (m_hDIB != NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
}
if (m_palDIB != NULL)
{
delete m_palDIB;
}
CDialog::OnCancel();
}
void TwoThreshold::OnOK()
{
// TODO: Add extra validation here
delete []gray;
if (m_hDIB != NULL)
{
::GlobalFree((HGLOBAL) m_hDIB);
}
if (m_palDIB != NULL)
{
delete m_palDIB;
}
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -