Add Code 93 support. Update tests to reflect new (better) number of successes.
[zxing.git] / core / src / com / google / zxing / oned / rss / AbstractRSSReader.java
1 /*
2  * Copyright (C) 2010 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.oned.rss;
18
19 import com.google.zxing.NotFoundException;
20 import com.google.zxing.oned.OneDReader;
21
22 public abstract class AbstractRSSReader extends OneDReader {
23
24   private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.2f);
25   private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.4f);
26
27   private static final float MIN_FINDER_PATTERN_RATIO = 9.5f / 12.0f;
28   private static final float MAX_FINDER_PATTERN_RATIO = 12.5f / 14.0f;
29
30   protected final int[] decodeFinderCounters;
31   protected final int[] dataCharacterCounters;
32   protected final float[] oddRoundingErrors;
33   protected final float[] evenRoundingErrors;
34   protected final int[] oddCounts;
35   protected final int[] evenCounts;
36
37   protected AbstractRSSReader(){
38       decodeFinderCounters = new int[4];
39       dataCharacterCounters = new int[8];
40       oddRoundingErrors = new float[4];
41       evenRoundingErrors = new float[4];
42       oddCounts = new int[dataCharacterCounters.length / 2];
43       evenCounts = new int[dataCharacterCounters.length / 2];
44   }
45
46
47   protected static int parseFinderValue(int[] counters, int [][] finderPatterns) throws NotFoundException {
48     for (int value = 0; value < finderPatterns.length; value++) {
49       if (patternMatchVariance(counters, finderPatterns[value], MAX_INDIVIDUAL_VARIANCE) <
50           MAX_AVG_VARIANCE) {
51         return value;
52       }
53     }
54     throw NotFoundException.getNotFoundInstance();
55   }
56
57   protected static int count(int[] array) {
58     int count = 0;
59     for (int i = 0; i < array.length; i++) {
60       count += array[i];
61     }
62     return count;
63   }
64
65   protected static void increment(int[] array, float[] errors) {
66     int index = 0;
67     float biggestError = errors[0];
68     for (int i = 1; i < array.length; i++) {
69       if (errors[i] > biggestError) {
70         biggestError = errors[i];
71         index = i;
72       }
73     }
74     array[index]++;
75   }
76
77   protected static void decrement(int[] array, float[] errors) {
78     int index = 0;
79     float biggestError = errors[0];
80     for (int i = 1; i < array.length; i++) {
81       if (errors[i] < biggestError) {
82         biggestError = errors[i];
83         index = i;
84       }
85     }
86     array[index]--;
87   }
88
89   protected static boolean isFinderPattern(int[] counters) {
90     int firstTwoSum = counters[0] + counters[1];
91     int sum = firstTwoSum + counters[2] + counters[3];
92     float ratio = (float) firstTwoSum / (float) sum;
93     if (ratio >= MIN_FINDER_PATTERN_RATIO && ratio <= MAX_FINDER_PATTERN_RATIO) {
94       // passes ratio test in spec, but see if the counts are unreasonable
95       int minCounter = Integer.MAX_VALUE;
96       int maxCounter = Integer.MIN_VALUE;
97       for (int i = 0; i < counters.length; i++) {
98         int counter = counters[i];
99         if (counter > maxCounter) {
100           maxCounter = counter;
101         }
102         if (counter < minCounter) {
103           minCounter = counter;
104         }
105       }
106       return maxCounter < 10 * minCounter;
107     }
108     return false;
109   }
110 }