Undo the ^= true thing in more files
[zxing.git] / core / src / com / google / zxing / oned / Code128Reader.java
1 /*
2  * Copyright 2008 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;
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
25 import java.util.Hashtable;
26
27 /**
28  * <p>Decodes Code 128 barcodes.</p>
29  *
30  * @author Sean Owen
31  */
32 public final class Code128Reader extends OneDReader {
33
34   private static final int[][] CODE_PATTERNS = {
35       {2, 1, 2, 2, 2, 2}, // 0
36       {2, 2, 2, 1, 2, 2},
37       {2, 2, 2, 2, 2, 1},
38       {1, 2, 1, 2, 2, 3},
39       {1, 2, 1, 3, 2, 2},
40       {1, 3, 1, 2, 2, 2}, // 5
41       {1, 2, 2, 2, 1, 3},
42       {1, 2, 2, 3, 1, 2},
43       {1, 3, 2, 2, 1, 2},
44       {2, 2, 1, 2, 1, 3},
45       {2, 2, 1, 3, 1, 2}, // 10
46       {2, 3, 1, 2, 1, 2},
47       {1, 1, 2, 2, 3, 2},
48       {1, 2, 2, 1, 3, 2},
49       {1, 2, 2, 2, 3, 1},
50       {1, 1, 3, 2, 2, 2}, // 15
51       {1, 2, 3, 1, 2, 2},
52       {1, 2, 3, 2, 2, 1},
53       {2, 2, 3, 2, 1, 1},
54       {2, 2, 1, 1, 3, 2},
55       {2, 2, 1, 2, 3, 1}, // 20
56       {2, 1, 3, 2, 1, 2},
57       {2, 2, 3, 1, 1, 2},
58       {3, 1, 2, 1, 3, 1},
59       {3, 1, 1, 2, 2, 2},
60       {3, 2, 1, 1, 2, 2}, // 25
61       {3, 2, 1, 2, 2, 1},
62       {3, 1, 2, 2, 1, 2},
63       {3, 2, 2, 1, 1, 2},
64       {3, 2, 2, 2, 1, 1},
65       {2, 1, 2, 1, 2, 3}, // 30
66       {2, 1, 2, 3, 2, 1},
67       {2, 3, 2, 1, 2, 1},
68       {1, 1, 1, 3, 2, 3},
69       {1, 3, 1, 1, 2, 3},
70       {1, 3, 1, 3, 2, 1}, // 35
71       {1, 1, 2, 3, 1, 3},
72       {1, 3, 2, 1, 1, 3},
73       {1, 3, 2, 3, 1, 1},
74       {2, 1, 1, 3, 1, 3},
75       {2, 3, 1, 1, 1, 3}, // 40
76       {2, 3, 1, 3, 1, 1},
77       {1, 1, 2, 1, 3, 3},
78       {1, 1, 2, 3, 3, 1},
79       {1, 3, 2, 1, 3, 1},
80       {1, 1, 3, 1, 2, 3}, // 45
81       {1, 1, 3, 3, 2, 1},
82       {1, 3, 3, 1, 2, 1},
83       {3, 1, 3, 1, 2, 1},
84       {2, 1, 1, 3, 3, 1},
85       {2, 3, 1, 1, 3, 1}, // 50
86       {2, 1, 3, 1, 1, 3},
87       {2, 1, 3, 3, 1, 1},
88       {2, 1, 3, 1, 3, 1},
89       {3, 1, 1, 1, 2, 3},
90       {3, 1, 1, 3, 2, 1}, // 55
91       {3, 3, 1, 1, 2, 1},
92       {3, 1, 2, 1, 1, 3},
93       {3, 1, 2, 3, 1, 1},
94       {3, 3, 2, 1, 1, 1},
95       {3, 1, 4, 1, 1, 1}, // 60
96       {2, 2, 1, 4, 1, 1},
97       {4, 3, 1, 1, 1, 1},
98       {1, 1, 1, 2, 2, 4},
99       {1, 1, 1, 4, 2, 2},
100       {1, 2, 1, 1, 2, 4}, // 65
101       {1, 2, 1, 4, 2, 1},
102       {1, 4, 1, 1, 2, 2},
103       {1, 4, 1, 2, 2, 1},
104       {1, 1, 2, 2, 1, 4},
105       {1, 1, 2, 4, 1, 2}, // 70
106       {1, 2, 2, 1, 1, 4},
107       {1, 2, 2, 4, 1, 1},
108       {1, 4, 2, 1, 1, 2},
109       {1, 4, 2, 2, 1, 1},
110       {2, 4, 1, 2, 1, 1}, // 75
111       {2, 2, 1, 1, 1, 4},
112       {4, 1, 3, 1, 1, 1},
113       {2, 4, 1, 1, 1, 2},
114       {1, 3, 4, 1, 1, 1},
115       {1, 1, 1, 2, 4, 2}, // 80
116       {1, 2, 1, 1, 4, 2},
117       {1, 2, 1, 2, 4, 1},
118       {1, 1, 4, 2, 1, 2},
119       {1, 2, 4, 1, 1, 2},
120       {1, 2, 4, 2, 1, 1}, // 85
121       {4, 1, 1, 2, 1, 2},
122       {4, 2, 1, 1, 1, 2},
123       {4, 2, 1, 2, 1, 1},
124       {2, 1, 2, 1, 4, 1},
125       {2, 1, 4, 1, 2, 1}, // 90
126       {4, 1, 2, 1, 2, 1},
127       {1, 1, 1, 1, 4, 3},
128       {1, 1, 1, 3, 4, 1},
129       {1, 3, 1, 1, 4, 1},
130       {1, 1, 4, 1, 1, 3}, // 95
131       {1, 1, 4, 3, 1, 1},
132       {4, 1, 1, 1, 1, 3},
133       {4, 1, 1, 3, 1, 1},
134       {1, 1, 3, 1, 4, 1},
135       {1, 1, 4, 1, 3, 1}, // 100
136       {3, 1, 1, 1, 4, 1},
137       {4, 1, 1, 1, 3, 1},
138       {2, 1, 1, 4, 1, 2},
139       {2, 1, 1, 2, 1, 4},
140       {2, 1, 1, 2, 3, 2}, // 105
141       {2, 3, 3, 1, 1, 1, 2}
142   };
143
144   private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f);
145   private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
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) {
183         counters[counterPosition]++;
184       } else {
185         if (counterPosition == patternLength - 1) {
186           int bestVariance = MAX_AVG_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                 MAX_INDIVIDUAL_VARIANCE);
191             if (variance < bestVariance) {
192               bestVariance = variance;
193               bestMatch = startCode;
194             }
195           }
196           if (bestMatch >= 0) {
197             // Look for whitespace before start pattern, >= 50% of width of start pattern
198             if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart,
199                 false)) {
200               return new int[]{patternStart, i, bestMatch};
201             }
202           }
203           patternStart += counters[0] + counters[1];
204           for (int y = 2; y < patternLength; y++) {
205             counters[y - 2] = counters[y];
206           }
207           counters[patternLength - 2] = 0;
208           counters[patternLength - 1] = 0;
209           counterPosition--;
210         } else {
211           counterPosition++;
212         }
213         counters[counterPosition] = 1;
214         isWhite = !isWhite;
215       }
216     }
217     throw ReaderException.getInstance();
218   }
219
220   private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
221     recordPattern(row, rowOffset, counters);
222     int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
223     int bestMatch = -1;
224     for (int d = 0; d < CODE_PATTERNS.length; d++) {
225       int[] pattern = CODE_PATTERNS[d];
226       int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
227       if (variance < bestVariance) {
228         bestVariance = variance;
229         bestMatch = d;
230       }
231     }
232     // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
233     if (bestMatch >= 0) {
234       return bestMatch;
235     } else {
236       throw ReaderException.getInstance();
237     }
238   }
239
240   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
241
242     int[] startPatternInfo = findStartPattern(row);
243     int startCode = startPatternInfo[2];
244     int codeSet;
245     switch (startCode) {
246       case CODE_START_A:
247         codeSet = CODE_CODE_A;
248         break;
249       case CODE_START_B:
250         codeSet = CODE_CODE_B;
251         break;
252       case CODE_START_C:
253         codeSet = CODE_CODE_C;
254         break;
255       default:
256         throw ReaderException.getInstance();
257     }
258
259     boolean done = false;
260     boolean isNextShifted = false;
261
262     StringBuffer result = new StringBuffer(20);
263     int lastStart = startPatternInfo[0];
264     int nextStart = startPatternInfo[1];
265     int[] counters = new int[6];
266
267     int lastCode = 0;
268     int code = 0;
269     int checksumTotal = startCode;
270     int multiplier = 0;
271     boolean lastCharacterWasPrintable = true;
272
273     while (!done) {
274
275       boolean unshift = isNextShifted;
276       isNextShifted = false;
277
278       // Save off last code
279       lastCode = code;
280
281       // Decode another code from image
282       code = decodeCode(row, counters, nextStart);
283
284       // Remember whether the last code was printable or not (excluding CODE_STOP)
285       if (code != CODE_STOP) {
286         lastCharacterWasPrintable = true;
287       }
288
289       // Add to checksum computation (if not CODE_STOP of course)
290       if (code != CODE_STOP) {
291         multiplier++;
292         checksumTotal += multiplier * code;
293       }
294
295       // Advance to where the next code will to start
296       lastStart = nextStart;
297       for (int i = 0; i < counters.length; i++) {
298         nextStart += counters[i];
299       }
300
301       // Take care of illegal start codes
302       switch (code) {
303         case CODE_START_A:
304         case CODE_START_B:
305         case CODE_START_C:
306           throw ReaderException.getInstance();
307       }
308
309       switch (codeSet) {
310
311         case CODE_CODE_A:
312           if (code < 64) {
313             result.append((char) (' ' + code));
314           } else if (code < 96) {
315             result.append((char) (code - 64));
316           } else {
317             // Don't let CODE_STOP, which always appears, affect whether whether we think the last
318             // code was printable or not.
319             if (code != CODE_STOP) {
320               lastCharacterWasPrintable = false;
321             }
322             switch (code) {
323               case CODE_FNC_1:
324               case CODE_FNC_2:
325               case CODE_FNC_3:
326               case CODE_FNC_4_A:
327                 // do nothing?
328                 break;
329               case CODE_SHIFT:
330                 isNextShifted = true;
331                 codeSet = CODE_CODE_B;
332                 break;
333               case CODE_CODE_B:
334                 codeSet = CODE_CODE_B;
335                 break;
336               case CODE_CODE_C:
337                 codeSet = CODE_CODE_C;
338                 break;
339               case CODE_STOP:
340                 done = true;
341                 break;
342             }
343           }
344           break;
345         case CODE_CODE_B:
346           if (code < 96) {
347             result.append((char) (' ' + code));
348           } else {
349             if (code != CODE_STOP) {
350               lastCharacterWasPrintable = false;
351             }
352             switch (code) {
353               case CODE_FNC_1:
354               case CODE_FNC_2:
355               case CODE_FNC_3:
356               case CODE_FNC_4_B:
357                 // do nothing?
358                 break;
359               case CODE_SHIFT:
360                 isNextShifted = true;
361                 codeSet = CODE_CODE_C;
362                 break;
363               case CODE_CODE_A:
364                 codeSet = CODE_CODE_A;
365                 break;
366               case CODE_CODE_C:
367                 codeSet = CODE_CODE_C;
368                 break;
369               case CODE_STOP:
370                 done = true;
371                 break;
372             }
373           }
374           break;
375         case CODE_CODE_C:
376           if (code < 100) {
377             if (code < 10) {
378               result.append('0');
379             }
380             result.append(code);
381           } else {
382             if (code != CODE_STOP) {
383               lastCharacterWasPrintable = false;
384             }
385             switch (code) {
386               case CODE_FNC_1:
387                 // do nothing?
388                 break;
389               case CODE_CODE_A:
390                 codeSet = CODE_CODE_A;
391                 break;
392               case CODE_CODE_B:
393                 codeSet = CODE_CODE_B;
394                 break;
395               case CODE_STOP:
396                 done = true;
397                 break;
398             }
399           }
400           break;
401       }
402
403       // Unshift back to another code set if we were shifted
404       if (unshift) {
405         switch (codeSet) {
406           case CODE_CODE_A:
407             codeSet = CODE_CODE_C;
408             break;
409           case CODE_CODE_B:
410             codeSet = CODE_CODE_A;
411             break;
412           case CODE_CODE_C:
413             codeSet = CODE_CODE_B;
414             break;
415         }
416       }
417
418     }
419
420     // Check for ample whitespace following pattern, but, to do this we first need to remember that
421     // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
422     // to read off. Would be slightly better to properly read. Here we just skip it:
423     int width = row.getSize();
424     while (nextStart < width && row.get(nextStart)) {
425       nextStart++;
426     }
427     if (!row.isRange(nextStart, Math.min(width, nextStart + (nextStart - lastStart) / 2),
428         false)) {
429       throw ReaderException.getInstance();
430     }
431
432     // Pull out from sum the value of the penultimate check code
433     checksumTotal -= multiplier * lastCode;
434     // lastCode is the checksum then:
435     if (checksumTotal % 103 != lastCode) {
436       throw ReaderException.getInstance();
437     }
438
439     // Need to pull out the check digits from string
440     int resultLength = result.length();
441     // Only bother if the result had at least one character, and if the checksum digit happened to
442     // be a printable character. If it was just interpreted as a control code, nothing to remove.
443     if (resultLength > 0 && lastCharacterWasPrintable) {
444       if (codeSet == CODE_CODE_C) {
445         result.delete(resultLength - 2, resultLength);
446       } else {
447         result.delete(resultLength - 1, resultLength);
448       }
449     }
450
451     String resultString = result.toString();
452
453     if (resultString.length() == 0) {
454       // Almost surely a false positive
455       throw ReaderException.getInstance();
456     }
457
458     float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
459     float right = (float) (nextStart + lastStart) / 2.0f;
460     return new Result(
461         resultString,
462         null,
463         new ResultPoint[]{
464             new ResultPoint(left, (float) rowNumber),
465             new ResultPoint(right, (float) rowNumber)},
466         BarcodeFormat.CODE_128);
467
468   }
469
470 }