www.pudn.com > VC_图像小波分解.rar > ImgDemoDoc.cpp


// ImgDemoDoc.cpp : implementation of the CImgDemoDoc class 
// 
 
#include "stdafx.h" 
#include "ImgDemo.h" 
 
#include "ImgDemoDoc.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgDemoDoc 
 
IMPLEMENT_DYNCREATE(CImgDemoDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CImgDemoDoc, CDocument) 
	//{{AFX_MSG_MAP(CImgDemoDoc) 
	ON_COMMAND(ID_TOOLS_FILL, OnToolsFill) 
	ON_COMMAND(ID_TOOLS_ConvertGray, OnTOOLSConvertGray) 
	ON_COMMAND(ID_ToolsZoomIn, OnToolsZoomIn) 
	ON_COMMAND(ID_TOOLS_Sample, OnTOOLSSample) 
	ON_COMMAND(ID_TOOLS_Match, OnTOOLSMatch) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgDemoDoc construction/destruction 
 
CImgDemoDoc::CImgDemoDoc() 
{ 
	// TODO: add one-time construction code here 
	m_nDocSize.cx = 800; 
	m_nDocSize.cy = 800; 
 
	m_palDIB = NULL; 
	m_hDIB = NULL; 
} 
 
CImgDemoDoc::~CImgDemoDoc() 
{ 
	if (m_hDIB != NULL) 
	{ 
		::GlobalFree((HGLOBAL) m_hDIB); 
		m_hDIB=NULL; 
	}	 
	if (m_palDIB != NULL) 
	{ 
		delete m_palDIB; 
		m_palDIB=NULL; 
	} 
 
} 
 
BOOL CImgDemoDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
	m_hDIB = NewDIB(m_nDocSize.cx, m_nDocSize.cy,24); 
	InitDIBData(); 
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgDemoDoc serialization 
 
void CImgDemoDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgDemoDoc diagnostics 
 
#ifdef _DEBUG 
void CImgDemoDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CImgDemoDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CImgDemoDoc commands 
BOOL CImgDemoDoc::InitDIBData() 
{ 
	if (m_palDIB != NULL) 
	{ 
		delete m_palDIB; 
		m_palDIB = NULL; 
	} 
	if (m_hDIB == NULL) 
	{ 
		return FALSE; 
	} 
	m_palDIB = new CPalette; 
	if (m_palDIB == NULL) 
	{ 
		// we must be really low on memory 
		::GlobalFree((HGLOBAL) m_hDIB); 
		m_hDIB = NULL; 
		return FALSE; 
	} 
 
	if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL) 
	{ 
		// DIB may not have a palette 
		delete m_palDIB; 
		m_palDIB = NULL; 
	} 
 
	return TRUE; 
} 
 
BOOL CImgDemoDoc::ReadImgFile(CString sName) 
{ 
	if (m_hDIB != NULL) 
	{ 
		::GlobalFree((HGLOBAL) m_hDIB); 
		m_hDIB=NULL; 
	}	 
	if (m_palDIB != NULL) 
	{ 
		delete m_palDIB; 
		m_palDIB=NULL; 
	} 
 
	// replace calls to Serialize with ReadDIBFile function 
	CFile nFile; 
	if(!nFile.Open(sName,CFile::modeRead)) 
		return false; 
	m_hDIB = ReadDIBFile(nFile); 
	nFile.Close(); 
	InitDIBData(); 
 
	if (m_hDIB == NULL) 
		return FALSE; 
	return TRUE; 
} 
 
BOOL CImgDemoDoc::OnOpenDocument(LPCTSTR lpszPathName)  
{ 
	if (!CDocument::OnOpenDocument(lpszPathName)) 
		return FALSE; 
	 
	// TODO: Add your specialized creation code here 
	BOOL b = ReadImgFile(lpszPathName); 
	if(b ) 
	{ 
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); 
		m_nDocSize.cx = (int) ::DIBWidth(lpDIB);         // Size of DIB - x 
		m_nDocSize.cy = (int) ::DIBHeight(lpDIB);        // Size of DIB - y 
		::GlobalUnlock((HGLOBAL) m_hDIB); 
	} 
	POSITION pos; 
	pos = GetFirstViewPosition(); 
	while(pos != NULL) 
	{ 
		CScrollView * pView = (CScrollView *)GetNextView(pos); 
		if(pView != NULL) 
		{ 
			pView->SetScrollSizes(MM_TEXT, m_nDocSize); 
 
		} 
	} 
 
	return b; 
} 
 
BOOL CImgDemoDoc::OnSaveDocument(LPCTSTR lpszPathName)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	CFile nFile; 
	if(m_hDIB == NULL) 
		return  false; 
	nFile.Open(lpszPathName,CFile::modeWrite | CFile::modeCreate); 
	SaveDIB(m_hDIB, nFile); 
	nFile.Close(); 
	return true; 
