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

📄 shiyudoc.cpp

📁 数字图像处理的灰度处理源代码.rar
💻 CPP
字号:
// shiyuDoc.cpp : implementation of the CShiyuDoc class
//

#include "stdafx.h"
#include "shiyu.h"

#include "shiyuDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CShiyuDoc

IMPLEMENT_DYNCREATE(CShiyuDoc, CDocument)

BEGIN_MESSAGE_MAP(CShiyuDoc, CDocument)
	//{{AFX_MSG_MAP(CShiyuDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShiyuDoc construction/destruction

CShiyuDoc::CShiyuDoc()
{
	// TODO: add one-time construction code here
    m_refColorBKG=0x00808080;
	m_hDIB=NULL;
	m_palDIB=NULL;
	m_sizeDoc=CSize(1,1);

}

CShiyuDoc::~CShiyuDoc()
{
	if(m_hDIB!=NULL)
		::GlobalFree((HGLOBAL)m_hDIB);

	if(m_palDIB!=NULL)
		delete m_palDIB;

}

BOOL CShiyuDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CShiyuDoc serialization

void CShiyuDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CShiyuDoc diagnostics

#ifdef _DEBUG
void CShiyuDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CShiyuDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShiyuDoc commands

BOOL CShiyuDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	CFile file;
	CFileException fe;

	if(!file.Open(lpszPathName,CFile::modeRead|CFile::shareDenyWrite,&fe))
	{
		ReportSaveLoadException(lpszPathName,&fe,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);

		return FALSE;
	}

	DeleteContents();

	BeginWaitCursor();

	TRY
	{
		m_hDIB=::ReadDIBFile(file);
	}
	CATCH(CFileException,eLoad)
	{
		file.Abort();

		EndWaitCursor();

		ReportSaveLoadException(lpszPathName,eLoad,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);

		m_hDIB=NULL;

		return FALSE;
	}
	END_CATCH

	InitDIBData();

	EndWaitCursor();

	if(m_hDIB==NULL)
	{
		CString strMsg;
		strMsg="读取图像时出错!可能是不支持该类型的图像文件!";

		MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);

		return FALSE;
	}

	SetPathName(lpszPathName);

	SetModifiedFlag(FALSE);

	
	return TRUE;
}

BOOL CShiyuDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized code here and/or call the base class
	CFile file;
	CFileException fe;

	if(!file.Open(lpszPathName,CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive,&fe))
	{
		ReportSaveLoadException(lpszPathName,&fe,TRUE,AFX_IDP_INVALID_FILENAME);

		return FALSE;
	}

	BOOL bSuccess=FALSE;

	TRY
	{
		BeginWaitCursor();

		bSuccess=::SaveDIB(m_hDIB,file);

		file.Close();
	}
	CATCH(CException,eSave)
	{
		file.Abort();

		EndWaitCursor();

		ReportSaveLoadException(lpszPathName,eSave,TRUE,AFX_IDP_FAILED_TO_SAVE_DOC);


		return FALSE;
	}
	END_CATCH

	EndWaitCursor();

	SetModifiedFlag(FALSE);

	if(!bSuccess)
	{
		CString strMsg;
		strMsg="无法保存BMP图像";

		MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);

	}

	
	return CDocument::OnSaveDocument(lpszPathName);
}

void CShiyuDoc::DeleteContents() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDocument::DeleteContents();
}

BOOL CShiyuDoc::CanCloseFrame(CFrameWnd* pFrame) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDocument::CanCloseFrame(pFrame);
}

void CShiyuDoc::InitDIBData()
{
if(m_palDIB!=NULL)
	{
		delete m_palDIB;

		m_palDIB=NULL;
	}

	if(m_hDIB==NULL)
		return;

	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="BMP图像太大!";

		MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);

		return;
	}

	m_sizeDoc=CSize((int)::DIBWidth(lpDIB),(int)::DIBHeight(lpDIB));

	::GlobalUnlock((HGLOBAL)m_hDIB);

	m_palDIB=new CPalette;

	if(m_palDIB==NULL)
	{
		::GlobalFree((HGLOBAL)m_hDIB);

		m_hDIB=NULL;

		return;
	}

	if(::CreateDIBPalette(m_hDIB,m_palDIB)==NULL)
	{
		delete m_palDIB;

		m_palDIB=NULL;

		return;
	}

}

void CShiyuDoc::ReplaceHDIB(HDIB hDIB)
{
	if(m_hDIB!=NULL)
		::GlobalFree((HGLOBAL)m_hDIB);

	m_hDIB=hDIB;


}

void CShiyuDoc::SetDib()
{		
	LPSTR lpdib = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);	
	
	if (::DIBWidth(lpdib)>INT_MAX||::DIBHeight(lpdib)>INT_MAX)// 判断图象是否过大
	{
		::GlobalUnlock((HGLOBAL) m_hDIB);				
		::GlobalFree((HGLOBAL) m_hDIB);	// 释放DIB对象			
		m_hDIB = NULL;// 设置DIB为空		
		AfxMessageBox("初始化失败"); 		
		return;
	}	
	m_sizeDoc = CSize((int)::DIBWidth(lpdib), (int)::DIBHeight(lpdib));// 设置文档大小	
	::GlobalUnlock((HGLOBAL) m_hDIB);	
	m_palDIB = new CPalette;// 创建新调色板		
	if (m_palDIB == NULL)// 判断是否创建成功
	{		
		::GlobalFree((HGLOBAL) m_hDIB);	// 失败		
		m_hDIB = NULL;// 设置DIB对象为空
		return;
	}	
	// 调用CreateDIBPalette来创建调色板
/*	if (m_dib.ConstructPalette(m_hDIB, m_palDIB) == NULL)
	{				
		delete m_palDIB;// 删除				
		m_palDIB = NULL;// 设置为空	
		return;// 返回空
	}*/
		if(::CreateDIBPalette(m_hDIB,m_palDIB)==NULL)
	{
		delete m_palDIB;

		m_palDIB=NULL;

		return;
	}
}

⌨️ 快捷键说明

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