// ICubeSDKVidControlDlg.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "ICubeSDKSample.h"
#include "ICubeSDKVidControlDlg.h"
#include "NET_ICube_API.h"

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

extern int g_iCurrentCam;


#define SHIFT_VAL   12
#define LOW_MASK     0x00000FFF
#define HIGH_MASK    0x000FF000


static struct symbolic_list GammaModelist[] = {
	{ " ITU709" },
	{ " sRGB "	},
	{ " DEFAULT"},
	{ NULL }
};
/////////////////////////////////////////////////////////////////////////////
// Dialogfeld CICubeSDKVidControlDlg 


CICubeSDKVidControlDlg::CICubeSDKVidControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CICubeSDKVidControlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CICubeSDKVidControlDlg)
		// HINWEIS: Der Klassen-Assistent fügt hier Elementinitialisierung ein
	//}}AFX_DATA_INIT

	m_pParent = pParent;
	bExposureSliderH = false;
}


void CICubeSDKVidControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CICubeSDKVidControlDlg)
	DDX_Control(pDX, IDC_COMBO_GAMMA_MODE, m_Combo_GammaMode);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CICubeSDKVidControlDlg, CDialog)
	//{{AFX_MSG_MAP(CICubeSDKVidControlDlg)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_DEFAULT, OnButtonDefault)
	ON_BN_CLICKED(IDC_BUTTON_WB_ONE_PUSH, OnButtonWbOnePush)
	ON_BN_CLICKED(IDC_CHECK_EXPTIME, OnCheckExposuretime)
	ON_BN_CLICKED(IDC_CHECK_BLACKLEVEL, OnCheckBlacklevel)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_COLOR_PROCESSING, OnCheckEnableColorProcessing)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_GAMMA, OnCheckEnableGamma)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_COLOR_CORRECTION, OnCheckEnableColorCorrection)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_TNR, OnCheckEnableTnr)
	ON_BN_CLICKED(IDC_RADIO_BAYER_3x3, OnRADIOBAYER3x3)
	ON_BN_CLICKED(IDC_RADIO_BAYER_5x5, OnRADIOBAYER5x5)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_INVERT_PIXEL, OnCheckEnableInvertPixel)
	ON_CBN_SELCHANGE(IDC_COMBO_GAMMA_MODE, OnSelchangeComboGammaMode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Behandlungsroutinen für Nachrichten CICubeSDKVidControlDlg 


//---------------------------------------------------------------------
// Parameter Value is in milli-seconds.
// Be aware that every change of clock
// and width will lead to different exposure values
//
void CICubeSDKVidControlDlg::ShowExposureMsec(void)
{
CEdit		*pEdit;
int nEnableL=0,nEnableH=0;

	if(IsDlgButtonChecked(IDC_CHECK_EXPTIME))		// Auto exposure
	{
		SetDlgItemText ( IDC_EDIT_EXPTIME, "auto");
	}
	else
	{
		float value;
        int nStat = ICubeSDK_GetExposure(g_iCurrentCam,&value);
		if ( nStat != IC_SUCCESS )
			AfxMessageBox("Error GetExposure!");
        CString sTmp;
		sTmp.Format("%.2f",value);
		SetDlgItemText ( IDC_EDIT_EXPTIME, sTmp);

		nEnableL = 1;
		nEnableH = bExposureSliderH;
	}

	pEdit = (CEdit *)GetDlgItem(IDC_SLIDER_EXPTIME_L);	// Disable sliders, when auto exposure
	pEdit->EnableWindow(nEnableL);

	pEdit = (CEdit *)GetDlgItem(IDC_SLIDER_EXPTIME_H);
	pEdit->EnableWindow(nEnableH);

}
//---------------------------------------------------------------------
//
void CICubeSDKVidControlDlg::ShowGamma(int nVal)
{
	double dVal=(float)((float)nVal/(float)DEFAULT_GAMMA);
	CString sTmp;
	sTmp.Format("%.2f",dVal);
	SetDlgItemText ( IDC_EDIT_GAMMA, sTmp);
}
//---------------------------------------------------------------------
// Initialize Video controls
//

int CICubeSDKVidControlDlg::InitControl(int nType,int ID_SLDR,int ID_EDIT,int ID_AUTO,
										int ID_ONEPUSH,int ID_SLDR_H,int ID_ENABLE)
{
unsigned long nVal;
PARAM_PROPERTY param_prop;

CSliderCtrl *pSlider;
CButton		*pCheckBox;
CButton		*pButton;
CEdit		*pEdit;

    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,nType,&param_prop)!= IC_SUCCESS )
		return IC_ERROR;

	if(ICubeSDK_GetCamParameter(g_iCurrentCam,nType,&nVal)!= IC_SUCCESS )
		return IC_ERROR;

	// Control supports auto-mode
	if(param_prop.bAuto){
		int bAuto;
		if(ICubeSDK_GetParamAuto(g_iCurrentCam,nType,&bAuto)!= IC_SUCCESS )
			return IC_ERROR;

		pCheckBox = (CButton *)GetDlgItem(ID_AUTO);
		pCheckBox->EnableWindow(param_prop.bEnabled);		// Control is enabled
		pCheckBox->SetCheck(bAuto);
	}

	// Control supports one push-mode
	if(param_prop.bOnePush){
		pButton = (CButton *)GetDlgItem(ID_ONEPUSH);
		pButton->EnableWindow(param_prop.bEnabled);			// Control is enabled
	}
	// Control has Slider
	if(ID_SLDR!=NULL)				
	{
		pEdit = (CEdit *)GetDlgItem(ID_EDIT);
		pEdit->EnableWindow(param_prop.bEnabled);			// Control is enabled
		
		if(nType==REG_GAMMA)
			ShowGamma(nVal);		// show gamma as double
		else if(nType==REG_EXPOSURE_TIME){
			if(param_prop.nMax>0x7FFF)
				bExposureSliderH = true;					// save status
			ShowExposureMsec();		// show exposure in milli seconds
		}
		else
			SetDlgItemInt(ID_EDIT,nVal,FALSE); 

		if(param_prop.nMax>0x7FFF)	// MaxVal doesnt fit into one slider
		{
			pSlider = (CSliderCtrl*)GetDlgItem(ID_SLDR_H);	// High slider
			if(!param_prop.bAuto)
				pSlider->EnableWindow(param_prop.bEnabled);	// enable control
			pSlider->SetRange(0,(param_prop.nMax & HIGH_MASK)>>SHIFT_VAL);
			pSlider->SetPos((nVal&HIGH_MASK)>>SHIFT_VAL);

			param_prop.nMax=LOW_MASK-1;						// Low slider values
			nVal=nVal&LOW_MASK;
		}

		pSlider = (CSliderCtrl*)GetDlgItem(ID_SLDR);
		if(!param_prop.bAuto)
			pSlider->EnableWindow(param_prop.bEnabled);		// enable control 
		pSlider->SetRange(param_prop.nMin,param_prop.nMax);
		pSlider->SetPos(nVal);
	}

	// (enable ColorProcessing,enable Gamma,enable ColorCorrection matrix)
	if(ID_ENABLE!=NULL)				
	{
		pCheckBox = (CButton *)GetDlgItem(ID_ENABLE);
		pCheckBox->EnableWindow(param_prop.bEnabled);	// Control is enabled
		pCheckBox->SetCheck(nVal);
	}


