mirror of
https://github.com/zxing/zxing.git
synced 2024-09-21 05:57:45 -07:00
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:
parent
be31406b94
commit
b7f70012ca
|
@ -2,7 +2,6 @@
|
||||||
* LuminanceSource.cpp
|
* LuminanceSource.cpp
|
||||||
* zxing
|
* zxing
|
||||||
*
|
*
|
||||||
* Created by Ralf Kistner on 16/10/2009.
|
|
||||||
* Copyright 2008 ZXing authors All rights reserved.
|
* Copyright 2008 ZXing authors All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* 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* matrix = new unsigned char[width * height];
|
||||||
unsigned char* row = new unsigned char[width];
|
unsigned char* row = new unsigned char[width];
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
getRow(y, row);
|
row = getRow(y, row);
|
||||||
memcpy(&matrix[y * width], row, width);
|
memcpy(&matrix[y * width], row, width);
|
||||||
}
|
}
|
||||||
delete [] row;
|
delete [] row;
|
||||||
|
|
|
@ -32,6 +32,7 @@ public:
|
||||||
virtual int getWidth() const = 0;
|
virtual int getWidth() const = 0;
|
||||||
virtual int getHeight() 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* getRow(int y, unsigned char* row) = 0;
|
||||||
virtual unsigned char* getMatrix();
|
virtual unsigned char* getMatrix();
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ Ref<BitArray> GlobalHistogramBinarizer::getBlackRow(int y, Ref<BitArray> 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];
|
||||||
row_pixels = getLuminanceSource()->getRow(y, row_pixels);
|
row_pixels = source.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]++;
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
|
||||||
int rownum = height * y / 5;
|
int rownum = height * y / 5;
|
||||||
int right = (width << 2) / 5;
|
int right = (width << 2) / 5;
|
||||||
int sdf;
|
int sdf;
|
||||||
getLuminanceSource()->getRow(rownum,row);
|
row = source.getRow(rownum, row);
|
||||||
for (int x = width / 5; x < right; x++) {
|
for (int x = width / 5; x < right; x++) {
|
||||||
histogram[row[x] >> LUMINANCE_SHIFT]++;
|
histogram[row[x] >> LUMINANCE_SHIFT]++;
|
||||||
sdf = 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));
|
Ref<BitMatrix> matrix_ref(new BitMatrix(width, height));
|
||||||
BitMatrix& matrix = *matrix_ref;
|
BitMatrix& matrix = *matrix_ref;
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
getLuminanceSource()->getRow(y,row);
|
row = source.getRow(y, row);
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
if (row[x] <= blackPoint)
|
if (row[x] <= blackPoint)
|
||||||
matrix.set(x, y);
|
matrix.set(x, y);
|
||||||
|
@ -133,7 +133,6 @@ Ref<BitMatrix> GlobalHistogramBinarizer::getBlackMatrix() {
|
||||||
}
|
}
|
||||||
|
|
||||||
cached_matrix_ = matrix_ref;
|
cached_matrix_ = matrix_ref;
|
||||||
|
|
||||||
delete [] row;
|
delete [] row;
|
||||||
return matrix_ref;
|
return matrix_ref;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,13 @@ unsigned char* GreyscaleLuminanceSource::getRow(int y, unsigned char* row) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* GreyscaleLuminanceSource::getMatrix() {
|
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_;
|
return greyData_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,7 @@ void HybridBinarizer::binarizeEntireImage() {
|
||||||
cached_matrix_.reset(new BitMatrix(width,height));
|
cached_matrix_.reset(new BitMatrix(width,height));
|
||||||
calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, cached_matrix_);
|
calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, cached_matrix_);
|
||||||
delete [] blackPoints;
|
delete [] blackPoints;
|
||||||
|
delete [] luminances;
|
||||||
} else {
|
} else {
|
||||||
// If the image is too small, fall back to the global histogram approach.
|
// If the image is too small, fall back to the global histogram approach.
|
||||||
cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
|
cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
|
||||||
|
|
Loading…
Reference in a new issue