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

📄 example1dlg.cpp

📁 一个Fourier变换的小程序;算法简单实用。
💻 CPP
字号:
// example1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "example1.h"
#include "example1Dlg.h"
#include "Fourier.h"

#include <COMPLEX>
using namespace std;

#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()

/////////////////////////////////////////////////////////////////////////////
// CExample1Dlg dialog

CExample1Dlg::CExample1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExample1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExample1Dlg)
	m_FileIn = _T("");
	m_FileOut = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_Width=0;
	m_Height=0;
	m_Type = -1;
}

void CExample1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExample1Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_FileIn);
	DDX_Text(pDX, IDC_EDIT2, m_FileOut);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CExample1Dlg, CDialog)
	//{{AFX_MSG_MAP(CExample1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExample1Dlg message handlers

BOOL CExample1Dlg::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 CExample1Dlg::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 CExample1Dlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CExample1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CExample1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"rmg文件(*.rmg)|*.rmg|所有文件(*.*)|*.*||");
	if(dlg.DoModal()!=IDOK)
		return;
	m_FileIn =dlg.GetPathName();

	int dot =m_FileIn.ReverseFind('.');

	CString m_FileInH;
	if(dot==-1)
		m_FileInH =m_FileIn+".ldr";
	else
		m_FileInH =m_FileIn.Left(dot)+".ldr";
	FILE *fp;
	fp=fopen(m_FileInH,"r");
	if(fp==NULL)
	{
		MessageBox("无法打开图像属性文件!","错误",MB_ICONERROR|MB_OK);
		m_FileIn="";
		m_FileInH="";
		return;
	}
	char buf[1000];
	while(!feof(fp))
	{
		fscanf(fp,"%s",buf);
		if(!strcmp(buf,"[像元数]"))
		{
			fscanf(fp,"%s",buf);
			m_Width=atoi(buf);
		}
		if(!strcmp(buf,"[行数]"))
		{
			fscanf(fp,"%s",buf);
			m_Height=atoi(buf);
		}
		if(!strcmp(buf,"[数据类型]"))
		{
			fscanf(fp,"%s",buf);
			m_Type=atoi(buf);
		}
	}
	fclose(fp);
	UpdateData(false);		
}

void CExample1Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	CFileDialog dlgfile(false,"rmg",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
		"rmg文件(*.rmg)|*.rmg|所有文件(*.*)|*.*||",NULL);
	if((dlgfile.DoModal())==IDOK)
	{
		m_FileOut=dlgfile.GetPathName();
		UpdateData(false);
	}			
}

