]> Pileus Git - ~andy/freeotp/blob - src/com/google/zxing/common/GlobalHistogramBinarizer.java
Add native camera support
[~andy/freeotp] / src / com / google / zxing / common / GlobalHistogramBinarizer.java
1 /*
2  * Copyright 2009 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.common;
18
19 import com.google.zxing.Binarizer;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.NotFoundException;
22
23 /**
24  * This Binarizer implementation uses the old ZXing global histogram approach. It is suitable
25  * for low-end mobile devices which don't have enough CPU or memory to use a local thresholding
26  * algorithm. However, because it picks a global black point, it cannot handle difficult shadows
27  * and gradients.
28  *
29  * Faster mobile devices and all desktop applications should probably use HybridBinarizer instead.
30  *
31  * @author dswitkin@google.com (Daniel Switkin)
32  * @author Sean Owen
33  */
34 public class GlobalHistogramBinarizer extends Binarizer {
35
36   private static final int LUMINANCE_BITS = 5;
37   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
38   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
39   private static final byte[] EMPTY = new byte[0];
40
41   private byte[] luminances;
42   private final int[] buckets;
43
44   public GlobalHistogramBinarizer(LuminanceSource source) {
45     super(source);
46     luminances = EMPTY;
47     buckets = new int[LUMINANCE_BUCKETS];
48   }
49
50   // Applies simple sharpening to the row data to improve performance of the 1D Readers.
51   @Override
52   public BitArray getBlackRow(int y, BitArray row) throws NotFoundException {
53     LuminanceSource source = getLuminanceSource();
54     int width = source.getWidth();
55     if (row == null || row.getSize() < width) {
56       row = new BitArray(width);
57     } else {
58       row.clear();
59     }
60
61     initArrays(width);
62     byte[] localLuminances = source.getRow(y, luminances);
63     int[] localBuckets = buckets;
64     for (int x = 0; x < width; x++) {
65       int pixel = localLuminances[x] & 0xff;
66       localBuckets[pixel >> LUMINANCE_SHIFT]++;
67     }
68     int blackPoint = estimateBlackPoint(localBuckets);
69
70     int left = localLuminances[0] & 0xff;
71     int center = localLuminances[1] & 0xff;
72     for (int x = 1; x < width - 1; x++) {
73       int right = localLuminances[x + 1] & 0xff;
74       // A simple -1 4 -1 box filter with a weight of 2.
75       int luminance = ((center << 2) - left - right) >> 1;
76       if (luminance < blackPoint) {
77         row.set(x);
78       }
79       left = center;
80       center = right;
81     }
82     return row;
83   }
84
85   // Does not sharpen the data, as this call is intended to only be used by 2D Readers.
86   @Override
87   public BitMatrix getBlackMatrix() throws NotFoundException {
88     LuminanceSource source = getLuminanceSource();
89     int width = source.getWidth();
90     int height = source.getHeight();
91     BitMatrix matrix = new BitMatrix(width, height);
92
93     // Quickly calculates the histogram by sampling four rows from the image. This proved to be
94     // more robust on the blackbox tests than sampling a diagonal as we used to do.
95     initArrays(width);
96     int[] localBuckets = buckets;
97     for (int y = 1; y < 5; y++) {
98       int row = height * y / 5;
99       byte[] localLuminances = source.getRow(row, luminances);
100       int right = (width << 2) / 5;
101       for (int x = width / 5; x < right; x++) {
102         int pixel = localLuminances[x] & 0xff;
103         localBuckets[pixel >> LUMINANCE_SHIFT]++;
104       }
105     }
106     int blackPoint = estimateBlackPoint(localBuckets);
107
108     // We delay reading the entire image luminance until the black point estimation succeeds.
109     // Although we end up reading four rows twice, it is consistent with our motto of
110     // "fail quickly" which is necessary for continuous scanning.
111     byte[] localLuminances = source.getMatrix();
112     for (int y = 0; y < height; y++) {
113       int offset = y * width;
114       for (int x = 0; x< width; x++) {
115         int pixel = localLuminances[offset + x] & 0xff;
116         if (pixel < blackPoint) {
117           matrix.set(x, y);
118         }
119       }
120     }
121
122     return matrix;
123   }
124
125   @Override
126   public Binarizer createBinarizer(LuminanceSource source) {
127     return new GlobalHistogramBinarizer(source);
128   }
129
130   private void initArrays(int luminanceSize) {
131     if (luminances.length < luminanceSize) {
132       luminances = new byte[luminanceSize];
133     }
134     for (int x = 0; x < LUMINANCE_BUCKETS; x++) {
135       buckets[x] = 0;
136     }
137   }
138
139   private static int estimateBlackPoint(int[] buckets) throws NotFoundException {
140     // Find the tallest peak in the histogram.
141     int numBuckets = buckets.length;
142     int maxBucketCount = 0;
143     int firstPeak = 0;
144     int firstPeakSize = 0;
145     for (int x = 0; x < numBuckets; x++) {
146       if (buckets[x] > firstPeakSize) {
147         firstPeak = x;
148         firstPeakSize = buckets[x];
149       }
150       if (buckets[x] > maxBucketCount) {
151         maxBucketCount = buckets[x];
152       }
153     }
154
155     // Find the second-tallest peak which is somewhat far from the tallest peak.
156     int secondPeak = 0;
157     int secondPeakScore = 0;
158     for (int x = 0; x < numBuckets; x++) {
159       int distanceToBiggest = x - firstPeak;
160       // Encourage more distant second peaks by multiplying by square of distance.
161       int score = buckets[x] * distanceToBiggest * distanceToBiggest;
162       if (score > secondPeakScore) {
163         secondPeak = x;
164         secondPeakScore = score;
165       }
166     }
167
168     // Make sure firstPeak corresponds to the black peak.
169     if (firstPeak > secondPeak) {
170       int temp = firstPeak;
171       firstPeak = secondPeak;
172       secondPeak = temp;
173     }
174
175     // If there is too little contrast in the image to pick a meaningful black point, throw rather
176     // than waste time trying to decode the image, and risk false positives.
177     if (secondPeak - firstPeak <= numBuckets >> 4) {
178       throw NotFoundException.getNotFoundInstance();
179     }
180
181     // Find a valley between them that is low and closer to the white peak.
182     int bestValley = secondPeak - 1;
183     int bestValleyScore = -1;
184     for (int x = secondPeak - 1; x > firstPeak; x--) {
185       int fromFirst = x - firstPeak;
186       int score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);
187       if (score > bestValleyScore) {
188         bestValley = x;
189         bestValleyScore = score;
190       }
191     }
192
193     return bestValley << LUMINANCE_SHIFT;
194   }
195
196 }