// analyzerDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "analyzer.h"
#include "analyzerDlg.h"

//头文件和命名空间
using namespace std;
#include "const_variable.h"
#include <vector>
#include <cmath>
#include "psnr.h"
#include "first_derivative.h"
#include <iostream>
#include <fstream>
#include "correlation.h"

//常量定义
const int PSNR = 0;
const int FIRST_DERIVATIVE = 1;
const int SSIM = 2;

const int TIMER_O = 1111;
const int TIMER_C = 1112;

const int fps = 30;

//变量定义

//评价方法
int n_method = 0;
int n_group = 0;



#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CanalyzerDlg 对话框




CanalyzerDlg::CanalyzerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CanalyzerDlg::IDD, pParent)
	, yuv_o_filename(_T(""))
	, yuv_c_filename(_T(""))
	, result(0)
	, dir_data(_T(""))
	, dir_output(_T(""))
	, obj_filename(_T(""))
	, sub_filename(_T(""))
	, similarity(0)
	, isplay_o(false)
	, isplay_c(false)
	, file_ori(NULL)
	, file_cmp(NULL)
	, playtimer_o(0)
	, playtimer_c(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CanalyzerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_METHOD, method);
	DDX_Text(pDX, IDC_EDIT_YUV_O, yuv_o_filename);
	DDX_Text(pDX, IDC_EDIT_YUV_C, yuv_c_filename);
	DDX_Text(pDX, IDC_STATIC_RESULT, result);
	DDX_Text(pDX, IDC_EDIT_DIR_DATA, dir_data);
	DDX_Text(pDX, IDC_EDIT_OUTPUT_FILENAME, dir_output);
	DDX_Text(pDX, IDC_EDIT_OBJ, obj_filename);
	DDX_Text(pDX, IDC_EDIT_SUB, sub_filename);
	DDX_Control(pDX, IDC_COMBO_GROUP, group);
	DDX_Text(pDX, IDC_STATIC_SIMILARITY, similarity);
	DDX_Control(pDX, IDC_STATIC_YUV_O, playback_o);
	DDX_Control(pDX, IDC_STATIC_YUV_C, playback_c);
	DDX_Control(pDX, IDC_BUTTON_PLAY_YUV_O, playpause_o);
	DDX_Control(pDX, IDC_BUTTON_PLAY_YUV_C, playpause_c);
}

BEGIN_MESSAGE_MAP(CanalyzerDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO_METHOD, &CanalyzerDlg::OnCbnSelchangeComboMethod)
	ON_CBN_SELCHANGE(IDC_COMBO_GROUP, &CanalyzerDlg::OnCbnSelchangeComboGroup)
	ON_BN_CLICKED(IDC_BUTTON_CALC, &CanalyzerDlg::OnBnClickedButtonCalc)
	ON_BN_CLICKED(IDC_BUTTON_OUTPUT, &CanalyzerDlg::OnBnClickedButtonOutput)
	ON_BN_CLICKED(IDC_BUTTON_COMPARE, &CanalyzerDlg::OnBnClickedButtonCompare)
	ON_BN_CLICKED(IDC_BUTTON_YUV_O, &CanalyzerDlg::OnBnClickedButtonYuvO)
	ON_BN_CLICKED(IDC_BUTTON_YUV_C, &CanalyzerDlg::OnBnClickedButtonYuvC)
	ON_BN_CLICKED(IDC_BUTTON_OBJ, &CanalyzerDlg::OnBnClickedButtonObj)
	ON_BN_CLICKED(IDC_BUTTON_SUB, &CanalyzerDlg::OnBnClickedButtonSub)
	ON_BN_CLICKED(IDC_BUTTON_PLAY_YUV_O, &CanalyzerDlg::OnBnClickedButtonPlayYuvO)
	ON_BN_CLICKED(IDC_BUTTON_PLAY_YUV_C, &CanalyzerDlg::OnBnClickedButtonPlayYuvC)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_OUTPUTALL, &CanalyzerDlg::OnBnClickedButtonOutputall)
	ON_BN_CLICKED(IDC_BUTTON_COMPAREALL, &CanalyzerDlg::OnBnClickedButtonCompareall)
