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

Hit ctrl-enter to execute.

Description:

	A simple script to perform 3 times binning on the frontmost image
	using the icol and irow intrinsic variables

	Copyright © 2001 Gatan Inc.

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

// First, grab the front image and load it into image img

image img := GetFrontImage()


// Next, find out the width and height of the image

number width, height
Get2dSize(img, width, height)


// Create a Real 4-byte image with dimensions 1/3rd of the input image
  
image BinImg := RealImage( "Binned (3x)", 4 , width/3, height/3)


// Set each pixel in BinImg to the average of the 9 pixels in the 
// corresponding 3x3 sample in img.
// Note the use of the icol and irow intrinsic variables to avoid using a
// for loop to operate on each pixel within the image.  In this context, 
// icol and irow take on the values take on context specific values with
// respect to BinImg, not to img, hence icol refers to the BinImg column
// number, ranging from 0 to width/3, and likewise for irow,
// hence it is neccessary to include the 3* factor  
// Note the  character '\' denotes the command continues on the following line.

BinImg = ( img[ 3*icol-1, 3*irow - 1 ] + 	\
		   img[ 3*icol-1, 3*irow ]     + 	\
		   img[ 3*icol-1, 3*irow + 1 ] + 	\
		   img[ 3*icol,   3*irow - 1 ] + 	\
		   img[ 3*icol,   3*irow ]     + 	\
		   img[ 3*icol,   3*irow+1 ]   + 	\
		   img[ 3*icol+1, 3*irow-1 ]   + 	\
		   img[ 3*icol+1, 3*irow ]     + 	\
		   img[ 3*icol+1, 3*irow + 1 ] ) / 9
	
		
// Finally, display the binned image

ShowImage( BinImg ) 