X-Git-Url: http://git.rot13.org/?p=zxing.git;a=blobdiff_plain;f=core%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fcommon%2FHybridBinarizer.java;h=e89b06f5d094684070a59312b0661cb8210465f0;hp=d8681483aa0b4ac8132688656d50d02a6884e28c;hb=71ee4dc882b86ac5ca2cb2848158076e434de2dc;hpb=9b7f1f6235cafd1f2c3154fbaad9d8ac7e570425 diff --git a/core/src/com/google/zxing/common/HybridBinarizer.java b/core/src/com/google/zxing/common/HybridBinarizer.java index d8681483..e89b06f5 100644 --- a/core/src/com/google/zxing/common/HybridBinarizer.java +++ b/core/src/com/google/zxing/common/HybridBinarizer.java @@ -18,7 +18,7 @@ package com.google.zxing.common; import com.google.zxing.Binarizer; import com.google.zxing.LuminanceSource; -import com.google.zxing.ReaderException; +import com.google.zxing.NotFoundException; /** * This class implements a local thresholding algorithm, which while slower than the @@ -49,7 +49,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { super(source); } - public BitMatrix getBlackMatrix() throws ReaderException { + public BitMatrix getBlackMatrix() throws NotFoundException { binarizeEntireImage(); return matrix; } @@ -61,7 +61,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { // Calculates the final BitMatrix once for all requests. This could be called once from the // constructor instead, but there are some advantages to doing it lazily, such as making // profiling easier, and not doing heavy lifting when callers don't expect it. - private void binarizeEntireImage() throws ReaderException { + private void binarizeEntireImage() throws NotFoundException { if (matrix == null) { LuminanceSource source = getLuminanceSource(); if (source.getWidth() >= MINIMUM_DIMENSION && source.getHeight() >= MINIMUM_DIMENSION) { @@ -69,11 +69,17 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { int width = source.getWidth(); int height = source.getHeight(); int subWidth = width >> 3; + if ((width & 0x07) != 0) { + subWidth++; + } int subHeight = height >> 3; - int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width); + if ((height & 0x07) != 0) { + subHeight++; + } + int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); matrix = new BitMatrix(width, height); - calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix); + calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, matrix); } else { // If the image is too small, fall back to the global histogram approach. matrix = super.getBlackMatrix(); @@ -82,13 +88,20 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { } // For each 8x8 block in the image, calculate the average black point using a 5x5 grid - // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels - // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not - // multiples of eight. In practice, leaving those pixels white does not seem to be a problem. + // of the blocks around it. Also handles the corner cases (fractional blocks are computed based + // on the last 8 pixels in the row/column which are also used in the previous block). private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight, - int stride, int[][] blackPoints, BitMatrix matrix) { + int width, int height, int[][] blackPoints, BitMatrix matrix) { for (int y = 0; y < subHeight; y++) { + int yoffset = y << 3; + if ((yoffset + 8) >= height) { + yoffset = height - 8; + } for (int x = 0; x < subWidth; x++) { + int xoffset = x << 3; + if ((xoffset + 8) >= width) { + xoffset = width - 8; + } int left = (x > 1) ? x : 2; left = (left < subWidth - 2) ? left : subWidth - 3; int top = (y > 1) ? y : 2; @@ -103,7 +116,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { sum += blackRow[left + 2]; } int average = sum / 25; - threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix); + threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix); } } } @@ -124,15 +137,23 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { // Calculates a single black point for each 8x8 block of pixels and saves it away. private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight, - int stride) { + int width, int height) { int[][] blackPoints = new int[subHeight][subWidth]; for (int y = 0; y < subHeight; y++) { + int yoffset = y << 3; + if ((yoffset + 8) >= height) { + yoffset = height - 8; + } for (int x = 0; x < subWidth; x++) { + int xoffset = x << 3; + if ((xoffset + 8) >= width) { + xoffset = width - 8; + } int sum = 0; int min = 255; int max = 0; for (int yy = 0; yy < 8; yy++) { - int offset = ((y << 3) + yy) * stride + (x << 3); + int offset = (yoffset + yy) * width + xoffset; for (int xx = 0; xx < 8; xx++) { int pixel = luminances[offset + xx] & 0xff; sum += pixel; @@ -148,7 +169,13 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer { // If the contrast is inadequate, use half the minimum, so that this block will be // treated as part of the white background, but won't drag down neighboring blocks // too much. - int average = (max - min > 24) ? (sum >> 6) : (min >> 1); + int average; + if (max - min > 24) { + average = sum >> 6; + } else { + // When min == max == 0, let average be 1 so all is black + average = max == 0 ? 1 : min >> 1; + } blackPoints[y][x] = average; } }