index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = composite;
  7. var _utils = require("@jimp/utils");
  8. var constants = _interopRequireWildcard(require("../constants"));
  9. var compositeModes = _interopRequireWildcard(require("./composite-modes"));
  10. /**
  11. * Composites a source image over to this image respecting alpha channels
  12. * @param {Jimp} src the source Jimp instance
  13. * @param {number} x the x position to blit the image
  14. * @param {number} y the y position to blit the image
  15. * @param {object} options determine what mode to use
  16. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  17. * @returns {Jimp} this for chaining of methods
  18. */
  19. function composite(src, x, y) {
  20. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  21. var cb = arguments.length > 4 ? arguments[4] : undefined;
  22. if (typeof options === 'function') {
  23. cb = options;
  24. options = {};
  25. }
  26. if (!(src instanceof this.constructor)) {
  27. return _utils.throwError.call(this, 'The source must be a Jimp image', cb);
  28. }
  29. if (typeof x !== 'number' || typeof y !== 'number') {
  30. return _utils.throwError.call(this, 'x and y must be numbers', cb);
  31. }
  32. var _options = options,
  33. mode = _options.mode,
  34. opacitySource = _options.opacitySource,
  35. opacityDest = _options.opacityDest;
  36. if (!mode) {
  37. mode = constants.BLEND_SOURCE_OVER;
  38. }
  39. if (typeof opacitySource !== 'number' || opacitySource < 0 || opacitySource > 1) {
  40. opacitySource = 1.0;
  41. }
  42. if (typeof opacityDest !== 'number' || opacityDest < 0 || opacityDest > 1) {
  43. opacityDest = 1.0;
  44. }
  45. var blendmode = compositeModes[mode]; // round input
  46. x = Math.round(x);
  47. y = Math.round(y);
  48. var baseImage = this;
  49. if (opacityDest !== 1.0) {
  50. baseImage.opacity(opacityDest);
  51. }
  52. src.scanQuiet(0, 0, src.bitmap.width, src.bitmap.height, function (sx, sy, idx) {
  53. var dstIdx = baseImage.getPixelIndex(x + sx, y + sy, constants.EDGE_CROP);
  54. var blended = blendmode({
  55. r: this.bitmap.data[idx + 0] / 255,
  56. g: this.bitmap.data[idx + 1] / 255,
  57. b: this.bitmap.data[idx + 2] / 255,
  58. a: this.bitmap.data[idx + 3] / 255
  59. }, {
  60. r: baseImage.bitmap.data[dstIdx + 0] / 255,
  61. g: baseImage.bitmap.data[dstIdx + 1] / 255,
  62. b: baseImage.bitmap.data[dstIdx + 2] / 255,
  63. a: baseImage.bitmap.data[dstIdx + 3] / 255
  64. }, opacitySource);
  65. baseImage.bitmap.data[dstIdx + 0] = this.constructor.limit255(blended.r * 255);
  66. baseImage.bitmap.data[dstIdx + 1] = this.constructor.limit255(blended.g * 255);
  67. baseImage.bitmap.data[dstIdx + 2] = this.constructor.limit255(blended.b * 255);
  68. baseImage.bitmap.data[dstIdx + 3] = this.constructor.limit255(blended.a * 255);
  69. });
  70. if ((0, _utils.isNodePattern)(cb)) {
  71. cb.call(this, null, this);
  72. }
  73. return this;
  74. }
  75. module.exports = exports.default;
  76. //# sourceMappingURL=index.js.map