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

Hit ctrl-enter to execute. 

Description:

	These functions perform 3x3 low pass and high pass
	filters .

	Copyright (C) 1995 Gatan Inc.
	All Rights Reserved
	Written by Chris Meyer
	Annotated by Paul Thomas March 2001 

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


/* This function performs a 3x3 sharpen (high pass filter)
	on the given real image and returns a new image that is
	filtered				*/

Image LowPass( Image img )
{
	// Determine source size and define a sub-area 1 pixel from the edges
	number width, height
	GetSize( img, width, height )
	SubArea sub_img := img[ 1, 1, height - 2, width - 2 ]

	// Create the destination image and give an appropriate name
	Image low_pass := img.ImageClone()
	low_pass.SetName(img.GetName() + " (low-pass filtered)")
	
	// Perform filter
	low_pass[ 1, 1, height - 2, width - 2 ] = ( \
		offset( sub_img, -1, -1 ) + \
		offset( sub_img, -1,  0 ) + \
		offset( sub_img, -1,  1 ) + \
		offset( sub_img,  0, -1 ) + \
		offset( sub_img,  0,  0 ) + \
		offset( sub_img,  0,  1 ) + \
		offset( sub_img,  1, -1 ) + \
		offset( sub_img,  1,  0 ) + \
		offset( sub_img,  1,  1 ) ) / 9.

	// Return the filtered image
	return low_pass
}


/*	This function performs a 3x3 sharpen (high pass filter)
	on the given real image and returns a new image that is
	filtered.			*/
	
Image HighPass( Image img)
{
	// Determine source size and define a sub-area 1 pixel from the edges
	number width, height
	GetSize( img, width, height )
	SubArea sub_img := img[ 1, 1, height - 2, width - 2 ]

	// Create the destination image and give an appropriate name
	Image high_pass := img.ImageClone()
	high_pass.SetName(img.GetName() + " (high-pass filtered)")
	
	// Perform high pass filter on the sub image
	high_pass[ 1, 1, height - 2, width - 2 ] = \
		9 * offset( sub_img, 0, 0 ) - \
		offset( sub_img, -1, -1 ) - \
		offset( sub_img, -1,  0 ) - \
		offset( sub_img, -1,  1 ) - \
		offset( sub_img,  0, -1 ) - \
		offset( sub_img,  0,  1 ) - \
		offset( sub_img,  1, -1 ) - \
		offset( sub_img,  1,  0 ) - \
		offset( sub_img,  1,  1 )

	// Return the hi-pass image
	return high_pass
}

// Examples of use:

Image front := GetFrontImage()
ShowImage( LowPass( front ) )
ShowImage( HighPass( front ) )


