New C# port from Suraj Supekar
[zxing.git] / csharp / common / GlobalHistogramBinarizer.cs
diff --git a/csharp/common/GlobalHistogramBinarizer.cs b/csharp/common/GlobalHistogramBinarizer.cs
new file mode 100755 (executable)
index 0000000..f6d0ae2
--- /dev/null
@@ -0,0 +1,250 @@
+/*\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 Binarizer implementation uses the old ZXing global histogram approach. It is suitable\r
+       /// for low-end mobile devices which don't have enough CPU or memory to use a local thresholding\r
+       /// algorithm. However, because it picks a global black point, it cannot handle difficult shadows\r
+       /// and gradients.\r
+       /// \r
+       /// Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.\r
+       /// \r
+       /// </summary>\r
+       /// <author>  dswitkin@google.com (Daniel Switkin)\r
+       /// </author>\r
+       /// <author>  Sean Owen\r
+       /// </author>\r
+       /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
+       /// </author>\r
+       public class GlobalHistogramBinarizer:Binarizer\r
+       {\r
+               override public BitMatrix BlackMatrix\r
+               {\r
+                       // Does not sharpen the data, as this call is intended to only be used by 2D Readers.\r
+                       \r
+                       get\r
+                       {\r
+                               LuminanceSource source = LuminanceSource;\r
+                // Redivivus.in Java to c# Porting update\r
+                // 30/01/2010 \r
+                // Added\r
+                // START\r
+                sbyte[] localLuminances;\r
+                               //END\r
+                \r
+                int width = source.Width;\r
+                               int height = source.Height;\r
+                               BitMatrix matrix = new BitMatrix(width, height);\r
+                               \r
+                               // Quickly calculates the histogram by sampling four rows from the image. This proved to be\r
+                               // more robust on the blackbox tests than sampling a diagonal as we used to do.\r
+                               initArrays(width);\r
+                               int[] localBuckets = buckets;\r
+                               for (int y = 1; y < 5; y++)\r
+                               {\r
+                                       int row = height * y / 5;\r
+                    // Redivivus.in Java to c# Porting update\r
+                    // 30/01/2010 \r
+                    // Commented & Added\r
+                    // START\r
+                    //sbyte[] localLuminances = source.getRow(row, luminances);\r
+                    localLuminances = source.getRow(row, luminances);\r
+                    // END\r
+                                       int right = (width << 2) / 5;\r
+                                       for (int x = width / 5; x < right; x++)\r
+                                       {\r
+                                               int pixel = localLuminances[x] & 0xff;\r
+                                               localBuckets[pixel >> LUMINANCE_SHIFT]++;\r
+                                       }\r
+                               }\r
+                               int blackPoint = estimateBlackPoint(localBuckets);\r
+                               \r
+                               // We delay reading the entire image luminance until the black point estimation succeeds.\r
+                               // Although we end up reading four rows twice, it is consistent with our motto of\r
+                               // "fail quickly" which is necessary for continuous scanning.\r
+\r
+                               localLuminances = source.Matrix; // Govinda : Removed sbyte []\r
+                               for (int y = 0; y < height; y++)\r
+                               {\r
+                                       int offset = y * width;\r
+                                       for (int x = 0; x < width; x++)\r
+                                       {\r
+                                               int pixel = localLuminances[offset + x] & 0xff;\r
+                                               if (pixel < blackPoint)\r
+                                               {\r
+                                                       matrix.set_Renamed(x, y);\r
+                                               }\r
+                                       }\r
+                               }\r
+                               \r
+                               return matrix;\r
+                       }\r
+                       \r
+               }\r
+               \r
+               private const int LUMINANCE_BITS = 5;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'LUMINANCE_SHIFT '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private static readonly int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;\r
+               //UPGRADE_NOTE: Final was removed from the declaration of 'LUMINANCE_BUCKETS '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
+               private static readonly int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;\r
+               \r
+               private sbyte[] luminances = null;\r
+               private int[] buckets = null;\r
+               \r
+               public GlobalHistogramBinarizer(LuminanceSource source):base(source)\r
+               {\r
+               }\r
+               \r
+               // Applies simple sharpening to the row data to improve performance of the 1D Readers.\r
+               public override BitArray getBlackRow(int y, BitArray row)\r
+               {\r
+                       LuminanceSource source = LuminanceSource;\r
+                       int width = source.Width;\r
+                       if (row == null || row.Size < width)\r
+                       {\r
+                               row = new BitArray(width);\r
+                       }\r
+                       else\r
+                       {\r
+                               row.clear();\r
+                       }\r
+                       \r
+                       initArrays(width);\r
+                       sbyte[] localLuminances = source.getRow(y, luminances);\r
+                       int[] localBuckets = buckets;\r
+                       for (int x = 0; x < width; x++)\r
+                       {\r
+                               int pixel = localLuminances[x] & 0xff;\r
+                               localBuckets[pixel >> LUMINANCE_SHIFT]++;\r
+                       }\r
+                       int blackPoint = estimateBlackPoint(localBuckets);\r
+                       \r
+                       int left = localLuminances[0] & 0xff;\r
+                       int center = localLuminances[1] & 0xff;\r
+                       for (int x = 1; x < width - 1; x++)\r
+                       {\r
+                               int right = localLuminances[x + 1] & 0xff;\r
+                               // A simple -1 4 -1 box filter with a weight of 2.\r
+                               int luminance = ((center << 2) - left - right) >> 1;\r
+                               if (luminance < blackPoint)\r
+                               {\r
+                                       row.set_Renamed(x);\r
+                               }\r
+                               left = center;\r
+                               center = right;\r
+                       }\r
+                       return row;\r
+               }\r
+               \r
+               public override Binarizer createBinarizer(LuminanceSource source)\r
+               {\r
+                       return new GlobalHistogramBinarizer(source);\r
+               }\r
+               \r
+               private void  initArrays(int luminanceSize)\r
+               {\r
+                       if (luminances == null || luminances.Length < luminanceSize)\r
+                       {\r
+                               luminances = new sbyte[luminanceSize];\r
+                       }\r
+                       if (buckets == null)\r
+                       {\r
+                               buckets = new int[LUMINANCE_BUCKETS];\r
+                       }\r
+                       else\r
+                       {\r
+                               for (int x = 0; x < LUMINANCE_BUCKETS; x++)\r
+                               {\r
+                                       buckets[x] = 0;\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               private static int estimateBlackPoint(int[] buckets)\r
+               {\r
+                       // Find the tallest peak in the histogram.\r
+                       int numBuckets = buckets.Length;\r
+                       int maxBucketCount = 0;\r
+                       int firstPeak = 0;\r
+                       int firstPeakSize = 0;\r
+                       for (int x = 0; x < numBuckets; x++)\r
+                       {\r
+                               if (buckets[x] > firstPeakSize)\r
+                               {\r
+                                       firstPeak = x;\r
+                                       firstPeakSize = buckets[x];\r
+                               }\r
+                               if (buckets[x] > maxBucketCount)\r
+                               {\r
+                                       maxBucketCount = buckets[x];\r
+                               }\r
+                       }\r
+                       \r
+                       // Find the second-tallest peak which is somewhat far from the tallest peak.\r
+                       int secondPeak = 0;\r
+                       int secondPeakScore = 0;\r
+                       for (int x = 0; x < numBuckets; x++)\r
+                       {\r
+                               int distanceToBiggest = x - firstPeak;\r
+                               // Encourage more distant second peaks by multiplying by square of distance.\r
+                               int score = buckets[x] * distanceToBiggest * distanceToBiggest;\r
+                               if (score > secondPeakScore)\r
+                               {\r
+                                       secondPeak = x;\r
+                                       secondPeakScore = score;\r
+                               }\r
+                       }\r
+                       \r
+                       // Make sure firstPeak corresponds to the black peak.\r
+                       if (firstPeak > secondPeak)\r
+                       {\r
+                               int temp = firstPeak;\r
+                               firstPeak = secondPeak;\r
+                               secondPeak = temp;\r
+                       }\r
+                       \r
+                       // If there is too little contrast in the image to pick a meaningful black point, throw rather\r
+                       // than waste time trying to decode the image, and risk false positives.\r
+                       // TODO: It might be worth comparing the brightest and darkest pixels seen, rather than the\r
+                       // two peaks, to determine the contrast.\r
+                       if (secondPeak - firstPeak <= numBuckets >> 4)\r
+                       {\r
+                               throw ReaderException.Instance;\r
+                       }\r
+                       \r
+                       // Find a valley between them that is low and closer to the white peak.\r
+                       int bestValley = secondPeak - 1;\r
+                       int bestValleyScore = - 1;\r
+                       for (int x = secondPeak - 1; x > firstPeak; x--)\r
+                       {\r
+                               int fromFirst = x - firstPeak;\r
+                               int score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);\r
+                               if (score > bestValleyScore)\r
+                               {\r
+                                       bestValley = x;\r
+                                       bestValleyScore = score;\r
+                               }\r
+                       }\r
+                       \r
+                       return bestValley << LUMINANCE_SHIFT;\r
+               }\r
+       }\r
+}
\ No newline at end of file