diff --git a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp index 581d04645..75bb4ffe2 100644 --- a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp +++ b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp @@ -56,44 +56,41 @@ Ref GlobalHistogramBinarizer::getBlackRow(int y, Ref 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]++; + row_pixels = new unsigned char[width]; + row_pixels = getLuminanceSource()->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); } - int blackPoint = estimate(histogram) << LUMINANCE_SHIFT; + left = center; + center = right; + } - - Ref 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; - } - - 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; } } @@ -213,4 +210,3 @@ Ref GlobalHistogramBinarizer::createBinarizer(Ref so } } // namespace zxing - diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp index 486800301..8ff6f8c49 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp @@ -23,9 +23,10 @@ namespace zxing { -GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, - int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight), - left_(left), top_(top), width_(width), height_(height) { +GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, + int dataHeight, int left, int top, int width, int height) : greyData_(greyData), + dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width), + height_(height) { if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) { throw IllegalArgumentException("Crop rectangle does not fit within image data."); @@ -33,7 +34,6 @@ GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int } unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) { - if (y < 0 || y >= this->getHeight()) { throw IllegalArgumentException("Requested row is outside the image: " + y); } @@ -44,11 +44,9 @@ unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) { } int offset = (y + top_) * dataWidth_ + left_; memcpy(row, &greyData_[offset], width); - return row; } - unsigned char* GreyscaleLuminanceSource::getMatrix() { return greyData_; } @@ -56,10 +54,8 @@ unsigned char* GreyscaleLuminanceSource::getMatrix() { Ref GreyscaleLuminanceSource::rotateCounterClockwise() { // Intentionally flip the left, top, width, and height arguments as needed. dataWidth and // dataHeight are always kept unrotated. - return Ref (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, dataHeight_, - top_, left_, height_, width_)); + return Ref (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, + dataHeight_, top_, left_, height_, width_)); } - } /* namespace */ - diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h index 944b289f4..4729191f3 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.h @@ -35,11 +35,10 @@ class GreyscaleLuminanceSource : public LuminanceSource { int height_; public: - GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, - int width, int height); + GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, + int top, int width, int height); unsigned char* getRow(int y, unsigned char* row); - unsigned char* getMatrix(); bool isRotateSupported() const { @@ -55,7 +54,7 @@ class GreyscaleLuminanceSource : public LuminanceSource { } Ref rotateCounterClockwise(); - + }; } /* namespace */ diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp index 2f5d236c9..4e3aec81a 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp @@ -25,9 +25,10 @@ namespace zxing { // Note that dataWidth and dataHeight are not reversed, as we need to be able to traverse the // greyData correctly, which does not get rotated. -GreyscaleRotatedLuminanceSource::GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, - int left, int top, int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight), - left_(left), top_(top), width_(width), height_(height) { +GreyscaleRotatedLuminanceSource::GreyscaleRotatedLuminanceSource(unsigned char* greyData, + int dataWidth, int dataHeight, int left, int top, int width, int height) : greyData_(greyData), + dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width), + height_(height) { // Intentionally comparing to the opposite dimension since we're rotated. if (left + width > dataHeight || top + height > dataWidth) { diff --git a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h index c38d1a105..80535ab08 100644 --- a/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h +++ b/cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h @@ -35,11 +35,10 @@ class GreyscaleRotatedLuminanceSource : public LuminanceSource { int height_; public: - GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, - int width, int height); + GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, + int left, int top, int width, int height); unsigned char* getRow(int y, unsigned char* row); - unsigned char* getMatrix(); bool isRotateSupported() const { @@ -55,7 +54,7 @@ public: } }; + } /* namespace */ - #endif