Now use integer math in patternMatchVariance for performance, plus a few other simila...
[zxing.git] / core / src / com / google / zxing / oned / Code128Reader.java
1 /*
2  * Copyright 2008 Google Inc.
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;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.Result;
22 import com.google.zxing.ResultPoint;
23 import com.google.zxing.common.BitArray;
24 import com.google.zxing.common.GenericResultPoint;
25
26 import java.util.Hashtable;
27
28 /**
29  * <p>Decodes Code 128 barcodes.</p>
30  *
31  * @author srowen@google.com (Sean Owen)
32  */
33 public final class Code128Reader extends AbstractOneDReader {
34
35   private static final int[][] CODE_PATTERNS = {
36       {2, 1, 2, 2, 2, 2}, // 0
37       {2, 2, 2, 1, 2, 2},
38       {2, 2, 2, 2, 2, 1},
39       {1, 2, 1, 2, 2, 3},
40       {1, 2, 1, 3, 2, 2},
41       {1, 3, 1, 2, 2, 2}, // 5
42       {1, 2, 2, 2, 1, 3},
43       {1, 2, 2, 3, 1, 2},
44       {1, 3, 2, 2, 1, 2},
45       {2, 2, 1, 2, 1, 3},
46       {2, 2, 1, 3, 1, 2}, // 10
47       {2, 3, 1, 2, 1, 2},
48       {1, 1, 2, 2, 3, 2},
49       {1, 2, 2, 1, 3, 2},
50       {1, 2, 2, 2, 3, 1},
51       {1, 1, 3, 2, 2, 2}, // 15
52       {1, 2, 3, 1, 2, 2},
53       {1, 2, 3, 2, 2, 1},
54       {2, 2, 3, 2, 1, 1},
55       {2, 2, 1, 1, 3, 2},
56       {2, 2, 1, 2, 3, 1}, // 20
57       {2, 1, 3, 2, 1, 2},
58       {2, 2, 3, 1, 1, 2},
59       {3, 1, 2, 1, 3, 1},
60       {3, 1, 1, 2, 2, 2},
61       {3, 2, 1, 1, 2, 2}, // 25
62       {3, 2, 1, 2, 2, 1},
63       {3, 1, 2, 2, 1, 2},
64       {3, 2, 2, 1, 1, 2},
65       {3, 2, 2, 2, 1, 1},
66       {2, 1, 2, 1, 2, 3}, // 30
67       {2, 1, 2, 3, 2, 1},
68       {2, 3, 2, 1, 2, 1},
69       {1, 1, 1, 3, 2, 3},
70       {1, 3, 1, 1, 2, 3},
71       {1, 3, 1, 3, 2, 1}, // 35
72       {1, 1, 2, 3, 1, 3},
73       {1, 3, 2, 1, 1, 3},
74       {1, 3, 2, 3, 1, 1},
75       {2, 1, 1, 3, 1, 3},
76       {2, 3, 1, 1, 1, 3}, // 40
77       {2, 3, 1, 3, 1, 1},
78       {1, 1, 2, 1, 3, 3},
79       {1, 1, 2, 3, 3, 1},
80       {1, 3, 2, 1, 3, 1},
81       {1, 1, 3, 1, 2, 3}, // 45
82       {1, 1, 3, 3, 2, 1},
83       {1, 3, 3, 1, 2, 1},
84       {3, 1, 3, 1, 2, 1},
85       {2, 1, 1, 3, 3, 1},
86       {2, 3, 1, 1, 3, 1}, // 50
87       {2, 1, 3, 1, 1, 3},
88       {2, 1, 3, 3, 1, 1},
89       {2, 1, 3, 1, 3, 1},
90       {3, 1, 1, 1, 2, 3},
91       {3, 1, 1, 3, 2, 1}, // 55
92       {3, 3, 1, 1, 2, 1},
93       {3, 1, 2, 1, 1, 3},
94       {3, 1, 2, 3, 1, 1},
95       {3, 3, 2, 1, 1, 1},
96       {3, 1, 4, 1, 1, 1}, // 60
97       {2, 2, 1, 4, 1, 1},
98       {4, 3, 1, 1, 1, 1},
99       {1, 1, 1, 2, 2, 4},
100       {1, 1, 1, 4, 2, 2},
101       {1, 2, 1, 1, 2, 4}, // 65
102       {1, 2, 1, 4, 2, 1},
103       {1, 4, 1, 1, 2, 2},
104       {1, 4, 1, 2, 2, 1},
105       {1, 1, 2, 2, 1, 4},
106       {1, 1, 2, 4, 1, 2}, // 70
107       {1, 2, 2, 1, 1, 4},
108       {1, 2, 2, 4, 1, 1},
109       {1, 4, 2, 1, 1, 2},
110       {1, 4, 2, 2, 1, 1},
111       {2, 4, 1, 2, 1, 1}, // 75
112       {2, 2, 1, 1, 1, 4},
113       {4, 1, 3, 1, 1, 1},
114       {2, 4, 1, 1, 1, 2},
115       {1, 3, 4, 1, 1, 1},
116       {1, 1, 1, 2, 4, 2}, // 80
117       {1, 2, 1, 1, 4, 2},
118       {1, 2, 1, 2, 4, 1},
119       {1, 1, 4, 2, 1, 2},
120       {1, 2, 4, 1, 1, 2},
121       {1, 2, 4, 2, 1, 1}, // 85
122       {4, 1, 1, 2, 1, 2},
123       {4, 2, 1, 1, 1, 2},
124       {4, 2, 1, 2, 1, 1},
125       {2, 1, 2, 1, 4, 1},
126       {2, 1, 4, 1, 2, 1}, // 90
127       {4, 1, 2, 1, 2, 1},
128       {1, 1, 1, 1, 4, 3},
129       {1, 1, 1, 3, 4, 1},
130       {1, 3, 1, 1, 4, 1},
131       {1, 1, 4, 1, 1, 3}, // 95
132       {1, 1, 4, 3, 1, 1},
133       {4, 1, 1, 1, 1, 3},
134       {4, 1, 1, 3, 1, 1},
135       {1, 1, 3, 1, 4, 1},
136       {1, 1, 4, 1, 3, 1}, // 100
137       {3, 1, 1, 1, 4, 1},
138       {4, 1, 1, 1, 3, 1},
139       {2, 1, 1, 4, 1, 2},
140       {2, 1, 1, 2, 1, 4},
141       {2, 1, 1, 2, 3, 2}, // 105
142       {2, 3, 3, 1, 1, 1, 2}
143   };
144
145   private static final int MAX_VARIANCE = 56;
146
147   private static final int CODE_SHIFT = 98;
148
149   private static final int CODE_CODE_C = 99;
150   private static final int CODE_CODE_B = 100;
151   private static final int CODE_CODE_A = 101;
152
153   private static final int CODE_FNC_1 = 102;
154   private static final int CODE_FNC_2 = 97;
155   private static final int CODE_FNC_3 = 96;
156   private static final int CODE_FNC_4_A = 101;
157   private static final int CODE_FNC_4_B = 100;
158
159   private static final int CODE_START_A = 103;
160   private static final int CODE_START_B = 104;
161   private static final int CODE_START_C = 105;
162   private static final int CODE_STOP = 106;
163
164   private static int[] findStartPattern(BitArray row) throws ReaderException {
165     int width = row.getSize();
166     int rowOffset = 0;
167     while (rowOffset < width) {
168       if (row.get(rowOffset)) {
169         break;
170       }
171       rowOffset++;
172     }
173
174     int counterPosition = 0;
175     int[] counters = new int[6];
176     int patternStart = rowOffset;
177     boolean isWhite = false;
178     int patternLength = counters.length;
179
180     for (int i = rowOffset; i < width; i++) {
181       boolean pixel = row.get(i);
182       if ((!pixel && isWhite) || (pixel && !isWhite)) {
183         counters[counterPosition]++;
184       } else {
185         if (counterPosition == patternLength - 1) {
186           int bestVariance = MAX_VARIANCE;
187           int bestMatch = -1;
188           for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
189             int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode]);
190             if (variance < bestVariance) {
191               bestVariance = variance;
192               bestMatch = startCode;
193             }
194           }
195           if (bestMatch >= 0) {
196             return new int[]{patternStart, i, bestMatch};
197           }
198           patternStart += counters[0] + counters[1];
199           for (int y = 2; y < patternLength; y++) {
200             counters[y - 2] = counters[y];
201           }
202           counters[patternLength - 2] = 0;
203           counters[patternLength - 1] = 0;
204           counterPosition--;
205         } else {
206           counterPosition++;
207         }
208         counters[counterPosition] = 1;
209         isWhite = !isWhite;
210       }
211     }
212     throw new ReaderException("Can't find pattern");
213   }
214
215   private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
216     recordPattern(row, rowOffset, counters);
217     int bestVariance = MAX_VARIANCE; // worst variance we'll accept
218     int bestMatch = -1;
219     for (int d = 0; d < CODE_PATTERNS.length; d++) {
220       int[] pattern = CODE_PATTERNS[d];
221       int variance = patternMatchVariance(counters, pattern);
222       if (variance < bestVariance) {
223         bestVariance = variance;
224         bestMatch = d;
225       }
226     }
227     // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6
228     if (bestMatch >= 0) {
229       return bestMatch;
230     } else {
231       throw new ReaderException("Could not match any code pattern");
232     }
233   }
234
235   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
236
237     int[] startPatternInfo = findStartPattern(row);
238     int startCode = startPatternInfo[2];
239     int codeSet;
240     switch (startCode) {
241       case CODE_START_A:
242         codeSet = CODE_CODE_A;
243         break;
244       case CODE_START_B:
245         codeSet = CODE_CODE_B;
246         break;
247       case CODE_START_C:
248         codeSet = CODE_CODE_C;
249         break;
250       default:
251         throw new ReaderException("Illegal start code");
252     }
253
254     boolean done = false;
255     boolean isNextShifted = false;
256
257     StringBuffer result = new StringBuffer();
258     int lastStart = startPatternInfo[0];
259     int nextStart = startPatternInfo[1];
260     int[] counters = new int[6];
261
262     int lastCode = 0;
263     int code = 0;
264     int checksumTotal = startCode;
265     int multiplier = 0;
266
267     while (!done) {
268
269       boolean unshift = isNextShifted;
270       isNextShifted = false;
271
272       lastCode = code;
273       code = decodeCode(row, counters, nextStart);
274       if (code != CODE_STOP) {
275         multiplier++;
276         checksumTotal += multiplier * code;
277       }
278
279       lastStart = nextStart;
280       for (int i = 0; i < counters.length; i++) {
281         nextStart += counters[i];
282       }
283
284       // Take care of illegal start codes
285       switch (code) {
286         case CODE_START_A:
287         case CODE_START_B:
288         case CODE_START_C:
289           throw new ReaderException("Unexpected start code");
290       }
291
292       switch (codeSet) {
293
294         case CODE_CODE_A:
295           if (code < 64) {
296             result.append((char) (' ' + code));
297           } else if (code < 96) {
298             result.append((char) (code - 64));
299           } else {
300             switch (code) {
301               case CODE_FNC_1:
302               case CODE_FNC_2:
303               case CODE_FNC_3:
304               case CODE_FNC_4_A:
305                 // do nothing?
306                 break;
307               case CODE_SHIFT:
308                 isNextShifted = true;
309                 codeSet = CODE_CODE_B;
310                 break;
311               case CODE_CODE_B:
312                 codeSet = CODE_CODE_B;
313                 break;
314               case CODE_CODE_C:
315                 codeSet = CODE_CODE_C;
316                 break;
317               case CODE_STOP:
318                 done = true;
319                 break;
320             }
321           }
322           break;
323         case CODE_CODE_B:
324           if (code < 96) {
325             result.append((char) (' ' + code));
326           } else {
327             switch (code) {
328               case CODE_FNC_1:
329               case CODE_FNC_2:
330               case CODE_FNC_3:
331               case CODE_FNC_4_B:
332                 // do nothing?
333                 break;
334               case CODE_SHIFT:
335                 isNextShifted = true;
336                 codeSet = CODE_CODE_C;
337                 break;
338               case CODE_CODE_A:
339                 codeSet = CODE_CODE_A;
340                 break;
341               case CODE_CODE_C:
342                 codeSet = CODE_CODE_C;
343                 break;
344               case CODE_STOP:
345                 done = true;
346                 break;
347             }
348           }
349           break;
350         case CODE_CODE_C:
351           if (code < 100) {
352             if (code < 10) {
353               result.append('0');
354             }
355             result.append(code);
356           } else {
357             switch (code) {
358               case CODE_FNC_1:
359                 // do nothing?
360                 break;
361               case CODE_CODE_A:
362                 codeSet = CODE_CODE_A;
363                 break;
364               case CODE_CODE_B:
365                 codeSet = CODE_CODE_B;
366                 break;
367               case CODE_STOP:
368                 done = true;
369                 break;
370             }
371           }
372           break;
373       }
374
375       if (unshift) {
376         switch (codeSet) {
377           case CODE_CODE_A:
378             codeSet = CODE_CODE_C;
379             break;
380           case CODE_CODE_B:
381             codeSet = CODE_CODE_A;
382             break;
383           case CODE_CODE_C:
384             codeSet = CODE_CODE_B;
385             break;
386         }
387       }
388
389     }
390
391     // Pull out from sum the value of the penultimate check code
392     checksumTotal -= multiplier * lastCode;
393     if (checksumTotal % 103 != lastCode) {
394       throw new ReaderException("Checksum failed");
395     }
396
397     // Need to pull out the check digits from string
398     int resultLength = result.length();
399     if (resultLength > 0) {
400       if (codeSet == CODE_CODE_C) {
401         result.delete(resultLength - 2, resultLength);
402       } else {
403         result.delete(resultLength - 1, resultLength);
404       }
405     }
406
407     String resultString = result.toString();
408     float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
409     float right = (float) (nextStart + lastStart) / 2.0f;
410     return new Result(
411         resultString,
412         null,
413         new ResultPoint[]{
414             new GenericResultPoint(left, (float) rowNumber),
415             new GenericResultPoint(right, (float) rowNumber)},
416         BarcodeFormat.CODE_128);
417
418   }
419
420 }