From: dswitkin@google.com Date: Wed, 11 Aug 2010 22:10:29 +0000 (+0000) Subject: Fixed issue 503, adding support for getMatrix() in GreyscaleLuminanceSource. Also... X-Git-Url: http://git.rot13.org/?p=zxing.git;a=commitdiff_plain;h=7d5a7a2cd5b4d694a69145afe550981bc8e0be94 Fixed issue 503, adding support for getMatrix() in GreyscaleLuminanceSource. Also clarified 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 --- diff --git a/cpp/core/src/zxing/LuminanceSource.cpp b/cpp/core/src/zxing/LuminanceSource.cpp index 23b1e8c5..0667d4cc 100644 --- a/cpp/core/src/zxing/LuminanceSource.cpp +++ b/cpp/core/src/zxing/LuminanceSource.cpp @@ -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; diff --git a/cpp/core/src/zxing/LuminanceSource.h b/cpp/core/src/zxing/LuminanceSource.h index fb541156..bbcfa26a 100644 --- a/cpp/core/src/zxing/LuminanceSource.h +++ b/cpp/core/src/zxing/LuminanceSource.h @@ -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(); diff --git a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp index 75bb4ffe..cb7654f8 100644 --- a/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp +++ b/cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp @@ -61,7 +61,7 @@ Ref GlobalHistogramBinarizer::getBlackRow(int y, Ref 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 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 GlobalHistogramBinarizer::getBlackMatrix() { Ref 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 GlobalHistogramBinarizer::getBlackMatrix() { } cached_matrix_ = matrix_ref; - delete [] row; return matrix_ref; } diff --git a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp index 8ff6f8c4..07c8ba26 100644 --- a/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp +++ b/cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp @@ -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_; } diff --git a/cpp/core/src/zxing/common/HybridBinarizer.cpp b/cpp/core/src/zxing/common/HybridBinarizer.cpp index 7fdb6642..6cfec7ce 100644 --- a/cpp/core/src/zxing/common/HybridBinarizer.cpp +++ b/cpp/core/src/zxing/common/HybridBinarizer.cpp @@ -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());