Issue 508
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 12 Aug 2010 20:24:38 +0000 (20:24 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Thu, 12 Aug 2010 20:24:38 +0000 (20:24 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@1523 59b500cc-1b3d-0410-9834-0bbf25fbcc57

core/src/com/google/zxing/common/HybridBinarizer.java
cpp/core/src/zxing/common/HybridBinarizer.cpp
cpp/core/src/zxing/common/HybridBinarizer.h

index e3eb0a2..e89b06f 100644 (file)
@@ -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;
index 6cfec7c..91e9e05 100644 (file)
@@ -56,10 +56,16 @@ void HybridBinarizer::binarizeEntireImage() {
       int width = source->getWidth();
       int height = source->getHeight();
       int subWidth = width >> 3;
+      if (width & 0x07) {
+        subWidth++;
+      }
       int subHeight = height >> 3;
-      int *blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);
+      if (height & 0x07) {
+        subHeight++;
+      }
+      int *blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
       cached_matrix_.reset(new BitMatrix(width,height));
-      calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, cached_matrix_);
+      calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, cached_matrix_);
       delete [] blackPoints;
       delete [] luminances;
     } else {
@@ -70,9 +76,17 @@ void HybridBinarizer::binarizeEntireImage() {
 }
 
 void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight,
-    int stride, int blackPoints[], Ref<BitMatrix> matrix) {
+    int width, int height, int blackPoints[], Ref<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;
@@ -87,7 +101,7 @@ void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int
         sum += blackRow[left + 2];
       }
       int average = sum / 25;
-      threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
+      threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
     }
   }
 }
@@ -106,15 +120,23 @@ void HybridBinarizer::threshold8x8Block(unsigned char* luminances, int xoffset,
 }
 
 int* HybridBinarizer::calculateBlackPoints(unsigned char* 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;
index cac8d5b..2d7653d 100644 (file)
@@ -43,9 +43,9 @@ namespace zxing {
     void binarizeEntireImage();
     // We'll be using one-D arrays because C++ can't dynamically allocate 2D arrays
     int* calculateBlackPoints(unsigned char* luminances, int subWidth, int subHeight,
-      int stride);
+      int width, int height);
     void calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight,
-      int stride, int blackPoints[], Ref<BitMatrix> matrix);
+      int width, int height, int blackPoints[], Ref<BitMatrix> matrix);
     void threshold8x8Block(unsigned char* luminances, int xoffset, int yoffset, int threshold,
       int stride, Ref<BitMatrix> matrix);
        };