Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXQRCodeDataMask.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2012 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. #import "ZXBitMatrix.h"
  17. #import "ZXQRCodeDataMask.h"
  18. /**
  19. * 000: mask bits for which (x + y) mod 2 == 0
  20. */
  21. @interface ZXDataMask000 : ZXQRCodeDataMask
  22. @end
  23. @implementation ZXDataMask000
  24. - (BOOL)isMasked:(int)i j:(int)j {
  25. return ((i + j) & 0x01) == 0;
  26. }
  27. @end
  28. /**
  29. * 001: mask bits for which x mod 2 == 0
  30. */
  31. @interface ZXDataMask001 : ZXQRCodeDataMask
  32. @end
  33. @implementation ZXDataMask001
  34. - (BOOL)isMasked:(int)i j:(int)j {
  35. return (i & 0x01) == 0;
  36. }
  37. @end
  38. /**
  39. * 010: mask bits for which y mod 3 == 0
  40. */
  41. @interface ZXDataMask010 : ZXQRCodeDataMask
  42. @end
  43. @implementation ZXDataMask010
  44. - (BOOL)isMasked:(int)i j:(int)j {
  45. return j % 3 == 0;
  46. }
  47. @end
  48. /**
  49. * 011: mask bits for which (x + y) mod 3 == 0
  50. */
  51. @interface ZXDataMask011 : ZXQRCodeDataMask
  52. @end
  53. @implementation ZXDataMask011
  54. - (BOOL)isMasked:(int)i j:(int)j {
  55. return (i + j) % 3 == 0;
  56. }
  57. @end
  58. /**
  59. * 100: mask bits for which (x/2 + y/3) mod 2 == 0
  60. */
  61. @interface ZXDataMask100 : ZXQRCodeDataMask
  62. @end
  63. @implementation ZXDataMask100
  64. - (BOOL)isMasked:(int)i j:(int)j {
  65. return (((i / 2) + (j /3)) & 0x01) == 0;
  66. }
  67. @end
  68. /**
  69. * 101: mask bits for which xy mod 2 + xy mod 3 == 0
  70. */
  71. @interface ZXDataMask101 : ZXQRCodeDataMask
  72. @end
  73. @implementation ZXDataMask101
  74. - (BOOL)isMasked:(int)i j:(int)j {
  75. int temp = i * j;
  76. return (temp & 0x01) + (temp % 3) == 0;
  77. }
  78. @end
  79. /**
  80. * 110: mask bits for which (xy mod 2 + xy mod 3) mod 2 == 0
  81. */
  82. @interface ZXDataMask110 : ZXQRCodeDataMask
  83. @end
  84. @implementation ZXDataMask110
  85. - (BOOL)isMasked:(int)i j:(int)j {
  86. int temp = i * j;
  87. return (((temp & 0x01) + (temp % 3)) & 0x01) == 0;
  88. }
  89. @end
  90. /**
  91. * 111: mask bits for which ((x+y)mod 2 + xy mod 3) mod 2 == 0
  92. */
  93. @interface ZXDataMask111 : ZXQRCodeDataMask
  94. @end
  95. @implementation ZXDataMask111
  96. - (BOOL)isMasked:(int)i j:(int)j {
  97. return ((((i + j) & 0x01) + ((i * j) % 3)) & 0x01) == 0;
  98. }
  99. @end
  100. @implementation ZXQRCodeDataMask
  101. /**
  102. * See ISO 18004:2006 6.8.1
  103. */
  104. static NSArray *DATA_MASKS = nil;
  105. /**
  106. * Implementations of this method reverse the data masking process applied to a QR Code and
  107. * make its bits ready to read.
  108. */
  109. - (void)unmaskBitMatrix:(ZXBitMatrix *)bits dimension:(int)dimension {
  110. for (int i = 0; i < dimension; i++) {
  111. for (int j = 0; j < dimension; j++) {
  112. if ([self isMasked:i j:j]) {
  113. [bits flipX:j y:i];
  114. }
  115. }
  116. }
  117. }
  118. - (BOOL)isMasked:(int)i j:(int)j {
  119. @throw [NSException exceptionWithName:NSInternalInconsistencyException
  120. reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
  121. userInfo:nil];
  122. }
  123. + (ZXQRCodeDataMask *)forReference:(int)reference {
  124. if (!DATA_MASKS) {
  125. DATA_MASKS = @[[[ZXDataMask000 alloc] init],
  126. [[ZXDataMask001 alloc] init],
  127. [[ZXDataMask010 alloc] init],
  128. [[ZXDataMask011 alloc] init],
  129. [[ZXDataMask100 alloc] init],
  130. [[ZXDataMask101 alloc] init],
  131. [[ZXDataMask110 alloc] init],
  132. [[ZXDataMask111 alloc] init]];
  133. }
  134. if (reference < 0 || reference > 7) {
  135. [NSException raise:NSInvalidArgumentException format:@"Invalid reference value"];
  136. }
  137. return DATA_MASKS[reference];
  138. }
  139. @end