// Filename:    ROI.OGS
// Creation:    by CPY 10/1/01
// Purpose:     Region of Interests Tools script
// Modifications:
// EJP 2006-03-10 v8.0374 ROI_CROP_CALL_XFUNC
// EJP 2008-04-08 v8.0840 QA80-11237 MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
// EJP 2008-05-22 v8.0870 QA80-11237 ROI_NEED_SUPPORT_IMAGE_AND_DATA
// EJP 2008-10-08 v8.0953 QA80-12333 MATRIX_CROP_NEED_MAINTAIN_COORDINATES
// Max 12/12/08 v8.0986 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS

[CheckIsMatrix]
	if(exist(%H) != 5)
	{
		type -b "Only support operation on matrix";
		return 1;
	}
	return 0;

// rect region
[Copy]
	if(run.Section(,CheckIsMatrix))
		return 1;

	/// EJP 2008-04-08 v8.0840 QA80-11237 MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
	/*
	if( 0 == @ROI )
		run.Section(, OldCopy);
	else
		run.Section(, NewCopy);
	*/
	int top, left, bottom, right;
	/// Max 12/12/08 v8.0986 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS
	//get_active_ROI_rect(top, left, bottom, right);
	run -oc get_active_ROI_rect(top, left, bottom, right);
	///---
	int width = right - left;
	int height = bottom - top;
	mCopy om:=<optional> x:=left y:=top w:=width h:=height;
	/// end MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT

	return 0;

[OldCopy]
	matrix -ib ROI.x1 ROI.y1 ROI.x3 ROI.y3;
	return 0;

[NewCopy]
	if(run.Section(,AttentionBox))
		return 1;

	Copy ROI %H;
	return 0;
	
[AttentionBox]
	break.style=1;
	break.open("Loading and compiling Origin C function...");
	// now attempt to load and compile function
	if(run.LoadOC("%YOriginC\OriginLab\ROIProcessing.c") != 0)
	{
	 // report if failure
	    break.close();
	    type -b "Error trying to load and compile Origin C file 'ROIProcessing.c";      
	    return 1;
	 }
	// close attention box
	break.close();
	return 0;

[OldCrop]
	matrix -ic ROI.x1 ROI.y1 ROI.x3 ROI.y3;
	label -r ROI;
	return 0;

[NewCrop]
	if(run.Section(,AttentionBox))
		return 1;

	Crop ROI %H;
	return 0;
[OldCreateNew]
	%W = %H;
	left = ROI.x1;
	top = ROI.y1;
	right = ROI.x3;
	bottom = ROI.y3;
	%Z=page.label$;// need to copy this too

	win -t matrix;
	matrix -ic left top right bottom %W;
	
	del -v top;
	del -v left;
	del -v right;
	del -v bottom;

	// also copy the name
	if(%Z!="")
		%Z = "%Z (copy)";
	else
		%Z = "copy of %W";

	page.label$="%Z";
	page.title = 3;// to show both name and label
	return 0;

[NewCreateNew]
	if(run.Section(,AttentionBox))
		return 1;
		
	CreateNew ROI %H;
	return 0;

[Crop]
	/// EJP 2006-03-10 v8.0374 ROI_CROP_CALL_XFUNC
	/*
	if(run.Section(,CheckIsMatrix))
		return 1;

	if( 0 == @ROI )
		run.Section(, OldCrop);
	else
		run.Section(, NewCrop);
	*/
	%W = %H;

	/// EJP 2008-04-08 v8.0840 QA80-11237 MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
	///left = int(ROI.x1);
	///top = int(ROI.y1);
	///width = int(ROI.x3) - left + 1;
	///height = int(ROI.y3) - top + 1;
	///
	///imgCrop left top width height %W;
	int top, left, bottom, right;
	/// Max 12/12/08 v8.0986 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS
	//get_active_ROI_rect(top, left, bottom, right);
	run -oc get_active_ROI_rect(top, left, bottom, right);
	///---
	int width = right - left;
	int height = bottom - top;
	/// end MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT

	/// EJP 2008-05-22 v8.0870 QA80-11237 ROI_NEED_SUPPORT_IMAGE_AND_DATA
	///imgCrop left top width height %W;
	/// TD 01-22-2009 QA80-12333_S2 EXPOSE_AND_USE_CopyMatrixObj_FROM_OC 
	//int isImage;
	//matrix -pg image isImage;
	//if( isImage )
	/// end EXPOSE_AND_USE_CopyMatrixObj_FROM_OC
	{
		/// EJP 2008-10-08 v8.0953 QA80-12333 MATRIX_CROP_NEED_MAINTAIN_COORDINATES
		///imgCrop left top width height %W;
		mCrop left top width height %W; // use mCrop to perform operation at matrix level
		/// end MATRIX_CROP_NEED_MAINTAIN_COORDINATES
	}
	/// TD 01-22-2009 QA80-12333_S2 EXPOSE_AND_USE_CopyMatrixObj_FROM_OC 
	//else
	//	matrix -ic left top right bottom;
	/// end EXPOSE_AND_USE_CopyMatrixObj_FROM_OC
	/// end ROI_NEED_SUPPORT_IMAGE_AND_DATA

	label -r ROI; // delete ROI object after cropping
	;clr;
	/// end ROI_CROP_CALL_XFUNC
	return 0;

[CreateNew]
	/// EJP 2008-04-08 v8.0840 QA80-11237 MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT
	/*
	if(run.Section(,CheckIsMatrix))
		return 1;

	if( 0 == @ROI )
		run.Section(, OldCreateNew);
	else
		run.Section(, NewCreateNew);
	*/
	%W = %H;

	int top, left, bottom, right;
	/// Max 12/12/08 v8.0986 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS
	//get_active_ROI_rect(top, left, bottom, right);
	run -oc get_active_ROI_rect(top, left, bottom, right);
	///---
	int width = right - left;
	int height = bottom - top;

	%Z=page.label$;// need to copy this too

	/// EJP 2008-05-22 v8.0870 QA80-11237 ROI_NEED_SUPPORT_IMAGE_AND_DATA
	///imgCrop left top width height %W <new>;
	int isImage;
	matrix -pg image isImage;
	if( isImage )
	{
		/// EJP 2008-10-08 v8.0953 QA80-12333 MATRIX_CROP_NEED_MAINTAIN_COORDINATES
		///imgCrop left top width height %W <new>;
		mCrop left top width height %W <new>; // use mCrop to perform operation at matrix level
		/// end MATRIX_CROP_NEED_MAINTAIN_COORDINATES
	}
	else
	{
		win -t matrix;
		matrix -ic left top right bottom %W;
		matrix -ii; // set new matrix to image view 
	}
	/// end ROI_NEED_SUPPORT_IMAGE_AND_DATA

	// also copy the name
	if(%Z!="")
		%Z = "%Z (copy)";
	else
		%Z = "copy of %W";

	page.label$="%Z";
	page.title = 3;// to show both name and label
	/// end MAP_ROI_MATRIX_RECT_TO_IMAGE_RECT

	return 0;