5 * Copyright 2010 ZXing authors All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <zxing/common/HybridBinarizer.h>
22 #include <zxing/common/IllegalArgumentException.h>
27 static const int MINIMUM_DIMENSION = 40;
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;
33 HybridBinarizer::HybridBinarizer(Ref<LuminanceSource> source) :
34 GlobalHistogramBinarizer(source), cached_matrix_(NULL), cached_row_(NULL), cached_row_num_(-1) {
38 HybridBinarizer::~HybridBinarizer() {
42 Ref<BitMatrix> HybridBinarizer::getBlackMatrix() {
43 binarizeEntireImage();
44 return cached_matrix_;
47 Ref<Binarizer> HybridBinarizer::createBinarizer(Ref<LuminanceSource> source) {
48 return Ref<Binarizer> (new HybridBinarizer(source));
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;
62 int subHeight = height >> 3;
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;
72 // If the image is too small, fall back to the global histogram approach.
73 cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
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++) {
82 if (yoffset + 8 >= height) {
85 for (int x = 0; x < subWidth; x++) {
87 if (xoffset + 8 >= width) {
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;
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];
103 int average = sum / 25;
104 threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
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);
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;
130 for (int x = 0; x < subWidth; x++) {
131 int xoffset = x << 3;
132 if (xoffset + 8 >= width) {
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;
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
156 if (max - min > 24) {
157 average = (sum >> 6);
159 average = max == 0 ? 1 : (min >> 1);
161 blackPoints[y * subWidth + x] = average;