www.pudn.com > xbwretrieval.rar > xbwDlg.cpp


// xbwDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "xbw.h" 
#include "xbwDlg.h" 
 
#include "DirDialog.h" 
#include "TokenEx.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CXbwDlg dialog 
 
CXbwDlg::CXbwDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CXbwDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CXbwDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CXbwDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CXbwDlg) 
	DDX_Control(pDX, IDC_LIST_FOUNDED, m_ListFound); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CXbwDlg, CDialog) 
	//{{AFX_MSG_MAP(CXbwDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad) 
	ON_LBN_SELCHANGE(IDC_LIST_FOUNDED, OnSelchangeListFounded) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CXbwDlg message handlers 
 
BOOL CXbwDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CXbwDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CXbwDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
		RefreshDisplay();//更新结果窗口显示 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CXbwDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CXbwDlg::OnButtonLoad()  
{ 
	// TODO: Add your control notification handler code here 
	char szFilter[] = "Bmp File (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0\0";//文件滤波器 
	CFileDialog dlg(TRUE); 
	CString strTemp; 
	CString strTemp1; 
	CString strNamaFile; 
	ULONG ulJumlahRecords; 
	int iLoopRecNo = 0; 
	// 
	CxImage imgTemp; 
	CxImage rfft1; 
	CxImage ifft1; 
	int iWidth = 256; 
	int iHeight = 256; 
	 
	int iPixelSize = 1; // 1=GrayScale, 3=24bpp, 4=32bpp 
	int iBlockSize = 16; 
	int i = iWidth/iBlockSize; 
	int j = iHeight/iBlockSize; 
	 
	int iRow; 
	int iCol; 
	int iLoopRow; 
	int iLoopCol; 
	double dTemp; 
	int iBlockNum = 0; 
	long lVal1; 
	BYTE *pImage; 
	 
	// 
	// 
	// 
	ulJumlahRecords = 0L; 
	//dlg.m_ofn.lpstrInitialDir = m_strLokasiLogFile; 
	dlg.m_ofn.lpstrFilter = szFilter; 
 
	if( dlg.DoModal() == IDOK ) 
	{ 
		strNamaFile = dlg.GetPathName(); 
		m_imgLoaded.Load(strNamaFile, CXIMAGE_FORMAT_BMP); 
		RefreshDisplay(); 
 
		if( !m_imgLoaded.IsValid() ) return; 
		 
		// 
		imgTemp.Copy(m_imgLoaded); 
		imgTemp.Resample(iWidth, iHeight); 
		imgTemp.FFT2(&imgTemp, &imgTemp, &rfft1, &ifft1);		 
		pImage = rfft1.GetBits(); 
 
		// 
		for(iRow = 0; iRow < iHeight; iRow += iBlockSize) 
		{		 
			for(iCol = 0; iCol < iWidth; iCol += iBlockSize ) 
			{ 
				// Block Processing 
				m_KeySignature[iBlockNum] = 0.0; 
				dTemp = 0.0; 
				for(iLoopRow = 0; iLoopRow < iBlockSize; iLoopRow++ ) 
				{ 
					for(iLoopCol = 0; iLoopCol < iBlockSize; iLoopCol++ ) 
					{ 
						lVal1 = pImage[((iRow+iLoopRow)*iPixelSize*iWidth) + ((iCol+iLoopCol)*iPixelSize)]; 
						dTemp += (double) lVal1; 
					} 
				} 
				 
				m_KeySignature[iBlockNum] = sqrt(dTemp); 
				iBlockNum++; 
			} 
		} 
		 
		// 
		// Normalize 
		// 
		dTemp = 0; 
		for(i = 0; i < 256; i++ ) 
		{ 
			dTemp += m_KeySignature[i]; 
		} 
		 
		for(i = 0; i < 256; i++ ) 
		{ 
			m_KeySignature[i] /= dTemp; 
		} 
		 
	} 
	else 
	{ 
		AfxMessageBox("Please select a BMP file!"); 
		return; 
	}	 
} 
 
 
void CXbwDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
	 
	//CDialog::OnOK(); 
	CDirDialog dlg; 
	dlg.m_strSelDir  = "C:\\"; 
	dlg.m_strWindowTitle = "Select .JPEG File(s) Directory"; 
	dlg.m_strTitle = "Browse Folder for .BMP files location"; 
	m_strExtension = "*.BMP"; 
	 
	if( dlg.DoBrowse(this) ) 
	{		 
		// 
		// 
		// 
		m_strBeginPath = dlg.m_strPath; 
		m_dwJumlahFile = 0x0; 
 
		CWinThread* pThreadAsu = AfxBeginThread(SiapCariRecursive, this); 
	} 
 
} 
 
