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 Ref<BitArray> LocalBlockBinarizer::estimateBlackRow(int y, Ref<BitArray> row) {
38 return Ref<BitArray>();
41 // Calculates the final BitMatrix once for all requests. This could be called once from the
42 // constructor instead, but there are some advantages to doing it lazily, such as making
43 // profiling easier, and not doing heavy lifting when callers don't expect it.
44 Ref<BitMatrix> LocalBlockBinarizer::estimateBlackMatrix() {
45 Ref<LuminanceSource> source = getSource();
46 unsigned char* luminances = source->copyMatrix();
47 int width = source->getWidth();
48 int height = source->getHeight();
49 // Sharpening does not really help for 2d barcodes
50 // sharpenRow(luminances, width, height);
52 int subWidth = width >> 3;
53 int subHeight = height >> 3;
55 unsigned char* averages = new unsigned char[subWidth * subHeight];
56 unsigned char* types = new unsigned char[subWidth * subHeight];
58 calculateBlackPoints(luminances, averages, types, subWidth, subHeight, width);
60 Ref<BitMatrix> matrix(new BitMatrix(width, height));
61 calculateThresholdForBlock(luminances, subWidth, subHeight, width, averages, types, *matrix);
70 // For each 8x8 block in the image, calculate the average black point using a 5x5 grid
71 // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels
72 // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not
73 // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.
74 void LocalBlockBinarizer::calculateThresholdForBlock(const unsigned char* luminances, int subWidth, int subHeight,
75 int stride, const unsigned char* averages, const unsigned char* types, BitMatrix& matrix) {
76 // Calculate global average
78 for (int y = 0; y < subHeight; y++) {
79 for (int x = 0; x < subWidth; x++) {
80 global += averages[y * subWidth + x];
84 global /= subWidth * subHeight;
87 for (int y = 0; y < subHeight; y++) {
88 for (int x = 0; x < subWidth; x++) {
89 int left = (x > 0) ? x : 1;
90 left = (left < subWidth - 1) ? left : subWidth - 2;
91 int top = (y > 0) ? y : 1;
92 top = (top < subHeight - 1) ? top : subHeight - 2;
95 for (int z = -1; z <= 1; z++) {
96 // sum += averages[(top + z) * subWidth + left - 2];
97 sum += averages[(top + z) * subWidth + left - 1];
98 sum += averages[(top + z) * subWidth + left];
99 sum += averages[(top + z) * subWidth + left + 1];
100 // sum += averages[(top + z) * subWidth + left + 2];
102 // type += types[(top + z) * subWidth + left - 2];
103 contrast += types[(top + z) * subWidth + left - 1];
104 contrast += types[(top + z) * subWidth + left];
105 contrast += types[(top + z) * subWidth + left + 1];
106 // type += types[(top + z) * subWidth + left + 2];
108 int average = sum / 9;
112 threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);
113 // else if(average < global) // Black
114 // matrix.setRegion(x << 3, y << 3, 8, 8);
115 // If white, we don't need to do anything - the block is already cleared.
120 // Applies a single threshold to an 8x8 block of pixels.
121 void LocalBlockBinarizer::threshold8x8Block(const unsigned char* luminances, int xoffset, int yoffset, int threshold,
122 int stride, BitMatrix& matrix) {
123 for (int y = 0; y < 8; y++) {
124 int offset = (yoffset + y) * stride + xoffset;
125 for (int x = 0; x < 8; x++) {
126 int pixel = luminances[offset + x];
127 if (pixel < threshold) {
128 matrix.set(xoffset + x, yoffset + y);
134 // Calculates a single black point for each 8x8 block of pixels and saves it away.
135 void LocalBlockBinarizer::calculateBlackPoints(const unsigned char* luminances, unsigned char* averages,
136 unsigned char* types, int subWidth, int subHeight, int stride) {
137 for (int y = 0; y < subHeight; y++) {
138 for (int x = 0; x < subWidth; x++) {
142 for (int yy = 0; yy < 8; yy++) {
143 int offset = ((y << 3) + yy) * stride + (x << 3);
144 const unsigned char* lumo = luminances + offset;
145 for (int xx = 0; xx < 8; xx++) {
146 int pixel = lumo[xx];
157 // If the contrast is inadequate, we treat the block as white.
158 // An arbitrary value is chosen here. Higher values mean less noise, but may also reduce
159 // the ability to recognise some barcodes.
160 int average = sum >> 6;
167 // int average = (max - min > 24) ? (sum >> 6) : (min-1);
168 averages[y * subWidth + x] = average;
169 types[y * subWidth + x] = type;
174 // Applies a simple -1 4 -1 box filter with a weight of 2 to each row.
175 void LocalBlockBinarizer::sharpenRow(unsigned char* luminances, int width, int height) {
176 for (int y = 0; y < height; y++) {
177 int offset = y * width;
178 int left = luminances[offset];
179 int center = luminances[offset + 1];
180 for (int x = 1; x < width - 1; x++) {
181 unsigned char right = luminances[offset + x + 1];
182 int pixel = ((center << 2) - left - right) >> 1;
183 // Must clamp values to 0..255 so they will fit in a byte.
186 } else if (pixel < 0) {
189 luminances[offset + x] = (unsigned char)pixel;