1 package ij.plugin.filter;
12 private boolean canceled;
13 private boolean first;
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;
34 public void run(ImageProcessor ip) {
41 if (arg.equals(
"add")) {
42 if (first) addValue = getValue(
"Add",
"Value: ", addValue, 0);
48 if (arg.equals(
"sub")) {
49 if (first) addValue = getValue(
"Subtract",
"Value: ", addValue, 0);
55 if (arg.equals(
"mul")) {
56 if (first) mulValue = getValue(
"Multiply",
"Value: ", mulValue, 2);
58 ip.multiply(mulValue);
62 if (arg.equals(
"div")) {
63 if (first) mulValue = getValue(
"Divide",
"Value: ", mulValue, 2);
65 if (mulValue!=0.0) ip.multiply(1.0/mulValue);
69 if (arg.equals(
"and")) {
70 if (first) andValue = getBinaryValue(
"AND",
"Value (binary): ", andValue);
73 ip.and(Integer.parseInt(andValue,2));
74 }
catch (NumberFormatException e) {
75 andValue = defaultAndValue;
76 IJ.
error(
"Binary number required");
81 if (arg.equals(
"or")) {
82 if (first) andValue = getBinaryValue(
"OR",
"Value (binary): ", andValue);
85 ip.or(Integer.parseInt(andValue,2));
86 }
catch (NumberFormatException e) {
87 andValue = defaultAndValue;
88 IJ.
error(
"Binary number required");
93 if (arg.equals(
"xor")) {
94 if (first) andValue = getBinaryValue(
"XOR",
"Value (binary): ", andValue);
97 ip.xor(Integer.parseInt(andValue,2));
98 }
catch (NumberFormatException e) {
99 andValue = defaultAndValue;
100 IJ.
error(
"Binary number required");
105 if (arg.equals(
"min")) {
106 if (first) minValue = getValue(
"Min",
"Value: ", minValue, 0);
107 if (canceled)
return;
109 if (!(ip instanceof ByteProcessor))
114 if (arg.equals(
"max")) {
115 if (first) maxValue = getValue(
"Max",
"Value: ", maxValue, 0);
116 if (canceled)
return;
118 if (!(ip instanceof ByteProcessor))
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;
131 ip.gamma(gammaValue);
135 if (arg.equals(
"log")) {
140 if (arg.equals(
"sqr")) {
145 if (arg.equals(
"sqrt")) {
150 if (arg.equals(
"reciprocal")) {
151 if (!(ip instanceof FloatProcessor)) {
152 IJ.
error(
"32-bit float image required");
156 float[] pixels = (
float[])ip.getPixels();
157 for (
int i=0; i<ip.getWidth()*ip.getHeight(); i++) {
159 pixels[i] = Float.NaN;
161 pixels[i] = 1f/pixels[i];
167 if (arg.equals(
"nan")) {
168 setBackgroundToNaN(ip);
172 if (arg.equals(
"abs")) {
173 if (!(ip instanceof FloatProcessor)) {
174 IJ.
error(
"32-bit float image required");
178 float[] pixels = (
float[])ip.getPixels();
179 for (
int i=0; i<ip.getWidth()*ip.getHeight(); i++)
180 pixels[i] = Math.abs(pixels[i]);
187 double getValue (String title, String prompt,
double defaultValue,
int digits) {
188 GenericDialog gd =
new GenericDialog(title);
189 gd.addNumericField(prompt, defaultValue, digits);
193 canceled = gd.wasCanceled();
196 return gd.getNextNumber();
199 String getBinaryValue (String title, String prompt, String defaultValue) {
200 GenericDialog gd =
new GenericDialog(title);
201 gd.addStringField(prompt, defaultValue);
205 canceled = gd.wasCanceled();
208 return gd.getNextString();
212 void setBackgroundToNaN(ImageProcessor ip) {
214 lower = ip.getMinThreshold();
215 upper = ip.getMaxThreshold();
217 if (lower==ImageProcessor.NO_THRESHOLD || !(ip instanceof FloatProcessor)) {
218 IJ.
error(
"Thresholded 32-bit float image required");
223 float[] pixels = (
float[])ip.getPixels();
224 int width = ip.getWidth();
225 int height = ip.getHeight();
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;