Skip to content

Commit a178077

Browse files
committed
DefaultASCII: reorganize math
1 parent bacd4e5 commit a178077

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ public static <T extends RealType<T>> String ascii(
117117
// normalized = (value - min) / (max - min)
118118
tmp.set(cursor.get());
119119
tmp.sub(min);
120-
// NB: if a value is below min we set tmp to zero.
121-
if (tmp.compareTo(zero) < 0) tmp.setZero();
122-
final double normalized = tmp.getRealDouble() / span.getRealDouble();
120+
tmp.div(span);
121+
final double normalized = tmp.getRealDouble();
123122

124123
final int charLen = CHARS.length();
125-
final int charIndex = (int) (charLen * normalized);
124+
int charIndex = (int) (charLen * normalized);
125+
126+
// NB: clamp charIndex to [0, charLen) to prevent
127+
// StringIndexOutOfBoundsExceptions
128+
if (charIndex < 0) charIndex = 0;
129+
if (charIndex >= charLen) charIndex = charLen - 1;
130+
126131
c[index] = CHARS.charAt(charIndex < charLen ? charIndex : charLen - 1);
127132
}
128133

0 commit comments

Comments
 (0)