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