File tree Expand file tree Collapse file tree 1 file changed +9
-4
lines changed
src/main/java/net/imagej/ops/image/ascii Expand file tree Collapse file tree 1 file changed +9
-4
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments