Fixed the C++ port to compile on a Linux system.
[zxing.git] / cpp / core / src / zxing / common / LocalBlockBinarizer.cpp
1 /*
2  *  LocalBlockBinarizer.cpp
3  *  zxing
4  *
5  *  Created by Ralf Kistner on 17/10/2009.
6  *  Copyright 2008 ZXing authors All rights reserved.
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include <zxing/common/LocalBlockBinarizer.h>
22
23 namespace zxing {
24
25 const int GLOBAL = 0;
26 const int THRESHOLD = 1;
27
28 LocalBlockBinarizer::LocalBlockBinarizer(Ref<LuminanceSource> source) :
29     Binarizer(source) {
30
31 }
32
33 LocalBlockBinarizer::~LocalBlockBinarizer() {
34 }
35
36 Ref<BitArray> LocalBlockBinarizer::estimateBlackRow(int y, Ref<BitArray> row) {
37   //TODO: implement
38   return Ref<BitArray>();
39 }
40
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);
51
52   int subWidth = width >> 3;
53   int subHeight = height >> 3;
54
55   unsigned char* averages = new unsigned char[subWidth * subHeight];
56   unsigned char* types = new unsigned char[subWidth * subHeight];
57
58   calculateBlackPoints(luminances, averages, types, subWidth, subHeight, width);
59
60   Ref<BitMatrix> matrix(new BitMatrix(width, height));
61   calculateThresholdForBlock(luminances, subWidth, subHeight, width, averages, types, *matrix);
62
63   delete[] averages;
64   delete[] types;
65   delete[] luminances;
66
67   return matrix;
68 }
69
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
77   int global = 0;
78   for (int y = 0; y < subHeight; y++) {
79     for (int x = 0; x < subWidth; x++) {
80       global += averages[y * subWidth + x];
81     }
82   }
83
84   global /= subWidth * subHeight;
85
86
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;
93       int sum = 0;
94       int contrast = 0;
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];
101
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];
107       }
108       int average = sum / 9;
109
110
111       if (contrast > 2)
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.
116     }
117   }
118 }
119
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);
129       }
130     }
131   }
132 }
133
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++) {
139       int sum = 0;
140       int min = 255;
141       int max = 0;
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];
147           sum += pixel;
148           if (pixel < min) {
149             min = pixel;
150           }
151           if (pixel > max) {
152             max = pixel;
153           }
154         }
155       }
156
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;
161       int type;
162
163       if (max - min > 30)
164         type = THRESHOLD;
165       else
166         type = GLOBAL;
167       //                        int average = (max - min > 24) ? (sum >> 6) : (min-1);
168       averages[y * subWidth + x] = average;
169       types[y * subWidth + x] = type;
170     }
171   }
172 }
173
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.
184       if (pixel > 255) {
185         pixel = 255;
186       } else if (pixel < 0) {
187         pixel = 0;
188       }
189       luminances[offset + x] = (unsigned char)pixel;
190       left = center;
191       center = right;
192     }
193   }
194 }
195
196 }