Issue 511
[zxing.git] / cpp / core / src / zxing / common / HybridBinarizer.cpp
1 /*
2  *  HybridBinarizer.cpp
3  *  zxing
4  *
5  *  Copyright 2010 ZXing authors All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <zxing/common/HybridBinarizer.h>
21
22 #include <zxing/common/IllegalArgumentException.h>
23
24 namespace zxing {
25 using namespace std;
26
27 static const int MINIMUM_DIMENSION = 40;
28
29 static const int LUMINANCE_BITS = 5;
30 static const int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
31 static const int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
32
33 HybridBinarizer::HybridBinarizer(Ref<LuminanceSource> source) :
34   GlobalHistogramBinarizer(source), cached_matrix_(NULL), cached_row_(NULL), cached_row_num_(-1) {
35
36 }
37
38 HybridBinarizer::~HybridBinarizer() {
39 }
40
41
42 Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
43   binarizeEntireImage();
44   return cached_matrix_;
45 }
46
47 Ref<Binarizer> HybridBinarizer::createBinarizer(Ref<LuminanceSource> source) {
48   return Ref<Binarizer> (new HybridBinarizer(source));
49 }
50
51 void HybridBinarizer::binarizeEntireImage() {
52   if (cached_matrix_ == NULL) {
53     Ref<LuminanceSource> source = getLuminanceSource();
54     if (source->getWidth() >= MINIMUM_DIMENSION && source->getHeight() >= MINIMUM_DIMENSION) {
55       unsigned char* luminances = source->getMatrix();
56       int width = source->getWidth();
57       int height = source->getHeight();
58       int subWidth = width >> 3;
59       int subHeight = height >> 3;
60       int *blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);
61       cached_matrix_.reset(new BitMatrix(width,height));
62       calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, cached_matrix_);
63       delete [] blackPoints;
64       delete [] luminances;
65     } else {
66       // If the image is too small, fall back to the global histogram approach.
67       cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
68     }
69   }
70 }
71
72 void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight,
73     int stride, int blackPoints[], Ref<BitMatrix> matrix) {
74   for (int y = 0; y < subHeight; y++) {
75     for (int x = 0; x < subWidth; x++) {
76       int left = (x > 1) ? x : 2;
77       left = (left < subWidth - 2) ? left : subWidth - 3;
78       int top = (y > 1) ? y : 2;
79       top = (top < subHeight - 2) ? top : subHeight - 3;
80       int sum = 0;
81       for (int z = -2; z <= 2; z++) {
82         int *blackRow = &blackPoints[(top + z) * subWidth];
83         sum += blackRow[left - 2];
84         sum += blackRow[left - 1];
85         sum += blackRow[left];
86         sum += blackRow[left + 1];
87         sum += blackRow[left + 2];
88       }
89       int average = sum / 25;
90       threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
91     }
92   }
93 }
94
95 void HybridBinarizer::threshold8x8Block(unsigned char* luminances, int xoffset, int yoffset, int threshold,
96     int stride, Ref<BitMatrix> matrix) {
97   for (int y = 0; y < 8; y++) {
98     int offset = (yoffset + y) * stride + xoffset;
99     for (int x = 0; x < 8; x++) {
100       int pixel = luminances[offset + x] & 0xff;
101       if (pixel < threshold) {
102         matrix->set(xoffset + x, yoffset + y);
103       }
104     }
105   }
106 }
107
108 int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWidth, int subHeight,
109     int stride) {
110   int *blackPoints = new int[subHeight * subWidth];
111   for (int y = 0; y < subHeight; y++) {
112     for (int x = 0; x < subWidth; x++) {
113       int sum = 0;
114       int min = 255;
115       int max = 0;
116       for (int yy = 0; yy < 8; yy++) {
117         int offset = ((y << 3) + yy) * stride + (x << 3);
118         for (int xx = 0; xx < 8; xx++) {
119           int pixel = luminances[offset + xx] & 0xff;
120           sum += pixel;
121           if (pixel < min) {
122             min = pixel;
123           }
124           if (pixel > max) {
125             max = pixel;
126           }
127         }
128       }
129
130       // If the contrast is inadequate, use half the minimum, so that this block will be
131       // treated as part of the white background, but won't drag down neighboring blocks
132       // too much.
133       int average;
134       if (max - min > 24) {
135           average = (sum >> 6);
136       } else {
137         average = max == 0 ? 1 : (min >> 1);
138       }
139       blackPoints[y * subWidth + x] = average;
140     }
141   }
142   return blackPoints;
143 }
144
145 } // namespace zxing
146