//	return CDocument::OnSaveDocument(lpszPathName); 
} 
 
void CImgDemoDoc::OnToolsFill()  
{ 
	// TODO: Add your command handler code here 
	BYTE *lpDIB=(BYTE*)::GlobalLock((HGLOBAL) m_hDIB); 
	BYTE *pScrBuff =(BYTE*)(lpDIB+sizeof(BITMAPINFOHEADER)); 
	int w, h, dw, i,j; 
	w = (int) ::DIBWidth((char *)lpDIB);         // Size of DIB - x 
	h = (int) ::DIBHeight((char *)lpDIB);        // Size of DIB - y 
// 
	int numColors=(int) ::DIBNumColors((char *)lpDIB); 
    
	BYTE *pBuf = pScrBuff+numColors*4; 
    switch (numColors) 
	{ 
		case 2: 
		    dw = (w +7)/8; 
            break; 
		case 16: 
			dw = ((w +7)/8+3)/4*4; 
		    break; 
		case 256:  
			dw = (w +3)/4*4; 
		    break; 
        case 0: 
		default:   
			dw = (w*3 +3)/4*4; 
	}	 
 
	if(	numColors==0) 
	  for(i = 0; i< h;i++) 
			{ 
				for(j = 0; j< w; j++) 
				{ 
					*(pBuf + j*3) = 0xff; 
					*(pBuf + j*3+1) = 0x00; 
					*(pBuf + j*3+2) = 0x00; 
				} 
				pBuf += dw;  
			} 
	  else 
		  for(i = 0; i< h;i++) 
			{ 
				for(j = 0; j< w; j++) 
					*(pBuf + j) = 0x00; 
					 
				pBuf += dw;  
			} 
		 
	::GlobalUnlock((HGLOBAL) m_hDIB); 
	UpdateAllViews(NULL, 0, NULL); 
} 
/* 
void CImgDemoDoc::OnToolsFill()  
{ 
	// TODO: Add your command handler code here 
	BYTE *lpDIB=(BYTE*)::GlobalLock((HGLOBAL) m_hDIB); 
	BYTE *pScrBuff =(BYTE*)(lpDIB+sizeof(BITMAPINFOHEADER)); 
	int w, h, dw, i,j; 
	w = (int) ::DIBWidth((char *)lpDIB);         // Size of DIB - x 
	h = (int) ::DIBHeight((char *)lpDIB);        // Size of DIB - y 
	dw = (w*3 +3)/4*4; 
	 
	BYTE *pBuf = pScrBuff; 
 
	for(i = 0; i< h;i++) 
	{ 
		for(j = 0; j< w; j++) 
		{ 
			*(pBuf + j*3) = 0xff; 
			*(pBuf + j*3+1) = 0x00; 
			*(pBuf + j*3+2) = 0x00; 
		} 
		pBuf += dw;  
	} 
	::GlobalUnlock((HGLOBAL) m_hDIB); 
	UpdateAllViews(NULL, 0, NULL); 
} 
*/ 
 
void CImgDemoDoc::OnTOOLSConvertGray()  
{ 
	// TODO: Add your command handler code here 
	BYTE *lpDIB=(BYTE*)::GlobalLock((HGLOBAL) m_hDIB); 
	BYTE *pScrBuff =(BYTE*)(lpDIB+sizeof(BITMAPINFOHEADER)); 
	int numColors=(int) ::DIBNumColors((char *)lpDIB); 
    if (numColors!=0)  
	{ 
		::GlobalUnlock((HGLOBAL) m_hDIB); 
		return; 
	} 
 
	int w, h, dw, i,j,gdw; 
	w = (int) ::DIBWidth((char *)lpDIB);         // Size of DIB - x 
	h = (int) ::DIBHeight((char *)lpDIB);        // Size of DIB - y 
	dw = (w*3 +3)/4*4; 
	BYTE *srcBuf = pScrBuff; 
	 
    HDIB grayhDIB=NewDIB(w, h,8); 
 
	BYTE *glpDIB=(BYTE*)::GlobalLock((HGLOBAL) grayhDIB); 
	BYTE *pDestBuff =(BYTE*)(glpDIB+sizeof(BITMAPINFOHEADER)); 
	gdw = (w +3)/4*4; 
    BYTE *destBuf = pDestBuff+256*4; 
	for(i = 0; i< h;i++) 
			{ 
				for(j = 0; jnewcorrelat) 
			{ 
				newcorrelat=correlat; 
				i0=i; 
				j0=j; 
			} 
			TemplateBuff=pTemplateBuff; 
			ScrBuff=StoreBuff; 
			 
		} 
		ScrBuff+=dw; 
	} 
	 
	ScrBuff=pScrBuff; 
	for(i=0;i