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

Hit ctrl-enter to execute.

Description:

	This script calculates the diffractogram of the topmost image. 
	A region with sides a power of two must be selected with the selection tool (use Alt. pressed down) or, 
	if the whole image is used, it should have sides of length to the power of two.

	Copyright © 2001 Gatan Inc.

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

/*	Procedure for calculation of diffractogram:

	Step 1: subtract mean of selection of image.
	Step 2: multiply selection with appropriately sized Hanning window.
	Step 3: perform a Fourier transform.
	Step 4: Display the log of the amplitude.			*/


// Grab the frontmost image
image img
img.GetFrontImage()	

// Get bounds of any area selected with an ROI. 
// Uses whole image rectangle if no selection is present									
number top, left, bottom, right
GetSelection( img, top, left, bottom, right) 				

// Calculate the width and height of the region
number width = right - left									
number height = bottom - top
								
// Create an image for the selected region 
image WindowedImg := RealImage("", 4, width, height)

// and create a Hanning window of the same size		
image Hanning := RealImage("", 4, width, height) 			
Hanning = ( 1-cos(2*pi()*icol/width) )*( 1-cos(2*pi()*irow/height) )	

// Copy the selected region, subtracting the mean and multiplying by the Hanning window.  
// Note [] denotes use sub-region.
WindowedImg = Hanning*( img[] - average(img[]) )			

// Generate diffractogram
compleximage diffractogram := PackedFFT(windowedImg)		

// Set diffractogram name...and display
SetName(diffractogram, "Diffractogram")							
ShowImage(diffractogram)								
	