GreyscaleRotatedLuminanceSource: implemented getMatrix()
[zxing.git] / cpp / core / src / zxing / oned / Code128Reader.cpp
1 /*
2  *  Code128Reader.cpp
3  *  ZXing
4  *
5  *  Copyright 2010 ZXing authors All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include "Code128Reader.h"
21 #include <zxing/oned/OneDResultPoint.h>
22 #include <zxing/common/Array.h>
23 #include <zxing/ReaderException.h>
24 #include <math.h>
25 #include <string.h>
26 #include <sstream>
27
28 namespace zxing {
29         namespace oned {
30
31                 const int CODE_PATTERNS_LENGTH = 107;
32                 const int countersLength = 6;
33                 static const int CODE_PATTERNS[CODE_PATTERNS_LENGTH][countersLength] = {
34                         {2, 1, 2, 2, 2, 2}, /* 0 */
35                         {2, 2, 2, 1, 2, 2},
36                         {2, 2, 2, 2, 2, 1},
37                         {1, 2, 1, 2, 2, 3},
38                         {1, 2, 1, 3, 2, 2},
39                         {1, 3, 1, 2, 2, 2}, /* 5 */
40                         {1, 2, 2, 2, 1, 3},
41                         {1, 2, 2, 3, 1, 2},
42                         {1, 3, 2, 2, 1, 2},
43                         {2, 2, 1, 2, 1, 3},
44                         {2, 2, 1, 3, 1, 2}, /* 10 */
45                         {2, 3, 1, 2, 1, 2},
46                         {1, 1, 2, 2, 3, 2},
47                         {1, 2, 2, 1, 3, 2},
48                         {1, 2, 2, 2, 3, 1},
49                         {1, 1, 3, 2, 2, 2}, /* 15 */
50                         {1, 2, 3, 1, 2, 2},
51                         {1, 2, 3, 2, 2, 1},
52                         {2, 2, 3, 2, 1, 1},
53                         {2, 2, 1, 1, 3, 2},
54                         {2, 2, 1, 2, 3, 1}, /* 20 */
55                         {2, 1, 3, 2, 1, 2},
56                         {2, 2, 3, 1, 1, 2},
57                         {3, 1, 2, 1, 3, 1},
58                         {3, 1, 1, 2, 2, 2},
59                         {3, 2, 1, 1, 2, 2}, /* 25 */
60                         {3, 2, 1, 2, 2, 1},
61                         {3, 1, 2, 2, 1, 2},
62                         {3, 2, 2, 1, 1, 2},
63                         {3, 2, 2, 2, 1, 1},
64                         {2, 1, 2, 1, 2, 3}, /* 30 */
65                         {2, 1, 2, 3, 2, 1},
66                         {2, 3, 2, 1, 2, 1},
67                         {1, 1, 1, 3, 2, 3},
68                         {1, 3, 1, 1, 2, 3},
69                         {1, 3, 1, 3, 2, 1}, /* 35 */
70                         {1, 1, 2, 3, 1, 3},
71                         {1, 3, 2, 1, 1, 3},
72                         {1, 3, 2, 3, 1, 1},
73                         {2, 1, 1, 3, 1, 3},
74                         {2, 3, 1, 1, 1, 3}, /* 40 */
75                         {2, 3, 1, 3, 1, 1},
76                         {1, 1, 2, 1, 3, 3},
77                         {1, 1, 2, 3, 3, 1},
78                         {1, 3, 2, 1, 3, 1},
79                         {1, 1, 3, 1, 2, 3}, /* 45 */
80                         {1, 1, 3, 3, 2, 1},
81                         {1, 3, 3, 1, 2, 1},
82                         {3, 1, 3, 1, 2, 1},
83                         {2, 1, 1, 3, 3, 1},
84                         {2, 3, 1, 1, 3, 1}, /* 50 */
85                         {2, 1, 3, 1, 1, 3},
86                         {2, 1, 3, 3, 1, 1},
87                         {2, 1, 3, 1, 3, 1},
88                         {3, 1, 1, 1, 2, 3},
89                         {3, 1, 1, 3, 2, 1}, /* 55 */
90                         {3, 3, 1, 1, 2, 1},
91                         {3, 1, 2, 1, 1, 3},
92                         {3, 1, 2, 3, 1, 1},
93                         {3, 3, 2, 1, 1, 1},
94                         {3, 1, 4, 1, 1, 1}, /* 60 */
95                         {2, 2, 1, 4, 1, 1},
96                         {4, 3, 1, 1, 1, 1},
97                         {1, 1, 1, 2, 2, 4},
98                         {1, 1, 1, 4, 2, 2},
99                         {1, 2, 1, 1, 2, 4}, /* 65 */
100                         {1, 2, 1, 4, 2, 1},
101                         {1, 4, 1, 1, 2, 2},
102                         {1, 4, 1, 2, 2, 1},
103                         {1, 1, 2, 2, 1, 4},
104                         {1, 1, 2, 4, 1, 2}, /* 70 */
105                         {1, 2, 2, 1, 1, 4},
106                         {1, 2, 2, 4, 1, 1},
107                         {1, 4, 2, 1, 1, 2},
108                         {1, 4, 2, 2, 1, 1},
109                         {2, 4, 1, 2, 1, 1}, /* 75 */
110                         {2, 2, 1, 1, 1, 4},
111                         {4, 1, 3, 1, 1, 1},
112                         {2, 4, 1, 1, 1, 2},
113                         {1, 3, 4, 1, 1, 1},
114                         {1, 1, 1, 2, 4, 2}, /* 80 */
115                         {1, 2, 1, 1, 4, 2},
116                         {1, 2, 1, 2, 4, 1},
117                         {1, 1, 4, 2, 1, 2},
118                         {1, 2, 4, 1, 1, 2},
119                         {1, 2, 4, 2, 1, 1}, /* 85 */
120                         {4, 1, 1, 2, 1, 2},
121                         {4, 2, 1, 1, 1, 2},
122                         {4, 2, 1, 2, 1, 1},
123                         {2, 1, 2, 1, 4, 1},
124                         {2, 1, 4, 1, 2, 1}, /* 90 */
125                         {4, 1, 2, 1, 2, 1},
126                         {1, 1, 1, 1, 4, 3},
127                         {1, 1, 1, 3, 4, 1},
128                         {1, 3, 1, 1, 4, 1},
129                         {1, 1, 4, 1, 1, 3}, /* 95 */
130                         {1, 1, 4, 3, 1, 1},
131                         {4, 1, 1, 1, 1, 3},
132                         {4, 1, 1, 3, 1, 1},
133                         {1, 1, 3, 1, 4, 1},
134                         {1, 1, 4, 1, 3, 1}, /* 100 */
135                         {3, 1, 1, 1, 4, 1},
136                         {4, 1, 1, 1, 3, 1},
137                         {2, 1, 1, 4, 1, 2},
138                         {2, 1, 1, 2, 1, 4},
139                         {2, 1, 1, 2, 3, 2}, /* 105 */
140                         {2, 3, 3, 1, 1, 1}
141                 };
142
143
144                 Code128Reader::Code128Reader(){
145                 }
146
147                 int* Code128Reader::findStartPattern(Ref<BitArray> row){
148                         int width = row->getSize();
149                         int rowOffset = 0;
150                         while (rowOffset < width) {
151                                 if (row->get(rowOffset)) {
152                                         break;
153                                 }
154                                 rowOffset++;
155                         }
156
157                         int counterPosition = 0;
158                         int counters[countersLength] = {0,0,0,0,0,0};
159                         int patternStart = rowOffset;
160                         bool isWhite = false;
161                         int patternLength =  sizeof(counters) / sizeof(int);
162
163                         for (int i = rowOffset; i < width; i++) {
164                                 bool pixel = row->get(i);
165                                 if (pixel ^ isWhite) {
166                                         counters[counterPosition]++;
167                                 } else {
168                                         if (counterPosition == patternLength - 1) {
169                                                 unsigned int bestVariance = MAX_AVG_VARIANCE;
170                                                 int bestMatch = -1;
171                                                 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
172                                                         unsigned int variance = patternMatchVariance(counters, sizeof(counters) / sizeof(int),
173                                                             CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
174                                                         if (variance < bestVariance) {
175                                                                 bestVariance = variance;
176                                                                 bestMatch = startCode;
177                                                         }
178                                                 }
179                                                 if (bestMatch >= 0) {
180                                                         // Look for whitespace before start pattern, >= 50% of width of start pattern
181                                                         if (row->isRange(fmaxl(0, patternStart - (i - patternStart) / 2), patternStart,
182                                                             false)) {
183                                                                 int* resultValue = new int[3];
184                                                                 resultValue[0] = patternStart;
185                                                                 resultValue[1] = i;
186                                                                 resultValue[2] = bestMatch;
187                                                                 return resultValue;
188                                                         }
189                                                 }
190                                                 patternStart += counters[0] + counters[1];
191                                                 for (int y = 2; y < patternLength; y++) {
192                                                         counters[y - 2] = counters[y];
193                                                 }
194                                                 counters[patternLength - 2] = 0;
195                                                 counters[patternLength - 1] = 0;
196                                                 counterPosition--;
197                                         } else {
198                                                 counterPosition++;
199                                         }
200                                         counters[counterPosition] = 1;
201                                         isWhite = !isWhite;
202                                 }
203                         }
204                         throw ReaderException("");
205                 }
206
207                 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount,
208                     int rowOffset) {
209                   if (!recordPattern(row, rowOffset, counters, countersCount)) {
210                     throw ReaderException("");
211                   }
212                         unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
213                         int bestMatch = -1;
214                         for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
215                                 int pattern[countersLength];
216
217                                 for(int ind = 0; ind< countersLength; ind++){
218                                         pattern[ind] = CODE_PATTERNS[d][ind];
219                                 }
220 //                              memcpy(pattern, CODE_PATTERNS[d], countersLength);
221                                 unsigned int variance = patternMatchVariance(counters, countersCount, pattern,
222                                     MAX_INDIVIDUAL_VARIANCE);
223                                 if (variance < bestVariance) {
224                                         bestVariance = variance;
225                                         bestMatch = d;
226                                 }
227                         }
228                         // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
229                         if (bestMatch >= 0) {
230                                 return bestMatch;
231                         } else {
232                                 throw ReaderException("");
233                         }
234                 }
235
236                 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row) {
237                   int* startPatternInfo = NULL;
238                   try {
239         startPatternInfo = findStartPattern(row);
240         int startCode = startPatternInfo[2];
241         int codeSet;
242         switch (startCode) {
243           case CODE_START_A:
244             codeSet = CODE_CODE_A;
245             break;
246           case CODE_START_B:
247             codeSet = CODE_CODE_B;
248             break;
249           case CODE_START_C:
250             codeSet = CODE_CODE_C;
251             break;
252           default:
253             throw ReaderException("");
254         }
255
256         bool done = false;
257         bool isNextShifted = false;
258
259         std::string tmpResultString;
260         std::stringstream tmpResultSStr; // used if its Code 128C
261
262         int lastStart = startPatternInfo[0];
263         int nextStart = startPatternInfo[1];
264         int counters[countersLength] = {0,0,0,0,0,0};
265
266         int lastCode = 0;
267         int code = 0;
268         int checksumTotal = startCode;
269         int multiplier = 0;
270         bool lastCharacterWasPrintable = true;
271
272         while (!done) {
273           bool unshift = isNextShifted;
274           isNextShifted = false;
275
276           // Save off last code
277           lastCode = code;
278
279           // Decode another code from image
280           try {
281             code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
282           } catch (ReaderException re) {
283             throw re;
284           }
285
286           // Remember whether the last code was printable or not (excluding CODE_STOP)
287           if (code != CODE_STOP) {
288             lastCharacterWasPrintable = true;
289           }
290
291           // Add to checksum computation (if not CODE_STOP of course)
292           if (code != CODE_STOP) {
293             multiplier++;
294             checksumTotal += multiplier * code;
295           }
296
297           // Advance to where the next code will to start
298           lastStart = nextStart;
299           int _countersLength = sizeof(counters) / sizeof(int);
300           for (int i = 0; i < _countersLength; i++) {
301             nextStart += counters[i];
302           }
303
304           // Take care of illegal start codes
305           switch (code) {
306             case CODE_START_A:
307             case CODE_START_B:
308             case CODE_START_C:
309               throw ReaderException("");
310           }
311
312           switch (codeSet) {
313
314             case CODE_CODE_A:
315               if (code < 64) {
316                 tmpResultString.append(1, (char) (' ' + code));
317               } else if (code < 96) {
318                 tmpResultString.append(1, (char) (code - 64));
319               } else {
320                 // Don't let CODE_STOP, which always appears, affect whether whether we think the
321                 // last code was printable or not.
322                 if (code != CODE_STOP) {
323                   lastCharacterWasPrintable = false;
324                 }
325                 switch (code) {
326                   case CODE_FNC_1:
327                   case CODE_FNC_2:
328                   case CODE_FNC_3:
329                   case CODE_FNC_4_A:
330                     // do nothing?
331                     break;
332                   case CODE_SHIFT:
333                     isNextShifted = true;
334                     codeSet = CODE_CODE_B;
335                     break;
336                   case CODE_CODE_B:
337                     codeSet = CODE_CODE_B;
338                     break;
339                   case CODE_CODE_C:
340                     codeSet = CODE_CODE_C;
341                     break;
342                   case CODE_STOP:
343                     done = true;
344                     break;
345                 }
346               }
347               break;
348             case CODE_CODE_B:
349               if (code < 96) {
350                 tmpResultString.append(1, (char) (' ' + code));
351               } else {
352                 if (code != CODE_STOP) {
353                   lastCharacterWasPrintable = false;
354                 }
355                 switch (code) {
356                   case CODE_FNC_1:
357                   case CODE_FNC_2:
358                   case CODE_FNC_3:
359                   case CODE_FNC_4_B:
360                     // do nothing?
361                     break;
362                   case CODE_SHIFT:
363                     isNextShifted = true;
364                     codeSet = CODE_CODE_C;
365                     break;
366                   case CODE_CODE_A:
367                     codeSet = CODE_CODE_A;
368                     break;
369                   case CODE_CODE_C:
370                     codeSet = CODE_CODE_C;
371                     break;
372                   case CODE_STOP:
373                     done = true;
374                     break;
375                 }
376               }
377               break;
378             case CODE_CODE_C:
379             // the code read in this case is the number encoded directly
380               if (code < 100) {
381                 if (code < 10)
382                 tmpResultSStr << '0';
383               tmpResultSStr << code;
384               } else {
385                 if (code != CODE_STOP) {
386                   lastCharacterWasPrintable = false;
387                 }
388                 switch (code) {
389                   case CODE_FNC_1:
390                     // do nothing?
391                     break;
392                   case CODE_CODE_A:
393                     codeSet = CODE_CODE_A;
394                     break;
395                   case CODE_CODE_B:
396                     codeSet = CODE_CODE_B;
397                     break;
398                   case CODE_STOP:
399                     done = true;
400                     break;
401                 }
402               }
403               break;
404           }
405
406           // Unshift back to another code set if we were shifted
407           if (unshift) {
408             switch (codeSet) {
409               case CODE_CODE_A:
410                 codeSet = CODE_CODE_C;
411                 break;
412               case CODE_CODE_B:
413                 codeSet = CODE_CODE_A;
414                 break;
415               case CODE_CODE_C:
416                 codeSet = CODE_CODE_B;
417                 break;
418             }
419           }
420
421         }
422
423         // Check for ample whitespace following pattern, but, to do this we first need to remember that
424         // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
425         // to read off. Would be slightly better to properly read. Here we just skip it:
426         int width = row->getSize();
427         while (nextStart < width && row->get(nextStart)) {
428           nextStart++;
429         }
430         if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
431           throw ReaderException("");
432         }
433
434         // Pull out from sum the value of the penultimate check code
435         checksumTotal -= multiplier * lastCode;
436         // lastCode is the checksum then:
437         if (checksumTotal % 103 != lastCode) {
438           throw ReaderException("");
439         }
440
441         if (codeSet == CODE_CODE_C)
442           tmpResultString.append(tmpResultSStr.str());
443
444         // Need to pull out the check digits from string
445         int resultLength = tmpResultString.length();
446         // Only bother if the result had at least one character, and if the checksum digit happened to
447         // be a printable character. If it was just interpreted as a control code, nothing to remove.
448         if (resultLength > 0 && lastCharacterWasPrintable) {
449           if (codeSet == CODE_CODE_C) {
450             tmpResultString.erase(resultLength - 2, resultLength);
451           } else {
452             tmpResultString.erase(resultLength - 1, resultLength);
453           }
454         }
455
456         Ref<String> resultString(new String(tmpResultString));
457         if (tmpResultString.length() == 0) {
458           // Almost surely a false positive
459           throw ReaderException("");
460         }
461
462         float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
463         float right = (float) (nextStart + lastStart) / 2.0f;
464
465         std::vector< Ref<ResultPoint> > resultPoints(2);
466         Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
467         Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
468         resultPoints[0] = resultPoint1;
469         resultPoints[1] = resultPoint2;
470
471         delete [] startPatternInfo;
472         ArrayRef<unsigned char> resultBytes(1);
473         return Ref<Result>(new Result(resultString, resultBytes, resultPoints,
474             BarcodeFormat_CODE_128));
475                         } catch (ReaderException const& re) {
476                           delete [] startPatternInfo;
477                           return Ref<Result>();
478                         }
479                 }
480
481                 void Code128Reader::append(char* s, char c){
482                         int len = strlen(s);
483                         s[len] = c;
484                         s[len + 1] = '\0';
485                 }
486
487                 Code128Reader::~Code128Reader(){
488                 }
489         }
490 }