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

Hit ctrl-enter to execute.

Description:

	This script demonstrates calculation of an intensity histogram
	of a row from the front image, integrated over a specified number of channels, 
	using the ImageCalculateHistogram function	
							
	Copyright © 2001 Gatan Inc.

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

	

/*	This method calulates the histogram of a row from the input image
	It is called with an input image (should be 2d), the row number to calculate the 
	histogram from (row), and the number of rows to integrate the histogram sampling region over (width) 
	The method returns a 1d histogram image															*/

image CreateHistogramOfRow( image img, number row, number width )
{
	// Get the width and height of the input image in pixels
	number img_width  = img.ImageGetDimensionSize(0)			
	number img_height = img.ImageGetDimensionSize(1)			

	// Ensure the number of rows to integrate over is non-zero and odd										
	if ( width < 0 )											
		Throw( "Width in CreateHistogramOfRow must be greater than 0")
	width = nearest(width/2)+1	

	// Check the sampling region does not lie outside the scope of the image
	if ( row - ( width - 1)/2 < 0 || row + ( width - 1)/2 >= img_height ) 
		Throw( "Histogram goes outside of the image boundaries in CreateHistogramOfRow")
																
	// Creat a sample image corresponding to the image region for histogram analysis; 
	// that is, the area bound by top = row-(width-1)/2, left = 0, bottom = row-(width+1)/2, right = img_width			
	image sample_img := img[ row - ( width - 1 )/2, 0, row +\
								 ( width - 1 )/2, img_width] 
																
	// Determine the maximum and minimum of the image img																																			
	number min = min( img )										
	number max = max( img )

	// Perform a simplistic check to ensure there is enough varyation in values to produce a meaningful histogram
	// If not, throw an error
	if (max - min <= 2)
		Throw( "The intensity range of the front-most image is too low.\nSelect a different image for this example." )
																
	// Create and initialise the destination image for the histogram - Real, 4-byte, 
	// of size max-min (rounded down if non-integer) by 1 channel 
	image hist := RealImage( "Histogram of row " + row, 4, floor(max - min), 1 )																	
	hist = 0													

	// Calculate the histogram, from souce image 'sample_img' to destination image 'hist', min value min, max value max
	// and calibrate the x-axis (dimension 0) calibration of the histogram array, with 'min' origin' and unit scale and empty axis label
	ImageCalculateHistogram( sample_img, hist, 0, min, max )	
	hist.ImageSetDimensionCalibration(0, min, 1, "", 0)	
	
	// Return the histogram	
	return hist													
}

  
// Grab front image, and ask user for suitable row and width for histogram 
image img := GetFrontImage()
number row = 10
number width = 3
if (! GetNumber("Enter image row for histogram :", row, row)) exit(0)
if (! GetNumber("Enter sample width :", width, width)) exit(0)

// Call the CreateHistogramOfRow function, and display histogram
ShowImage( CreateHistogramOfRow( img, row, width ) )     
