mirror of
https://github.com/zxing/zxing.git
synced 2025-01-14 12:48:03 -08:00
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:
parent
09b0039530
commit
be31406b94
|
@ -56,44 +56,41 @@ Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> 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<BitArray> 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<Binarizer> GlobalHistogramBinarizer::createBinarizer(Ref<LuminanceSource> so
|
|||
}
|
||||
|
||||
} // namespace zxing
|
||||
|
||||
|
|
|
@ -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<LuminanceSource> GreyscaleLuminanceSource::rotateCounterClockwise() {
|
||||
// Intentionally flip the left, top, width, and height arguments as needed. dataWidth and
|
||||
// dataHeight are always kept unrotated.
|
||||
return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_, dataHeight_,
|
||||
top_, left_, height_, width_));
|
||||
return Ref<LuminanceSource> (new GreyscaleRotatedLuminanceSource(greyData_, dataWidth_,
|
||||
dataHeight_, top_, left_, height_, width_));
|
||||
}
|
||||
|
||||
|
||||
} /* namespace */
|
||||
|
||||
|
|
|
@ -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<LuminanceSource> rotateCounterClockwise();
|
||||
|
||||
|
||||
};
|
||||
|
||||
} /* namespace */
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue