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: https://zxing.googlecode.com/svn/trunk@1521 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin@google.com 2010-08-11 22:10:29 +00:00
parent be31406b94
commit b7f70012ca
5 changed files with 13 additions and 6 deletions

View 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;

View 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();

View 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;
}

View 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_;
}

View 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());