package ij.plugin;
import ij.*;
import ij.gui.*;
import java.awt.*;
public class StackPlotter implements PlugIn {
private int channel = 1;
private int slice = 1;
private int frame = 1;
private int frames = 1;
public void run(String arg) {
ImagePlus imp = IJ.getImage();
if (imp.getRoi() == null) {
IJ.error("Stack Plotter", "Line or rectangular selection required");
return;
}
int dim = imp.getNDimensions();
if (dim < 3) {
IJ.error("Stack Plotter","This plugin requires a stack");
return;
}
int length = 0;
if (dim==3)
length = imp.getImageStackSize();
boolean plotFrames = true;
if (dim>3) {
channel = imp.getChannel();
slice = imp.getSlice();
frame = imp.getFrame();
length = frames = imp.getNFrames();
if (dim==4 && length==1) {
plotFrames = false;
length = imp.getNSlices();
}
} else
slice = imp.getCurrentSlice();
ProfilePlot[] pPlot = new ProfilePlot[length];
double ymin = 0;
double ymax = 0;
for (int i=0; i<length; i++) {
if (dim == 3) imp.setPosition(i+1);
if (dim > 3) {
if (plotFrames)
imp.setPosition(channel,slice,i+1);
else
imp.setPosition(channel,i+1,frame);
}
pPlot[i] = new ProfilePlot(imp);
if (pPlot[i] == null) return;
if (pPlot[i].getMin() < ymin) ymin = pPlot[i].getMin();
if (pPlot[i].getMax() > ymax) ymax = pPlot[i].getMax();
}
double pp_min = ProfilePlot.getFixedMin();
double pp_max = ProfilePlot.getFixedMax();
ProfilePlot.setMinAndMax(ymin,ymax);
Plot plot = pPlot[0].getPlot();
Dimension size = plot.getSize();
ImageStack stack = new ImageStack(size.width,size.height);
for (int i=0; i< length; i++) {
plot = pPlot[i].getPlot();
stack.addSlice(plot.getProcessor());
}
ImagePlus output = new ImagePlus("Profile Plots",stack);
output.show();
output.setSlice(slice);
if (dim==3) imp.setPosition(slice);
if (dim>3) imp.setPosition(channel,slice,frame);
ProfilePlot.setMinAndMax(pp_min,pp_max);
}
}