2 * LocalBlockBinarizer.cpp
5 * Created by Ralf Kistner on 17/10/2009.
6 * Copyright 2008 ZXing authors All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include <zxing/common/LocalBlockBinarizer.h>
26 const int THRESHOLD = 1;
28 LocalBlockBinarizer::LocalBlockBinarizer(Ref<LuminanceSource> source) :
33 LocalBlockBinarizer::~LocalBlockBinarizer() {
36 // Calculates the final BitMatrix once for all requests. This could be called once from the
37 // constructor instead, but there are some advantages to doing it lazily, such as making
38 // profiling easier, and not doing heavy lifting when callers don't expect it.
39 Ref<BitMatrix> LocalBlockBinarizer::estimateBlackMatrix() {
40 Ref<LuminanceSource> source = getSource();
41 unsigned char* luminances = source->copyMatrix();
42 int width = source->getWidth();
43 int height = source->getHeight();
44 // Sharpening does not really help for 2d barcodes
45 // sharpenRow(luminances, width, height);
47 int subWidth = width >> 3;
48 int subHeight = height >> 3;
50 unsigned char* averages = new unsigned char[subWidth * subHeight];
51 unsigned char* types = new unsigned char[subWidth * subHeight];
53 calculateBlackPoints(luminances, averages, types, subWidth, subHeight, width);
55 Ref<BitMatrix> matrix(new BitMatrix(width, height));
56 calculateThresholdForBlock(luminances, subWidth, subHeight, width, averages, types, *matrix);
65 // For each 8x8 block in the image, calculate the average black point using a 5x5 grid
66 // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels
67 // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not
68 // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.
69 void LocalBlockBinarizer::calculateThresholdForBlock(const unsigned char* luminances, int subWidth, int subHeight,
70 int stride, const unsigned char* averages, const unsigned char* types, BitMatrix& matrix) {
71 // Calculate global average
73 for (int y = 0; y < subHeight; y++) {
74 for (int x = 0; x < subWidth; x++) {
75 global += averages[y * subWidth + x];
79 global /= subWidth * subHeight;
82 for (int y = 0; y < subHeight; y++) {
83 for (int x = 0; x < subWidth; x++) {
84 int left = (x > 0) ? x : 1;
85 left = (left < subWidth - 1) ? left : subWidth - 2;
86 int top = (y > 0) ? y : 1;
87 top = (top < subHeight - 1) ? top : subHeight - 2;
90 for (int z = -1; z <= 1; z++) {
91 // sum += averages[(top + z) * subWidth + left - 2];
92 sum += averages[(top + z) * subWidth + left - 1];
93 sum += averages[(top + z) * subWidth + left];
94 sum += averages[(top + z) * subWidth + left + 1];
95 // sum += averages[(top + z) * subWidth + left + 2];
97 // type += types[(top + z) * subWidth + left - 2];
98 contrast += types[(top + z) * subWidth + left - 1];
99 contrast += types[(top + z) * subWidth + left];
100 contrast += types[(top + z) * subWidth + left + 1];
101 // type += types[(top + z) * subWidth + left + 2];
103 int average = sum / 9;
107 threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
108 // else if(average < global) // Black
109 // matrix.setRegion(x << 3, y << 3, 8, 8);
110 // If white, we don't need to do anything - the block is already cleared.
115 // Applies a single threshold to an 8x8 block of pixels.
116 void LocalBlockBinarizer::threshold8x8Block(const unsigned char* luminances, int xoffset, int yoffset, int threshold,
117 int stride, BitMatrix& matrix) {
118 for (int y = 0; y < 8; y++) {
119 int offset = (yoffset + y) * stride + xoffset;
120 for (int x = 0; x < 8; x++) {
121 int pixel = luminances[offset + x];
122 if (pixel < threshold) {
123 matrix.set(xoffset + x, yoffset + y);
129 // Calculates a single black point for each 8x8 block of pixels and saves it away.
130 void LocalBlockBinarizer::calculateBlackPoints(const unsigned char* luminances, unsigned char* averages,
131 unsigned char* types, int subWidth, int subHeight, int stride) {
132 for (int y = 0; y < subHeight; y++) {
133 for (int x = 0; x < subWidth; x++) {
137 for (int yy = 0; yy < 8; yy++) {
138 int offset = ((y << 3) + yy) * stride + (x << 3);
139 const unsigned char* lumo = luminances + offset;
140 for (int xx = 0; xx < 8; xx++) {
141 int pixel = lumo[xx];
152 // If the contrast is inadequate, we treat the block as white.
153 // An arbitrary value is chosen here. Higher values mean less noise, but may also reduce
154 // the ability to recognise some barcodes.
155 int average = sum >> 6;
162 // int average = (max - min > 24) ? (sum >> 6) : (min-1);
163 averages[y * subWidth + x] = average;
164 types[y * subWidth + x] = type;
169 // Applies a simple -1 4 -1 box filter with a weight of 2 to each row.
170 void LocalBlockBinarizer::sharpenRow(unsigned char* luminances, int width, int height) {
171 for (int y = 0; y < height; y++) {
172 int offset = y * width;
173 int left = luminances[offset];
174 int center = luminances[offset + 1];
175 for (int x = 1; x < width - 1; x++) {
176 unsigned char right = luminances[offset + x + 1];
177 int pixel = ((center << 2) - left - right) >> 1;
178 // Must clamp values to 0..255 so they will fit in a byte.
181 } else if (pixel < 0) {
184 luminances[offset + x] = (unsigned char)pixel;