// PictureAreaMove.cpp : implementation file
//

#include "stdafx.h"
#include "CgDemoBy5000asi.h"
#include "PictureAreaMove.h"
#include "mydemo.h"
#include "imagereader.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPictureAreaMove dialog

#define MOVE_STOP  5

CPictureAreaMove::CPictureAreaMove(CWnd* pParent /*=NULL*/)
	: CDialog(CPictureAreaMove::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPictureAreaMove)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPictureAreaMove::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPictureAreaMove)
	DDX_Control(pDX, IDC_AREA_MOVE_SPEED_SEL_SCROLLBAR, m_SpeedSel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPictureAreaMove, CDialog)
	//{{AFX_MSG_MAP(CPictureAreaMove)
	ON_BN_CLICKED(IDC_AREA_MOVE_STOP, OnAreaMoveStop)
	ON_BN_CLICKED(IDC_AREA_MOVE_DOWN, OnAreaMoveDown)
	ON_BN_CLICKED(IDC_AREA_MOVE_LEFT, OnAreaMoveLeft)
	ON_BN_CLICKED(IDC_AREA_MOVE_RIGHT, OnAreaMoveRight)
	ON_BN_CLICKED(IDC_AREA_MOVE_UP, OnAreaMoveUp)
	ON_BN_CLICKED(IDC_PICTURE_AREA_MOVE_SEL_PIC, OnPictureAreaMoveSelPic)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*
   移动线程控制函数
*/

UINT MoveArea(LPVOID pParam)
{
	float fTemp1 = 100 ;
	float fTemp2 = 200 ;
	CString str;

	CPictureAreaMove* pMoveDlg = (CPictureAreaMove*)(pParam);	

	while(WaitForSingleObject(pMoveDlg->m_hExitEvent, 0) == WAIT_TIMEOUT)
	{		
		pMoveDlg->AreaMove();
		Seekfit_By5000asiplay_CgUpdateOneFrame(pMoveDlg->m_uCardNum);
	}

	return 0;

}

/////////////////////////////////////////////////////////////////////////////
// CPictureAreaMove message handlers

BOOL CPictureAreaMove::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CButton *pButton = (CButton*)( GetDlgItem( IDC_AREA_MOVE_STOP ) );
	pButton ->SetCheck( 1 );
	m_ulMoveDiretion = 0;
	m_ulMoveDirLast = 0;
	m_Left = 0 ;
	m_Up = 0 ;
	wrLong = NULL;
	m_nMoveStep = 1;
	m_SpeedSel.SetScrollRange( 1,20 );
	m_SpeedSel.SetScrollPos( m_nMoveStep );

	CString str;
	str.Format(_T("Speed Sel  %d  "),m_nMoveStep);
	GetDlgItem(IDC_STATIC_AREA_MOVE_SPEED_SEL)->SetWindowText(str);	

	m_VideoLine = 720;
	m_VideoCol = 576;

	wrLong = new ULONG[ BURST_LENGTH*BURST_NO ];

	m_hExitEvent = CreateEvent(	NULL,
		TRUE,	// Manual reset event
		FALSE,
		NULL
		) ;
	if (m_hExitEvent == NULL)
		return FALSE ;

	m_iMoving = 0;
	m_event = OpenEvent(EVENT_ALL_ACCESS, TRUE, m_strEventName);
	CWinThread * pWinThread;
	pWinThread = AfxBeginThread((AFX_THREADPROC)MoveArea,this);
	m_hMoveAreaThread = pWinThread->m_hThread;
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPictureAreaMove::OnAreaMoveStop() 
{
	// TODO: Add your control notification handler code here
	m_ulMoveDiretion = 0;

}

void CPictureAreaMove::OnAreaMoveUp() 
{
	// TODO: Add your control notification handler code here

	m_ulMoveDiretion = 1;


}



void CPictureAreaMove::OnAreaMoveDown() 
{
	// TODO: Add your control notification handler code here
	
	m_ulMoveDiretion = 2;	

}



void CPictureAreaMove::OnAreaMoveRight() 
{
	// TODO: Add your control notification handler code here

	m_ulMoveDiretion = 3;	
}

void CPictureAreaMove::OnAreaMoveLeft() 
{
	// TODO: Add your control notification handler code here
	m_ulMoveDiretion = 4;	

}

void CPictureAreaMove::OnOK() 
{
	CDialog::OnOK();
}

