New C# port from Suraj Supekar
[zxing.git] / csharp / common / HybridBinarizer.cs
diff --git a/csharp/common/HybridBinarizer.cs b/csharp/common/HybridBinarizer.cs
new file mode 100755 (executable)
index 0000000..19d8fa3
--- /dev/null
@@ -0,0 +1,187 @@
+/*\r
+* Copyright 2009 ZXing authors\r
+*\r
+* Licensed under the Apache License, Version 2.0 (the "License");\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*      http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+*/\r
+using System;\r
+using Binarizer = com.google.zxing.Binarizer;\r
+using LuminanceSource = com.google.zxing.LuminanceSource;\r
+using ReaderException = com.google.zxing.ReaderException;\r
+namespace com.google.zxing.common\r
+{\r
+       \r
+       /// <summary> This class implements a local thresholding algorithm, which while slower than the\r
+       /// GlobalHistogramBinarizer, is fairly efficient for what it does. It is designed for\r
+       /// high frequency images of barcodes with black data on white backgrounds. For this application,\r
+       /// it does a much better job than a global blackpoint with severe shadows and gradients.\r
+       /// However it tends to produce artifacts on lower frequency images and is therefore not\r
+       /// a good general purpose binarizer for uses outside ZXing.\r
+       /// \r
+       /// This class extends GlobalHistogramBinarizer, using the older histogram approach for 1D readers,\r
+       /// and the newer local approach for 2D readers. 1D decoding using a per-row histogram is already\r
+       /// inherently local, and only fails for horizontal gradients. We can revisit that problem later,\r
+       /// but for now it was not a win to use local blocks for 1D.\r
+       /// \r
+       /// This Binarizer is the default for the unit tests and the recommended class for library users.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin)\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public sealed class HybridBinarizer:GlobalHistogramBinarizer\r
+       {\r
+               override public BitMatrix BlackMatrix\r
+               {\r
+                       get\r
+                       {\r
+                               binarizeEntireImage();\r
+                               return matrix;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               // This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.\r
+               // So this is the smallest dimension in each axis we can accept.\r
+               private const int MINIMUM_DIMENSION = 40;\r
+               \r
+               private BitMatrix matrix = null;\r
+               \r
+               public HybridBinarizer(LuminanceSource source):base(source)\r
+               {\r
+               }\r
+               \r
+               public override Binarizer createBinarizer(LuminanceSource source)\r
+               {\r
+                       return new HybridBinarizer(source);\r
+               }\r
+               \r
+               // Calculates the final BitMatrix once for all requests. This could be called once from the\r
+               // constructor instead, but there are some advantages to doing it lazily, such as making\r
+               // profiling easier, and not doing heavy lifting when callers don't expect it.\r
+               private void  binarizeEntireImage()\r
+               {\r
+                       if (matrix == null)\r
+                       {\r
+                               LuminanceSource source = LuminanceSource;\r
+                               if (source.Width >= MINIMUM_DIMENSION && source.Height >= MINIMUM_DIMENSION)\r
+                               {\r
+                                       sbyte[] luminances = source.Matrix;\r
+                                       int width = source.Width;\r
+                                       int height = source.Height;\r
+                                       int subWidth = width >> 3;\r
+                                       int subHeight = height >> 3;\r
+                                       int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width);\r
+                                       \r
+                                       matrix = new BitMatrix(width, height);\r
+                                       calculateThresholdForBlock(luminances, subWidth, subHeight, width, blackPoints, matrix);\r
+                               }\r
+                               else\r
+                               {\r
+                                       // If the image is too small, fall back to the global histogram approach.\r
+                                       matrix = base.BlackMatrix;\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               // For each 8x8 block in the image, calculate the average black point using a 5x5 grid\r
+               // of the blocks around it. Also handles the corner cases, but will ignore up to 7 pixels\r
+               // on the right edge and 7 pixels at the bottom of the image if the overall dimensions are not\r
+               // multiples of eight. In practice, leaving those pixels white does not seem to be a problem.\r
+               private static void  calculateThresholdForBlock(sbyte[] luminances, int subWidth, int subHeight, int stride, int[][] blackPoints, BitMatrix matrix)\r
+               {\r
+                       for (int y = 0; y < subHeight; y++)\r
+                       {\r
+                               for (int x = 0; x < subWidth; x++)\r
+                               {\r
+                                       int left = (x > 1)?x:2;\r
+                                       left = (left < subWidth - 2)?left:subWidth - 3;\r
+                                       int top = (y > 1)?y:2;\r
+                                       top = (top < subHeight - 2)?top:subHeight - 3;\r
+                                       int sum = 0;\r
+                                       for (int z = - 2; z <= 2; z++)\r
+                                       {\r
+                                               int[] blackRow = blackPoints[top + z];\r
+                                               sum += blackRow[left - 2];\r
+                                               sum += blackRow[left - 1];\r
+                                               sum += blackRow[left];\r
+                                               sum += blackRow[left + 1];\r
+                                               sum += blackRow[left + 2];\r
+                                       }\r
+                                       int average = sum / 25;\r
+                                       threshold8x8Block(luminances, x << 3, y << 3, average, stride, matrix);\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               // Applies a single threshold to an 8x8 block of pixels.\r
+               private static void  threshold8x8Block(sbyte[] luminances, int xoffset, int yoffset, int threshold, int stride, BitMatrix matrix)\r
+               {\r
+                       for (int y = 0; y < 8; y++)\r
+                       {\r
+                               int offset = (yoffset + y) * stride + xoffset;\r
+                               for (int x = 0; x < 8; x++)\r
+                               {\r
+                                       int pixel = luminances[offset + x] & 0xff;\r
+                                       if (pixel < threshold)\r
+                                       {\r
+                                               matrix.set_Renamed(xoffset + x, yoffset + y);\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               // Calculates a single black point for each 8x8 block of pixels and saves it away.\r
+               private static int[][] calculateBlackPoints(sbyte[] luminances, int subWidth, int subHeight, int stride)\r
+               {\r
+                       int[][] blackPoints = new int[subHeight][];\r
+                       for (int i = 0; i < subHeight; i++)\r
+                       {\r
+                               blackPoints[i] = new int[subWidth];\r
+                       }\r
+                       for (int y = 0; y < subHeight; y++)\r
+                       {\r
+                               for (int x = 0; x < subWidth; x++)\r
+                               {\r
+                                       int sum = 0;\r
+                                       int min = 255;\r
+                                       int max = 0;\r
+                                       for (int yy = 0; yy < 8; yy++)\r
+                                       {\r
+                                               int offset = ((y << 3) + yy) * stride + (x << 3);\r
+                                               for (int xx = 0; xx < 8; xx++)\r
+                                               {\r
+                                                       int pixel = luminances[offset + xx] & 0xff;\r
+                                                       sum += pixel;\r
+                                                       if (pixel < min)\r
+                                                       {\r
+                                                               min = pixel;\r
+                                                       }\r
+                                                       if (pixel > max)\r
+                                                       {\r
+                                                               max = pixel;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                                       \r
+                                       // If the contrast is inadequate, use half the minimum, so that this block will be\r
+                                       // treated as part of the white background, but won't drag down neighboring blocks\r
+                                       // too much.\r
+                                       int average = (max - min > 24)?(sum >> 6):(min >> 1);\r
+                                       blackPoints[y][x] = average;\r
+                               }\r
+                       }\r
+                       return blackPoints;\r
+               }\r
+       }\r
+}
\ No newline at end of file