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

/** This plugin isolates pixels in an RGB image or stack according to a range of Hue.
	Bob Dougherty 5/10/2002
 */
/*	License:
	Copyright (c) 2002, 2005, OptiNav, Inc.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		Redistributions of source code must retain the above copyright
	notice, this list of conditions and the following disclaimer.
		Redistributions in binary form must reproduce the above copyright
	notice, this list of conditions and the following disclaimer in the
	documentation and/or other materials provided with the distribution.
		Neither the name of OptiNav, Inc. nor the names of its contributors
	may be used to endorse or promote products derived from this software
	without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class Band_Pass implements PlugInFilter {

	ImagePlus imp;
	boolean canceled = false;
	int low = 0;
	int high = 255;

	public int setup(String arg, ImagePlus imp) {
		this.imp = imp;
		return DOES_RGB;
	}

	public void run(ImageProcessor ip) {

		int numSlices = imp.getStackSize();
		ImageStack stack = imp.getStack();
		int numPixels = stack.getWidth()*stack.getHeight();

		//Get the "band";
		getBand();
		if (canceled) return;

		//Make work arrays for Hue, Saturation, and Brightness.
		byte[] h = new byte[numPixels];
		byte[] s = new byte[numPixels];
		byte[] b = new byte[numPixels];


        //Go through the slices of the stack.
		for (int i = 0; i < numSlices; i++){

			//Get a slice.
 			ColorProcessor cpSlice = (ColorProcessor)stack.getProcessor(i+1);
 			IJ.showStatus("Bandpassing slice "+(i+1));

 			//Filter the slice.
 			cpSlice.getHSB(h,s,b);
 			for (int j = 0; j < numPixels; j++){
 				int hue = h[j]&0xff;
 				if ((hue < low)|(hue > high)){
 					h[j] = 0;
 					s[j] = 0;
 					b[j] = 0;
 				}
  			}
 			cpSlice.setHSB(h,s,b);

		}

		//Update the image from the stack.
        imp.setStack(null,stack);
 	}

	private void getBand() {
		GenericDialog gd = new GenericDialog("Band Selection", IJ.getInstance());
		gd.addNumericField("Lowest Hue to keep", low, 0);
		gd.addNumericField("Highest Hue to keep", high, 0);
		gd.showDialog();
		if (gd.wasCanceled()) {
			canceled = true;
		}
		low = (int)gd.getNextNumber();
		high = (int)gd.getNextNumber();
	}
}


