Committed C# port from Mohamad
[zxing.git] / csharp / common / BlackPointEstimator.cs
diff --git a/csharp/common/BlackPointEstimator.cs b/csharp/common/BlackPointEstimator.cs
new file mode 100755 (executable)
index 0000000..d922193
--- /dev/null
@@ -0,0 +1,118 @@
+/*\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
+\r
+using System;\r
+namespace com.google.zxing.common\r
+{\r
+\r
+    /// <summary> <p>Encapsulates logic that estimates the optimal "black point", the luminance value\r
+    /// which is the best line between "white" and "black" in a grayscale image.</p>\r
+    /// \r
+    /// <p>For an interesting discussion of this issue, see\r
+    /// <a href="http://webdiis.unizar.es/~neira/12082/thresholding.pdf">http://webdiis.unizar.es/~neira/12082/thresholding.pdf</a>.\r
+    /// </p>\r
+    /// \r
+    /// </summary>\r
+    /// <author>  srowen@google.com (Sean Owen)\r
+    /// </author>\r
+    /// <author>  dswitkin@google.com (Daniel Switkin)\r
+    /// </author>\r
+    public sealed class BlackPointEstimator\r
+    { \r
+          private BlackPointEstimator() \r
+          {\r
+          }\r
+\r
+          /**\r
+           * <p>Given an array of <em>counts</em> of luminance values (i.e. a histogram), this method\r
+           * decides which bucket of values corresponds to the black point -- which bucket contains the\r
+           * count of the brightest luminance values that should be considered "black".</p>\r
+           *\r
+           * @param histogram an array of <em>counts</em> of luminance values\r
+           * @return index within argument of bucket corresponding to brightest values which should be\r
+           *         considered "black"\r
+           * @throws ReaderException if "black" and "white" appear to be very close in luminance in the image\r
+           */\r
+          public static int estimate(int[] histogram)\r
+          {\r
+            try{\r
+          \r
+                int numBuckets = histogram.Length;\r
+                int maxBucketCount = 0;\r
+                // Find tallest peak in histogram\r
+                int firstPeak = 0;\r
+                int firstPeakSize = 0;\r
+                for (int i = 0; i < numBuckets; i++) {\r
+                  if (histogram[i] > firstPeakSize) {\r
+                    firstPeak = i;\r
+                    firstPeakSize = histogram[i];\r
+                  }\r
+                  if (histogram[i] > maxBucketCount) {\r
+                    maxBucketCount = histogram[i];\r
+                  }\r
+                }\r
+\r
+                // Find second-tallest peak -- well, another peak that is tall and not\r
+                // so close to the first one\r
+                int secondPeak = 0;\r
+                int secondPeakScore = 0;\r
+                for (int i = 0; i < numBuckets; i++) {\r
+                  int distanceToBiggest = i - firstPeak;\r
+                  // Encourage more distant second peaks by multiplying by square of distance\r
+                  int score = histogram[i] * distanceToBiggest * distanceToBiggest;\r
+                  if (score > secondPeakScore) {\r
+                    secondPeak = i;\r
+                    secondPeakScore = score;\r
+                  }\r
+                }\r
+\r
+                // Put firstPeak first\r
+                if (firstPeak > secondPeak) {\r
+                  int temp = firstPeak;\r
+                  firstPeak = secondPeak;\r
+                  secondPeak = temp;\r
+                }\r
+\r
+                // Kind of aribtrary; if the two peaks are very close, then we figure there is so little\r
+                // dynamic range in the image, that discriminating black and white is too error-prone.\r
+                // Decoding the image/line is either pointless, or may in some cases lead to a false positive\r
+                // for 1D formats, which are relatively lenient.\r
+                // We arbitrarily say "close" is "<= 1/16 of the total histogram buckets apart"\r
+                if (secondPeak - firstPeak <= numBuckets >> 4) {\r
+                  throw new ReaderException("");\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 i = secondPeak - 1; i > firstPeak; i--) {\r
+                  int fromFirst = i - firstPeak;\r
+                  // Favor a "valley" that is not too close to either peak -- especially not the black peak --\r
+                  // and that has a low value of course\r
+                  int score = fromFirst * fromFirst * (secondPeak - i) * (maxBucketCount - histogram[i]);\r
+                  if (score > bestValleyScore) {\r
+                    bestValley = i;\r
+                    bestValleyScore = score;\r
+                  }\r
+                }\r
+\r
+                return bestValley;\r
+                }\r
+            catch (Exception e)\r
+            {\r
+                throw (ReaderException) e; \r
+            }\r
+          }\r
+    }\r
+}
\ No newline at end of file