grid.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import BitMatrix from './bitmat';
  22. var GridSampler = {};
  23. GridSampler.checkAndNudgePoints = function(image, points) {
  24. var width = image.width;
  25. var height = image.height;
  26. // Check and nudge points from start until we see some that are OK:
  27. var nudged = true;
  28. for (var offset = 0; offset < points.length && nudged; offset += 2) {
  29. var x = Math.floor(points[offset]);
  30. var y = Math.floor(points[offset + 1]);
  31. if (x < -1 || x > width || y < -1 || y > height) {
  32. throw "Error.checkAndNudgePoints ";
  33. }
  34. nudged = false;
  35. if (x == -1) {
  36. points[offset] = 0.0;
  37. nudged = true;
  38. } else if (x == width) {
  39. points[offset] = width - 1;
  40. nudged = true;
  41. }
  42. if (y == -1) {
  43. points[offset + 1] = 0.0;
  44. nudged = true;
  45. } else if (y == height) {
  46. points[offset + 1] = height - 1;
  47. nudged = true;
  48. }
  49. }
  50. // Check and nudge points from end:
  51. nudged = true;
  52. for (var offset = points.length - 2; offset >= 0 && nudged; offset -= 2) {
  53. var x = Math.floor(points[offset]);
  54. var y = Math.floor(points[offset + 1]);
  55. if (x < -1 || x > width || y < -1 || y > height) {
  56. throw "Error.checkAndNudgePoints ";
  57. }
  58. nudged = false;
  59. if (x == -1) {
  60. points[offset] = 0.0;
  61. nudged = true;
  62. } else if (x == width) {
  63. points[offset] = width - 1;
  64. nudged = true;
  65. }
  66. if (y == -1) {
  67. points[offset + 1] = 0.0;
  68. nudged = true;
  69. } else if (y == height) {
  70. points[offset + 1] = height - 1;
  71. nudged = true;
  72. }
  73. }
  74. };
  75. GridSampler.sampleGrid3 = function(image, dimension, transform) {
  76. var bits = new BitMatrix(dimension);
  77. var points = new Array(dimension << 1);
  78. for (var y = 0; y < dimension; y++) {
  79. var max = points.length;
  80. var iValue = y + 0.5;
  81. for (var x = 0; x < max; x += 2) {
  82. points[x] = (x >> 1) + 0.5;
  83. points[x + 1] = iValue;
  84. }
  85. transform.transformPoints1(points);
  86. // Quick check to see if points transformed to something inside the image
  87. // sufficient to check the endpoints
  88. GridSampler.checkAndNudgePoints(image, points);
  89. try {
  90. for (var x = 0; x < max; x += 2) {
  91. var bit = image.data[Math.floor(points[x]) + image.width * Math.floor(points[x + 1])];
  92. if (bit)
  93. bits.set_Renamed(x >> 1, y);
  94. }
  95. } catch (aioobe) {
  96. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  97. // transform gets "twisted" such that it maps a straight line of points to a set of points
  98. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  99. // way to detect this about the transformation that I don't know yet.
  100. // This results in an ugly runtime exception despite our clever checks above -- can't have
  101. // that. We could check each point's coordinates but that feels duplicative. We settle for
  102. // catching and wrapping ArrayIndexOutOfBoundsException.
  103. throw "Error.checkAndNudgePoints";
  104. }
  105. }
  106. return bits;
  107. };
  108. export default GridSampler;