void CrossShifts112(number &delX,number &delY,image first,image next)
	{
	//this calculates the relative shift between two images
	number width,height
	first.getSize(width,height)

	image Cross =CrossCorrelate(next,first)
	Cross.max(delX,delY)

	delX -=(width/2)
	delY -=(height/2)
	}

image centerShifts112(image shifts,number &minSh,number &maxSh)
	{
	//this function minimizes the overall shift by counting it from the middle value
	shifts.minmax(minSh,maxSh)

	number meanSh =(minSh+maxSh)/2

	shifts -=meanSh

	minSh -=meanSh
	maxSh -=meanSh

	minSh =modulus(minSh)

	return shifts
	}

image CorrelateImages112(image cube)
	{
	//this function input datacube of uncorrelated images
	//and output a (smaller) cube of cross-correlated images
	number width,height,Nimag
	cube.get3Dsize(width,height,Nimag)
	
	image shifts :=integerImage("",2,1,2,Nimag)	//shiftX in 0, shiftY in 1 pixel

	number shiftX,shiftY

	//calculate shifts relative the first image
	for(number i=1;i<Nimag;i++)
		{
		image first =cube[0,0,0,width,height,1]	//first image as  reference
		image next =cube[0,0,i,width,height,i+1]
		CrossShifts112(shiftX,shiftY,first,next)
		shifts.setPixel(0,i,shiftX)
		shifts.setPixel(1,i,shiftY)
		}
	
	//minimaze the overall shifts 	
	number marginXL,marginXR,marginYT,marginYB	
	shifts[0,0,Nimag,1]	=centerShifts112(shifts[0,0,Nimag,1],marginXL,marginXR)
	shifts[0,1,Nimag,2]	=centerShifts112(shifts[0,1,Nimag,2],marginYT,marginYB)

//shifts.showimage()
	
	//offset the images
	image cubeCorr :=cube.ImageClone()
	for(number i=0;i<Nimag;i++)
		{
		shiftX =shifts.getPixel(0,i)
		shiftY =shifts.getPixel(1,i)
		cubeCorr[0,0,i,width,height,i+1] =offset(cube[0,0,i,width,height,i+1],shiftX,shiftY)
		}

	//clip the borders
	image cubeClipped :=integerImage("",2,1,width-marginXL-marginXR,height-marginYT-marginYB,2)

	cubeClipped =cubeCorr[marginXL,marginYT,0,width-marginXR,height-marginYB,Nimag]
			
	return cubeClipped
	}




void RoundMarker112(image target,number cX,number cY,number radius, number Ints)
	{
	number width =target.ImageGetDimensionSize(0)
	cX *=width
	cY *=width
	radius *=width

	target = tert(((icol-cX)**2+(irow-cY)**2)<radius**2,Ints,target)
	}

image PrepareImage112(image in,number axis,number pointRad,number fill,number minInt)
	{
	number width,height
	in.GetSize(width,height)

	//the radius of output circle, the perepheria will be backgrounded
	number radius =min(width/2,height/2)

	//rotate
	image rotated =in.rotate(axis)
	number widthR,heightR
	rotated.GetSize(widthR,heightR)
//result("\n"+width+" "+height+"    "+widthR+" "+heightR)

	//clip corners
	number top =trunc(heightR/2-radius)
	number bottom =trunc(heightR/2+radius)
	number left =trunc(widthR/2-radius)
	number right =trunc(widthR/2+radius)
//result("\n"+top+" "+left+" "+bottom+" "+right)
	image clipped =rotated[top,left,bottom,right]

	//make circle			
	clipped =tert(iradius<radius,clipped,fill)

	//make round markers
	clipped.RoundMarker112(0.5,PointRad,PointRad,minInt)
	clipped.RoundMarker112(0.5,1-PointRad,PointRad,minInt)

	return clipped 
	}

image makeStereoPair112(image cubed, number way, number axis,number pointRad,number backgrd)
	{
	number width =cubed.ImageGetDImensionSize(0)
	number height =cubed.ImageGetDImensionSize(1)
	
	//extract left and right images from datacube (should be already cross-correlated)
	image left :=cubed[0,0,0,width,height,1]
	image right :=cubed[0,0,1,width,height,2]

	//eqialize intensity in both images
	number mean =mean(left)
	right *=(mean/mean(right))	
	
	//define background
	number var =variance(left).sqrt()
	number minInt = mean -3*var
	number maxInt = mean +3*var
	number fill
	if(backgrd ==1) fill = minInt
	if(backgrd ==2) fill = mean
	if(backgrd ==3) fill = maxInt
	
	//prepare circle images
	image leftStereo :=left.PrepareImage112(axis,pointRad,fill,minInt)
	image rightStereo :=right.PrepareImage112(axis,pointRad,fill,minInt)

	number widthS,heightS 
	leftStereo.getSize(widthS,heightS)
	
	//prepare stereo pair image
	number type =left.ImageGetDataType()
	image pair :=NewImage("pair",type,2*widthS,heightS)
	
	if(way ==1)
		{
		pair[0,0,heightS,widthS] =leftStereo
		pair[0,widthS,heightS,2*widthS]  =rightStereo
		}
	if(way ==2)
		{
		pair[0,0,heightS,widthS] =rightStereo
		pair[0,widthS,heightS,2*widthS]  =leftStereo
		}
		
	return pair
	}