Fixed a bug which prevented this binarizer from caching rows.
[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     } else {
65       // If the image is too small, fall back to the global histogram approach.
66       cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
67     }
68   }
69 }
70
71 void HybridBinarizer::calculateThresholdForBlock(unsigned char* luminances, int subWidth, int subHeight,
72     int stride, int blackPoints[], Ref<BitMatrix> matrix) {
73   for (int y = 0; y < subHeight; y++) {
74     for (int x = 0; x < subWidth; x++) {
75       int left = (x > 1) ? x : 2;
76       left = (left < subWidth - 2) ? left : subWidth - 3;
77       int top = (y > 1) ? y : 2;
78       top = (top < subHeight - 2) ? top : subHeight - 3;
79       int sum = 0;
80       for (int z = -2; z <= 2; z++) {
81         int *blackRow = &blackPoints[(top + z) * subWidth];
82         sum += blackRow[left - 2];
83         sum += blackRow[left - 1];
84         sum += blackRow[left];
85         sum += blackRow[left + 1];
86         sum += blackRow[left + 2];
87       }
88       int average = sum / 25;
89       threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
90     }
91   }
92 }
93
94 void HybridBinarizer::threshold8x8Block(unsigned char* luminances, int xoffset, int yoffset, int threshold,
95     int stride, Ref<BitMatrix> matrix) {
96   for (int y = 0; y < 8; y++) {
97     int offset = (yoffset + y) * stride + xoffset;
98     for (int x = 0; x < 8; x++) {
99       int pixel = luminances[offset + x] & 0xff;
100       if (pixel < threshold) {
101         matrix->set(xoffset + x, yoffset + y);
102       }
103     }
104   }
105 }
106
107 int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWidth, int subHeight,
108     int stride) {
109   int *blackPoints = new int[subHeight * subWidth];
110   for (int y = 0; y < subHeight; y++) {
111     for (int x = 0; x < subWidth; x++) {
112       int sum = 0;
113       int min = 255;
114       int max = 0;
115       for (int yy = 0; yy < 8; yy++) {
116         int offset = ((y << 3) + yy) * stride + (x << 3);
117         for (int xx = 0; xx < 8; xx++) {
118           int pixel = luminances[offset + xx] & 0xff;
119           sum += pixel;
120           if (pixel < min) {
121             min = pixel;
122           }
123           if (pixel > max) {
124             max = pixel;
125           }
126         }
127       }
128
129       // If the contrast is inadequate, use half the minimum, so that this block will be
130       // treated as part of the white background, but won't drag down neighboring blocks
131       // too much.
132       int average;
133       if (max - min > 24) {
134           average = (sum >> 6);
135       } else {
136         average = max == 0 ? 1 : (min >> 1);
137       }
138       blackPoints[y * subWidth + x] = average;
139     }
140   }
141   return blackPoints;
142 }
143
144 } // namespace zxing
145