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;
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;
66 // If the image is too small, fall back to the global histogram approach.
67 cached_matrix_.reset(GlobalHistogramBinarizer::getBlackMatrix());
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;
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];
89 int average = sum / 25;
90 threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
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);
108 int* HybridBinarizer::calculateBlackPoints(unsigned char* luminances, int subWidth, int subHeight,
110 int *blackPoints = new int[subHeight * subWidth];
111 for (int y = 0; y < subHeight; y++) {
112 for (int x = 0; x < subWidth; x++) {
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;
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
134 if (max - min > 24) {
135 average = (sum >> 6);
137 average = max == 0 ? 1 : (min >> 1);
139 blackPoints[y * subWidth + x] = average;