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 "DataBlock.h"
22 #include "../../common/IllegalArgumentException.h"
28 using namespace common;
30 ArrayRef<Ref<DataBlock> >
31 DataBlock::getDataBlocks(ArrayRef<unsigned char> rawCodewords,
33 ErrorCorrectionLevel &ecLevel) {
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);
39 // First count the total number of data blocks
41 vector<Version::ECB*> ecBlockArray = ecBlocks.getECBlocks();
42 for (size_t i = 0; i < ecBlockArray.size(); i++) {
43 totalBlocks += ecBlockArray[i]->getCount();
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;
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) {
69 if (numCodewords != shorterBlocksTotalCodewords + 1) {
70 throw new IllegalArgumentException("Data block sizes differ by more than 1");
72 longerBlocksStartAt--;
74 longerBlocksStartAt++;
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++];
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++];
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++];
98 if ((size_t) rawCodewordsOffset != rawCodewords.size()) {
99 throw new IllegalArgumentException("rawCodewordsOffset != rawCodewords.length");