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

📄 imagestudiodoc.cpp

📁 关于数字图像处理的例子,包括傅立叶变换,直方图变换等
💻 CPP
字号:
// ImageStudioDoc.cpp : implementation of the CImageStudioDoc class
//

#include "stdafx.h"
#include "ImageStudio.h"

#include "ImageStudioDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImageStudioDoc

IMPLEMENT_DYNCREATE(CImageStudioDoc, CDocument)

BEGIN_MESSAGE_MAP(CImageStudioDoc, CDocument)
	//{{AFX_MSG_MAP(CImageStudioDoc)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CImageStudioDoc construction/destruction

CImageStudioDoc::CImageStudioDoc()
{
	// TODO: add one-time construction code here
	PixelsImage= NULL;
	m_refColorBKG = 0x00808080;	
	
	// 初始化变量
	m_hDIB = NULL;
	m_palDIB = NULL;
	m_sizeDoc = CSize(1,1);
}

CImageStudioDoc::~CImageStudioDoc()
{
	if(PixelsImage != NULL)
		delete []PixelsImage;
	if (m_hDIB != NULL)
	{
		// 清除DIB对象
		::GlobalFree((HGLOBAL) m_hDIB);
	}
	
	// 判断调色板是否存在
	if (m_palDIB != NULL)
	{
		// 清除调色板
		delete m_palDIB;
	}
}

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

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

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CImageStudioDoc serialization

/*void CImageStudioDoc::Serialize(CArchive& ar)
{
	int i, j;
	int Offset;
	int residue;
	BYTE  ImageHead[60], mPixel;
	BYTE* pSourceImage;
	BYTE* pPixelsImage;
    
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
		for(i=0; i < 53; i++)
			ar >> ImageHead[i];
		if(ImageHead[0] == 'B' && ImageHead[1] == 'M'){
		   Offset= ImageHead[7];
		   Offset= Offset*256+ImageHead[8];
		   Offset= Offset*256+ImageHead[9];
		   Offset= Offset*256+ImageHead[10];

           Image.Width= ImageHead[21];
           Image.Width= Image.Width*256+ImageHead[20];
		   Image.Width= Image.Width*256+ImageHead[19];
		   Image.Width= Image.Width*256+ImageHead[18];
		   Image.Height= ImageHead[25];
		   Image.Height= Image.Height*256+ImageHead[24];
		   Image.Height= Image.Height*256+ImageHead[23];
		   Image.Height= Image.Height*256+ImageHead[22];

		   for(i= 54; i < Offset-1; i++)
			   ar >> ImageHead[0];
           
		   Image.PixelBits= ImageHead[28];
		   Image.Source= new BYTE[Image.Width*Image.Height*3]; 
		   if(Image.Source == NULL)
			   return;
		   pSourceImage= Image.Source;
		   residue= Image.Width%4;
		   if( Image.PixelBits == 8){
			   for(i= 0; i < Image.Width; i++){
				   for(j= 0; j < Image.Height; j++){
					   ar >> mPixel;
					   *pSourceImage++= mPixel;
					   *pSourceImage++= mPixel;
					   *pSourceImage++= mPixel;
				   }
				   if(residue == 0)
					   continue;
				   else if(residue == 1){
					   ar >> mPixel;
				   }
				   else if(residue == 2){
					   ar >> mPixel;
					   ar >> mPixel;
				   }
				   else if(residue == 3){
					   ar >> mPixel;
					   ar >> mPixel;
					   ar >> mPixel;
				   }
			   }
		   }
		   else if(Image.PixelBits == 24){
			   for(i= 0; i < Image.Width; i++){
				   for(j= 0; j < Image.Height; j++){
					   ar >> *pSourceImage++;
					   ar >> *pSourceImage++;
					   ar >> *pSourceImage++;

				   }
				   if(residue == 0)
					   continue;
				   else if(residue == 1){
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
				   }
				   else if(residue == 2){
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
				   }
				   else if(residue == 3){
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
					   ar >> *pSourceImage;
				   }
			   }
		   }

           PixelsImage= new BYTE[Image.Width*Image.Height*3];

		   if(PixelsImage == NULL)
			   return;
		   if(Image.PixelBits != 8 && Image.PixelBits != 24)
			   return;
		   pPixelsImage= PixelsImage;
		   pSourceImage= Image.Source;
		   for(i= 0; i < Image.Width; i++){
			   for(j=0; j < Image.Height; j++){
				   *pPixelsImage++= *pSourceImage++;
				   *pPixelsImage++= *pSourceImage++;
				   *pPixelsImage++= *pSourceImage++;
			   }
		   }
		}
	}
	return;
}
*/
/////////////////////////////////////////////////////////////////////////////
// CImageStudioDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CImageStudioDoc commands

BOOL CImageStudioDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
		CFile file;
	CFileException fe;

	// 打开文件
	if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
	{
		// 失败
		ReportSaveLoadException(lpszPathName, &fe,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		
		// 返回FALSE
		return FALSE;
	}
	
	DeleteContents();
	
	// 更改光标形状
	BeginWaitCursor();
	
	// 尝试调用ReadDIBFile()读取图像
	TRY
	{
		m_hDIB = ::ReadDIBFile(file);
	}
	CATCH (CFileException, eLoad)
	{
		// 读取失败
		file.Abort();
		
		// 恢复光标形状
		EndWaitCursor();
		
		// 报告失败
		ReportSaveLoadException(lpszPathName, eLoad,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		
		// 设置DIB为空
		m_hDIB = NULL;
		
		// 返回FALSE
		return FALSE;
	}
	END_CATCH
	
	// 初始化DIB
	InitDIBData();
	
	// 恢复光标形状
	EndWaitCursor();
	
	// 判断读取文件是否成功
	if (m_hDIB == NULL)
	{
		// 失败,可能非BMP格式
		CString strMsg;
		strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
		
		// 提示出错
		MessageBox(NULL, strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
		
		// 返回FALSE
		return FALSE;
	}
	
	// 设置文件名称
	SetPathName(lpszPathName);
	
	// 初始化胀标记为FALSE
	SetModifiedFlag(FALSE);
	
	// 返回TRUE
	return TRUE;
}

void CImageStudioDoc::InitDIBData()
{
// 初始化DIB对象

	// 判断调色板是否为空
	if (m_palDIB != NULL)
	{
		// 删除调色板对象
		delete m_palDIB;

		// 重置调色板为空
		m_palDIB = NULL;
	}
	
	// 如果DIB对象为空,直接返回
	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);
		
		// 释放DIB对象
		::GlobalFree((HGLOBAL) m_hDIB);
		
		// 设置DIB为空
		m_hDIB = NULL;
		
		CString strMsg;
		strMsg = "BMP图像太大!";
		
		// 提示用户
		MessageBox(NULL, strMsg, "系统提示", 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);
		
		// 设置DIB对象为空
		m_hDIB = NULL;
		
		// 返回
		return;
	}
	
	// 调用CreateDIBPalette来创建调色板
	if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
	{
		// 返回空,可能该DIB对象没有调色板
		
		// 删除
		delete m_palDIB;
		
		// 设置为空
		m_palDIB = NULL;
		
		// 返回
		return;
	}
}

⌨️ 快捷键说明

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