File: StackEditor.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 (321 lines) | stat: -rw-r--r-- 9,304 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.Calibration;
import ij.macro.Interpreter;
import ij.io.FileInfo;
import java.awt.*;
import java.util.ArrayList;

/** Implements the AddSlice, DeleteSlice and "Stack to Images" commands. */
public class StackEditor implements PlugIn {
	ImagePlus imp;
	int nSlices, width, height;

	public void run(String arg) {
		imp = IJ.getImage();
    	nSlices = imp.getStackSize();
    	width = imp.getWidth();
    	height = imp.getHeight();
    	
    	if (arg.equals("add"))
    		addSlice();
    	else if (arg.equals("delete"))
    		deleteSlice();
    	else if (arg.equals("toimages"))
    		convertStackToImages(imp);
	}

	void addSlice() {
		if (imp.isHyperStack() || imp.isComposite()) {
			addHyperstackChannelSliceOrFrame();
			return;
		}
 		if (!imp.lock()) return;
		int id = 0;
		ImageStack stack = imp.getStack();
		if (stack.getSize()==1) {
			String label = stack.getSliceLabel(1);
			if (label!=null && label.indexOf("\n")!=-1)
				stack.setSliceLabel(null, 1);
			Object obj = imp.getProperty("Label");
			if (obj!=null && (obj instanceof String))
				stack.setSliceLabel((String)obj, 1);
			id = imp.getID();
		}
		ImageProcessor ip = imp.getProcessor();
		int n = imp.getCurrentSlice();
		if (IJ.altKeyDown()) n--; // insert in front of current slice
		stack.addSlice(null, ip.createProcessor(width, height), n);
		imp.setStack(null, stack);
		imp.setSlice(n+1);
		imp.unlock();
		imp.changes = true;
		if (id!=0) IJ.selectWindow(id); // prevents macros from failing
	}
	
	void deleteSlice() {
		if (nSlices<2)
			{IJ.error("\"Delete Slice\" requires a stack"); return;}
		if (imp.isHyperStack() || (imp.isComposite() && nSlices==imp.getNChannels())) {
			deleteHyperstackChannelSliceOrFrame();
			return;
		}
		if (!imp.lock()) return;
		ImageStack stack = imp.getStack();
		int n = imp.getCurrentSlice();
 		stack.deleteSlice(n);
 		if (stack.getSize()==1) {
			String label = stack.getSliceLabel(1);
 			if (label!=null) imp.setProperty("Label", label);
 		}
		imp.setStack(null, stack);
 		if (n--<1) n = 1;
		imp.setSlice(n);
		imp.unlock();
		imp.changes = true;
	}

	void addHyperstackChannelSliceOrFrame() {
		int channels = imp.getNChannels();
		int slices = imp.getNSlices();
		int frames = imp.getNFrames();
		int c1 = imp.getChannel();
		int z1 = imp.getSlice();
		int t1 = imp.getFrame();
		String[] choices = {"channel", "slice", "frame"};
		String choice = choices[0];
		if (frames>1 && slices==1)
			choice = "frame";
		else if (slices>1)
			choice = "slice";
		GenericDialog gd = new GenericDialog("Add");
		gd.addChoice("Add", choices, choice);
		gd.addCheckbox("Prepend", false);
		gd.showDialog();
		if (gd.wasCanceled())
			return;
		choice = gd.getNextChoice();
		boolean prepend = gd.getNextBoolean();
		if (!imp.lock())
			return;
		ImageStack stack = imp.getStack();
		LUT[] luts = null;
		if (choice.equals("frame")) { // add time point
			int index = imp.getStackIndex(channels, slices, t1);
			if (prepend)
				index = 0;
			for (int i=0; i<channels*slices; i++) {
				ImageProcessor ip = stack.getProcessor(1).duplicate();
				ip.setColor(0); ip.fill();
				stack.addSlice(null, ip, index);
			}
			frames++;
		} else if (choice.equals("slice")) { // add slice to all volumes
			for (int t=frames; t>=1; t--) {
				int index = imp.getStackIndex(channels, z1, t);
				if (prepend)
					index = (t-1)*channels*slices;
				for (int i=0; i<channels; i++) {
					ImageProcessor ip = stack.getProcessor(1).duplicate();
					ip.setColor(0); ip.fill();
					stack.addSlice(null, ip, index);
				}
			}
			slices++;
		} else if (choice.equals("channel")) { // add channel
			if (imp.isComposite())
				luts = ((CompositeImage)imp).getLuts();
			int index = imp.getStackIndex(c1, slices, frames);
			int minIndex = 1;
			if (prepend) {
				index = channels*slices*frames - channels;
				minIndex = 0;
				c1 = 0;
			}
			while (index>=minIndex) {
				ImageProcessor ip = stack.getProcessor(1).duplicate();
				ip.setColor(0); ip.fill();
				stack.addSlice(null, ip, index);
				index -= channels;
			}
			channels++;
		}
		imp.setStack(stack, channels, slices, frames);
		if (luts!=null) {
			LUT[] luts2 = new LUT[luts.length+1];
			int index = 0;
			for (int i=0; i<luts2.length; i++) {
				if (i==c1)
					luts2[i] = LUT.createLutFromColor(Color.white);
				else 
					luts2[i] = luts[index++];
			}
			CompositeImage cimp = (CompositeImage)imp;
			for (int c=1; c<=channels; c++)
				cimp.setChannelLut(luts2[c-1], c);
			imp.updateAndDraw();
		}
		imp.unlock();
		imp.repaintWindow();
		if (prepend) {
			imp.setPosition(channels, slices, frames);
			imp.setPosition(1, 1, 1);
		}
		imp.changes = true;
	}
	
