Adjust formatting on last change. Simplify GridSampler
[zxing.git] / core / src / com / google / zxing / qrcode / detector / FinderPatternFinder.java
1 /*\r
2  * Copyright 2007 ZXing authors\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.DecodeHintType;\r
20 import com.google.zxing.NotFoundException;\r
21 import com.google.zxing.ResultPoint;\r
22 import com.google.zxing.ResultPointCallback;\r
23 import com.google.zxing.common.BitMatrix;\r
24 import com.google.zxing.common.Collections;\r
25 import com.google.zxing.common.Comparator;\r
26 \r
27 import java.util.Hashtable;\r
28 import java.util.Vector;\r
29 \r
30 /**\r
31  * <p>This class attempts to find finder patterns in a QR Code. Finder patterns are the square\r
32  * markers at three corners of a QR Code.</p>\r
33  *\r
34  * <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.\r
35  *\r
36  * @author Sean Owen\r
37  */\r
38 public class FinderPatternFinder {\r
39 \r
40   private static final int CENTER_QUORUM = 2;\r
41   protected static final int MIN_SKIP = 3; // 1 pixel/module times 3 modules/center\r
42   protected static final int MAX_MODULES = 57; // support up to version 10 for mobile clients\r
43   private static final int INTEGER_MATH_SHIFT = 8;\r
44 \r
45   private final BitMatrix image;\r
46   private final Vector possibleCenters;\r
47   private boolean hasSkipped;\r
48   private final int[] crossCheckStateCount;\r
49   private final ResultPointCallback resultPointCallback;\r
50 \r
51   /**\r
52    * <p>Creates a finder that will search the image for three finder patterns.</p>\r
53    *\r
54    * @param image image to search\r
55    */\r
56   public FinderPatternFinder(BitMatrix image) {\r
57     this(image, null);\r
58   }\r
59 \r
60   public FinderPatternFinder(BitMatrix image, ResultPointCallback resultPointCallback) {\r
61     this.image = image;\r
62     this.possibleCenters = new Vector();\r
63     this.crossCheckStateCount = new int[5];\r
64     this.resultPointCallback = resultPointCallback;\r
65   }\r
66 \r
67   protected BitMatrix getImage() {\r
68     return image;\r
69   }\r
70 \r
71   protected Vector getPossibleCenters() {\r
72     return possibleCenters;\r
73   }\r
74 \r
75   FinderPatternInfo find(Hashtable hints) throws NotFoundException {\r
76     boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);\r
77     int maxI = image.getHeight();\r
78     int maxJ = image.getWidth();\r
79     // We are looking for black/white/black/white/black modules in\r
80     // 1:1:3:1:1 ratio; this tracks the number of such modules seen so far\r
81 \r
82     // Let's assume that the maximum version QR Code we support takes up 1/4 the height of the\r
83     // image, and then account for the center being 3 modules in size. This gives the smallest\r
84     // number of pixels the center could be, so skip this often. When trying harder, look for all\r
85     // QR versions regardless of how dense they are.\r
86     int iSkip = (3 * maxI) / (4 * MAX_MODULES);\r
87     if (iSkip < MIN_SKIP || tryHarder) {\r
88       iSkip = MIN_SKIP;\r
89     }\r
90 \r
91     boolean done = false;\r
92     int[] stateCount = new int[5];\r
93     for (int i = iSkip - 1; i < maxI && !done; i += iSkip) {\r
94       // Get a row of black/white values\r
95       stateCount[0] = 0;\r
96       stateCount[1] = 0;\r
97       stateCount[2] = 0;\r
98       stateCount[3] = 0;\r
99       stateCount[4] = 0;\r
100       int currentState = 0;\r
101       for (int j = 0; j < maxJ; j++) {\r
102         if (image.get(j, i)) {\r
103           // Black pixel\r
104           if ((currentState & 1) == 1) { // Counting white pixels\r
105             currentState++;\r
106           }\r
107           stateCount[currentState]++;\r
108         } else { // White pixel\r
109           if ((currentState & 1) == 0) { // Counting black pixels\r
110             if (currentState == 4) { // A winner?\r
111               if (foundPatternCross(stateCount)) { // Yes\r
112                 boolean confirmed = handlePossibleCenter(stateCount, i, j);\r
113                 if (confirmed) {\r
114                   // Start examining every other line. Checking each line turned out to be too\r
115                   // expensive and didn't improve performance.\r
116                   iSkip = 2;\r
117                   if (hasSkipped) {\r
118                     done = haveMultiplyConfirmedCenters();\r
119                   } else {\r
120                     int rowSkip = findRowSkip();\r
121                     if (rowSkip > stateCount[2]) {\r
122                       // Skip rows between row of lower confirmed center\r
123                       // and top of presumed third confirmed center\r
124                       // but back up a bit to get a full chance of detecting\r
125                       // it, entire width of center of finder pattern\r
126 \r
127                       // Skip by rowSkip, but back off by stateCount[2] (size of last center\r
128                       // of pattern we saw) to be conservative, and also back off by iSkip which\r
129                       // is about to be re-added\r
130                       i += rowSkip - stateCount[2] - iSkip;\r
131                       j = maxJ - 1;\r
132                     }\r
133                   }\r
134                 } else {\r
135                   stateCount[0] = stateCount[2];\r
136                   stateCount[1] = stateCount[3];\r
137                   stateCount[2] = stateCount[4];\r
138                   stateCount[3] = 1;\r
139                   stateCount[4] = 0;\r
140                   currentState = 3;\r
141                   continue;\r
142                 }\r
143                 // Clear state to start looking again\r
144                 currentState = 0;\r
145                 stateCount[0] = 0;\r
146                 stateCount[1] = 0;\r
147                 stateCount[2] = 0;\r
148                 stateCount[3] = 0;\r
149                 stateCount[4] = 0;\r
150               } else { // No, shift counts back by two\r
151                 stateCount[0] = stateCount[2];\r
152                 stateCount[1] = stateCount[3];\r
153                 stateCount[2] = stateCount[4];\r
154                 stateCount[3] = 1;\r
155                 stateCount[4] = 0;\r
156                 currentState = 3;\r
157               }\r
158             } else {\r
159               stateCount[++currentState]++;\r
160             }\r
161           } else { // Counting white pixels\r
162             stateCount[currentState]++;\r
163           }\r
164         }\r
165       }\r
166       if (foundPatternCross(stateCount)) {\r
167         boolean confirmed = handlePossibleCenter(stateCount, i, maxJ);\r
168         if (confirmed) {\r
169           iSkip = stateCount[0];\r
170           if (hasSkipped) {\r
171             // Found a third one\r
172             done = haveMultiplyConfirmedCenters();\r
173           }\r
174         }\r
175       }\r
176     }\r
177 \r
178     FinderPattern[] patternInfo = selectBestPatterns();\r
179     ResultPoint.orderBestPatterns(patternInfo);\r
180 \r
181     return new FinderPatternInfo(patternInfo);\r
182   }\r
183 \r
184   /**\r
185    * Given a count of black/white/black/white/black pixels just seen and an end position,\r
186    * figures the location of the center of this run.\r
187    */\r
188   private static float centerFromEnd(int[] stateCount, int end) {\r
189     return (float) (end - stateCount[4] - stateCount[3]) - stateCount[2] / 2.0f;\r
190   }\r
191 \r
192   /**\r
193    * @param stateCount count of black/white/black/white/black pixels just read\r
194    * @return true iff the proportions of the counts is close enough to the 1/1/3/1/1 ratios\r
195    *         used by finder patterns to be considered a match\r
196    */\r
197   protected static boolean foundPatternCross(int[] stateCount) {\r
198     int totalModuleSize = 0;\r
199     for (int i = 0; i < 5; i++) {\r
200       int count = stateCount[i];\r
201       if (count == 0) {\r
202         return false;\r
203       }\r
204       totalModuleSize += count;\r
205     }\r
206     if (totalModuleSize < 7) {\r
207       return false;\r
208     }\r
209     int moduleSize = (totalModuleSize << INTEGER_MATH_SHIFT) / 7;\r
210     int maxVariance = moduleSize / 2;\r
211     // Allow less than 50% variance from 1-1-3-1-1 proportions\r
212     return Math.abs(moduleSize - (stateCount[0] << INTEGER_MATH_SHIFT)) < maxVariance &&\r
213         Math.abs(moduleSize - (stateCount[1] << INTEGER_MATH_SHIFT)) < maxVariance &&\r
214         Math.abs(3 * moduleSize - (stateCount[2] << INTEGER_MATH_SHIFT)) < 3 * maxVariance &&\r
215         Math.abs(moduleSize - (stateCount[3] << INTEGER_MATH_SHIFT)) < maxVariance &&\r
216         Math.abs(moduleSize - (stateCount[4] << INTEGER_MATH_SHIFT)) < maxVariance;\r
217   }\r
218 \r
219   private int[] getCrossCheckStateCount() {\r
220     crossCheckStateCount[0] = 0;\r
221     crossCheckStateCount[1] = 0;\r
222     crossCheckStateCount[2] = 0;\r
223     crossCheckStateCount[3] = 0;\r
224     crossCheckStateCount[4] = 0;\r
225     return crossCheckStateCount;\r
226   }\r
227 \r
228   /**\r
229    * <p>After a horizontal scan finds a potential finder pattern, this method\r
230    * "cross-checks" by scanning down vertically through the center of the possible\r
231    * finder pattern to see if the same proportion is detected.</p>\r
232    *\r
233    * @param startI row where a finder pattern was detected\r
234    * @param centerJ center of the section that appears to cross a finder pattern\r
235    * @param maxCount maximum reasonable number of modules that should be\r
236    * observed in any reading state, based on the results of the horizontal scan\r
237    * @return vertical center of finder pattern, or {@link Float#NaN} if not found\r
238    */\r
239   private float crossCheckVertical(int startI, int centerJ, int maxCount,\r
240       int originalStateCountTotal) {\r
241     BitMatrix image = this.image;\r
242 \r
243     int maxI = image.getHeight();\r
244     int[] stateCount = getCrossCheckStateCount();\r
245 \r
246     // Start counting up from center\r
247     int i = startI;\r
248     while (i >= 0 && image.get(centerJ, i)) {\r
249       stateCount[2]++;\r
250       i--;\r
251     }\r
252     if (i < 0) {\r
253       return Float.NaN;\r
254     }\r
255     while (i >= 0 && !image.get(centerJ, i) && stateCount[1] <= maxCount) {\r
256       stateCount[1]++;\r
257       i--;\r
258     }\r
259     // If already too many modules in this state or ran off the edge:\r
260     if (i < 0 || stateCount[1] > maxCount) {\r
261       return Float.NaN;\r
262     }\r
263     while (i >= 0 && image.get(centerJ, i) && stateCount[0] <= maxCount) {\r
264       stateCount[0]++;\r
265       i--;\r
266     }\r
267     if (stateCount[0] > maxCount) {\r
268       return Float.NaN;\r
269     }\r
270 \r
271     // Now also count down from center\r
272     i = startI + 1;\r
273     while (i < maxI && image.get(centerJ, i)) {\r
274       stateCount[2]++;\r
275       i++;\r
276     }\r
277     if (i == maxI) {\r
278       return Float.NaN;\r
279     }\r
280     while (i < maxI && !image.get(centerJ, i) && stateCount[3] < maxCount) {\r
281       stateCount[3]++;\r
282       i++;\r
283     }\r
284     if (i == maxI || stateCount[3] >= maxCount) {\r
285       return Float.NaN;\r
286     }\r
287     while (i < maxI && image.get(centerJ, i) && stateCount[4] < maxCount) {\r
288       stateCount[4]++;\r
289       i++;\r
290     }\r
291     if (stateCount[4] >= maxCount) {\r
292       return Float.NaN;\r
293     }\r
294 \r
295     // If we found a finder-pattern-like section, but its size is more than 40% different than\r
296     // the original, assume it's a false positive\r
297     int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +\r
298         stateCount[4];\r
299     if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {\r
300       return Float.NaN;\r
301     }\r
302 \r
303     return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : Float.NaN;\r
304   }\r
305 \r
306   /**\r
307    * <p>Like {@link #crossCheckVertical(int, int, int, int)}, and in fact is basically identical,\r
308    * except it reads horizontally instead of vertically. This is used to cross-cross\r
309    * check a vertical cross check and locate the real center of the alignment pattern.</p>\r
310    */\r
311   private float crossCheckHorizontal(int startJ, int centerI, int maxCount,\r
312       int originalStateCountTotal) {\r
313     BitMatrix image = this.image;\r
314 \r
315     int maxJ = image.getWidth();\r
316     int[] stateCount = getCrossCheckStateCount();\r
317 \r
318     int j = startJ;\r
319     while (j >= 0 && image.get(j, centerI)) {\r
320       stateCount[2]++;\r
321       j--;\r
322     }\r
323     if (j < 0) {\r
324       return Float.NaN;\r
325     }\r
326     while (j >= 0 && !image.get(j, centerI) && stateCount[1] <= maxCount) {\r
327       stateCount[1]++;\r
328       j--;\r
329     }\r
330     if (j < 0 || stateCount[1] > maxCount) {\r
331       return Float.NaN;\r
332     }\r
333     while (j >= 0 && image.get(j, centerI) && stateCount[0] <= maxCount) {\r
334       stateCount[0]++;\r
335       j--;\r
336     }\r
337     if (stateCount[0] > maxCount) {\r
338       return Float.NaN;\r
339     }\r
340 \r
341     j = startJ + 1;\r
342     while (j < maxJ && image.get(j, centerI)) {\r
343       stateCount[2]++;\r
344       j++;\r
345     }\r
346     if (j == maxJ) {\r
347       return Float.NaN;\r
348     }\r
349     while (j < maxJ && !image.get(j, centerI) && stateCount[3] < maxCount) {\r
350       stateCount[3]++;\r
351       j++;\r
352     }\r
353     if (j == maxJ || stateCount[3] >= maxCount) {\r
354       return Float.NaN;\r
355     }\r
356     while (j < maxJ && image.get(j, centerI) && stateCount[4] < maxCount) {\r
357       stateCount[4]++;\r
358       j++;\r
359     }\r
360     if (stateCount[4] >= maxCount) {\r
361       return Float.NaN;\r
362     }\r
363 \r
364     // If we found a finder-pattern-like section, but its size is significantly different than\r
365     // the original, assume it's a false positive\r
366     int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +\r
367         stateCount[4];\r
368     if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= originalStateCountTotal) {\r
369       return Float.NaN;\r
370     }\r
371 \r
372     return foundPatternCross(stateCount) ? centerFromEnd(stateCount, j) : Float.NaN;\r
373   }\r
374 \r
375   /**\r
376    * <p>This is called when a horizontal scan finds a possible alignment pattern. It will\r
377    * cross check with a vertical scan, and if successful, will, ah, cross-cross-check\r
378    * with another horizontal scan. This is needed primarily to locate the real horizontal\r
379    * center of the pattern in cases of extreme skew.</p>\r
380    *\r
381    * <p>If that succeeds the finder pattern location is added to a list that tracks\r
382    * the number of times each location has been nearly-matched as a finder pattern.\r
383    * Each additional find is more evidence that the location is in fact a finder\r
384    * pattern center\r
385    *\r
386    * @param stateCount reading state module counts from horizontal scan\r
387    * @param i row where finder pattern may be found\r
388    * @param j end of possible finder pattern in row\r
389    * @return true if a finder pattern candidate was found this time\r
390    */\r
391   protected boolean handlePossibleCenter(int[] stateCount, int i, int j) {\r
392     int stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2] + stateCount[3] +\r
393         stateCount[4];\r
394     float centerJ = centerFromEnd(stateCount, j);\r
395     float centerI = crossCheckVertical(i, (int) centerJ, stateCount[2], stateCountTotal);\r
396     if (!Float.isNaN(centerI)) {\r
397       // Re-cross check\r
398       centerJ = crossCheckHorizontal((int) centerJ, (int) centerI, stateCount[2], stateCountTotal);\r
399       if (!Float.isNaN(centerJ)) {\r
400         float estimatedModuleSize = (float) stateCountTotal / 7.0f;\r
401         boolean found = false;\r
402         int max = possibleCenters.size();\r
403         for (int index = 0; index < max; index++) {\r
404           FinderPattern center = (FinderPattern) possibleCenters.elementAt(index);\r
405           // Look for about the same center and module size:\r
406           if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {\r
407             center.incrementCount();\r
408             found = true;\r
409             break;\r
410           }\r
411         }\r
412         if (!found) {\r
413           ResultPoint point = new FinderPattern(centerJ, centerI, estimatedModuleSize);\r
414           possibleCenters.addElement(point);\r
415           if (resultPointCallback != null) {\r
416             resultPointCallback.foundPossibleResultPoint(point);\r
417           }\r
418         }\r
419         return true;\r
420       }\r
421     }\r
422     return false;\r
423   }\r
424 \r
425   /**\r
426    * @return number of rows we could safely skip during scanning, based on the first\r
427    *         two finder patterns that have been located. In some cases their position will\r
428    *         allow us to infer that the third pattern must lie below a certain point farther\r
429    *         down in the image.\r
430    */\r
431   private int findRowSkip() {\r
432     int max = possibleCenters.size();\r
433     if (max <= 1) {\r
434       return 0;\r
435     }\r
436     FinderPattern firstConfirmedCenter = null;\r
437     for (int i = 0; i < max; i++) {\r
438       FinderPattern center = (FinderPattern) possibleCenters.elementAt(i);\r
439       if (center.getCount() >= CENTER_QUORUM) {\r
440         if (firstConfirmedCenter == null) {\r
441           firstConfirmedCenter = center;\r
442         } else {\r
443           // We have two confirmed centers\r
444           // How far down can we skip before resuming looking for the next\r
445           // pattern? In the worst case, only the difference between the\r
446           // difference in the x / y coordinates of the two centers.\r
447           // This is the case where you find top left last.\r
448           hasSkipped = true;\r
449           return (int) (Math.abs(firstConfirmedCenter.getX() - center.getX()) -\r
450               Math.abs(firstConfirmedCenter.getY() - center.getY())) / 2;\r
451         }\r
452       }\r
453     }\r
454     return 0;\r
455   }\r
456 \r
457   /**\r
458    * @return true iff we have found at least 3 finder patterns that have been detected\r
459    *         at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the\r
460    *         candidates is "pretty similar"\r
461    */\r
462   private boolean haveMultiplyConfirmedCenters() {\r
463     int confirmedCount = 0;\r
464     float totalModuleSize = 0.0f;\r
465     int max = possibleCenters.size();\r
466     for (int i = 0; i < max; i++) {\r
467       FinderPattern pattern = (FinderPattern) possibleCenters.elementAt(i);\r
468       if (pattern.getCount() >= CENTER_QUORUM) {\r
469         confirmedCount++;\r
470         totalModuleSize += pattern.getEstimatedModuleSize();\r
471       }\r
472     }\r
473     if (confirmedCount < 3) {\r
474       return false;\r
475     }\r
476     // OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"\r
477     // and that we need to keep looking. We detect this by asking if the estimated module sizes\r
478     // vary too much. We arbitrarily say that when the total deviation from average exceeds\r
479     // 5% of the total module size estimates, it's too much.\r
480     float average = totalModuleSize / (float) max;\r
481     float totalDeviation = 0.0f;\r
482     for (int i = 0; i < max; i++) {\r
483       FinderPattern pattern = (FinderPattern) possibleCenters.elementAt(i);\r
484       totalDeviation += Math.abs(pattern.getEstimatedModuleSize() - average);\r
485     }\r
486     return totalDeviation <= 0.05f * totalModuleSize;\r
487   }\r
488 \r
489   /**\r
490    * @return the 3 best {@link FinderPattern}s from our list of candidates. The "best" are\r
491    *         those that have been detected at least {@link #CENTER_QUORUM} times, and whose module\r
492    *         size differs from the average among those patterns the least\r
493    * @throws NotFoundException if 3 such finder patterns do not exist\r
494    */\r
495   private FinderPattern[] selectBestPatterns() throws NotFoundException {\r
496 \r
497     int startSize = possibleCenters.size();\r
498     if (startSize < 3) {\r
499       // Couldn't find enough finder patterns\r
500       throw NotFoundException.getNotFoundInstance();\r
501     }\r
502 \r
503     // Filter outlier possibilities whose module size is too different\r
504     if (startSize > 3) {\r
505       // But we can only afford to do so if we have at least 4 possibilities to choose from\r
506       float totalModuleSize = 0.0f;\r
507       float square = 0.0f;\r
508       for (int i = 0; i < startSize; i++) {\r
509         float size = ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
510         totalModuleSize += size;\r
511         square += size * size;\r
512       }\r
513       float average = totalModuleSize / (float) startSize;\r
514       float stdDev = (float) Math.sqrt(square / startSize - average * average);\r
515 \r
516       Collections.insertionSort(possibleCenters, new FurthestFromAverageComparator(average));\r
517 \r
518       float limit = Math.max(0.2f * average, stdDev);\r
519 \r
520       for (int i = 0; i < possibleCenters.size() && possibleCenters.size() > 3; i++) {\r
521         FinderPattern pattern = (FinderPattern) possibleCenters.elementAt(i);\r
522         if (Math.abs(pattern.getEstimatedModuleSize() - average) > limit) {\r
523           possibleCenters.removeElementAt(i);\r
524           i--;\r
525         }\r
526       }\r
527     }\r
528 \r
529     if (possibleCenters.size() > 3) {\r
530       // Throw away all but those first size candidate points we found.\r
531 \r
532       float totalModuleSize = 0.0f;\r
533       for (int i = 0; i < possibleCenters.size(); i++) {\r
534         totalModuleSize += ((FinderPattern) possibleCenters.elementAt(i)).getEstimatedModuleSize();\r
535       }\r
536 \r
537       float average = totalModuleSize / (float) possibleCenters.size();\r
538 \r
539       Collections.insertionSort(possibleCenters, new CenterComparator(average));\r
540 \r
541       possibleCenters.setSize(3);\r
542     }\r
543 \r
544     return new FinderPattern[]{\r
545         (FinderPattern) possibleCenters.elementAt(0),\r
546         (FinderPattern) possibleCenters.elementAt(1),\r
547         (FinderPattern) possibleCenters.elementAt(2)\r
548     };\r
549   }\r
550 \r
551   /**\r
552    * <p>Orders by furthest from average</p>\r
553    */\r
554   private static class FurthestFromAverageComparator implements Comparator {\r
555     private final float average;\r
556     public FurthestFromAverageComparator(float f) {\r
557       average = f;\r
558     }\r
559     public int compare(Object center1, Object center2) {\r
560       float dA = Math.abs(((FinderPattern) center2).getEstimatedModuleSize() - average);\r
561       float dB = Math.abs(((FinderPattern) center1).getEstimatedModuleSize() - average);\r
562       return dA < dB ? -1 : (dA == dB ? 0 : 1);\r
563     }\r
564   }\r
565 \r
566   /**\r
567    * <p>Orders by {@link FinderPattern#getCount()}, descending.</p>\r
568    */\r
569   private static class CenterComparator implements Comparator {\r
570     private final float average;\r
571     public CenterComparator(float f) {\r
572       average = f;\r
573     }\r
574     public int compare(Object center1, Object center2) {\r
575       if (((FinderPattern) center2).getCount() != ((FinderPattern) center1).getCount()) {\r
576         return ((FinderPattern) center2).getCount() - ((FinderPattern) center1).getCount();\r
577       } else {\r
578         float dA = Math.abs(((FinderPattern) center2).getEstimatedModuleSize() - average);\r
579         float dB = Math.abs(((FinderPattern) center1).getEstimatedModuleSize() - average);\r
580         return dA < dB ? 1 : (dA == dB ? 0 : -1);\r
581       }\r
582     }\r
583   }\r
584 \r
585 }\r