File: SubHyperstackMaker.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (183 lines) | stat: -rw-r--r-- 6,166 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ij.plugin;
import ij.*;
import ij.gui.GenericDialog;
import ij.process.ImageProcessor;
import ij.process.LUT;
import java.util.ArrayList;
import java.util.List;
import java.awt.Color;

/**
 * This plugin is used by the Image/Stacks/Tools/Make Substack
 * command to create substacks of hyperstacks.
 *  
 * @author Curtis Rueden
 */
public class SubHyperstackMaker implements PlugIn {

	public void run(String arg) {
		// verify input image is appropriate
		ImagePlus input = WindowManager.getCurrentImage();
		if (input == null) {
			IJ.showMessage("No image open.");
			return;
		}
		if (input.getStackSize() == 1) {
			IJ.showMessage("Image is not a stack.");
			return;
		}
		int cCount = input.getNChannels();
		int zCount = input.getNSlices();
		int tCount = input.getNFrames();
		boolean hasC = cCount > 1;
		boolean hasZ = zCount > 1;
		boolean hasT = tCount > 1;

		// prompt for C, Z and T ranges
		GenericDialog gd = new GenericDialog("Subhyperstack Maker");
		gd.addMessage("Enter a range (e.g. 2-14), a range with increment\n"
			+ "(e.g. 1-100-2) or a list (e.g. 7,9,25,27)", null, Color.darkGray);
		if (hasC) gd.addStringField("Channels:", "1-" + cCount, 40);
		if (hasZ) gd.addStringField("Slices:", "1-" + zCount, 40);
		if (hasT) gd.addStringField("Frames:", "1-" + tCount, 40);
		gd.showDialog();
		if (gd.wasCanceled()) return;
		String cString = hasC ? gd.getNextString() : "1";
		String zString = hasZ ? gd.getNextString() : "1";
		String tString = hasT ? gd.getNextString() : "1";

		// compute subhyperstack
		ImagePlus output = makeSubhyperstack(input, cString, zString, tString);

		// display result
		output.show();
	}

	public static ImagePlus makeSubhyperstack(ImagePlus input, String cString, String zString, String tString) {
		ArrayList<Integer> cList = parseList(cString, input.getNChannels());
		ArrayList<Integer> zList = parseList(zString, input.getNSlices());
		ArrayList<Integer> tList = parseList(tString, input.getNFrames());
		return makeSubhyperstack(input, cList, zList, tList);
	}

	public static ImagePlus makeSubhyperstack(ImagePlus input, List<Integer> cList, List<Integer> zList, List<Integer> tList) {
		// validate inputs
		if (cList.size() == 0)
			throw new IllegalArgumentException("Must specify at least one channel");
		if (zList.size() == 0)
			throw new IllegalArgumentException("Must specify at least one slice");
		if (tList.size() == 0)
			throw new IllegalArgumentException("Must specify at least one frame");

		ImageStack inputStack = input.getImageStack();

		int cCount = input.getNChannels();
		int zCount = input.getNSlices();
		int tCount = input.getNFrames();

		for (int c : cList)
			check("C", c, cCount);
		for (int z : zList)
			check("Z", z, zCount);
		for (int t : tList)
			check("T", t, tCount);

		// create output image
		String title = WindowManager.getUniqueName(input.getTitle());
		ImagePlus output = IJ.createHyperStack(title, input.getWidth(), input.getHeight(), cList.size(), zList.size(), tList.size(), input.getBitDepth());
		//ImagePlus output = input.createHyperStack(title, cList.size(), zList.size(), tList.size(), input.getBitDepth());	
		ImageStack outputStack = output.getImageStack();

		// add specified planes to subhyperstack
		int oc = 0, oz, ot;
		for (int c : cList) {
			oc++;
			oz = 0;
			for (int z : zList) {
				oz++;
				ot = 0;
				for (int t : tList) {
					ot++;
					int i = input.getStackIndex(c, z, t);
					int oi = output.getStackIndex(oc, oz, ot);
					String label = inputStack.getSliceLabel(i);
					ImageProcessor ip = inputStack.getProcessor(i);
					outputStack.setSliceLabel(label, oi);
					outputStack.setPixels(ip.getPixels(), oi);
					//IJ.log("  "+c + "  "+z+"  "+t+"  "+i +" "+oi+"  "+outputStack.getProcessor(1).getPixelValue(0,0));	
				}
			}
		}
		output.setStack(outputStack);

		// propagate composite image settings, if appropriate
		if (input instanceof CompositeImage) {
			CompositeImage compositeInput = (CompositeImage) input;
			CompositeImage compositeOutput =
				new CompositeImage(output, compositeInput.getMode());
			oc = 0;
			for (int c : cList) {
				oc++;
				LUT table = compositeInput.getChannelLut(c);
				compositeOutput.setChannelLut(table, oc);
				compositeOutput.setPositionWithoutUpdate(oc, 1, 1);
				compositeInput.setPositionWithoutUpdate(c, 1, 1);
				double min = compositeInput.getDisplayRangeMin();
				double max = compositeInput.getDisplayRangeMax();
				compositeOutput.setDisplayRange(min, max);
			}
			output = compositeOutput;
		}
		return output;
	}

	private static void check(String name, int index, int count) {
		if (index < 1 || index > count) {
			throw new IllegalArgumentException("Invalid " + name + " index: " +
				index);
		}
	}

	private static ArrayList<Integer> parseList(String planeString, int count) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (String token : planeString.split("\\s*,\\s*")) {
			int dash1 = token.indexOf("-");
			int dash2 = token.lastIndexOf("-");
			if (dash1 < 0) {
				// single number
				int index;
				try {
					index = Integer.parseInt(token);
				} catch (NumberFormatException exc) {
					throw new IllegalArgumentException("Invalid number: " + token);
				}
				if (index < 1 || index > count)
					throw new IllegalArgumentException("Invalid number: " + token);
				list.add(Integer.parseInt(token));
			} else {
				// range, with or without increment
				int min, max, step;
				try {
					min = Integer.parseInt(token.substring(0, dash1));
					if (dash1 == dash2) {
						// range (e.g. 2-14)
						max = Integer.parseInt(token.substring(dash1 + 1));
						step = 1;
					} else {
						// range with increment (e.g. 1-100-2)
						max = Integer.parseInt(token.substring(dash1 + 1, dash2));
						step = Integer.parseInt(token.substring(dash2 + 1));
					}
				} catch (NumberFormatException exc) {
					throw new IllegalArgumentException("Invalid range: " + token);
				}
				if (min < 1 || min > max || max > count || step < 1)
					throw new IllegalArgumentException("Invalid range: " + token);
				for (int index = min; index <= max; index += step)
					list.add(index);
			}
		}
		return list;
	}

}