DataMask.php 4.6 KB

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