index.js 2.2 KB

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