import ij.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;

/** Amplitude from RIS
  *
  * Gets Amplitude-Phase from Real-Imaginary-Saturation.
  *
  * This example adapted by pf from tutorial plugins named
  * ColorInverter & StackAverage found with the tutorial at
  * http://www.fh-hagenberg.at/mtd/depot/imaging/imagej
  * 
  */
public class AmplitudeFromRI_ implements PlugInFilter {

	protected ImagePlus imp; // defines an instance variable, protected means what?
	

	public int setup(String arg, ImagePlus imp) {
		if (arg.equals("about"))
			{showAbout(); return DONE;}
		this.imp=imp;
                return DOES_32+STACK_REQUIRED; // checks for stacked floats as input
	}

	public void run(ImageProcessor ip) {

		ImageStack stack=imp.getStack(); // sets up the image stack for access
		// get width, height and the region of interest
		int w = ip.getWidth();     
		int h = ip.getHeight();    
		int nslices = (int) stack.getSize(); // gets the number of stack slices
		int dimension = ip.getWidth()*ip.getHeight();	
		float[] pixels01 = (float[]) stack.getPixels(1); // gets the first slice pixel array
		float[] pixels02 = (float[]) stack.getPixels(2); // gets the 2nd slice pixel array
		float[] pixels03 = new float[dimension];
		if (nslices>2) {pixels03 = (float[]) stack.getPixels(3);}
	
		double amplitude=0;
		double phase=0;
		int i;
		// IJ.showMessageWithCancel("testwindow","look out");

		// create a new image with the same size and copy the pixels of the original image
		ImagePlus fpimage = NewImage.createFloatImage("Amplitude",w,h,1,NewImage.FILL_BLACK);
		ImageProcessor fp_ip = fpimage.getProcessor();
		float[] apixels = (float[]) fp_ip.getPixels();
		for (int j=0;j<dimension;j++) {		
			apixels[j] = (float) Math.sqrt(pixels01[j]*pixels01[j]+pixels02[j]*pixels02[j]);
		}

		fp_ip.resetMinAndMax();
		fpimage.show();
		fpimage.updateAndDraw();



	}

	void showAbout() {
		// called by setup if the string argument is "about"
		IJ.showMessage("Amplitude from Real-Imaginary",
			"creates an amplitude image from a real-imaginary stack");
	}

}