	void deleteHyperstackChannelSliceOrFrame() {
		int channels = imp.getNChannels();
		int slices = imp.getNSlices();
		int frames = imp.getNFrames();
		int c1 = imp.getChannel();
		int z1 = imp.getSlice();
		int t1 = imp.getFrame();
		ArrayList list = new ArrayList();
		if (channels>1) list.add("channel");
		if (slices>1) list.add("slice");
		if (frames>1) list.add("frame");
		String[] choices = new String[list.size()];
		list.toArray(choices);
		String choice = choices[0];
		if (frames>1 && slices==1)
			choice = "frame";
		else if (slices>1)
			choice = "slice";
    	String options = Macro.getOptions();
		if (IJ.isMacro() && options!=null && !options.contains("delete=")) {
			if (options.contains("delete"))
    			Macro.setOptions("delete=frame");
    		else
    			Macro.setOptions("delete=slice");
    	}
		if (IJ.isMacro() && options==null && (imp.isComposite() && imp.getStackSize()==imp.getNChannels()))
			Macro.setOptions("delete=channel");
		GenericDialog gd = new GenericDialog("Delete");
		gd.addChoice("Delete current", choices, choice);
		gd.showDialog();
		if (gd.wasCanceled()) return;
		choice = gd.getNextChoice();
		if (!imp.lock()) return;
		ImageStack stack = imp.getStack();
		LUT[] luts = null;
		if (choice.equals("frame")) { // delete time point
			for (int z=slices; z>=1; z--) {
				int index = imp.getStackIndex(channels, z, t1);
				for (int i=0; i<channels; i++)
					stack.deleteSlice(index-i);
			}
			frames--;
		} else if (choice.equals("slice")) { // delete slice z1 from all volumes
			for (int t=frames; t>=1; t--) {
				int index = imp.getStackIndex(channels, z1, t);
				for (int i=0; i<channels; i++)
					stack.deleteSlice(index-i);
			}
			slices--;
		} else if (choice.equals("channel")) { // delete channe c1
			if (imp.isComposite())
				luts = ((CompositeImage)imp).getLuts();
			int index = imp.getStackIndex(c1, slices, frames);
			while (index>0) {
				stack.deleteSlice(index);
				index -= channels;
			}
			channels--;
		}
		//imp.setDimensions(channels, slices, frames);
		imp.setStack(stack, channels, slices, frames);
		if (luts!=null) {
			for (int i=c1-1; i<luts.length-1; i++)
				luts[i] = luts[i+1];
			CompositeImage cimp = (CompositeImage)imp;
			for (int c=1; c<=channels; c++)
				cimp.setChannelLut(luts[c-1], c);
			imp.updateAndDraw();
		}
		imp.unlock();
		imp.repaintWindow();
		imp.changes = true;
	}

	public void convertImagesToStack() {
		(new ImagesToStack()).run("");
	}

	public void convertStackToImages(ImagePlus imp) {
		if (nSlices<2) {
			IJ.wait(500);
			imp = IJ.getImage();
			nSlices = imp.getStackSize();
		}
		if (nSlices<2) {
			IJ.error("\"Convert Stack to Images\" requires a stack\n"+imp);
			return;
		}
		if (!imp.lock())
			return;
		ImageStack stack = imp.getStack();
		int size = stack.getSize();
		if (size>30 && !IJ.isMacro()) {
			boolean ok = IJ.showMessageWithCancel("Convert to Images?",
			"Are you sure you want to convert this\nstack to "
			+size+" separate windows?");
			if (!ok)
				{imp.unlock(); return;}
		}
		Calibration cal = imp.getCalibration();
		CompositeImage cimg = imp.isComposite()?(CompositeImage)imp:null;
		if (imp.getNChannels()!=imp.getStackSize()) cimg = null;
		Overlay overlay = imp.getOverlay();
		int lastImageID = 0;
		for (int i=1; i<=size; i++) {
			String label = stack.getShortSliceLabel(i);
			String title = label!=null&&!label.equals("")?label:getTitle(imp, i);
			ImageProcessor ip = stack.getProcessor(i);
			if (cimg!=null) {
				LUT lut = cimg.getChannelLut(i);
				if (lut!=null) {
					ip.setColorModel(lut);
					ip.setMinAndMax(lut.min, lut.max);
				}
			}
			ImagePlus imp2 = new ImagePlus(title, ip);
			imp2.setCalibration(cal);
			String info = stack.getSliceLabel(i);
			if (info!=null && !info.equals(label))
				imp2.setProperty("Info", info);
			imp2.setIJMenuBar(i==size);
			if (overlay!=null) {
				Overlay overlay2 = new Overlay();
				for (int j=0; j<overlay.size(); j++) {
					Roi roi = overlay.get(j);
					if (roi.getPosition()==i) {
						roi.setPosition(0);
						overlay2.add((Roi)roi.clone());
					}
				}
				if (overlay2.size()>0)
					imp2.setOverlay(overlay2);
			}
			if (i==size)
				lastImageID = imp2.getID();
			imp2.show();
		}
		imp.changes = false;
		ImageWindow win = imp.getWindow();
		if (win!=null)
			win.close();
		else if (Interpreter.isBatchMode())
			Interpreter.removeBatchModeImage(imp);
		imp.unlock();
	}

	String getTitle(ImagePlus imp, int n) {
		String digits = "00000000"+n;
		return imp.getShortTitle()+"-"+digits.substring(digits.length()-4,digits.length());
	}
	
}