index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export function isNodePattern(cb) {
  2. if (typeof cb === 'undefined') {
  3. return false;
  4. }
  5. if (typeof cb !== 'function') {
  6. throw new TypeError('Callback must be a function');
  7. }
  8. return true;
  9. }
  10. export function throwError(error, cb) {
  11. if (typeof error === 'string') {
  12. error = new Error(error);
  13. }
  14. if (typeof cb === 'function') {
  15. return cb.call(this, error);
  16. }
  17. throw error;
  18. }
  19. export function scan(image, x, y, w, h, f) {
  20. // round input
  21. x = Math.round(x);
  22. y = Math.round(y);
  23. w = Math.round(w);
  24. h = Math.round(h);
  25. for (let _y = y; _y < y + h; _y++) {
  26. for (let _x = x; _x < x + w; _x++) {
  27. const idx = (image.bitmap.width * _y + _x) << 2;
  28. f.call(image, _x, _y, idx);
  29. }
  30. }
  31. return image;
  32. }
  33. export function* scanIterator(image, x, y, w, h) {
  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 (let _y = y; _y < y + h; _y++) {
  40. for (let _x = x; _x < x + w; _x++) {
  41. const idx = (image.bitmap.width * _y + _x) << 2;
  42. yield { x: _x, y: _y, idx, image };
  43. }
  44. }
  45. }