datamask.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. import {URShift} from './qrcode';
  22. var DataMask = {};
  23. DataMask.forReference = function(reference) {
  24. if (reference < 0 || reference > 7) {
  25. throw "System.ArgumentException";
  26. }
  27. return DataMask.DATA_MASKS[reference];
  28. };
  29. function DataMask000() {
  30. this.unmaskBitMatrix = function(bits, dimension) {
  31. for (var i = 0; i < dimension; i++) {
  32. for (var j = 0; j < dimension; j++) {
  33. if (this.isMasked(i, j)) {
  34. bits.flip(j, i);
  35. }
  36. }
  37. }
  38. };
  39. this.isMasked = function(i, j) {
  40. return ((i + j) & 0x01) == 0;
  41. };
  42. }
  43. function DataMask001() {
  44. this.unmaskBitMatrix = function(bits, dimension) {
  45. for (var i = 0; i < dimension; i++) {
  46. for (var j = 0; j < dimension; j++) {
  47. if (this.isMasked(i, j)) {
  48. bits.flip(j, i);
  49. }
  50. }
  51. }
  52. };
  53. this.isMasked = function(i, j) {
  54. return (i & 0x01) == 0;
  55. };
  56. }
  57. function DataMask010() {
  58. this.unmaskBitMatrix = function(bits, dimension) {
  59. for (var i = 0; i < dimension; i++) {
  60. for (var j = 0; j < dimension; j++) {
  61. if (this.isMasked(i, j)) {
  62. bits.flip(j, i);
  63. }
  64. }
  65. }
  66. };
  67. this.isMasked = function(i, j) {
  68. return j % 3 == 0;
  69. };
  70. }
  71. function DataMask011() {
  72. this.unmaskBitMatrix = function(bits, dimension) {
  73. for (var i = 0; i < dimension; i++) {
  74. for (var j = 0; j < dimension; j++) {
  75. if (this.isMasked(i, j)) {
  76. bits.flip(j, i);
  77. }
  78. }
  79. }
  80. };
  81. this.isMasked = function(i, j) {
  82. return (i + j) % 3 == 0;
  83. };
  84. }
  85. function DataMask100() {
  86. this.unmaskBitMatrix = function(bits, dimension) {
  87. for (var i = 0; i < dimension; i++) {
  88. for (var j = 0; j < dimension; j++) {
  89. if (this.isMasked(i, j)) {
  90. bits.flip(j, i);
  91. }
  92. }
  93. }
  94. };
  95. this.isMasked = function(i, j) {
  96. return (((URShift(i, 1)) + (j / 3)) & 0x01) == 0;
  97. };
  98. }
  99. function DataMask101() {
  100. this.unmaskBitMatrix = function(bits, dimension) {
  101. for (var i = 0; i < dimension; i++) {
  102. for (var j = 0; j < dimension; j++) {
  103. if (this.isMasked(i, j)) {
  104. bits.flip(j, i);
  105. }
  106. }
  107. }
  108. };
  109. this.isMasked = function(i, j) {
  110. var temp = i * j;
  111. return (temp & 0x01) + (temp % 3) == 0;
  112. };
  113. }
  114. function DataMask110() {
  115. this.unmaskBitMatrix = function(bits, dimension) {
  116. for (var i = 0; i < dimension; i++) {
  117. for (var j = 0; j < dimension; j++) {
  118. if (this.isMasked(i, j)) {
  119. bits.flip(j, i);
  120. }
  121. }
  122. }
  123. };
  124. this.isMasked = function(i, j) {
  125. var temp = i * j;
  126. return (((temp & 0x01) + (temp % 3)) & 0x01) == 0;
  127. };
  128. }
  129. function DataMask111() {
  130. this.unmaskBitMatrix = function(bits, dimension) {
  131. for (var i = 0; i < dimension; i++) {
  132. for (var j = 0; j < dimension; j++) {
  133. if (this.isMasked(i, j)) {
  134. bits.flip(j, i);
  135. }
  136. }
  137. }
  138. };
  139. this.isMasked = function(i, j) {
  140. return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0;
  141. };
  142. }
  143. DataMask.DATA_MASKS = [new DataMask000(), new DataMask001(), new DataMask010(), new DataMask011(), new DataMask100(), new DataMask101(), new DataMask110(), new DataMask111()];
  144. export default DataMask;