void CPictureAreaMove::OnPictureAreaMoveSelPic() 
{	
	CBitmap * pBitmap ;
	pBitmap = new CBitmap;
	CTgaReader tgaReader;
	BITMAP bm;

	CString strFileName;
	POSITION posFile;
	ULONG *pPicture,ulLeftDistance,ulUpDistance;
	unsigned int i,j,k;

	CString strErr;

	
	CFileDialog dlg(TRUE,
		NULL,
		_T("*.tga"),
        OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_NOREADONLYRETURN ,
		_T("Picture Files (*.tga) | *.tga | ")
        _T("All Files (*.*) | *.* ||"));

	i = 0;

	if ( dlg.DoModal()==IDOK )
	{
		posFile = dlg.GetStartPosition();
		while( posFile )
		{
			strFileName = dlg.GetNextPathName( posFile );
			tgaReader.LoadImage(strFileName,pBitmap);
			pBitmap->GetBitmap(&bm);
			pPicture = new ULONG[ bm.bmWidth*bm.bmHeight ];
			pBitmap ->GetBitmapBits(bm.bmWidth*bm.bmHeight*4,pPicture);

			if( BURST_LENGTH > bm.bmWidth )
				ulLeftDistance = ( BURST_LENGTH - bm.bmWidth ) / 2;
			else
				ulLeftDistance = 0;
			
			if( BURST_NO > bm.bmHeight )
				ulUpDistance = ( BURST_NO - bm.bmHeight ) / 2;
			else
				ulUpDistance = 0;

			for( j=0;j<BURST_NO;j++ )
			{
				if( ( j < ( bm.bmHeight + ulUpDistance ) ) && ( j >= ulUpDistance ))
				{
					for( k=0; k<BURST_LENGTH; k++ )
					if( ( k < ( bm.bmWidth + ulLeftDistance ) ) && ( k >= ulLeftDistance ) )
						wrLong[ j*BURST_LENGTH + k ] = ( pPicture[ ( j - ulUpDistance )*bm.bmWidth + k - ulLeftDistance ] );
					else
						wrLong[ j*BURST_LENGTH + k ] = 0;
				}
				else
					for( k=0; k<BURST_LENGTH; k++ )
						wrLong[ j*BURST_LENGTH + k ] = 0;

			}
			Sleep(100) ;

			for( j=0;j<BURST_NO;j++ )
				Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,&wrLong[ j*BURST_LENGTH ], m_uCardNum);
			while (!Seekfit_By5000asiplay_CgUpdateOneFrame(m_uCardNum))
				Sleep(5);
			delete pPicture;
		}

	}

	delete pBitmap;
}

void CPictureAreaMove::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	m_nMoveStep = m_SpeedSel.GetScrollPos();
	if( nSBCode == SB_LINELEFT )
		m_nMoveStep--;
	else
	if( nSBCode == SB_LINERIGHT )
		m_nMoveStep++;
	else
	if( nSBCode == SB_PAGELEFT )
		m_nMoveStep -= 4;
	else
	if( nSBCode == SB_PAGERIGHT )
		m_nMoveStep += 4;

	m_nMoveStep = min( 20,max( m_nMoveStep,1 ) );
	m_SpeedSel.SetScrollPos(m_nMoveStep);

	CString str;
	str.Format(_T("Speed Sel  %d  "),m_nMoveStep);
	
	GetDlgItem(IDC_STATIC_AREA_MOVE_SPEED_SEL)->SetWindowText(str);
	
	UpdateData();

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CPictureAreaMove::PostNcDestroy() 
{
	OnAreaMoveStop();
	m_iMoving = MOVE_STOP;

	SetEvent(m_hExitEvent);
	HANDLE handle[1] = {m_hMoveAreaThread};
	WaitForMultipleObjects(1, handle, TRUE, INFINITE);
	CloseHandle(m_hExitEvent);
	m_hExitEvent = NULL;

	CloseHandle(m_event);

	AreaMove();
	Seekfit_By5000asiplay_CgUpdateOneFrame(m_uCardNum);

	if (wrLong) delete wrLong;  
	wrLong = NULL;

	CDialog::PostNcDestroy();
}
/*
   视频区域移动函数，在线程中调用
   参数为场号，如果是奇数则写偶数场，如果是偶数则写奇数场
*/


