Skip to content

Commit f717bad

Browse files
gselzerctrueden
authored andcommitted
Use helper Ops to simplify code
1 parent fb40484 commit f717bad

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/main/java/net/imagej/ops/image/ascii/DefaultASCII.java

+23-27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import net.imglib2.Cursor;
3737
import net.imglib2.IterableInterval;
3838
import net.imglib2.type.numeric.RealType;
39+
import net.imglib2.type.numeric.real.DoubleType;
3940
import net.imglib2.util.Pair;
4041

4142
import org.scijava.plugin.Parameter;
@@ -79,55 +80,50 @@ public String calculate(final IterableInterval<T> input) {
7980
if (min == null) min = minMax.getA();
8081
if (max == null) max = minMax.getB();
8182
}
82-
return ascii(input, min, max);
83+
84+
DoubleType minSource = new DoubleType(min.getRealDouble());
85+
DoubleType maxSource = new DoubleType(max.getRealDouble());
86+
DoubleType minTarget = new DoubleType(0);
87+
DoubleType maxTarget = new DoubleType(CHARS.length());
88+
89+
IterableInterval<DoubleType> converted = ops().convert().float64(input);
90+
IterableInterval<DoubleType> normalized = ops().image().normalize(converted,
91+
minSource, maxSource, minTarget, maxTarget);
92+
93+
return ascii(normalized);
8394
}
8495

8596
// -- Utility methods --
8697

87-
public static <T extends RealType<T>> String ascii(
88-
final IterableInterval<T> image, final T min, final T max)
89-
{
98+
public static String ascii(final IterableInterval<DoubleType> image) {
9099
final long dim0 = image.dimension(0);
91100
final long dim1 = image.dimension(1);
92-
// TODO: Check bounds.
101+
93102
final int w = (int) (dim0 + 1);
94103
final int h = (int) dim1;
95104

96-
// span = max - min
97-
final T span = max.copy();
98-
span.sub(min);
99-
100105
// allocate ASCII character array
101106
final char[] c = new char[w * h];
102107
for (int y = 1; y <= h; y++) {
103108
c[w * y - 1] = '\n'; // end of row
104109
}
105110

106111
// loop over all available positions
107-
final Cursor<T> cursor = image.localizingCursor();
112+
final Cursor<DoubleType> cursor = image.localizingCursor();
108113
final int[] pos = new int[image.numDimensions()];
109-
final T tmp = image.firstElement().copy();
110-
final T zero = tmp.copy();
111-
zero.setZero();
112114
while (cursor.hasNext()) {
113115
cursor.fwd();
114116
cursor.localize(pos);
115117
final int index = w * pos[1] + pos[0];
116118

117-
// normalized = (value - min) / (max - min)
118-
tmp.set(cursor.get());
119-
tmp.sub(min);
120-
final double normalized = tmp.getRealDouble() / span.getRealDouble();
121-
122-
final int charLen = CHARS.length();
123-
int charIndex = (int) (charLen * normalized);
124-
125-
// NB: clamp charIndex to [0, charLen) to prevent
126-
// StringIndexOutOfBoundsExceptions
127-
if (charIndex < 0) charIndex = 0;
128-
if (charIndex >= charLen) charIndex = charLen - 1;
129-
130-
c[index] = CHARS.charAt(charIndex);
119+
// grab the value from the normalized image, convert it to an ASCII char.
120+
// N.B. if the original value was at the max for the type range it will be
121+
// equal to the length of the char array after normalization. Thus to
122+
// prevent an exception when converting to ASCII we subtract one when the
123+
// normalized image value is equal to the length.
124+
int val = (int) cursor.get().getRealDouble();
125+
if (val == CHARS.length()) val--;
126+
c[index] = CHARS.charAt(val);
131127
}
132128

133129
return new String(c);

0 commit comments

Comments
 (0)