// Script function: Image function with a condition
// Requirement: Open an image

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined 
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html

image EMimgFunction(image MyInitialIMG, number MyCondition) 
{	
	image MyFunctionAction

	number MyScalex, MyScaley
	getscale(MyInitialIMG,MyScalex, MyScaley)
	
	if(MyCondition==0) // Copy the original image if the flag is set to 0
		{
			MyFunctionAction = MyInitialIMG
			setscale(MyFunctionAction, MyScalex, MyScaley)
		}
	else // Inverse the contrast of image
		{
			MyFunctionAction = MyInitialIMG*-1
			setscale(MyFunctionAction, MyScalex, MyScaley)
		}

return MyFunctionAction
}

image MyFrontIMG:=getfrontimage()[] // Execute the script on the ROI of the most front image or the most front image
string MyIMGname=getname(MyFrontIMG)

number MySetting=0 // Copy the original image when the flag is set to 0
image GlobalSinoOriginal=EMimgFunction(MyFrontIMG,MySetting)
showimage(GlobalSinoOriginal)
imagecopycalibrationfrom(GlobalSinoOriginal,MyFrontIMG)
setname(GlobalSinoOriginal,"Original image of '"+MyIMGname+"'")

MySetting=1 // Inverse the contrast of image
image GlobalSinoInverse=EMimgFunction(MyFrontIMG,MySetting)
showimage(GlobalSinoInverse)
imagecopycalibrationfrom(GlobalSinoInverse,MyFrontIMG)
setname(GlobalSinoInverse, " Inversed image of '" + MyIMGname+"'")
