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

DataMask.cs 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using ZXing.Common;
  17. namespace ZXing.QrCode.Internal
  18. {
  19. /// <summary> <p>Encapsulates data masks for the data bits in a QR code, per ISO 18004:2006 6.8. Implementations
  20. /// of this class can un-mask a raw BitMatrix. For simplicity, they will unmask the entire BitMatrix,
  21. /// including areas used for finder patterns, timing patterns, etc. These areas should be unused
  22. /// after the point they are unmasked anyway.</p>
  23. ///
  24. /// <p>Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position
  25. /// and j is row position. In fact, as the text says, i is row position and j is column position.</p>
  26. ///
  27. /// </summary>
  28. /// <author> Sean Owen
  29. /// </author>
  30. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  31. /// </author>
  32. abstract class DataMask
  33. {
  34. /// <summary> See ISO 18004:2006 6.8.1</summary>
  35. private static readonly DataMask[] DATA_MASKS = new DataMask[]
  36. {
  37. new DataMask000(),
  38. new DataMask001(),
  39. new DataMask010(),
  40. new DataMask011(),
  41. new DataMask100(),
  42. new DataMask101(),
  43. new DataMask110(),
  44. new DataMask111()
  45. };
  46. private DataMask()
  47. {
  48. }
  49. /// <summary> <p>Implementations of this method reverse the data masking process applied to a QR Code and
  50. /// make its bits ready to read.</p>
  51. ///
  52. /// </summary>
  53. /// <param name="bits">representation of QR Code bits
  54. /// </param>
  55. /// <param name="dimension">dimension of QR Code, represented by bits, being unmasked
  56. /// </param>
  57. internal void unmaskBitMatrix(BitMatrix bits, int dimension)
  58. {
  59. for (int i = 0; i < dimension; i++)
  60. {
  61. for (int j = 0; j < dimension; j++)
  62. {
  63. if (isMasked(i, j))
  64. {
  65. bits.flip(j, i);
  66. }
  67. }
  68. }
  69. }
  70. internal abstract bool isMasked(int i, int j);
  71. /// <param name="reference">a value between 0 and 7 indicating one of the eight possible
  72. /// data mask patterns a QR Code may use
  73. /// </param>
  74. /// <returns> {@link DataMask} encapsulating the data mask pattern
  75. /// </returns>
  76. internal static DataMask forReference(int reference)
  77. {
  78. if (reference < 0 || reference > 7)
  79. {
  80. throw new System.ArgumentException();
  81. }
  82. return DATA_MASKS[reference];
  83. }
  84. /// <summary> 000: mask bits for which (x + y) mod 2 == 0</summary>
  85. private sealed class DataMask000 : DataMask
  86. {
  87. internal override bool isMasked(int i, int j)
  88. {
  89. return ((i + j) & 0x01) == 0;
  90. }
  91. }
  92. /// <summary> 001: mask bits for which x mod 2 == 0</summary>
  93. private sealed class DataMask001 : DataMask
  94. {
  95. internal override bool isMasked(int i, int j)
  96. {
  97. return (i & 0x01) == 0;
  98. }
  99. }
  100. /// <summary> 010: mask bits for which y mod 3 == 0</summary>
  101. private sealed class DataMask010 : DataMask
  102. {
  103. internal override bool isMasked(int i, int j)
  104. {
  105. return j % 3 == 0;
  106. }
  107. }
  108. /// <summary> 011: mask bits for which (x + y) mod 3 == 0</summary>
  109. private sealed class DataMask011 : DataMask
  110. {
  111. internal override bool isMasked(int i, int j)
  112. {
  113. return (i + j) % 3 == 0;
  114. }
  115. }
  116. /// <summary> 100: mask bits for which (x/2 + y/3) mod 2 == 0</summary>
  117. private sealed class DataMask100 : DataMask
  118. {
  119. internal override bool isMasked(int i, int j)
  120. {
  121. return ((((int)((uint)i >> 1)) + (j / 3)) & 0x01) == 0;
  122. }
  123. }
  124. /// <summary> 101: mask bits for which xy mod 2 + xy mod 3 == 0</summary>
  125. private sealed class DataMask101 : DataMask
  126. {
  127. internal override bool isMasked(int i, int j)
  128. {
  129. int temp = i * j;
  130. return (temp & 0x01) + (temp % 3) == 0;
  131. }
  132. }
  133. /// <summary> 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0</summary>
  134. private sealed class DataMask110 : DataMask
  135. {
  136. internal override bool isMasked(int i, int j)
  137. {
  138. int temp = i * j;
  139. return (((temp & 0x01) + (temp % 3)) & 0x01) == 0;
  140. }
  141. }
  142. /// <summary> 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0</summary>
  143. private sealed class DataMask111 : DataMask
  144. {
  145. internal override bool isMasked(int i, int j)
  146. {
  147. return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0;
  148. }
  149. }
  150. }
  151. }