void CExample1Dlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(true);
	BeginWaitCursor();
	//
	int i,j;
	CFile file1;
	CFileException ex1;
	int w,h;
	int wp,hp;
	w=1;h=1;
	wp=0;hp=0;
	while(w<m_Width)
	{
		w=w*2;
		wp=wp+1;
	}
	while(h<m_Height)
	{
		h=h*2;
		hp=hp+1;
	}
	//
	if(m_Type==1)	//short
	{
		short* data=new short[m_Width*m_Height];
		//
		if(!file1.Open(m_FileIn,CFile::modeRead | CFile::shareDenyWrite,&ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError, 1024);
			AfxMessageBox(szError);
			return ;
		}
		file1.Read(data,sizeof(short)*m_Width*m_Height);
		file1.Close();
		//
		//
		complex<float>* outdata=new complex<float>[w*h];
		memset(outdata,0,sizeof(complex<float>));	//设置为0,最好设置
		//
		CFourier fre;
		fre.Fourier2(data,m_Width,m_Height,outdata);
		fre.fftshift(outdata,w,h,1);
		fre.fftshift(outdata,w,h,2);
		//
		if(!file1.Open(m_FileOut,CFile::modeWrite |
					CFile::shareExclusive | CFile::modeCreate, &ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError,1024);
			AfxMessageBox(szError);
			return;
		}
		file1.Write(outdata,sizeof(complex<float>)*w*h);
		file1.Close();
		//
		delete[] data;
		delete[] outdata;
	}
	else if(m_Type==2)	//int 
	{
		int* data=new int[m_Width*m_Height];
		//
		if(!file1.Open(m_FileIn,CFile::modeRead | CFile::shareDenyWrite,&ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError, 1024);
			AfxMessageBox(szError);
			return ;
		}
		file1.Read(data,sizeof(int)*m_Width*m_Height);
		file1.Close();
		//
		//
		complex<float>* outdata=new complex<float>[w*h];
		//
		CFourier fre;
		fre.Fourier2(data,m_Width,m_Height,outdata);
		fre.fftshift(outdata,w,h,1);
		fre.fftshift(outdata,w,h,2);
		//
		if(!file1.Open(m_FileOut,CFile::modeWrite |
					CFile::shareExclusive | CFile::modeCreate, &ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError,1024);
			AfxMessageBox(szError);
			return;
		}
		file1.Write(outdata,sizeof(complex<float>)*w*h);
		file1.Close();
		//
		delete[] data;
		delete[] outdata;		
	}
	else if(m_Type==3)	//float
	{
		//先读数据
		float * data_1=new float[m_Width*m_Height];	//原始数据
		float * data_2=new float[m_Height*m_Width];	//转置数据
		if(!file1.Open(m_FileIn,CFile::modeRead | CFile::shareDenyWrite,&ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError, 1024);
			AfxMessageBox(szError);
			return ;
		}
		file1.Read(data_1,sizeof(float)*m_Width*m_Height);
		file1.Close();
		//转置
		for(i=0;i<m_Height;i++)
		{
			for(j=0;j<m_Width;j++)
			{
				data_2[j*m_Height+i]=data_1[i*m_Width+j];
			}
		}
		delete[] data_1;	//释放临时内存空间
		//
		data_1 = new* 
		//-------------------------------------
		//先对行处理FFT
		int New_H1 = 25*1024*1024/(sizeof(float)*m_Width);		//25M作为一个分块的高度
		int block_1 = m_Height/New_H1 + 1 ;	//分的块数

		complex<float>* outdata=new complex<float>[w*m_Height];	//中间分配的内存

		float* data=new float[m_Width*New_H1];					//每一块分配的内存		
		//打开读的文件
		if(!file1.Open(m_FileIn,CFile::modeRead | CFile::shareDenyWrite,&ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError, 1024);
			AfxMessageBox(szError);
			return ;
		}
		//
		CFourier fre;
		for(int i=0;i<block_1;i++)
		{
			if(i<block_1-1)		//前面的小块
			{
				file1.Read(data,sizeof(float)*m_Width*New_H1);
				//
				fre.Fourier2(data,m_Width,New_H1,Outdata+i*New_H1*w,2);			//2--->对行
			}
			else				//最后的一块
			{
				file1.Read(data,sizeof(float)*m_Width*(m_Height-(block_1-1)*New_H1));
				//
				fre.Fourier2(data,m_Width,(m_Height-(block_1-1)*New_H1),outdata+i*New_H1*w,2);
			}
		}
		//
		fre.fftshift(outdata,w,m_Height,2);		//对行
		//
		file1.Close();		//关闭文件
		delete[] data;		//释放内存
		//---------------------------------------------------------------
		//再对列处理FFT
		if(!file1.Open(m_FileOut,CFile::modeWrite |
					CFile::shareExclusive | CFile::modeCreate, &ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError,1024);
			AfxMessageBox(szError);
			return;
		}
		//
		complex<float>*TD=new complex<float>[h];	//分配内存存储时域数据
		//
		for(i=0;i<w;i++)
		{
			memset(TD,0,sizeof(complex<float>)*h);	//赋值0
			//将一列数据存到TD中
			for(int j=0;j<lHeight;j++)
			{
				TD[j]=complex<float>(outdata[j*w+i],0);
			}
			//对TD做FFT
			fre.FFT(TD,hp);
			//将数据保存到FDresult
			for(j=0;j<h;j++)
			{
				FDresult[j*+i]=TD[j];
			}		
		}
		//
		delete[] TD;
		delete[] FDresult;

		//
		if(!file1.Open(m_FileOut,CFile::modeWrite |
					CFile::shareExclusive | CFile::modeCreate, &ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError,1024);
			AfxMessageBox(szError);
			return;
		}
		file1.Write(outdata,sizeof(complex<float>)*w*h);
		file1.Close();
		//
		delete[] data;
		delete[] outdata;
	}
	else if(m_Type==6)	//complex<float>
	{
		complex<float>* data=new complex<float>[m_Width*m_Height];
		//
		if(!file1.Open(m_FileIn,CFile::modeRead | CFile::shareDenyWrite,&ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError, 1024);
			AfxMessageBox(szError);
			return ;
		}
		file1.Read(data,sizeof(complex<float>)*m_Width*m_Height);
		file1.Close();
		//
		//
		complex<float>* outdata=new complex<float>[w*h];
		//
		CFourier fre;
		//正变换
		fre.Fourier2(data,m_Width,m_Height,outdata);
		fre.fftshift(outdata,w,h,1);	//对行
		fre.fftshift(outdata,w,h,2);	//对列
//		//反变换
//		fre.fftshift(data,m_Width,m_Height,1);
//		fre.fftshift(data,m_Width,m_Height,2);
//		fre.InFourier2(data,m_Width,m_Height,outdata);
		//
		if(!file1.Open(m_FileOut,CFile::modeWrite |
					CFile::shareExclusive | CFile::modeCreate, &ex1))
		{
			TCHAR szError[1024];
			ex1.GetErrorMessage(szError,1024);
			AfxMessageBox(szError);
			return;
		}
		file1.Write(outdata,sizeof(complex<float>)*w*h);
		file1.Close();
		//
		delete[] data;
		delete[] outdata;
	}

	//
	EndWaitCursor();
	CDialog::OnOK();
}

⌨️ 快捷键说明

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