class StereoPair_112:UIFrame112
{
number way
number axis
number zoom,point,backgr

string WayName,axisName
string zoomName,pointName,backgrName

TagGroup CreateAll(object self)
	{
	TagGroup dialog1 = DLGCreateDialog("Dialog").DLGTableLayout(3,1,0);

		number Bcgrd =210
		number size =20	
	Dialog1.DLGAddElement(self.settingsButton(Size,Bcgrd,"Settings").DLGAnchor("West"))
			TagGroup PopUpItem
			TagGroup PopUp = DLGCreatePopup(PopUpItem,1)
			PopUpItem.DLGAddPopupItemEntry("diverge")
			PopUpItem.DLGAddPopupItemEntry("cross")
	Dialog1.DLGAddElement(PopUp.DLGIdentifier("way"))
	Dialog1.DLGAddElement(DLGCreatePushButton("make pair", "MakePair").DLGAnchor("East"))

	TagGroup dialog2 = DLGCreateDialog("Dialog").DLGTableLayout(3,1,0);

	Dialog2.DLGAddElement(DLGCreatePushButton("get axis", "GetAxis").DLGAnchor("East"))
	Dialog2.DLGAddElement(DLGCreateIntegerField(90, 4).DLGAnchor("West").DLGIdentifier("axis"))
	Dialog2.DLGAddElement(DLGCreateLabel("degree").DLGAnchor("East"))	
	
	TagGroup dialog =DLGCreateDialog("Dialog")
	Dialog.DLGAddElement(Dialog1)
	Dialog.DLGAddElement(Dialog2)
	
	return Dialog
	}

object init(object self,string Package,string Name,string LabelP)
	{
	//names to store parameters
	WayName = "way"	
	axisName = "axis"
	zoomName = "zoom"
	pointName = "point"
	backgrName = "background"
	
	//initialize
self.initUIFrame112(self.CreateAll(),Package,Name)

	//read parameters
	way =self.LoadNumber112(1,wayName)
	axis =self.LoadNumber112(90,axisName)
	zoom =self.LoadNumber112(0.1,zoomName)
	point =self.LoadNumber112(0.03,pointName)
	backgr =self.LoadNumber112(2,backgrName)
	
try{	//set dislayed parameters
	self.LookUpElement("way").DLGValue(way)	
	self.LookUpElement("axis").DLGValue(axis)
	}
catch{break;}

