All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ij.plugin.HyperStackConverter Maven / Gradle / Ivy

Go to download

ImageJ is an open source Java image processing program inspired by NIH Image for the Macintosh.

There is a newer version: 1.54m
Show newest version
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.Calibration;
import ij.macro.Interpreter;
import ij.io.FileInfo;
import ij.plugin.frame.Recorder;
import java.awt.image.ColorModel;


/** Implements the "Stack to HyperStack", "RGB to HyperStack" 
	and "HyperStack to Stack" commands. */
public class HyperStackConverter implements PlugIn {
	public static final int CZT=0, CTZ=1, ZCT=2, ZTC=3, TCZ=4, TZC=5;
	static final int C=0, Z=1, T=2;
    static final String[] orders = {"xyczt(default)", "xyctz", "xyzct", "xyztc", "xytcz", "xytzc"};
    static int ordering = CZT;
    static boolean splitRGB = true;

	public void run(String arg) {
		if (arg.equals("new"))
			{newHyperStack(); return;}
		ImagePlus imp = IJ.getImage();
    	if (arg.equals("stacktohs"))
    		convertStackToHS(imp);
    	else if (arg.equals("hstostack"))
    		convertHSToStack(imp);
	}
	
	/** Converts the specified stack into a hyperstack with 'c' channels, 'z' slices and
		 't' frames using the default ordering ("xyczt") and display mode ("Composite"). */
	public static ImagePlus toHyperStack(ImagePlus imp, int c, int z, int t) {
		return toHyperStack(imp, c, z, t, null, null);
	}

	/** Converts the specified stack into a hyperstack with 'c' channels,
	 *  'z' slices and 't' frames. The default "xyczt" order is used if
	 * 'order' is null. The default "composite" display mode is used
	 * if 'mode' is null.
	 * @param imp the stack to be converted
	 * @param c channels
	 * @param z slices
	 * @param t frames
	 * @param order hyperstack order ("default", "xyctz", "xyzct", "xyztc", "xytcz" or "xytzc")
	 * @param mode display mode ("composite", "color" or "grayscale")
	 * @return the resulting hyperstack
	*/
	public static ImagePlus toHyperStack(ImagePlus imp, int c, int z, int t, String order, String mode) {
		int n = imp.getStackSize();
		if (n==1 || imp.getBitDepth()==24)
			throw new IllegalArgumentException("Non-RGB stack required");
		if (c*z*t!=n)
			throw new IllegalArgumentException("C*Z*T not equal stack size");
		imp.setDimensions(c, z, t);
		if (order==null || order.equals("default") || order.equals("xyczt"))
			order = orders[0];
		int intOrder = CZT;
		for (int i=0; i1) {
			LUT[] luts = imp.getLuts();
			if (luts!=null && luts.length1) {
			IJ.error("HyperStack Converter", "RGB stacks are limited to one channel");
			return;
		}
		if (nChannels*nSlices*nFrames!=stackSize) {
			IJ.error("HyperStack Converter", "channels x slices x frames <> stack size");
			return;
		}
		imp.setDimensions(nChannels, nSlices, nFrames);
		if (ordering!=CZT && imp.getStack().isVirtual())
			IJ.error("HyperStack Converter", "Virtual stacks must by in XYCZT order.");
		else {
			shuffle(imp, ordering);
			ImagePlus imp2 = imp;
			if (nChannels>1 && imp.getBitDepth()!=24) {
				LUT[] luts = imp.getLuts();
				if (luts!=null && luts.length




© 2015 - 2025 Weber Informatics LLC | Privacy Policy