GreyscaleRotatedLuminanceSource: implemented getMatrix()
[zxing.git] / cpp / core / src / zxing / common / GlobalHistogramBinarizer.cpp
index 1d2ebfe..cb7654f 100644 (file)
@@ -41,7 +41,11 @@ GlobalHistogramBinarizer::~GlobalHistogramBinarizer() {
 
 Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> row) {
   if (y == cached_row_num_) {
-    return cached_row_;
+    if (cached_row_ != NULL) {
+      return cached_row_;
+    } else {
+      throw IllegalArgumentException("Too little dynamic range in luminance");
+    }
   }
 
   vector<int> histogram(LUMINANCE_BUCKETS, 0);
@@ -52,44 +56,41 @@ Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> row) {
   } else {
     row->clear();
   }
-  
+
   //TODO(flyashi): cache this instead of allocating and deleting per row
   unsigned char* row_pixels = NULL;
   try {
-      row_pixels = new unsigned char[width];
-      getLuminanceSource()->getRow(y,row_pixels);
-      for (int x = 0; x < width; x++) {
-          histogram[row_pixels[x] >> LUMINANCE_SHIFT]++;
-      }
-      int blackPoint = estimate(histogram) << LUMINANCE_SHIFT;
-
-
-      Ref<BitArray> array_ref(new BitArray(width));
-      BitArray& array = *array_ref;
-
-      int left = row_pixels[0];
-      int center = row_pixels[1];
-      for (int x = 1; x < width - 1; x++) {
-          int right = row_pixels[x + 1];
-          // A simple -1 4 -1 box filter with a weight of 2.
-          int luminance = ((center << 2) - left - right) >> 1;
-          if (luminance < blackPoint) {
-              array.set(x);
-          }
-          left = center;
-          center = right;
+    row_pixels = new unsigned char[width];
+    row_pixels = source.getRow(y, row_pixels);
+    for (int x = 0; x < width; x++) {
+      histogram[row_pixels[x] >> LUMINANCE_SHIFT]++;
+    }
+    int blackPoint = estimate(histogram) << LUMINANCE_SHIFT;
+
+    BitArray& array = *row;
+    int left = row_pixels[0];
+    int center = row_pixels[1];
+    for (int x = 1; x < width - 1; x++) {
+      int right = row_pixels[x + 1];
+      // A simple -1 4 -1 box filter with a weight of 2.
+      int luminance = ((center << 2) - left - right) >> 1;
+      if (luminance < blackPoint) {
+        array.set(x);
       }
+      left = center;
+      center = right;
+    }
 
-      cached_row_ = array_ref;
-      cached_row_num_ = y;
-      delete [] row_pixels;
-      return array_ref;
+    cached_row_ = row;
+    cached_row_num_ = y;
+    delete [] row_pixels;
+    return row;
   } catch (IllegalArgumentException const& iae) {
-      // Cache the fact that this row failed.
-      cached_row_ = NULL;
-      cached_row_num_ = y;
-      delete [] row_pixels;
-      throw iae;
+    // Cache the fact that this row failed.
+    cached_row_ = NULL;
+    cached_row_num_ = y;
+    delete [] row_pixels;
+    throw iae;
   }
 }
 
@@ -112,7 +113,7 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
     int rownum = height * y / 5;
     int right = (width << 2) / 5;
     int sdf;
-    getLuminanceSource()->getRow(rownum,row);
+    row = source.getRow(rownum, row);
     for (int x = width / 5; x < right; x++) {
       histogram[row[x] >> LUMINANCE_SHIFT]++;
       sdf = histogram[row[x] >> LUMINANCE_SHIFT];
@@ -124,7 +125,7 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
   Ref<BitMatrix> matrix_ref(new BitMatrix(width, height));
   BitMatrix& matrix = *matrix_ref;
   for (int y = 0; y < height; y++) {
-    getLuminanceSource()->getRow(y,row);
+    row = source.getRow(y, row);
     for (int x = 0; x < width; x++) {
       if (row[x] <= blackPoint)
         matrix.set(x, y);
@@ -132,7 +133,6 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
   }
 
   cached_matrix_ = matrix_ref;
-
   delete [] row;
   return matrix_ref;
 }
@@ -209,4 +209,3 @@ Ref<Binarizer> GlobalHistogramBinarizer::createBinarizer(Ref<LuminanceSource> so
 }
 
 } // namespace zxing
-