Fixed issue 503, adding support for getMatrix() in GreyscaleLuminanceSource. Also...
authordswitkin@google.com <dswitkin@google.com@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 11 Aug 2010 22:10:29 +0000 (22:10 +0000)
committerdswitkin@google.com <dswitkin@google.com@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Wed, 11 Aug 2010 22:10:29 +0000 (22:10 +0000)
that getRow() and getMatrix() require the caller to delete the allocated memory.

git-svn-id: http://zxing.googlecode.com/svn/trunk@1521 59b500cc-1b3d-0410-9834-0bbf25fbcc57

cpp/core/src/zxing/LuminanceSource.cpp
cpp/core/src/zxing/LuminanceSource.h
cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp
cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp
cpp/core/src/zxing/common/HybridBinarizer.cpp

index 23b1e8c..0667d4c 100644 (file)
@@ -2,7 +2,6 @@
  *  LuminanceSource.cpp
  *  zxing
  *
- *  Created by Ralf Kistner on 16/10/2009.
  *  Copyright 2008 ZXing authors All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -35,7 +34,7 @@ unsigned char* LuminanceSource::getMatrix() {
   unsigned char* matrix = new unsigned char[width * height];
   unsigned char* row = new unsigned char[width];
   for (int y = 0; y < height; y++) {
-    getRow(y, row);
+    row = getRow(y, row);
     memcpy(&matrix[y * width], row, width);
   }
   delete [] row;
index fb54115..bbcfa26 100644 (file)
@@ -32,6 +32,7 @@ public:
   virtual int getWidth() const = 0;
   virtual int getHeight() const = 0;
 
+  // Callers take ownership of the returned memory and must call delete [] on it themselves.
   virtual unsigned char* getRow(int y, unsigned char* row) = 0;
   virtual unsigned char* getMatrix();
 
index 75bb4ff..cb7654f 100644 (file)
@@ -61,7 +61,7 @@ Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> row) {
   unsigned char* row_pixels = NULL;
   try {
     row_pixels = new unsigned char[width];
-    row_pixels = getLuminanceSource()->getRow(y, row_pixels);
+    row_pixels = source.getRow(y, row_pixels);
     for (int x = 0; x < width; x++) {
       histogram[row_pixels[x] >> LUMINANCE_SHIFT]++;
     }
@@ -113,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];
@@ -125,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);
@@ -133,7 +133,6 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
   }
 
   cached_matrix_ = matrix_ref;
-
   delete [] row;
   return matrix_ref;
 }
index 8ff6f8c..07c8ba2 100644 (file)
@@ -48,6 +48,13 @@ unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) {
 }
 
 unsigned char* GreyscaleLuminanceSource::getMatrix() {
+  if (left_ != 0 || top_ != 0 || dataWidth_ != width_ || dataHeight_ != height_) {
+    unsigned char* cropped = new unsigned char[width_ * height_];
+    for (int row = 0; row < height_; row++) {
+      memcpy(cropped + row * width_, greyData_ + (top_ + row) * dataWidth_ + left_, width_);
+    }
+    return cropped;
+  }
   return greyData_;
 }
 
index 7fdb664..6cfec7c 100644 (file)
@@ -61,6 +61,7 @@ void HybridBinarizer::binarizeEntireImage() {
       cached_matrix_.reset(new BitMatrix(width,height));
       calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, cached_matrix_);
       delete [] blackPoints;
+      delete [] luminances;
     } else {
       // If the image is too small, fall back to the global histogram approach.
       cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());