Don't need to block multiple thread access. Refactor and update a bit for an upcoming...
[zxing.git] / csharp / common / HybridBinarizer.cs
1 /*\r
2 * Copyright 2009 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 using Binarizer = com.google.zxing.Binarizer;\r
18 using LuminanceSource = com.google.zxing.LuminanceSource;\r
19 using ReaderException = com.google.zxing.ReaderException;\r
20 namespace com.google.zxing.common\r
21 {\r
22         \r
23         /// <summary> This class implements a local thresholding algorithm, which while slower than the\r
24         /// GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for\r
25         /// high frequency images of barcodes with black data on white backgrounds. For this application,\r
26         /// it does a much better job than a global blackpoint with severe shadows and gradients.\r
27         /// However it tends to produce artifacts on lower frequency images and is therefore not\r
28         /// a good general purpose binarizer for uses outside ZXing.\r
29         /// \r
30         /// This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,\r
31         /// and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already\r
32         /// inherently local, and only fails for horizontal gradients. We can revisit that problem later,\r
33         /// but for now it was not a win to use local blocks for 1D.\r
34         /// \r
35         /// This Binarizer is the default for the unit tests and the recommended class for library users.\r
36         /// \r
37         /// </summary>\r
38         /// <author>  dswitkin@google.com (Daniel Switkin)\r
39         /// </author>\r
40         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
41         /// </author>\r
42         public sealed class HybridBinarizer:GlobalHistogramBinarizer\r
43         {\r
44                 override public BitMatrix BlackMatrix\r
45                 {\r
46                         get\r
47                         {\r
48                                 binarizeEntireImage();\r
49                                 return matrix;\r
50                         }\r
51                         \r
52                 }\r
53                 \r
54                 // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.\r
55                 // So this is the smallest dimension in each axis we can accept.\r
56                 private const int MINIMUM_DIMENSION = 40;\r
57                 \r
58                 private BitMatrix matrix = null;\r
59                 \r
60                 public HybridBinarizer(LuminanceSource source):base(source)\r
61                 {\r
62                 }\r
63                 \r
64                 public override Binarizer createBinarizer(LuminanceSource source)\r
65                 {\r
66                         return new HybridBinarizer(source);\r
67                 }\r
68                 \r
69                 // Calculates the final BitMatrix once for all requests. This could be called once from the\r
70                 // constructor instead, but there are some advantages to doing it lazily, such as making\r
71                 // profiling easier, and not doing heavy lifting when callers don't expect it.\r
72                 private void  binarizeEntireImage()\r
73                 {\r
74                         if (matrix == null)\r
75                         {\r
76                                 LuminanceSource source = LuminanceSource;\r
77                                 if (source.Width >= MINIMUM_DIMENSION && source.Height >= MINIMUM_DIMENSION)\r
78                                 {\r
79                                         sbyte[] luminances = source.Matrix;\r
80                                         int width = source.Width;\r
81                                         int height = source.Height;\r
82                                         int subWidth = width >> 3;\r
83                                         int subHeight = height >> 3;\r
84                                         int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);\r
85                                         \r
86                                         matrix = new BitMatrix(width, height);\r
87                                         calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix);\r
88                                 }\r
89                                 else\r
90                                 {\r
91                                         // If the image is too small, fall back to the global histogram approach.\r
92                                         matrix = base.BlackMatrix;\r
93                                 }\r
94                         }\r
95                 }\r
96                 \r
97                 // For each 8x8 block in the image, calculate the average black point using a 5x5 grid\r
98                 // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels\r
99                 // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not\r
100                 // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.\r
101                 private static void  calculateThresholdForBlock(sbyte[] luminances, int subWidth, int subHeight, int stride, int[][] blackPoints, BitMatrix matrix)\r
102                 {\r
103                         for (int y = 0; y < subHeight; y++)\r
104                         {\r
105                                 for (int x = 0; x < subWidth; x++)\r
106                                 {\r
107                                         int left = (x > 1)?x:2;\r
108                                         left = (left < subWidth - 2)?left:subWidth - 3;\r
109                                         int top = (y > 1)?y:2;\r
110                                         top = (top < subHeight - 2)?top:subHeight - 3;\r
111                                         int sum = 0;\r
112                                         for (int z = - 2; z <= 2; z++)\r
113                                         {\r
114                                                 int[] blackRow = blackPoints[top + z];\r
115                                                 sum += blackRow[left - 2];\r
116                                                 sum += blackRow[left - 1];\r
117                                                 sum += blackRow[left];\r
118                                                 sum += blackRow[left + 1];\r
119                                                 sum += blackRow[left + 2];\r
120                                         }\r
121                                         int average = sum / 25;\r
122                                         threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);\r
123                                 }\r
124                         }\r
125                 }\r
126                 \r
127                 // Applies a single threshold to an 8x8 block of pixels.\r
128                 private static void  threshold8x8Block(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)\r
129                 {\r
130                         for (int y = 0; y < 8; y++)\r
131                         {\r
132                                 int offset = (yoffset + y) * stride + xoffset;\r
133                                 for (int x = 0; x < 8; x++)\r
134                                 {\r
135                                         int pixel = luminances[offset + x] & 0xff;\r
136                                         if (pixel < threshold)\r
137                                         {\r
138                                                 matrix.set_Renamed(xoffset + x, yoffset + y);\r
139                                         }\r
140                                 }\r
141                         }\r
142                 }\r
143                 \r
144                 // Calculates a single black point for each 8x8 block of pixels and saves it away.\r
145                 private static int[][] calculateBlackPoints(sbyte[] luminances, int subWidth, int subHeight, int stride)\r
146                 {\r
147                         int[][] blackPoints = new int[subHeight][];\r
148                         for (int i = 0; i < subHeight; i++)\r
149                         {\r
150                                 blackPoints[i] = new int[subWidth];\r
151                         }\r
152                         for (int y = 0; y < subHeight; y++)\r
153                         {\r
154                                 for (int x = 0; x < subWidth; x++)\r
155                                 {\r
156                                         int sum = 0;\r
157                                         int min = 255;\r
158                                         int max = 0;\r
159                                         for (int yy = 0; yy < 8; yy++)\r
160                                         {\r
161                                                 int offset = ((y << 3) + yy) * stride + (x << 3);\r
162                                                 for (int xx = 0; xx < 8; xx++)\r
163                                                 {\r
164                                                         int pixel = luminances[offset + xx] & 0xff;\r
165                                                         sum += pixel;\r
166                                                         if (pixel < min)\r
167                                                         {\r
168                                                                 min = pixel;\r
169                                                         }\r
170                                                         if (pixel > max)\r
171                                                         {\r
172                                                                 max = pixel;\r
173                                                         }\r
174                                                 }\r
175                                         }\r
176                                         \r
177                                         // If the contrast is inadequate, use half the minimum, so that this block will be\r
178                                         // treated as part of the white background, but won't drag down neighboring blocks\r
179                                         // too much.\r
180                                         int average = (max - min > 24)?(sum >> 6):(min >> 1);\r
181                                         blackPoints[y][x] = average;\r
182                                 }\r
183                         }\r
184                         return blackPoints;\r
185                 }\r
186         }\r
187 }