Added a --products_only flag to the CommandLineRunner.
[zxing.git] / core / src / com / google / zxing / common / LocalBlockBinarizer.java
index 7a19fea..8f37098 100644 (file)
@@ -17,7 +17,6 @@
 package com.google.zxing.common;
 
 import com.google.zxing.Binarizer;
-import com.google.zxing.ReaderException;
 import com.google.zxing.LuminanceSource;
 
 /**
@@ -28,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 {
@@ -38,12 +39,14 @@ public final class LocalBlockBinarizer extends Binarizer {
     super(source);
   }
 
-  public BitArray getBlackRow(int y, BitArray row) throws ReaderException {
+  // TODO: Consider a different strategy for 1D Readers.
+  public BitArray getBlackRow(int y, BitArray row) {
     binarizeEntireImage();
     return matrix.getRow(y, row);
   }
 
-  public BitMatrix getBlackMatrix() throws ReaderException {
+  // TODO: If getBlackRow() calculates its own values, removing sharpening here.
+  public BitMatrix getBlackMatrix() {
     binarizeEntireImage();
     return matrix;
   }
@@ -74,17 +77,17 @@ 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) {
     for (int y = 0; y < subHeight; y++) {
       for (int x = 0; x < subWidth; x++) {
-        int sum = 0;
         int left = (x > 1) ? x : 2;
         left = (left < subWidth - 2) ? left : subWidth - 3;
         int top = (y > 1) ? y : 2;
         top = (top < subHeight - 2) ? top : subHeight - 3;
+        int sum = 0;
         for (int z = -2; z <= 2; z++) {
           sum += blackPoints[top + z][left - 2];
           sum += blackPoints[top + z][left - 1];
@@ -93,7 +96,7 @@ public final class LocalBlockBinarizer extends Binarizer {
           sum += blackPoints[top + z][left + 2];
         }
         int average = sum / 25;
-        threshold8x8Block(luminances, x * 8, y * 8, average, stride, matrix);
+        threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
       }
     }
   }
@@ -122,7 +125,7 @@ public final class LocalBlockBinarizer extends Binarizer {
         int min = 255;
         int max = 0;
         for (int yy = 0; yy < 8; yy++) {
-          int offset = (y * 8 + yy) * stride + (x * 8);
+          int offset = ((y << 3) + yy) * stride + (x << 3);
           for (int xx = 0; xx < 8; xx++) {
             int pixel = luminances[offset + xx] & 0xff;
             sum += pixel;
@@ -153,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;
       }