More javadoc
[zxing.git] / core / src / com / google / zxing / qrcode / detector / AlignmentPatternFinder.java
1 /*\r
2  * Copyright 2007 Google Inc.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.qrcode.detector;\r
18 \r
19 import com.google.zxing.MonochromeBitmapSource;\r
20 import com.google.zxing.ReaderException;\r
21 import com.google.zxing.common.BitArray;\r
22 \r
23 import java.util.Vector;\r
24 \r
25 /**\r
26  * <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder\r
27  * patterns but are smaller and appear at regular intervals throughout the image.</p>\r
28  *\r
29  * <p>At the moment this only looks for the bottom-right alignment pattern.</p>\r
30  *\r
31  * <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,\r
32  * pasted and stripped down here for maximum performance but does unfortunately duplicate\r
33  * some code.</p>\r
34  *\r
35  * <p>This class is not thread-safe.</p>\r
36  *\r
37  * @author srowen@google.com (Sean Owen)\r
38  */\r
39 final class AlignmentPatternFinder {\r
40 \r
41   private final MonochromeBitmapSource image;\r
42   private final Vector possibleCenters;\r
43   private final int startX;\r
44   private final int startY;\r
45   private final int width;\r
46   private final int height;\r
47   private final float moduleSize;\r
48 \r
49   /**\r
50    * <p>Creates a finder that will look in a portion of the whole image.</p>\r
51    *\r
52    * @param image image to search\r
53    * @param startX left column from which to start searching\r
54    * @param startY top row from which to start searching\r
55    * @param width width of region to search\r
56    * @param height height of region to search\r
57    * @param moduleSize estimated module size so far\r
58    */\r
59   AlignmentPatternFinder(MonochromeBitmapSource image,\r
60                          int startX,\r
61                          int startY,\r
62                          int width,\r
63                          int height,\r
64                          float moduleSize) {\r
65     this.image = image;\r
66     this.possibleCenters = new Vector(5);\r
67     this.startX = startX;\r
68     this.startY = startY;\r
69     this.width = width;\r
70     this.height = height;\r
71     this.moduleSize = moduleSize;\r
72   }\r
73 \r
74   /**\r
75    * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since\r
76    * it's pretty performance-critical and so is written to be fast foremost.</p>\r
77    *\r
78    * @return {@link AlignmentPattern} if found\r
79    * @throws ReaderException if not found\r
80    */\r
81   AlignmentPattern find() throws ReaderException {\r
82     int startX = this.startX;\r
83     int height = this.height;\r
84     int maxJ = startX + width;\r
85     int middleI = startY + (height >> 1);\r
86     BitArray luminanceRow = new BitArray(width);\r
87     // We are looking for black/white/black modules in 1:1:1 ratio;\r
88     // this tracks the number of black/white/black modules seen so far\r
89     int[] stateCount = new int[3];\r
90     for (int iGen = 0; iGen < height; iGen++) {\r
91       // Search from middle outwards\r
92       int i = middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1));\r
93       image.getBlackRow(i, luminanceRow, startX, width);\r
94       stateCount[0] = 0;\r
95       stateCount[1] = 0;\r
96       stateCount[2] = 0;\r
97       int currentState = 0;\r
98       int j = startX;\r
99       // Burn off leading white pixels before anything else; if we start in the middle of\r
100       // a white run, it doesn't make sense to count its length, since we don't know if the\r
101       // white run continued to the left of the start point\r
102       while (!luminanceRow.get(j - startX) && j < maxJ) {\r
103         j++;\r
104       }\r
105       while (j < maxJ) {\r
106         if (luminanceRow.get(j - startX)) {\r
107           // Black pixel\r
108           if (currentState == 1) { // Counting black pixels\r
109             stateCount[currentState]++;\r
110           } else { // Counting white pixels\r
111             if (currentState == 2) { // A winner?\r
112               if (foundPatternCross(stateCount)) { // Yes\r
113                 AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, j);\r
114                 if (confirmed != null) {\r
115                   return confirmed;\r
116                 }\r
117               }\r
118               stateCount[0] = stateCount[2];\r
119               stateCount[1] = 1;\r
120               stateCount[2] = 0;\r
121               currentState = 1;\r
122             } else {\r
123               stateCount[++currentState]++;\r
124             }\r
125           }\r
126         } else { // White pixel\r
127           if (currentState == 1) { // Counting black pixels\r
128             currentState++;\r
129           }\r
130           stateCount[currentState]++;\r
131         }\r
132         j++;\r
133       }\r
134       if (foundPatternCross(stateCount)) {\r
135         AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, maxJ);\r
136         if (confirmed != null) {\r
137           return confirmed;\r
138         }\r
139       }\r
140 \r
141     }\r
142 \r
143     // Hmm, nothing we saw was observed and confirmed twice. If we had\r
144     // any guess at all, return it.\r
145     if (!possibleCenters.isEmpty()) {\r
146       return (AlignmentPattern) possibleCenters.elementAt(0);\r
147     }\r
148 \r
149     throw new ReaderException("Could not find alignment pattern");\r
150   }\r
151 \r
152   /**\r
153    * Given a count of black/white/black pixels just seen and an end position,\r
154    * figures the location of the center of this black/white/black run.\r
155    */\r
156   private static float centerFromEnd(int[] stateCount, int end) {\r
157     return (float) (end - stateCount[2]) - stateCount[1] / 2.0f;\r
158   }\r
159 \r
160   /**\r
161    * @param stateCount count of black/white/black pixels just read\r
162    * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios\r
163    *  used by alignment patterns to be considered a match\r
164    */\r
165   private boolean foundPatternCross(int[] stateCount) {\r
166     float moduleSize = this.moduleSize;\r
167     for (int i = 0; i < 3; i++) {\r
168       if (2.0f * Math.abs(moduleSize - stateCount[i]) >= moduleSize) {\r
169         return false;\r
170       }\r
171     }\r
172     return true;\r
173   }\r
174 \r
175   /**\r
176    * <p>After a horizontal scan finds a potential alignment pattern, this method\r
177    * "cross-checks" by scanning down vertically through the center of the possible\r
178    * alignment pattern to see if the same proportion is detected.</p>\r
179    *\r
180    * @param startI row where an alignment pattern was detected\r
181    * @param centerJ center of the section that appears to cross an alignment pattern\r
182    * @param maxCount maximum reasonable number of modules that should be\r
183    *  observed in any reading state, based on the results of the horizontal scan\r
184    * @return vertical center of alignment pattern, or {@link Float#NaN} if not found\r
185    */\r
186   private float crossCheckVertical(int startI, int centerJ, int maxCount) {\r
187     MonochromeBitmapSource image = this.image;\r
188 \r
189     int maxI = image.getHeight();\r
190     int[] stateCount = new int[3];\r
191 \r
192     // Start counting up from center\r
193     int i = startI;\r
194     while (i >= 0 && image.isBlack(centerJ, i) && stateCount[1] <= maxCount) {\r
195       stateCount[1]++;\r
196       i--;\r
197     }\r
198     // If already too many modules in this state or ran off the edge:\r
199     if (i < 0 || stateCount[1] > maxCount) {\r
200       return Float.NaN;\r
201     }\r
202     while (i >= 0 && !image.isBlack(centerJ, i) && stateCount[0] <= maxCount) {\r
203       stateCount[0]++;\r
204       i--;\r
205     }\r
206     if (stateCount[0] > maxCount) {\r
207       return Float.NaN;\r
208     }\r
209 \r
210     // Now also count down from center\r
211     i = startI + 1;\r
212     while (i < maxI && image.isBlack(centerJ, i) && stateCount[1] <= maxCount) {\r
213       stateCount[1]++;\r
214       i++;\r
215     }\r
216     if (i == maxI || stateCount[1] > maxCount) {\r
217       return Float.NaN;\r
218     }\r
219     while (i < maxI && !image.isBlack(centerJ, i) && stateCount[2] <= maxCount) {\r
220       stateCount[2]++;\r
221       i++;\r
222     }\r
223     if (stateCount[2] > maxCount) {\r
224       return Float.NaN;\r
225     }\r
226 \r
227     return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : Float.NaN;\r
228   }\r
229 \r
230   /**\r
231    * <p>This is called when a horizontal scan finds a possible alignment pattern. It will\r
232    * cross check with a vertical scan, and if successful, will see if this pattern had been\r
233    * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have\r
234    * found the alignment pattern.</p>\r
235    *\r
236    * @param stateCount reading state module counts from horizontal scan\r
237    * @param i row where alignment pattern may be found\r
238    * @param j end of possible alignment pattern in row\r
239    * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not\r
240    */\r
241   private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j) {\r
242     float centerJ = centerFromEnd(stateCount, j);\r
243     float centerI = crossCheckVertical(i, (int) centerJ, 2 * stateCount[1]);\r
244     if (!Float.isNaN(centerI)) {\r
245       float estimatedModuleSize = (float) (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f;\r
246       int max = possibleCenters.size();\r
247       for (int index = 0; index < max; index++) {\r
248         AlignmentPattern center = (AlignmentPattern) possibleCenters.elementAt(index);\r
249         // Look for about the same center and module size:\r
250         if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {\r
251           return new AlignmentPattern(centerJ, centerI, estimatedModuleSize);\r
252         }\r
253       }\r
254       // Hadn't found this before; save it\r
255       possibleCenters.addElement(new AlignmentPattern(centerJ, centerI, estimatedModuleSize));\r
256     }\r
257     return null;\r
258   }\r
259 \r
260 }