Added a --products_only flag to the CommandLineRunner.
[zxing.git] / core / src / com / google / zxing / common / LocalBlockBinarizer.java
index 55be9b8..8f37098 100644 (file)
@@ -27,6 +27,8 @@ import com.google.zxing.LuminanceSource;
  * However it tends to produce artifacts on lower frequency images and is therefore not
  * a good general purpose binarizer for uses outside ZXing.
  *
+ * NOTE: This class is still experimental and may not be ready for prime time yet.
+ *
  * @author dswitkin@google.com (Daniel Switkin)
  */
 public final class LocalBlockBinarizer extends Binarizer {
@@ -37,11 +39,13 @@ public final class LocalBlockBinarizer extends Binarizer {
     super(source);
   }
 
+  // TODO: Consider a different strategy for 1D Readers.
   public BitArray getBlackRow(int y, BitArray row) {
     binarizeEntireImage();
     return matrix.getRow(y, row);
   }
 
+  // TODO: If getBlackRow() calculates its own values, removing sharpening here.
   public BitMatrix getBlackMatrix() {
     binarizeEntireImage();
     return matrix;
@@ -73,7 +77,7 @@ public final class LocalBlockBinarizer extends Binarizer {
 
   // 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 dimsions are not
+  // 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.
   private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight,
       int stride, int[][] blackPoints, BitMatrix matrix) {
@@ -152,7 +156,14 @@ public final class LocalBlockBinarizer extends Binarizer {
       int center = luminances[offset + 1] & 0xff;
       for (int x = 1; x < width - 1; x++) {
         int right = luminances[offset + x + 1] & 0xff;
-        luminances[x] = (byte)(((center << 2) - left - right) >> 1);
+        int pixel = ((center << 2) - left - right) >> 1;
+        // Must clamp values to 0..255 so they will fit in a byte.
+        if (pixel > 255) {
+          pixel = 255;
+        } else if (pixel < 0) {
+          pixel = 0;
+        }
+        luminances[offset + x] = (byte)pixel;
         left = center;
         center = right;
       }