Removed an extra BitArray, honored the result of LuminanceSource.getRow(), and fixed

a bunch of formatting.


git-svn-id: https://zxing.googlecode.com/svn/trunk@1520 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin@google.com 2010-08-11 15:17:35 +00:00
parent 09b0039530
commit be31406b94
5 changed files with 45 additions and 54 deletions

View file

@ -60,40 +60,37 @@ Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> row) {
//TODO(flyashi): cache this instead of allocating and deleting per row //TODO(flyashi): cache this instead of allocating and deleting per row
unsigned char* row_pixels = NULL; unsigned char* row_pixels = NULL;
try { try {
row_pixels = new unsigned char[width]; row_pixels = new unsigned char[width];
getLuminanceSource()->getRow(y,row_pixels); row_pixels = getLuminanceSource()->getRow(y, row_pixels);
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
histogram[row_pixels[x] >> LUMINANCE_SHIFT]++; 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;
}
cached_row_ = row;
Ref<BitArray> array_ref(new BitArray(width)); cached_row_num_ = y;
BitArray& array = *array_ref; delete [] row_pixels;
return 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;
} catch (IllegalArgumentException const& iae) { } catch (IllegalArgumentException const& iae) {
// Cache the fact that this row failed. // Cache the fact that this row failed.
cached_row_ = NULL; cached_row_ = NULL;
cached_row_num_ = y; cached_row_num_ = y;
delete [] row_pixels; delete [] row_pixels;
throw iae; throw iae;
} }
} }
@ -213,4 +210,3 @@ Ref<Binarizer> GlobalHistogramBinarizer::createBinarizer(Ref<LuminanceSource> so
} }
} // namespace zxing } // namespace zxing

View file

@ -23,9 +23,10 @@
namespace zxing { namespace zxing {
GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, GreyscaleLuminanceSource::GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth,
int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight), int dataHeight, int left, int top, int width, int height) : greyData_(greyData),
left_(left), top_(top), width_(width), height_(height) { dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width),
height_(height) {
if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) { if (left + width > dataWidth || top + height > dataHeight || top < 0 || left < 0) {
throw IllegalArgumentException("Crop rectangle does not fit within image data."); 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) { unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) {
if (y < 0 || y >= this->getHeight()) { if (y < 0 || y >= this->getHeight()) {
throw IllegalArgumentException("Requested row is outside the image: " + y); 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_; int offset = (y + top_) * dataWidth_ + left_;
memcpy(row, &greyData_[offset], width); memcpy(row, &greyData_[offset], width);
return row; return row;
} }
unsigned char* GreyscaleLuminanceSource::getMatrix() { unsigned char* GreyscaleLuminanceSource::getMatrix() {
return greyData_; return greyData_;
} }
@ -56,10 +54,8 @@ unsigned char* GreyscaleLuminanceSource::getMatrix() {
Ref<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() { Ref<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() {
// Intentionally flip the left, top, width, and height arguments as needed. dataWidth and // Intentionally flip the left, top, width, and height arguments as needed. dataWidth and
// dataHeight are always kept unrotated. // dataHeight are always kept unrotated.
return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, dataHeight_, return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_,
top_, left_, height_, width_)); dataHeight_, top_, left_, height_, width_));
} }
} /* namespace */ } /* namespace */

View file

@ -35,11 +35,10 @@ class GreyscaleLuminanceSource : public LuminanceSource {
int height_; int height_;
public: public:
GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, GreyscaleLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left,
int width, int height); int top, int width, int height);
unsigned char* getRow(int y, unsigned char* row); unsigned char* getRow(int y, unsigned char* row);
unsigned char* getMatrix(); unsigned char* getMatrix();
bool isRotateSupported() const { bool isRotateSupported() const {

View file

@ -25,9 +25,10 @@ namespace zxing {
// Note that dataWidth and dataHeight are not reversed, as we need to be able to traverse the // Note that dataWidth and dataHeight are not reversed, as we need to be able to traverse the
// greyData correctly, which does not get rotated. // greyData correctly, which does not get rotated.
GreyscaleRotatedLuminanceSource::GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, GreyscaleRotatedLuminanceSource::GreyscaleRotatedLuminanceSource(unsigned char* greyData,
int left, int top, int width, int height) : greyData_(greyData), dataWidth_(dataWidth), dataHeight_(dataHeight), int dataWidth, int dataHeight, int left, int top, int width, int height) : greyData_(greyData),
left_(left), top_(top), width_(width), height_(height) { dataWidth_(dataWidth), dataHeight_(dataHeight), left_(left), top_(top), width_(width),
height_(height) {
// Intentionally comparing to the opposite dimension since we're rotated. // Intentionally comparing to the opposite dimension since we're rotated.
if (left + width > dataHeight || top + height > dataWidth) { if (left + width > dataHeight || top + height > dataWidth) {

View file

@ -35,11 +35,10 @@ class GreyscaleRotatedLuminanceSource : public LuminanceSource {
int height_; int height_;
public: public:
GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight, int left, int top, GreyscaleRotatedLuminanceSource(unsigned char* greyData, int dataWidth, int dataHeight,
int width, int height); int left, int top, int width, int height);
unsigned char* getRow(int y, unsigned char* row); unsigned char* getRow(int y, unsigned char* row);
unsigned char* getMatrix(); unsigned char* getMatrix();
bool isRotateSupported() const { bool isRotateSupported() const {
@ -55,7 +54,7 @@ public:
} }
}; };
} /* namespace */ } /* namespace */
#endif #endif