GreyscaleRotatedLuminanceSource: implemented getMatrix()
[zxing.git] / cpp / core / src / zxing / oned / EAN13Reader.cpp
1 /*
2  *  EAN13Reader.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 "EAN13Reader.h"
21 #include <zxing/ReaderException.h>
22
23 namespace zxing {
24   namespace oned {
25
26     static const int FIRST_DIGIT_ENCODINGS[10] = {
27       0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A
28     };
29
30     EAN13Reader::EAN13Reader() { }
31
32     int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen,
33         std::string& resultString) {
34       const int countersLen = 4;
35       int counters[countersLen] = { 0, 0, 0, 0 };
36
37       int end = row->getSize();
38       int rowOffset = startRange[1];
39       int lgPatternFound = 0;
40
41       for (int x = 0; x < 6 && rowOffset < end; x++) {
42         int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
43             UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
44         if (bestMatch < 0) {
45           return -1;
46         }
47         resultString.append(1, (char) ('0' + bestMatch % 10));
48         for (int i = 0; i < countersLen; i++) {
49           rowOffset += counters[i];
50         }
51         if (bestMatch >= 10) {
52           lgPatternFound |= 1 << (5 - x);
53         }
54       }
55
56       if (!determineFirstDigit(resultString, lgPatternFound)) {
57         return -1;
58       }
59
60       int* middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
61             getMIDDLE_PATTERN_LEN());
62       if (middleRange != NULL) {
63         rowOffset = middleRange[1];
64         for (int x = 0; x < 6 && rowOffset < end; x++) {
65           int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
66               UPC_EAN_PATTERNS_L_PATTERNS);
67           if (bestMatch < 0) {
68             delete [] middleRange;
69             return -1;
70           }
71           resultString.append(1, (char) ('0' + bestMatch));
72           for (int i = 0; i < countersLen; i++) {
73             rowOffset += counters[i];
74           }
75         }
76
77         delete [] middleRange;
78         return rowOffset;
79       }
80       return -1;
81     }
82
83     bool EAN13Reader::determineFirstDigit(std::string& resultString, int lgPatternFound) {
84       for (int d = 0; d < 10; d++) {
85         if (lgPatternFound == FIRST_DIGIT_ENCODINGS[d]) {
86           resultString.insert(0, 1, (char) ('0' + d));
87           return true;
88         }
89       }
90       return false;
91     }
92
93     BarcodeFormat EAN13Reader::getBarcodeFormat(){
94       return BarcodeFormat_EAN_13;
95     }
96   }
97 }