END_MESSAGE_MAP()


// CanalyzerDlg 消息处理程序

BOOL CanalyzerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	//添加METHOD
	method.InsertString(PSNR,"PSNR");
	method.InsertString(FIRST_DERIVATIVE,"FIRST_DERIVATIVE");
	method.InsertString(SSIM,"SSIM");
	method.SetCurSel(PSNR);
	n_method = PSNR;

	//添加GROUP
	group.AddString("akyio");
	group.AddString("carphone");
	group.AddString("foreman");
	group.AddString("hallmonitor");
	group.AddString("mobile");
	group.AddString("news");
	group.AddString("salesman");
	group.AddString("silent");
	group.AddString("suzie");
	group.AddString("walk");
	group.SetCurSel(0);
	n_group = 0;

	dir_data = "e:\\trs\\data\\";
	dir_output.Insert(0,"e:\\trs\\datamatrix\\obj\\");

	//设置playback
	playback_o.SetYUVSize(width,height);
	playback_o.SetColorful(TRUE);
	playback_o.SetShowMV(FALSE);
	playback_o.ShowWindow(SW_SHOWNORMAL);
	playback_c.SetYUVSize(width,height);
	playback_c.SetColorful(TRUE);
	playback_c.SetShowMV(FALSE);
	playback_c.ShowWindow(SW_SHOWNORMAL);

	isplay_o = FALSE;
	isplay_c = FALSE;

	UpdateData(FALSE);
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CanalyzerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CanalyzerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CanalyzerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CanalyzerDlg::OnCbnSelchangeComboMethod()
{
	// TODO: 在此添加控件通知处理程序代码
	n_method = method.GetCurSel();	
}

void CanalyzerDlg::OnCbnSelchangeComboGroup()
{
	// TODO: 在此添加控件通知处理程序代码
	n_group = group.GetCurSel();
}

