Small style stuff
[zxing.git] / symbian / QQrDecoder / zxing / oned / Code128Reader.cpp
1 /*
2  *  Code128Reader.cpp
3  *  ZXing
4  *
5  *  Created by Lukasz Warchol on 10-01-15.
6  *  Copyright 2010 ZXing authors All rights reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include "Code128Reader.h"
22 #include <zxing/oned/OneDResultPoint.h>
23 #include <zxing/common/Array.h>
24 #include <zxing/ReaderException.h>
25 #include <math.h>
26 #include <string.h>
27 #include <sstream>
28
29 namespace zxing {
30         namespace oned {
31                 
32                 const int CODE_PATTERNS_LENGTH = 107;
33                 const int countersLength = 6;
34                 static const int CODE_PATTERNS[CODE_PATTERNS_LENGTH][countersLength] = {
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}
142                 };
143                 
144                 
145                 Code128Reader::Code128Reader(){
146                 }
147                 
148                 int* Code128Reader::findStartPattern(Ref<BitArray> row){
149                         int width = row->getSize();
150                         int rowOffset = 0;
151                         while (rowOffset < width) {
152                                 if (row->get(rowOffset)) {
153                                         break;
154                                 }
155                                 rowOffset++;
156                         }
157                         
158                         int counterPosition = 0;
159                         int counters[countersLength] = {0,0,0,0,0,0};
160                         int patternStart = rowOffset;
161                         bool isWhite = false;
162                         int patternLength =  sizeof(counters) / sizeof(int);
163                         
164                         for (int i = rowOffset; i < width; i++) {
165                                 bool pixel = row->get(i);
166                                 if (pixel ^ isWhite) {
167                                         counters[counterPosition]++;
168                                 } else {
169                                         if (counterPosition == patternLength - 1) {
170                                                 unsigned int bestVariance = MAX_AVG_VARIANCE;
171                                                 int bestMatch = -1;
172                                                 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
173                                                         unsigned int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), 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, false)) {
182                                                                 int* resultValue = new int[3];
183                                                                 resultValue[0] = patternStart;
184                                                                 resultValue[1] = i;
185                                                                 resultValue[2] = bestMatch;
186                                                                 return resultValue;
187                                                         }
188                                                 }
189                                                 patternStart += counters[0] + counters[1];
190                                                 for (int y = 2; y < patternLength; y++) {
191                                                         counters[y - 2] = counters[y];
192                                                 }
193                                                 counters[patternLength - 2] = 0;
194                                                 counters[patternLength - 1] = 0;
195                                                 counterPosition--;
196                                         } else {
197                                                 counterPosition++;
198                                         }
199                                         counters[counterPosition] = 1;
200                                         isWhite = !isWhite;
201                                 }
202                         }
203                         throw ReaderException("");
204                 }
205                 
206                 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){
207                         recordPattern(row, rowOffset, counters, countersCount);
208                         unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
209                         int bestMatch = -1;
210                         for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
211                                 int pattern[countersLength];
212                                 
213                                 for(int ind = 0; ind< countersLength; ind++){
214                                         pattern[ind] = CODE_PATTERNS[d][ind];
215                                 }
216 //                              memcpy(pattern, CODE_PATTERNS[d], countersLength);
217                                 unsigned int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE);
218                                 if (variance < bestVariance) {
219                                         bestVariance = variance;
220                                         bestMatch = d;
221                                 }
222                         }
223                         // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
224                         if (bestMatch >= 0) {
225                                 return bestMatch;
226                         } else {
227                                 throw ReaderException("");
228                         }
229                 }
230                 
231                 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row){
232                         
233                         int* startPatternInfo = findStartPattern(row);
234                         int startCode = startPatternInfo[2];
235                         int codeSet;
236                         switch (startCode) {
237                                 case CODE_START_A:
238                                         codeSet = CODE_CODE_A;
239                                         break;
240                                 case CODE_START_B:
241                                         codeSet = CODE_CODE_B;
242                                         break;
243                                 case CODE_START_C:
244                                         codeSet = CODE_CODE_C;
245                                         break;
246                                 default:
247                                         delete [] startPatternInfo;
248                                         throw ReaderException("");
249                         }
250                         
251                         bool done = false;
252                         bool isNextShifted = false;
253                         
254                         std::string tmpResultString;
255                         std::stringstream tmpResultSStr; // used if its Code 128C
256                         
257                         int lastStart = startPatternInfo[0];
258                         int nextStart = startPatternInfo[1];
259                         int counters[countersLength] = {0,0,0,0,0,0};
260                         
261                         int lastCode = 0;
262                         int code = 0;
263                         int checksumTotal = startCode;
264                         int multiplier = 0;
265                         bool lastCharacterWasPrintable = true;
266                         
267                         while (!done) {
268                                 
269                                 bool unshift = isNextShifted;
270                                 isNextShifted = false;
271                                 
272                                 // Save off last code
273                                 lastCode = code;
274                                 
275                                 // Decode another code from image
276                                 try {
277                                 code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
278                                 } catch (ReaderException re) {
279                                         delete [] startPatternInfo;
280                                         throw re;
281                                 }
282                                 
283                                 // Remember whether the last code was printable or not (excluding CODE_STOP)
284                                 if (code != CODE_STOP) {
285                                         lastCharacterWasPrintable = true;
286                                 }
287                                 
288                                 // Add to checksum computation (if not CODE_STOP of course)
289                                 if (code != CODE_STOP) {
290                                         multiplier++;
291                                         checksumTotal += multiplier * code;
292                                 }
293                                 
294                                 // Advance to where the next code will to start
295                                 lastStart = nextStart;
296                                 int _countersLength = sizeof(counters) / sizeof(int);
297                                 for (int i = 0; i < _countersLength; 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                                                 delete [] startPatternInfo;
307                                                 throw ReaderException("");
308                                 }
309                                 
310                                 switch (codeSet) {
311                                                 
312                                         case CODE_CODE_A:
313                                                 if (code < 64) {
314                                                         tmpResultString.append(1, (char) (' ' + code));
315                                                 } else if (code < 96) {
316                                                         tmpResultString.append(1, (char) (code - 64));
317                                                 } else {
318                                                         // Don't let CODE_STOP, which always appears, affect whether whether we think the last
319                                                         // code was printable or not.
320                                                         if (code != CODE_STOP) {
321                                                                 lastCharacterWasPrintable = false;
322                                                         }
323                                                         switch (code) {
324                                                                 case CODE_FNC_1:
325                                                                 case CODE_FNC_2:
326                                                                 case CODE_FNC_3:
327                                                                 case CODE_FNC_4_A:
328                                                                         // do nothing?
329                                                                         break;
330                                                                 case CODE_SHIFT:
331                                                                         isNextShifted = true;
332                                                                         codeSet = CODE_CODE_B;
333                                                                         break;
334                                                                 case CODE_CODE_B:
335                                                                         codeSet = CODE_CODE_B;
336                                                                         break;
337                                                                 case CODE_CODE_C:
338                                                                         codeSet = CODE_CODE_C;
339                                                                         break;
340                                                                 case CODE_STOP:
341                                                                         done = true;
342                                                                         break;
343                                                         }
344                                                 }
345                                                 break;
346                                         case CODE_CODE_B:
347                                                 if (code < 96) {
348                                                         tmpResultString.append(1, (char) (' ' + code));
349                                                 } else {
350                                                         if (code != CODE_STOP) {
351                                                                 lastCharacterWasPrintable = false;
352                                                         }
353                                                         switch (code) {
354                                                                 case CODE_FNC_1:
355                                                                 case CODE_FNC_2:
356                                                                 case CODE_FNC_3:
357                                                                 case CODE_FNC_4_B:
358                                                                         // do nothing?
359                                                                         break;
360                                                                 case CODE_SHIFT:
361                                                                         isNextShifted = true;
362                                                                         codeSet = CODE_CODE_C;
363                                                                         break;
364                                                                 case CODE_CODE_A:
365                                                                         codeSet = CODE_CODE_A;
366                                                                         break;
367                                                                 case CODE_CODE_C:
368                                                                         codeSet = CODE_CODE_C;
369                                                                         break;
370                                                                 case CODE_STOP:
371                                                                         done = true;
372                                                                         break;
373                                                         }
374                                                 }
375                                                 break;
376                                         case CODE_CODE_C:
377                                         // the code read in this case is the number encoded directly
378                                                 if (code < 100) {
379                                                         if (code < 10) 
380                                                         tmpResultSStr << '0';
381                                                 tmpResultSStr << code;
382                                                 } else {
383                                                         if (code != CODE_STOP) {
384                                                                 lastCharacterWasPrintable = false;
385                                                         }
386                                                         switch (code) {
387                                                                 case CODE_FNC_1:
388                                                                         // do nothing?
389                                                                         break;
390                                                                 case CODE_CODE_A:
391                                                                         codeSet = CODE_CODE_A;
392                                                                         break;
393                                                                 case CODE_CODE_B:
394                                                                         codeSet = CODE_CODE_B;
395                                                                         break;
396                                                                 case CODE_STOP:
397                                                                         done = true;
398                                                                         break;
399                                                         }
400                                                 }
401                                                 break;
402                                 }
403                                 
404                                 // Unshift back to another code set if we were shifted
405                                 if (unshift) {
406                                         switch (codeSet) {
407                                                 case CODE_CODE_A:
408                                                         codeSet = CODE_CODE_C;
409                                                         break;
410                                                 case CODE_CODE_B:
411                                                         codeSet = CODE_CODE_A;
412                                                         break;
413                                                 case CODE_CODE_C:
414                                                         codeSet = CODE_CODE_B;
415                                                         break;
416                                         }
417                                 }
418                                 
419                         }
420                         
421                         // Check for ample whitespace following pattern, but, to do this we first need to remember that
422                         // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
423                         // to read off. Would be slightly better to properly read. Here we just skip it:
424                         int width = row->getSize();
425                         while (nextStart < width && row->get(nextStart)) {
426                                 nextStart++;
427                         }
428                         if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
429                                 delete [] startPatternInfo;
430                                 throw ReaderException("");
431                         }
432                         
433                         // Pull out from sum the value of the penultimate check code
434                         checksumTotal -= multiplier * lastCode;
435                         // lastCode is the checksum then:
436                         if (checksumTotal % 103 != lastCode) {
437                                 delete [] startPatternInfo;
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 //                      String resultString(tmpResultString);
458                         
459                         if (tmpResultString.length() == 0) {
460                                 delete [] startPatternInfo;
461                                 // Almost surely a false positive
462                                 throw ReaderException("");
463                         }
464                         
465                         float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
466                         float right = (float) (nextStart + lastStart) / 2.0f;
467                         
468                         std::vector< Ref<ResultPoint> > resultPoints(2);
469                         Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
470                         Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
471                         resultPoints[0] = resultPoint1;
472                         resultPoints[1] = resultPoint2;
473                         
474                         ArrayRef<unsigned char> resultBytes(1);
475                         
476                         delete [] startPatternInfo;
477                         startPatternInfo = NULL;
478                         
479                         Ref<Result> res(new Result(resultString, resultBytes, resultPoints, BarcodeFormat_CODE_128));
480                         return res;
481                 }
482                 
483                 void Code128Reader::append(char* s, char c){
484                         int len = strlen(s);
485                         s[len] = c;
486                         s[len + 1] = '\0';
487                 }
488                 
489                 Code128Reader::~Code128Reader(){
490                 }
491         }
492 }