forked from jayunruh/Jay_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_hs_slice_jru_v1.java
More file actions
114 lines (108 loc) · 2.96 KB
/
Copy pathdelete_hs_slice_jru_v1.java
File metadata and controls
114 lines (108 loc) · 2.96 KB
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
/*******************************************************************************
* Copyright (c) 2012 Jay Unruh, Stowers Institute for Medical Research.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v2.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
******************************************************************************/
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
public class delete_hs_slice_jru_v1 implements PlugIn {
int slices,frames,channels;
public void run(String arg) {
GenericDialog gd=new GenericDialog("Options");
String[] choices={"Channel","Z Slice","Time Point"};
gd.addChoice("Delete Current",choices,choices[0]);
gd.showDialog(); if(gd.wasCanceled()){return;}
int index=gd.getNextChoiceIndex();
ImagePlus imp=WindowManager.getCurrentImage();
channels=imp.getNChannels();
slices=imp.getNSlices();
frames=imp.getNFrames();
int currchan=imp.getChannel()-1;
int currslice=imp.getSlice()-1;
int currframe=imp.getFrame()-1;
if(!imp.lock()){return;}
if(index==0){
delete_channel(imp,currchan);
}
if(index==1){
delete_slice(imp,currslice);
}
if(index==2){
delete_frame(imp,currframe);
}
imp.unlock();
imp.updateAndDraw();
}
private void delete_channel(ImagePlus imp,int channel){
ImageStack stack=imp.getStack();
LUT[] luts=null;
if(imp.isComposite()){
luts=new LUT[channels-1];
LUT[] oldluts=((CompositeImage)imp).getLuts();
int counter=0;
for(int i=0;i<channels;i++){
if(i!=channel){
luts[counter]=oldluts[i];
counter++;
}
}
}
int counter=0;
for(int i=0;i<frames;i++){
for(int j=0;j<slices;j++){
for(int k=0;k<channels;k++){
if(k==channel){
stack.deleteSlice(k+j*channels+i*slices*channels+1-counter);
counter++;
}
}
}
}
boolean composite=imp.isComposite();
imp.setStack(stack);
imp.setDimensions(channels-1,slices,frames);
if(composite){
((CompositeImage)imp).setLuts(luts);
} else {
if(luts!=null && (channels-1)==1){
imp.getProcessor().setColorModel(luts[0]);
}
}
imp.setSlice(1);
}
private void delete_slice(ImagePlus imp,int slice){
ImageStack stack=imp.getStack();
int counter=0;
for(int i=0;i<frames;i++){
for(int j=0;j<slices;j++){
for(int k=0;k<channels;k++){
if(j==slice){
stack.deleteSlice(k+j*channels+i*slices*channels+1-counter);
counter++;
}
}
}
}
imp.setDimensions(channels,slices-1,frames);
}
private void delete_frame(ImagePlus imp,int frame){
ImageStack stack=imp.getStack();
int counter=0;
for(int i=0;i<frames;i++){
for(int j=0;j<slices;j++){
for(int k=0;k<channels;k++){
if(i==frame){
stack.deleteSlice(k+j*channels+i*slices*channels+1-counter);
counter++;
}
}
}
}
imp.setDimensions(channels,slices,frames-1);
}
}