void CanalyzerDlg::OnBnClickedButtonCalc()
{
	// TODO: 在此添加控件通知处理程序代码

	//更新文件路径
	UpdateData(TRUE);
	CFile *file_o = new CFile();
	CFile *file_c = new CFile();
	
	//判断文件是否打开
	if (!file_o -> Open(yuv_o_filename,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
	{
		AfxMessageBox("Cannot open file_o!");
		return;
	}
	if (!file_c -> Open(yuv_c_filename,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
	{
		AfxMessageBox("Cannot open file_c!");
		return;
	}

	//根据选定的方法评价
	switch (n_method)
	{
	case PSNR:
		result = calc_psnr(file_o,file_c);
		break;
	case FIRST_DERIVATIVE:
		result = calc_first_derivative(file_o,file_c);
		break;
	default:
		AfxMessageBox("Choose a method!");
	}

	delete file_o;
	delete file_c;
	
	UpdateData(FALSE);
}

void CanalyzerDlg::OnBnClickedButtonOutput()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	//防止访问不存在的文件夹；自动加backslash
	if (dir_data.IsEmpty())
	{
		AfxMessageBox("Please input the directory of data!");
	}
	else if (dir_data.GetAt(dir_data.GetLength() - 1) != '\\')
	{
		dir_data.Insert(dir_data.GetLength(),'\\');
		UpdateData(FALSE);
	}

	//设置具体文件夹目录
	CString dir_data_group;
	group.GetLBText(n_group,dir_data_group);
	dir_data_group.Insert(0,dir_data);
	dir_data_group.Insert(dir_data_group.GetLength(),'\\');

	//评价
	//存储22个对照数据的结果
	double res[23];

	switch (n_method)
	{
	case PSNR:
		{
			//原始文件指针和文件名
			CFile *file_o = new CFile();
			CString filename_o(dir_data_group);
			filename_o.Insert(filename_o.GetLength(),"1000");
			filename_o.Insert(filename_o.GetLength(),".yuv");

			////打开文件检验
			if (!file_o -> Open(filename_o,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
			{
				AfxMessageBox("Cannot open original data!");
				return;
			}

			for (int i = 1001;i <= 1022;++i)
			{
				//原始文件回到开始
				file_o -> Seek(0,CFile::begin);

				//对照文件指针和文件名
				CFile *file_c = new CFile();
				CString filename_c(dir_data_group);
				CString temp_c_number;
				temp_c_number.Format("%d",i);
				filename_c.Insert(filename_c.GetLength(),temp_c_number);
				filename_c.Insert(filename_c.GetLength(),".yuv");

				//打开文件检验
				if (!file_c -> Open(filename_c,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
				{
					AfxMessageBox("Cannot open compare data!");
					return;
				}

				//评价和保存结果
				res[i - 1000] = calc_psnr(file_o,file_c);	

				//关闭对照文件，释放内存
				file_c -> Close();
				delete file_c;
			}
			
			//原始数据质量最高
			res[0] = 100;

			//关闭原始文件，释放内存
			file_o -> Close();
			delete file_o;

			break;
		}
	case FIRST_DERIVATIVE:
		{
			//原始文件指针和文件名
			CFile *file_o = new CFile();
			CString filename_o(dir_data_group);
			filename_o.Insert(filename_o.GetLength(),"1000");
			filename_o.Insert(filename_o.GetLength(),".yuv");

			////打开文件检验
			if (!file_o -> Open(filename_o,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
			{
				AfxMessageBox("Cannot open original data!");
				return;
			}

			for (int i = 1001;i <= 1022;++i)
			{
				//原始文件回到开始
				file_o -> Seek(0,CFile::begin);

				//对照文件指针和文件名
				CFile *file_c = new CFile();
				CString filename_c(dir_data_group);
				CString temp_c_number;
				temp_c_number.Format("%d",i);
				filename_c.Insert(filename_c.GetLength(),temp_c_number);
				filename_c.Insert(filename_c.GetLength(),".yuv");

				//打开文件检验
				if (!file_c -> Open(filename_c,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
				{
					AfxMessageBox("Cannot open compare data!");
					return;
				}

				//评价和保存结果
				res[i - 1000] = calc_first_derivative(file_o,file_c);	

				//关闭对照文件，释放内存
				file_c -> Close();
				delete file_c;
			}

			//原始数据质量最高
			res[0] = 100;

			//关闭原始文件，释放内存
			file_o -> Close();
			delete file_o;

			break;
		}
	default:
		AfxMessageBox("Choose a method!");
	}

	//输出文件
	if (dir_output.GetAt(dir_output.GetLength() - 1) != '\\')
	{
		dir_output.Insert(dir_output.GetLength(),'\\');
		UpdateData(FALSE);
	}
	CString output_filename;
	group.GetLBText(n_group,output_filename);
	output_filename.Insert(0,dir_output);
	output_filename.Insert(output_filename.GetLength(),"_");
	CString method_temp;
	method.GetLBText(n_method,method_temp);
	output_filename.Insert(output_filename.GetLength(),method_temp);
	output_filename.Insert(output_filename.GetLength(),".csv");
	
	ofstream fout;
	fout.open(output_filename);
	if (output_filename.IsEmpty() || !fout)
	{
		AfxMessageBox("Cannot create output file!");
		return;
	}
	
	//输出评价数据
	//原始数据（第1行）
	for (int j = 0;j < 23;++j)
	{
		fout << 0;
		if (j != 22)
			fout << ",";
		else
			fout << endl;
	}
	//对照数据（第2行-第23行）
	for (int i = 1;i < 23;++i)
	{
		for (int j = 0;j < 23;++j)
		{
			fout << (res[j] > res[i]);
			if (j != 22)
				fout << ",";
			else
				fout << endl;
		}
	}
	fout.close();
	fout.clear();

	//告知结束
	AfxMessageBox("Calculation finished!");
}

void CanalyzerDlg::OnBnClickedButtonCompare()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	similarity = correlation(string(sub_filename),string(obj_filename));

	UpdateData(FALSE);
}

void CanalyzerDlg::OnBnClickedButtonYuvO()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog yuv_o_dlg(TRUE,"yuv","*.yuv",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.yuv");

	if (yuv_o_dlg.DoModal() != IDCANCEL)
	{
		yuv_o_filename = yuv_o_dlg.GetPathName();

		char yuv_o_filename_temp[1024];
		sprintf(yuv_o_filename_temp,"%s",yuv_o_filename);
		playback_o.SetPathName(yuv_o_filename_temp);
		
		playback_o.ReStart();

		UpdateData(FALSE);
	}
}

void CanalyzerDlg::OnBnClickedButtonYuvC()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog yuv_c_dlg(TRUE,"yuv","*.yuv",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.yuv");

	if (yuv_c_dlg.DoModal() != IDCANCEL)
	{
		yuv_c_filename = yuv_c_dlg.GetPathName();

		char yuv_c_filename_temp[1024];
		sprintf(yuv_c_filename_temp,"%s",yuv_c_filename);
		playback_c.SetPathName(yuv_c_filename_temp);

		playback_c.ReStart();

		UpdateData(FALSE);
	}
}

void CanalyzerDlg::OnBnClickedButtonObj()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog obj_dlg(TRUE,"csv","*.csv",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.csv");

	if (obj_dlg.DoModal() != IDCANCEL)
	{
		obj_filename = obj_dlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CanalyzerDlg::OnBnClickedButtonSub()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog sub_dlg(TRUE,"csv","*.csv",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"*.csv");

	if (sub_dlg.DoModal() != IDCANCEL)
	{
		sub_filename = sub_dlg.GetPathName();
		UpdateData(FALSE);
	}
}

void CanalyzerDlg::OnBnClickedButtonPlayYuvO()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (yuv_o_filename.IsEmpty())
	{
		AfxMessageBox("No original file!");
		return;
	}

	if (!isplay_o)
	{
		isplay_o = TRUE;
		playpause_o.SetWindowText("PAUSE");

		if (playtimer_o == TIMER_O)
			KillTimer(playtimer_o);
		playtimer_o = SetTimer(TIMER_O,1000/fps,NULL);
		playback_o.StartPlay();
	}
	else
	{
		isplay_o = FALSE;
		playpause_o.SetWindowText("PLAY");

		if (playtimer_o == TIMER_O)
			KillTimer(TIMER_O);
		playback_o.PausePlay();
	}
}

void CanalyzerDlg::OnBnClickedButtonPlayYuvC()
{
	// TODO: 在此添加控件通知处理程序代码
	
	UpdateData(TRUE);
	if (yuv_c_filename.IsEmpty())
	{
		AfxMessageBox("No compare file!");
		return;
	}

	if (!isplay_c)
	{
		isplay_c = TRUE;
		playpause_c.SetWindowText("PAUSE");

		if (playtimer_c == TIMER_C)
			KillTimer(playtimer_c);
		playtimer_c = SetTimer(TIMER_C,1000/fps,NULL);
		playback_c.StartPlay();
	}
	else
	{
		isplay_c = FALSE;
		playpause_c.SetWindowText("PLAY");

		if (playtimer_c == TIMER_C)
			KillTimer(TIMER_C);
		playback_c.PausePlay();
	}
}

void CanalyzerDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent == TIMER_O)
	{
		++playback_o.iCurrFrameNumber;
		if (playback_o.iCurrFrameNumber >= playback_o.iTotalFrameNumber)
			playback_o.iCurrFrameNumber = 0;

		/*
		char sFrameNumber[16];
		sprintf(sFrameNumber,"%d/%d",iCurrFrameNumber+1,iMaxFrameNumber );
		m_frame_num.SetWindowText(sFrameNumber);
		m_progress.SetPos(iCurrFrameNumber);
		*/

		if (playback_o.iCurrFrameNumber < playback_o.iTotalFrameNumber)
		{
			playback_o.ContinuePlay();
		}
	}
	else if (nIDEvent == TIMER_C)
	{
		++playback_c.iCurrFrameNumber;
		if (playback_c.iCurrFrameNumber >= playback_c.iTotalFrameNumber)
			playback_c.iCurrFrameNumber = 0;

		/*
		char sFrameNumber[16];
		sprintf(sFrameNumber,"%d/%d",iCurrFrameNumber+1,iMaxFrameNumber );
		m_frame_num.SetWindowText(sFrameNumber);
		m_progress.SetPos(iCurrFrameNumber);
		*/

		if (playback_c.iCurrFrameNumber < playback_c.iTotalFrameNumber)
		{
			playback_c.ContinuePlay();
		}
	}

	CDialog::OnTimer(nIDEvent);
}

void CanalyzerDlg::OnBnClickedButtonOutputall()
{
	// TODO: 在此添加控件通知处理程序代码
	for (n_group= 0;n_group < group.GetCount();++n_group)
	{
		group.SetCurSel(n_group);
		UpdateData(FALSE);
		
		UpdateData(TRUE);

		//防止访问不存在的文件夹；自动加backslash
		if (dir_data.IsEmpty())
		{
			AfxMessageBox("Please input the directory of data!");
		}
		else if (dir_data.GetAt(dir_data.GetLength() - 1) != '\\')
		{
			dir_data.Insert(dir_data.GetLength(),'\\');
			UpdateData(FALSE);
		}

		//设置具体文件夹目录
		CString dir_data_group;
		group.GetLBText(n_group,dir_data_group);
		dir_data_group.Insert(0,dir_data);
		dir_data_group.Insert(dir_data_group.GetLength(),'\\');

		//评价
		//存储22个对照数据的结果
		double res[23];

		switch (n_method)
		{
		case PSNR:
			{
				//原始文件指针和文件名
				CFile *file_o = new CFile();
				CString filename_o(dir_data_group);
				filename_o.Insert(filename_o.GetLength(),"1000");
				filename_o.Insert(filename_o.GetLength(),".yuv");

				////打开文件检验
				if (!file_o -> Open(filename_o,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
				{
					AfxMessageBox("Cannot open original data!");
					return;
				}

				for (int i = 1001;i <= 1022;++i)
				{
					//原始文件回到开始
					file_o -> Seek(0,CFile::begin);

					//对照文件指针和文件名
					CFile *file_c = new CFile();
					CString filename_c(dir_data_group);
					CString temp_c_number;
					temp_c_number.Format("%d",i);
					filename_c.Insert(filename_c.GetLength(),temp_c_number);
					filename_c.Insert(filename_c.GetLength(),".yuv");

					//打开文件检验
					if (!file_c -> Open(filename_c,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
					{
						AfxMessageBox("Cannot open compare data!");
						return;
					}

					//评价和保存结果
					res[i - 1000] = calc_psnr(file_o,file_c);	

					//关闭对照文件，释放内存
					file_c -> Close();
					delete file_c;
				}

				//原始数据质量最高
				res[0] = 100;

				//关闭原始文件，释放内存
				file_o -> Close();
				delete file_o;

				break;
			}
		case FIRST_DERIVATIVE:
			{
				//原始文件指针和文件名
				CFile *file_o = new CFile();
				CString filename_o(dir_data_group);
				filename_o.Insert(filename_o.GetLength(),"1000");
				filename_o.Insert(filename_o.GetLength(),".yuv");

				////打开文件检验
				if (!file_o -> Open(filename_o,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
				{
					AfxMessageBox("Cannot open original data!");
					return;
				}

				for (int i = 1001;i <= 1022;++i)
				{
					//原始文件回到开始
					file_o -> Seek(0,CFile::begin);

					//对照文件指针和文件名
					CFile *file_c = new CFile();
					CString filename_c(dir_data_group);
					CString temp_c_number;
					temp_c_number.Format("%d",i);
					filename_c.Insert(filename_c.GetLength(),temp_c_number);
					filename_c.Insert(filename_c.GetLength(),".yuv");

					//打开文件检验
					if (!file_c -> Open(filename_c,CFile::typeBinary | CFile::modeRead | CFile::shareDenyNone))
					{
						AfxMessageBox("Cannot open compare data!");
						return;
					}

					//评价和保存结果
					res[i - 1000] = calc_first_derivative(file_o,file_c);	

					//关闭对照文件，释放内存
					file_c -> Close();
					delete file_c;
				}

				//原始数据质量最高
				res[0] = 100;

				//关闭原始文件，释放内存
				file_o -> Close();
				delete file_o;

				break;
			}
		default:
			AfxMessageBox("Choose a method!");
		}

		//输出文件
		if (dir_output.GetAt(dir_output.GetLength() - 1) != '\\')
		{
			dir_output.Insert(dir_output.GetLength(),'\\');
			UpdateData(FALSE);
		}
		CString output_filename;
		group.GetLBText(n_group,output_filename);
		output_filename.Insert(0,dir_output);
		output_filename.Insert(output_filename.GetLength(),"_");
		CString method_temp;
		method.GetLBText(n_method,method_temp);
		output_filename.Insert(output_filename.GetLength(),method_temp);
		output_filename.Insert(output_filename.GetLength(),".csv");

		ofstream fout;
		fout.open(output_filename);
		if (output_filename.IsEmpty() || !fout)
		{
			AfxMessageBox("Cannot create output file!");
			return;
		}

		//输出评价数据
		//原始数据（第1行）
		for (int j = 0;j < 23;++j)
		{
			fout << 0;
			if (j != 22)
				fout << ",";
			else
				fout << endl;
		}
		//对照数据（第2行-第23行）
		for (int i = 1;i < 23;++i)
		{
			for (int j = 0;j < 23;++j)
			{
				fout << (res[j] > res[i]);
				if (j != 22)
					fout << ",";
				else
					fout << endl;
			}
		}
		fout.close();
		fout.clear();
	}
	n_group = 0;
	group.SetCurSel(n_group);
	UpdateData(FALSE);

	//告知结束
	AfxMessageBox("Output finished!");
}

void CanalyzerDlg::OnBnClickedButtonCompareall()
{
	// TODO: 在此添加控件通知处理程序代码
	CString sub_pathname = "e:\\trs\\datamatrix\\sub\\";
	CString obj_pathname = "e:\\trs\\datamatrix\\obj\\";
	CString sub_filename_temp,obj_filename_temp;

	CString method_temp;
	method.GetLBText(n_method,method_temp);
	
	double res[10];
	for (n_group = 0;n_group < group.GetCount();++n_group)
	{
		group.GetLBText(n_group,obj_filename_temp);
		obj_filename_temp.Insert(0,obj_pathname);
		obj_filename_temp.Insert(obj_filename_temp.GetLength(),"_");
		obj_filename_temp.Insert(obj_filename_temp.GetLength(),method_temp);
		obj_filename_temp.Insert(obj_filename_temp.GetLength(),".csv");

		group.GetLBText(n_group,sub_filename_temp);
		sub_filename_temp.Insert(0,sub_pathname);
		sub_filename_temp.Insert(sub_filename_temp.GetLength(),".csv");

		res[n_group] = correlation(string(sub_filename_temp),string(obj_filename_temp));
	}
	n_group = 0;
	group.SetCurSel(n_group);

	//输出	
	ofstream fout;
	CString output_filename = "e:\\trs\\datamatrix\\correlation_";
	output_filename.Insert(output_filename.GetLength(),method_temp);
	output_filename.Insert(output_filename.GetLength(),".txt");
	fout.open(output_filename);
	if (!fout)
	{
		AfxMessageBox("Cannot open output file!");
		return;
	}

	for (int i = 0;i < 10;++i)
	{
		CString group_filename_temp;
		group.GetLBText(i,group_filename_temp);
		fout << group_filename_temp << " " << res[i] << endl;
	}
	fout.close();
	fout.clear();
	AfxMessageBox("Output finished!");

	UpdateData(FALSE);
}
