DataMask.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * DataMask.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/DataMask.h>
  21. #include <zxing/common/IllegalArgumentException.h>
  22. namespace zxing {
  23. namespace qrcode {
  24. using namespace std;
  25. DataMask::DataMask() {
  26. }
  27. DataMask::~DataMask() {
  28. }
  29. vector<Ref<DataMask> > DataMask::DATA_MASKS;
  30. static int N_DATA_MASKS = DataMask::buildDataMasks();
  31. DataMask &DataMask::forReference(int reference) {
  32. if (reference < 0 || reference > 7) {
  33. throw IllegalArgumentException("reference must be between 0 and 7");
  34. }
  35. return *DATA_MASKS[reference];
  36. }
  37. void DataMask::unmaskBitMatrix(BitMatrix& bits, size_t dimension) {
  38. for (size_t y = 0; y < dimension; y++) {
  39. for (size_t x = 0; x < dimension; x++) {
  40. // TODO: check why the coordinates have to be swapped
  41. if (isMasked(y, x)) {
  42. bits.flip(x, y);
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * 000: mask bits for which (x + y) mod 2 == 0
  49. */
  50. class DataMask000 : public DataMask {
  51. public:
  52. bool isMasked(size_t x, size_t y) {
  53. // return ((x + y) & 0x01) == 0;
  54. return ((x + y) % 2) == 0;
  55. }
  56. };
  57. /**
  58. * 001: mask bits for which x mod 2 == 0
  59. */
  60. class DataMask001 : public DataMask {
  61. public:
  62. bool isMasked(size_t x, size_t) {
  63. // return (x & 0x01) == 0;
  64. return (x % 2) == 0;
  65. }
  66. };
  67. /**
  68. * 010: mask bits for which y mod 3 == 0
  69. */
  70. class DataMask010 : public DataMask {
  71. public:
  72. bool isMasked(size_t, size_t y) {
  73. return y % 3 == 0;
  74. }
  75. };
  76. /**
  77. * 011: mask bits for which (x + y) mod 3 == 0
  78. */
  79. class DataMask011 : public DataMask {
  80. public:
  81. bool isMasked(size_t x, size_t y) {
  82. return (x + y) % 3 == 0;
  83. }
  84. };
  85. /**
  86. * 100: mask bits for which (x/2 + y/3) mod 2 == 0
  87. */
  88. class DataMask100 : public DataMask {
  89. public:
  90. bool isMasked(size_t x, size_t y) {
  91. // return (((x >> 1) + (y / 3)) & 0x01) == 0;
  92. return (((x >> 1) + (y / 3)) % 2) == 0;
  93. }
  94. };
  95. /**
  96. * 101: mask bits for which xy mod 2 + xy mod 3 == 0
  97. */
  98. class DataMask101 : public DataMask {
  99. public:
  100. bool isMasked(size_t x, size_t y) {
  101. size_t temp = x * y;
  102. // return (temp & 0x01) + (temp % 3) == 0;
  103. return (temp % 2) + (temp % 3) == 0;
  104. }
  105. };
  106. /**
  107. * 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0
  108. */
  109. class DataMask110 : public DataMask {
  110. public:
  111. bool isMasked(size_t x, size_t y) {
  112. size_t temp = x * y;
  113. // return (((temp & 0x01) + (temp % 3)) & 0x01) == 0;
  114. return (((temp % 2) + (temp % 3)) % 2) == 0;
  115. }
  116. };
  117. /**
  118. * 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0
  119. */
  120. class DataMask111 : public DataMask {
  121. public:
  122. bool isMasked(size_t x, size_t y) {
  123. // return ((((x + y) & 0x01) + ((x * y) % 3)) & 0x01) == 0;
  124. return ((((x + y) % 2) + ((x * y) % 3)) % 2) == 0;
  125. }
  126. };
  127. int DataMask::buildDataMasks() {
  128. DATA_MASKS.push_back(Ref<DataMask> (new DataMask000()));
  129. DATA_MASKS.push_back(Ref<DataMask> (new DataMask001()));
  130. DATA_MASKS.push_back(Ref<DataMask> (new DataMask010()));
  131. DATA_MASKS.push_back(Ref<DataMask> (new DataMask011()));
  132. DATA_MASKS.push_back(Ref<DataMask> (new DataMask100()));
  133. DATA_MASKS.push_back(Ref<DataMask> (new DataMask101()));
  134. DATA_MASKS.push_back(Ref<DataMask> (new DataMask110()));
  135. DATA_MASKS.push_back(Ref<DataMask> (new DataMask111()));
  136. return DATA_MASKS.size();
  137. }
  138. }
  139. }