Closes Issue #607 : guard against very narrow images which would otherwise cause an exception

This commit is contained in:
Sean Owen 2016-06-10 13:58:51 +01:00
parent b1f4b5a269
commit 6cdc7493d5

View file

@ -62,22 +62,29 @@ public class GlobalHistogramBinarizer extends Binarizer {
byte[] localLuminances = source.getRow(y, luminances); byte[] localLuminances = source.getRow(y, luminances);
int[] localBuckets = buckets; int[] localBuckets = buckets;
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
int pixel = localLuminances[x] & 0xff; localBuckets[(localLuminances[x] & 0xff) >> LUMINANCE_SHIFT]++;
localBuckets[pixel >> LUMINANCE_SHIFT]++;
} }
int blackPoint = estimateBlackPoint(localBuckets); int blackPoint = estimateBlackPoint(localBuckets);
int left = localLuminances[0] & 0xff; if (width < 3) {
int center = localLuminances[1] & 0xff; // Special case for very small images
for (int x = 1; x < width - 1; x++) { for (int x = 0; x < width; x++) {
int right = localLuminances[x + 1] & 0xff; if ((localLuminances[x] & 0xff) < blackPoint) {
// A simple -1 4 -1 box filter with a weight of 2. row.set(x);
int luminance = ((center * 4) - left - right) / 2; }
if (luminance < blackPoint) { }
row.set(x); } else {
int left = localLuminances[0] & 0xff;
int center = localLuminances[1] & 0xff;
for (int x = 1; x < width - 1; x++) {
int right = localLuminances[x + 1] & 0xff;
// A simple -1 4 -1 box filter with a weight of 2.
if (((center * 4) - left - right) / 2 < blackPoint) {
row.set(x);
}
left = center;
center = right;
} }
left = center;
center = right;
} }
return row; return row;
} }