mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
C++ changes for r2277
git-svn-id: https://zxing.googlecode.com/svn/trunk@2330 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
67e77e3b88
commit
9a36418805
|
@ -27,8 +27,8 @@ using namespace zxing;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const int BLOCK_SIZE_POWER = 3;
|
const int BLOCK_SIZE_POWER = 3;
|
||||||
const int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER;
|
const int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00
|
||||||
const int BLOCK_SIZE_MASK = BLOCK_SIZE - 1;
|
const int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; // ...0011...11
|
||||||
const int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
|
const int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,20 +45,21 @@ HybridBinarizer::createBinarizer(Ref<LuminanceSource> source) {
|
||||||
return Ref<Binarizer> (new HybridBinarizer(source));
|
return Ref<Binarizer> (new HybridBinarizer(source));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the final BitMatrix once for all requests. This could be called once from the
|
||||||
|
* constructor instead, but there are some advantages to doing it lazily, such as making
|
||||||
|
* profiling easier, and not doing heavy lifting when callers don't expect it.
|
||||||
|
*/
|
||||||
Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
|
Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
|
||||||
// Calculates the final BitMatrix once for all requests. This could
|
|
||||||
// be called once from the constructor instead, but there are some
|
|
||||||
// advantages to doing it lazily, such as making profiling easier,
|
|
||||||
// and not doing heavy lifting when callers don't expect it.
|
|
||||||
if (matrix_) {
|
if (matrix_) {
|
||||||
return matrix_;
|
return matrix_;
|
||||||
}
|
}
|
||||||
LuminanceSource& source = *getLuminanceSource();
|
LuminanceSource& source = *getLuminanceSource();
|
||||||
if (source.getWidth() >= MINIMUM_DIMENSION &&
|
int width = source.getWidth();
|
||||||
source.getHeight() >= MINIMUM_DIMENSION) {
|
int height = source.getHeight();
|
||||||
|
if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) {
|
||||||
unsigned char* luminances = source.getMatrix();
|
unsigned char* luminances = source.getMatrix();
|
||||||
int width = source.getWidth();
|
|
||||||
int height = source.getHeight();
|
|
||||||
int subWidth = width >> BLOCK_SIZE_POWER;
|
int subWidth = width >> BLOCK_SIZE_POWER;
|
||||||
if ((width & BLOCK_SIZE_MASK) != 0) {
|
if ((width & BLOCK_SIZE_MASK) != 0) {
|
||||||
subWidth++;
|
subWidth++;
|
||||||
|
@ -93,6 +94,12 @@ Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
|
||||||
return matrix_;
|
return matrix_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
inline int cap(int value, int min, int max) {
|
||||||
|
return value < min ? min : value > max ? max : value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances,
|
HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances,
|
||||||
int subWidth,
|
int subWidth,
|
||||||
|
@ -103,18 +110,18 @@ HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances,
|
||||||
Ref<BitMatrix> const& matrix) {
|
Ref<BitMatrix> const& matrix) {
|
||||||
for (int y = 0; y < subHeight; y++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
if (yoffset + BLOCK_SIZE >= height) {
|
int maxYOffset = height - BLOCK_SIZE;
|
||||||
yoffset = height - BLOCK_SIZE;
|
if (yoffset > maxYOffset) {
|
||||||
|
yoffset = maxYOffset;
|
||||||
}
|
}
|
||||||
for (int x = 0; x < subWidth; x++) {
|
for (int x = 0; x < subWidth; x++) {
|
||||||
int xoffset = x << BLOCK_SIZE_POWER;
|
int xoffset = x << BLOCK_SIZE_POWER;
|
||||||
if (xoffset + BLOCK_SIZE >= width) {
|
int maxXOffset = width - BLOCK_SIZE;
|
||||||
xoffset = width - BLOCK_SIZE;
|
if (xoffset > maxXOffset) {
|
||||||
|
xoffset = maxXOffset;
|
||||||
}
|
}
|
||||||
int left = (x > 1) ? x : 2;
|
int left = cap(x, 2, subWidth - 3);
|
||||||
left = (left < subWidth - 2) ? left : subWidth - 3;
|
int top = cap(y, 2, subHeight - 3);
|
||||||
int top = (y > 1) ? y : 2;
|
|
||||||
top = (top < subHeight - 2) ? top : subHeight - 3;
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int z = -2; z <= 2; z++) {
|
for (int z = -2; z <= 2; z++) {
|
||||||
int *blackRow = &blackPoints[(top + z) * subWidth];
|
int *blackRow = &blackPoints[(top + z) * subWidth];
|
||||||
|
@ -125,17 +132,17 @@ HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances,
|
||||||
sum += blackRow[left + 2];
|
sum += blackRow[left + 2];
|
||||||
}
|
}
|
||||||
int average = sum / 25;
|
int average = sum / 25;
|
||||||
threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
|
thresholdBlock(luminances, xoffset, yoffset, average, width, matrix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HybridBinarizer::threshold8x8Block(unsigned char* luminances,
|
void HybridBinarizer::thresholdBlock(unsigned char* luminances,
|
||||||
int xoffset,
|
int xoffset,
|
||||||
int yoffset,
|
int yoffset,
|
||||||
int threshold,
|
int threshold,
|
||||||
int stride,
|
int stride,
|
||||||
Ref<BitMatrix> const& matrix) {
|
Ref<BitMatrix> const& matrix) {
|
||||||
for (int y = 0, offset = yoffset * stride + xoffset;
|
for (int y = 0, offset = yoffset * stride + xoffset;
|
||||||
y < BLOCK_SIZE;
|
y < BLOCK_SIZE;
|
||||||
y++, offset += stride) {
|
y++, offset += stride) {
|
||||||
|
@ -157,20 +164,25 @@ namespace {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWidth, int subHeight,
|
int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances,
|
||||||
int width, int height) {
|
int subWidth,
|
||||||
|
int subHeight,
|
||||||
|
int width,
|
||||||
|
int height) {
|
||||||
const int minDynamicRange = 24;
|
const int minDynamicRange = 24;
|
||||||
|
|
||||||
int *blackPoints = new int[subHeight * subWidth];
|
int *blackPoints = new int[subHeight * subWidth];
|
||||||
for (int y = 0; y < subHeight; y++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
if (yoffset + BLOCK_SIZE >= height) {
|
int maxYOffset = height - BLOCK_SIZE;
|
||||||
yoffset = height - BLOCK_SIZE;
|
if (yoffset > maxYOffset) {
|
||||||
|
yoffset = maxYOffset;
|
||||||
}
|
}
|
||||||
for (int x = 0; x < subWidth; x++) {
|
for (int x = 0; x < subWidth; x++) {
|
||||||
int xoffset = x << BLOCK_SIZE_POWER;
|
int xoffset = x << BLOCK_SIZE_POWER;
|
||||||
if (xoffset + BLOCK_SIZE >= width) {
|
int maxXOffset = width - BLOCK_SIZE;
|
||||||
xoffset = width - BLOCK_SIZE;
|
if (xoffset > maxXOffset) {
|
||||||
|
xoffset = maxXOffset;
|
||||||
}
|
}
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int min = 0xFF;
|
int min = 0xFF;
|
||||||
|
|
|
@ -55,12 +55,12 @@ namespace zxing {
|
||||||
int height,
|
int height,
|
||||||
int blackPoints[],
|
int blackPoints[],
|
||||||
Ref<BitMatrix> const& matrix);
|
Ref<BitMatrix> const& matrix);
|
||||||
void threshold8x8Block(unsigned char* luminances,
|
void thresholdBlock(unsigned char* luminances,
|
||||||
int xoffset,
|
int xoffset,
|
||||||
int yoffset,
|
int yoffset,
|
||||||
int threshold,
|
int threshold,
|
||||||
int stride,
|
int stride,
|
||||||
Ref<BitMatrix> const& matrix);
|
Ref<BitMatrix> const& matrix);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue