Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageMath.java
1 package ij.plugin.filter;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import java.awt.*;
6 
8 public class ImageMath implements PlugInFilter {
9 
10  private String arg;
11  private ImagePlus imp;
12  private boolean canceled;
13  private boolean first;
14  private double lower;
15  private double upper;
16 
17  private static double addValue = 25;
18  private static double mulValue = 1.25;
19  private static double minValue = 0;
20  private static double maxValue = 255;
21  private static final String defaultAndValue = "11110000";
22  private static String andValue = defaultAndValue;
23  private static final double defaultGammaValue = 0.5;
24  private static double gammaValue = defaultGammaValue;
25 
26  public int setup(String arg, ImagePlus imp) {
27  this.arg = arg;
28  this.imp = imp;
29  first = true;
30  IJ.register(ImageMath.class);
32  }
33 
34  public void run(ImageProcessor ip) {
35 
36  double value;
37 
38  if (canceled)
39  return;
40 
41  if (arg.equals("add")) {
42  if (first) addValue = getValue("Add", "Value: ", addValue, 0);
43  if (canceled) return;
44  ip.add(addValue);
45  return;
46  }
47 
48  if (arg.equals("sub")) {
49  if (first) addValue = getValue("Subtract", "Value: ", addValue, 0);
50  if (canceled) return;
51  ip.add(-addValue);
52  return;
53  }
54 
55  if (arg.equals("mul")) {
56  if (first) mulValue = getValue("Multiply", "Value: ", mulValue, 2);
57  if (canceled) return;
58  ip.multiply(mulValue);
59  return;
60  }
61 
62  if (arg.equals("div")) {
63  if (first) mulValue = getValue("Divide", "Value: ", mulValue, 2);
64  if (canceled) return;
65  if (mulValue!=0.0) ip.multiply(1.0/mulValue);
66  return;
67  }
68 
69  if (arg.equals("and")) {
70  if (first) andValue = getBinaryValue("AND", "Value (binary): ", andValue);
71  if (canceled) return;
72  try {
73  ip.and(Integer.parseInt(andValue,2));
74  } catch (NumberFormatException e) {
75  andValue = defaultAndValue;
76  IJ.error("Binary number required");
77  }
78  return;
79  }
80 
81  if (arg.equals("or")) {
82  if (first) andValue = getBinaryValue("OR", "Value (binary): ", andValue);
83  if (canceled) return;
84  try {
85  ip.or(Integer.parseInt(andValue,2));
86  } catch (NumberFormatException e) {
87  andValue = defaultAndValue;
88  IJ.error("Binary number required");
89  }
90  return;
91  }
92 
93  if (arg.equals("xor")) {
94  if (first) andValue = getBinaryValue("XOR", "Value (binary): ", andValue);
95  if (canceled) return;
96  try {
97  ip.xor(Integer.parseInt(andValue,2));
98  } catch (NumberFormatException e) {
99  andValue = defaultAndValue;
100  IJ.error("Binary number required");
101  }
102  return;
103  }
104 
105  if (arg.equals("min")) {
106  if (first) minValue = getValue("Min", "Value: ", minValue, 0);
107  if (canceled) return;
108  ip.min(minValue);
109  if (!(ip instanceof ByteProcessor))
110  ip.resetMinAndMax();
111  return;
112  }
113 
114  if (arg.equals("max")) {
115  if (first) maxValue = getValue("Max", "Value: ", maxValue, 0);
116  if (canceled) return;
117  ip.max(maxValue);
118  if (!(ip instanceof ByteProcessor))
119  ip.resetMinAndMax();
120  return;
121  }
122 
123  if (arg.equals("gamma")) {
124  if (first) gammaValue = getValue("Gamma", "Value (0.1-5.0): ", gammaValue, 2);
125  if (canceled) return;
126  if (gammaValue<0.1 || gammaValue>5.0) {
127  IJ.error("Gamma must be between 0.1 and 5.0");
128  gammaValue = defaultGammaValue;
129  return;
130  }
131  ip.gamma(gammaValue);
132  return;
133  }
134 
135  if (arg.equals("log")) {
136  ip.log();
137  return;
138  }
139 
140  if (arg.equals("sqr")) {
141  ip.sqr();
142  return;
143  }
144 
145  if (arg.equals("sqrt")) {
146  ip.sqrt();
147  return;
148  }
149 
150  if (arg.equals("reciprocal")) {
151  if (!(ip instanceof FloatProcessor)) {
152  IJ.error("32-bit float image required");
153  canceled = true;
154  return;
155  }
156  float[] pixels = (float[])ip.getPixels();
157  for (int i=0; i<ip.getWidth()*ip.getHeight(); i++) {
158  if (pixels[i]==0f)
159  pixels[i] = Float.NaN;
160  else
161  pixels[i] = 1f/pixels[i];
162  }
163  ip.resetMinAndMax();
164  return;
165  }
166 
167  if (arg.equals("nan")) {
168  setBackgroundToNaN(ip);
169  return;
170  }
171 
172  if (arg.equals("abs")) {
173  if (!(ip instanceof FloatProcessor)) {
174  IJ.error("32-bit float image required");
175  canceled = true;
176  return;
177  }
178  float[] pixels = (float[])ip.getPixels();
179  for (int i=0; i<ip.getWidth()*ip.getHeight(); i++)
180  pixels[i] = Math.abs(pixels[i]);
181  ip.resetMinAndMax();
182  return;
183  }
184 
185  }
186 
187  double getValue (String title, String prompt, double defaultValue, int digits) {
188  GenericDialog gd = new GenericDialog(title);
189  gd.addNumericField(prompt, defaultValue, digits);
190  gd.showDialog();
191  if (first) imp.startTiming();
192  first = false;
193  canceled = gd.wasCanceled();
194  if (canceled)
195  return defaultValue;
196  return gd.getNextNumber();
197  }
198 
199  String getBinaryValue (String title, String prompt, String defaultValue) {
200  GenericDialog gd = new GenericDialog(title);
201  gd.addStringField(prompt, defaultValue);
202  gd.showDialog();
203  if (first) imp.startTiming();
204  first = false;
205  canceled = gd.wasCanceled();
206  if (canceled)
207  return defaultValue;
208  return gd.getNextString();
209  }
210 
212  void setBackgroundToNaN(ImageProcessor ip) {
213  if (first) {
214  lower = ip.getMinThreshold();
215  upper = ip.getMaxThreshold();
216  first = false;
217  if (lower==ImageProcessor.NO_THRESHOLD || !(ip instanceof FloatProcessor)) {
218  IJ.error("Thresholded 32-bit float image required");
219  canceled = true;
220  return;
221  }
222  }
223  float[] pixels = (float[])ip.getPixels();
224  int width = ip.getWidth();
225  int height = ip.getHeight();
226  double v;
227  for (int y=0; y<height; y++) {
228  for (int x=0; x<width; x++) {
229  v = pixels[y*width+x];
230  if (v<lower || v>upper)
231  pixels[y*width+x] = Float.NaN;
232  }
233  }
234  ip.resetMinAndMax();
235  return;
236  }
237 
238 }