C++ changes that match r1969

git-svn-id: https://zxing.googlecode.com/svn/trunk@1972 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2011-10-14 13:03:03 +00:00
parent 6d2e7ab5ca
commit 69988df528

View file

@ -22,14 +22,19 @@
#include <zxing/common/IllegalArgumentException.h> #include <zxing/common/IllegalArgumentException.h>
namespace zxing {
using namespace std; using namespace std;
using namespace zxing;
static const int MINIMUM_DIMENSION = 40; namespace {
const int LUMINANCE_BITS = 5;
const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
static const int LUMINANCE_BITS = 5; const int BLOCK_SIZE_POWER = 3;
static const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; const int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER;
static const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; const int BLOCK_SIZE_MASK = BLOCK_SIZE - 1;
const int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
}
HybridBinarizer::HybridBinarizer(Ref<LuminanceSource> source) : HybridBinarizer::HybridBinarizer(Ref<LuminanceSource> source) :
GlobalHistogramBinarizer(source), matrix_(NULL), cached_row_(NULL), cached_row_num_(-1) { GlobalHistogramBinarizer(source), matrix_(NULL), cached_row_(NULL), cached_row_num_(-1) {
@ -56,12 +61,12 @@ Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
unsigned char* luminances = source.getMatrix(); unsigned char* luminances = source.getMatrix();
int width = source.getWidth(); int width = source.getWidth();
int height = source.getHeight(); int height = source.getHeight();
int subWidth = width >> 3; int subWidth = width >> BLOCK_SIZE_POWER;
if ((width & 0x07) != 0) { if ((width & BLOCK_SIZE_MASK) != 0) {
subWidth++; subWidth++;
} }
int subHeight = height >> 3; int subHeight = height >> BLOCK_SIZE_POWER;
if ((height & 0x07) != 0) { if ((height & BLOCK_SIZE_MASK) != 0) {
subHeight++; subHeight++;
} }
int* blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height); int* blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
@ -85,14 +90,14 @@ Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight, void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight,
int width, int height, int blackPoints[], Ref<BitMatrix> matrix) { int width, int height, int blackPoints[], Ref<BitMatrix> matrix) {
for (int y = 0; y < subHeight; y++) { for (int y = 0; y < subHeight; y++) {
int yoffset = y << 3; int yoffset = y << BLOCK_SIZE_POWER;
if (yoffset + 8 >= height) { if (yoffset + BLOCK_SIZE >= height) {
yoffset = height - 8; yoffset = height - BLOCK_SIZE;
} }
for (int x = 0; x < subWidth; x++) { for (int x = 0; x < subWidth; x++) {
int xoffset = x << 3; int xoffset = x << BLOCK_SIZE_POWER;
if (xoffset + 8 >= width) { if (xoffset + BLOCK_SIZE >= width) {
xoffset = width - 8; xoffset = width - BLOCK_SIZE;
} }
int left = (x > 1) ? x : 2; int left = (x > 1) ? x : 2;
left = (left < subWidth - 2) ? left : subWidth - 3; left = (left < subWidth - 2) ? left : subWidth - 3;
@ -115,9 +120,10 @@ void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int
void HybridBinarizer::threshold8x8Block(unsigned char* luminances, int xoffset, int yoffset, int threshold, void HybridBinarizer::threshold8x8Block(unsigned char* luminances, int xoffset, int yoffset, int threshold,
int stride, Ref<BitMatrix> matrix) { int stride, Ref<BitMatrix> matrix) {
for (int y = 0; y < 8; y++) { for (int y = 0, offset = yoffset * stride + xoffset;
int offset = (yoffset + y) * stride + xoffset; y < BLOCK_SIZE;
for (int x = 0; x < 8; x++) { y++, offset += stride) {
for (int x = 0; x < BLOCK_SIZE; x++) {
int pixel = luminances[offset + x] & 0xff; int pixel = luminances[offset + x] & 0xff;
if (pixel < threshold) { if (pixel < threshold) {
matrix->set(xoffset + x, yoffset + y); matrix->set(xoffset + x, yoffset + y);
@ -130,22 +136,23 @@ int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWid
int width, int height) { int width, int height) {
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 << 3; int yoffset = y << BLOCK_SIZE_POWER;
if (yoffset + 8 >= height) { if (yoffset + BLOCK_SIZE >= height) {
yoffset = height - 8; yoffset = height - BLOCK_SIZE;
} }
for (int x = 0; x < subWidth; x++) { for (int x = 0; x < subWidth; x++) {
int xoffset = x << 3; int xoffset = x << BLOCK_SIZE_POWER;
if (xoffset + 8 >= width) { if (xoffset + BLOCK_SIZE >= width) {
xoffset = width - 8; xoffset = width - BLOCK_SIZE;
} }
int sum = 0; int sum = 0;
int min = 255; int min = 0xFF;
int max = 0; int max = 0;
for (int yy = 0; yy < 8; yy++) { for (int yy = 0, offset = yoffset * width + xoffset;
int offset = (yoffset + yy) * width + xoffset; yy < BLOCK_SIZE;
for (int xx = 0; xx < 8; xx++) { yy++, offset += width) {
int pixel = luminances[offset + xx] & 0xff; for (int xx = 0; xx < BLOCK_SIZE; xx++) {
int pixel = luminances[offset + xx] & 0xFF;
sum += pixel; sum += pixel;
if (pixel < min) { if (pixel < min) {
min = pixel; min = pixel;
@ -171,5 +178,3 @@ int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWid
return blackPoints; return blackPoints;
} }
} // namespace zxing