return IC_SUCCESS;
}
//---------------------------------------------------------------------
//
int CICubeSDKVidControlDlg::SetControl(int nType,int ID_EDIT,int nNewVal,int nNewValH,bool b2Vals)
{
unsigned long nVal=nNewVal;

	if(b2Vals==true)	// Control has 2 sliders (Exposure)		
	{					// if Auto exposure is ON, changing exposure+gain values is not allowed !
		nVal=(nNewValH<<SHIFT_VAL) + nNewVal;
	}
	
	int stat= ICubeSDK_SetCamParameter(g_iCurrentCam,nType,nVal);
	if (stat!= IC_SUCCESS) return stat;

	if(nType==REG_GAMMA)
		ShowGamma(nVal);		// show gamma as double
	else if(nType==REG_EXPOSURE_TIME)
		ShowExposureMsec();
	else
		SetDlgItemInt(ID_EDIT,nVal,FALSE);


return IC_SUCCESS;
}
//---------------------------------------------------------------------
//
int CICubeSDKVidControlDlg::SetControlToDefault(int nType,int ID_SLDR,int ID_EDIT,
												int ID_AUTO,int ID_SLDR_H,int ID_ENABLE)
{
PARAM_PROPERTY param_prop;
CSliderCtrl *pSlider;
CButton		*pCheckBox;

    if (ICubeSDK_GetCamParameterRange(g_iCurrentCam,nType,&param_prop)!= IC_SUCCESS )
		return IC_ERROR;
 
	if(param_prop.bEnabled)				// Control is supported
	{
		if(param_prop.bAuto)			// Control supports auto-mode
		{
			// Set auto-parameter to default
			if(ICubeSDK_SetParamAutoDef(g_iCurrentCam,nType)!= IC_SUCCESS )
				return IC_ERROR;
			// get the value for the checkbox
			int bAuto;
			if(ICubeSDK_GetParamAuto(g_iCurrentCam,nType,&bAuto)!= IC_SUCCESS )
				return IC_ERROR;

			pCheckBox = (CButton *)GetDlgItem(ID_AUTO);
			pCheckBox->SetCheck(bAuto);
		}

		if(ID_SLDR!=NULL)				// Control has Slider
		{
			if (ICubeSDK_SetCamParameter(g_iCurrentCam,nType,param_prop.nDef)!= IC_SUCCESS )
				return IC_ERROR;

			pSlider = (CSliderCtrl*)GetDlgItem(ID_SLDR);
			pSlider->SetPos(param_prop.nDef);

			if(ID_SLDR_H!=NULL)			// Control has 2 sliders (Exposure)
			{
				pSlider = (CSliderCtrl*)GetDlgItem(ID_SLDR_H);
				pSlider->SetPos(param_prop.nDef&HIGH_MASK);	
			}
 			
			if(nType==REG_GAMMA)
				ShowGamma(param_prop.nDef);		// show gamma as double
			else if(nType==REG_EXPOSURE_TIME)
				ShowExposureMsec();
			else
				SetDlgItemInt(ID_EDIT,param_prop.nDef,FALSE);

		}

		// (enable ColorProcessing,enable Gamma,enable ColorCorrection matrix)
		if(ID_ENABLE!=NULL)				
		{
			if (ICubeSDK_SetCamParameter(g_iCurrentCam,nType,param_prop.nDef)!= IC_SUCCESS )
				return IC_ERROR;

			pCheckBox = (CButton *)GetDlgItem(ID_ENABLE);
			pCheckBox->SetCheck(param_prop.nDef);
		}
	}

return IC_SUCCESS;
}
//---------------------------------------------------------------------

