// extractorDoc.cpp : implementation of the CExtractorDoc class
//

#include "stdafx.h"
#include "extractor.h"

#include "extractorDoc.h"
#include "avi.h"
#include "OutputDlg.h"
//#include "

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

/////////////////////////////////////////////////////////////////////////////
// CExtractorDoc

IMPLEMENT_DYNCREATE(CExtractorDoc, CDocument)

BEGIN_MESSAGE_MAP(CExtractorDoc, CDocument)
	//{{AFX_MSG_MAP(CExtractorDoc)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILTER, OnFilter)
	ON_COMMAND(ID_OUTPUT, OnOutput)
	ON_COMMAND(ID_BUTTON_START, OnButtonStart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExtractorDoc construction/destruction

CExtractorDoc::CExtractorDoc()
{
	// TODO: add one-time construction code here

}

CExtractorDoc::~CExtractorDoc()
{
}

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

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

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CExtractorDoc serialization

void CExtractorDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CExtractorDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CExtractorDoc commands

//open a avi file for extractor
void CExtractorDoc::OnFileOpen() 
{
	// TODO: Add your command handler code here

	CWnd* pFrame = AfxGetMainWnd();
	m_strFileName.Format("0");	 //initial the filename.

	CFileDialog *openfile = new CFileDialog(TRUE,NULL,NULL,
		OFN_HIDEREADONLY,
		"avi Files (*.avi)|*.avi|All Files (*.*)|*.*||",NULL);

	if(openfile->DoModal()==TRUE)
		m_strFileName.Format("%s",openfile->GetPathName());
	
	::_mkdir("picture");

	if(m_strFileName.Compare("0")==1)
	{

		m_avi.AviInit(m_strFileName);	//open an avi file according to the name, and get the information of its stream.

		//show the inforamtion.
		pFrame->SetWindowText("easy extractor---ready");
		pFrame->Invalidate();
	
	}

}


//setting the filter and critical values
void CExtractorDoc::OnFilter() 
{
	m_dlgFilter.DoModal();

	//set filter
	m_avi.SetFilterType( m_dlgFilter.GetFilterType() );
	m_avi.SetFilterRange( m_dlgFilter.GetFilterRange() );

	m_avi.SetPixelThreshold( m_dlgFilter.GetMinDistance() );
	m_avi.SetIFrameThresdRelative ( m_dlgFilter.GetMaxDiff() );
	m_avi.SetPFrameThreshold ( m_dlgFilter.GetMinDiff() );
}

//set the type of I-frame and P-frame.
void CExtractorDoc::OnOutput() 
{
    m_dlgOutput.DoModal();

	//set the type of outpur
	m_avi.SetIFrameType( m_dlgOutput.GetIFrameType() );
	m_avi.SetPFrameType( m_dlgOutput.GetPFrameType() );

	if(m_dlgOutput.CheckMaxIFrameIntvl())
		m_avi.SetIFrameInterval( m_dlgOutput.GetMaxIFrameInterval() );
	else
		m_avi.SetIFrameInterval(-1);

	if(m_dlgOutput.CheckProbe())
		m_avi.SetProbeInterval( m_dlgOutput.GetProbeInterval() );
	else
		m_avi.SetProbeInterval(-1);


}

//begian the process of extractor
void CExtractorDoc::OnButtonStart() 
{
	CWnd* pFrame = AfxGetMainWnd();
	CString strFinish;
	CDialog dlgMsg;
	if(m_strFileName.Compare("0")==1)
	{
		clock_t StartTime, FinishTime;
		double  Duration;

		StartTime = clock();

		m_avi.AviInit(m_strFileName);	//open an avi file according to the name, and get the information of its stream.
		m_avi.AviProcess(pFrame);		//main process to comparing every 2 frames, and show the rate of  process.
	
		FinishTime = clock();
		Duration = (double)(FinishTime - StartTime)/ CLOCKS_PER_SEC;

		strFinish.Format(" finished\n duration: %d seconds",(int)Duration);
		dlgMsg.MessageBox(strFinish);
	}

}

CMyAvi* CExtractorDoc::Getm_avi()
{
	return &this->m_avi;
}
