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