void CXbwDlg::OnSelchangeListFounded()  
{ 
	// TODO: Add your control notification handler code here 
	CString strTemp1; 
	CString strTemp2; 
	CStringArray strArr; 
	CTokenEx t; 
 
	m_ListFound.GetText(m_ListFound.GetCurSel(), strTemp1); 
	t.Split(strTemp1, " ", strArr); 
 
	m_imgFound.Load(strArr[2], CXIMAGE_FORMAT_BMP); 
	RefreshDisplay(); 
 
	 
} 
 
void CXbwDlg::RefreshDisplay() 
{ 
	if( m_imgLoaded.IsValid() ) 
	{ 
		CRect r;			 
		CWnd *p = (CWnd*) GetDlgItem(IDC_STATIC_KEYIMAGE); 
		CDC *pDC = p->GetDC(); 
		p->GetWindowRect(&r); 
		 
		 
		m_imgLoaded.Stretch(pDC->m_hDC, 0, 0, r.Width(), r.Height()); 
		p->ReleaseDC(pDC); 
	} 
 
	if( m_imgFound.IsValid() ) 
	{ 
		CRect r;			 
		CWnd *p = (CWnd*) GetDlgItem(IDC_STATIC_FOUNDIMAGE); 
		CDC *pDC = p->GetDC(); 
		p->GetWindowRect(&r); 
		 
		 
		m_imgFound.Stretch(pDC->m_hDC, 0, 0, r.Width(), r.Height()); 
		p->ReleaseDC(pDC); 
	} 
} 
 
UINT __cdecl CXbwDlg::SiapCariRecursive(LPVOID lpParam) 
{ 
	CXbwDlg *p = (CXbwDlg *) lpParam; 
	CWnd *w; 
	 
	w = (CWnd*) p->GetDlgItem(IDCANCEL); 
	w->EnableWindow(FALSE); 
 
	w = (CWnd*) p->GetDlgItem(IDOK); 
	w->EnableWindow(FALSE); 
 
	p->m_ListFound.ResetContent(); 
 
	// 
	p->CariRecursive(p->m_strBeginPath, p->m_strExtension); 
 
	w = (CWnd*) p->GetDlgItem(IDCANCEL); 
	w->EnableWindow(TRUE); 
 
	w = (CWnd*) p->GetDlgItem(IDOK); 
	w->EnableWindow(TRUE); 
 
	// Find the longest string in the list box. 
	CString str; 
	CSize   sz; 
	int     dx=0; 
	CDC*    pDC = p->m_ListFound.GetDC(); 
	for (int i=0;i < p->m_ListFound.GetCount();i++) 
	{ 
		p->m_ListFound.GetText( i, str ); 
		sz = pDC->GetTextExtent(str); 
		 
		if (sz.cx > dx) 
			dx = sz.cx; 
	} 
	p->m_ListFound.ReleaseDC(pDC); 
	 
	// Set the horizontal extent so every character of all strings  
	// can be scrolled to. 
	p->m_ListFound.SetHorizontalExtent(dx); 
 
	return TRUE; 
} 
 
 
void CXbwDlg::CariRecursive(CString strInitPath, CString strFileName) 
{ 
	CFileFind ff; 
	CString strPath; 
	BOOL bFound = TRUE; 
 
	// 
	// Cari Directory dahulu 
	// 
	strPath = strInitPath + "\\*"; 
	if( ff.FindFile(strPath) ) 
	{		 
		while(bFound) 
		{ 
			bFound = ff.FindNextFile(); 
			if(!ff.IsDots()) 
			{ 
				CString strFile = ff.GetFilePath(); 
				 
				if(ff.IsDirectory()) 
				{ 
					CariRecursive(strFile, strFileName); 
				} 
			} 
		} 
 
		ScanFiles(strInitPath + "\\" + strFileName ); 
	} 
 
	ff.Close(); 
} 
 
