Added a project written on Qt framework for Symbian and added tutorials for both...
[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
28 namespace zxing {
29         namespace oned {
30                 
31                 const int CODE_PATTERNS_LENGHT = 107;
32                 const int countersLenght = 6;
33                 static const int CODE_PATTERNS[CODE_PATTERNS_LENGHT][countersLenght] = {
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[countersLenght] = {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                                                 int bestVariance = MAX_AVG_VARIANCE;
170                                                 int bestMatch = -1;
171                                                 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
172                                                         int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
173                                                         if (variance < bestVariance) {
174                                                                 bestVariance = variance;
175                                                                 bestMatch = startCode;
176                                                         }
177                                                 }
178                                                 if (bestMatch >= 0) {
179                                                         // Look for whitespace before start pattern, >= 50% of width of start pattern
180                                                         if (row->isRange(fmaxl(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
181                                                                 int* resultValue = new int[3];
182                                                                 resultValue[0] = patternStart;
183                                                                 resultValue[1] = i;
184                                                                 resultValue[2] = bestMatch;
185                                                                 return resultValue;
186                                                         }
187                                                 }
188                                                 patternStart += counters[0] + counters[1];
189                                                 for (int y = 2; y < patternLength; y++) {
190                                                         counters[y - 2] = counters[y];
191                                                 }
192                                                 counters[patternLength - 2] = 0;
193                                                 counters[patternLength - 1] = 0;
194                                                 counterPosition--;
195                                         } else {
196                                                 counterPosition++;
197                                         }
198                                         counters[counterPosition] = 1;
199                                         isWhite = !isWhite;
200                                 }
201                         }
202                         throw ReaderException("");
203                 }
204                 
205                 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){
206                         recordPattern(row, rowOffset, counters, countersCount);
207                         int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
208                         int bestMatch = -1;
209                         for (int d = 0; d < CODE_PATTERNS_LENGHT; d++) {
210                                 int pattern[countersLenght];
211                                 
212                                 for(int ind = 0; ind< countersLenght; ind++){
213                                         pattern[ind] = CODE_PATTERNS[d][ind];
214                                 }
215 //                              memcpy(pattern, CODE_PATTERNS[d], countersLenght);
216                                 int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE);
217                                 if (variance < bestVariance) {
218                                         bestVariance = variance;
219                                         bestMatch = d;
220                                 }
221                         }
222                         // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
223                         if (bestMatch >= 0) {
224                                 return bestMatch;
225                         } else {
226                                 throw ReaderException("");
227                         }
228                 }
229                 
230                 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row){
231                         
232                         int* startPatternInfo = findStartPattern(row);
233                         int startCode = startPatternInfo[2];
234                         int codeSet;
235                         switch (startCode) {
236                                 case CODE_START_A:
237                                         codeSet = CODE_CODE_A;
238                                         break;
239                                 case CODE_START_B:
240                                         codeSet = CODE_CODE_B;
241                                         break;
242                                 case CODE_START_C:
243                                         codeSet = CODE_CODE_C;
244                                         break;
245                                 default:
246                                         throw ReaderException("");
247                         }
248                         
249                         bool done = false;
250                         bool isNextShifted = false;
251                         
252                         std::string tmpResultString;
253
254                         
255                         int lastStart = startPatternInfo[0];
256                         int nextStart = startPatternInfo[1];
257                         int counters[countersLenght] = {0,0,0,0,0,0};
258                         
259                         int lastCode = 0;
260                         int code = 0;
261                         int checksumTotal = startCode;
262                         int multiplier = 0;
263                         bool lastCharacterWasPrintable = true;
264                         
265                         while (!done) {
266                                 
267                                 bool unshift = isNextShifted;
268                                 isNextShifted = false;
269                                 
270                                 // Save off last code
271                                 lastCode = code;
272                                 
273                                 // Decode another code from image
274                                 code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
275                                 
276                                 // Remember whether the last code was printable or not (excluding CODE_STOP)
277                                 if (code != CODE_STOP) {
278                                         lastCharacterWasPrintable = true;
279                                 }
280                                 
281                                 // Add to checksum computation (if not CODE_STOP of course)
282                                 if (code != CODE_STOP) {
283                                         multiplier++;
284                                         checksumTotal += multiplier * code;
285                                 }
286                                 
287                                 // Advance to where the next code will to start
288                                 lastStart = nextStart;
289                                 int _countersLenght = sizeof(counters) / sizeof(int);
290                                 for (int i = 0; i < _countersLenght; i++) {
291                                         nextStart += counters[i];
292                                 }
293                                 
294                                 // Take care of illegal start codes
295                                 switch (code) {
296                                         case CODE_START_A:
297                                         case CODE_START_B:
298                                         case CODE_START_C:
299                                                 throw ReaderException("");
300                                 }
301                                 
302                                 switch (codeSet) {
303                                                 
304                                         case CODE_CODE_A:
305                                                 if (code < 64) {
306                                                         tmpResultString.append(1, (char) (' ' + code));
307                                                 } else if (code < 96) {
308                                                         tmpResultString.append(1, (char) (code - 64));
309                                                 } else {
310                                                         // Don't let CODE_STOP, which always appears, affect whether whether we think the last
311                                                         // code was printable or not.
312                                                         if (code != CODE_STOP) {
313                                                                 lastCharacterWasPrintable = false;
314                                                         }
315                                                         switch (code) {
316                                                                 case CODE_FNC_1:
317                                                                 case CODE_FNC_2:
318                                                                 case CODE_FNC_3:
319                                                                 case CODE_FNC_4_A:
320                                                                         // do nothing?
321                                                                         break;
322                                                                 case CODE_SHIFT:
323                                                                         isNextShifted = true;
324                                                                         codeSet = CODE_CODE_B;
325                                                                         break;
326                                                                 case CODE_CODE_B:
327                                                                         codeSet = CODE_CODE_B;
328                                                                         break;
329                                                                 case CODE_CODE_C:
330                                                                         codeSet = CODE_CODE_C;
331                                                                         break;
332                                                                 case CODE_STOP:
333                                                                         done = true;
334                                                                         break;
335                                                         }
336                                                 }
337                                                 break;
338                                         case CODE_CODE_B:
339                                                 if (code < 96) {
340                                                         tmpResultString.append(1, (char) (' ' + code));
341                                                 } else {
342                                                         if (code != CODE_STOP) {
343                                                                 lastCharacterWasPrintable = false;
344                                                         }
345                                                         switch (code) {
346                                                                 case CODE_FNC_1:
347                                                                 case CODE_FNC_2:
348                                                                 case CODE_FNC_3:
349                                                                 case CODE_FNC_4_B:
350                                                                         // do nothing?
351                                                                         break;
352                                                                 case CODE_SHIFT:
353                                                                         isNextShifted = true;
354                                                                         codeSet = CODE_CODE_C;
355                                                                         break;
356                                                                 case CODE_CODE_A:
357                                                                         codeSet = CODE_CODE_A;
358                                                                         break;
359                                                                 case CODE_CODE_C:
360                                                                         codeSet = CODE_CODE_C;
361                                                                         break;
362                                                                 case CODE_STOP:
363                                                                         done = true;
364                                                                         break;
365                                                         }
366                                                 }
367                                                 break;
368                                         case CODE_CODE_C:
369                                                 if (code < 100) {
370                                                         if (code < 10) {
371                                                                 tmpResultString.append(1, '0');
372                                                         }
373                                                         tmpResultString.append(1, code);
374                                                 } else {
375                                                         if (code != CODE_STOP) {
376                                                                 lastCharacterWasPrintable = false;
377                                                         }
378                                                         switch (code) {
379                                                                 case CODE_FNC_1:
380                                                                         // do nothing?
381                                                                         break;
382                                                                 case CODE_CODE_A:
383                                                                         codeSet = CODE_CODE_A;
384                                                                         break;
385                                                                 case CODE_CODE_B:
386                                                                         codeSet = CODE_CODE_B;
387                                                                         break;
388                                                                 case CODE_STOP:
389                                                                         done = true;
390                                                                         break;
391                                                         }
392                                                 }
393                                                 break;
394                                 }
395                                 
396                                 // Unshift back to another code set if we were shifted
397                                 if (unshift) {
398                                         switch (codeSet) {
399                                                 case CODE_CODE_A:
400                                                         codeSet = CODE_CODE_C;
401                                                         break;
402                                                 case CODE_CODE_B:
403                                                         codeSet = CODE_CODE_A;
404                                                         break;
405                                                 case CODE_CODE_C:
406                                                         codeSet = CODE_CODE_B;
407                                                         break;
408                                         }
409                                 }
410                                 
411                         }
412                         
413                         // Check for ample whitespace following pattern, but, to do this we first need to remember that
414                         // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
415                         // to read off. Would be slightly better to properly read. Here we just skip it:
416                         int width = row->getSize();
417                         while (nextStart < width && row->get(nextStart)) {
418                                 nextStart++;
419                         }
420                         if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
421                                 throw ReaderException("");
422                         }
423                         
424                         // Pull out from sum the value of the penultimate check code
425                         checksumTotal -= multiplier * lastCode;
426                         // lastCode is the checksum then:
427                         if (checksumTotal % 103 != lastCode) {
428                                 throw ReaderException("");
429                         }
430                         
431                         // Need to pull out the check digits from string
432                         int resultLength = tmpResultString.length();
433                         // Only bother if the result had at least one character, and if the checksum digit happened to
434                         // be a printable character. If it was just interpreted as a control code, nothing to remove.
435                         if (resultLength > 0 && lastCharacterWasPrintable) {
436                                 if (codeSet == CODE_CODE_C) {
437                                         tmpResultString.erase(resultLength - 2, resultLength);
438                                 } else {
439                                         tmpResultString.erase(resultLength - 1, resultLength);
440                                 }
441                         }
442                         
443                         Ref<String> resultString(new String(tmpResultString));
444 //                      String resultString(tmpResultString);
445                         
446                         if (tmpResultString.length() == 0) {
447                                 // Almost surely a false positive
448                                 throw ReaderException("");
449                         }
450                         
451                         float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
452                         float right = (float) (nextStart + lastStart) / 2.0f;
453                         
454                         std::vector< Ref<ResultPoint> > resultPoints(2);
455                         Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
456                         Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
457                         resultPoints[0] = resultPoint1;
458                         resultPoints[1] = resultPoint2;
459                         
460                         ArrayRef<unsigned char> resultBytes(1);
461                         
462                         delete [] startPatternInfo;
463                         startPatternInfo = NULL;
464                         
465                         Ref<Result> res(new Result(resultString, resultBytes, resultPoints, BarcodeFormat_CODE_128));
466                         return res;
467                 }
468                 
469                 void Code128Reader::append(char* s, char c){
470                         int len = strlen(s);
471                         s[len] = c;
472                         s[len + 1] = '\0';
473                 }
474                 
475                 Code128Reader::~Code128Reader(){
476                 }
477         }
478 }