index.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _utils = require("@jimp/utils");
  9. var _default = function _default() {
  10. return {
  11. /**
  12. * Blits a source image on to this image
  13. * @param {Jimp} src the source Jimp instance
  14. * @param {number} x the x position to blit the image
  15. * @param {number} y the y position to blit the image
  16. * @param {number} srcx (optional) the x position from which to crop the source image
  17. * @param {number} srcy (optional) the y position from which to crop the source image
  18. * @param {number} srcw (optional) the width to which to crop the source image
  19. * @param {number} srch (optional) the height to which to crop the source image
  20. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  21. * @returns {Jimp} this for chaining of methods
  22. */
  23. blit: function blit(src, x, y, srcx, srcy, srcw, srch, cb) {
  24. if (!(src instanceof this.constructor)) {
  25. return _utils.throwError.call(this, 'The source must be a Jimp image', cb);
  26. }
  27. if (typeof x !== 'number' || typeof y !== 'number') {
  28. return _utils.throwError.call(this, 'x and y must be numbers', cb);
  29. }
  30. if (typeof srcx === 'function') {
  31. cb = srcx;
  32. srcx = 0;
  33. srcy = 0;
  34. srcw = src.bitmap.width;
  35. srch = src.bitmap.height;
  36. } else if ((0, _typeof2["default"])(srcx) === (0, _typeof2["default"])(srcy) && (0, _typeof2["default"])(srcy) === (0, _typeof2["default"])(srcw) && (0, _typeof2["default"])(srcw) === (0, _typeof2["default"])(srch)) {
  37. srcx = srcx || 0;
  38. srcy = srcy || 0;
  39. srcw = srcw || src.bitmap.width;
  40. srch = srch || src.bitmap.height;
  41. } else {
  42. return _utils.throwError.call(this, 'srcx, srcy, srcw, srch must be numbers', cb);
  43. } // round input
  44. x = Math.round(x);
  45. y = Math.round(y); // round input
  46. srcx = Math.round(srcx);
  47. srcy = Math.round(srcy);
  48. srcw = Math.round(srcw);
  49. srch = Math.round(srch);
  50. var maxWidth = this.bitmap.width;
  51. var maxHeight = this.bitmap.height;
  52. var baseImage = this;
  53. src.scanQuiet(srcx, srcy, srcw, srch, function (sx, sy, idx) {
  54. var xOffset = x + sx - srcx;
  55. var yOffset = y + sy - srcy;
  56. if (xOffset >= 0 && yOffset >= 0 && maxWidth - xOffset > 0 && maxHeight - yOffset > 0) {
  57. var dstIdx = baseImage.getPixelIndex(xOffset, yOffset);
  58. var _src = {
  59. r: this.bitmap.data[idx],
  60. g: this.bitmap.data[idx + 1],
  61. b: this.bitmap.data[idx + 2],
  62. a: this.bitmap.data[idx + 3]
  63. };
  64. var dst = {
  65. r: baseImage.bitmap.data[dstIdx],
  66. g: baseImage.bitmap.data[dstIdx + 1],
  67. b: baseImage.bitmap.data[dstIdx + 2],
  68. a: baseImage.bitmap.data[dstIdx + 3]
  69. };
  70. baseImage.bitmap.data[dstIdx] = (_src.a * (_src.r - dst.r) - dst.r + 255 >> 8) + dst.r;
  71. baseImage.bitmap.data[dstIdx + 1] = (_src.a * (_src.g - dst.g) - dst.g + 255 >> 8) + dst.g;
  72. baseImage.bitmap.data[dstIdx + 2] = (_src.a * (_src.b - dst.b) - dst.b + 255 >> 8) + dst.b;
  73. baseImage.bitmap.data[dstIdx + 3] = this.constructor.limit255(dst.a + _src.a);
  74. }
  75. });
  76. if ((0, _utils.isNodePattern)(cb)) {
  77. cb.call(this, null, this);
  78. }
  79. return this;
  80. }
  81. };
  82. };
  83. exports["default"] = _default;
  84. //# sourceMappingURL=index.js.map