index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.isNodePattern = isNodePattern;
  7. exports.throwError = throwError;
  8. exports.scan = scan;
  9. exports.scanIterator = scanIterator;
  10. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  11. require("regenerator-runtime/runtime");
  12. var _marked =
  13. /*#__PURE__*/
  14. _regenerator["default"].mark(scanIterator);
  15. function isNodePattern(cb) {
  16. if (typeof cb === 'undefined') {
  17. return false;
  18. }
  19. if (typeof cb !== 'function') {
  20. throw new TypeError('Callback must be a function');
  21. }
  22. return true;
  23. }
  24. function throwError(error, cb) {
  25. if (typeof error === 'string') {
  26. error = new Error(error);
  27. }
  28. if (typeof cb === 'function') {
  29. return cb.call(this, error);
  30. }
  31. throw error;
  32. }
  33. function scan(image, x, y, w, h, f) {
  34. // round input
  35. x = Math.round(x);
  36. y = Math.round(y);
  37. w = Math.round(w);
  38. h = Math.round(h);
  39. for (var _y = y; _y < y + h; _y++) {
  40. for (var _x = x; _x < x + w; _x++) {
  41. var idx = image.bitmap.width * _y + _x << 2;
  42. f.call(image, _x, _y, idx);
  43. }
  44. }
  45. return image;
  46. }
  47. function scanIterator(image, x, y, w, h) {
  48. var _y, _x, idx;
  49. return _regenerator["default"].wrap(function scanIterator$(_context) {
  50. while (1) {
  51. switch (_context.prev = _context.next) {
  52. case 0:
  53. // round input
  54. x = Math.round(x);
  55. y = Math.round(y);
  56. w = Math.round(w);
  57. h = Math.round(h);
  58. _y = y;
  59. case 5:
  60. if (!(_y < y + h)) {
  61. _context.next = 17;
  62. break;
  63. }
  64. _x = x;
  65. case 7:
  66. if (!(_x < x + w)) {
  67. _context.next = 14;
  68. break;
  69. }
  70. idx = image.bitmap.width * _y + _x << 2;
  71. _context.next = 11;
  72. return {
  73. x: _x,
  74. y: _y,
  75. idx: idx,
  76. image: image
  77. };
  78. case 11:
  79. _x++;
  80. _context.next = 7;
  81. break;
  82. case 14:
  83. _y++;
  84. _context.next = 5;
  85. break;
  86. case 17:
  87. case "end":
  88. return _context.stop();
  89. }
  90. }
  91. }, _marked);
  92. }
  93. //# sourceMappingURL=index.js.map