
#include "ICubeInterface.h"



#define GICubeControlInterface {0xa2a937b4, 0x1142, 0x4a2b, {0xbf, 0x16, 0x29, 0x40, 0xf1, 0x78, 0xc9, 0x78}}

GUID ICubeControlInterface = GICubeControlInterface;

ROI_PARAMS roi_params;
RGB_PARAMS rgb_params;

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Interface functions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Get Data (ICubeControl-Parameter)
//
HRESULT GetICubeControl(IBaseFilter * mpCap, long Property,long *lValue)
{
 	HRESULT hr;

    if( !mpCap )
        return E_NOINTERFACE ;

	IAMCustomInterface *iVC=0;

    hr=mpCap->QueryInterface(ICubeControlInterface, (void **)&iVC);
	if( SUCCEEDED( hr ) )
    {
   	   hr=  iVC->GetControl( Property,lValue);
	    if( hr != S_OK)
		{
		   iVC->Release();
		   return hr;
		}
	iVC->Release();
    }

return hr;
}
//---------------------------------------------------------------------------
// Set Data (ICubeControl-Parameter)
//
HRESULT SetICubeControl(IBaseFilter * mpCap,long Property,long lValue)
{
	HRESULT hr;

    if( !mpCap )
        return E_NOINTERFACE ;

 	IAMCustomInterface *iVC=0;

    hr=mpCap->QueryInterface(ICubeControlInterface, (void **)&iVC);
	if( SUCCEEDED( hr ) )
    {
   	   hr=  iVC->SetControl( Property,lValue);
	    if( hr != S_OK)
		{
		   iVC->Release();
		   return hr;
		}
	iVC->Release();
    }
 
return hr;
}
//---------------------------------------------------------------------------
// Get Range (ICubeControl-Parameter)
//
HRESULT GetICubeControlRange(IBaseFilter * mpCap,long Property,long *lMin,long *lDef,long *lMax)
{
	HRESULT hr;

    if( !mpCap )
       return E_NOINTERFACE ;

 	IAMCustomInterface *iVC=0;

    hr=mpCap->QueryInterface(ICubeControlInterface, (void **)&iVC);
	if( SUCCEEDED( hr ) )
    {
   	   hr=  iVC->GetControlRange(Property,lMin,lDef,lMax);
	    if( hr != S_OK)
		{
		   iVC->Release();
		   return hr;
		}
	iVC->Release();
    }

return hr;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Helper functions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Set Region of Interest
//
BOOL ICubeSetROI(IBaseFilter * mpCap,ROI_PARAMS roi_param,int nMode)
{
	HRESULT hr;

    if( !mpCap )
       return E_NOINTERFACE ;

    if(nMode==MODE_ON)
    {
        // Set ROI parameters only in "On-Mode" necessary

        hr=SetICubeControl(mpCap,REG_ROI_HOR_POS,roi_param.nROIPosHor);
        if( hr != S_OK)
           return(FALSE);
           
        hr=SetICubeControl(mpCap,REG_ROI_VER_POS,roi_param.nROIPosVer);
        if( hr != S_OK)
            return(FALSE);

        hr=SetICubeControl(mpCap,REG_ROI_HOR_SIZE,roi_param.nROISizeHor);
        if( hr != S_OK)
            return(FALSE);

        hr=SetICubeControl(mpCap,REG_ROI_VER_SIZE,roi_param.nROISizeVer);
        if( hr != S_OK)
            return(FALSE);
   }

   /*   nMode=MODE_ON: Set Roi-format as default format (format_0).
        You can do this also with IAMStreamConfig::SetFormat()
        ROI-format is the last available format.

        nMode=MODE_OFF: Set format_1 as default format (format_0). */
   
   hr=SetICubeControl(mpCap,REG_ROI,nMode);
    if( hr != S_OK)
        return(FALSE);

return(TRUE);
}
//---------------------------------------------------------------------------
//
BOOL ICubeWhiteBalance(IBaseFilter * mpCap,PRGB_PARAMS rgb_param,int nMode)
{
	HRESULT hr;
    long tmp=0;
    int stat;

    nMode=0;                // Currently only "one push mode" implemented !

    if( !mpCap )
       return E_NOINTERFACE ;

    hr=SetICubeControl(mpCap,REG_WHITE_BALANCE,nMode);    // do one push
    if( hr != S_OK)
        return(FALSE);

    if(nMode==0)            
	{
	unsigned int rdyCnt0=0;
	while(tmp==0)          // check wb finished
		{
		Sleep(1);
		rdyCnt0++;
		if(rdyCnt0>1000)
			 return(FALSE); // timeout

		stat =GetICubeControl(mpCap,REG_WHITE_BALANCE,&tmp);
		if(stat!=0)
			 return(FALSE);
		}

    // Get new RGB values

	stat =GetICubeControl(mpCap,REG_RED,&rgb_param->nRed);
	if(stat!=0)
		 return(FALSE);

	stat =GetICubeControl(mpCap,REG_GREEN,&rgb_param->nGreen);
	if(stat!=0)
		 return(FALSE);

	stat =GetICubeControl(mpCap,REG_BLUE,&rgb_param->nBlue);
	if(stat!=0)
		 return(FALSE);
    }

return(TRUE);
}
//---------------------------------------------------------------------------






