-
Notifications
You must be signed in to change notification settings - Fork 221
/
Undo.java
215 lines (205 loc) · 5.75 KB
/
Undo.java
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
/**Implements the Edit/Undo command.*/
package ij;
import ij.process.*;
import ij.gui.*;
import ij.measure.Calibration;
import java.awt.*;
import java.awt.image.*;
/** This class consists of static methods and
fields that implement ImageJ's Undo command. */
public class Undo {
public static final int NOTHING = 0;
/** Undo using ImageProcessor.snapshot. */
public static final int FILTER = 1;
/** Undo using an ImageProcessor copy. */
public static final int TYPE_CONVERSION = 2;
public static final int PASTE = 3;
public static final int COMPOUND_FILTER = 4;
public static final int COMPOUND_FILTER_DONE = 5;
/** Undo using a single image, or composite color stack, copy (limited to 200MB). */
public static final int TRANSFORM = 6;
public static final int OVERLAY_ADDITION = 7;
public static final int ROI = 8;
public static final int MACRO = 9;
private static int whatToUndo = NOTHING;
private static int imageID;
private static ImageProcessor ipCopy = null;
private static ImagePlus impCopy;
private static Calibration calCopy;
private static Roi roiCopy;
private static double displayRangeMin, displayRangeMax;
private static LUT lutCopy;
private static Overlay overlayCopy;
public static void setup(int what, ImagePlus imp) {
if (imp==null) {
whatToUndo = NOTHING;
reset();
return;
}
if (IJ.debugMode) IJ.log("Undo.setup: "+what+" "+imp);
if (what==FILTER && whatToUndo==COMPOUND_FILTER)
return;
if (what==COMPOUND_FILTER_DONE) {
if (whatToUndo==COMPOUND_FILTER)
whatToUndo = what;
return;
}
whatToUndo = what;
imageID = imp.getID();
if (what==TYPE_CONVERSION) {
ipCopy = imp.getProcessor();
calCopy = (Calibration)imp.getCalibration().clone();
} else if (what==TRANSFORM) {
if ((!IJ.macroRunning()||Prefs.supportMacroUndo) && (imp.getStackSize()==1||imp.getDisplayMode()==IJ.COMPOSITE) && imp.getSizeInBytes()<209715200)
impCopy = imp.duplicate();
else
reset();
} else if (what==MACRO) {
ipCopy = imp.getProcessor().duplicate();
calCopy = (Calibration)imp.getCalibration().clone();
impCopy = null;
} else if (what==COMPOUND_FILTER) {
ImageProcessor ip = imp.getProcessor();
if (ip!=null)
ipCopy = ip.duplicate();
else
ipCopy = null;
} else if (what==OVERLAY_ADDITION) {
impCopy = null;
ipCopy = null;
} else if (what==ROI) {
impCopy = null;
ipCopy = null;
Roi roi = imp.getRoi();
if (roi!=null) {
roiCopy = (Roi)roi.clone();
roiCopy.setImage(null);
} else
whatToUndo = NOTHING;
} else {
ipCopy = null;
ImageProcessor ip = imp.getProcessor();
//lutCopy = (LUT)ip.getLut().clone();
}
}
public static void saveOverlay(ImagePlus imp) {
Overlay overlay = imp!=null?imp.getOverlay():null;
if (overlay!=null)
overlayCopy = overlay.duplicate();
else
overlayCopy = null;
}
public static void reset() {
if (IJ.debugMode) IJ.log("Undo.reset: "+ whatToUndo+" "+impCopy);
if (whatToUndo==COMPOUND_FILTER || whatToUndo==OVERLAY_ADDITION)
return;
whatToUndo = NOTHING;
imageID = 0;
ipCopy = null;
impCopy = null;
calCopy = null;
roiCopy = null;
lutCopy = null;
overlayCopy = null;
}
public static void undo() {
ImagePlus imp = WindowManager.getCurrentImage();
if (IJ.debugMode) IJ.log("Undo.undo: "+ whatToUndo+" "+imp+" "+impCopy);
if (imp==null || imageID!=imp.getID()) {
if (imp!=null && !IJ.macroRunning()) { // does image still have an undo buffer?
ImageProcessor ip2 = imp.getProcessor();
ip2.swapPixelArrays();
imp.updateAndDraw();
} else
reset();
return;
}
switch (whatToUndo) {
case FILTER:
if (overlayCopy!=null) {
Overlay overlay = imp.getOverlay();
if (overlay!=null) {
imp.setOverlay(overlayCopy);
overlayCopy = overlay.duplicate();
}
}
ImageProcessor ip = imp.getProcessor();
if (ip!=null) {
if (!IJ.macroRunning()) {
ip.swapPixelArrays();
imp.updateAndDraw();
return; // don't reset
} else {
ip.reset();
imp.updateAndDraw();
}
}
break;
case TYPE_CONVERSION:
case COMPOUND_FILTER:
case COMPOUND_FILTER_DONE:
if (ipCopy!=null) {
if (whatToUndo==TYPE_CONVERSION && calCopy!=null)
imp.setCalibration(calCopy);
if (swapImages(new ImagePlus("",ipCopy), imp)) {
imp.updateAndDraw();
return;
} else
imp.setProcessor(null, ipCopy);
}
break;
case TRANSFORM:
if (impCopy!=null)
imp.setStack(impCopy.getStack());
break;
case PASTE:
Roi roi = imp.getRoi();
if (roi!=null)
roi.abortPaste();
break;
case ROI:
Roi roiCopy2 = roiCopy;
setup(ROI, imp); // setup redo
imp.setRoi(roiCopy2);
return; //don't reset
case MACRO:
if (ipCopy!=null) {
imp.setProcessor(ipCopy);
if (calCopy!=null) imp.setCalibration(calCopy);
}
break;
case OVERLAY_ADDITION:
Overlay overlay = imp.getOverlay();
if (overlay==null)
{IJ.beep(); return;}
int size = overlay.size();
if (size>0) {
overlay.remove(size-1);
imp.draw();
} else {
IJ.beep();
return;
}
return; //don't reset
}
reset();
}
static boolean swapImages(ImagePlus imp1, ImagePlus imp2) {
if (imp1.getWidth()!=imp2.getWidth() || imp1.getHeight()!=imp2.getHeight()
|| imp1.getBitDepth()!=imp2.getBitDepth() || IJ.macroRunning())
return false;
ImageProcessor ip1 = imp1.getProcessor();
ImageProcessor ip2 = imp2.getProcessor();
double min1 = ip1.getMin();
double max1 = ip1.getMax();
double min2 = ip2.getMin();
double max2 = ip2.getMax();
ip2.setSnapshotPixels(ip1.getPixels());
ip2.swapPixelArrays();
ip1.setPixels(ip2.getSnapshotPixels());
ip2.setSnapshotPixels(null);
ip1.setMinAndMax(min2, max2);
ip2.setMinAndMax(min1, max1);
return true;
}
}