	return self
	}

void Settings(object self)
	{	
	number NumberLines = 3		//number of adjusted parameters
	
	TagGroup zoomT = DLGCreateRealField(zoom,6,4)	
	TagGroup pointT = DLGCreateRealField(point,6,4)
			TagGroup PopUpItem
	TagGroup backgrT = DLGCreatePopup(PopUpItem,backgr)
			PopUpItem.DLGAddPopupItemEntry("black")
			PopUpItem.DLGAddPopupItemEntry("middle")
			PopUpItem.DLGAddPopupItemEntry("white")
	
	//Compose DLG tags
	TagGroup SettingList = DLGCreateBox("Settings").DLGTableLayout(1,NumberLines,1)
	
	//SettingsLine is UIFrame112 
	SettingList.DLGaddElement(self.SettingsLine(zoomName,zoomT).DLGAnchor("East"))
	SettingList.DLGaddElement(self.SettingsLine(pointName,pointT).DLGAnchor("East"))
	SettingList.DLGaddElement(self.SettingsLine(backgrName,backgrT).DLGAnchor("East"))
		
	object SettingsFrame = Alloc(UIframe).init(SettingList)
	if(SettingsFrame.Pose())
		{
		zoom =self.SettingNumberParameter(zoomT,zoomName) 
		point =self.SettingNumberParameter(pointT,pointName)	
		backgr =self.SettingNumberParameter(backgrT,backgrName)
		}
	}
	
void CloseUIFrame112(object self)
	{
	//add some actions if neccesary
	}
number AboutToCloseUIFrame112( object self, number verify )
	{
	//read and store displayed parameter
	way =self.LookUpElement("way").DLGGetValue()	
	axis =self.LookUpElement("axis").DLGGetValue()
		
	self.StoreNumber112(way,wayName)	
	self.StoreNumber112(axis,axisName)	
	}		


void GetAxis(object self)
	{
	//get image where alpha dirction is denoted
	image imag
	try{
	imag:=getFrontImage()
		}
	catch{
		OKdialog("search image with the indicated alpha-direction should be front most !!")
		exit(0)	
		}	
	imageDisplay disp =imag.ImageGetImageDisplay(0)

	//find line annotation denoting the alpha direction
	Component line 
	number type
	for(number i=(disp.ComponentCountChildren()-1);i>=0;i--)
		{ 
		line =Disp.ComponentGetChild(i)
		type =line.ComponentGetType()
//result("\n"+i+"type "+type)	
		if((type ==2)||(type ==3)||(type ==4) ) break
			//line		arrow		double arrow
		}		
			
	if(!line.ComponentIsValid()) 
		{
		OKdialog("Draw line denoting the alpha-direction first !!")
		exit(0)
		}
	
	//get alpha direction	
	number top,left,bottom,right
	line.ComponentGetRect(top,left,bottom,right)
	number dX =right -left
	number dY =bottom -top
	axis = atan2(dY,-dX)*57.2958
//result("\ndX "+dX+" dY "+dY+" "+axis)

	//set axis in frame
	self.LookUpElement("axis").DLGValue(axis)	
	
	//paint line
	line.ComponentSetForegroundColor( 1, 0, 0 )	
	}


void MakePair(object self)
	{
	image leftIm,rightIm 
	if(!GetTwoImages("Choose images for stereo pair",leftIm,rightIm )) exit(0)
	
	number width,height
	leftIm.GetSize(width,height)

	//read displayed parameters
	way =self.LookUpElement("way").DLGGetValue()
	axis =self.LookUpElement("axis").DLGGetValue()
	
	number angle =axis /57.2958	//from grad to radian

	number level

	//prepare data cube with left and right images
	number type =leftIm.ImageGetDataType()
	image comb :=NewImage("pair",type,width,height,2)
	comb[0,0,0,width,height,1] =leftIm
	comb[0,0,1,width,height,2] =rightIm

	//make cross correlation
	image correlated :=comb.CorrelateImages112()
//correlated.showImage()


	//prepare stereo pair
	image pair :=correlated.makeStereoPair112(way,angle,point,backgr)
	number pairHeight =pair.ImageGetDimensionSize(1)

	//transfer calibration
	number origin,scale
	string units
	leftIm.ImageGetDimensionCalibration(0,origin,scale,units,1)	//calibration of right image is assumed identical
	pair.ImageSetDimensionCalibration(0,origin,scale,units,1)
	pair.ImageSetDimensionCalibration(1,origin,scale,units,1)

	//display
	if(way==1) pair.setZoom(zoom)
	pair.showimage()
	
	//add scale markers
	imagedisplay disp =pair.ImageGetImageDisplay(0)
	number top =7/8*pairHeight
	number left =0
	number bottom =pairHeight
	number right =pairHeight/4
	component ScaleBar1 = NewComponent(31, top,left,bottom,right)
	disp.ComponentAddChildAtEnd(ScaleBar1)
	component ScaleBar2 = NewComponent(31, top,left+pairHeight,bottom,right+pairHeight)
	disp.ComponentAddChildAtEnd(ScaleBar2)	
	
	//contrast
	number low,high,bright,contrast,gamma
	imagedisplay disp0 =leftIm.ImageGetImageDisplay(0)
	disp0.ImageDisplayGetContrastLimits(low,high)
	disp0.ImageDisplayGetContrastParameters(bright,height)
	gamma =disp0.ImageDisplayGetGammaCorrection()
result("\nlimits "+low+"-"+high+" brightness "+bright+" contrast "+contrast+ " gamma "+gamma)	
	disp.ImageDisplaySetContrastLimits(low,high)
	disp.ImageDisplaySetContrastParameters(bright,height)
	disp.ImageDisplaySetGammaCorrection(gamma)
	}	
}


{
//check if Extended Frames are loaded
if(!doesFunctionExist("LoadNumber112")) 
	{
	OKdialog("Please drop 'temDM extFrames.gt3' plugin in the plugins folder !!");
	exit(0);
	}	
	
alloc(StereoPair_112).init("_temDM-Scripts:Stereo","Stereo","Stereo") 
}