int width = image.width();
int height = image.height();
int[] histogram = new int[LUMINANCE_BUCKETS];
- float biasTowardsWhite = 1.0f;
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
int startI = height == minDimension ? 0 : (height - width) >> 1;
if (argument < 0 || argument >= height) {
throw new IllegalArgumentException("Row is not within the image: " + argument);
}
- biasTowardsWhite = 2.0f;
int[] pixelRow = new int[width];
image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
for (int x = 0; x < width; x++) {
} else {
throw new IllegalArgumentException("Unknown method: " + method);
}
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
lastMethod = method;
lastArgument = argument;
}
int width = image.width();
int height = image.height();
int[] histogram = new int[LUMINANCE_BUCKETS];
- float biasTowardsWhite = 1.0f;
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
int startI = height == minDimension ? 0 : (height - width) >> 1;
if (argument < 0 || argument >= height) {
throw new IllegalArgumentException("Row is not within the image: " + argument);
}
- biasTowardsWhite = 2.0f;
int[] pixelRow = new int[width];
image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
for (int x = 0; x < width; x++) {
} else {
throw new IllegalArgumentException("Unknown method: " + method);
}
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
lastMethod = method;
lastArgument = argument;
}
* 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
* @throws ReaderException if "black" and "white" appear to be very close in luminance in the image\r
*/\r
- public static int estimate(int[] histogram, float biasTowardsWhite) throws ReaderException{\r
-\r
- if (Float.isNaN(biasTowardsWhite) || biasTowardsWhite <= 0.0f) {\r
- throw new IllegalArgumentException("Illegal biasTowardsWhite: " + biasTowardsWhite);\r
- }\r
+ public static int estimate(int[] histogram) throws ReaderException{\r
\r
int numBuckets = histogram.length;\r
\r
int bestValley = secondPeak - 1;\r
int bestValleyScore = -1;\r
for (int i = secondPeak - 1; i > firstPeak; i--) {\r
- int fromFirst = (int) (biasTowardsWhite * (i - firstPeak));\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) * (256 - histogram[i]);\r
--- /dev/null
+/*
+ * Copyright 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.zxing.common;
+
+import com.google.zxing.ReaderException;
+import junit.framework.TestCase;
+
+/**
+ * @author srowen@google.com (Sean Owen)
+ */
+public final class BlackPointEstimationMethodTestCase extends TestCase {
+
+ public void testBasic() throws ReaderException {
+ int[] histogram = { 0, 0, 11, 43, 37, 18, 3, 1, 0, 0, 13, 36, 24, 0, 11, 2 };
+ int point = BlackPointEstimator.estimate(histogram);
+ assertEquals(8, point);
+ }
+
+ public void testTooLittleRange() {
+ try {
+ int[] histogram = { 0, 0, 0, 0, 0, 0, 1, 43, 48, 18, 3, 1, 0, 0, 0, 0 };
+ BlackPointEstimator.estimate(histogram);
+ fail("Should have thrown an exception");
+ } catch (ReaderException re) {
+ // good
+ }
+ }
+
+}
\ No newline at end of file
public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
if (!method.equals(lastMethod) || argument != lastArgument) {
int[] histogram = new int[LUMINANCE_BUCKETS];
- float biasTowardsWhite = 1.0f;
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
if (argument < 0 || argument >= height) {
throw new IllegalArgumentException("Row is not within the image: " + argument);
}
- biasTowardsWhite = 2.0f;
int offset = argument * width;
for (int x = 0; x < width; x++) {
histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
} else {
throw new IllegalArgumentException("Unknown method: " + method);
}
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
lastMethod = method;
lastArgument = argument;
}
int width = image.getWidth();
int height = image.getHeight();
int[] histogram = new int[LUMINANCE_BUCKETS];
- float biasTowardsWhite = 1.0f;
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
int minDimension = width < height ? width : height;
int startI = height == minDimension ? 0 : (height - width) >> 1;
if (argument < 0 || argument >= height) {
throw new IllegalArgumentException("Row is not within the image: " + argument);
}
- biasTowardsWhite = 2.0f;
int[] rgbArray = new int[width];
image.getRGB(0, argument, width, 1, rgbArray, 0, width);
for (int x = 0; x < width; x++) {
} else {
throw new IllegalArgumentException("Unknown method: " + method);
}
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
+ blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
lastMethod = method;
lastArgument = argument;
}