C++ port: UPCEAN reader fixes
[zxing.git] / cpp / core / src / zxing / oned / UPCEANReader.cpp
1 /*
2  *  UPCEANReader.cpp
3  *  ZXing
4  *
5  *  Created by Lukasz Warchol on 10-01-21.
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 "UPCEANReader.h"
22 #include <zxing/oned/OneDResultPoint.h>
23 #include <zxing/ReaderException.h>
24 namespace zxing {
25         namespace oned {
26                 
27                 /**
28                  * Start/end guard pattern.
29                  */
30                 static const int START_END_PATTERN[3] = {1, 1, 1};
31                 
32                 /**
33                  * Pattern marking the middle of a UPC/EAN pattern, separating the two halves.
34                  */
35                 static const int MIDDLE_PATTERN_LEN = 5;
36                 static const int MIDDLE_PATTERN[MIDDLE_PATTERN_LEN] = {1, 1, 1, 1, 1};
37                 
38                 /**
39                  * "Odd", or "L" patterns used to encode UPC/EAN digits.
40                  */
41                 const int L_PATTERNS_LEN = 10;
42                 const int L_PATTERNS_SUB_LEN = 4;
43                 const int L_PATTERNS[10][4] = {
44                         {3, 2, 1, 1}, // 0
45                         {2, 2, 2, 1}, // 1
46                         {2, 1, 2, 2}, // 2
47                         {1, 4, 1, 1}, // 3
48                         {1, 1, 3, 2}, // 4
49                         {1, 2, 3, 1}, // 5
50                         {1, 1, 1, 4}, // 6
51                         {1, 3, 1, 2}, // 7
52                         {1, 2, 1, 3}, // 8
53                         {3, 1, 1, 2}  // 9
54                 };
55                 
56                 /**
57                  * As above but also including the "even", or "G" patterns used to encode UPC/EAN digits.
58                  */
59                 const int L_AND_G_PATTERNS_LEN = 20;
60                 const int L_AND_G_PATTERNS_SUB_LEN = 4;
61                 const int L_AND_G_PATTERNS[L_AND_G_PATTERNS_LEN][L_AND_G_PATTERNS_SUB_LEN] = {
62                         {3, 2, 1, 1}, // 0
63                         {2, 2, 2, 1}, // 1
64                         {2, 1, 2, 2}, // 2
65                         {1, 4, 1, 1}, // 3
66                         {1, 1, 3, 2}, // 4
67                         {1, 2, 3, 1}, // 5
68                         {1, 1, 1, 4}, // 6
69                         {1, 3, 1, 2}, // 7
70                         {1, 2, 1, 3}, // 8
71                         {3, 1, 1, 2}, // 9
72                         {1, 1, 2, 3}, // 10 reversed 0
73                         {1, 2, 2, 2}, // 11 reversed 1
74                         {2, 2, 1, 2}, // 12 reversed 2
75                         {1, 1, 4, 1}, // 13 reversed 3
76                         {2, 3, 1, 1}, // 14 reversed 4
77                         {1, 3, 2, 1}, // 15 reversed 5
78                         {4, 1, 1, 1}, // 16 reversed 6
79                         {2, 1, 3, 1}, // 17 reversed 7
80                         {3, 1, 2, 1}, // 18 reversed 8
81                         {2, 1, 1, 3}  // 19 reversed 9
82                 };
83                 
84
85                 const int UPCEANReader::getMIDDLE_PATTERN_LEN(){
86                         return MIDDLE_PATTERN_LEN;
87                 }
88                 const int* UPCEANReader::getMIDDLE_PATTERN(){
89                         return MIDDLE_PATTERN;
90                 }
91                 
92                 UPCEANReader::UPCEANReader(){
93                 }
94                 
95                 
96                 Ref<Result> UPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row){
97                         return decodeRow(rowNumber, row, findStartGuardPattern(row));
98                 }
99                 Ref<Result> UPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]){
100                         
101                         std::string tmpResultString;
102                         std::string& tmpResultStringRef = tmpResultString;
103                         int endStart;
104                         try {
105                                 endStart = decodeMiddle(row, startGuardRange, 2 /*reference findGuardPattern*/ , tmpResultStringRef);
106                         } catch (ReaderException re) {
107                                 if (startGuardRange!=NULL) {
108                                         delete [] startGuardRange;
109                                         startGuardRange = NULL;
110                                 }
111                                 throw re;
112                         }
113                         
114                         int* endRange = decodeEnd(row, endStart);
115                                                 
116                         // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
117                         // spec might want more whitespace, but in practice this is the maximum we can count on.
118                         size_t end = endRange[1];
119                         size_t quietEnd = end + (end - endRange[0]);
120                         if (quietEnd >= row->getSize() || !row->isRange(end, quietEnd, false)) {
121                                 throw ReaderException("Quiet zone asserrt fail.");
122                         }
123                         
124                         if (!checkChecksum(tmpResultString)) {
125                                 if (startGuardRange!=NULL) {
126                                         delete [] startGuardRange;
127                                         startGuardRange = NULL;
128                                 }
129                                 if (endRange!=NULL) {
130                                         delete [] endRange;
131                                         endRange = NULL;
132                                 }
133                                 throw ReaderException("Checksum fail.");
134                         }
135                         
136                         Ref<String> resultString(new String(tmpResultString));
137                         
138                         float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
139                         float right = (float) (endRange[1] + endRange[0]) / 2.0f;
140                         
141                         std::vector< Ref<ResultPoint> > resultPoints(2);
142                         Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
143                         Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
144                         resultPoints[0] = resultPoint1;
145                         resultPoints[1] = resultPoint2;
146                         
147                         ArrayRef<unsigned char> resultBytes(1);
148                         
149                         if (startGuardRange!=NULL) {
150                                 delete [] startGuardRange;
151                                 startGuardRange = NULL;
152                         }
153                         if (endRange!=NULL) {
154                                 delete [] endRange;
155                                 endRange = NULL;
156                         }
157                         
158                         Ref<Result> res(new Result(resultString, resultBytes, resultPoints, getBarcodeFormat()));
159                         return res;
160                 }
161                 
162                 int* UPCEANReader::findStartGuardPattern(Ref<BitArray> row){
163                         bool foundStart = false;
164                         
165                         int* startRange = NULL;
166                         int nextStart = 0;
167                         while (!foundStart) {
168                                 startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN, sizeof(START_END_PATTERN)/sizeof(int));
169                                 int start = startRange[0];
170                                 nextStart = startRange[1];
171                                 // Make sure there is a quiet zone at least as big as the start pattern before the barcode.
172                                 // If this check would run off the left edge of the image, do not accept this barcode,
173                                 // as it is very likely to be a false positive.
174                                 int quietStart = start - (nextStart - start);
175                                 if (quietStart >= 0) {
176                                         foundStart = row->isRange(quietStart, start, false);
177                                 }
178                                 if (!foundStart) {
179                                         delete [] startRange;
180                                 }
181                         }
182                         return startRange;
183                 }
184                 
185                 int* UPCEANReader::findGuardPattern(Ref<BitArray> row, int rowOffset, bool whiteFirst, const int pattern[], int patternLen){
186                         int patternLength = patternLen;
187                         
188                         int counters[patternLength];
189                         int countersCount = sizeof(counters)/sizeof(int);
190                         for (int i=0; i<countersCount ; i++) {
191                                 counters[i]=0;
192                         }
193                         int width = row->getSize();
194                         bool isWhite = false;
195                         while (rowOffset < width) {
196                                 isWhite = !row->get(rowOffset);
197                                 if (whiteFirst == isWhite) {
198                                         break;
199                                 }
200                                 rowOffset++;
201                         }
202                         
203                         int counterPosition = 0;
204                         int patternStart = rowOffset;
205                         for (int x = rowOffset; x < width; x++) {
206                                 bool pixel = row->get(x);
207                                 if (pixel ^ isWhite) {
208                                         counters[counterPosition]++;
209                                 } else {
210                                         if (counterPosition == patternLength - 1) {
211                                                 if (patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
212                                                         int* resultValue = new int[2];
213                                                         resultValue[0] = patternStart;
214                                                         resultValue[1] = x;
215                                                         return resultValue;
216                                                 }
217                                                 patternStart += counters[0] + counters[1];
218                                                 for (int y = 2; y < patternLength; y++) {
219                                                         counters[y - 2] = counters[y];
220                                                 }
221                                                 counters[patternLength - 2] = 0;
222                                                 counters[patternLength - 1] = 0;
223                                                 counterPosition--;
224                                         } else {
225                                                 counterPosition++;
226                                         }
227                                         counters[counterPosition] = 1;
228                                         isWhite = !isWhite;
229                                 }
230                         }
231                         throw ReaderException("findGuardPattern");
232                 }
233                 
234                 int* UPCEANReader::decodeEnd(Ref<BitArray> row, int endStart){
235                         return findGuardPattern(row, endStart, false, START_END_PATTERN, sizeof(START_END_PATTERN)/sizeof(int));
236                 }
237                 
238 //              int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, int** patterns/*[][]*/, int paterns1Len, int paterns2Len)              
239                 int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, UPC_EAN_PATTERNS patternType){
240                         recordPattern(row, rowOffset, counters, countersLen);
241                         unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
242                         int bestMatch = -1;
243                         
244                         int max = 0;
245                         switch (patternType) {
246                                 case UPC_EAN_PATTERNS_L_PATTERNS:
247                                         max = L_PATTERNS_LEN;
248                                         for (int i = 0; i < max; i++) {
249                                                 int pattern[countersLen];
250                                                 for(int j = 0; j< countersLen; j++){
251                                                         pattern[j] = L_PATTERNS[i][j];
252                                                 }
253                                                 
254                                                 unsigned int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
255                                                 if (variance < bestVariance) {
256                                                         bestVariance = variance;
257                                                         bestMatch = i;
258                                                 }
259                                         }
260                                         break;
261                                 case UPC_EAN_PATTERNS_L_AND_G_PATTERNS:
262                                         max = L_AND_G_PATTERNS_LEN;
263                                         for (int i = 0; i < max; i++) {
264                                                 int pattern[countersLen];
265                                                 for(int j = 0; j< countersLen; j++){
266                                                         pattern[j] = L_AND_G_PATTERNS[i][j];
267                                                 }
268                                                 
269                                                 unsigned int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
270                                                 if (variance < bestVariance) {
271                                                         bestVariance = variance;
272                                                         bestMatch = i;
273                                                 }
274                                         }
275                                         break;
276                                 default:
277                                         break;
278                         }
279                         if (bestMatch >= 0) {
280                                 return bestMatch;
281                         } else {
282                                 throw ReaderException("UPCEANReader::decodeDigit: No best mach");
283                         }
284                 }
285                 
286                 
287                 /**
288                  * @return {@link #checkStandardUPCEANChecksum(String)}
289                  */
290                 bool UPCEANReader::checkChecksum(std::string s){
291                         return checkStandardUPCEANChecksum(s);
292                 }
293                 
294                 /**
295                  * Computes the UPC/EAN checksum on a string of digits, and reports
296                  * whether the checksum is correct or not.
297                  *
298                  * @param s string of digits to check
299                  * @return true iff string of digits passes the UPC/EAN checksum algorithm
300                  * @throws ReaderException if the string does not contain only digits
301                  */
302                 bool UPCEANReader::checkStandardUPCEANChecksum(std::string s){
303                         int length = s.length();
304                         if (length == 0) {
305                                 return false;
306                         }
307                         
308                         int sum = 0;
309                         for (int i = length - 2; i >= 0; i -= 2) {
310                                 int digit = (int) s[i] - (int) '0';
311                                 if (digit < 0 || digit > 9) {
312                                         throw ReaderException("checkStandardUPCEANChecksum");
313                                 }
314                                 sum += digit;
315                         }
316                         sum *= 3;
317                         for (int i = length - 1; i >= 0; i -= 2) {
318                                 int digit = (int) s[i] - (int) '0';
319                                 if (digit < 0 || digit > 9) {
320                                         throw ReaderException("checkStandardUPCEANChecksum");
321                                 }
322                                 sum += digit;
323                         }
324                         return sum % 10 == 0;
325                 }
326                 UPCEANReader::~UPCEANReader(){
327                 }
328         }
329 }