#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	func_exp,
	func_ln,
	func_sigmoid,
};
enum{
	channel_rgb,
	channel_red,
	channel_green,
	channel_blue,
};


#include <GetNBox.h>
#include <image_utils.h>
#include <..\Originlab\matdata_utils.h>	

#define LUT_LEN_8 256
#define LUT_LEN_12 4096
#define LUT_LEN_16 65536


static int _apply_functional_lut(BITMAPHANDLE* pLBmp, int* pLUT,UINT uLen,UINT uChannel,UINT uFunc,int nFact)
{
	waitCursor junk;
	if(GRAY_NO == L_IsGrayScaleBitmap(pLBmp) && uLen >256)
	{
		return(XFERR_CAN_NOT_SUPPORT_16BIT_COLOR_OR_UPWARDS);
	}
	
	
	vector<int> vnFlags = {FLT_EXP, FLT_LN,FLT_SIGMOID};
	if(uFunc < 0 || uFunc >= vnFlags.GetSize())
		return INTOPTN_OUT_OF_RANGE;
	
	uFunc = vnFlags[uFunc];
	
	if(uFunc==FLT_LN && nFact<0)
	{
		return INTOPTN_OUT_OF_RANGE;
	}
	
	vector<int> vnChannel = {CHANNEL_MASTER, CHANNEL_RED, CHANNEL_GREEN, CHANNEL_BLUE};
	
	if(uChannel < 0 || uChannel >= vnChannel.GetSize())
		return INTOPTN_OUT_OF_RANGE;
	
	uChannel=vnChannel[uChannel];
	
	int nRet =  L_GetFunctionalLookupTable(pLUT,uLen,0,uLen-1,nFact, uFunc);
	if(nRet != SUCCESS)
	{
		return nRet;
	}
	

	return  L_RemapBitmapIntensity(pLBmp,pLUT,uLen,uChannel);
}






void imgFuncLUT(const Image& img, const int& fact, const int& func, const int& channel, Image& oimg, vector& lut)
{
	
	oimg.SetLBmp(img.GetLBmp(), TRUE);
	BITMAPHANDLE* phBmp = oimg.GetLBmp();
	
	int nLen=get_image_data_max_val(phBmp)+1;
	
	vector<int> nvLUT;
	nvLUT.SetSize(nLen);
	
	int nRet = _apply_functional_lut(phBmp,nvLUT,nLen,channel,func,fact);
	
	if(nRet < MAX_ERROR_CODE_FROM_LT)
	{
		XF_THROW_EX(XFERR_LEADTOOLS_FUNCTION_UNKNOWN_ERR, nRet);
	}
	
	if(nRet !=  SUCCESS)
	{
		XF_THROW(nRet);	
	}
	
	if(lut)
	{
		lut.SetSize(nLen);
		lut=nvLUT;
	}
	
	
	
}

//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 imgFuncLUT_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return imgFuncLUT_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int imgFuncLUT_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 imgFuncLUT_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//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 imgFuncLUT_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}


// this is called when the GetNImageBox dialog is open
// bUpdateGrid = false when entering this function
// you need to set bUpdateGrid = true if you need to modify trGetN data and want to see the
// grid updated, for example
// trGetN.angle.nVal = 0; // force rotation to init to 0
// bUpdateGrid = true;
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
// example: 
// if(0==trGetN.angle.nVal) // no change, no point to click Ok to do nothing
//    bOKEnable = false; 
static void GetNImageBox_OnInit(TreeNode& trGetN, ImageControlBase* pimgCntrl, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{
	//trGetN.fact.nVal=0;
	
	pBITMAPHANDLE pPreviewBitmap;
	pPreviewBitmap=pimgCntrl->GetBitmapHandlePtr();
	int nLen=get_image_data_max_val(pPreviewBitmap)+1;
	
	vector<int> nvLUT;
	nvLUT.SetSize(nLen);

	int nRet = _apply_functional_lut(pPreviewBitmap, nvLUT,nLen, trGetN.channel.nVal,trGetN.func.nVal,trGetN.fact.nVal);
	if( SUCCESS != nRet)
	{
		bOKEnable = false;
		strErrMsg = nRet;
	}	
	bUpdateGrid = true;
}

// this is called whenever any controls in the grid is modified by the user
// upon entering function
// bUpdatePreview = true;// to update the preview image pPreviewBitmap
// bUpdateGrid = false; // event triggered by grid change, so default no need to update again
// if you want to modify other controls in the grid, then you need to set bUpdateGrid to true
// bOKEnable is default = true, you may add code to check trGetN and decide that the OK button should be disabled, then set this to false
static void GetNImageBox_OnChange(BITMAPHANDLE* pPreviewBitmap, TreeNode& trGetN, TreeNode& trNode, ImageControlBase* pimgCntrl, bool& bUpdatePreview, bool& bUpdateGrid, bool& bOKEnable, string& strErrMsg)
{
	vector<int> vnFlags = {FLT_EXP, FLT_LN, FLT_LINEAR,FLT_SIGMOID};
	
	//if(trGetN.func.DataID==trNode.DataID)
	if(trNode)
	{
		if(trNode.tagName == "func")
		{
			if(vnFlags[trGetN.func.nVal]==FLT_LN||vnFlags[trGetN.func.nVal]==FLT_SIGMOID)
			{
				trGetN.fact.SetAttribute(STR_COMBO_ATTRIB,"0|100|200");
				trGetN.fact.nVal = 0;
			}
			
			else
				trGetN.fact.SetAttribute(STR_COMBO_ATTRIB,"-100|100|200");
			
			bUpdateGrid = true;
	
		}
	}

	
	int nLen=get_image_data_max_val(pPreviewBitmap)+1;
	
	vector<int> nvLUT;
	nvLUT.SetSize(nLen);

	int nRet = _apply_functional_lut(pPreviewBitmap, nvLUT,nLen, trGetN.channel.nVal,trGetN.func.nVal,trGetN.fact.nVal);
	if( SUCCESS != nRet)
	{
		bOKEnable = false;
		strErrMsg = nRet;
	}

	bUpdatePreview = true;
}
	
int GetNImageBox_imgFuncLUT(TreeNode& trGetN, int nRow, int nCol, TreeNode& trNode, DWORD dwCntrl, int nType, WndContainer& theDlg, ImageControlBase* pimgCntrl)
{
	return xf_GetNImageBox_event_handler(trGetN, nRow, nCol, trNode, dwCntrl, nType, pimgCntrl,
								GetNImageBox_OnInit, GetNImageBox_OnChange);	
}
