|
36 | 36 | import net.imglib2.Cursor;
|
37 | 37 | import net.imglib2.IterableInterval;
|
38 | 38 | import net.imglib2.type.numeric.RealType;
|
| 39 | +import net.imglib2.type.numeric.real.DoubleType; |
39 | 40 | import net.imglib2.util.Pair;
|
40 | 41 |
|
41 | 42 | import org.scijava.plugin.Parameter;
|
@@ -79,55 +80,50 @@ public String calculate(final IterableInterval<T> input) {
|
79 | 80 | if (min == null) min = minMax.getA();
|
80 | 81 | if (max == null) max = minMax.getB();
|
81 | 82 | }
|
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); |
83 | 94 | }
|
84 | 95 |
|
85 | 96 | // -- Utility methods --
|
86 | 97 |
|
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) { |
90 | 99 | final long dim0 = image.dimension(0);
|
91 | 100 | final long dim1 = image.dimension(1);
|
92 |
| - // TODO: Check bounds. |
| 101 | + |
93 | 102 | final int w = (int) (dim0 + 1);
|
94 | 103 | final int h = (int) dim1;
|
95 | 104 |
|
96 |
| - // span = max - min |
97 |
| - final T span = max.copy(); |
98 |
| - span.sub(min); |
99 |
| - |
100 | 105 | // allocate ASCII character array
|
101 | 106 | final char[] c = new char[w * h];
|
102 | 107 | for (int y = 1; y <= h; y++) {
|
103 | 108 | c[w * y - 1] = '\n'; // end of row
|
104 | 109 | }
|
105 | 110 |
|
106 | 111 | // loop over all available positions
|
107 |
| - final Cursor<T> cursor = image.localizingCursor(); |
| 112 | + final Cursor<DoubleType> cursor = image.localizingCursor(); |
108 | 113 | final int[] pos = new int[image.numDimensions()];
|
109 |
| - final T tmp = image.firstElement().copy(); |
110 |
| - final T zero = tmp.copy(); |
111 |
| - zero.setZero(); |
112 | 114 | while (cursor.hasNext()) {
|
113 | 115 | cursor.fwd();
|
114 | 116 | cursor.localize(pos);
|
115 | 117 | final int index = w * pos[1] + pos[0];
|
116 | 118 |
|
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); |
131 | 127 | }
|
132 | 128 |
|
133 | 129 | return new String(c);
|
|
0 commit comments