void CXbwDlg::ScanFiles(CString strNamaFile) 
{ 
	CFileFind ff; 
	BOOL bFound = TRUE; 
 
	if( ff.FindFile(strNamaFile) ) 
	{ 
		while(bFound) 
		{ 
			bFound = ff.FindNextFile(); 
			if((!ff.IsDots()) && (!ff.IsDirectory())) 
			{ 
				CString strFile = ff.GetFilePath(); 
				 
				// 
				// Do image analysis 
				// 
				Madhosi(strFile);				 
				m_dwJumlahFile++;				 
			} 
		} 
	} 
 
	ff.Close(); 
} 
 
double CXbwDlg::Madhosi(CString strNamaFile) 
{ 
	CString strTemp; 
	CxImage imageAsli; 
	CxImage rfft1; 
	CxImage ifft1; 
	int iWidth = 256; 
	int iHeight = 256; 
 
	int iPixelSize = 1; // 1=GrayScale, 3=24bpp, 4=32bpp 
	int iBlockSize = 16; 
	int i = iWidth/iBlockSize; 
	int j = iHeight/iBlockSize; 
 
	int iRow; 
	int iCol; 
	int iLoopRow; 
	int iLoopCol; 
	double *lSignature; 
	double dTemp; 
	int iBlockNum = 0; 
	long lVal1; 
	BYTE *pImage; 
 
	imageAsli.Load(strNamaFile, CXIMAGE_FORMAT_BMP); 
	if( !imageAsli.IsValid() ) return 1.0; 
 
	imageAsli.Resample(iWidth, iHeight); 
	imageAsli.FFT2(&imageAsli, &imageAsli, &rfft1, &ifft1); 
 
	lSignature = (double*) malloc(sizeof(double)*i*j); 
	pImage = rfft1.GetBits(); 
	for(iRow = 0; iRow < iHeight; iRow += iBlockSize) 
	{		 
		for(iCol = 0; iCol < iWidth; iCol += iBlockSize ) 
		{ 
			// Block Processing 
			lSignature[iBlockNum] = 0.0; 
			dTemp = 0.0; 
			for(iLoopRow = 0; iLoopRow < iBlockSize; iLoopRow++ ) 
			{ 
				for(iLoopCol = 0; iLoopCol < iBlockSize; iLoopCol++ ) 
				{ 
					lVal1 = pImage[((iRow+iLoopRow)*iPixelSize*iWidth) + ((iCol+iLoopCol)*iPixelSize)]; 
					dTemp += (double) lVal1; 
				} 
			} 
 
			lSignature[iBlockNum] = sqrt(dTemp); 
			iBlockNum++; 
		} 
	} 
 
	// 
	// Normalize 
	// 
	dTemp=0.0; 
	for(i = 0; i < 256; i++ ) 
	{ 
		dTemp += lSignature[i]; 
	} 
 
	for(i = 0; i < 256; i++ ) 
	{ 
		lSignature[i] /= dTemp; 
	} 
 
 
	// 
	// There are 256 elements for each image. Compare them using Eucliean Distance 
	// The smaller the value the similiar the image visually 
	// 
	dTemp = 0.0; 
	for(i = 0; i < 256; i++ ) 
	{ 
		dTemp += (m_KeySignature[i]-lSignature[i])*(m_KeySignature[i]-lSignature[i]); 
	} 
 
	// 
	dTemp = sqrt(dTemp); 
 
	// 
	free(lSignature); 
 
	strTemp.Format("%.7f - %s", dTemp, strNamaFile); 
	m_ListFound.AddString(strTemp); 
 
	return dTemp; 
}