Small style stuff
[zxing.git] / cpp / core / src / zxing / oned / EAN8Reader.cpp
1 /*
2  *  EAN8Reader.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 "EAN8Reader.h"
21 #include <zxing/ReaderException.h>
22
23 namespace zxing {
24   namespace oned {
25
26     EAN8Reader::EAN8Reader(){ }
27
28     int EAN8Reader::decodeMiddle(Ref<BitArray> row, int startGuardBegin, int startGuardEnd,
29         std::string& resultString){
30       const int countersLen = 4;
31       int counters[countersLen] = { 0, 0, 0, 0 };
32
33       int end = row->getSize();
34       int rowOffset = startGuardEnd;
35
36       for (int x = 0; x < 4 && rowOffset < end; x++) {
37         int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
38             UPC_EAN_PATTERNS_L_PATTERNS);
39         if (bestMatch < 0) {
40           return -1;
41         }
42         resultString.append(1, (char) ('0' + bestMatch));
43         for (int i = 0; i < countersLen; i++) {
44           rowOffset += counters[i];
45         }
46       }
47
48       int middleRangeStart;
49       int middleRangeEnd;
50       if (findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
51             getMIDDLE_PATTERN_LEN(), &middleRangeStart, &middleRangeEnd)) {
52         rowOffset = middleRangeEnd;
53         for (int x = 0; x < 4 && rowOffset < end; x++) {
54           int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
55               UPC_EAN_PATTERNS_L_PATTERNS);
56           if (bestMatch < 0) {
57             return -1;
58           }
59           resultString.append(1, (char) ('0' + bestMatch));
60           for (int i = 0; i < countersLen; i++) {
61             rowOffset += counters[i];
62           }
63         }
64         return rowOffset;
65       }
66       return -1;
67     }
68
69     BarcodeFormat EAN8Reader::getBarcodeFormat(){
70       return BarcodeFormat_EAN_8;
71     }
72   }
73 }