#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	po_zero,
	po_map,
	po_repeat,
};


//put additional include files here
#include <GetNBox.h>
#include <image_utils.h>
//put your own support static functions here
//enum {
	//ERR_INVALID_MATRIXOBJECT = XF_LOCAL_ERR_BEGIN,
	//ERR_CORRE_OUT_RANGE,
	//ERR_MATOBJ_COPY_FAILED,
	//ERR_IN_APPLY_FILTER,
//};

static vector<int> vnType = {MFILTER_ZEROPADDINGWINDOW, MFILTER_MAPPADDINGWINDOW, MFILTER_REPEATEDGEPADDINGWINDOW};
//

void medianflt2(const MatrixObject& im, MatrixObject& om, const int& n, const int& po)
{
	bool bNorm = true;
	
	
	if(po >= vnType.GetSize() || po < 0 )
	{
		XF_THROW(CER_INPUT_OPTION_OUT_OF_RANGE);
	}
	
	if(n<=2)
		XF_THROW(XFERR_FILTER_TOO_SMALL);
	
	if(!matobj_copy(om,im))
	{
		XF_THROW(MATOBJ_COPY_FAILED);
	}
	
	matrixbase& matOut = om.GetDataObject();
		
	if(!matOut.MedianFilter(n, vnType[po]))
		XF_THROW(XFERR_IN_APPLY_FILTER);
	
}

//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 medianflt2_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return medianflt2_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	MatrixObject im;
	if(!( okxf_resolve_string_get_origin_object(trGetN.im.strVal, &im) && im.IsValid() && im.HasData(true)) )
	{
		///Sophy 6/25/2010 ORG-322-P1 BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
		//strErrMsg = XFERR_INVALID_MATOBJ;
		if ( !im.IsValid() )
			strErrMsg = XFERR_INVALID_MATOBJ;
		else
			strErrMsg = CER_COLOR_IMAGE_NEED_CVT;
		///end BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
		bOKEnable = false;
		return false;
	}
	
	
	if(trGetN.n.nVal>9)
	{
		strErrMsg = XFWAR_FILTER_TOO_LARGE;
		bOKEnable = true;
	}	
	
	if(trGetN.n.nVal<=2)
	{
		strErrMsg = XFERR_FILTER_TOO_SMALL;
		bOKEnable = false;
	}	
	
	return true;

}

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