Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ZXQRCodeMaskUtil.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "ZXByteMatrix.h"
  17. #import "ZXQRCode.h"
  18. #import "ZXQRCodeMaskUtil.h"
  19. // Penalty weights from section 6.8.2.1
  20. const int ZX_N1 = 3;
  21. const int ZX_N2 = 3;
  22. const int ZX_N3 = 40;
  23. const int ZX_N4 = 10;
  24. @implementation ZXQRCodeMaskUtil
  25. + (int)applyMaskPenaltyRule1:(ZXByteMatrix *)matrix {
  26. return [self applyMaskPenaltyRule1Internal:matrix isHorizontal:YES] + [self applyMaskPenaltyRule1Internal:matrix isHorizontal:NO];
  27. }
  28. + (int)applyMaskPenaltyRule2:(ZXByteMatrix *)matrix {
  29. int penalty = 0;
  30. int8_t **array = matrix.array;
  31. int width = matrix.width;
  32. int height = matrix.height;
  33. for (int y = 0; y < height - 1; y++) {
  34. for (int x = 0; x < width - 1; x++) {
  35. int value = array[y][x];
  36. if (value == array[y][x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1]) {
  37. penalty++;
  38. }
  39. }
  40. }
  41. return ZX_N2 * penalty;
  42. }
  43. + (int)applyMaskPenaltyRule3:(ZXByteMatrix *)matrix {
  44. int numPenalties = 0;
  45. int8_t **array = matrix.array;
  46. int width = matrix.width;
  47. int height = matrix.height;
  48. for (int y = 0; y < height; y++) {
  49. for (int x = 0; x < width; x++) {
  50. int8_t *arrayY = array[y]; // We can at least optimize this access
  51. if (x + 6 < width &&
  52. arrayY[x] == 1 &&
  53. arrayY[x + 1] == 0 &&
  54. arrayY[x + 2] == 1 &&
  55. arrayY[x + 3] == 1 &&
  56. arrayY[x + 4] == 1 &&
  57. arrayY[x + 5] == 0 &&
  58. arrayY[x + 6] == 1 &&
  59. ([self isWhiteHorizontal:arrayY length:width from:x - 4 to:x] || [self isWhiteHorizontal:arrayY length:width from:x + 7 to:x + 11])) {
  60. numPenalties++;
  61. }
  62. if (y + 6 < height &&
  63. array[y][x] == 1 &&
  64. array[y + 1][x] == 0 &&
  65. array[y + 2][x] == 1 &&
  66. array[y + 3][x] == 1 &&
  67. array[y + 4][x] == 1 &&
  68. array[y + 5][x] == 0 &&
  69. array[y + 6][x] == 1 &&
  70. ([self isWhiteVertical:array length:width col:x from:y - 4 to:y] || [self isWhiteVertical:array length:height col:x from:y + 7 to:y + 11])) {
  71. numPenalties++;
  72. }
  73. }
  74. }
  75. return numPenalties * ZX_N3;
  76. }
  77. + (BOOL)isWhiteHorizontal:(int8_t *)rowArray length:(int)length from:(int)from to:(int)to {
  78. for (int i = from; i < to; i++) {
  79. if (i >= 0 && i < length && rowArray[i] == 1) {
  80. return NO;
  81. }
  82. }
  83. return YES;
  84. }
  85. + (BOOL)isWhiteVertical:(int8_t **)array length:(int)length col:(int)col from:(int)from to:(int)to {
  86. for (int i = from; i < to; i++) {
  87. if (i >= 0 && i < length && array[i][col] == 1) {
  88. return NO;
  89. }
  90. }
  91. return YES;
  92. }
  93. + (int)applyMaskPenaltyRule4:(ZXByteMatrix *)matrix {
  94. int numDarkCells = 0;
  95. int8_t **array = matrix.array;
  96. int width = matrix.width;
  97. int height = matrix.height;
  98. for (int y = 0; y < height; y++) {
  99. int8_t *arrayY = array[y];
  100. for (int x = 0; x < width; x++) {
  101. if (arrayY[x] == 1) {
  102. numDarkCells++;
  103. }
  104. }
  105. }
  106. int numTotalCells = [matrix height] * [matrix width];
  107. int fivePercentVariances = abs(numDarkCells * 2 - numTotalCells) * 10 / numTotalCells;
  108. return fivePercentVariances * ZX_N4;
  109. }
  110. + (BOOL)dataMaskBit:(int)maskPattern x:(int)x y:(int)y {
  111. int intermediate;
  112. int temp;
  113. switch (maskPattern) {
  114. case 0:
  115. intermediate = (y + x) & 0x1;
  116. break;
  117. case 1:
  118. intermediate = y & 0x1;
  119. break;
  120. case 2:
  121. intermediate = x % 3;
  122. break;
  123. case 3:
  124. intermediate = (y + x) % 3;
  125. break;
  126. case 4:
  127. intermediate = ((y / 2) + (x / 3)) & 0x1;
  128. break;
  129. case 5:
  130. temp = y * x;
  131. intermediate = (temp & 0x1) + (temp % 3);
  132. break;
  133. case 6:
  134. temp = y * x;
  135. intermediate = ((temp & 0x1) + (temp % 3)) & 0x1;
  136. break;
  137. case 7:
  138. temp = y * x;
  139. intermediate = ((temp % 3) + ((y + x) & 0x1)) & 0x1;
  140. break;
  141. default:
  142. [NSException raise:NSInvalidArgumentException format:@"Invalid mask pattern: %d", maskPattern];
  143. }
  144. return intermediate == 0;
  145. }
  146. /**
  147. * Helper function for applyMaskPenaltyRule1. We need this for doing this calculation in both
  148. * vertical and horizontal orders respectively.
  149. */
  150. + (int)applyMaskPenaltyRule1Internal:(ZXByteMatrix *)matrix isHorizontal:(BOOL)isHorizontal {
  151. int penalty = 0;
  152. int iLimit = isHorizontal ? matrix.height : matrix.width;
  153. int jLimit = isHorizontal ? matrix.width : matrix.height;
  154. int8_t **array = matrix.array;
  155. for (int i = 0; i < iLimit; i++) {
  156. int numSameBitCells = 0;
  157. int prevBit = -1;
  158. for (int j = 0; j < jLimit; j++) {
  159. int bit = isHorizontal ? array[i][j] : array[j][i];
  160. if (bit == prevBit) {
  161. numSameBitCells++;
  162. } else {
  163. if (numSameBitCells >= 5) {
  164. penalty += ZX_N1 + (numSameBitCells - 5);
  165. }
  166. numSameBitCells = 1; // Include the cell itself.
  167. prevBit = bit;
  168. }
  169. }
  170. if (numSameBitCells >= 5) {
  171. penalty += ZX_N1 + (numSameBitCells - 5);
  172. }
  173. }
  174. return penalty;
  175. }
  176. @end