Major refactoring of 1D barcode code. Moved into com.google.zxing.oned package. Misc...
[zxing.git] / core / src / com / google / zxing / common / BlackPointEstimator.java
index a570a19..556da17 100644 (file)
@@ -25,6 +25,7 @@ package com.google.zxing.common;
  * </p>\r
  *\r
  * @author srowen@google.com (Sean Owen)\r
+ * @author dswitkin@google.com (Daniel Switkin)\r
  */\r
 public final class BlackPointEstimator {\r
 \r
@@ -37,10 +38,17 @@ public final class BlackPointEstimator {
    * 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
+   * @param biasTowardsWhite values higher than 1.0 suggest that a higher black point is desirable (e.g.\r
+   *  more values are considered black); less than 1.0 suggests that lower is desirable. Must be greater\r
+   *  than 0.0; 1.0 is a good "default"\r
    * @return index within argument of bucket corresponding to brightest values which should be\r
    *  considered "black"\r
    */\r
-  public static int estimate(int[] histogram) {\r
+  public static int estimate(int[] histogram, float biasTowardsWhite) {\r
+\r
+         if (Float.isNaN(biasTowardsWhite) || biasTowardsWhite <= 0.0f) {\r
+                 throw new IllegalArgumentException("Illegal biasTowardsWhite: " + biasTowardsWhite);\r
+         }\r
 \r
     int numBuckets = histogram.length;\r
 \r
@@ -79,7 +87,7 @@ public final class BlackPointEstimator {
     int bestValley = secondPeak - 1;\r
     int bestValleyScore = -1;\r
     for (int i = secondPeak - 1; i > firstPeak; i--) {\r
-      int fromFirst = i - firstPeak;\r
+      int fromFirst = (int) (biasTowardsWhite * (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) * (256 - histogram[i]);\r