/*-----------------------------------------------------------------------------------

Hit ctrl-enter to execute.

Description:

	This script simulates a noisy TEM image of a crystal with X-rays.
	A dialog box will ask for the signal-to-noise ratio and the approximate number
	of X-rays you want to appear. You may enter "0" for the number of X-rays, in which
	case no X-rays are added to the image.
	
	It demonstrates use of the icol and irow intrinsic variables, and additionally
	use of the Random function			
	Copyright © 2001 Gatan Inc.

------------------------------------------------------------------------------------*/

// Set up the x and y lattice spacings in pixels
number  pi = 3.1415926, spacingX = 20, spacingY = 35	

// The background scales the overall intensity, and the contrast 
// determines the relative intensity of the pattern		
number  background = 200, contrast = 0.1						

// This section asks the user for a number of factors
// including image dimensions, SNR and number of x-rays
number width, height, SNR, xRays								
GetNumber("Enter image width:", 256, width)						
GetNumber("Enter image height:", width, height)	
GetNumber("Enter signal-to-noise ratio:", 1, SNR)
GetNumber("Enter number of X-rays:", 0, xRays)

// Create the destination image; real 4byte with 2 dimensions.
image crystal := RealImage("Simulated Crystal", 4,	width, height)
			
// Generate the crystal image, scaled with respect to the background signal 
// with a random noise component added 
// Note Random Returns a random number in the range 0 to 65535. 
crystal = background*(1 + contrast*( sin(2*pi*icol/SpacingX) \
							*sin(2*pi*irow/spacingY) + Random()/(SNR*65536))) 	
										
// Add the number of specified x-rays; this conditional argument says that \
//if the random number generated is less than the value xrays/(width*height), 
//then add 2000 to that pixel.  If not, leave it as is. Note it acts on all pixels/
crystal += (Random() / 65536 < xRays/(width*height)) ? 2000 : 0 

// Set image contrast survey mode to 'entire image', and display the image
SetSurveyTechnique(crystal, 1)									
ShowImage(crystal)												

