not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB

  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. function DataBlock(numDataCodewords, codewords)
  22. {
  23. this.numDataCodewords = numDataCodewords;
  24. this.codewords = codewords;
  25. this.__defineGetter__("NumDataCodewords", function()
  26. {
  27. return this.numDataCodewords;
  28. });
  29. this.__defineGetter__("Codewords", function()
  30. {
  31. return this.codewords;
  32. });
  33. }
  34. DataBlock.getDataBlocks=function(rawCodewords, version, ecLevel)
  35. {
  36. if (rawCodewords.length != version.TotalCodewords)
  37. {
  38. throw "ArgumentException";
  39. }
  40. // Figure out the number and size of data blocks used by this version and
  41. // error correction level
  42. var ecBlocks = version.getECBlocksForLevel(ecLevel);
  43. // First count the total number of data blocks
  44. var totalBlocks = 0;
  45. var ecBlockArray = ecBlocks.getECBlocks();
  46. for (var i = 0; i < ecBlockArray.length; i++)
  47. {
  48. totalBlocks += ecBlockArray[i].Count;
  49. }
  50. // Now establish DataBlocks of the appropriate size and number of data codewords
  51. var result = new Array(totalBlocks);
  52. var numResultBlocks = 0;
  53. for (var j = 0; j < ecBlockArray.length; j++)
  54. {
  55. var ecBlock = ecBlockArray[j];
  56. for (var i = 0; i < ecBlock.Count; i++)
  57. {
  58. var numDataCodewords = ecBlock.DataCodewords;
  59. var numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords;
  60. result[numResultBlocks++] = new DataBlock(numDataCodewords, new Array(numBlockCodewords));
  61. }
  62. }
  63. // All blocks have the same amount of data, except that the last n
  64. // (where n may be 0) have 1 more byte. Figure out where these start.
  65. var shorterBlocksTotalCodewords = result[0].codewords.length;
  66. var longerBlocksStartAt = result.length - 1;
  67. while (longerBlocksStartAt >= 0)
  68. {
  69. var numCodewords = result[longerBlocksStartAt].codewords.length;
  70. if (numCodewords == shorterBlocksTotalCodewords)
  71. {
  72. break;
  73. }
  74. longerBlocksStartAt--;
  75. }
  76. longerBlocksStartAt++;
  77. var shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock;
  78. // The last elements of result may be 1 element longer;
  79. // first fill out as many elements as all of them have
  80. var rawCodewordsOffset = 0;
  81. for (var i = 0; i < shorterBlocksNumDataCodewords; i++)
  82. {
  83. for (var j = 0; j < numResultBlocks; j++)
  84. {
  85. result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
  86. }
  87. }
  88. // Fill out the last data block in the longer ones
  89. for (var j = longerBlocksStartAt; j < numResultBlocks; j++)
  90. {
  91. result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
  92. }
  93. // Now add in error correction blocks
  94. var max = result[0].codewords.length;
  95. for (var i = shorterBlocksNumDataCodewords; i < max; i++)
  96. {
  97. for (var j = 0; j < numResultBlocks; j++)
  98. {
  99. var iOffset = j < longerBlocksStartAt?i:i + 1;
  100. result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
  101. }
  102. }
  103. return result;
  104. }