DataBlock.cpp 4.0 KB

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