5 * Created by Christian Brunschen on 19/05/2008.
6 * Copyright 2008 ZXing authors All rights reserved.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <zxing/qrcode/decoder/DataBlock.h>
22 #include <zxing/common/IllegalArgumentException.h>
29 DataBlock::DataBlock(int numDataCodewords, ArrayRef<unsigned char> codewords) :
30 numDataCodewords_(numDataCodewords), codewords_(codewords) {
33 int DataBlock::getNumDataCodewords() {
34 return numDataCodewords_;
37 ArrayRef<unsigned char> DataBlock::getCodewords() {
42 std::vector<Ref<DataBlock> > DataBlock::getDataBlocks(ArrayRef<unsigned char> rawCodewords, Version *version,
43 ErrorCorrectionLevel &ecLevel) {
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);
51 // First count the total number of data blocks
53 vector<ECB*> ecBlockArray = ecBlocks.getECBlocks();
54 for (size_t i = 0; i < ecBlockArray.size(); i++) {
55 totalBlocks += ecBlockArray[i]->getCount();
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;
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) {
81 if (numCodewords != shorterBlocksTotalCodewords + 1) {
82 throw IllegalArgumentException("Data block sizes differ by more than 1");
84 longerBlocksStartAt--;
86 longerBlocksStartAt++;
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++];
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++];
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++];
110 if ((size_t)rawCodewordsOffset != rawCodewords.size()) {
111 throw IllegalArgumentException("rawCodewordsOffset != rawCodewords.length");