#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <..\Originlab\matdata_utils.h>

//put additional include files here
#define TEMP_IMAGES_OBJECTS "TempForMerge"
#include <image_utils.h>
//put your own support static functions here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/19/09 CHECK_MATRIX_OBJECT_VALID_BEFORE_USE						*
 *------------------------------------------------------------------------------*/

static bool _extract_data_from_image_channel(Image& img, int nChannel)
{
	
	pBITMAPHANDLE pLBmpSrc = (pBITMAPHANDLE)img.GetLBmp();
	
	MatrixPage mp(TEMP_IMAGES_OBJECTS);
	MatrixLayer ml = mp.Layers(0);
	MatrixObject mo = ml.MatrixObjects(nChannel - 1);	
	
	if(is_image_grayscale(pLBmpSrc))
	{		
		img.CopyTo(mo);
	}	
	
	if( pLBmpSrc )
	{	
		pBITMAPHANDLE pbhR, pbhG, pbhB;
		pbhR = ol_new_bitmap();
		pbhG = ol_new_bitmap();
		pbhB = ol_new_bitmap();
		int nRet = ol_color_separate_bitmap(pLBmpSrc, COLORSEP_RGB, pbhB, pbhG, pbhR);
		if( SUCCESS == nRet )
		{
			if(nChannel == CHANNEL_RED)
				mo.SetLeadBitmap(pbhR, true, true);
			else if(nChannel == CHANNEL_GREEN)
				mo.SetLeadBitmap(pbhG, true, true);
			else if(nChannel == CHANNEL_BLUE)
				mo.SetLeadBitmap(pbhB, true, true);
			else
				return false;

		}
		
		L_FreeBitmap(pbhR);
		L_FreeBitmap(pbhG);
		L_FreeBitmap(pbhB);
	}
	return true;
}

///------ Folger 10/19/09 CHECK_MATRIX_OBJECT_VALID_BEFORE_USE
static	BOOL	_is_input_matrices_valid(TreeNode& trGetN)
{
	MatrixObject mor, mog, mob;
	return ( okxf_resolve_string_get_origin_object(trGetN.r.strVal, &mor) && mor.IsValid() && mor.HasImage()
			&& okxf_resolve_string_get_origin_object(trGetN.g.strVal, &mog) && mog.IsValid() && mog.HasImage()
			&& okxf_resolve_string_get_origin_object(trGetN.b.strVal, &mob) && mob.IsValid() && mob.HasImage() );
}
///------ End CHECK_MATRIX_OBJECT_VALID_BEFORE_USE


void imgRGBmerge(const Image& r, const Image& g, const Image& b, Image& oimg)
{
	
	MatrixPage mp;
	mp.Create("Origin" , CREATE_HIDDEN|CREATE_SET_MISSING_IN_MANAGER);
	mp.Rename(TEMP_IMAGES_OBJECTS);
	MatrixLayer ml = mp.Layers(0);
	ml.SetSize(3, -1, -1);
	
	_extract_data_from_image_channel(r, CHANNEL_RED);
	_extract_data_from_image_channel(g, CHANNEL_GREEN);
	_extract_data_from_image_channel(b, CHANNEL_BLUE);
	 
	MatrixObject moR = ml.MatrixObjects(0);
	MatrixObject moG = ml.MatrixObjects(1);
	MatrixObject moB = ml.MatrixObjects(2);	
	
	Image imR(moR);
	Image imG(moG);
	Image imB(moB);
	
	image_merge_rgb(oimg, imR, imG,imB);
	
	mp.Destroy();
	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int imgRGBmerge_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return imgRGBmerge_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	if(nEventID == GETNE_ON_INIT|| !lstrcmpi(lpcszNodeName, "r") || !lstrcmpi(lpcszNodeName, "g") || !lstrcmpi(lpcszNodeName, "b"))
	{
		strErrMsg = "";
		///------ Folger 10/19/09 CHECK_MATRIX_OBJECT_VALID_BEFORE_USE
		//MatrixObject mor, mog, mob;
		//if(!okxf_resolve_string_get_origin_object(trGetN.r.strVal, &mor)||mor.HasData(false)
			//||!okxf_resolve_string_get_origin_object(trGetN.g.strVal, &mog)||mog.HasData(false)
			//||!okxf_resolve_string_get_origin_object(trGetN.b.strVal, &mob)||mob.HasData(false))
		if ( !_is_input_matrices_valid(trGetN) )
		///------ End CHECK_MATRIX_OBJECT_VALID_BEFORE_USE
		{
			strErrMsg = CER_INVALID_MATOBJ;
			bOKEnable = false;
			return true;
		}
	}
	return false;

}

int imgRGBmerge_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void imgRGBmerge_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///------ Folger 10/19/09 CHECK_MATRIX_OBJECT_VALID_BEFORE_USE
	//MatrixObject mor, mog, mob;
	//if(!okxf_resolve_string_get_origin_object(trGetN.r.strVal, &mor)||mor.HasData(false)
		//||!okxf_resolve_string_get_origin_object(trGetN.g.strVal, &mog)||mog.HasData(false)
		//||!okxf_resolve_string_get_origin_object(trGetN.b.strVal, &mob)||mob.HasData(false))
	if ( !_is_input_matrices_valid(trGetN) )
	///------ End CHECK_MATRIX_OBJECT_VALID_BEFORE_USE
	{
		nRet = XFEVT_PROCEED_WITH_DLG;
	}
}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int imgRGBmerge_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