BOOL CICubeSDKVidControlDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// These controls are belonging to the ColorProcessing module
	// they can be en/disabled through REG_COLOR_PROCCESSING
	// if non of these controls are used, disable to save CPU load

	if (InitControl(REG_COLOR_PROCESSING,NULL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_COLOR_PROCESSING)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_BRIGHTNESS,IDC_SLIDER_BRIGHTNESS,IDC_EDIT_BRIGHTNESS)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_CONTRAST,IDC_SLIDER_CONTRAST,IDC_EDIT_CONTRAST)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_SATURATION,IDC_SLIDER_SATURATION,IDC_EDIT_SATURATION)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_HUE,IDC_SLIDER_HUE,IDC_EDIT_HUE)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// These controls are belonging to the Gamma module
	// they can be en/disabled through REG_GAMMA_ENABLE
	// if non of these controls are used, disable to save CPU load

	if (InitControl(REG_GAMMA_ENABLE,NULL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_GAMMA)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_GAMMA,IDC_SLIDER_GAMMA,IDC_EDIT_GAMMA)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// Gamma mode	
	PARAM_PROPERTY param_prop;
	unsigned long nVal;
    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,REG_GAMMA_MODE,&param_prop)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter Range!");
	if(ICubeSDK_GetCamParameter(g_iCurrentCam,REG_GAMMA_MODE,&nVal)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	CButton		*pBox;
	pBox = (CButton *)GetDlgItem(IDC_COMBO_GAMMA_MODE);
	pBox->EnableWindow(param_prop.bEnabled);				// Control is enabled

	for(unsigned int i=param_prop.nMin;i<=param_prop.nMax;i++)	
		m_Combo_GammaMode.InsertString(i,GammaModelist[i].name);
	m_Combo_GammaMode.SetCurSel(nVal);
	
	//------------------------------------------------------------------
	// more controls 

	if (InitControl(REG_BLACKLEVEL,IDC_SLIDER_BLACKLEVEL,IDC_EDIT_BLACKLEVEL,IDC_CHECK_BLACKLEVEL)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_EXPOSURE_TIME,IDC_SLIDER_EXPTIME_L,IDC_EDIT_EXPTIME,IDC_CHECK_EXPTIME,NULL,IDC_SLIDER_EXPTIME_H)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_EXPOSURE_TARGET,IDC_SLIDER_EXPOSURETARGET,IDC_EDIT_EXPOSURETARGET)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_GAIN,IDC_SLIDER_GAIN,IDC_EDIT_GAIN)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_PLL,IDC_SLIDER_CLOCK,IDC_EDIT_CLOCK)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_AQU_FRAMERATE,IDC_SLIDER_AQU_FRAMERATE,IDC_EDIT_AQU_FRAMERATE)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	if (InitControl(REG_WHITE_BALANCE,0,0,IDC_CHECK_WB,IDC_BUTTON_WB_ONE_PUSH)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// RGB Gain
	if (InitControl(REG_RED,IDC_SLIDER_RED,IDC_EDIT_RED)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_GREEN,IDC_SLIDER_GREEN,IDC_EDIT_GREEN)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_BLUE,IDC_SLIDER_BLUE,IDC_EDIT_BLUE)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// RGB Offset
	if (InitControl(REG_RED_OFFSET,IDC_SLIDER_RED_OFFSET,IDC_EDIT_RED_OFFSET)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_GREEN_OFFSET,IDC_SLIDER_GREEN_OFFSET,IDC_EDIT_GREEN_OFFSET)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_BLUE_OFFSET,IDC_SLIDER_BLUE_OFFSET,IDC_EDIT_BLUE_OFFSET)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");


	if (InitControl(REG_COLOR_CORRECTION,NULL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_COLOR_CORRECTION)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_TNR,NULL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_TNR)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_INVERT_PIXEL,NULL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_INVERT_PIXEL)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// Bayer conversion
    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,REG_BAYER_CONVERSION,&param_prop)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter Range!");
	if(ICubeSDK_GetCamParameter(g_iCurrentCam,REG_BAYER_CONVERSION,&nVal)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	pBox = (CButton *)GetDlgItem(IDC_RADIO_BAYER_3x3);
	pBox->EnableWindow(param_prop.bEnabled);				// Control is enabled
	if(nVal == DE_BAYER_3x3) 
		pBox->SetCheck(1);
	pBox = (CButton *)GetDlgItem(IDC_RADIO_BAYER_5x5);
	pBox->EnableWindow(param_prop.bEnabled);				// Control is enabled
	if(nVal == DE_BAYER_5x5) 
		pBox->SetCheck(1);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX-Eigenschaftenseiten sollten FALSE zurückgeben
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
int nStat;
int nNewVal,nNewVal2;

	CSliderCtrl *pSlider = (CSliderCtrl *)pScrollBar;

	nNewVal=pSlider->GetPos();

	switch(pSlider->GetDlgCtrlID())
	{
	case IDC_SLIDER_BRIGHTNESS:
		nStat=SetControl(REG_BRIGHTNESS,IDC_EDIT_BRIGHTNESS,nNewVal);
		break;
	case IDC_SLIDER_CONTRAST:
		nStat=SetControl(REG_CONTRAST,IDC_EDIT_CONTRAST,nNewVal);
		break;
	case IDC_SLIDER_SATURATION:
		nStat=SetControl(REG_SATURATION,IDC_EDIT_SATURATION,nNewVal);
		break;
	case IDC_SLIDER_HUE:
		nStat=SetControl(REG_HUE,IDC_EDIT_HUE,nNewVal);
		break;
	case IDC_SLIDER_GAMMA:
		nStat=SetControl(REG_GAMMA,IDC_EDIT_GAMMA,nNewVal);
		break;
	case IDC_SLIDER_BLACKLEVEL:
		nStat=SetControl(REG_BLACKLEVEL,IDC_EDIT_BLACKLEVEL,nNewVal);
		break;
	case IDC_SLIDER_EXPTIME_L:
		pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_EXPTIME_H);	// get 2nd slider
		nNewVal2=pSlider->GetPos();
		nStat=SetControl(REG_EXPOSURE_TIME,IDC_EDIT_EXPTIME,nNewVal,nNewVal2,true);
		break;
	case IDC_SLIDER_EXPTIME_H:
		pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_EXPTIME_L);	// get 2nd slider
		nNewVal2=pSlider->GetPos();
		nStat=SetControl(REG_EXPOSURE_TIME,IDC_EDIT_EXPTIME,nNewVal2,nNewVal,true);
		break;
	case IDC_SLIDER_EXPOSURETARGET:
		nStat=SetControl(REG_EXPOSURE_TARGET,IDC_EDIT_EXPOSURETARGET,nNewVal);
		break;
	case IDC_SLIDER_GAIN:
		nStat=SetControl(REG_GAIN,IDC_EDIT_GAIN,nNewVal);
		break;
	case IDC_SLIDER_CLOCK:
		nStat=SetControl(REG_PLL,IDC_EDIT_CLOCK,nNewVal);
		break;
	case IDC_SLIDER_AQU_FRAMERATE:
		nStat=SetControl(REG_AQU_FRAMERATE,IDC_EDIT_AQU_FRAMERATE,nNewVal);
		break;
	case IDC_SLIDER_RED:
		nStat=SetControl(REG_RED,IDC_EDIT_RED,nNewVal);
		break;
	case IDC_SLIDER_GREEN:
		nStat=SetControl(REG_GREEN,IDC_EDIT_GREEN,nNewVal);
		break;
	case IDC_SLIDER_BLUE:
		nStat=SetControl(REG_BLUE,IDC_EDIT_BLUE,nNewVal);
		break;
	case IDC_SLIDER_RED_OFFSET:
		nStat=SetControl(REG_RED_OFFSET,IDC_EDIT_RED_OFFSET,nNewVal);
		break;
	case IDC_SLIDER_GREEN_OFFSET:
		nStat=SetControl(REG_GREEN_OFFSET,IDC_EDIT_GREEN_OFFSET,nNewVal);
		break;
	case IDC_SLIDER_BLUE_OFFSET:
		nStat=SetControl(REG_BLUE_OFFSET,IDC_EDIT_BLUE_OFFSET,nNewVal);
		break;
	default:
		break;
	}	

	if ( nStat != IC_SUCCESS )
	{
		if( nStat == IC_IF_NOT_ENABLED )
			AfxMessageBox("Error: Parameter not enabled!");
		else if( nStat == IC_COLOR_CAM_ONLY )
			AfxMessageBox("Error: Parameter only for color models!");
		else
			AfxMessageBox("Error: Set Parameter!");
	}

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

