?? dipdoc.cpp
字號:
// DipDoc.cpp : implementation of the CDipDoc class
//
#include "stdafx.h"
#include "Dip.h"
#include "DipDoc.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "DipView.h"
#include "PointPro.h"
#include "AreaPro.h"
#include "DlgLinerPara.h"
#include "DlgPointStre.h"
#include "DlgThreshold.h"
#include "DlgSmooth.h"
#include "EdgeDetDlg.h"
#include "LapDlg.h"
#include "DlgColor.h"
#include "DlgMedianFilter.h"
#include "DlgSeedFill.h"
#include "DlgProject.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern char szFilter[];
/////////////////////////////////////////////////////////////////////////////
// CDipDoc
IMPLEMENT_DYNCREATE(CDipDoc, CDocument)
BEGIN_MESSAGE_MAP(CDipDoc, CDocument)
//{{AFX_MSG_MAP(CDipDoc)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
ON_COMMAND(ID_POINT_EQUA, OnPointEqua)
ON_COMMAND(ID_POINT_LINER, OnPointLiner)
ON_COMMAND(ID_POINT_STRE, OnPointStre)
ON_COMMAND(ID_POINT_CONTAUTO, OnPointContauto)
ON_COMMAND(ID_POINT_THRE, OnPointThre)
ON_COMMAND(ID_ENHA_SMOOTH, OnEnhaSmooth)
ON_COMMAND(ID_POINT_PSEUDOCOLOR, OnPointPseudocolor)
ON_COMMAND(ID_ENHA_GRADSHARP, OnEnhaGradsharp)
ON_COMMAND(ID_ENHA_TEMPSHARP, OnEnhaTempsharp)
ON_COMMAND(ID_ENHA_LAPSHARP, OnEnhaLapsharp)
ON_COMMAND(ID_POINT_INVERT, OnPointInvert)
ON_COMMAND(ID_CHANNEL_SPLIT, OnChannelSplit)
ON_UPDATE_COMMAND_UI(ID_CHANNEL_SPLIT, OnUpdateChannelSplit)
ON_COMMAND(ID_CHANNEL_BLUE, OnChannelBlue)
ON_UPDATE_COMMAND_UI(ID_CHANNEL_BLUE, OnUpdateChannelBlue)
ON_COMMAND(ID_CHANNEL_GREEN, OnChannelGreen)
ON_COMMAND(ID_CHANNEL_MIXED, OnChannelMixed)
ON_COMMAND(ID_CHANNEL_RED, OnChannelRed)
ON_UPDATE_COMMAND_UI(ID_CHANNEL_GREEN, OnUpdateChannelGreen)
ON_UPDATE_COMMAND_UI(ID_CHANNEL_MIXED, OnUpdateChannelMixed)
ON_UPDATE_COMMAND_UI(ID_CHANNEL_RED, OnUpdateChannelRed)
ON_UPDATE_COMMAND_UI(ID_POINT_THRE, OnUpdatePointThre)
ON_UPDATE_COMMAND_UI(ID_POINT_PSEUDOCOLOR, OnUpdatePointPseudocolor)
ON_COMMAND(ID_ENHA_MidianF, OnEnhaMidianF)
ON_COMMAND(ID_EDGE_TRACE, OnEdgeTrace)
ON_UPDATE_COMMAND_UI(ID_EDGE_TRACE, OnUpdateEdgeTrace)
ON_COMMAND(ID_EDGE_CONTOUR, OnEdgeContour)
ON_COMMAND(ID_EDGE_FILL, OnEdgeFill)
ON_UPDATE_COMMAND_UI(ID_EDGE_CONTOUR, OnUpdateEdgeContour)
ON_COMMAND(ID_DETECT_MINUS, OnDetectMinus)
ON_UPDATE_COMMAND_UI(ID_DETECT_MINUS, OnUpdateDetectMinus)
ON_COMMAND(ID_DETECT_TEMPLATE, OnDetectTemplate)
ON_UPDATE_COMMAND_UI(ID_DETECT_TEMPLATE, OnUpdateDetectTemplate)
ON_COMMAND(ID_DETECT_PROJECT, OnDetectProject)
ON_UPDATE_COMMAND_UI(ID_DETECT_PROJECT, OnUpdateDetectProject)
ON_UPDATE_COMMAND_UI(ID_EDGE_FILL, OnUpdateEdgeFill)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDipDoc construction/destruction
CDipDoc::CDipDoc()
{
// TODO: add one-time construction code here
m_pDibObject = NULL;
m_bImageLoaded = FALSE;
m_nColorIndex = 0;
m_bBinary = FALSE;
m_lHeight = 0;
m_lWidth = 0;
}
CDipDoc::~CDipDoc()
{
if(m_pDibObject != NULL)
{
delete m_pDibObject;
m_pDibObject = NULL;
}
}
BOOL CDipDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDipDoc serialization
void CDipDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CDipDoc diagnostics
#ifdef _DEBUG
void CDipDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CDipDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDipDoc commands
void CDipDoc::OnFileNew()
{
// TODO: Add your command handler code here
}
void CDipDoc::OnFileOpen()
{
// TODO: Add your command handler code here
static int nIndex = 1;
CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
FileDlg.m_ofn.nFilterIndex = (DWORD) nIndex;
if( FileDlg.DoModal() == IDOK )
{
CString strPathName = FileDlg.GetPathName();
AfxGetApp()->OpenDocumentFile( strPathName );
nIndex = (int) FileDlg.m_ofn.nFilterIndex;
if( !LoadImageToDocument() )
{
AfxMessageBox("無法載入圖像文件!");
return;
}
}
}
BOOL CDipDoc::LoadImageToDocument()
{
CString strPathName = GetPathName();
//設置等待光標
BeginWaitCursor();
m_pDibObject = new CDibObject( strPathName.GetBuffer(3) );
//取消等待光標
EndWaitCursor();
//讀入圖像文件失敗
if( m_pDibObject == NULL )
{
AfxMessageBox("無法創建圖像類對象!");
//返回FALSE
return(FALSE);
}
//獲取主框架窗口指針
CMainFrame *pFrame = (CMainFrame *) AfxGetMainWnd();
//獲取子框架窗口指針
CChildFrame *pChild = (CChildFrame *) pFrame->MDIGetActive();
pChild->m_nWidth = m_pDibObject->GetWidth();
pChild->m_nHeight = m_pDibObject->GetHeight();
pChild->SetWindowPos( NULL, 0, 0, pChild->m_nWidth + 12,
pChild->m_nHeight + 38, SWP_NOZORDER | SWP_NOMOVE );
//讀入圖像文件成功,設置相應變量
m_bImageLoaded = TRUE;
//返回TRUE
return(TRUE);
}
void CDipDoc::OnFileSaveAs()
{
// TODO: Add your command handler code here
static int nIndex = 1;
CFileDialog DialogSaveAs( FALSE, NULL, m_pDibObject->GetImageName(),
OFN_HIDEREADONLY, szFilter );
DialogSaveAs.m_ofn.nFilterIndex = (DWORD) nIndex;
if( DialogSaveAs.DoModal() == IDOK )
{
CMainFrame *pMainFrame = ( CMainFrame * )AfxGetMainWnd();
CChildFrame *pChildFrame = ( CChildFrame * )pMainFrame->MDIGetActive();
CDipView *pDipView = ( CDipView * )pChildFrame->GetActiveView();
nIndex = (int) DialogSaveAs.m_ofn.nFilterIndex;
if( nIndex == 5 )
{
if( m_pDibObject->GetNumBits() != 24 )
{
AfxMessageBox("必須是24位真彩色圖像才能存為JPEG格式!");
return;
}
}
if( m_pDibObject != NULL )
{
CString strPathName = DialogSaveAs.GetPathName();
int nFindIndex = strPathName.Find(".");
if( nFindIndex != -1)
strPathName = strPathName.Left( nFindIndex );
strPathName += CDibObject::szExtensions[ nIndex - 1 ];
//m_pDibObject->ProcessImageHeader();
//m_pDibObject->ProcessPalette();
m_pDibObject->Save( strPathName );
CString strFileName = DialogSaveAs.GetFileName();
nFindIndex = strFileName.Find(".");
if ( nFindIndex != -1 )
strFileName = strFileName.Left( nFindIndex );
strFileName += CDibObject::szExtensions[ nIndex - 1 ];
pChildFrame->SetWindowText( strFileName );
SetPathName( strPathName );
if( nIndex == 5 )
{
m_pDibObject->Load( strPathName );
pDipView->InvalidateRect( NULL, FALSE );
pDipView->UpdateWindow();
}
}
}
}
void CDipDoc::OnPointEqua()
{
//創建點處理CPointPro類對象
CPointPro PointOperation( m_pDibObject );
// 更改光標形狀
BeginWaitCursor();
// 調用LinerTrans()函數進行線性變換
PointOperation.InteEqualize( m_pDibObject );
// 設置臟標記
//SetModifiedFlag(TRUE);
// 更新視圖
UpdateAllViews(NULL);
// 恢復光標
EndWaitCursor();
}
void CDipDoc::OnPointLiner()
{
// 創建對話框
CDlgLinerPara dlgPara;
// 線性變換的斜率
float fa;
// 線性變換的截距
float fb;
// 初始化變量值
dlgPara.m_fA = 1.0;
dlgPara.m_fB = 0.0;
// 顯示對話框,提示用戶設定平移量
if (dlgPara.DoModal() != IDOK)
{
// 返回
return;
}
// 獲取用戶設定的平移量
fa = dlgPara.m_fA;
fb = dlgPara.m_fB;
// 刪除對話框
delete dlgPara;
//創建點處理CPointPro類對象
CPointPro PointOperation( m_pDibObject );
// 更改光標形狀
BeginWaitCursor();
// 調用LinerTrans()函數進行線性變換
PointOperation.LinerTran(fa, fb);
// 設置臟標記
//SetModifiedFlag(TRUE);
// 更新視圖
UpdateAllViews(NULL);
// 恢復光標
EndWaitCursor();
}
void CDipDoc::OnPointStre()
{
// 創建對話框
CDlgPointStre dlgPara;
// 點1坐標
BYTE bX1;
BYTE bY1;
// 點2坐標
BYTE bX2;
BYTE bY2;
// 初始化變量值
dlgPara.m_bX1 = 50;
dlgPara.m_bY1 = 30;
dlgPara.m_bX2 = 200;
dlgPara.m_bY2 = 220;
// 顯示對話框,提示用戶設定拉伸位置
if (dlgPara.DoModal() != IDOK)
{
// 返回
return;
}
// 獲取用戶的設定
bX1 = dlgPara.m_bX1;
bY1 = dlgPara.m_bY1;
bX2 = dlgPara.m_bX2;
bY2 = dlgPara.m_bY2;
// 刪除對話框
delete dlgPara;
//創建點處理CPointPro類對象
CPointPro PointOperation( m_pDibObject );
// 更改光標形狀
BeginWaitCursor();
// 調用LinerTrans()函數進行線性變換
PointOperation.GrayStretch(bX1, bY1, bX2, bY2);
// 設置臟標記
//SetModifiedFlag(TRUE);
// 更新視圖
UpdateAllViews(NULL);
// 恢復光標
EndWaitCursor();
}
void CDipDoc::OnPointContauto()
{
//判斷當前是否有圖像對象
if( m_pDibObject == NULL ) return;
//在點處理CPointPro類中創建用來繪制直方圖的數據
CPointPro PointOperation( m_pDibObject );
int *pHistogram = PointOperation.GetHistogram();
int i = 0;
int nGrayMax = 0, nGrayMin = 0;
//查找最小灰度值
while(nGrayMin == 0 && i <= 255)
{
if( pHistogram[i] >= 100 )
{
nGrayMin = i;
}
i++;
}
//查找最大灰度值
i = 255;
while(nGrayMax == 0 && i >= 0)
{
if( pHistogram[i] >= 100 )
{
nGrayMax = i;
}
i--;
}
delete [] pHistogram;
// 更改光標形狀
BeginWaitCursor();
// 調用LinerTrans()函數進行線性變換
PointOperation.GrayStretch(nGrayMin,0,nGrayMax,255);
// 設置臟標記
//SetModifiedFlag(TRUE);
// 更新視圖
UpdateAllViews(NULL);
// 恢復光標
EndWaitCursor();
}
void CDipDoc::OnPointThre()
{
//判斷當前是否有圖像對象
if( m_pDibObject == NULL ) return;
//在點處理CPointPro類中創建用來繪制直方圖的數據
CPointPro PointOperation( m_pDibObject );
int *pHistogram = PointOperation.GetHistogram();
//生成一個對話框CHistDlg類的實例
CDlgThreshold ThreshDlg;
//將繪制直方圖的數據傳遞給CHistDlg對話框類的公有成員變量m_pnHistogram
if( pHistogram != NULL )
{
ThreshDlg.m_pnHistogram = pHistogram;
//設置當前像素值為0的像素數
//HistDlg.m_nCurrentPiexsNum = pHistogram[0];
}
// 更改光標形狀
BeginWaitCursor();
ThreshDlg.m_nThresholdAutoAdjust = PointOperation.FindThresh_Discriminant();
ThreshDlg.m_nThresholdDiscriminant = PointOperation.FindThresh_Auto();
ThreshDlg.m_nCurrentThreshold = ThreshDlg.m_nThresholdAutoAdjust;
//顯示對話框
if ( ThreshDlg.DoModal() != IDOK)
return;
delete [] pHistogram;
// 調用Threshold()函數進行閾值化操作
PointOperation.Threshold(ThreshDlg.m_nCurrentThreshold,
ThreshDlg.m_nThresholdType, TRUE);
m_bBinary = TRUE;
// 設置臟標記
//SetModifiedFlag(TRUE);
// 更新視圖
UpdateAllViews(NULL);
// 恢復光標
EndWaitCursor();
}
void CDipDoc::OnPointInvert()
{
//創建點處理CPointPro類對象
CPointPro PointOperation( m_pDibObject );
BeginWaitCursor();
//反色:fa=-1.0,fb=255
PointOperation.LinerTran(-1.0, 255);
EndWaitCursor();
//SetModifiedFlag(TRUE);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -