Added a project written on Qt framework for Symbian and added tutorials for both...
[zxing.git] / symbian / QQrDecoder / zxing / qrcode / decoder / DataBlock.cpp
1 /*
2  *  DataBlock.cpp
3  *  zxing
4  *
5  *  Created by Christian Brunschen on 19/05/2008.
6  *  Copyright 2008 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 <zxing/qrcode/decoder/DataBlock.h>
22 #include <zxing/common/IllegalArgumentException.h>
23
24 namespace zxing {
25 namespace qrcode {
26
27 using namespace std;
28
29 DataBlock::DataBlock(int numDataCodewords, ArrayRef<unsigned char> codewords) :
30     numDataCodewords_(numDataCodewords), codewords_(codewords) {
31 }
32
33 int DataBlock::getNumDataCodewords() {
34   return numDataCodewords_;
35 }
36
37 ArrayRef<unsigned char> DataBlock::getCodewords() {
38   return codewords_;
39 }
40
41
42 std::vector<Ref<DataBlock> > DataBlock::getDataBlocks(ArrayRef<unsigned char> rawCodewords, Version *version,
43     ErrorCorrectionLevel &ecLevel) {
44
45
46   // Figure out the number and size of data blocks used by this version and
47   // error correction level
48   ECBlocks &ecBlocks = version->getECBlocksForLevel(ecLevel);
49
50
51   // First count the total number of data blocks
52   int totalBlocks = 0;
53   vector<ECB*> ecBlockArray = ecBlocks.getECBlocks();
54   for (size_t i = 0; i < ecBlockArray.size(); i++) {
55     totalBlocks += ecBlockArray[i]->getCount();
56   }
57
58   // Now establish DataBlocks of the appropriate size and number of data codewords
59   std::vector<Ref<DataBlock> > result(totalBlocks);
60   int numResultBlocks = 0;
61   for (size_t j = 0; j < ecBlockArray.size(); j++) {
62     ECB *ecBlock = ecBlockArray[j];
63     for (int i = 0; i < ecBlock->getCount(); i++) {
64       int numDataCodewords = ecBlock->getDataCodewords();
65       int numBlockCodewords = ecBlocks.getECCodewords() + numDataCodewords;
66       ArrayRef<unsigned char> buffer(numBlockCodewords);
67       Ref<DataBlock> blockRef(new DataBlock(numDataCodewords, buffer));
68       result[numResultBlocks++] = blockRef;
69     }
70   }
71
72   // All blocks have the same amount of data, except that the last n
73   // (where n may be 0) have 1 more byte. Figure out where these start.
74   int shorterBlocksTotalCodewords = result[0]->codewords_.size();
75   int longerBlocksStartAt = result.size() - 1;
76   while (longerBlocksStartAt >= 0) {
77     int numCodewords = result[longerBlocksStartAt]->codewords_.size();
78     if (numCodewords == shorterBlocksTotalCodewords) {
79       break;
80     }
81     if (numCodewords != shorterBlocksTotalCodewords + 1) {
82       throw IllegalArgumentException("Data block sizes differ by more than 1");
83     }
84     longerBlocksStartAt--;
85   }
86   longerBlocksStartAt++;
87
88   int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.getECCodewords();
89   // The last elements of result may be 1 element longer;
90   // first fill out as many elements as all of them have
91   int rawCodewordsOffset = 0;
92   for (int i = 0; i < shorterBlocksNumDataCodewords; i++) {
93     for (int j = 0; j < numResultBlocks; j++) {
94       result[j]->codewords_[i] = rawCodewords[rawCodewordsOffset++];
95     }
96   }
97   // Fill out the last data block in the longer ones
98   for (int j = longerBlocksStartAt; j < numResultBlocks; j++) {
99     result[j]->codewords_[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
100   }
101   // Now add in error correction blocks
102   int max = result[0]->codewords_.size();
103   for (int i = shorterBlocksNumDataCodewords; i < max; i++) {
104     for (int j = 0; j < numResultBlocks; j++) {
105       int iOffset = j < longerBlocksStartAt ? i : i + 1;
106       result[j]->codewords_[iOffset] = rawCodewords[rawCodewordsOffset++];
107     }
108   }
109
110   if ((size_t)rawCodewordsOffset != rawCodewords.size()) {
111     throw IllegalArgumentException("rawCodewordsOffset != rawCodewords.length");
112   }
113
114   return result;
115 }
116
117 }
118 }