void CPictureAreaMove::AreaMove()
{
    int j;
	ULONG test[720];	
	if(!wrLong)return;

	switch( m_ulMoveDiretion )
	{
		case 0:
			goto still;		//如果停止移动则静止显示
			break;

			case 1	:// Up
				m_Up += m_nMoveStep;
				m_Up = m_Up % m_VideoCol;
				for( j = 0 ;j<m_VideoCol;j+=2 )
				{						
				    if( ( j + m_Up ) < m_VideoCol )
					    memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
				    else
					    memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);					
				    Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
				}
				m_Up += m_nMoveStep;
				m_Up = m_Up % m_VideoCol;
				for( j = 1 ;j<m_VideoCol;j+=2 )
				{						
					if( ( j + m_Up ) < m_VideoCol )
						memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
					else
						memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);					
					Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
				}
				
				break;

			case 2	:// Down
				m_Up -= m_nMoveStep;
				if( m_Up < 0 )
					m_Up = m_Up + m_VideoCol;
				m_Up = m_Up % m_VideoCol;
				for( j = 0 ;j<m_VideoCol;j+=2 )
				{						
				    if( ( j + m_Up ) < m_VideoCol )
					    memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
				    else
				 	    memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);					
				    Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
				}
				m_Up -= m_nMoveStep;
				if( m_Up < 0 )
					m_Up = m_Up + m_VideoCol;
				m_Up = m_Up % m_VideoCol;
				for( j = 1 ;j<m_VideoCol;j+=2 )
				{						
					if( ( j + m_Up ) < m_VideoCol )
						memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
					else
						memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);					
					Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
				}
				
				break;

			case 3	:
				m_Left -= m_nMoveStep;
				if( m_Left < 0 )
					m_Left = 720 + m_Left;
				for( j = 0 ;j<m_VideoCol;j+=2 )
				{	
			        memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
			        memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					
			        Seekfit_By5000asiplay_CgSetRectangle(0,j ,720,1,test, m_uCardNum);	
				}
				
				m_Left -= m_nMoveStep;
				if( m_Left < 0 )
					m_Left = 720 + m_Left;
				for( j = 1 ;j<m_VideoCol;j+=2 )
				{	
					memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
					memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					
					Seekfit_By5000asiplay_CgSetRectangle(0,j ,720,1,test, m_uCardNum);	
				}

				break;

			case 4	:
				m_Left += m_nMoveStep;
				m_Left = m_Left % 720;

				for( j = 0 ;j<m_VideoCol;j+=2 )
				{	
			        memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
			        memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					
			        Seekfit_By5000asiplay_CgSetRectangle(0,j ,720,1,test, m_uCardNum);	
				}		
				
				m_Left += m_nMoveStep;
				m_Left = m_Left % 720;

				for( j = 1 ;j<m_VideoCol;j+=2 )
				{	
					memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
					memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					
					Seekfit_By5000asiplay_CgSetRectangle(0,j ,720,1,test, m_uCardNum);	
				}		

				break;
	}
	m_ulMoveDirLast = m_ulMoveDiretion;

	return;

still:    
	switch( m_ulMoveDirLast )
	{
		case 0:			
		break;
		case 1	:// Up
			m_Up += m_nMoveStep;
			m_Up = m_Up % m_VideoCol;
			for( j = 0 ;j<m_VideoCol;j++ )
			{
				if( ( j + m_Up ) < m_VideoCol )
					memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
				else
					memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);

				Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
			}

			break;

		case 2	:// Down
			m_Up -= m_nMoveStep;
			if( m_Up < 0 )
				m_Up = m_Up + m_VideoCol;
			m_Up = m_Up % m_VideoCol;
			for( j = 0 ;j<m_VideoCol;j++ )
			{						
				if( ( j + m_Up ) < m_VideoCol )
					memcpy(test,&wrLong[( j + m_Up )*720],720*4);						
				else
					memcpy(test,&wrLong[( j + m_Up-m_VideoCol )*720],720*4);					

				Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
			}

			break;

		case 3	:
			m_Left -= m_nMoveStep;
			if( m_Left < 0 )
				m_Left = 720 + m_Left;
			for( j = 0 ;j<m_VideoCol;j++ )
			{	
				memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
				memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					

				Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);
			}
			break;

		case 4	:
			m_Left += m_nMoveStep;
			m_Left = m_Left % 720;

			for( j = 0 ;j<m_VideoCol;j++ )
			{	
				memcpy(test,&wrLong[ j*720+m_Left],(720-m_Left)*4);
				memcpy(&test[720-m_Left],&wrLong[ j*720],m_Left*4);					

				Seekfit_By5000asiplay_CgSetRectangle(0,j,720,1,test, m_uCardNum);	
			}		
			break;
	}		 
	m_ulMoveDirLast = 0;
}