alignpat.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. export default function AlignmentPattern(posX, posY, estimatedModuleSize) {
  22. this.x = posX;
  23. this.y = posY;
  24. this.count = 1;
  25. this.estimatedModuleSize = estimatedModuleSize;
  26. }
  27. Object.defineProperty(AlignmentPattern.prototype, "X", {
  28. get: function() {
  29. return Math.floor(this.x);
  30. }
  31. });
  32. Object.defineProperty(AlignmentPattern.prototype, "Y", {
  33. get: function() {
  34. return Math.floor(this.y);
  35. }
  36. });
  37. AlignmentPattern.prototype.incrementCount = function() {
  38. this.count++;
  39. };
  40. AlignmentPattern.prototype.aboutEquals = function(moduleSize, i, j) {
  41. if (Math.abs(i - this.y) <= moduleSize && Math.abs(j - this.x) <= moduleSize) {
  42. var moduleSizeDiff = Math.abs(moduleSize - this.estimatedModuleSize);
  43. return moduleSizeDiff <= 1.0 || moduleSizeDiff / this.estimatedModuleSize <= 1.0;
  44. }
  45. return false;
  46. };
  47. export function AlignmentPatternFinder(image, startX, startY, width, height, moduleSize, resultPointCallback) {
  48. this.image = image;
  49. this.possibleCenters = [];
  50. this.startX = startX;
  51. this.startY = startY;
  52. this.width = width;
  53. this.height = height;
  54. this.moduleSize = moduleSize;
  55. this.crossCheckStateCount = [0, 0, 0];
  56. this.resultPointCallback = resultPointCallback;
  57. }
  58. AlignmentPatternFinder.prototype.centerFromEnd = function(stateCount, end) {
  59. return (end - stateCount[2]) - stateCount[1] / 2.0;
  60. };
  61. AlignmentPatternFinder.prototype.foundPatternCross = function(stateCount) {
  62. var moduleSize = this.moduleSize;
  63. var maxVariance = moduleSize / 2.0;
  64. for (var i = 0; i < 3; i++) {
  65. if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. };
  71. AlignmentPatternFinder.prototype.crossCheckVertical = function(startI, centerJ, maxCount, originalStateCountTotal) {
  72. var image = this.image;
  73. var maxI = image.height;
  74. var stateCount = this.crossCheckStateCount;
  75. stateCount[0] = 0;
  76. stateCount[1] = 0;
  77. stateCount[2] = 0;
  78. // Start counting up from center
  79. var i = startI;
  80. while (i >= 0 && image.data[centerJ + i * image.width] && stateCount[1] <= maxCount) {
  81. stateCount[1]++;
  82. i--;
  83. }
  84. // If already too many modules in this state or ran off the edge:
  85. if (i < 0 || stateCount[1] > maxCount) {
  86. return NaN;
  87. }
  88. while (i >= 0 && !image.data[centerJ + i * image.width] && stateCount[0] <= maxCount) {
  89. stateCount[0]++;
  90. i--;
  91. }
  92. if (stateCount[0] > maxCount) {
  93. return NaN;
  94. }
  95. // Now also count down from center
  96. i = startI + 1;
  97. while (i < maxI && image.data[centerJ + i * image.width] && stateCount[1] <= maxCount) {
  98. stateCount[1]++;
  99. i++;
  100. }
  101. if (i == maxI || stateCount[1] > maxCount) {
  102. return NaN;
  103. }
  104. while (i < maxI && !image.data[centerJ + i * image.width] && stateCount[2] <= maxCount) {
  105. stateCount[2]++;
  106. i++;
  107. }
  108. if (stateCount[2] > maxCount) {
  109. return NaN;
  110. }
  111. var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  112. if (5 * Math.abs(stateCountTotal - originalStateCountTotal) >= 2 * originalStateCountTotal) {
  113. return NaN;
  114. }
  115. return this.foundPatternCross(stateCount) ? this.centerFromEnd(stateCount, i) : NaN;
  116. };
  117. AlignmentPatternFinder.prototype.handlePossibleCenter = function(stateCount, i, j) {
  118. var stateCountTotal = stateCount[0] + stateCount[1] + stateCount[2];
  119. var centerJ = this.centerFromEnd(stateCount, j);
  120. var centerI = this.crossCheckVertical(i, Math.floor(centerJ), 2 * stateCount[1], stateCountTotal);
  121. if (!isNaN(centerI)) {
  122. var estimatedModuleSize = (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0;
  123. var max = this.possibleCenters.length;
  124. for (var index = 0; index < max; index++) {
  125. var center = this.possibleCenters[index];
  126. // Look for about the same center and module size:
  127. if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
  128. return new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
  129. }
  130. }
  131. // Hadn't found this before; save it
  132. var point = new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
  133. this.possibleCenters.push(point);
  134. if (this.resultPointCallback != null) {
  135. this.resultPointCallback.foundPossibleResultPoint(point);
  136. }
  137. }
  138. return null;
  139. };
  140. AlignmentPatternFinder.prototype.find = function() {
  141. var image = this.image;
  142. var startX = this.startX;
  143. var height = this.height;
  144. var maxJ = startX + this.width;
  145. var middleI = this.startY + (height >> 1);
  146. // We are looking for black/white/black modules in 1:1:1 ratio;
  147. // this tracks the number of black/white/black modules seen so far
  148. var stateCount = [0, 0, 0];
  149. for (var iGen = 0; iGen < height; iGen++) {
  150. // Search from middle outwards
  151. var i = middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1));
  152. stateCount[0] = 0;
  153. stateCount[1] = 0;
  154. stateCount[2] = 0;
  155. var j = startX;
  156. // Burn off leading white pixels before anything else; if we start in the middle of
  157. // a white run, it doesn't make sense to count its length, since we don't know if the
  158. // white run continued to the left of the start point
  159. while (j < maxJ && !image.data[j + image.width * i]) {
  160. j++;
  161. }
  162. var currentState = 0;
  163. while (j < maxJ) {
  164. if (image.data[j + i * image.width]) {
  165. // Black pixel
  166. if (currentState == 1) {
  167. // Counting black pixels
  168. stateCount[currentState]++;
  169. } else {
  170. // Counting white pixels
  171. if (currentState == 2) {
  172. // A winner?
  173. if (this.foundPatternCross(stateCount)) {
  174. // Yes
  175. var confirmed = this.handlePossibleCenter(stateCount, i, j);
  176. if (confirmed != null) {
  177. return confirmed;
  178. }
  179. }
  180. stateCount[0] = stateCount[2];
  181. stateCount[1] = 1;
  182. stateCount[2] = 0;
  183. currentState = 1;
  184. } else {
  185. stateCount[++currentState]++;
  186. }
  187. }
  188. } else {
  189. // White pixel
  190. if (currentState == 1) {
  191. // Counting black pixels
  192. currentState++;
  193. }
  194. stateCount[currentState]++;
  195. }
  196. j++;
  197. }
  198. if (this.foundPatternCross(stateCount)) {
  199. var confirmed = this.handlePossibleCenter(stateCount, i, maxJ);
  200. if (confirmed != null) {
  201. return confirmed;
  202. }
  203. }
  204. }
  205. // Hmm, nothing we saw was observed and confirmed twice. If we had
  206. // any guess at all, return it.
  207. if (!(this.possibleCenters.length == 0)) {
  208. return this.possibleCenters[0];
  209. }
  210. throw "Couldn't find enough alignment patterns";
  211. };