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

Hit ctrl-enter to execute.

Description:

	This script generates an image of a grid, and then applies a warp factor to it
	demonstrating the warp function; 
	RealImageExpression Warp( RealImage source, RealImageExpression x, RealImageExpression y ) 

	Copyright © 2001 Gatan Inc.

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

/* 	This method creates a 2d grid image of the specified size, with grid-lines speced
	by the specified spacing
	The method is called with the desired image width and height, and the grid spacing in pixels.
 	It returns the grid image */

image CreateGridImage( number width, number height, number spacing )
{
	// Create a 2 byte integer image of the specified width and height
	image img := IntegerImage("Original", 2, 0, width, height)			
	
	// Set all pixels to unity where icol, the column intrinsic variable, divided by spacing is an integer
	// and likewise for irow.  Set all other pixels to 0.
	img = (floor(icol/spacing) == icol/spacing || floor(irow/spacing) == irow/spacing) ? 1 : 0					
	
	// Return the grid image																	
	return(img)															
}

// Set the warp factor to be used...
// and lso the source image size and grid spacing
number warpfactor = 2														
number width = 512, height = 512, spacing = 10							

// Create and show a grid image, using the CreteGridImage method defined above 
image img := CreateGridImage( width, height, spacing )					
ShowImage( img )
														
// Create the destination image for the warp function, of equal width and height
// Then call the warp routine; the function is called with the source image, and 
// the x and y-transform co-ordinate sets. It returns the bilinear interpolated value 
// at the position [x,y] within the source image. 
image warped := RealImage( "Warped", 4, width, height )					
warped = warp( img, icol, irow/warpfactor)								

// Finally, display the warped image.																			
ShowImage(warped)																										