void CICubeSDKVidControlDlg::OnButtonDefault() 
{
	// ColorProcessing module
	if (SetControlToDefault(REG_COLOR_PROCESSING,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_COLOR_PROCESSING)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_BRIGHTNESS,IDC_SLIDER_BRIGHTNESS,IDC_EDIT_BRIGHTNESS)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_CONTRAST,IDC_SLIDER_CONTRAST,IDC_EDIT_CONTRAST)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_SATURATION,IDC_SLIDER_SATURATION,IDC_EDIT_SATURATION)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_HUE,IDC_SLIDER_HUE,IDC_EDIT_HUE)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");

	// Gamma module
	if (SetControlToDefault(REG_GAMMA_ENABLE,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_GAMMA)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_GAMMA,IDC_SLIDER_GAMMA,IDC_EDIT_GAMMA)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	PARAM_PROPERTY param_prop;
	CButton		*pBox;
    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,REG_GAMMA_MODE,&param_prop)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter Range!");
	if(param_prop.bEnabled)	// only for color models
	{
		if(ICubeSDK_SetCamParameter(g_iCurrentCam,REG_GAMMA_MODE,param_prop.nDef)!= IC_SUCCESS )
			AfxMessageBox("Error Set Parameter!");
		m_Combo_GammaMode.SetCurSel(param_prop.nDef);
	}

	if (SetControlToDefault(REG_BLACKLEVEL,IDC_SLIDER_BLACKLEVEL,IDC_EDIT_BLACKLEVEL,IDC_CHECK_BLACKLEVEL)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_EXPOSURE_TIME,IDC_SLIDER_EXPTIME_L,IDC_EDIT_EXPTIME,IDC_CHECK_EXPTIME,IDC_SLIDER_EXPTIME_H)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_EXPOSURE_TARGET,IDC_SLIDER_EXPOSURETARGET,IDC_EDIT_EXPOSURETARGET)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_GAIN,IDC_SLIDER_GAIN,IDC_EDIT_GAIN)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_PLL,IDC_SLIDER_CLOCK,IDC_EDIT_CLOCK)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_AQU_FRAMERATE,IDC_SLIDER_AQU_FRAMERATE,IDC_EDIT_AQU_FRAMERATE)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_WHITE_BALANCE,NULL,NULL,IDC_CHECK_WB)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_RED,IDC_SLIDER_RED,IDC_EDIT_RED)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_GREEN,IDC_SLIDER_GREEN,IDC_EDIT_GREEN)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_BLUE,IDC_SLIDER_BLUE,IDC_EDIT_BLUE)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_RED_OFFSET,IDC_SLIDER_RED_OFFSET,IDC_EDIT_RED_OFFSET)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_GREEN_OFFSET,IDC_SLIDER_GREEN_OFFSET,IDC_EDIT_GREEN_OFFSET)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");
	if (SetControlToDefault(REG_BLUE_OFFSET,IDC_SLIDER_BLUE_OFFSET,IDC_EDIT_BLUE_OFFSET)!= IC_SUCCESS )	
		AfxMessageBox("Error Set Default Parameter!");


	if (SetControlToDefault(REG_COLOR_CORRECTION,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_COLOR_CORRECTION)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_TNR,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_TNR)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");

	if (SetControlToDefault(REG_INVERT_PIXEL,NULL,NULL,NULL,NULL,IDC_CHECK_ENABLE_INVERT_PIXEL)!= IC_SUCCESS )		
		AfxMessageBox("Error Set Default Parameter!");

	// Bayer conversion
    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,REG_BAYER_CONVERSION,&param_prop)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter Range!");

	if(ICubeSDK_SetCamParameter(g_iCurrentCam,REG_BAYER_CONVERSION,param_prop.nDef)!= IC_SUCCESS )
		AfxMessageBox("Error Set Parameter!");

	pBox = (CButton *)GetDlgItem(IDC_RADIO_BAYER_3x3);
	if(param_prop.nDef == DE_BAYER_3x3) 
		pBox->SetCheck(1);
	else
		pBox->SetCheck(0);
	pBox = (CButton *)GetDlgItem(IDC_RADIO_BAYER_5x5);
	if(param_prop.nDef == DE_BAYER_5x5) 
		pBox->SetCheck(1);
	else
		pBox->SetCheck(0);

}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCancel() 
{
	m_pParent->PostMessage(WM_VIDCONTROL_DLG,WM_CANCELMODE);
	CDialog::OnCancel();
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnButtonWbOnePush() 
{													// White balance one push
	unsigned long nVal=0;

	if (ICubeSDK_SetParamOnePush(g_iCurrentCam,REG_WHITE_BALANCE)!= IC_SUCCESS )
		AfxMessageBox("Error One Push");

	Sleep(100);
	unsigned int rdyCnt0=0;
	while(nVal==0)				// check for finished
	{
		Sleep(1);		
		rdyCnt0++;
		if(rdyCnt0>1000){
			AfxMessageBox("Error: timeout");
			return;
		}
		if(ICubeSDK_GetCamParameter(g_iCurrentCam,REG_WHITE_BALANCE,&nVal)!= IC_SUCCESS )
		{
			AfxMessageBox("Error: Set Default Parameter!");
			return;
		}
	}
	// update rgb parameter
	if (InitControl(REG_RED,IDC_SLIDER_RED,IDC_EDIT_RED)!= IC_SUCCESS )	
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_GREEN,IDC_SLIDER_GREEN,IDC_EDIT_GREEN)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_BLUE,IDC_SLIDER_BLUE,IDC_EDIT_BLUE)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	// update gain parameter
	if (InitControl(REG_GAIN,IDC_SLIDER_GAIN,IDC_EDIT_GAIN)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckExposuretime() 
{
	unsigned long nVal = OFF;
	// if Auto exposure is ON, changing exposure is not allowed
	// (1044CU/BU) changing gain  values is not allowed !
	if(IsDlgButtonChecked(IDC_CHECK_EXPTIME))		// Auto exposure
		nVal = ON;
	if (ICubeSDK_SetParamAuto(g_iCurrentCam,REG_EXPOSURE_TIME,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error Auto");

	ShowExposureMsec();
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckBlacklevel() 
{
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_BLACKLEVEL))		// Auto blacklevel
		nVal = ON;
	if (ICubeSDK_SetParamAuto(g_iCurrentCam,REG_BLACKLEVEL,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error Auto");
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckEnableColorProcessing() 
{
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_COLOR_PROCESSING))	
		nVal = ON;
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_COLOR_PROCESSING,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");

	// Reinit Controls
	if (InitControl(REG_BRIGHTNESS,IDC_SLIDER_BRIGHTNESS,IDC_EDIT_BRIGHTNESS)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_CONTRAST,IDC_SLIDER_CONTRAST,IDC_EDIT_CONTRAST)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_SATURATION,IDC_SLIDER_SATURATION,IDC_EDIT_SATURATION)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	if (InitControl(REG_HUE,IDC_SLIDER_HUE,IDC_EDIT_HUE)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckEnableGamma() 
{
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_GAMMA))	
		nVal = ON;
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_GAMMA_ENABLE,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");

	// Reinit Controls
	if (InitControl(REG_GAMMA,IDC_SLIDER_GAMMA,IDC_EDIT_GAMMA)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");

	PARAM_PROPERTY param_prop;
    if(ICubeSDK_GetCamParameterRange(g_iCurrentCam,REG_GAMMA_MODE,&param_prop)!= IC_SUCCESS )
		AfxMessageBox("Error Get Parameter!");
	CButton		*pComboBox;
	pComboBox = (CButton *)GetDlgItem(IDC_COMBO_GAMMA_MODE);
	pComboBox->EnableWindow(param_prop.bEnabled);				

}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckEnableColorCorrection() 
{
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_COLOR_CORRECTION))	
		nVal = ON;
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_COLOR_CORRECTION,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");	
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckEnableTnr() 
{
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_TNR))	
		nVal = ON;
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_TNR,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");	
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnRADIOBAYER3x3() 
{
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_BAYER_CONVERSION,DE_BAYER_3x3)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");		
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnRADIOBAYER5x5() 
{
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_BAYER_CONVERSION,DE_BAYER_5x5)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");		
	
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnCheckEnableInvertPixel() 
{
	// only for bw models
	unsigned long nVal = OFF;
	if(IsDlgButtonChecked(IDC_CHECK_ENABLE_INVERT_PIXEL))	
		nVal = ON;
	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_INVERT_PIXEL,nVal)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");	
	
}
//---------------------------------------------------------------------

void CICubeSDKVidControlDlg::OnSelchangeComboGammaMode() 
{
	unsigned long lGammaMode;
	lGammaMode =m_Combo_GammaMode.GetCurSel();	

	if (ICubeSDK_SetCamParameter(g_iCurrentCam,REG_GAMMA_MODE,lGammaMode)!= IC_SUCCESS )
		AfxMessageBox("Error ICubeSDK_SetCamParameter");	
}
//---------------------------------------------------------------------
